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);
}
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) {
$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 {
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',
);
}
}
}
function merci_inventory_autocomplete_validate($element, &$form_state, $form) {
$name = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
$matches = array();
$result = preg_match('/\\[([0-9]+)\\]$/', $name, $matches);
if ($result > 0) {
form_set_value($element, $matches[$result], $form_state);
}
}
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' => 'Entity',
'controller class' => 'EntityAPIController',
'base table' => 'merci_inventory',
'fieldable' => TRUE,
'access callback' => 'merci_inventory_access_callback',
'entity keys' => array(
'id' => 'inventory_id',
),
'admin ui' => array(
'path' => MERCI_INVENTORY_TYPE_PATH,
'controller class' => 'EntityDefaultUIController',
),
'bundles' => array(
'merci_inventory' => array(
'label' => t('Merci inventory'),
'admin' => array(
'path' => MERCI_INVENTORY_TYPE_PATH,
),
),
),
'module' => 'merci_inventory',
);
return $info;
}
function merci_inventory_entity_property_info() {
$info = array();
$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',
'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',
'schema field' => 'location_id',
);
return $info;
}
class MerciInventoryEntity extends Entity {
public function __construct(array $values = array(), $entityType = NULL) {
parent::__construct($values, 'merci_inventory');
}
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);
}
protected function defaultLabel() {
$type = entity_test_get_types($this->name);
return $type->label;
}
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;
}
function merci_inventory_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'merci_inventory') . '/views',
);
}