<?php
/**
 * @file
 * Creates custom pages and menu items for the administration of content
 * to create a tree based content administration and editing experience.
 */

/**
 * Implements hook_menu().
 */
function content_admin_tree_menu() {
  // Initialise the array to hold the menu items.
  $items = array();
  // Defining the array item for the admin/content/% page.
  // In this case % is content type.
  $items['admin/content/%node_type'] = array(
    'title callback' => 'content_admin_tree_title_node_type',
    'title arguments' => array(2),
    'description' => 'Manage all the different content throughout the site.',
    'page callback' => 'content_admin_tree_page_node_type',
    'page arguments' => array(2),
    'access arguments' => array('access content overview'),
    'file' => 'includes/content_admin_tree.page.inc',
  );
  // Defining the page for the overview default menu task.
  $items['admin/content/%node_type/overview'] = array(
    'title' => 'Content Overview',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  // Defining the page for the comments connected to a particular node type.
  $items['admin/content/%node_type/comment'] = array(
    'title' => 'Comments',
    'description' => 'Manage the comments attached to the active node type.',
    'page callback' => 'content_admin_tree_node_type_comments',
    'page arguments' => array(2),
    'access arguments' => array('access content overview'),
    'type' => MENU_LOCAL_TASK,
    'file' => 'includes/content_admin_tree.comments.page.inc',
  );
  // Defining the secondary menu default local menu item.
  $items['admin/content/%node_type/comment/comment'] = array(
    'title' => 'Comments',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  // Defining the page for the unapproved comments
  // connected to a particular node type.
  $items['admin/content/%node_type/comment/approval'] = array(
    'title' => 'Unapproved Comments',
    'description' => 'Manage the unapproved comments attached to the active node type.',
    'page callback' => 'content_admin_tree_node_type_comments_approval',
    'page arguments' => array(2),
    'access arguments' => array('access content overview'),
    'type' => MENU_LOCAL_TASK,
    'file' => 'includes/content_admin_tree.comments.page.inc',
  );
  // Defining the array item for content type/taxonomy term.
  $items['admin/content/%node_type/overview/%taxonomy_term'] = array(
    'title callback' => 'content_admin_tree_title_node_type_taxonomy',
    'title arguments' => array(2, 4),
    'description' => 'Manage all the different content throughout the site.',
    'page callback' => 'content_admin_tree_page_node_type_taxonomy',
    'page arguments' => array(2, 4),
    'access arguments' => array('access content overview'),
    'file' => 'includes/content_admin_tree.page.inc',
  );
  // Defining the page for the overview default menu task for taxonomy term.
  $items['admin/content/%node_type/overview/%taxonomy_term/overview'] = array(
    'title' => 'Content Overview',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  // Defining the page for the comments connected with a
  // particular taxonomy term and it's current node type parent.
  $items['admin/content/%node_type/overview/%taxonomy_term/comment'] = array(
    'title' => 'Comments',
    'description' => 'Manage the comments attached to the active node type and taxonomy term.',
    'page callback' => 'content_admin_tree_node_type_taxonomy_term_comments',
    'page arguments' => array(2, 4),
    'access arguments' => array('access content overview'),
    'type' => MENU_LOCAL_TASK,
    'file' => 'includes/content_admin_tree.comments.page.inc',
  );
  // Defining the secondary menu default local menu
  // item for node type -> taxonomy term.
  $items['admin/content/%node_type/overview/%taxonomy_term/comment/comment'] = array(
    'title' => 'Comments',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  // Defining the page for the unnaproved comments
  // connected with a particular taxonomy term
  // and it's current node type parent.
  $items['admin/content/%node_type/overview/%taxonomy_term/comment/approval'] = array(
    'title' => 'Unapproved comments',
    'description' => 'Manage the unapproved comments attached to the active node type and taxonomy term.',
    'page callback' => 'content_admin_tree_node_type_taxonomy_term_comments_approval',
    'page arguments' => array(2, 4),
    'access arguments' => array('access content overview'),
    'type' => MENU_LOCAL_TASK,
    'file' => 'includes/content_admin_tree.comments.page.inc',
  );
  // Defining the array item for admin/config/content/content_admin_tree.
  $items['admin/config/content/content-admin-tree'] = array(
    'title' => 'Content Admin Tree Configuration',
    'description' => 'Configure the content admin tree administration page.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('content_admin_tree_form'),
    'access arguments' => array('adminster site configuration'),
    'file' => 'includes/content_admin_tree_form.inc',
  );
  return $items;
}

/**
 * Implements hook_menu_alter().
 */
function content_admin_tree_menu_alter(&$items) {
  // Override current admin/content page.
  $module_path = drupal_get_path('module', 'content_admin_tree');
  $items['admin/content'] = array(
    'title' => 'Content',
    'description' => 'Manage all the different content throughout the site.',
    'page callback' => 'content_admin_tree_page',
    'access arguments' => array('access content overview'),
    'weight' => -10,
    'file' => $module_path . '/includes/content_admin_tree.page.inc',
  );
}
/**
 * Implements hook_theme().
 */
function content_admin_tree_theme() {
  // Theme each item (block by content type) of data
  // with a template file.
  return array(
    'content_admin_tree_action_links' => array(
      'arguments' => array(
        'node_type' => NULL,
        'taxonomy_term' => NULL,
      ),
      'template' => 'templates/content-admin-tree-action-links',
    ),
    'content_admin_tree_menu_item' => array(
      'arguments' => array(
        'node_type' => NULL,
        'node_type_url' => NULL,
        'taxonomy_tree_html' => NULL,
        'block' => NULL,
        'start' => NULL,
        'end' => NULL,
        'active' => NULL,
      ),
      'template' => 'templates/content-admin-tree-menu-item',
    ),
    'content_admin_tree_views_block' => array(
      'arguments' => array('views_block' => NULL),
      'template' => 'templates/content-admin-tree-views-block',
    ),
    'content_admin_tree_taxonomy_menu_item' => array(
      'arguments' => array(
        'term_name' => NULL,
        'term_url' => NULL,
        'start' => NULL,
        'end' => NULL,
        'has_children' => NULL,
        'children' => NULL,
      ),
      'file' => 'includes/content_admin_tree.page.inc',
    ),
    'content_admin_tree_comment' => array(
      'arguments' => array(
        'views_block' => NULL,
      ),
      'file' => 'includes/content_admin_tree.comments.page.inc',
    ),
  );
}
/**
 * Preprocess action links.
 */
function content_admin_tree_preprocess_content_admin_tree_action_links(&$variables) {
  global $base_path;
  if (isset($variables['node_type'])) {
    if (isset($variables['taxonomy_term'])) {
      $node_type = str_replace('_', '-', $variables['node_type']->type);
      $path = $base_path . 'node/add/' . check_plain($node_type);
      $variables['action_path'] = $path;
      $variables['action_title'] = 'Add ' . check_plain(drupal_strtolower($variables['node_type']->name)) . ' content';
    }
    else {
      $node_type = str_replace('_', '-', $variables['node_type']->type);
      $path = $base_path . 'node/add/' . check_plain($node_type);
      $variables['action_path'] = $path;
      $variables['action_title'] = 'Add ' . check_plain(drupal_strtolower($variables['node_type']->name)) . ' content';
    }
  }
}
/**
 * Preprocess menu item page output.
 */
function content_admin_tree_preprocess_content_admin_tree_menu_item(&$variables) {
  $variables['node_type_name'] = check_plain(t($variables['node_type_name']));
  $variables['node_type'] = check_plain($variables['node_type']);
  $variables['node_type_url'] = check_plain($variables['node_type_url']);
}
/**
 * Implements hook_views_api().
 */
function content_admin_tree_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'content_admin_tree') . '/includes/views',
  );
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function content_admin_tree_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
  // Where the current path is an admin/content path
  // change the views exposed form submit button
  // from apply to search.
  $current_path_arr = drupal_get_destination();
  $current_path = $current_path_arr['destination'];
  $ca_pos = strpos($current_path, 'admin/content');
  if ($ca_pos !== -1 || $ca_pos !== FALSE) {
    $form['submit']['#value'] = t('Search');
  }
}
/**
 * Implements hook_node_type_insert().
 */
function content_admin_tree_node_type_insert($info) {
  content_admin_tree_clear_menu_list_array_cache();
}

/**
 * Implements hook_node_type_delete().
 */
function content_admin_tree_node_type_delete($info) {
  content_admin_tree_clear_menu_list_array_cache();
  variable_del('content_admin_tree' . $info->type);
}

/**
 * Implements hook_node_type_update().
 */
function content_admin_tree_node_type_update($info) {
  content_admin_tree_clear_menu_list_array_cache();
}

/**
 * Implements hook_taxonomy_term_insert().
 */
function content_admin_tree_taxonomy_term_insert($term) {
  content_admin_tree_clear_menu_list_array_cache();
}

/**
 * Implements hook_taxonomy_term_delete().
 */
function content_admin_tree_taxonomy_term_delete($term) {
  content_admin_tree_clear_menu_list_array_cache();
}

/**
 * Implements hook_taxonomy_term_update().
 */
function content_admin_tree_taxonomy_term_update($term) {
  content_admin_tree_clear_menu_list_array_cache();
}

/**
 * Implements hook_taxonomy_vocabulary_insert().
 */
function content_admin_tree_taxonomy_vocabulary_insert($vocabulary) {
  content_admin_tree_clear_menu_list_array_cache();
}

/**
 * Implements hook_taxonomy_vocabulary_delete().
 */
function content_admin_tree_taxonomy_vocabulary_delete($vocabulary) {
  content_admin_tree_clear_menu_list_array_cache();
}

/**
 * Implements hook_taxonomy_vocabulary_update().
 */
function content_admin_tree_taxonomy_vocabulary_update($vocabulary) {
  content_admin_tree_clear_menu_list_array_cache();
}

/**
 * Helper function to clear the menu data's ctools cache.
 */
function content_admin_tree_clear_menu_list_array_cache() {
  ctools_include('object-cache');
  $cache = ctools_object_cache_get('content_admin_tree', 'mla');
  if ($cache !== NULL || !empty($cache) || isset($cache)) {
    ctools_object_cache_clear('content_admin_tree', 'mla');
  }
}
