You are here

taxonomy_publisher_filter.module in Taxonomy Tools 8

Same filename and directory in other branches
  1. 7 taxonomy_publisher_filter/taxonomy_publisher_filter.module

File

taxonomy_publisher_filter/taxonomy_publisher_filter.module
View source
<?php

/**
 * @file
 * The module allow you to filter taxonomy term by role
 */
module_load_include('inc', 'taxonomy_publisher_filter', 'taxonomy_publisher_filter.api');

/**
 * Implements hook_permission().
 */
function taxonomy_publisher_filter_permission() {
  $items = array();
  $vocabularies = array_filter(variable_get('taxonomy_tools_publisher_config', array()));
  foreach ($vocabularies as $vocabulary) {
    $tvl = taxonomy_vocabulary_machine_name_load($vocabulary);
    $items["taxonomy_publisher_filter_widget_{$tvl->machine_name}"] = array(
      'title' => t("Field Widget - use publish for taxonomy vocabulary {$tvl->name} :"),
      'description' => t("The vocabulary {$tvl->name} will use publish/unpublish only for the selected role(s), for the other roles will be ignored."),
    );
    $items["taxonomy_publisher_filter_views_{$tvl->machine_name}"] = array(
      'title' => t("Views filter - use publish for taxonomy vocabulary  {$tvl->name} :"),
      'description' => t("The vocabulary  {$tvl->name} will use publish/unpublish only for the selected role(s), for the other roles will be ignored."),
    );
    $items["taxonomy_publisher_filter_custom_{$tvl->machine_name}"] = array(
      'title' => t("Custom filter - use publish for taxonomy vocabulary  {$tvl->name} :"),
      'description' => t("The vocabulary  {$tvl->name} will use publish/unpublish only for the selected role(s), for the other roles will be ignored."),
    );
  }
  return $items;
}

/**
 * Implements hook_menu().
 */
function taxonomy_publisher_filter_menu() {
  $items['admin/config/taxonomy-tools/publisher-filter'] = array(
    'title' => 'Taxonomy Publisher Filter',
    'description' => 'Configure Taxonomy Publisher Filter settings.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'taxonomy_publisher_filter_admin_form',
    ),
    'access arguments' => array(
      'administer taxonomy publisher configuration',
    ),
    'file' => 'taxonomy_publisher_filter.admin.inc',
    'file path' => drupal_get_path('module', 'taxonomy_publisher_filter'),
  );
  return $items;
}

/**
 * Implements hook_field_widget_form_alter().
 */
function taxonomy_publisher_filter_field_widget_form_alter(&$element, &$form_state, $context) {
  global $user;
  if ($context['field']['type'] == 'taxonomy_term_reference' && ($element['#type'] == 'select' || $element['#type'] == 'radios') || $element['#type'] == 'checkboxes') {
    if (user_access('taxonomy_publisher_filter_widget_' . $context['field']['settings']['allowed_values'][0]['vocabulary'])) {
      $roles = array_values($user->roles);
      $settings = array(
        'role' => end($roles),
        'vocabulary' => $context['field']['settings']['allowed_values'][0]['vocabulary'],
        'form_id' => $form_state['build_info']['form_id'],
      );
      _taxonomy_publisher_filter_term_list($element['#options'], TAXONOMY_PUBLISHER_FILTER_CACHE, $settings);
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function taxonomy_publisher_filter_form_views_exposed_form_alter(&$form, $form_state, $form_id) {
  global $user;
  $vocabularies = array_filter(variable_get('taxonomy_tools_publisher_config', array()));
  $t_fields = _taxonomy_publisher_filter_return_taxonomy_fields($form['#info']);
  $roles = array_values($user->roles);
  $roles = end($roles);
  if (count($t_fields)) {
    foreach ($t_fields as $field) {
      if ($form[$field]['#type'] == 'select' || $form[$field]['#type'] == 'radios' || $form[$field]['#type'] == 'checkboxes') {
        $settings = array(
          'role' => $roles,
          'vocabulary' => $field,
          'display_id' => $form['#id'],
        );
        _taxonomy_publisher_filter_filter_options($form[$field]['#options'], $field, $vocabularies, $settings);
      }
    }
  }
}

/**
 * Implements hook_query_alter().
 */
function taxonomy_publisher_filter_query_alter(&$query) {
  $arg = arg();
  if (in_array('taxonomy', $arg) && in_array('autocomplete', $arg)) {
    $vid = '';
    $display_is = 'views';
    $conditions = $query
      ->conditions();
    foreach ($conditions as $index => $value) {
      if (is_numeric($index) && isset($value['field']) && $value['field'] == 't.vid') {
        $vid = $value['value'];
        if (is_array($vid)) {
          $vid = $vid[0];
          $display_is = 'widget';
        }
        break;
      }
    }
    if (is_numeric($vid)) {
      $tvl = taxonomy_vocabulary_load($vid);
      switch ($display_is) {
        case 'views':
          $access = user_access("taxonomy_publisher_filter_views_{$tvl->machine_name}");
          break;
        case 'widget':
          $access = user_access("taxonomy_publisher_filter_widget_{$tvl->machine_name}");
          break;
      }
      if ($access) {
        $query
          ->leftJoin('field_revision_field_taxonomy_term_status', 'fs', 'fs.entity_id = t.tid');
        $db_or = db_or();
        $db_or
          ->condition('fs.entity_id', NULL, 'IS NULL');
        $db_or
          ->condition('fs.field_taxonomy_term_status_value', 1, '=');
        $query
          ->condition($db_or);
      }
    }
  }
}