You are here

pmnote.module in Drupal PM (Project Management) 7

Functions for the PM Note module.

File

pmnote/pmnote.module
View source
<?php

/**
 * @file
 * Functions for the PM Note module.
 */

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

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

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

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

/**
 * Implements hook_theme().
 */
function pmnote_theme() {
  return array(
    'pmnote_view' => array(
      'file' => 'pmnote.theme.inc',
      'variables' => array(
        'node',
        'view_mode',
      ),
    ),
  );
}

/**
 * Implements hook_node_info().
 */
function pmnote_node_info() {
  return array(
    'pmnote' => array(
      'name' => t('Note'),
      'base' => 'pmnote',
      'description' => t("A note for Project Management."),
    ),
  );
}

/**
 * Implements hook_field_extra_fields().
 */
function pmnote_field_extra_fields() {
  $extra['node']['pmnote'] = array(
    'form' => array(
      'group1' => array(
        'label' => 'Organization/Project/Task Group',
        'weight' => -20,
      ),
    ),
  );
  return $extra;
}

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

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

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

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

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

/**
 * Implements hook_pmtask_change_hierarchy().
 */
function pmnote_pmtask_change_hierarchy($task_nid, $organization_nid, $organization_title, $project_nid, $project_title) {
  $query = db_update('pmnote')
    ->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 pmnote_form(&$node, $form_state) {
  $breadcrumb = array();
  $breadcrumb[] = l(t('Project Management'), 'pm');
  $breadcrumb[] = l(t('Notes'), 'pm/notes');
  drupal_set_breadcrumb($breadcrumb);
  $type = node_type_get_type($node);
  $info = field_info_extra_fields('node', 'pmnote', 'form');
  $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('n.type', 'pmorganization')
    ->condition('sor.isactive', 1)
    ->orderBy('n.title', 'ASC')
    ->addTag('node_access')
    ->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' => 'pmnote_ajax_organization_nid',
      'wrapper' => 'pmnote-project-nid',
    ),
  );
  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 note.'), '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')
    ->orderBy('n.title', 'ASC')
    ->addTag('node_access')
    ->execute();
  $projects = array();
  foreach ($pro_result as $project) {
    $projects[$project->nid] = $project->title;
  }
  $project_nid = isset($form_state['values']['project_nid']) ? $form_state['values']['project_nid'] : (isset($node->project_nid) ? $node->project_nid : NULL);
  $form['group1']['project_nid'] = array(
    '#type' => 'select',
    '#title' => t('Project'),
    '#default_value' => $project_nid,
    '#options' => array(
      0 => '-',
    ) + $projects,
    '#prefix' => '<div id="pmnote-project-nid">',
    '#suffix' => '</div>',
    '#ajax' => array(
      'callback' => 'pmnote_ajax_project_nid',
      'wrapper' => 'pmnote-task-nid',
    ),
  );
  $tree = is_null($project_nid) ? array() : _pmtask_get_tree($project_nid);
  $tasks = _pmtask_plain_tree($tree);
  $form['group1']['task_nid'] = array(
    '#type' => 'select',
    '#title' => t('Task'),
    '#default_value' => isset($node->task_nid) ? $node->task_nid : NULL,
    '#options' => array(
      0 => '-',
    ) + $tasks,
    '#prefix' => '<div id="pmnote-task-nid">',
    '#suffix' => '</div>',
  );
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => check_plain($type->title_label),
    '#required' => TRUE,
    '#default_value' => $node->title,
    '#weight' => $info['title']['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;
  }
  return $form;
}

/**
 * Ajax callback for the PM Note Organization Nid field.
 */
function pmnote_ajax_organization_nid(&$form, &$form_state) {
  return $form['group1']['project_nid'];
}

/**
 * Ajax callback for the PM Note Project Nid field.
 */
function pmnote_ajax_project_nid(&$form, &$form_state) {
  return $form['group1']['task_nid'];
}

/**
 * Implements hook_insert().
 */
function pmnote_insert($node) {
  _pmnote_beforesave($node);
  db_insert('pmnote')
    ->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,
  ))
    ->execute();
}

/**
 * Implements hook_update().
 */
function pmnote_update($node) {
  _pmnote_beforesave($node);
  if ($node->revision) {
    pmnote_insert($node);
  }
  else {
    db_update('pmnote')
      ->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,
    ))
      ->condition('vid', $node->vid)
      ->execute();
  }
}

/**
 * Pre-processing for the PM Note content type (before save).
 */
function _pmnote_beforesave(&$node) {
  $org_result = db_select('node', 'n')
    ->fields('n', array(
    'title',
  ))
    ->condition('n.type', 'pmorganization')
    ->condition('n.nid', $node->organization_nid)
    ->execute();
  $organization = $org_result
    ->fetchObject();
  $node->organization_title = $organization->title;
  $pro_result = db_select('node', 'n')
    ->fields('n', array(
    'title',
  ))
    ->condition('n.type', 'pmproject')
    ->condition('n.nid', $node->project_nid)
    ->execute();
  $project = $pro_result
    ->fetchObject();
  $node->project_title = isset($project->title) ? $project->title : '';
  $task_result = db_select('node', 'n')
    ->fields('n', array(
    'title',
  ))
    ->condition('n.type', 'pmtask')
    ->condition('n.nid', $node->task_nid)
    ->execute();
  $task = $task_result
    ->fetchObject();
  $node->task_title = isset($task->title) ? $task->title : '';
}

/**
 * Implements hook_nodeapi().
 */
function pmnote_nodeapi(&$node, $op, $teaser, $page) {
  switch ($op) {
    case 'delete revision':

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

/**
 * Implements hook_delete().
 */
function pmnote_delete($node) {
  db_delete('pmnote')
    ->condition('nid', $node->nid)
    ->execute();
}

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

/**
 * Implements hook_view().
 */
function pmnote_view($node, $view_mode = 'full') {
  if ($view_mode = 'full') {
    $breadcrumb = array();
    $breadcrumb[] = l(t('Project Management'), 'pm');
    $breadcrumb[] = l(t('Notes'), 'pm/notes');
    drupal_set_breadcrumb($breadcrumb);
  }
  return theme('pmnote_view', array(
    'node' => $node,
    'view_mode' => $view_mode,
  ));
}

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

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

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

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

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

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

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

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

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

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

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

/**
 * Implements hook_node_grants().
 */
function pmnote_node_grants($account, $op) {
  if (module_exists('pmperson')) {
    _pmperson_user_load($account);
  }
  $grants = array();
  switch ($op) {
    case 'view':
      if (user_access('Project Management note: view all', $account)) {
        $grants['pmnote_view_all'] = array(
          0,
        );
      }
      if (user_access('Project Management note: view own', $account)) {
        $grants['pmnote_view_own'] = array(
          $account->uid,
        );
      }
      if (user_access('Project Management note: view of user organization', $account) and !empty($account->pmorganization_nid)) {
        $grants['pmnote_view_if_user_organization'] = array(
          $account->pmorganization_nid,
        );
      }
      break;
    case 'update':
      if (user_access('Project Management note: edit all', $account)) {
        $grants['pmnote_update_all'] = array(
          0,
        );
      }
      if (user_access('Project Management note: edit own', $account)) {
        $grants['pmnote_update_own'] = array(
          $account->uid,
        );
      }
      if (user_access('Project Management note: edit of user organization', $account) and !empty($account->pmorganization_nid)) {
        $grants['pmnote_update_if_user_organization'] = array(
          $account->pmorganization_nid,
        );
      }
      break;
    case 'delete':
      if (user_access('Project Management note: delete all', $account)) {
        $grants['pmnote_delete_all'] = array(
          0,
        );
      }
      if (user_access('Project Management note: delete own', $account)) {
        $grants['pmnote_delete_own'] = array(
          $account->uid,
        );
      }
      if (user_access('Project Management note: delete of user organization', $account) and !empty($account->pmorganization_nid)) {
        $grants['pmnote_delete_if_user_organization'] = array(
          $account->pmorganization_nid,
        );
      }
      break;
  }
  return $grants;
}

/**
 * Implements hook_pm_dashboard_links().
 */
function pmnote_pm_dashboard_links($type) {
  $links = array();
  if ($type == 'page' || $type == 'block') {
    $links[] = array(
      'theme' => 'pm_dashboard_link',
      'title' => t('Notes'),
      'icon' => 'pmnotes',
      'path' => 'pm/notes',
      'params' => array(),
      'node_type' => 'pmnote',
      'add_type' => 'pmnote',
      'map' => array(),
      'weight' => 9,
    );
  }
  return $links;
}

Functions

Namesort descending Description
pmnote_ajax_organization_nid Ajax callback for the PM Note Organization Nid field.
pmnote_ajax_project_nid Ajax callback for the PM Note Project Nid field.
pmnote_delete Implements hook_delete().
pmnote_field_extra_fields Implements hook_field_extra_fields().
pmnote_field_extra_fields_alter Implements hook_field_extra_fields_alter().
pmnote_form Implements hook_form().
pmnote_help Implements hook_help().
pmnote_insert Implements hook_insert().
pmnote_load Implements hook_load().
pmnote_nodeapi Implements hook_nodeapi().
pmnote_node_access Implements hook_node_access().
pmnote_node_access_records Implements hook_node_access_records().
pmnote_node_grants Implements hook_node_grants().
pmnote_node_info Implements hook_node_info().
pmnote_permission Implements hook_permission().
pmnote_pmorganization_change Implements hook_pmorganization_change().
pmnote_pmproject_change Implements hook_pmproject_change().
pmnote_pmproject_change_hierarchy Implements hook_pmproject_change_hierarchy().
pmnote_pmtask_change Implements hook_pmtask_change().
pmnote_pmtask_change_hierarchy Implements hook_pmtask_change_hierarchy().
pmnote_pm_dashboard_links Implements hook_pm_dashboard_links().
pmnote_theme Implements hook_theme().
pmnote_update Implements hook_update().
pmnote_view Implements hook_view().
pmnote_views_api Implements hook_views_api().
_pmnote_beforesave Pre-processing for the PM Note content type (before save).