annotate sites/all/modules/ctools/plugins/access/entity_bundle.inc @ 9:830c812b520f

added smtp module
author root <root@paio.local>
date Mon, 28 Oct 2013 15:34:27 +0000
parents ff03f76ab3fe
children
rev   line source
danielebarchiesi@0 1 <?php
danielebarchiesi@0 2
danielebarchiesi@0 3 /**
danielebarchiesi@0 4 * @file
danielebarchiesi@0 5 * Plugin to provide access control based upon entity bundle.
danielebarchiesi@0 6 */
danielebarchiesi@0 7
danielebarchiesi@0 8 /**
danielebarchiesi@0 9 * Plugins are described by creating a $plugin array which will be used
danielebarchiesi@0 10 * by the system that includes this file.
danielebarchiesi@0 11 */
danielebarchiesi@0 12 $plugin = array(
danielebarchiesi@0 13 'title' => t("Entity: bundle"),
danielebarchiesi@0 14 'description' => t('Control access by entity bundle.'),
danielebarchiesi@0 15 'callback' => 'ctools_entity_bundle_ctools_access_check',
danielebarchiesi@0 16 'default' => array('type' => array()),
danielebarchiesi@0 17 'settings form' => 'ctools_entity_bundle_ctools_access_settings',
danielebarchiesi@0 18 'settings form submit' => 'ctools_entity_bundle_ctools_access_settings_submit',
danielebarchiesi@0 19 'summary' => 'ctools_entity_bundle_ctools_access_summary',
danielebarchiesi@0 20 'restrictions' => 'ctools_entity_bundle_ctools_access_restrictions',
danielebarchiesi@0 21 'get child' => 'ctools_entity_bundle_ctools_access_get_child',
danielebarchiesi@0 22 'get children' => 'ctools_entity_bundle_ctools_access_get_children',
danielebarchiesi@0 23 );
danielebarchiesi@0 24
danielebarchiesi@0 25 function ctools_entity_bundle_ctools_access_get_child($plugin, $parent, $child) {
danielebarchiesi@0 26 $plugins = ctools_entity_bundle_ctools_access_get_children($plugin, $parent);
danielebarchiesi@0 27 return $plugins[$parent . ':' . $child];
danielebarchiesi@0 28 }
danielebarchiesi@0 29
danielebarchiesi@0 30 function ctools_entity_bundle_ctools_access_get_children($plugin, $parent) {
danielebarchiesi@0 31 $entities = entity_get_info();
danielebarchiesi@0 32 $plugins = array();
danielebarchiesi@0 33 foreach ($entities as $entity_type => $entity) {
danielebarchiesi@0 34 $plugin['title'] = t('@entity: Bundle', array('@entity' => $entity['label']));
danielebarchiesi@0 35 $plugin['keyword'] = $entity_type;
danielebarchiesi@0 36 $plugin['description'] = t('Control access by @entity entity bundle.', array('@entity' => $entity_type));
danielebarchiesi@0 37 $plugin['name'] = $parent . ':' . $entity_type;
danielebarchiesi@0 38 $plugin['required context'] = new ctools_context_required(t(ucfirst($entity_type)), $entity_type);
danielebarchiesi@0 39 $plugins[$parent . ':' . $entity_type] = $plugin;
danielebarchiesi@0 40 }
danielebarchiesi@0 41
danielebarchiesi@0 42 return $plugins;
danielebarchiesi@0 43 }
danielebarchiesi@0 44
danielebarchiesi@0 45 /**
danielebarchiesi@0 46 * Settings form for the 'by entity_bundle' access plugin
danielebarchiesi@0 47 */
danielebarchiesi@0 48 function ctools_entity_bundle_ctools_access_settings($form, &$form_state, $conf) {
danielebarchiesi@0 49 $plugin = $form_state['plugin'];
danielebarchiesi@0 50 $entity_type = explode(':', $plugin['name']);
danielebarchiesi@0 51 $entity_type = $entity_type[1];
danielebarchiesi@0 52 $entity = entity_get_info($entity_type);
danielebarchiesi@0 53 foreach ($entity['bundles'] as $type => $info) {
danielebarchiesi@0 54 $options[$type] = check_plain($info['label']);
danielebarchiesi@0 55 }
danielebarchiesi@0 56
danielebarchiesi@0 57 $form['settings']['type'] = array(
danielebarchiesi@0 58 '#title' => t('Entity Bundle'),
danielebarchiesi@0 59 '#type' => 'checkboxes',
danielebarchiesi@0 60 '#options' => $options,
danielebarchiesi@0 61 '#description' => t('Only the checked entity bundles will be valid.'),
danielebarchiesi@0 62 '#default_value' => $conf['type'],
danielebarchiesi@0 63 );
danielebarchiesi@0 64 return $form;
danielebarchiesi@0 65 }
danielebarchiesi@0 66
danielebarchiesi@0 67 /**
danielebarchiesi@0 68 * Compress the entity bundles allowed to the minimum.
danielebarchiesi@0 69 */
danielebarchiesi@0 70 function ctools_entity_bundle_ctools_access_settings_submit($form, &$form_state) {
danielebarchiesi@0 71 $form_state['values']['settings']['type'] = array_filter($form_state['values']['settings']['type']);
danielebarchiesi@0 72 }
danielebarchiesi@0 73
danielebarchiesi@0 74 /**
danielebarchiesi@0 75 * Check for access.
danielebarchiesi@0 76 */
danielebarchiesi@0 77 function ctools_entity_bundle_ctools_access_check($conf, $context, $plugin) {
danielebarchiesi@0 78 list($plugin_name, $entity_type) = explode(':', $plugin['name']);
danielebarchiesi@0 79 if (!$entity_type) {
danielebarchiesi@0 80 return FALSE;
danielebarchiesi@0 81 };
danielebarchiesi@0 82
danielebarchiesi@0 83 $entity = entity_get_info($entity_type);
danielebarchiesi@0 84 // As far as I know there should always be a context at this point, but this
danielebarchiesi@0 85 // is safe.
danielebarchiesi@0 86 if (empty($context) || empty($context->data) || empty($context->data->{$entity['entity keys']['bundle']})) {
danielebarchiesi@0 87 return FALSE;
danielebarchiesi@0 88 }
danielebarchiesi@0 89
danielebarchiesi@0 90 if (array_filter($conf['type']) && empty($conf['type'][$context->data->{$entity['entity keys']['bundle']}])) {
danielebarchiesi@0 91 return FALSE;
danielebarchiesi@0 92 }
danielebarchiesi@0 93
danielebarchiesi@0 94 return TRUE;
danielebarchiesi@0 95 }
danielebarchiesi@0 96
danielebarchiesi@0 97 /**
danielebarchiesi@0 98 * Inform the UI that we've eliminated a bunch of possibilities for this
danielebarchiesi@0 99 * context.
danielebarchiesi@0 100 */
danielebarchiesi@0 101 function ctools_entity_bundle_ctools_access_restrictions($conf, &$context) {
danielebarchiesi@0 102 if (isset($context->restrictions['type'])) {
danielebarchiesi@0 103 $context->restrictions['type'] = array_unique(array_merge($context->restrictions['type'], array_keys(array_filter($conf['type']))));
danielebarchiesi@0 104 }
danielebarchiesi@0 105 else {
danielebarchiesi@0 106 $context->restrictions['type'] = array_keys(array_filter($conf['type']));
danielebarchiesi@0 107 }
danielebarchiesi@0 108 }
danielebarchiesi@0 109
danielebarchiesi@0 110 /**
danielebarchiesi@0 111 * Provide a summary description based upon the checked entity_bundle.
danielebarchiesi@0 112 */
danielebarchiesi@0 113 function ctools_entity_bundle_ctools_access_summary($conf, $context, $plugin) {
danielebarchiesi@0 114 if (!isset($conf['type'])) {
danielebarchiesi@0 115 $conf['type'] = array();
danielebarchiesi@0 116 }
danielebarchiesi@0 117
danielebarchiesi@0 118 list($plugin_name, $entity_type) = explode(':', $plugin['name']);
danielebarchiesi@0 119 if (!$entity_type) {
danielebarchiesi@0 120 return t('Error, misconfigured entity_bundle access plugin');
danielebarchiesi@0 121 };
danielebarchiesi@0 122
danielebarchiesi@0 123 $entity = entity_get_info($entity_type);
danielebarchiesi@0 124
danielebarchiesi@0 125 $names = array();
danielebarchiesi@0 126 foreach (array_filter($conf['type']) as $type) {
danielebarchiesi@0 127 $names[] = check_plain($entity['bundles'][$type]['label']);
danielebarchiesi@0 128 }
danielebarchiesi@0 129
danielebarchiesi@0 130 if (empty($names)) {
danielebarchiesi@0 131 return t('@identifier is any bundle', array('@identifier' => $context->identifier));
danielebarchiesi@0 132 }
danielebarchiesi@0 133
danielebarchiesi@0 134 return format_plural(count($names), '@identifier is bundle "@types"', '@identifier bundle is one of "@types"', array('@types' => implode(', ', $names), '@identifier' => $context->identifier));
danielebarchiesi@0 135 }
danielebarchiesi@0 136