View source
<?php
include_once 'oa_buttons.features.inc';
define('OA_BUTTONS_CLEAR_CACHE_ALL', 0);
function oa_buttons_ctools_plugin_directory($owner, $plugin_type) {
if ($owner == 'ctools' && $plugin_type == 'content_types') {
return 'plugins/content_types';
}
}
function oa_buttons_form_oa_section_node_form_alter(&$form, &$form_state, $form_id) {
oa_buttons_node_overrides($form, $form_state, $form_id);
}
function oa_buttons_form_oa_space_node_form_alter(&$form, &$form_state, $form_id) {
oa_buttons_node_overrides($form, $form_state, $form_id);
}
function oa_buttons_node_overrides(&$form, &$form_state, $form_id) {
$form['#attached']['js'][] = array(
'data' => drupal_get_path('module', 'oa_buttons') . '/oa_buttons.js',
'type' => 'file',
);
}
function oa_buttons_oa_core_space_type_options($term, $vocab_name) {
$options = array(
'node_options' => array(),
);
$node_types = field_get_items('taxonomy_term', $term, 'field_oa_node_types', LANGUAGE_NONE);
if (!empty($node_types)) {
foreach ($node_types as $option) {
$options['node_options'][] = $option['value'];
}
}
return $options;
}
function oa_buttons_get_command_buttons_options() {
$node_type_options =& drupal_static(__FUNCTION__, array());
if (!$node_type_options) {
$result = db_select('command_buttons', 'b')
->fields('b', array(
'name',
'title',
));
$result = $result
->execute();
while ($button = $result
->fetchAssoc()) {
$node_type_options[$button['name']] = $button['title'];
}
}
return $node_type_options;
}
function oa_buttons_get_command_buttons($node, $all = FALSE) {
$buttons = array();
$node_types = array_flip(node_type_get_names());
_oa_buttons_get_space_command_buttons($node, $buttons, $all);
_oa_buttons_get_parent_command_buttons($node, $buttons);
foreach ($buttons as $button) {
$entity_type = $button['value'];
if (in_array($entity_type, $node_types) && !node_access('create', $entity_type)) {
unset($buttons[$entity_type]);
}
}
drupal_alter('oa_buttons_add_content', $buttons, $node);
return $buttons;
}
function _oa_buttons_get_node_command_buttons($node, &$buttons) {
$override_node_options = field_get_items('node', $node, 'field_oa_section_override');
if (empty($override_node_options[0]['value'])) {
if ($node->type == 'oa_section') {
$items = field_get_items('node', $node, 'field_oa_section');
}
else {
$items = field_get_items('node', $node, 'field_oa_space_type');
}
if (!empty($items) && ($tid = reset($items)) && ($term = taxonomy_term_load($tid['tid']))) {
$node_options = field_get_items('taxonomy_term', $term, 'field_oa_node_types');
}
}
else {
$node_options = field_get_items('node', $node, 'field_oa_node_types');
}
if (!empty($node_options)) {
foreach ($node_options as $opt) {
if (!isset($buttons[$opt['value'] . ':' . $node->nid])) {
$buttons[$opt['value'] . ':' . $node->nid] = array(
'value' => $opt['value'],
'provider_type' => $node->type,
'id' => $node->nid,
);
}
}
}
}
function _oa_buttons_get_parent_command_buttons($node, &$buttons) {
if (!module_exists('og_subgroups')) {
return;
}
module_load_include('inc', 'og_subgroups', 'og_subgroups.common');
$parent_nids = og_subgroups_parents_load('node', $node->nid, FALSE);
if (!empty($parent_nids)) {
$spaces = db_select('node', 'n')
->fields('n', array(
'nid',
))
->condition('n.type', 'oa_space')
->condition('n.nid', $parent_nids['node'], 'IN')
->execute()
->fetchAllAssoc('nid');
if (!empty($spaces)) {
$parent_groups = node_load_multiple(array_keys($spaces));
if (!empty($parent_groups)) {
foreach ($parent_groups as $parent) {
if ($parent->type == 'oa_space' && node_access('view', $node)) {
_oa_buttons_get_node_command_buttons($parent, $buttons);
}
}
}
}
}
}
function _oa_buttons_get_space_command_buttons($node, &$buttons, $all = FALSE) {
_oa_buttons_get_node_command_buttons($node, $buttons);
if ($node->type == 'oa_space') {
$sections = oa_core_space_sections($node->nid, NODE_PUBLISHED);
if (count($sections)) {
if (!$all && ($section = oa_core_get_section_context())) {
if (isset($sections[$section])) {
$section_node = node_load($section);
_oa_buttons_get_space_command_buttons($section_node, $buttons, $all);
}
}
else {
foreach ($sections as $nid => $section) {
$section_node = node_load($nid);
_oa_buttons_get_space_command_buttons($section_node, $buttons, $all);
}
}
}
}
}
function _oa_buttons_perm_name($machine_name, $tid) {
return 'use oa button ' . $machine_name . ' for ' . $tid . ' term';
}
function oa_buttons_og_permission() {
$perms = array();
if (module_exists('oa_subspaces') && ($vocabulary = oa_core_taxonomy_vocabulary('space_type')) && ($terms = taxonomy_get_tree($vocabulary->vid, 0))) {
$perms['use any oa button space_type'] = array(
'title' => t('Use any space type'),
'description' => t('Users may use any of the available space types..'),
'restrict access' => TRUE,
'default role' => array(
OG_ADMINISTRATOR_ROLE,
),
);
foreach ($terms as $term) {
$perms[_oa_buttons_perm_name('space_type', $term->tid)] = array(
'title' => t('Use @name space type', array(
'@name' => $term->name,
)),
'description' => t('Users may select @name space type on create space form. This permission will be ignored if user can create space globally or if they can administer current group.', array(
'@name' => $term->name,
)),
'restrict access' => TRUE,
'default role' => array(
OG_ADMINISTRATOR_ROLE,
),
);
}
}
return $perms;
}
function oa_buttons_node_insert($node) {
if ($node->type == OA_SPACE_TYPE) {
oa_buttons_clear_section_button_cache(OA_BUTTONS_CLEAR_CACHE_ALL);
}
elseif ($node->type == OA_SECTION_TYPE) {
$gid = !empty($node->{OG_AUDIENCE_FIELD}[LANGUAGE_NONE][0]['target_id']) ? $node->{OG_AUDIENCE_FIELD}[LANGUAGE_NONE][0]['target_id'] : OA_BUTTONS_CLEAR_CACHE_ALL;
oa_buttons_clear_section_button_cache($gid);
}
}
function oa_buttons_node_update($node) {
if ($node->type == OA_SPACE_TYPE) {
oa_buttons_clear_section_button_cache($node->nid);
}
elseif ($node->type == OA_SECTION_TYPE) {
$gid = !empty($node->{OG_AUDIENCE_FIELD}[LANGUAGE_NONE][0]['target_id']) ? $node->{OG_AUDIENCE_FIELD}[LANGUAGE_NONE][0]['target_id'] : OA_BUTTONS_CLEAR_CACHE_ALL;
oa_buttons_clear_section_button_cache($gid);
}
}
function oa_buttons_flag_flag($flag, $content_id, $account, $flagging) {
oa_buttons_trigger_flag('flag', $flag, $content_id, $account);
}
function oa_buttons_flag_unflag($flag, $content_id, $account, $flagging) {
oa_buttons_trigger_flag('unflag', $flag, $content_id, $account);
}
function oa_buttons_trigger_flag($op, $flag, $content_id, $account) {
if ($flag->name == 'trash' && ($node = node_load($content_id)) && $node->type == OA_SECTION_TYPE) {
$gid = !empty($node->{OG_AUDIENCE_FIELD}[LANGUAGE_NONE][0]['target_id']) ? $node->{OG_AUDIENCE_FIELD}[LANGUAGE_NONE][0]['target_id'] : OA_BUTTONS_CLEAR_CACHE_ALL;
oa_buttons_clear_section_button_cache($gid);
}
}
function oa_buttons_og_membership_insert($og_membership) {
if ($og_membership->entity_type == 'user') {
oa_buttons_clear_section_button_cache();
}
}
function oa_buttons_og_membership_update($og_membership) {
if ($og_membership->entity_type == 'user') {
oa_buttons_clear_section_button_cache();
}
}
function oa_buttons_og_membership_delete($og_membership) {
if ($og_membership->entity_type == 'user') {
oa_buttons_clear_section_button_cache();
}
}
function oa_buttons_og_role_change_permissions($role, $grant, $revoke) {
oa_buttons_clear_section_button_cache();
}
function oa_buttons_clear_section_button_cache($gid = NULL) {
if (!isset($gid)) {
$gid = oa_core_get_space_context();
}
$cache_name = 'oa_section_buttons:';
$bin = 'cache_oa_section_buttons';
if (!empty($gid)) {
$cache_key = $cache_name . $gid;
cache_clear_all($cache_key, $bin, TRUE);
if (module_exists('og_subgroups')) {
$subgroups = og_subgroups_children_load('node', $gid, TRUE, FALSE);
if (!empty($subgroups['node'])) {
foreach ($subgroups['node'] as $nid) {
$cache_key = $cache_name . $nid;
cache_clear_all($cache_key, $bin, TRUE);
}
}
}
}
else {
cache_clear_all($cache_name, $bin, TRUE);
}
}
function oa_buttons_flush_caches() {
return array(
'cache_oa_section_buttons',
);
}