Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
482 lines (426 sloc) 11.3 KB
<?php
/**
* @file
* Block Entity
*/
/**
* Implements hook_entity_info()
*/
function bean_entity_info() {
$return = array(
'bean' => array(
'label' => t('Block'),
'entity class' => 'Bean',
'controller class' => 'EntityAPIController',
'base table' => 'bean',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'bid',
'bundle' => 'type',
),
'bundles' => array(),
'bundle keys' => array(
'bundle' => 'type',
),
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'access callback' => 'bean_access',
'module' => 'bean',
'metadata controller class' => 'BeanMetadataController'
),
);
foreach (bean_get_types() as $type) {
$return['bean']['bundles'][$type->type] = array(
'label' => $type->getLabel(),
'admin' => array(
'path' => 'admin/structure/bean/manage/%bean_type',
'real path' => 'admin/structure/bean/manage/' . $type->buildURL(),
'bundle argument' => 4,
'access arguments' => array('administer bean types'),
)
);
}
return $return;
}
/**
* Implements hook_menu().
*/
function bean_menu() {
$items = array();
$items['block/add'] = array(
'title' => 'Add Block',
'page callback' => 'bean_add_page',
'access arguments' => array('add'),
'access callback' => 'bean_access',
'file' => 'includes/bean.pages.inc',
);
foreach (bean_get_types() as $type) {
$items['block/add/' . $type->buildURL()] = array(
'title' => $type->getLabel(),
'title callback' => 'check_plain',
'page callback' => 'bean_add',
'page arguments' => array($type->type),
'access arguments' => array('create any ' . $type->type . ' bean'),
//'description' => $type->description,
'file' => 'includes/bean.pages.inc',
);
}
$items['block/%bean'] = array(
'title' => 'Block',
'page callback' => 'bean_edit',
'page arguments' => array(1),
'access arguments' => array('edit', 1),
'access callback' => 'bean_access',
'file' => 'includes/bean.pages.inc',
);
$items['block/%bean/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
'context' => MENU_CONTEXT_NONE,
'weight' => -20,
);
$items['block/%bean/edit'] = array(
'title' => 'Edit block',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'page callback' => 'bean_edit',
'page arguments' => array(1),
'access arguments' => array('edit', 1),
'access callback' => 'bean_access',
'file' => 'includes/bean.pages.inc',
);
$items['block/%bean/delete'] = array(
'title' => 'Delete',
'page callback' => 'drupal_get_form',
'page arguments' => array('bean_delete_confirm', 1),
'access callback' => 'bean_access',
'access arguments' => array('delete', 1),
'weight' => 1,
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_NONE,
'file' => 'includes/bean.pages.inc',
);
$items['admin/structure/bean'] = array(
'title' => 'Block Type Admin',
'description' => 'Manage Block Type Fields',
'page callback' => 'bean_type_admin_page',
'access arguments' => array('administer bean types'),
'file' => 'includes/bean.admin.inc',
);
return $items;
}
/**
* Title of the bean
*/
function bean_page_title($bean) {
return $bean->label;
}
/**
* Menu Argument Loader
*/
function bean_type_load($type) {
return bean_load_plugin_class(strtr($type, array('-' => '_')));
}
/**
* Gets an array of all bean types, keyed by the type name.
*
* @param $type_name
* If set, the type with the given name is returned.
* @return BeanType[]
* Depending whether $type isset, an array of bean types or a single one.
*/
function bean_get_types() {
//$types = array();
// @TODO Add the table fetching
//$types = entity_load('bean_type', isset($type_name) ? array($type_name) : FALSE);
//return isset($type_name) ? reset($types) : $types;
return bean_load_plugin_class_all();
}
/**
* Fetch the widget plugin info
*/
function bean_fetch_plugin_info($plugin = NULL) {
ctools_include('plugins');
if (empty($plugin)) {
return ctools_get_plugins('bean', 'plugins');
}
else {
return ctools_get_plugins('bean', 'plugins', $plugin);
}
}
/**
* Load a widget class
*
* @param $plugin key - the key fo the plugin
* @return an object from the plugin
*/
function bean_load_plugin_class($plugin_key = NULL) {
$cache = &drupal_static(__FUNCTION__);
if (!isset($cache[$plugin_key])) {
ctools_include('plugins');
$plugin = bean_fetch_plugin_info($plugin_key);
if ($class = ctools_plugin_get_class($plugin, 'handler')) {
if (class_exists($class)) {
$cache[$plugin_key] = new $class($plugin);
}
}
}
return isset($cache[$plugin_key]) ? $cache[$plugin_key] : FALSE;
}
/**
* Load all widget classes
*/
function bean_load_plugin_class_all() {
$return = array();
foreach (bean_fetch_plugin_info() as $plugin) {
if ($plugin['name'] !== 'bean') {
$return[$plugin['name']] = bean_load_plugin_class($plugin['name']);
}
}
return $return;
}
/**
* Load all beans
*/
function bean_get_all_beans() {
return bean_load_multiple(FALSE);
}
/**
* Fetch a bean object.
*
* @param $bid
* Integer specifying the bean id.
* @param $reset
* A boolean indicating that the internal cache should be reset.
* @return
* A fully-loaded $bean object or FALSE if it cannot be loaded.
*
* @see bean_load_multiple()
*/
function bean_load($bid, $reset = FALSE) {
$beans = bean_load_multiple(array($bid), array(), $reset);
return reset($beans);
}
/**
* Load multiple beans based on certain conditions.
*
* @param $bids
* An array of bean IDs.
* @param $conditions
* An array of conditions to match against the {bean} table.
* @param $reset
* A boolean indicating that the internal cache should be reset.
* @return
* An array of bean objects, indexed by pid.
*
* @see entity_load()
* @see bean_load()
*/
function bean_load_multiple($bids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('bean', $bids, $conditions, $reset);
}
/**
* Implements hook_permissions()
*/
function bean_permissions() {
$perms = array(
'administer bean types' => array(
'title' => t('Administer bean types'),
'description' => t('Create and delete fields on beans, and set their permissions.'),
),
'administer beans' => array(
'title' => t('Administer beans'),
'description' => t('Edit and view all beans.'),
),
);
// Add a Permission for each entity type
foreach (bean_get_types() as $bean_type) {
$bean_type_name = check_plain($bean_type->type);
$perms += array(
"create any $bean_type_name bean" => array(
'title' => t('%type_name: add Bean', array('%type_name' => $bean_type->getLabel())),
),
"edit any $bean_type_name bean" => array(
'title' => t('%type_name: Edit Bean', array('%type_name' => $bean_type->getLabel())),
),
"view any $bean_type_name bean" => array(
'title' => t('%type_name: View Bean', array('%type_name' => $bean_type->getLabel())),
),
"delete any $bean_type_name bean" => array(
'title' => t('%type_name: delete Bean', array('%type_name' => $bean_type->getLabel())),
),
);
}
return $perms;
}
/**
* Access callback for the entity API.
*/
function bean_type_access($op, $type = NULL, $account = NULL) {
return user_access('administer bean types', $account);
}
/**
* Determines whether the given user has access to a bean.
*
* @param $op
* The operation being performed. One of 'view', 'update', 'create', 'delete'
* or just 'edit' (being the same as 'create' or 'update').
* @param $bean
* Optionally a bean or a bean type o check access for. If nothing is
* given, access for all beans is determined.
* @param $account
* The user to check for. Leave it to NULL to check for the global user.
* @return boolean
* Whether access is allowed or not.
*/
function bean_access($op, $bean = NULL, $account = NULL) {
if (user_access('administer beans', $account)) {
return TRUE;
}
switch ($op) {
case 'view':
case 'delete':
case 'create':
$op = $op;
break;
case 'add':
$op = 'create';
default:
$op = 'edit';
}
if (isset($bean) && $type_name = $bean->type) {
if (user_access("$op any $type_name bean", $account)) {
return TRUE;
}
}
else {
// Here we are looking for access to any of the types
foreach (bean_get_types() as $bean_type) {
$perm = $op . ' any ' . $bean_type->type . ' bean';
if (user_access($perm)) {
return TRUE;
}
}
}
return FALSE;
}
/**
* Inserts or updates a bean object into the database.
*
* @param $bean
* The bean object to be inserted.
*
* @return
* Failure to write a record will return FALSE. Otherwise SAVED_NEW or
* SAVED_UPDATED is returned depending on the operation performed.
*/
function bean_save($bean) {
return entity_save('bean', $bean);
}
/**
* Deletes an existing bean.
*
* @param $bean
* The message object to be bean.
*/
function bean_delete($bean) {
return entity_delete('bean', $bean);
}
/**
* Helper to easily create a bean.
*
* @param $values
* Array with the following keys:
* - "arguments" - Array with arguments that should be replaced on run time in
* the message type.
*/
function bean_create($values) {
return entity_create('bean', $values);
}
/**
* Implements hook_block_info().
*/
function bean_block_info() {
$blocks = array();
$beans = bean_get_all_beans();
foreach ($beans as $bean) {
$blocks[$bean->bid] = array(
'info' => $bean->label,
'cache' => $bean->getInfo('cache_level'),
);
}
return $blocks;
}
/**
* Implements hook_block_view
*/
function bean_block_view($delta = '') {
$bean = bean_load($delta);
if (bean_access('view', $bean)) {
return array(
'content' => $bean->view(),
'subject' => $bean->title,
);
}
}
/**
* Implements hook_ctools_plugin_type()
*/
function bean_ctools_plugin_type() {
return array(
'plugins' => array(
'cache' => TRUE,
'use hooks' => TRUE,
'classes' => array('handler'),
'defaults' => array(
'label' => t('Bean'),
'cache_level' => DRUPAL_CACHE_PER_ROLE,
),
),
);
}
/**
* Implements hook_bean_plugins()
*/
function bean_bean_plugins() {
$plugins = array();
$plugin_path = drupal_get_path('module', 'bean') . '/plugins';
$plugins['bean'] = array(
'handler' => array(
'path' => $plugin_path,
'file' => 'base.inc',
'class' => 'bean_plugin',
),
);
$plugins['bean_default'] = array(
'label' => t('Bean'),
'handler' => array(
'path' => $plugin_path,
'file' => 'base.inc',
'class' => 'bean_default',
'parent' => 'bean',
),
);
return $plugins;
}
/**
* Implements hook_theme().
*/
function bean_theme() {
return array(
'bean' => array(
'render element' => 'entity',
),
'bean_add_list' => array(
'variables' => array('content' => NULL),
),
);
}
/**
* Implements hook_block_view_alter().
*/
function bean_block_view_alter(&$data, $block) {
if ($block->module == 'bean') {
$data['content']['#contextual_links']['bean'] = array('block', array($block->delta));
}
}