You are here

tft_og.module in Taxonomy File Tree 7.2

Hook implementations for TFT OG.

File

modules/tft_og/tft_og.module
View source
<?php

/**
 * @file
 * Hook implementations for TFT OG.
 */

/**
 * Implements hook_menu().
 */
function tft_og_menu() {
  $menu = array(
    'og/%/tft' => array(
      'page callback' => 'tft_og_d6_redirect',
      'page arguments' => array(
        1,
      ),
      'access arguments' => array(
        'access content',
      ),
      'type' => MENU_CALLBACK,
    ),
    'node/%/tft' => array(
      'title' => "Files",
      'access callback' => 'og_user_access',
      'access arguments' => array(
        'node',
        1,
        TFT_PERM__ACCESS_FULL_TREE,
      ),
      'page callback' => 'tft_og',
      'page arguments' => array(
        1,
      ),
      'type' => MENU_CALLBACK,
    ),
  );
  return $menu;
}

/**
 * Implements hook_og_permission().
 */
function tft_og_og_permission() {
  $permissions = tft_permission();
  unset($permissions[TFT_PERM__ADMIN]);
  return $permissions;
}

/**
 * Implements hook_rules_action_info().
 */
function tft_og_rules_action_info() {
  return array(
    'tft_og_rules_create_og_root_folder' => array(
      'label' => t('Create a root folder for a group'),
      'group' => t('Taxonomy File Tree'),
      'parameter' => array(
        'group' => array(
          'type' => 'node',
          'label' => t('Group'),
        ),
      ),
    ),
  );
}

/**
 * Implements hook_tft_term_access().
 */
function tft_og_tft_term_access($tid, $account = NULL, $op = 'view') {
  if (!isset($account)) {
    global $user;
    $account = $user;
  }
  if ($og_nid = tft_og_get_og_nid($tid)) {
    if ($op === 'view') {
      return og_user_access('node', $og_nid, TFT_PERM__ACCESS_FULL_TREE, $account);
    }
    elseif ($op == 'delete') {
      return og_user_access('node', $og_nid, TFT_PERM__DELETE_TERMS, $account);
    }
    elseif ($op == 'add-file') {
      return og_user_access('node', $og_nid, TFT_PERM__ADD_FILE, $account);
    }
    elseif ($op == 'add-folder' || $op == 'edit') {
      return og_user_access('node', $og_nid, TFT_PERM__ADD_TERMS, $account);
    }
    elseif ($op == 'reorder') {
      return og_user_access('node', $og_nid, TFT_PERM__REORDER_ITEMS, $account);
    }
  }
}

/**
 * Implements hook_block_view_alter().
 *
 * Overwrite the TFT block to use the OG term (if in OG context).
 */
function tft_og_block_view_alter(&$data, $block) {
  if ($block->module == 'tft') {
    if (arg(0) == 'node' && is_numeric(arg(1))) {
      $tid = tft_og_get_og_tid(arg(1));
      $data['content'] = theme('tft_folder_explorer', array(
        'folders' => tft_output_tree(tft_folder_tree($tid)),
        'vid' => variable_get('tft_vocabulary_vid', 0),
      ));
    }
  }
}

/**
 * Implements hook_tft_folder_menu_links_alter().
 */
function tft_og_tft_folder_menu_links_alter(&$links, $tid) {
  $og_nid = tft_og_get_og_nid($tid);
  if ($og_nid) {
    if (!empty($links['reorder_items']) && !og_user_access('node', $og_nid, TFT_PERM__REORDER_ITEMS)) {
      unset($links['reorder_items']);
    }
    elseif (empty($links['reorder_items']) && og_user_access('node', $og_nid, TFT_PERM__REORDER_ITEMS)) {
      $links['reorder_items'] = array(
        'title' => t("reorder elements"),
        'href' => "tft/terms/reorder/{$tid}",
        'attributes' => array(
          'id' => 'manage-folders',
          'class' => array(
            'folder-menu-ops-link',
          ),
        ),
        'query' => array(
          'destination' => (!empty($_SESSION['tft']['q']) ? $_SESSION['tft']['q'] : '') . "#tft/{$tid}",
        ),
      );
    }
  }
}

/**
 * Implements hook_tft_get_add_content_links_alter().
 *
 * Check permissions against the OG groups.
 * This is pretty much a verbatim copy of tft_get_add_content_links.
 * @see tft_get_add_content_links().
 */
function tft_og_tft_get_add_content_links_alter(&$links, $tid) {
  $setting = tft_get_file_setting();
  $add_file_query = array(
    'destination' => $_SESSION['tft']['q'],
  );
  $add_term_query = array(
    'destination' => $_SESSION['tft']['q'],
  );
  $setting = tft_get_file_setting();

  // Do we have a tid ?
  if ($tid) {
    $add_file_query['tid'] = $tid;
    $add_term_query['parent'] = $tid;
  }
  $gid = tft_og_get_og_nid($tid);
  if (!$gid) {
    return;
  }

  // Can the user create files ?
  if (og_user_access('node', $gid, 'create ' . $setting['type'] . ' content') && og_user_access('node', $gid, TFT_PERM__ADD_FILE)) {
    if (!empty($gid)) {
      $add_file_query['og_group_ref'] = $gid;
    }
    $links['add_file'] = array(
      'title' => t("Add a file"),
      'href' => 'node/add/' . str_replace('_', '-', $setting['type']),
      'attributes' => array(
        'id' => 'add-child-file',
      ),
      'query' => array_reverse($add_file_query),
    );
  }
  if (og_user_access('node', $gid, TFT_PERM__ADD_TERMS)) {
    $links['add_term'] = array(
      'title' => t("Add a folder"),
      'href' => 'tft/term/add',
      'attributes' => array(
        'id' => 'add-child-folder',
      ),
      'query' => array_reverse($add_term_query),
    );
  }
}

/**
 * Implements hook_form_alter()
 */
function tft_og_form_alter(&$form, &$form_state, $form_id) {
  $setting = tft_get_file_setting();
  switch ($form_id) {
    case $setting['type'] . '_node_form':
      $form['og_group_ref'][LANGUAGE_NONE]['#prefix'] = '<div class="tft-hide-element element-hidden">' . (!empty($form['og_group_ref'][LANGUAGE_NONE]['#prefix']) ? $form['og_group_ref'][LANGUAGE_NONE]['#prefix'] : '');
      $form['og_group_ref'][LANGUAGE_NONE]['#suffix'] .= '</div>';
      $og_nid = $form['og_group_ref'][LANGUAGE_NONE][0]['default']['#default_value'];
      if (empty($og_nid) && !empty($form['og_group_ref'][LANGUAGE_NONE][0]['admin'][0]['target_id']['#entity']->og_group_ref[LANGUAGE_NONE][0]['target_id'])) {
        $og_nid = array(
          $form['og_group_ref'][LANGUAGE_NONE][0]['admin'][0]['target_id']['#entity']->og_group_ref[LANGUAGE_NONE][0]['target_id'],
        );
      }
      if (!empty($og_nid)) {

        // Overwrite the folder select field.
        $og_tid = tft_og_get_og_tid($og_nid[0]);
        $form['tft_select_folder']['tft_js_folder'] = array(
          '#markup' => '<div id="folder-explorer-container" class="tft-node-form">' . tft_output_tree(tft_folder_tree($og_tid, TRUE)) . '</div>',
        );
        $form['#validate'][] = 'tft_og_file_node_validate';
      }
      break;
  }
}

/**
 * Page callback: redirect old Drupal 6 URLs to the new format.
 */
function tft_og_d6_redirect($nid) {
  drupal_goto("node/{$nid}/tft");
}

/**
 * @todo check the user selects a term she has access to.
 *
 * Validate the node form.
 * Check the taxonomy term.
 */
function tft_og_file_node_validate($form, $form_state) {

  // If the user can only add terms to an OG term

  /*if (!user_access(TFT_ADD_TERMS)) {
      if (!tft_term_access(reset($form_state['values']['taxonomy'][variable_get('tft_vocabulary_vid', 0)]))) {
        form_set_error('taxonomy][' . variable_get('tft_vocabulary_vid', 0), t("You must select a parent folder that is part of a group you're a member of."));
      }
    }**/
}

/**
 * Action callback: add a folder for newly created groups.
 */
function tft_og_rules_create_og_root_folder($group) {
  $term = (object) array(
    'vid' => variable_get('tft_vocabulary_vid', 0),
    'name' => $group->title . " (node/{$group->nid})",
    'parent' => array(
      0,
    ),
  );
  taxonomy_term_save($term);
  db_insert('tft_tid_og_nid')
    ->fields(array(
    'tid' => $term->tid,
    'og_nid' => $group->nid,
  ))
    ->execute();
}

/**
 * Page callback: OG file tree.
 *
 * @param int $nid
 *        The OG nid
 *
 * @return string
 *        The HTML
 */
function tft_og($nid) {

  // Check if the user can access content of this group
  if (!node_access('view', node_load($nid))) {
    drupal_access_denied();
    return;
  }

  // Get the term tid
  $tid = db_query("SELECT tid FROM {tft_tid_og_nid} WHERE og_nid = :nid", array(
    ':nid' => $nid,
  ))
    ->fetchField();

  // Set the breadcrumb
  $breadcrumb = drupal_get_breadcrumb();
  $title = db_query("SELECT {node_revision}.title FROM {node_revision}\n                                      LEFT JOIN {node} ON {node}.vid = {node_revision}.vid\n                                    WHERE {node}.nid = :nid", array(
    ':nid' => $nid,
  ))
    ->fetchField();
  $breadcrumb[] = l($title, "node/{$nid}");
  drupal_set_breadcrumb($breadcrumb);

  // Check if we have a valid term tid
  if (!$tid) {
    return t("No term was found for this group ! Please contact your system administrator.");
  }
  else {
    module_load_include('inc', 'tft', 'includes/tft.pages');
    return tft($tid, $nid);
  }
}

/** *
 * Check if the current term is part of a OG term and return the OG nid.
 *
 * @param int $tid
 *
 * @return int|boolean
 *        The OG nid if found, else FALSE
 */
function tft_og_get_og_nid($tid) {
  static $cache = array();
  if (is_array($tid)) {
    $tid = $tid[0];
  }
  $tid = (int) $tid;
  if (!$tid) {
    return FALSE;
  }
  if (isset($cache[$tid])) {
    return $cache[$tid];
  }
  $param_tid = $tid;
  $depth = tft_get_depth($tid);
  $og_nid = db_query("SELECT og_nid FROM {tft_tid_og_nid} WHERE tid = :tid", array(
    ':tid' => $tid,
  ))
    ->fetchField();
  while ($depth && $tid && !$og_nid) {
    $tid = db_query("SELECT parent FROM {taxonomy_term_hierarchy} WHERE tid = :tid", array(
      ':tid' => $tid,
    ))
      ->fetchField();
    $depth--;
    $og_nid = db_query("SELECT og_nid FROM {tft_tid_og_nid} WHERE tid = :tid", array(
      ':tid' => $tid,
    ))
      ->fetchField();
  }
  if ($og_nid) {
    $cache[$param_tid] = (int) $og_nid;
  }
  else {
    $cache[$param_tid] = FALSE;
  }
  return $cache[$param_tid];
}

/**
 * Get the term tid associated with the OG.
 *
 * @param int $nid
 *
 * @return int|NULL
 */
function tft_og_get_og_tid($nid) {
  return db_query("SELECT tid FROM {tft_tid_og_nid} WHERE og_nid = :nid", array(
    ':nid' => $nid,
  ))
    ->fetchField();
}

Functions

Namesort descending Description
tft_og Page callback: OG file tree.
tft_og_block_view_alter Implements hook_block_view_alter().
tft_og_d6_redirect Page callback: redirect old Drupal 6 URLs to the new format.
tft_og_file_node_validate @todo check the user selects a term she has access to.
tft_og_form_alter Implements hook_form_alter()
tft_og_get_og_nid Check if the current term is part of a OG term and return the OG nid.
tft_og_get_og_tid Get the term tid associated with the OG.
tft_og_menu Implements hook_menu().
tft_og_og_permission Implements hook_og_permission().
tft_og_rules_action_info Implements hook_rules_action_info().
tft_og_rules_create_og_root_folder Action callback: add a folder for newly created groups.
tft_og_tft_folder_menu_links_alter Implements hook_tft_folder_menu_links_alter().
tft_og_tft_get_add_content_links_alter Implements hook_tft_get_add_content_links_alter().
tft_og_tft_term_access Implements hook_tft_term_access().