You are here

pmtask.module in Drupal PM (Project Management) 7

File

pmtask/pmtask.module
View source
<?php

/**
 * @file
 */

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

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

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

    // 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 Task: add', $account)) {
      return NODE_ACCESS_ALLOW;
    }
  }
  return NODE_ACCESS_IGNORE;
}

/**
 * Implements hook_theme().
 */
function pmtask_theme() {
  return array(
    'pmtask_view' => array(
      'file' => 'pmtask.theme.inc',
      'variables' => array(
        'node',
        'view_mode',
      ),
    ),
  );
}

/**
 * Implements hook_node_info().
 */
function pmtask_node_info() {
  return array(
    'pmtask' => array(
      'name' => t('Task'),
      'base' => 'pmtask',
      'description' => t("A task for Project Management."),
      'title_label' => t("Title"),
    ),
  );
}

/**
 * Implements hook_field_extra_fields().
 */
function pmtask_field_extra_fields() {
  $extra['node']['pmtask'] = array(
    'form' => array(
      'group1' => array(
        'label' => 'Organization/Project/Task/Weight/Step Group',
        'weight' => -20,
      ),
      'group2' => array(
        'label' => 'Category/Status/Priority Group',
        'weight' => -18,
      ),
      'group3' => array(
        'label' => 'Duration Group',
        'weight' => -16,
      ),
      'group4' => array(
        'label' => 'Price Group',
        'weight' => -15,
      ),
      'group5' => array(
        'label' => 'Assigned to',
        'weight' => -14,
      ),
      'group6' => array(
        'label' => 'Billable / Billed Group',
        'weight' => -13,
      ),
    ),
  );
  return $extra;
}

/**
 * Implements hook_field_extra_fields_alter().
 */
function pmtask_field_extra_fields_alter(&$info) {
  $info['node']['pmtask']['form']['title']['weight'] = -19;
}

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

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

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

/**
 * Implements hook_form().
 */
function pmtask_form($node, &$form_state) {
  $breadcrumb = array(
    l(t('Project Management'), 'pm'),
    l(t('Tasks'), 'pm/tasks'),
  );
  drupal_set_breadcrumb($breadcrumb);
  $type = node_type_get_type($node);
  $info = field_info_extra_fields('node', 'pmtask', 'form');
  $form['#prefix'] = '<div id="pmtask-form-wrapper">';
  $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_result = $org_query
    ->fields('n', array(
    'nid',
    'title',
  ))
    ->condition('n.status', 1)
    ->condition('sor.isactive', 1)
    ->condition('n.type', 'pmorganization')
    ->addTag('node_access')
    ->orderBy('n.title', 'ASC')
    ->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' => 'pmtask_form_ajax',
      'wrapper' => 'pmtask-form-wrapper',
    ),
  );
  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 task.'), 'error');
    $organization_nid = NULL;
  }
  $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('n.status', 1)
    ->condition('spr.organization_nid', $organization_nid)
    ->condition('n.type', 'pmproject')
    ->addTag('node_access')
    ->orderBy('n.title', 'ASC')
    ->execute();
  $projects = array();
  foreach ($pro_result as $project) {
    $projects[$project->nid] = $project->title;
    if (!isset($node->project_nid)) {
      $node->project_nid = $project->nid;
    }
  }
  $form['group1']['project_nid'] = array(
    '#type' => 'select',
    '#title' => t('Project'),
    '#default_value' => isset($node->project_nid) ? $node->project_nid : NULL,
    '#options' => $projects,
    '#required' => TRUE,
    '#prefix' => '<div id="pmtask-project-nid">',
    '#suffix' => '</div>',
    '#ajax' => array(
      'callback' => 'pmtask_form_ajax',
      'wrapper' => 'pmtask-form-wrapper',
    ),
  );
  $form['group1']['project_nid_old'] = array(
    '#type' => 'hidden',
    '#default_value' => isset($node->project_nid) ? $node->project_nid : NULL,
  );
  if (isset($node->project_nid)) {
    $project_nid = isset($form_state['values']['project_nid']) ? $form_state['values']['project_nid'] : $node->project_nid;
  }
  else {
    drupal_set_message(t('Please add a project to the system before trying to add a task.'), 'error');
    $project_nid = NULL;
  }
  $tree = is_null($project_nid) ? array() : _pmtask_get_tree($project_nid);
  $parent_tasks = _pmtask_plain_tree($tree);
  $form['group1']['parent_nid'] = array(
    '#type' => 'select',
    '#title' => t('Parent task'),
    '#default_value' => isset($node->parent_nid) ? $node->parent_nid : 0,
    '#options' => array(
      0 => '-',
    ) + $parent_tasks,
  );
  $form['group1']['weight'] = array(
    '#type' => 'weight',
    '#title' => t('Weight'),
    '#default_value' => isset($node->weight) ? $node->weight : 0,
  );
  $form['group1']['stepno'] = array(
    '#type' => 'textfield',
    '#title' => t('Step no.'),
    '#size' => 15,
    '#required' => FALSE,
    '#default_value' => isset($node->stepno) ? $node->stepno : NULL,
  );
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => check_plain($type->title_label),
    '#required' => TRUE,
    '#default_value' => $node->title,
    '#weight' => $info['title']['weight'],
  );
  $form['group2'] = array(
    '#type' => 'markup',
    '#theme' => 'pm_form_group',
    '#weight' => $info['group2']['weight'],
  );
  $taskcategory_list = pm_attributes_bydomain('Task category');
  $form['group2']['taskcategory'] = array(
    '#type' => 'select',
    '#title' => t('Category'),
    '#default_value' => isset($node->taskcategory) ? $node->taskcategory : $taskcategory_list['default'],
    '#options' => $taskcategory_list['values'],
  );
  $taskstatus_list = pm_attributes_bydomain('Task status');
  $form['group2']['taskstatus'] = array(
    '#type' => 'select',
    '#title' => t('Status'),
    '#default_value' => isset($node->taskstatus) ? $node->taskstatus : $taskstatus_list['default'],
    '#options' => $taskstatus_list['values'],
  );
  $taskpriority_list = pm_attributes_bydomain('Task priority');
  $form['group2']['taskpriority'] = array(
    '#type' => 'select',
    '#title' => t('Priority'),
    '#default_value' => isset($node->taskpriority) ? $node->taskpriority : $taskpriority_list['default'],
    '#options' => $taskpriority_list['values'],
  );
  $form['group3'] = array(
    '#type' => 'markup',
    '#theme' => 'pm_form_group',
    '#weight' => $info['group3']['weight'],
  );
  $durationunit_list = pm_attributes_bydomain('Duration unit');
  $form['group3']['durationunit'] = array(
    '#type' => 'select',
    '#title' => t('Duration unit'),
    '#default_value' => isset($node->durationunit) ? $node->durationunit : $durationunit_list['default'],
    '#options' => $durationunit_list['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' => $info['group4']['weight'],
  );
  $pricemode_list = pm_attributes_bydomain('Price mode');
  $form['group4']['pricemode'] = array(
    '#type' => 'select',
    '#title' => t('Price mode'),
    '#default_value' => isset($node->pricemode) ? $node->pricemode : $pricemode_list['default'],
    '#options' => $pricemode_list['values'],
  );
  $form['group4']['price'] = array(
    '#title' => t('Price'),
    '#type' => 'textfield',
    '#size' => 15,
    '#default_value' => isset($node->price) ? $node->price : NULL,
  );
  $currency_list = pm_attributes_bydomain('Currency');
  $form['group4']['currency'] = array(
    '#type' => 'select',
    '#title' => t('Price currency'),
    '#default_value' => isset($node->currency) ? $node->currency : $currency_list['default'],
    '#options' => $currency_list['values'],
  );
  $form['group5'] = array(
    '#type' => 'markup',
    '#theme' => 'pm_form_group',
    '#weight' => $info['group5']['weight'],
  );
  $options = pm_get_assignment_options($organization_nid, $project_nid);
  $assigned_nid = isset($form_state['values']['assigned_nid']) ? $form_state['values']['assigned_nid'] : (isset($node->assigned_nid) ? $node->assigned_nid : 0);
  $form['group5']['assigned_nid'] = array(
    '#type' => 'select',
    '#title' => t('Assigned to'),
    '#options' => $options,
    '#default_value' => $assigned_nid,
  );
  $form['group6'] = array(
    '#type' => 'markup',
    '#theme' => 'pm_form_group',
    '#weight' => $info['group6']['weight'],
  );

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

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

/**
 * Implements hook_insert().
 */
function pmtask_insert($node) {
  _pmtask_beforesave($node);
  db_insert('pmtask')
    ->fields(array(
    'vid' => $node->vid,
    'nid' => $node->nid,
    'stepno' => $node->stepno,
    'taskcategory' => $node->taskcategory,
    'taskstatus' => $node->taskstatus,
    'taskpriority' => $node->taskpriority,
    'organization_nid' => $node->organization_nid,
    'organization_title' => $node->organization_title,
    'project_nid' => $node->project_nid,
    'project_title' => $node->project_title,
    'parent_nid' => $node->parent_nid,
    'weight' => $node->weight,
    '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 pmtask_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();
  $node->project_title = $p->title;
  if ($node->project_nid != $node->project_nid_old) {
    module_invoke_all('pmtask_change_hierarchy', $node->nid, $node->organization_nid, $node->organization_title, $node->project_nid, $node->project_title);
  }

  // if this is a new node or we're adding a new revision,
  if ($node->revision) {
    pmtask_insert($node);
  }
  else {
    _pmtask_beforesave($node);
    db_update('pmtask')
      ->fields(array(
      'stepno' => $node->stepno,
      'taskcategory' => $node->taskcategory,
      'taskstatus' => $node->taskstatus,
      'taskpriority' => $node->taskpriority,
      'organization_nid' => $node->organization_nid,
      'organization_title' => $node->organization_title,
      'project_nid' => $node->project_nid,
      'project_title' => $node->project_title,
      'parent_nid' => $node->parent_nid,
      'weight' => $node->weight,
      '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 || $node->stepno != $node->stepno_old) {
      module_invoke_all('pmtask_change', $node->nid, $node->title, $node->stepno);
    }
  }
}

/**
 * Prepares pmtask for saving.
 */
function _pmtask_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',
  ))
    ->fields('spr', array(
    'organization_title',
  ))
    ->condition('n.type', 'pmproject')
    ->condition('n.nid', $node->project_nid)
    ->execute();
  $p = $pro_result
    ->fetchObject();
  $node->project_title = $p->title;
  $assigned = node_load($node->assigned_nid);
  $node->assigned_title = isset($assigned->title) ? $assigned->title : NULL;

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

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

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

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

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

/**
 * Implements hook_load().
 */
function pmtask_load($nodes) {
  foreach ($nodes as $nid => &$node) {
    $result = db_select('pmtask', 'sta')
      ->fields('sta')
      ->condition('vid', $node->vid)
      ->execute();
    $record = $result
      ->fetchAssoc();
    foreach ($record as $key => $value) {
      $node->{$key} = $value;
    }
    $node->title_old = $node->title;
    $node->stepno_old = isset($node->stepno) ? $node->stepno : NULL;
  }
}

/**
 * Implements hook_validate().
 */
function pmtask_validate(&$node) {
  if ($node->nid == $node->parent_nid && $node->parent_nid) {
    form_set_error('parent_nid', t('Impossible to assign itself as parent.'));
  }
}

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

/**
 * Determines whether the task tab of a project is visible.
 */
function _pmtask_tasks_access($node = NULL) {
  if ($node == NULL) {
    return FALSE;
  }
  if ($node->type == 'pmproject' && user_access('Project Management Task: access')) {
    return TRUE;
  }
  return FALSE;
}

/**
 * Provides a tree of tasks for a given project and (if selected) parent task.
 */
function _pmtask_get_tree($project_nid, $parent_nid = 0, $depth = -1, $max_depth = NULL, $where = NULL, $args = array()) {
  static $children, $parents, $tasks;
  $depth++;

  // We cache trees, so it's not CPU-intensive to call get_tree() on a term
  // and its children, too.
  if (!isset($children[$project_nid])) {
    $children[$project_nid] = array();
    $args = array_merge($args, array(
      $project_nid,
    ));
    $query = db_select('node', 'n');
    $query
      ->join('pmtask', 'pmta', 'n.vid = pmta.vid');
    $query
      ->fields('n', array(
      'title',
      'uid',
    ))
      ->fields('pmta')
      ->condition('n.status', 1)
      ->condition('n.type', 'pmtask')
      ->condition('pmta.project_nid', $args)
      ->addTag('node_access')
      ->orderBy('pmta.weight', 'ASC');
    $result = $query
      ->execute();
    foreach ($result as $task) {
      $children[$project_nid][$task->parent_nid][] = $task->nid;
      $parents[$project_nid][$task->nid][] = $task->parent_nid;
      $tasks[$project_nid][$task->nid] = $task;
    }
  }
  $max_depth = is_null($max_depth) ? count($children[$project_nid]) : $max_depth;
  if (isset($children[$project_nid][$parent_nid]) && $children[$project_nid][$parent_nid]) {
    foreach ($children[$project_nid][$parent_nid] as $child_nid) {
      if ($max_depth > $depth) {
        $task = clone $tasks[$project_nid][$child_nid];
        $task->depth = $depth;
        $task->parents = $parents[$project_nid][$child_nid];
        $tree[] = $task;
        if (isset($children[$project_nid][$child_nid])) {
          $tree = array_merge($tree, _pmtask_get_tree($project_nid, $child_nid, $depth, $max_depth, $where, $args));
        }
      }
    }
  }
  return isset($tree) ? $tree : array();
}

/**
 * Provides task details for tasks within a tree.
 */
function _pmtask_plain_tree($tree) {
  $rows = array();
  foreach ($tree as $item) {
    $nid = $item->nid;
    $title = check_plain($item->title);
    if ($item->stepno) {
      $title = check_plain($item->stepno) . ' ' . $title;
    }
    if ($nid) {
      $rows[$nid] = str_repeat('--', $item->depth) . ' ' . $title;
    }
  }
  return $rows;
}

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

/**
 * Implements hook_token_list().
 */
function pmtask_token_list($type = 'all') {
  $tokens = array();
  if ($type == 'node' || $type == 'all') {
    $tokens['node']['pmtask-step-no'] = t('Project Management Task: Step No.');
    $tokens['node']['pmtask-organization-nid'] = t('Project Management Task: Organization Node ID.');
    $tokens['node']['pmtask-organization-title'] = t('Project Management Task: Organization Title.');
    $tokens['node']['pmtask-project-nid'] = t('Project Management Task: Project Node ID.');
    $tokens['node']['pmtask-project-title'] = t('Project Management Task: Project Title.');
    $tokens['node']['pmtask-parent-nid'] = t('Project Management Task: Parent NID.');
    $tokens['node']['pmtask-taskstatus'] = t('Project Management Task: Project Status.');
    $tokens['node']['pmtask-taskcategory'] = t('Project Management Task: Task Category.');
    $tokens['node']['pmtask-taskpriority'] = t('Project Management Task: Task Priority.');
    $tokens['node']['pmtask-durationunit'] = t('Project Management Task: Task Duration Unit.');
    $tokens['node']['pmtask-duration'] = t('Project Management Task: Task Duration.');
    $tokens['node']['pmtask-price'] = t('Project Management Task: Task Price.');
    $tokens['node']['pmtask-pricemode'] = t('Project Management Task: Task Price Mode.');
    if (module_exists('pmperson')) {

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

/**
 * Implements hook_token_values().
 */
function pmtask_token_values($type, $object = NULL) {
  $values = array();
  $node = $object;
  if (($type == 'node' || $type == 'all') && $node->type === 'pmtask') {
    $values['pmtask-stepno'] = $node->stepno;
    $values['pmtask-organization-nid'] = $node->organization_nid;
    $values['pmtask-organization-title'] = $node->organization_title;
    $values['pmtask-project-nid'] = $node->project_nid;
    $values['pmtask-project-title'] = $node->project_title;
    $values['pmtask-parent-nid'] = $node->parent_nid;
    $values['pmtask-taskstatus'] = $node->taskstatus;
    $values['pmtask-taskpriority'] = $node->taskpriority;
    $values['pmtask-taskcategory'] = $node->taskcategory;
    $values['pmtask-durationunit'] = $node->durationunit;
    $values['pmtask-duration'] = $node->duration;
    $values['pmtask-price'] = $node->price;
    $values['pmtask-pricemode'] = $node->pricemode;
    if (module_exists('pmperson')) {

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

        // Assigned to one person
        if ($assignees_node->type === 'pmperson') {
          $values['pmtask-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['pmtask-assigned-to-email'] = $assigned;
        }
      }
      else {
        $values['pmtask-assigned-to-email'] = NULL;
      }
    }
  }
  return $values;
}

/**
 * Implements  hook_node_access_records().
 */
function pmtask_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 == 'pmtask') {

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

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

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

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

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

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

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

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

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

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

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

    // Project Management Task: delete if assigned to task
    if (isset($node->assigned_nid) && !empty($node->assigned_nid)) {
      $grants[] = array(
        'realm' => 'pmtask_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 pmtask_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 Task: view all', $account)) {
        $grants['pmtask_view_all'] = array(
          0,
        );
      }
      if (user_access('Project Management Task: view own', $account)) {
        $grants['pmtask_view_own'] = array(
          $account->uid,
        );
      }
      if (user_access('Project Management Task: view of user organization', $account) and !empty($account->pmorganization_nid)) {
        $grants['pmtask_view_if_user_organization'] = array(
          $account->pmorganization_nid,
        );
      }
      if (isset($account->pmperson_nid)) {
        if (user_access('Project Management Task: view if assigned to task', $account)) {
          $grants['pmtask_view_if_assigned'] = $assigned_nids;
        }
      }
      break;
    case 'update':
      if (user_access('Project Management Task: edit all', $account)) {
        $grants['pmtask_update_all'] = array(
          0,
        );
      }
      if (user_access('Project Management Task: edit own', $account)) {
        $grants['pmtask_update_own'] = array(
          $account->uid,
        );
      }
      if (user_access('Project Management Task: edit of user organization', $account) and !empty($account->pmorganization_nid)) {
        $grants['pmtask_update_if_user_organization'] = array(
          $account->pmorganization_nid,
        );
      }
      if (isset($account->pmperson_nid)) {
        if (user_access('Project Management Task: edit if assigned to task', $account)) {
          $grants['pmtask_update_if_assigned'] = $assigned_nids;
        }
      }
      break;
    case 'delete':
      if (user_access('Project Management Task: delete all', $account)) {
        $grants['pmtask_delete_all'] = array(
          0,
        );
      }
      if (user_access('Project Management Task: delete own', $account)) {
        $grants['pmtask_delete_own'] = array(
          $account->uid,
        );
      }
      if (user_access('Project Management Task: delete of user organization', $account) and !empty($account->pmorganization_nid)) {
        $grants['pmtask_delete_if_user_organization'] = array(
          $account->pmorganization_nid,
        );
      }
      if (isset($account->pmperson_nid)) {
        if (user_access('Project Management Task: delete if assigned to task', $account)) {
          $grants['pmtask_delete_if_assigned'] = $assigned_nids;
        }
      }
      break;
  }
  return $grants;
}

/**
 * Implements hook_pm_dashboard_links().
 */
function pmtask_pm_dashboard_links($type) {
  $links = array();
  if ($type == 'page' || $type == 'block') {
    $links[] = array(
      'theme' => 'pm_dashboard_link',
      'title' => t('Tasks'),
      'icon' => 'pmtasks',
      'path' => 'pm/tasks',
      'params' => array(),
      'node_type' => 'pmtask',
      'add_type' => 'pmtask',
      'map' => array(),
      'weight' => 5,
    );
  }
  return $links;
}

Functions

Namesort descending Description
pmtask_delete Implements hook_delete().
pmtask_field_extra_fields Implements hook_field_extra_fields().
pmtask_field_extra_fields_alter Implements hook_field_extra_fields_alter().
pmtask_form Implements hook_form().
pmtask_form_ajax Ajax callback for the pmtask node form.
pmtask_help Implements hook_help().
pmtask_insert Implements hook_insert().
pmtask_load Implements hook_load().
pmtask_node_access Implements hook_node_access().
pmtask_node_access_records Implements hook_node_access_records().
pmtask_node_grants Implements hook_node_grants().
pmtask_node_info Implements hook_node_info().
pmtask_node_revision_delete Implements hook_node_revision_delete().
pmtask_permission Implements hook_permission().
pmtask_pmorganization_change Implements hook_pmorganization_change().
pmtask_pmproject_change Implements hook_pmproject_change().
pmtask_pmproject_change_hierarchy Implements hook_pmproject_change_hierarchy().
pmtask_pm_dashboard_links Implements hook_pm_dashboard_links().
pmtask_theme Implements hook_theme().
pmtask_token_list Implements hook_token_list().
pmtask_token_values Implements hook_token_values().
pmtask_update Implements hook_update().
pmtask_validate Implements hook_validate().
pmtask_view Implements hook_view().
pmtask_views_api Implements hook_views_api().
_pmtask_beforesave Prepares pmtask for saving.
_pmtask_get_tree Provides a tree of tasks for a given project and (if selected) parent task.
_pmtask_plain_tree Provides task details for tasks within a tree.
_pmtask_tasks_access Determines whether the task tab of a project is visible.