View source
<?php
include_once 'command_buttons.features.inc';
function command_buttons_menu() {
$base = array(
'access arguments' => array(
'administer command buttons',
),
'file' => 'command_buttons.admin.inc',
);
$items['admin/structure/command-buttons'] = array(
'title' => 'Command Buttons',
'description' => 'Manage command button types.',
'page callback' => 'command_buttons_entities_page',
) + $base;
$items['admin/structure/command-buttons/list'] = array(
'title' => 'List',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
) + $base;
$items['admin/structure/command-buttons/manage/%command_buttons_type'] = array(
'title callback' => 'command_buttons_entities_title',
'title arguments' => array(
4,
),
'page callback' => 'command_buttons_entities_list_page',
'page arguments' => array(
4,
),
) + $base;
$items['admin/structure/command-buttons/manage/%command_buttons_type/add'] = array(
'title' => 'Add a command button',
'page callback' => 'command_buttons_entities_add_page',
'page arguments' => array(
4,
),
'type' => MENU_NORMAL_ITEM,
) + $base;
$items['admin/structure/command-buttons/view/%command_buttons'] = array(
'title callback' => 'command_buttons_entity_title',
'title arguments' => array(
4,
),
'page callback' => 'command_buttons_view',
'page arguments' => array(
4,
),
'access callback' => 'command_buttons_access',
'access arguments' => array(
'view',
4,
),
) + $base;
$items['admin/structure/command-buttons/view/%command_buttons/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
'page callback' => 'command_buttons_view',
'page arguments' => array(
4,
),
'access callback' => 'command_buttons_access',
'access arguments' => array(
'view',
4,
),
'weight' => -10,
) + $base;
$items['admin/structure/command-buttons/view/%command_buttons/edit'] = array(
'title' => 'Edit',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
'page callback' => 'command_buttons_entity_edit_page',
'page arguments' => array(
4,
),
'access callback' => 'command_buttons_access',
'access arguments' => array(
'update',
4,
),
'weight' => -9,
) + $base;
$items['admin/structure/command-buttons/view/%command_buttons/delete'] = array(
'title' => 'Delete',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
'page callback' => 'drupal_get_form',
'page arguments' => array(
'command_buttons_entity_delete_form',
4,
),
'access callback' => 'command_buttons_access',
'access arguments' => array(
'delete',
4,
),
'weight' => -8,
) + $base;
if (module_exists('devel')) {
$items['admin/structure/command-buttons/view/%command_buttons/devel'] = array(
'title' => 'Devel',
'page callback' => 'devel_load_object',
'page arguments' => array(
'command_button',
4,
),
'access arguments' => array(
'access devel information',
),
'type' => MENU_LOCAL_TASK,
'weight' => 100,
'file' => 'devel.pages.inc',
'file path' => drupal_get_path('module', 'devel'),
);
$items['admin/structure/command-buttons/view/%command_buttons/devel/load'] = array(
'title' => 'Load',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/structure/command-buttons/view/%command_buttons/devel/render'] = array(
'title' => 'Render',
'page callback' => 'devel_render_object',
'page arguments' => array(
'command_buttons',
4,
),
'access arguments' => array(
'access devel information',
),
'file' => 'devel.pages.inc',
'file path' => drupal_get_path('module', 'devel'),
'type' => MENU_LOCAL_TASK,
'weight' => 100,
);
}
return $items;
}
function command_buttons_entity_info_alter(&$entity_info) {
$entity_info['fieldable_panels_pane']['bundles']['command_buttons'] = array(
'label' => t('Add Command List'),
'pane category' => t('Custom'),
'pane top level' => TRUE,
'pane icon' => drupal_get_path('module', 'panopoly_widgets') . '/images/icon_link.png',
'admin' => array(
'path' => 'admin/structure/fieldable-panels-panes/manage/%fieldable_panels_panes_type',
'bundle argument' => 4,
'real path' => 'admin/structure/fieldable-panels-panes/manage/command-buttons',
'access arguments' => array(
'administer fieldable panels panes',
),
),
);
}
function command_buttons_modules_enabled($modules) {
$instance = field_info_instance('command_button', 'field_command_link', 'button');
if (empty($instance)) {
features_revert(array(
'command_buttons' => array(
'field_instance',
'field_base',
),
));
}
if (in_array('command_buttons', $modules)) {
$node_type = entity_get_info('node');
foreach ($node_type['bundles'] as $bundle => $definition) {
command_buttons_create_node_button($bundle, $definition['label']);
}
}
}
function command_buttons_node_type_insert($info) {
command_buttons_create_node_button($info->type, $info->name);
}
function command_buttons_create_node_button($type, $label) {
$new_buttons = array();
if (!command_buttons_machine_name_exists($type)) {
$type_url_str = str_replace('_', '-', $type);
$values = array(
'bundle' => 'node_add',
'name' => $type,
'title' => t('Create !title', array(
'!title' => $label,
)),
'language' => LANGUAGE_NONE,
'field_command_link' => array(
LANGUAGE_NONE => array(
array(
'url' => 'node/add/' . $type_url_str,
'title' => 'Create ' . $label,
),
),
),
);
$new_buttons[$type] = command_buttons_create($values);
}
if (count($new_buttons)) {
drupal_alter('command_buttons_node_buttons', $new_buttons);
if (count($new_buttons)) {
foreach ($new_buttons as $new_button) {
command_buttons_save($new_button);
}
}
}
}
function command_buttons_node_type_delete($info) {
if (command_buttons_machine_name_exists($info->name)) {
$bid = db_select('command_buttons', 'b')
->fields('b', array(
'bid',
))
->condition('name', $info->name)
->execute()
->fetchField();
command_buttons_delete($bid);
}
}
function command_buttons_type_load($type) {
$type = str_replace('-', '_', $type);
$entity_info = entity_get_info('command_button');
if (isset($entity_info['bundles'][$type])) {
return $type;
}
}
function command_buttons_load($bid) {
if (!is_numeric($bid)) {
return FALSE;
}
$conditions = array();
$entities = command_buttons_load_multiple(array(
$bid,
), $conditions);
if ($entities) {
return reset($entities);
}
}
function command_buttons_load_multiple($ids, $conditions = array(), $reset = FALSE) {
return entity_load('command_button', $ids, $conditions, $reset);
}
function command_buttons_permission() {
$perms = array(
'administer command buttons' => array(
'title' => t('Administer command buttons'),
'description' => t('Allows users to add and manage command buttons.'),
),
);
$entity_info = entity_get_info('command_button');
foreach ($entity_info['bundles'] as $bundle => $info) {
$perms["create button {$bundle}"] = array(
'title' => t('Create new %type', array(
'%type' => $info['label'],
)),
'description' => t('Allows users to create new command buttons of bundle %type.', array(
'%type' => $info['label'],
)),
);
$perms["edit button {$bundle}"] = array(
'title' => t('Edit %type', array(
'%type' => $info['label'],
)),
'description' => t('Allows users to edit command buttons of bundle %type. This is a minimum permission; it is required to be able to edit a command button at all, but higher access requirements on an individual button can override it.', array(
'%type' => $info['label'],
)),
);
$perms["delete button {$bundle}"] = array(
'title' => t('Delete %type', array(
'%type' => $info['label'],
)),
'description' => t('Allows users to delete command buttons of bundle %type.', array(
'%type' => $info['label'],
)),
);
}
return $perms;
}
function command_buttons_entity_info() {
$return = array(
'command_button' => array(
'label' => t('Command Button'),
'entity class' => 'Entity',
'controller class' => 'ButtonController',
'module' => 'command_buttons',
'base table' => 'command_buttons',
'fieldable' => TRUE,
'exportable' => TRUE,
'entity keys' => array(
'id' => 'bid',
'name' => 'name',
'bundle' => 'bundle',
'label' => 'title',
),
'view callback' => 'entity_metadata_view_single',
'creation callback' => 'command_buttons_create',
'access callback' => 'command_buttons_access',
'save callback' => 'command_buttons_save',
'bundles' => array(
'button' => array(
'label' => t('Command Button'),
'admin' => array(
'path' => 'admin/structure/command-buttons/manage/%command_buttons_type',
'bundle argument' => 4,
'real path' => 'admin/structure/command-buttons/manage/button',
'access arguments' => array(
'administer command buttons',
),
),
),
'node_add' => array(
'label' => t('Node Creation Button'),
'admin' => array(
'path' => 'admin/structure/command-buttons/manage/%command_buttons_type',
'bundle argument' => 4,
'real path' => 'admin/structure/command-buttons/manage/node-add',
'access arguments' => array(
'administer command buttons',
),
),
),
),
'view modes' => array(
'full' => array(
'label' => t('Full'),
'custom settings' => FALSE,
),
),
),
);
if (module_exists('entitycache')) {
$return['command_button']['field cache'] = FALSE;
$return['command_button']['entity cache'] = TRUE;
}
return $return;
}
function command_buttons_field_extra_fields() {
$extra = array();
$entity_info = entity_get_info('command_button');
foreach ($entity_info['bundles'] as $bundle => $info) {
$extra['command_button'][$bundle] = array(
'form' => array(
'title' => array(
'label' => t('Title'),
'description' => t('The displayed title of the entity.'),
'weight' => -5,
),
),
'display' => array(
'title' => array(
'label' => t('Title'),
'description' => t('The displayed title of the entity.'),
'weight' => -5,
),
),
);
}
return $extra;
}
function command_buttons_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'command_buttons') . '/plugins/views',
);
}
function command_buttons_entities_list_page($type) {
return views_embed_view('command_buttons_entities', 'default', $type);
}
function command_buttons_view($entity, $view_mode = 'full', $langcode = LANGUAGE_NONE) {
return entity_get_controller('command_button')
->view($entity, $view_mode, $langcode);
}
function command_button_view($entity, $view_mode = 'full', $langcode) {
return command_buttons_view($entity, $view_mode, $langcode);
}
function command_buttons_save($entity) {
return entity_get_controller('command_button')
->save($entity);
}
function command_buttons_delete($bid) {
command_buttons_delete_multiple(array(
$bid,
));
}
function command_buttons_delete_multiple($bids) {
return entity_get_controller('command_button')
->delete($bids);
}
function command_buttons_create($values = array()) {
return entity_get_controller('command_button')
->create($values);
}
function command_buttons_access($op, $entity = NULL, $account = NULL) {
return entity_get_controller('command_button')
->access($op, $entity, $account);
}
function command_buttons_entities_title($type) {
$type = str_replace('-', '_', $type);
$entity_info = entity_get_info('command_buttons');
if (isset($entity_info['bundles'][$type])) {
return $entity_info['bundles'][$type]['label'];
}
}
function command_buttons_get_bundle_label($bundle) {
$entity_info = entity_get_info('command_buttons');
if (empty($entity_info['bundles'][$bundle]['label'])) {
$output = t('Unknown bundle type');
}
return $entity_info['bundles'][$bundle]['label'];
}
function command_buttons_get_bundle_labels() {
$bundles = array();
$entity_info = entity_get_info('command_button');
foreach ($entity_info['bundles'] as $bundle => $info) {
$bundles[$bundle] = $info['label'];
}
asort($bundles);
return $bundles;
}
function command_buttons_entity_title($entity) {
if (!empty($entity->title)) {
return $entity->title;
}
return t('No title');
}
function command_buttons_entity_edit_form($form, &$form_state) {
$entity = $form_state['entity'];
$form['#entity_type'] = array(
'#type' => 'value',
'#value' => $entity->bundle,
);
$form_state['command_button'] = $form_state['entity'];
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#default_value' => $entity->title,
'#weight' => -10,
);
$form['name'] = array(
'#type' => 'machine_name',
'#default_value' => $entity->name,
'#maxlength' => 21,
'#machine_name' => array(
'source' => array(
'title',
),
'exists' => 'command_buttons_machine_name_exists',
),
);
$form['language'] = array(
'#type' => 'value',
'#value' => $entity->language,
);
$language = NULL;
if (function_exists('entity_language')) {
$language = entity_language('command_button', $entity);
}
field_attach_form('command_button', $entity, $form, $form_state, $language);
if (!empty($form_state['add submit'])) {
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
}
return $form;
}
function command_buttons_entity_edit_form_submit($form, &$form_state) {
$entity = $form_state['entity'];
$entity->title = $form_state['values']['title'];
$entity->name = $form_state['values']['name'];
field_attach_submit('command_button', $entity, $form, $form_state);
command_buttons_save($entity);
$form_state['redirect'] = 'admin/structure/command-buttons/view/' . $entity->bid;
if (!empty($form_state['add submit'])) {
drupal_set_message(t('The entity has been saved.'));
}
}
function command_buttons_entity_view($entity, $type, $view_mode, $langcode) {
if ($type == 'fieldable_panels_pane' && $entity->bundle == 'command_buttons') {
$classes = command_buttons_get_button_class($entity, $type, $view_mode, $langcode);
$items = array();
$use_dropdowns = field_get_items($type, $entity, 'field_use_dropdowns', $langcode);
$dropdown_label = field_get_items($type, $entity, 'field_dropdown_label', $langcode);
$commands = array();
$values = field_get_items($type, $entity, 'field_commands', $langcode);
if (count($values)) {
foreach ($values as $value) {
$commands[] = $value['entity'];
}
}
if (!empty($use_dropdowns[0]['value']) && !empty($dropdown_label[0]['value'])) {
$classes['use_dropdowns'] = TRUE;
$classes['dropdown_label'] = $dropdown_label[0]['value'];
}
else {
$classes['use_dropdowns'] = FALSE;
$classes['dropdown_label'] = '';
}
$output = command_buttons_render_buttons($commands, $classes);
if (!empty($output)) {
$entity->content['field_commands'] = array(
'#markup' => $output,
);
}
else {
unset($entity->content['field_commands']);
}
}
}
function command_buttons_render_buttons($commands, $classes, $source = array()) {
if (empty($source)) {
foreach ($commands as $command) {
$source[] = array(
'value' => $command->name,
'provider_type' => '',
);
}
}
$duplicates = array();
foreach ($source as $command_link) {
if (isset($duplicates[$command_link['value']])) {
$duplicates[$command_link['value']] = TRUE;
}
else {
$duplicates[$command_link['value']] = FALSE;
}
}
foreach ($commands as $command) {
$links = field_get_items('command_button', $command, 'field_command_link');
if (!empty($links)) {
foreach ($links as $link) {
$path = filter_xss(token_replace($link['url']));
if (!url_is_external($path)) {
$item = menu_get_item($path);
if (!$item || empty($item['access'])) {
continue;
}
}
$link['attributes']['class'] = !empty($classes['item_class']) ? $classes['item_class'] : array();
$link['attributes']['class'] += array(
' command-' . str_replace('-', '_', $command->name),
);
foreach ($source as $command_link) {
$options = array(
'attributes' => $link['attributes'],
);
if (!empty($command_link) && $command_link['value'] === $command->name) {
if ($command_link['provider_type'] == 'oa_section') {
$options['query'] = array(
'oa_section_ref' => $command_link['id'],
);
}
else {
if ($command_link['provider_type'] == 'oa_space') {
$options['query'] = array(
'og_group_ref' => $command_link['id'],
);
}
}
if (drupal_valid_path($path)) {
if (!$duplicates[$command_link['value']]) {
$title = token_replace($link['title']);
}
else {
$title = db_select('node', 'n')
->fields('n', array(
'title',
))
->condition('n.nid', $command_link['id'])
->execute()
->fetchField();
$title = token_replace($link['title'] . ' ' . t('in') . ' ' . $title);
}
$icon = field_get_items('command_button', $command, 'field_link_icon');
if (!empty($icon[0]['value'])) {
$title = '<i class="' . $icon[0]['value'] . '"></i> ' . $title;
$options['html'] = TRUE;
}
$options = array_merge_recursive($options, drupal_parse_url($path));
$path = $options['path'];
unset($options['path']);
$items[] = '<li>' . l($title, $path, $options) . '</li>';
}
}
}
}
}
}
if (!empty($items)) {
$vars['items'] = $items;
$vars['icon'] = !empty($classes['icon_class']) ? check_plain($classes['icon_class']) : '';
$vars['show_caret'] = isset($classes['show_caret']) ? $classes['show_caret'] : TRUE;
$vars['use_dropdowns'] = $classes['use_dropdowns'];
$vars['wrapper_class'] = implode(' ', $classes['wrapper_class']);
$vars['label'] = check_plain($classes['dropdown_label']);
$vars['btn_title'] = isset($classes['btn_title']) ? check_plain($classes['btn_title']) : '';
$vars['direction'] = isset($classes['direction']) ? $classes['direction'] : '';
return theme('command_buttons', $vars);
}
return '';
}
function command_buttons_theme() {
return array(
'command_buttons' => array(
'variables' => array(
'items' => array(),
),
'template' => 'command-buttons',
),
);
}
function command_buttons_get_button_class($entity, $type, $view_mode, $langcode) {
$return_classes = array(
'item_class' => array(
'oa-button',
),
'wrapper_class' => array(
'oa-buttons',
),
);
$style = field_get_items($type, $entity, 'field_button_style', $langcode);
if (!empty($style[0]['value']) && $style[0]['value'] != 'other') {
$classes[] = $style[0]['value'];
}
else {
$class = field_get_items($type, $entity, 'field_css_class', $langcode);
if (!empty($class[0]['safe_value'])) {
$classes[] = $class[0]['safe_value'];
}
}
$size = field_get_items($type, $entity, 'field_button_size', $langcode);
if (!empty($size[0]['value']) && $style[0]['value'] != 'default') {
$classes[] = $size[0]['value'];
}
$block = field_get_items($type, $entity, 'field_block_buttons', $langcode);
if (!empty($block[0]['value'])) {
$classes[] = 'btn-block';
}
$use_dropdowns = field_get_items($type, $entity, 'field_use_dropdowns', $langcode);
if (!empty($use_dropdowns[0]['value'])) {
$return_classes['wrapper_class'] = array_merge($classes, $return_classes['wrapper_class']);
}
else {
$return_classes['item_class'] = array_merge($classes, $return_classes['item_class']);
}
return $return_classes;
}
function command_buttons_form_command_buttons_entity_edit_form_alter(&$form, &$form_state, $form_id) {
if (!empty($form['field_command_link'])) {
$entity_info = entity_get_info('command_button');
$form['tokens'] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 20,
'#title' => t('Placeholder tokens'),
'#description' => t("The following placeholder tokens can be used in both paths and titles. When used in a path or title, they will be replaced with the appropriate values."),
);
$form['tokens']['help'] = array(
'#theme' => 'token_tree',
'#token_types' => array(
$entity_info['token type'],
),
'#global_types' => TRUE,
'#click_insert' => TRUE,
);
if (isset($form['field_link_icon'])) {
foreach ($form['field_link_icon'][LANGUAGE_NONE]['#options'] as $key => $option) {
if ($key != '_none') {
$form['field_link_icon'][LANGUAGE_NONE]['#options'][$key] = "<i class='" . $option . "'></i> " . $option;
}
}
}
}
}
function command_buttons_machine_name_exists($value) {
$name_exists = db_select('command_buttons', 'b')
->fields('b', array(
'name',
))
->condition('b.name', $value)
->execute()
->fetchField();
return !empty($name_exists);
}
function command_buttons_machine_name_load($buttons) {
$entities = array();
$bids = array();
if (!is_array($buttons)) {
$buttons = array(
$buttons,
);
}
if (empty($buttons)) {
return $entities;
}
$results = db_select('command_buttons', 'b')
->fields('b', array(
'bid',
))
->condition('name', $buttons, 'IN')
->orderBy('title')
->execute();
while ($result = $results
->fetchObject()) {
$bids[] = $result->bid;
}
if (count($bids)) {
$entities = command_buttons_load_multiple($bids);
}
return $entities;
}