You are here

merci_inventory.module in MERCI (Manage Equipment Reservations, Checkout and Inventory) 7.3

File

merci_inventory/merci_inventory.module
View source
<?php

define('MERCI_INVENTORY_TYPE_PATH', MERCI_ADMIN_CONFIG_PATH . '/merci_inventory');
function merci_inventory_merci_inventory_insert($entity) {
  _merci_inventory_update_statistics($entity);
}
function merci_inventory_merci_inventory_update($entity) {
  _merci_inventory_update_statistics($entity);
}

/**
 * Updates the comment statistics for a given node. This should be called any
 * time a comment is added, deleted, or updated.
 *
 * The following fields are contained in the node_comment_statistics table.
 * - last_comment_timestamp: the timestamp of the last comment for this node or the node create stamp if no comments exist for the node.
 * - last_comment_name: the name of the anonymous poster for the last comment
 * - last_comment_uid: the uid of the poster for the last comment for this node or the node authors uid if no comments exists for the node.
 * - comment_count: the total number of approved/published comments on this node.
 */
function _merci_inventory_update_statistics($entity) {
  $count = db_query('SELECT COUNT(inventory_id) FROM {merci_inventory_statistics} WHERE entity_id = :entity_id', array(
    ':entity_id' => (int) $entity->entity_id,
  ))
    ->fetchField();
  if ($count == 0) {
    db_insert('merci_inventory_statistics')
      ->fields(array(
      'entity_id' => (int) $entity->entity_id,
      'inventory_id' => $entity->inventory_id,
      'inventory_count' => 1,
      'last_inventory_timestamp' => $entity->created,
      'last_inventory_uid' => $entity->uid,
    ))
      ->execute();
  }
  else {
    $count = db_query('SELECT COUNT(inventory_id) FROM {merci_inventory} WHERE entity_id = :entity_id', array(
      ':entity_id' => (int) $entity->entity_id,
    ))
      ->fetchField();
    if ($count > 0) {

      // Comments exist.
      $last_inventory = db_query_range('SELECT inventory_id, created, uid FROM {merci_inventory} WHERE entity_id = :entity_id ORDER BY inventory_id DESC', 0, 1, array(
        ':entity_id' => (int) $entity->entity_id,
      ))
        ->fetchObject();
      db_update('merci_inventory_statistics')
        ->fields(array(
        'inventory_id' => $last_inventory->inventory_id,
        'inventory_count' => $count,
        'last_inventory_timestamp' => $last_inventory->created,
        'last_inventory_uid' => $last_inventory->uid,
      ))
        ->condition('entity_id', (int) $entity->entity_id)
        ->execute();
    }
    else {

      // Comments do not exist.
      db_update('merci_inventory_statistics')
        ->fields(array(
        'inventory_id' => 0,
        'inventory_count' => 0,
        'last_comment_timestamp' => $entity->created,
        'last_comment_uid' => $entity->uid,
      ))
        ->condition('entity_id', (int) $entity->entity_id)
        ->execute();
    }
  }
}
function merci_inventory_views_bulk_operations_form_alter(&$form, &$form_state, $vbo) {
  if ($form_state['step'] == 'views_bulk_operations_config_form' and array_key_exists('parameter', $form)) {
    if (array_key_exists('date', $form['parameter'])) {
    }
    if (array_key_exists('location', $form['parameter'])) {
      $form['parameter']['location']['settings']['location']['#autocomplete_path'] = 'address/autocomplete/homeroom';
      $form['parameter']['location']['settings']['location']['#element_validate'] = array(
        'merci_inventory_autocomplete_validate',
      );
    }
  }
}

/*
 * Strips out profile_id from name addressfield.
 */
function merci_inventory_autocomplete_validate($element, &$form_state, $form) {
  $name = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
  $matches = array();

  // This preg_match() looks for the last pattern like [33334] and if found
  // extracts the numeric portion.
  $result = preg_match('/\\[([0-9]+)\\]$/', $name, $matches);
  if ($result > 0) {
    form_set_value($element, $matches[$result], $form_state);
  }
}

/**
 * Implements hook_entity_info().
 */
function merci_inventory_entity_info() {
  $info['merci_inventory'] = array(
    'label' => t('Merci inventory'),
    'plural label' => t('Merci inventory'),
    'description' => t('An entity type used to inventory MERCI resources.'),
    //'entity_class' => 'MerciInventoryEntity',
    'entity_class' => 'Entity',
    'controller class' => 'EntityAPIController',
    'base table' => 'merci_inventory',
    'fieldable' => TRUE,
    'access callback' => 'merci_inventory_access_callback',
    'entity keys' => array(
      'id' => 'inventory_id',
    ),
    //'label callback' => 'entity_class_label',

    //'uri callback' => 'entity_class_uri',
    'admin ui' => array(
      'path' => MERCI_INVENTORY_TYPE_PATH,
      'controller class' => 'EntityDefaultUIController',
    ),
    'bundles' => array(
      'merci_inventory' => array(
        'label' => t('Merci inventory'),
        // Provide a default administration path for Field UI, but not if 'admin'
        // has been explicitly set to NULL.
        'admin' => array(
          'path' => MERCI_INVENTORY_TYPE_PATH,
        ),
      ),
    ),
    //'exportable' => TRUE,
    'module' => 'merci_inventory',
  );
  return $info;
}
function merci_inventory_entity_property_info() {
  $info = array();

  // Add meta-data about the basic commerce_line_item properties.
  $properties =& $info['merci_inventory']['properties'];
  $properties['entity_id'] = array(
    'label' => t('Resource'),
    'description' => t('The resource being inventoried.'),
    'type' => 'commerce_product',
    'setter callback' => 'entity_metadata_verbatim_set',
    'setter permission' => 'create merci_inventory',
    'schema field' => 'entity_id',
  );
  $properties['uid'] = array(
    'label' => t('User'),
    'description' => t('The user creating the inventory.'),
    'type' => 'user',
    'setter callback' => 'entity_metadata_verbatim_set',
    'setter permission' => 'create merci_inventory',
    //  'entity views field' => TRUE,
    'schema field' => 'uid',
  );
  $properties['created'] = array(
    'label' => t('Date created'),
    'description' => t('The date the inventory was created.'),
    'type' => 'date',
    'setter callback' => 'entity_metadata_verbatim_set',
    'setter permission' => 'create merci_inventory',
    'schema field' => 'created',
  );
  $properties['location_id'] = array(
    'label' => t('Location'),
    'description' => t('The location the inventory is stored.'),
    'type' => 'commerce_customer_profile',
    'setter callback' => 'entity_metadata_verbatim_set',
    'setter permission' => 'create merci_inventory',
    //'entity views field' => TRUE,
    'schema field' => 'location_id',
  );
  return $info;
}

/**
 * Main class for test entities.
 */
class MerciInventoryEntity extends Entity {
  public function __construct(array $values = array(), $entityType = NULL) {
    parent::__construct($values, 'merci_inventory');
  }

  /**
   * Override buildContent() to add the username to the output.
   */
  public function buildContent($view_mode = 'full', $langcode = NULL) {
    $content['user'] = array(
      '#markup' => "User: " . format_username(user_load($this->uid)),
    );
    return entity_get_controller($this->entityType)
      ->buildContent($this, $view_mode, $langcode, $content);
  }

  /**
   * Specifies the default label, which is picked up by label() by default.
   */
  protected function defaultLabel() {
    $type = entity_test_get_types($this->name);
    return $type->label;
  }

  /**
   * Specifies the default uri, which is picked up by uri() by default.
   */
  protected function defaultURI() {
    return array(
      'path' => MERCI_INVENTORY_TYPE_PATH . '/' . $this
        ->identifier(),
    );
  }

}
function merci_inventory_access_callback($op, $entity = NULL, $account = NULL) {
  return user_access($op . ' merci_inventory');
}
function merci_inventory_permission() {
  foreach (array(
    'view',
    'update',
    'create',
    'delete',
    'administer',
  ) as $op) {
    $permissions[$op . ' merci_inventory'] = array(
      'title' => t(ucfirst($op) . ' merci_inventory'),
      'description' => t('Allows users to ' . $op . ' merci_inventory'),
    );
  }
  return $permissions;
}

/**
 *  * Implements hook_views_api().
 *   */
function merci_inventory_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'merci_inventory') . '/views',
  );
}