You are here

pmticket.module in Drupal PM (Project Management) 7

1: Hooks 2: Access functions

File

pmticket/pmticket.module
View source
<?php

/**
 * @file
 *
 * 1: Hooks
 * 2: Access functions
 */

/**
 * Implements hook_help().
 */
function pmticket_help($path, $arg) {
  $o = '';
  switch ($path) {
    case "admin/help#pmticket":
      $o = '<p>' . t("Provides ticket support for Project Management") . '</p>';
      break;
  }
  return $o;
}

/**
 * Implements hook_permission().
 */
function pmticket_permission() {
  return array(
    'Project Management Ticket: access' => array(
      'title' => t('Access PM Ticket'),
      'description' => t('Allows the user to see pages and blocks associated with the PM Ticket module, but does not control which tickets are shown within them.'),
    ),
    'Project Management Ticket: add' => array(
      'title' => t('Add PM Ticket'),
      'description' => t('Allows the user to create a ticket.'),
    ),
    'Project Management Ticket: delete all' => array(
      'title' => t('Delete Any PM Ticket'),
      'description' => t('Allows the user to delete any ticket.'),
    ),
    'Project Management Ticket: delete own' => array(
      'title' => t('Delete Authored PM Ticket'),
      'description' => t('For tickets authored by the user, allows the user to delete the ticket.'),
    ),
    'Project Management Ticket: delete of user organization' => array(
      'title' => t('Delete PM Ticket in own organization'),
      'description' => t('For tickets assigned to the same PM Organization as a PM Person, allows the user associated with that Person to delete the ticket.'),
    ),
    'Project Management Ticket: delete if assigned to ticket' => array(
      'title' => t('Delete PM Ticket if user assigned to ticket'),
      'description' => t('For tickets assigned to a PM Person or Team, allows users associated with that Person or Team to delete the ticket.'),
    ),
    'Project Management Ticket: edit all' => array(
      'title' => t('Edit Any PM Ticket'),
      'description' => t('Allows the user to edit any ticket.'),
    ),
    'Project Management Ticket: edit own' => array(
      'title' => t('Edit Authored PM Ticket'),
      'description' => t('For tickets authored by the user, allows the user to edit the ticket.'),
    ),
    'Project Management Ticket: edit of user organization' => array(
      'title' => t('Edit PM Ticket in own Organization'),
      'description' => t('For tickets assigned to the same PM Organization as a PM Person, allows the user associated with that Person to edit the ticket.'),
    ),
    'Project Management Ticket: edit if assigned to ticket' => array(
      'title' => t('Edit PM Ticket if assigned to ticket'),
      'description' => t('For tickets assigned to a PM Person or Team, allows users associated with that Person or Team to edit the ticket.'),
    ),
    'Project Management Ticket: view all' => array(
      'title' => t('View Any PM Ticket'),
      'description' => t('Allows the user to view any ticket and see any ticket in lists or dropdowns elsewhere on the site.'),
    ),
    'Project Management Ticket: view own' => array(
      'title' => t('View Authored PM Ticket'),
      'description' => t('For tickets authored by the user, allows the user to view the ticket and see the ticket in lists or dropdowns elsewhere on the site.'),
    ),
    'Project Management Ticket: view of user organization' => array(
      'title' => t('View PM Ticket in own Organization'),
      'description' => t('For tickets assigned to the same PM Organization as a PM Person, allows the user associated with that Person to view the ticket and see the ticket in lists or dropdowns elsewhere on the site.'),
    ),
    'Project Management Ticket: view if assigned to ticket' => array(
      'title' => t('View PM Ticket if assigned to user'),
      'description' => t('For tickets assigned to a PM Person or Team, allows users associated with that Person or Team to view the ticket and see the ticket in lists or dropdowns elsewhere on the site.'),
    ),
  );
}

/**
 * Implements hook_node_access().
 */
function pmticket_node_access($node, $op, $account = NULL) {
  $type = is_string($node) ? $node : $node->type;
  if ($type == 'pmticket') {

    // If no account is specified, assume that the check is against the current logged in user
    if (is_null($account)) {
      global $user;
      $account = $user;
    }
    if ($op == 'create' and user_access('Project Management Ticket: add', $account)) {
      return NODE_ACCESS_ALLOW;
    }
  }
  return NODE_ACCESS_IGNORE;
}

/**
 * Implements hook_theme().
 */
function pmticket_theme() {
  return array(
    'pmticket_view' => array(
      'file' => 'pmticket.theme.inc',
      'variables' => array(
        'node',
        'view_mode',
      ),
    ),
  );
}

/**
 * Implements hook_node_info().
 */
function pmticket_node_info() {
  return array(
    'pmticket' => array(
      'name' => t('Ticket'),
      'base' => 'pmticket',
      'description' => t("A ticket for Project Management."),
      'title_label' => t("Title"),
    ),
  );
}

/**
 * Implements hook_field_extra_fields().
 */
function pmticket_field_extra_fields() {
  $extra['node']['pmticket'] = array(
    'form' => array(
      'group1' => array(
        'label' => 'Organization/Project/Task Group',
        'weight' => -20,
      ),
      'group2' => array(
        'label' => 'Category/Status/Priority Group',
        'weight' => -19,
      ),
      'group3' => array(
        'label' => 'Duration Group',
        'weight' => -16,
      ),
      'group4' => array(
        'label' => 'Price Group',
        'weight' => -15,
      ),
      'group5' => array(
        'label' => 'Assigned to',
        'weight' => -14,
      ),
    ),
  );
  return $extra;
}

/**
 * Implements hook_field_extra_fields_alter().
 */
function pmticket_field_extra_fields_alter(&$info) {
  $info['node']['pmticket']['form']['title']['weight'] = -18;
}

/**
 * Implements hook_pmorganization_change().
 */
function pmticket_pmorganization_change($organization_nid, $organization_title) {
  db_update('pmticket')
    ->fields(array(
    'organization_title' => $organization_title,
  ))
    ->condition('organization_nid', $organization_nid)
    ->execute();
}

/**
 * Implements hook_pmproject_change().
 */
function pmticket_pmproject_change($project_nid, $project_title) {
  db_update('pmticket')
    ->fields(array(
    'project_title' => $project_title,
  ))
    ->condition('project_nid', $project_nid)
    ->execute();
}

/**
 * Implements hook_pmtask_change().
 */
function pmticket_pmtask_change($task_nid, $task_title, $task_stepno) {
  db_update('pmticket')
    ->fields(array(
    'task_title' => $task_title,
    'task_stepno' => $task_stepno,
  ))
    ->condition('task_nid', $task_nid)
    ->execute();
}

/**
 * Implements hook_pmproject_change_hierarchy().
 */
function pmticket_pmproject_change_hierarchy($project_nid, $organization_nid, $organization_title) {
  db_update('pmticket')
    ->fields(array(
    'organization_nid' => $organization_nid,
    'organization_title' => $organization_title,
  ))
    ->condition('project_nid', $project_nid)
    ->execute();
}

/**
 * Implements hook_pmtask_change_hierarchy().
 */
function pmticket_pmtask_change_hierarchy($task_nid, $organization_nid, $organization_title, $project_nid, $project_title) {
  db_update('pmticket')
    ->fields(array(
    'organization_nid' => $organization_nid,
    'organization_title' => $organization_title,
    'project_nid' => $project_nid,
    'project_title' => $project_title,
  ))
    ->condition('task_nid', $task_nid)
    ->execute();
}

/**
 * Implements hook_form().
 */
function pmticket_form(&$node, $form_state) {
  $breadcrumb = array(
    l(t('Project Management'), 'pm'),
    l(t('Tickets'), 'pm/tickets'),
  );
  drupal_set_breadcrumb($breadcrumb);
  $type = node_type_get_type($node);
  $info = field_info_extra_fields('node', 'pmticket', 'form');
  $form['#attributes']['class'] = 'pmcomponent_node_form';
  $form['#prefix'] = '<div id="pmticket-form">';
  $form['#suffix'] = '</div>';
  $form['group1'] = array(
    '#type' => 'markup',
    '#theme' => 'pm_form_group',
    '#weight' => $info['group1']['weight'],
  );
  $org_query = db_select('node', 'n');
  $org_query
    ->join('pmorganization', 'sor', 'n.vid = sor.vid');
  $org_query
    ->fields('n', array(
    'nid',
    'title',
  ))
    ->condition('n.status', 1)
    ->condition('n.type', 'pmorganization')
    ->addTag('node_access')
    ->orderBy('n.title', 'ASC');
  $org_result = $org_query
    ->execute();
  $organizations = array();
  foreach ($org_result as $organization) {
    $organizations[$organization->nid] = $organization->title;
    if (!isset($node->organization_nid)) {
      $node->organization_nid = $organization->nid;
    }
  }
  $form['group1']['organization_nid'] = array(
    '#type' => 'select',
    '#title' => t('Organization'),
    '#default_value' => isset($node->organization_nid) ? $node->organization_nid : NULL,
    '#options' => $organizations,
    '#required' => TRUE,
    '#ajax' => array(
      'callback' => 'pmticket_form_ajax',
      'wrapper' => 'pmticket-form',
    ),
  );
  if (isset($node->organization_nid)) {
    $organization_nid = isset($form_state['values']['organization_nid']) ? $form_state['values']['organization_nid'] : $node->organization_nid;
  }
  else {
    drupal_set_message(t('Please add an organization to the system before trying to add a ticket.'), 'error');
  }
  $pro_query = db_select('node', 'n');
  $pro_query
    ->join('pmproject', 'spr', 'n.vid = spr.vid');
  $pro_result = $pro_query
    ->fields('n', array(
    'nid',
    'title',
  ))
    ->condition('spr.organization_nid', $organization_nid)
    ->condition('n.status', 1)
    ->condition('n.type', 'pmproject')
    ->orderBy('n.title', 'ASC')
    ->addTag('node_access')
    ->execute();
  $projects = array();
  foreach ($pro_result as $project) {
    $projects[$project->nid] = $project->title;
  }
  $form['group1']['project_nid'] = array(
    '#type' => 'select',
    '#title' => t('Project'),
    '#default_value' => isset($node->project_nid) ? $node->project_nid : 0,
    '#options' => array(
      0 => '-',
    ) + $projects,
    '#prefix' => '<div id="pmticket-project-nid">',
    '#suffix' => '</div>',
    '#ajax' => array(
      'callback' => 'pmticket_form_ajax',
      'wrapper' => 'pmticket-form',
    ),
  );
  $project_nid = isset($form_state['values']['project_nid']) ? $form_state['values']['project_nid'] : (isset($node->project_nid) ? $node->project_nid : 0);
  $tree = isset($project_nid) ? _pmtask_get_tree($project_nid) : array();
  $tasks = _pmtask_plain_tree($tree);
  $form['group1']['task_nid'] = array(
    '#type' => 'select',
    '#title' => t('Task'),
    '#default_value' => isset($node->task_nid) ? $node->task_nid : 0,
    '#options' => array(
      0 => '-',
    ) + $tasks,
    '#prefix' => '<div id="pmticket-task-nid">',
    '#suffix' => '</div>',
  );
  $form['group1']['task_nid_old'] = array(
    '#type' => 'hidden',
    '#default_value' => isset($node->task_nid) ? $node->task_nid : NULL,
  );
  $form['group2'] = array(
    '#type' => 'markup',
    '#theme' => 'pm_form_group',
    '#weight' => $info['group2']['weight'],
  );
  $category_list = pm_attributes_bydomain('Ticket category');
  $form['group2']['ticketcategory'] = array(
    '#type' => 'select',
    '#title' => t('Category'),
    '#default_value' => isset($node->ticketcategory) ? $node->ticketcategory : $category_list['default'],
    '#options' => $category_list['values'],
  );
  $status_list = pm_attributes_bydomain('Ticket status');
  $form['group2']['ticketstatus'] = array(
    '#type' => 'select',
    '#title' => t('Status'),
    '#default_value' => isset($node->ticketstatus) ? $node->ticketstatus : $status_list['default'],
    '#options' => $status_list['values'],
  );
  $priority_list = pm_attributes_bydomain('Ticket priority');
  $form['group2']['ticketpriority'] = array(
    '#type' => 'select',
    '#title' => t('Priority'),
    '#default_value' => isset($node->ticketpriority) ? $node->ticketpriority : $priority_list['default'],
    '#options' => $priority_list['values'],
  );
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => check_plain($type->title_label),
    '#required' => TRUE,
    '#default_value' => $node->title,
    '#weight' => $info['title']['weight'],
  );
  $form['group3'] = array(
    '#type' => 'markup',
    '#theme' => 'pm_form_group',
    '#weight' => $info['group3']['weight'],
  );
  $durationunits = pm_attributes_bydomain('Duration unit');
  $form['group3']['durationunit'] = array(
    '#type' => 'select',
    '#title' => t('Duration unit'),
    '#default_value' => isset($node->durationunit) ? $node->durationunit : $durationunits['default'],
    '#options' => $durationunits['values'],
  );
  $form['group3']['duration'] = array(
    '#type' => 'textfield',
    '#title' => t('Duration'),
    '#size' => 20,
    '#default_value' => isset($node->duration) ? $node->duration : NULL,
  );
  $form['group4'] = array(
    '#type' => 'markup',
    '#theme' => 'pm_form_group',
    '#weight' => 3,
    '#weight' => $info['group4']['weight'],
  );
  $pricemodes = pm_attributes_bydomain('Price mode');
  $form['group4']['pricemode'] = array(
    '#type' => 'select',
    '#title' => t('Price mode'),
    '#default_value' => isset($node->pricemode) ? $node->pricemode : $pricemodes['default'],
    '#options' => array(
      '-' => '-',
    ) + $pricemodes['values'],
  );
  $form['group4']['price'] = array(
    '#title' => t('Price'),
    '#type' => 'textfield',
    '#size' => 15,
    '#default_value' => isset($node->price) ? $node->price : NULL,
  );
  $currencies = pm_attributes_bydomain('Currency');
  $form['group4']['currency'] = array(
    '#type' => 'select',
    '#title' => t('Price currency'),
    '#default_value' => isset($node->currency) ? $node->currency : $currencies['default'],
    '#options' => array(
      '-' => '-',
    ) + $currencies['values'],
  );
  $form['group5'] = array(
    '#type' => 'markup',
    '#theme' => 'pm_form_group',
    '#weight' => $info['group5']['weight'],
  );
  $options = isset($project_nid) ? pm_get_assignment_options($organization_nid, $project_nid) : pm_get_assignment_options($organization_nid);
  $form['group5']['assigned_nid'] = array(
    '#type' => 'select',
    '#title' => t('Assigned to'),
    '#options' => $options,
    '#default_value' => isset($node->assigned_nid) ? $node->assigned_nid : NULL,
  );

  // Check to see if the body field is still there, if so, display it.
  $body = field_get_items('pmticket', $node, 'body');
  if ($body) {
    $form['body_field'] = $body;
  }
  $form['title_old'] = array(
    '#type' => 'hidden',
    '#default_value' => isset($node->title_old) ? $node->title_old : NULL,
  );
  return $form;
}

/**
 * Ajax callback for the pmticket node form.
 */
function pmticket_form_ajax(&$form, &$form_state) {
  return $form;
}

/**
 * Implements hook_insert().
 */
function pmticket_insert($node) {
  _pmticket_beforesave($node);
  db_insert('pmticket')
    ->fields(array(
    'vid' => $node->vid,
    'nid' => $node->nid,
    'organization_nid' => $node->organization_nid,
    'organization_title' => $node->organization_title,
    'project_nid' => $node->project_nid,
    'project_title' => $node->project_title,
    'task_nid' => $node->task_nid,
    'task_title' => $node->task_title,
    'task_stepno' => $node->task_stepno,
    'ticketcategory' => $node->ticketcategory,
    'ticketstatus' => $node->ticketstatus,
    'ticketpriority' => $node->ticketpriority,
    'durationunit' => $node->durationunit,
    'duration' => $node->duration,
    'pricemode' => $node->pricemode,
    'price' => $node->price,
    'currency' => $node->currency,
    'assigned_nid' => $node->assigned_nid,
    'assigned_title' => $node->assigned_title,
  ))
    ->execute();
}

/**
 * Implements hook_update().
 */
function pmticket_update($node) {
  $org_query = db_select('node', 'n');
  $org_query
    ->join('pmorganization', 'sor', 'n.vid = sor.vid');
  $org_result = $org_query
    ->fields('n', array(
    'title',
  ))
    ->condition('n.type', 'pmorganization')
    ->condition('n.nid', $node->organization_nid)
    ->execute();
  $o = $org_result
    ->fetchObject();
  $node->organization_title = $o->title;
  $pro_query = db_select('node', 'n');
  $pro_query
    ->join('pmproject', 'spr', 'n.vid = spr.vid');
  $pro_result = $pro_query
    ->fields('n', array(
    'title',
  ))
    ->condition('n.type', 'pmproject')
    ->condition('n.nid', $node->project_nid)
    ->execute();
  $p = $pro_result
    ->fetchObject();
  if ($p) {
    $node->project_title = $p->title;
  }
  else {
    $node->project_title = '';
  }
  $task_query = db_select('node', 'n');
  $task_query
    ->join('pmtask', 'sta', 'n.vid = sta.vid');
  $task_result = $task_query
    ->fields('n', array(
    'title',
  ))
    ->condition('n.type', 'pmtask')
    ->condition('n.nid', $node->task_nid)
    ->execute();
  if ($ta = $task_result
    ->fetchObject()) {
    $node->task_title = $ta->title;
  }
  if ($node->task_nid != $node->task_nid_old) {
    module_invoke_all('pmticket_change_hierarchy', $node->nid, $node->organization_nid, $node->organization_title, $node->project_nid, $node->project_title, $node->task_nid, $node->task_title);
  }

  // if this is a new node or we're adding a new revision,
  if ($node->revision) {
    pmticket_insert($node);
  }
  else {
    _pmticket_beforesave($node);
    db_update('pmticket')
      ->fields(array(
      'organization_nid' => $node->organization_nid,
      'organization_title' => $node->organization_title,
      'project_nid' => $node->project_nid,
      'project_title' => $node->project_title,
      'task_nid' => $node->task_nid,
      'task_title' => $node->task_title,
      'task_stepno' => $node->task_stepno,
      'ticketcategory' => $node->ticketcategory,
      'ticketstatus' => $node->ticketstatus,
      'ticketpriority' => $node->ticketpriority,
      'durationunit' => $node->durationunit,
      'duration' => $node->duration,
      'pricemode' => $node->pricemode,
      'price' => $node->price,
      'currency' => $node->currency,
      'assigned_nid' => $node->assigned_nid,
      'assigned_title' => $node->assigned_title,
    ))
      ->condition('vid', $node->vid)
      ->execute();
    if ($node->title != $node->title_old) {
      module_invoke_all('pmticket_change', $node->nid, $node->title);
    }
  }
}

/**
 * Prepares pmticket for saving.
 */
function _pmticket_beforesave(&$node) {
  $org_query = db_select('node', 'n');
  $org_query
    ->join('pmorganization', 'sor', 'n.vid = sor.vid');
  $org_result = $org_query
    ->fields('n', array(
    'title',
  ))
    ->condition('n.type', 'pmorganization')
    ->condition('n.nid', $node->organization_nid)
    ->execute();
  $o = $org_result
    ->fetchObject();
  $node->organization_title = $o->title;
  $pro_query = db_select('node', 'n');
  $pro_query
    ->join('pmproject', 'spr', 'n.vid = spr.vid');
  $pro_result = $pro_query
    ->fields('n', array(
    'title',
  ))
    ->condition('n.type', 'pmproject')
    ->condition('n.nid', $node->project_nid)
    ->execute();
  $p = $pro_result
    ->fetchObject();
  $node->project_title = isset($p->title) ? $p->title : '';
  $task_query = db_select('node', 'n');
  $task_query
    ->join('pmtask', 'sta', 'n.vid = sta.vid');
  $task_result = $task_query
    ->fields('n', array(
    'title',
  ))
    ->fields('sta', array(
    'stepno',
  ))
    ->condition('n.type', 'pmtask')
    ->condition('n.nid', $node->task_nid)
    ->execute();
  $ta = $task_result
    ->fetchObject();
  $node->task_title = isset($ta->title) ? $ta->title : '';
  $node->task_stepno = isset($ta->stepno) ? $ta->stepno : '';
  $assigned = node_load($node->assigned_nid);
  $node->assigned_title = isset($assigned->title) ? $assigned->title : '';

  // Allow use of comma when inputting numerical values - str_replace with period decimal
  $node->duration = str_replace(',', '.', $node->duration);
  $node->price = str_replace(',', '.', $node->price);
  if (!is_numeric($node->duration)) {
    $node->duration = 0;
  }
  if (!is_numeric($node->price)) {
    $node->price = 0;
  }
}

/**
 * Implements hook_node_revision_delete().
 */
function pmticket_node_revision_delete($node) {

  // Notice that we're matching a single revision based on the node's vid.
  db_delete('pmticket')
    ->condition('vid', $node->vid)
    ->execute();
}

/**
 * Implements hook_delete().
 */
function pmticket_delete($node) {

  // Notice that we're matching all revision, by using the node's nid.
  db_delete('pmticket')
    ->condition('nid', $node->nid)
    ->execute();
}

/**
 * Implements hook_load().
 */
function pmticket_load($nodes) {
  foreach ($nodes as $nid => &$node) {
    $result = db_select('pmticket', 'sti')
      ->fields('sti')
      ->condition('vid', $node->vid)
      ->execute();
    $record = $result
      ->fetchAssoc();
    foreach ($record as $key => $value) {
      $node->{$key} = $value;
    }
    $node->title_old = $node->title;
  }
}

/**
 * Implements hook_view().
 */
function pmticket_view($node, $view_mode) {
  $breadcrumb = array();
  $breadcrumb[] = l(t('Project Management'), 'pm');
  $breadcrumb[] = l(t('Tickets'), 'pm/tickets');
  drupal_set_breadcrumb($breadcrumb);
  return theme('pmticket_view', array(
    'node' => $node,
    'view_mode' => $view_mode,
  ));
}

/**
 * Implements hook_views_api().
 */
function pmticket_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'pmticket'),
  );
}

/**
 * Implements hook_token_list().
 */
function pmticket_token_list($type = 'all') {
  $tokens = array();
  if ($type == 'node' || $type == 'all') {
    $tokens['node']['pmticket-organization-nid'] = t('Project Management Ticket: Organization Node ID.');
    $tokens['node']['pmticket-organization-title'] = t('Project Management Ticket: Organization Title.');
    $tokens['node']['pmticket-project-nid'] = t('Project Management Ticket: Project Node ID.');
    $tokens['node']['pmticket-project-title'] = t('Project Management Ticket: Project Title.');
    $tokens['node']['pmticket-ticketstatus'] = t('Project Management Ticket: Project Status.');
    $tokens['node']['pmticket-ticketcategory'] = t('Project Management Ticket: Ticket Category.');
    $tokens['node']['pmticket-ticketpriority'] = t('Project Management Ticket: Ticket Priority.');
    $tokens['node']['pmticket-durationunit'] = t('Project Management Ticket: Ticket Duration Unit.');
    $tokens['node']['pmticket-duration'] = t('Project Management Ticket: Ticket Duration.');
    $tokens['node']['pmticket-price'] = t('Project Management Ticket: Ticket Price.');
    $tokens['node']['pmticket-pricemode'] = t('Project Management Ticket: Ticket Price Mode.');
    if (module_exists('pmperson')) {

      // Assignee(s)
      $tokens['node']['pmticket-assigned-to-email'] = t('Project Management Ticket: Ticket Assignee(s) Email Address(s).');
      $tokens['node']['pmticket-assigned-title'] = t('Project Management Ticket: Ticket Assignee(s) Title(s).');
    }
  }
  return $tokens;
}

/**
 * Implements hook_token_values().
 */
function pmticket_token_values($type, $object = NULL) {
  $values = array();
  $node = $object;
  if (($type == 'node' || $type == 'all') && $node->type === 'pmticket') {
    $values['pmticket-organization-nid'] = $node->organization_nid;
    $values['pmticket-organization-title'] = $node->organization_title;
    $values['pmticket-project-nid'] = $node->project_nid;
    $values['pmticket-project-title'] = $node->project_title;
    $values['pmticket-ticketstatus'] = $node->ticketstatus;
    $values['pmticket-ticketpriority'] = $node->ticketpriority;
    $values['pmticket-ticketcategory'] = $node->ticketcategory;
    $values['pmticket-durationunit'] = $node->durationunit;
    $values['pmticket-duration'] = $node->duration;
    $values['pmticket-price'] = $node->price;
    $values['pmticket-pricemode'] = $node->pricemode;
    if (module_exists('pmperson')) {

      // Project Assignee(s) e-mail
      if ($node->assigned_nid) {
        $values['pmticket-assigned-title'] = $node->assigned_title;
        $assignees_node = node_load($node->assigned_nid);

        // Assigned to one person
        if ($assignees_node->type === 'pmperson') {
          $values['pmticket-assigned-to-email'] = pmperson_primary_email($assignees_node);
        }
        else {
          $assignees_array = $assignees_node->members_array;
          $assignees = array();
          foreach ($assignees_array as $nid => $name) {
            $assignee = node_load($nid);
            $assignees[] = pmperson_primary_email($assignee);
          }
          $assigned = implode(", ", $assignees);

          // Return comma separated list of emails
          $values['pmticket-assigned-to-email'] = $assigned;
        }
      }
      else {
        $values['pmticket-assigned-to-email'] = NULL;
      }
    }
  }
  return $values;
}

/**
 * Implements hook_node_access_records().
 */
function pmticket_node_access_records($node) {
  if (empty($node->status)) {

    // Lets Drupal take care of permission to unpublished nodes.
    return array();
  }
  $type = is_string($node) ? $node : $node->type;
  $grants = array();
  if ($type == 'pmticket') {

    // Project Management Ticket: view all
    $grants[] = array(
      'realm' => 'pmticket_view_all',
      'gid' => 0,
      'grant_view' => 1,
      'grant_update' => 0,
      'grant_delete' => 0,
      'priority' => 0,
    );

    // Project Management Ticket: view own
    $grants[] = array(
      'realm' => 'pmticket_view_own',
      'gid' => $node->uid,
      'grant_view' => 1,
      'grant_update' => 0,
      'grant_delete' => 0,
      'priority' => 0,
    );

    // Project Management Ticket: view of user organization
    if (isset($node->organization_nid) && !empty($node->organization_nid)) {
      $grants[] = array(
        'realm' => 'pmticket_view_if_user_organization',
        'gid' => $node->organization_nid,
        'grant_view' => 1,
        'grant_update' => 0,
        'grant_delete' => 0,
        'priority' => 0,
      );
    }

    // Project Management Ticket: view if assigned to ticket
    if (isset($node->assigned_nid) && !empty($node->assigned_nid)) {
      $grants[] = array(
        'realm' => 'pmticket_view_if_assigned',
        'gid' => $node->assigned_nid,
        'grant_view' => 1,
        'grant_update' => 0,
        'grant_delete' => 0,
        'priority' => 0,
      );
    }

    // Project Management Ticket: edit all
    $grants[] = array(
      'realm' => 'pmticket_update_all',
      'gid' => 0,
      'grant_view' => 0,
      'grant_update' => 1,
      'grant_delete' => 0,
      'priority' => 0,
    );

    // Project Management Ticket: edit own
    $grants[] = array(
      'realm' => 'pmticket_update_own',
      'gid' => $node->uid,
      'grant_view' => 0,
      'grant_update' => 1,
      'grant_delete' => 0,
      'priority' => 0,
    );

    // Project Management Ticket: edit of user organization
    if (isset($node->organization_nid) && !empty($node->organization_nid)) {
      $grants[] = array(
        'realm' => 'pmticket_update_if_user_organization',
        'gid' => $node->organization_nid,
        'grant_view' => 0,
        'grant_update' => 1,
        'grant_delete' => 0,
        'priority' => 0,
      );
    }

    // Project Management Ticket: edit if assigned to ticket
    if (isset($node->assigned_nid) && !empty($node->assigned_nid)) {
      $grants[] = array(
        'realm' => 'pmticket_update_if_assigned',
        'gid' => $node->assigned_nid,
        'grant_view' => 0,
        'grant_update' => 1,
        'grant_delete' => 0,
        'priority' => 0,
      );
    }

    // Project Management Ticket: delete all
    $grants[] = array(
      'realm' => 'pmticket_delete_all',
      'gid' => 0,
      'grant_view' => 0,
      'grant_update' => 0,
      'grant_delete' => 1,
      'priority' => 0,
    );

    // Project Management Ticket: delete own
    $grants[] = array(
      'realm' => 'pmticket_delete_own',
      'gid' => $node->uid,
      'grant_view' => 0,
      'grant_update' => 0,
      'grant_delete' => 1,
      'priority' => 0,
    );

    // Project Management Ticket: delete of user organization
    if (isset($node->organization_nid) && !empty($node->organization_nid)) {
      $grants[] = array(
        'realm' => 'pmticket_delete_if_user_organization',
        'gid' => $node->organization_nid,
        'grant_view' => 0,
        'grant_update' => 0,
        'grant_delete' => 1,
        'priority' => 0,
      );
    }

    // Project Management Ticket: delete if assigned to ticket
    if (isset($node->assigned_nid) && !empty($node->assigned_nid)) {
      $grants[] = array(
        'realm' => 'pmticket_delete_if_assigned',
        'gid' => $node->assigned_nid,
        'grant_view' => 0,
        'grant_update' => 0,
        'grant_delete' => 1,
        'priority' => 0,
      );
    }
  }
  return $grants;
}

/**
 * Implements hook_node_grants().
 */
function pmticket_node_grants($account, $op) {
  if (module_exists('pmperson')) {
    _pmperson_user_load($account);
    if (!empty($account->pmteam) and is_array($account->pmteam)) {
      $assigned_nids = $account->pmteam;
    }
    if (isset($account->pmperson_nid) and !empty($account->pmperson_nid)) {
      $assigned_nids[] = $account->pmperson_nid;
    }
  }
  $grants = array();
  switch ($op) {
    case 'view':
      if (user_access('Project Management Ticket: view all', $account)) {
        $grants['pmticket_view_all'] = array(
          0,
        );
      }
      if (user_access('Project Management Ticket: view own', $account)) {
        $grants['pmticket_view_own'] = array(
          $account->uid,
        );
      }
      if (user_access('Project Management Ticket: view of user organization', $account) and !empty($account->pmorganization_nid)) {
        $grants['pmticket_view_if_user_organization'] = array(
          $account->pmorganization_nid,
        );
      }
      if (isset($account->pmperson_nid)) {
        if (user_access('Project Management Ticket: view if assigned to ticket', $account)) {
          $grants['pmticket_view_if_assigned'] = $assigned_nids;
        }
      }
      break;
    case 'update':
      if (user_access('Project Management Ticket: edit all', $account)) {
        $grants['pmticket_update_all'] = array(
          0,
        );
      }
      if (user_access('Project Management Ticket: edit own', $account)) {
        $grants['pmticket_update_own'] = array(
          $account->uid,
        );
      }
      if (user_access('Project Management Ticket: edit of user organization', $account) and !empty($account->pmorganization_nid)) {
        $grants['pmticket_update_if_user_organization'] = array(
          $account->pmorganization_nid,
        );
      }
      if (isset($account->pmperson_nid)) {
        if (user_access('Project Management Ticket: edit if assigned to ticket', $account)) {
          $grants['pmticket_update_if_assigned'] = $assigned_nids;
        }
      }
      break;
    case 'delete':
      if (user_access('Project Management Ticket: delete all', $account)) {
        $grants['pmticket_delete_all'] = array(
          0,
        );
      }
      if (user_access('Project Management Ticket: delete own', $account)) {
        $grants['pmticket_delete_own'] = array(
          $account->uid,
        );
      }
      if (user_access('Project Management Ticket: delete of user organization', $account) and !empty($account->pmorganization_nid)) {
        $grants['pmticket_delete_if_user_organization'] = array(
          $account->pmorganization_nid,
        );
      }
      if (isset($account->pmperson_nid)) {
        if (user_access('Project Management Ticket: delete if assigned to ticket', $account)) {
          $grants['pmticket_delete_if_assigned'] = $assigned_nids;
        }
      }
      break;
  }
  return $grants;
}

/**
 * Implements hook_pm_dashboard_links().
 */
function pmticket_pm_dashboard_links($type) {
  $links = array();
  if ($type == 'page' || $type == 'block') {
    $links[] = array(
      'theme' => 'pm_dashboard_link',
      'title' => t('Tickets'),
      'icon' => 'pmtickets',
      'path' => 'pm/tickets',
      'params' => array(),
      'node_type' => 'pmticket',
      'add_type' => 'pmticket',
      'map' => array(),
      'weight' => 6,
    );
  }
  return $links;
}

Functions

Namesort descending Description
pmticket_delete Implements hook_delete().
pmticket_field_extra_fields Implements hook_field_extra_fields().
pmticket_field_extra_fields_alter Implements hook_field_extra_fields_alter().
pmticket_form Implements hook_form().
pmticket_form_ajax Ajax callback for the pmticket node form.
pmticket_help Implements hook_help().
pmticket_insert Implements hook_insert().
pmticket_load Implements hook_load().
pmticket_node_access Implements hook_node_access().
pmticket_node_access_records Implements hook_node_access_records().
pmticket_node_grants Implements hook_node_grants().
pmticket_node_info Implements hook_node_info().
pmticket_node_revision_delete Implements hook_node_revision_delete().
pmticket_permission Implements hook_permission().
pmticket_pmorganization_change Implements hook_pmorganization_change().
pmticket_pmproject_change Implements hook_pmproject_change().
pmticket_pmproject_change_hierarchy Implements hook_pmproject_change_hierarchy().
pmticket_pmtask_change Implements hook_pmtask_change().
pmticket_pmtask_change_hierarchy Implements hook_pmtask_change_hierarchy().
pmticket_pm_dashboard_links Implements hook_pm_dashboard_links().
pmticket_theme Implements hook_theme().
pmticket_token_list Implements hook_token_list().
pmticket_token_values Implements hook_token_values().
pmticket_update Implements hook_update().
pmticket_view Implements hook_view().
pmticket_views_api Implements hook_views_api().
_pmticket_beforesave Prepares pmticket for saving.