View source
<?php
ctools_include('plugins');
module_load_include('inc', 'eck', 'eck.classes');
module_load_include('inc', 'eck', 'eck.entity_type');
module_load_include('inc', 'eck', 'eck.bundle');
module_load_include('inc', 'eck', 'eck.entity');
module_load_include('inc', 'eck', 'eck.default_properties');
module_load_include('inc', 'eck', 'eck.properties');
module_load_include('inc', 'eck', 'eck.property_behavior');
module_load_include('inc', 'eck', 'includes/eck.references_dialog');
function eckentity_load($id, $type) {
try {
$entity = entity_load_single($type, $id);
} catch (Exception $ex) {
drupal_not_found();
exit;
}
return $entity;
}
function eck_views_api() {
return array(
'api' => '3',
'path' => drupal_get_path('module', 'eck') . '/views',
);
}
function eck_flush_caches() {
EntityType::loadAll(TRUE);
Bundle::loadAll(TRUE);
}
function eck_eck_default_properties() {
$default_properties = array();
$default_properties['title'] = array(
'label' => "Title",
'type' => "text",
'behavior' => 'title',
);
$default_properties['uid'] = array(
'label' => "Author",
'type' => "integer",
'behavior' => 'author',
);
$default_properties['created'] = array(
'label' => "Created",
'type' => "integer",
'behavior' => 'created',
);
$default_properties['changed'] = array(
'label' => "Changed",
'type' => "integer",
'behavior' => 'changed',
);
$default_properties['language'] = array(
'label' => "Entity language",
'type' => "language",
'behavior' => 'language',
);
if (module_exists('uuid')) {
$default_properties['uuid'] = array(
'label' => "UUID",
'type' => "text",
'behavior' => 'uuid',
);
}
return $default_properties;
}
function eck_ctools_plugin_directory($owner, $plugin_type) {
if ($owner == 'eck' && $plugin_type == 'property_behavior') {
return 'plugins/' . $plugin_type;
}
}
function eck_ctools_plugin_type() {
return array(
'property_behavior' => array(),
);
}
function eck__entity__label($entity) {
$hook_names = array(
"eck_entity_label",
"eck_entity_{$entity->entityType()}_label",
"eck_entity_{$entity->entityType()}_{$entity->bundle()}_label",
);
$entity_id = entity_id($entity
->entityType(), $entity);
foreach ($hook_names as $hook_name) {
$new_label = module_invoke_all($hook_name, $entity, $entity_id);
$empty = empty($new_label);
if (!$empty) {
break;
}
}
if (!$empty) {
return $new_label[0];
}
else {
return $entity_id;
}
}
function eck__entity__uri($entity) {
$ids = entity_extract_ids($entity
->entityType(), $entity);
module_load_include('inc', 'eck', 'eck.entity');
$crud_info = get_bundle_crud_info($entity
->entityType(), $entity
->bundle());
$view_path = str_replace('%eckentity', $ids[0], $crud_info['view']['path']);
return array(
'path' => $view_path,
);
}
function eck_schema_alter(&$schema) {
if (db_table_exists('eck_entity_type')) {
foreach (EntityType::loadAll() as $entity_type) {
$schema = array_merge($schema, array(
"eck_{$entity_type->name}" => eck__entity_type__schema($entity_type),
));
$vars = array(
'entity_type' => $entity_type,
'schema' => $schema,
);
$vars = eck_property_behavior_invoke_plugin_alter($entity_type, 'schema', $vars);
if (array_key_exists('schema', $vars) && isset($vars['schema'])) {
$schema = array_merge($schema, $vars['schema']);
}
}
}
}
function entity_table($entities, $select = FALSE) {
module_load_include('inc', 'eck', 'eck.entity');
$crud_info = NULL;
$rows = array();
$header = array(
t('Name'),
array(
'data' => t('Operations'),
'colspan' => '1',
),
);
$info = NULL;
foreach ($entities as $entity) {
$info = array();
$entity_type = $entity
->entityType();
$bundle = $entity
->bundle();
$id = entity_id($entity_type, $entity);
if ($crud_info == NULL) {
$crud_info = get_bundle_crud_info($entity_type, $bundle);
}
$allowed_operations = '';
$destination = drupal_get_destination();
if (eck__entity_menu_access('edit', $entity_type, $bundle, $id)) {
$edit_path = str_replace('%eckentity', $id, $crud_info['edit']['path']);
$allowed_operations = l(t('edit'), $edit_path, array(
'query' => $destination,
));
}
if (eck__entity_menu_access('delete', $entity_type, $bundle, $id)) {
$delete_path = str_replace('%eckentity', $id, $crud_info['delete']['path']);
$allowed_operations .= ($allowed_operations ? '<br />' : '') . l(t('delete'), $delete_path, array(
'query' => $destination,
));
}
if (eck__entity_menu_access('view', $entity_type, $bundle, $id)) {
$uri = entity_uri($entity_type, $entity);
$row = array(
l(entity_label($entity_type, $entity), $uri['path'], $uri['options']),
);
}
else {
$row = array(
entity_label($entity_type, $entity),
);
}
$row[] = array(
'data' => $allowed_operations,
);
$info['entity'] = $entity;
drupal_alter("entity_{$entity_type}_{$bundle}_tr", $row, $info);
$info['bundle'] = $bundle;
drupal_alter("entity_{$entity_type}_tr", $row, $info);
$info['entity_type'] = $entity_type;
drupal_alter("entity_tr", $row, $info);
$rows[$id] = $row;
}
if ($info) {
unset($info['entity']);
drupal_alter("entity_th", $header, $info);
unset($info['entity_type']);
drupal_alter("entity_{$entity_type}_th", $header, $info);
unset($info['bundle']);
drupal_alter("entity_{$entity_type}_{$bundle}_th", $header, $info);
}
if ($select) {
if (!isset($entity_type)) {
return array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
);
}
else {
return drupal_get_form("entity_table_select_{$entity_type}_{$bundle}", $entity_type, $bundle, $header, $rows);
}
}
else {
return array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
);
}
}
function eck_entity_info() {
module_load_include('inc', 'eck', 'eck.entity_type');
$info = array();
foreach (EntityType::loadAll() as $entity_type) {
$info = array_merge($info, eck__entity_type__info($entity_type));
}
return $info;
}
function eck_entity_info_alter(&$info) {
foreach (EntityType::loadAll() as $entity_type) {
$entity_type_info = $info[$entity_type->name];
$entity_type_info = eck_property_behavior_invoke_plugin_alter($entity_type, 'entity_info', $entity_type_info);
if ($entity_type_info) {
$info[$entity_type->name] = $entity_type_info;
}
}
}
function eck_field_extra_fields() {
$extra = array();
foreach (EntityType::loadAll() as $entity_type) {
foreach (Bundle::loadByEntityType($entity_type) as $bundle) {
foreach ($entity_type->properties as $property_name => $property_info) {
if (!empty($bundle->config['managed_properties'][$property_name])) {
$extra[$entity_type->name][$bundle->name]['form'][$property_name] = array(
'label' => $property_info['label'],
'description' => t('Entity property'),
'weight' => 0,
);
$extra[$entity_type->name][$bundle->name]['display'][$property_name] = array(
'label' => $property_info['label'],
'description' => t('Entity property'),
'weight' => 0,
);
}
}
}
}
return $extra;
}
function entity_type_load($entity_type_name) {
return EntityType::loadByName($entity_type_name);
}
function bundle_load($entity_type_name, $bundle_name) {
return Bundle::loadByMachineName("{$entity_type_name}_{$bundle_name}");
}
function _eck_fake_exists() {
return FALSE;
}
function eck_entity_property_info() {
module_load_include('inc', 'entity', 'entity.info');
$info = array();
foreach (EntityType::loadAll() as $entity_type) {
$properties = $entity_type->properties;
$stuff = entity_metadata_convert_schema("eck_{$entity_type->name}");
foreach ($stuff as $key => $property) {
if (isset($properties[$key])) {
$property['label'] = $properties[$key]['label'];
}
$property['setter callback'] = "entity_property_verbatim_set";
$property['getter callback'] = 'entity_property_verbatim_get';
$property['description'] = $property['label'];
drupal_alter("entity_property_{$key}_info", $property);
drupal_alter("entity_property_{$entity_type->name}_{$key}_info", $property);
if ($key == 'type') {
$property['label'] = t('!entity_type type', array(
'!entity_type' => $entity_type->name,
));
$property['type'] = 'token';
$property['description'] = t('The type of this :entity_type entity.', array(
':entity_type' => $entity_type->name,
));
$property['options list'] = 'EntityDefaultMetadataController::bundleOptionsList';
$property['required'] = TRUE;
}
$stuff[$key] = $property;
}
$info[$entity_type->name]['properties'] = $stuff;
}
return $info;
}
function eck_entity_property_info_alter(&$info) {
foreach (EntityType::loadAll() as $entity_type) {
$entity_property_info = $info[$entity_type->name];
$entity_property_info = eck_property_behavior_invoke_plugin_alter($entity_type, 'property_info', $entity_property_info);
foreach ($entity_type->properties as $property => $stuff) {
foreach (array(
'setter',
'getter',
'validation',
) as $function_name) {
if (eck_property_behavior_implements($entity_type, $property, $function_name)) {
$entity_property_info['properties'][$property]["{$function_name} callback"] = "eck_property_behavior_{$function_name}";
}
}
}
if ($entity_property_info) {
$info[$entity_type->name] = $entity_property_info;
}
}
}
function eck_features_api() {
return array(
'eck_entity_type' => array(
'name' => t('Entity Types'),
'feature_source' => TRUE,
'file' => drupal_get_path('module', 'eck') . '/eck.features.inc',
'default_hook' => 'eck_entity_type_info',
),
'eck_bundle' => array(
'name' => t('Bundles'),
'feature_source' => TRUE,
'file' => drupal_get_path('module', 'eck') . '/eck.features.inc',
'default_hook' => 'eck_bundle_info',
),
);
}
function eck_menu() {
$menu = array();
module_load_include('inc', 'eck', 'eck.entity_type');
$menu = array_merge(eck__entity_type__menu(), $menu);
return $menu;
}
function eck_permission() {
module_load_include('inc', 'eck', 'eck.entity_type');
module_load_include('inc', 'eck', 'eck.bundle');
$perms = array(
'eck administer entity types' => array(
'title' => t('Administer Entity Types'),
'restrict access' => TRUE,
'description' => t('Grants the ability to administer(add/edit/delete/view) any ECK Entity Type.'),
),
'eck add entity types' => array(
'title' => t('Add Entity Types'),
'restrict access' => TRUE,
),
'eck edit entity types' => array(
'title' => t('Edit Entity Types'),
'restrict access' => TRUE,
),
'eck delete entity types' => array(
'title' => t('Delete Entity Types'),
'restrict access' => TRUE,
),
'eck list entity types' => array(
'title' => t('View Entity Type List'),
),
'eck administer bundles' => array(
'title' => t('Administer Bundles'),
'restrict access' => TRUE,
'description' => t('Grants the ability to administer(add/edit/delete/list) any ECK Bundle.'),
),
'eck add bundles' => array(
'title' => t('Add Bundles'),
'restrict access' => TRUE,
'description' => t('Grants the ability to add new bundles to any Entity type.'),
),
'eck edit bundles' => array(
'title' => t('Edit Bundles'),
'restrict access' => TRUE,
),
'eck delete bundles' => array(
'title' => t('Delete Bundles'),
'restrict access' => TRUE,
),
'eck list bundles' => array(
'title' => t('View Bundle Lists'),
),
'eck administer entities' => array(
'title' => t('Administer Entities'),
'restrict access' => TRUE,
),
'eck add entities' => array(
'title' => t('Add Entities'),
'restrict access' => TRUE,
'description' => t('Grants the ability to add new entities of any Entity type.'),
),
'eck edit entities' => array(
'title' => t('Edit Any Entity'),
'restrict access' => TRUE,
),
'eck delete entities' => array(
'title' => t('Delete Any Entity'),
'restrict access' => TRUE,
),
'eck view entities' => array(
'title' => t('View Any Entity'),
),
'eck list entities' => array(
'title' => t('View Entity Lists'),
'description' => t('Grants the ability to view the list of entities displayed on any bundle page.'),
),
);
$perms_info = array(
'eck administer' => 'Administer',
'eck add' => "Add",
'eck edit' => 'Edit',
'eck delete' => 'Delete',
'eck list' => 'View List of',
'eck view' => 'View',
);
foreach (EntityType::loadAll() as $entity_type) {
$perms["manage {$entity_type->name} properties"] = array(
'title' => "Manage {$entity_type->label} Properties",
);
foreach ($perms_info as $op => $op_label) {
$perms["{$op} {$entity_type->name} bundles"] = array(
'title' => "{$op_label} {$entity_type->label} Bundles",
);
}
foreach (Bundle::loadByEntityType($entity_type) as $bundle) {
$entity_perm_info = $perms_info + array(
'eck view' => "View",
);
foreach ($entity_perm_info as $op => $op_label) {
$perm_name = "{$op} {$entity_type->name} {$bundle->name} entities";
$perms[$perm_name] = array(
'title' => "{$op_label} {$entity_type->label} {$bundle->label} Entities",
);
}
}
}
return $perms;
}
function eck__multiple_access_check($perms, $b_own = FALSE, $account = NULL) {
if (!isset($account)) {
$account = $GLOBALS['user'];
}
foreach ($perms as $perm) {
if (user_access($perm, $account)) {
return TRUE;
}
}
if (!$b_own) {
return FALSE;
}
foreach ($perms as $perm) {
if (user_access($perm . ' own', $account)) {
return TRUE;
}
}
return FALSE;
}
function eck_forms($form_id, $args) {
$forms = array();
if (strpos($form_id, 'entity_table_select_') === 0) {
$forms[$form_id] = array(
'callback' => 'entity_table_select',
);
}
elseif (strpos($form_id, 'eck__entity__form_') === 0) {
$forms[$form_id] = array(
'callback' => 'eck__entity__form',
);
}
return $forms;
}
function entity_table_select($form, &$state, $entity_type, $bundle, $header, $rows) {
$form['entity_type'] = array(
'#type' => 'value',
'#value' => $entity_type,
);
$form['bundle'] = array(
'#type' => 'value',
'#value' => $bundle,
);
$form['entity_table'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $rows,
);
return $form;
}
class ECKEntity extends Entity {
}
function eck_entity_presave($entity, $entity_type) {
$entity_type = EntityType::loadByName($entity_type);
if ($entity_type) {
eck_property_behavior_invoke_plugin($entity_type, 'entity_save', array(
'entity' => $entity,
));
}
}
function eck_entity_insert($entity, $entity_type) {
$entity_type = EntityType::loadByName($entity_type);
if ($entity_type) {
eck_property_behavior_invoke_plugin($entity_type, 'entity_insert', array(
'entity' => $entity,
));
}
}
function eck_entity_update($entity, $entity_type) {
$entity_type = EntityType::loadByName($entity_type);
if ($entity_type) {
eck_property_behavior_invoke_plugin($entity_type, 'entity_update', array(
'entity' => $entity,
));
}
}
function eck_entity_delete($entity, $entity_type) {
$entity_type = EntityType::loadByName($entity_type);
if ($entity_type) {
eck_property_behavior_invoke_plugin($entity_type, 'entity_delete', array(
'entity' => $entity,
));
}
}
function eck__entity_menu_access($action, $entity_type_name, $bundle_name, $entity_id = NULL) {
if (isset($entity_id)) {
if (is_object($entity_id) and $entity_id
->entityType() == $entity_type_name) {
$entity_or_bundle = $entity_id;
}
else {
$entity_or_bundle = entity_load_single($entity_type_name, $entity_id);
}
if (empty($entity_or_bundle)) {
return FALSE;
}
}
else {
$entity_or_bundle = $bundle_name;
}
$crud_map = array(
'add' => 'create',
'edit' => 'update',
);
if (isset($crud_map[$action])) {
$action = $crud_map[$action];
}
return entity_access($action, $entity_type_name, $entity_or_bundle);
}
function eck__entity_access($op, $entity_or_bundle, $account, $entity_type_name) {
$crud_map = array(
'create' => 'add',
'update' => 'edit',
);
if (isset($crud_map[$op])) {
$op = $crud_map[$op];
}
$permissions = array(
'eck administer entities',
"eck {$op} entities",
);
if (isset($entity_or_bundle)) {
if (is_object($entity_or_bundle)) {
list(, , $bundle_name) = entity_extract_ids($entity_type_name, $entity_or_bundle);
}
else {
$bundle_name = $entity_or_bundle;
}
$permissions[] = "eck administer {$entity_type_name} {$bundle_name} entities";
$permissions[] = "eck {$op} {$entity_type_name} {$bundle_name} entities";
}
return eck__multiple_access_check($permissions, FALSE, $account);
}
function eck_entity_view_alter(&$build, $entity_type) {
if (!module_exists('contextual') || !user_access('access contextual links')) {
return;
}
$entity_types = EntityType::loadAll();
foreach ($entity_types as $et) {
if ($et->name == $entity_type) {
$entity = $build['#entity'];
$this_bundle = $entity
->bundle();
$build['#contextual_links']['eck'] = array(
"{$entity_type}/{$this_bundle}",
array(
entity_id($entity_type, $entity),
),
);
}
}
}
function eck_alphabetical_cmp($a, $b) {
return strcasecmp($a->name, $b->name);
}
function eck_init() {
global $_eck_entity_types_cache;
global $_eck_bundles_cache;
$_eck_entity_types_cache = new ECKCache("entity_types");
$_eck_bundles_cache = new ECKCache("bundles");
eck_clean_up_init();
}
function eck_clean_up_request() {
variable_set("eck_clear_caches", TRUE);
}
function eck_clean_up_init() {
if (variable_get("eck_clear_caches", FALSE)) {
variable_set("eck_clear_caches", FALSE);
eck_clean_up();
}
}
function eck_clean_up() {
drupal_get_schema(NULL, TRUE);
entity_info_cache_clear();
entity_flush_caches();
menu_rebuild();
}
function eck_system_info_alter(&$info, $file, $type) {
if ($type == "module" && array_key_exists("name", $info) && $info['name'] == "Entity Construction Kit") {
$done = FALSE;
$entity_types = EntityType::loadAll();
if (!empty($entity_types)) {
$info['required'] = TRUE;
$info['explanation'] = "Entity types created by ECK are still present in the system.";
$done = TRUE;
}
if (!$done) {
$query = db_select("system", "s");
$query
->fields('s', array(
'schema_version',
));
$query
->condition('s.name', 'eck_entitycache', '=');
$query
->condition('s.type', 'module', '=');
$result = $query
->execute();
foreach ($result as $row) {
$schema_version = $row->schema_version;
if ($schema_version == 0) {
$info['required'] = TRUE;
$info['explanation'] = "ECK EntityCache must be uninstalled before you can disable/uninstall ECK";
}
}
}
}
}
function eck_bundle_copy_info() {
$entity_info = array();
foreach (EntityType::loadAll() as $k => $entity_type) {
$entity_info[$k] = array(
'bundle_export_callback' => 'eck_bundle_copy_get_type',
'bundle_save_callback' => 'eck_bundle_copy_save',
'export_menu' => array(
'path' => "admin/structure/entity-type/{$k}/export",
'access arguments' => "manage {$k} properties",
),
'import_menu' => array(
'path' => "admin/structure/entity-type/{$k}/import",
'access arguments' => "manage {$k} properties",
),
);
}
return $entity_info;
}
function eck_bundle_copy_get_type() {
}
function eck_bundle_copy_save() {
}