You are here

casetracker_views.module in Case Tracker 5

Enables Views integration for Case Tracker.

File

casetracker_views.module
View source
<?php

/**
 * @file
 * Enables Views integration for Case Tracker.
 */

/**
 *  Implementation of hook_views_table().
 */
function casetracker_views_tables() {
  $tables['casetracker_case'] = array(
    'name' => 'casetracker_case',
    'join' => array(
      'left' => array(
        'table' => 'node',
        'field' => 'vid',
      ),
      'right' => array(
        'field' => 'vid',
        'type' => 'inner',
      ),
    ),
    'fields' => array(
      'pid' => array(
        'handler' => 'casetracker_views_project_name',
        'help' => t('Display the name of the Case Tracker project.'),
        'name' => t('Case Tracker: Project'),
        'sortable' => TRUE,
      ),
      'case_number' => array(
        'help' => t('Display the number of the Case Tracker case.'),
        'name' => t('Case Tracker: Case Number'),
        'sortable' => TRUE,
      ),
      'assign_to' => array(
        'handler' => 'casetracker_views_user_name',
        'help' => t('Display the user name the case has been assigned to.'),
        'name' => t('Case Tracker: Assigned To'),
        'sortable' => TRUE,
      ),
      'case_priority_id' => array(
        'handler' => 'casetracker_views_priority_name',
        'help' => t('Display the priority state of the case.'),
        'name' => t('Case Tracker: Case Priority'),
        'sortable' => TRUE,
      ),
      'case_status_id' => array(
        'handler' => 'casetracker_views_status_name',
        'help' => t('Display the status state of the case.'),
        'name' => t('Case Tracker: Case Status'),
        'sortable' => TRUE,
      ),
      'case_type_id' => array(
        'handler' => 'casetracker_views_type_name',
        'help' => t('Display the type state of the case.'),
        'name' => t('Case Tracker: Case Type'),
        'sortable' => TRUE,
      ),
    ),
    'sorts' => array(
      'case_number' => array(
        'help' => t('Sort by Case Tracker case number.'),
        'name' => t('Case Tracker: Case Number'),
        'sortable' => TRUE,
      ),
      'case_status_id' => array(
        'help' => t('Sort by Case Tracker status state.'),
        'name' => t('Case Tracker: Case Status'),
        'sortable' => TRUE,
      ),
      'case_priority_id' => array(
        'help' => t('Sort by Case Tracker priority state .'),
        'name' => t('Case Tracker: Case Priority'),
        'sortable' => TRUE,
      ),
    ),
    'filters' => array(
      'assign_to' => array(
        'help' => t('Filter Case Tracker cases by the assigned user.'),
        'list' => 'casetracker_views_user_options',
        'name' => t('Case Tracker: Assigned To'),
        'operator' => views_handler_operator_andor(),
        'value-type' => 'array',
      ),
      'case_priority_id' => array(
        'help' => t('Filter Case Tracker cases by its priority state.'),
        'list' => 'casetracker_views_priority_options',
        'name' => t('Case Tracker: Case Priority'),
        'operator' => views_handler_operator_andor(),
        'value-type' => 'array',
      ),
      'case_status_id' => array(
        'help' => t('Filter Case Tracker cases by its status state.'),
        'list' => 'casetracker_views_status_options',
        'name' => t('Case Tracker: Case Status'),
        'operator' => views_handler_operator_andor(),
        'value-type' => 'array',
      ),
      'case_type_id' => array(
        'help' => t('Filter Case Tracker cases by its type state.'),
        'list' => 'casetracker_views_type_options',
        'name' => t('Case Tracker: Case Type'),
        'operator' => views_handler_operator_andor(),
        'value-type' => 'array',
      ),
      'assign_to_currentuid' => array(
        'help' => t('Filter Case Tracker cases by the current logged in user.'),
        'list' => 'views_handler_filter_usercurrent',
        'list-type' => 'select',
        'name' => t('Case Tracker: Assigned to current logged in User'),
        'field' => 'assign_to',
        'operator' => 'views_handler_operator_eqneq',
      ),
    ),
  );
  $tables['casetracker_project'] = array(
    'name' => 'casetracker_project',
    'join' => array(
      'left' => array(
        'table' => 'casetracker_case',
        'field' => 'pid',
      ),
      'right' => array(
        'field' => 'nid',
        'type' => 'inner',
      ),
    ),
    'fields' => array(
      'project_number' => array(
        'handler' => 'casetracker_views_project_number',
        'help' => t('Display the Project number.'),
        'name' => t('Case Tracker: Project number'),
        'sortable' => TRUE,
      ),
      'project_number_full' => array(
        'notafield' => TRUE,
        'handler' => 'casetracker_views_case_number_full',
        'help' => t('Display the full Project-Case number.'),
        'name' => t('Case Tracker: Project-Case number'),
        'sortable' => FALSE,
      ),
    ),
    'filters' => array(
      'project_number' => array(
        'help' => t('Filter Case Tracker cases by their project.'),
        'list' => 'casetracker_views_project_options',
        'name' => t('Case Tracker: Project'),
        'operator' => views_handler_operator_andor(),
        'value-type' => 'array',
      ),
    ),
  );
  $tables['casetracker_case_states'] = array(
    'name' => 'casetracker_case',
    'join' => array(
      'left' => array(
        'table' => 'casetracker_case',
        'field' => 'case_status_id',
      ),
      'right' => array(
        'field' => 'csid',
        'type' => 'inner',
      ),
    ),
    'sorts' => array(
      'case_state_name' => array(
        'name' => t('Case Tracker: State Name'),
        'help' => t('Sort by Case Tracker state name.'),
        'sortable' => TRUE,
      ),
    ),
  );
  $tables['casetracker_case_node'] = array(
    'name' => 'node',
    'join' => array(
      'left' => array(
        'table' => 'node',
        'field' => 'vid',
      ),
      'right' => array(
        'field' => 'vid',
        'type' => 'inner',
      ),
    ),
    'fields' => array(
      'nid' => array(
        'handler' => 'casetracker_views_field_createCaseLink_handler',
        'name' => t('Case Tracker: Add Case link'),
        'option' => 'string',
        'notafield' => TRUE,
        'help' => t('Display a link to add a new Case for a Project. Enter the text of this link into the option field; if blank the default "Add Case" will be used.'),
      ),
      'view_cases' => array(
        'handler' => 'casetracker_views_field_viewProjectCases_link_handler',
        'name' => t('Case Tracker: View Cases link'),
        'option' => 'string',
        'notafield' => TRUE,
        'help' => t('Display a link to view all Project Cases. Enter the text of this link into the option field; if blank the default "View Cases" will be used.'),
      ),
    ),
  );
  return $tables;
}

/**
 * Views field handler; display the project number of the Case Tracker project.
 */
function casetracker_views_project_number($fieldinfo, $fielddata, $value, $data) {
  if (!empty($data->casetracker_project_project_number)) {
    return $data->casetracker_project_project_number;
  }
  $project = db_fetch_object(db_query(db_rewrite_sql("SELECT cp.project_number FROM {casetracker_project} cp WHERE cp.nid = %d"), $data->nid));
  if ($project->project_number) {
    return $project->project_number;
  }
}

/**
 * Views field handler; display the project-case number of the Case Tracker case.
 */
function casetracker_views_case_number_full($fieldinfo, $fielddata, $value, $data) {
  if (!empty($data->casetracker_project_project_number)) {
    return $data->casetracker_project_project_number . '-' . $data->casetracker_case_case_number;
  }
  $project = db_fetch_object(db_query(db_rewrite_sql("SELECT cp.project_number FROM {casetracker_project} cp WHERE cp.nid = %d"), $data->casetracker_case_pid));
  if ($project->project_number) {
    return $project->project_number . '-' . $data->casetracker_case_case_number;
  }
}

/**
 * Views field handler; display the priority state of the case.
 */
function casetracker_views_priority_name($fieldinfo, $fielddata, $value, $data) {
  return check_plain(casetracker_case_state_load('priority', $data->casetracker_case_case_priority_id));
}

/**
 * Views field handler; display the name of the Case Tracker project.
 */
function casetracker_views_project_name($fieldinfo, $fielddata, $value, $data) {
  $node = db_fetch_object(db_query(db_rewrite_sql("SELECT n.nid, n.title FROM {node} n WHERE n.nid = %d"), $data->casetracker_case_pid));
  if ($node->nid) {
    return l($node->title, "node/{$node->nid}");
  }

  // returns a linked case tracker project title.
}

/**
 * Views field handler; display the status state of the case.
 */
function casetracker_views_status_name($fieldinfo, $fielddata, $value, $data) {
  return check_plain(casetracker_case_state_load('status', $data->casetracker_case_case_status_id));
}

/**
 * Views field handler; display the type state of the case.
 */
function casetracker_views_type_name($fieldinfo, $fielddata, $value, $data) {
  return check_plain(casetracker_case_state_load('type', $data->casetracker_case_case_type_id));
}

/**
 * Views field handler; display the user name the case has been assigned to.
 */
function casetracker_views_user_name($fieldinfo, $fielddata, $value, $data) {
  return casetracker_get_name($data->casetracker_case_assign_to);
}

/**
 * Views filter handler; filter Case Tracker cases by its priority state.
 */
function casetracker_views_priority_options() {
  return casetracker_case_state_load('priority');
}

/**
 * Views filter handler; filter Case Tracker cases by their project.
 */
function casetracker_views_project_options() {
  $results = db_query(db_rewrite_sql("SELECT cp.project_number, n.title FROM {node} n INNER JOIN {casetracker_project} cp ON n.nid = cp.nid WHERE n.status = 1"));
  $projects = array();
  while ($result = db_fetch_object($results)) {
    $projects[$result->project_number] = $result->project_number . ' - ' . $result->title;
  }
  return $projects;
}

/**
 * Views filter handler; filter Case Tracker cases by its status state.
 */
function casetracker_views_status_options() {
  return casetracker_case_state_load('status');
}

/**
 * Views filter handler; filter Case Tracker cases by its type state.
 */
function casetracker_views_type_options() {
  return casetracker_case_state_load('type');
}

/**
 * Views filter handler; filter Case Tracker cases by the assigned user.
 */
function casetracker_views_user_options() {
  $users = array(
    0 => variable_get('casetracker_default_assign_to', variable_get('anonymous', t('Anonymous'))),
  );
  $results = db_query('SELECT uid, name FROM {users} WHERE status > 0 AND uid > 0');
  while ($result = db_fetch_object($results)) {
    $options[$result->uid] = check_plain($result->name);
  }
  return $options;
}

/**
 * Views field handler; Get a link to add a case for a project
 * @link http://drupal.org/node/99565
 * 
 * @param	array
 * @param 	array
 * @param 	mixed
 * @param 	array
 * @return 	string
 */
function casetracker_views_field_createCaseLink_handler($fieldinfo, $fielddata, $value, $data) {
  $node_types = node_get_types('names');
  $case_types = array_filter(variable_get('casetracker_case_node_types', array(
    'casetracker_basic_case',
  )));
  $operations = array();
  foreach ($case_types as $case_type) {
    if ($fielddata['options']) {
      $linkText = $fielddata['options'];
    }
    else {
      $linkText = t('Add !name', array(
        '!name' => $node_types[$case_type],
      ));
    }
    $operations[] = l($linkText, 'node/add/' . $case_type . '/' . $data->nid);
  }
  return implode(' | ', $operations);
}

/**
 * Views field handler; Get a link to view all project cases
 * 
 * @param	array
 * @param 	array
 * @param 	mixed
 * @param 	array
 * @return 	string
 */
function casetracker_views_field_viewProjectCases_link_handler($fieldinfo, $fielddata, $value, $data) {
  $linkText = $fielddata['options'] ? $linkText = $fielddata['options'] : t('View Cases');
  return l($linkText, 'casetracker/cases/' . $data->nid . '/all');
}

Functions

Namesort descending Description
casetracker_views_case_number_full Views field handler; display the project-case number of the Case Tracker case.
casetracker_views_field_createCaseLink_handler Views field handler; Get a link to add a case for a project @link http://drupal.org/node/99565
casetracker_views_field_viewProjectCases_link_handler Views field handler; Get a link to view all project cases
casetracker_views_priority_name Views field handler; display the priority state of the case.
casetracker_views_priority_options Views filter handler; filter Case Tracker cases by its priority state.
casetracker_views_project_name Views field handler; display the name of the Case Tracker project.
casetracker_views_project_number Views field handler; display the project number of the Case Tracker project.
casetracker_views_project_options Views filter handler; filter Case Tracker cases by their project.
casetracker_views_status_name Views field handler; display the status state of the case.
casetracker_views_status_options Views filter handler; filter Case Tracker cases by its status state.
casetracker_views_tables Implementation of hook_views_table().
casetracker_views_type_name Views field handler; display the type state of the case.
casetracker_views_type_options Views filter handler; filter Case Tracker cases by its type state.
casetracker_views_user_name Views field handler; display the user name the case has been assigned to.
casetracker_views_user_options Views filter handler; filter Case Tracker cases by the assigned user.