You are here

pmorganization.module in Drupal PM (Project Management) 7

File

pmorganization/pmorganization.module
View source
<?php

/**
 * @file
 */

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

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

/**
 * Implements hook_node_access().
 *
 * Define node access requirements for Project Management Organization nodes.
 */
function pmorganization_node_access($node, $op, $account = NULL) {
  $type = is_string($node) ? $node : $node->type;

  // 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 ($type == 'pmorganization' && $op == 'create') {
    if (user_access('Project Management Organization: add', $account)) {
      return NODE_ACCESS_ALLOW;
    }
  }
  return NODE_ACCESS_IGNORE;
}

/**
 * Implements hook_menu().
 */
function pmorganization_menu() {
  $items = array();
  $items['pm/organizations/autocomplete'] = array(
    'title' => 'Organization autocomplete',
    'page callback' => 'pmorganization_autocomplete',
    'access arguments' => array(
      'Project Management Organization: access',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'pmorganization.admin.inc',
  );
  $items['admin/config/pm/organization'] = array(
    'title' => 'Organizations',
    'description' => 'Configure default PM Organization.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'pmorganization_admin_settings',
    ),
    'access arguments' => array(
      'Project Management: access administration pages',
    ),
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function pmorganization_theme() {
  return array(
    'pmorganization_view' => array(
      'file' => 'pmorganization.theme.inc',
      'variables' => array(
        'node',
        'view_mode',
      ),
    ),
  );
}

/**
 * Implements hook_node_info().
 */
function pmorganization_node_info() {
  return array(
    'pmorganization' => array(
      'name' => t('Organization'),
      'base' => 'pmorganization',
      'description' => t("An organization for Project Management."),
      'title_label' => t("Name"),
      'body_label' => t("Description"),
    ),
  );
}

/**
 * Implements hook_field_extra_fields().
 */
function pmorganization_field_extra_fields() {
  $extra['node']['pmorganization'] = array(
    'form' => array(
      'group1' => array(
        'label' => 'Customer/Provider/Active Group',
        'weight' => -21,
      ),
      'group2' => array(
        'label' => 'Address Group',
        'weight' => -19,
      ),
      'group3' => array(
        'label' => 'Phone/WWW/Email Group',
        'weight' => -18,
      ),
      'group4' => array(
        'label' => 'Currency/Language/Tax ID Group',
        'weight' => -17,
      ),
    ),
  );
  return $extra;
}

/**
 * Implements hook_field_extra_fields_alter().
 */
function pmorganization_field_extra_fields_alter(&$info) {
  $info['node']['pmorganization']['form']['title']['weight'] = -20;
}

/**
 * Implements hook_form().
 */
function pmorganization_form(&$node) {
  $breadcrumb = array();
  $breadcrumb[] = l(t('Project Management'), 'pm');
  $breadcrumb[] = l(t('Organizations'), 'pm/organizations');
  drupal_set_breadcrumb($breadcrumb);
  $type = node_type_get_type($node);
  $info = field_info_extra_fields('node', 'pmorganization', 'form');
  $form['#attributes']['class'] = 'pmcomponent_node_form';
  $form['group1'] = array(
    '#type' => 'markup',
    '#theme' => 'pm_form_group',
    '#weight' => $info['group1']['weight'],
  );
  $form['group1']['iscustomer'] = array(
    '#type' => 'checkbox',
    '#title' => t('Customer'),
    '#default_value' => isset($node->iscustomer) ? $node->iscustomer : 1,
  );
  $form['group1']['isprovider'] = array(
    '#type' => 'checkbox',
    '#title' => t('Provider'),
    '#default_value' => isset($node->isprovider) ? $node->isprovider : 0,
  );
  $form['group1']['isactive'] = array(
    '#type' => 'checkbox',
    '#title' => t('Active'),
    '#default_value' => isset($node->isactive) ? $node->isactive : 1,
  );
  $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'],
  );
  $form['group2']['address'] = array(
    '#type' => 'textfield',
    '#title' => t('Address'),
    '#default_value' => isset($node->address) ? $node->address : NULL,
    '#weight' => 1,
  );
  $form['group2']['city'] = array(
    '#type' => 'textfield',
    '#title' => t('City'),
    '#size' => 20,
    '#default_value' => isset($node->city) ? $node->city : NULL,
    '#weight' => 2,
  );
  $form['group2']['provstate'] = array(
    '#type' => 'textfield',
    '#title' => t('Province / State'),
    '#size' => 20,
    '#default_value' => isset($node->provstate) ? $node->provstate : NULL,
    '#weight' => 3,
  );
  $country_list = pm_attributes_bydomain('Country');
  $form['group2']['country'] = array(
    '#type' => 'select',
    '#title' => t('Country'),
    '#options' => $country_list['values'],
    '#default_value' => isset($node->country) ? $node->country : $country_list['default'],
    '#weight' => 4,
  );
  $form['group2']['zip'] = array(
    '#type' => 'textfield',
    '#title' => t('Zip'),
    '#size' => 15,
    '#default_value' => isset($node->zip) ? $node->zip : NULL,
    '#weight' => 5,
  );
  $form['group3'] = array(
    '#type' => 'markup',
    '#theme' => 'pm_form_group',
    '#weight' => $info['group3']['weight'],
  );
  $form['group3']['phone'] = array(
    '#type' => 'textfield',
    '#title' => t('Phone'),
    '#default_value' => isset($node->phone) ? $node->phone : NULL,
  );
  $form['group3']['www'] = array(
    '#type' => 'textfield',
    '#title' => t('WWW'),
    '#size' => 30,
    '#default_value' => isset($node->www) ? $node->www : NULL,
  );
  $form['group3']['email'] = array(
    '#type' => 'textfield',
    '#title' => t('E-mail'),
    '#size' => 30,
    '#default_value' => isset($node->email) ? $node->email : NULL,
  );
  $form['group4'] = array(
    '#type' => 'markup',
    '#theme' => 'pm_form_group',
    '#weight' => $info['group4']['weight'],
  );
  $currency_list = pm_attributes_bydomain('Currency');
  $form['group4']['currency'] = array(
    '#type' => 'select',
    '#title' => t('Currency'),
    '#options' => $currency_list['values'],
    '#default_value' => isset($node->currency) ? $node->currency : $currency_list['default'],
  );
  $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,
  );
  $languages = language_list('language', TRUE);
  $languages_options = array();
  foreach ($languages as $language_code => $language) {
    $languages_options[$language_code] = $language->name;
  }
  $form['group4']['orglanguage'] = array(
    '#type' => 'select',
    '#title' => t('Language'),
    '#options' => $languages_options,
    '#default_value' => isset($node->orglanguage) ? $node->orglanguage : NULL,
  );
  $form['group4']['taxid'] = array(
    '#type' => 'textfield',
    '#title' => t('Tax ID'),
    '#size' => 20,
    '#default_value' => isset($node->taxid) ? $node->taxid : NULL,
  );

  // Check to see if the body field is still there, if so display it
  $body = field_get_items('pmorganization', $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;
}

/**
 * Implements hook_insert().
 */
function pmorganization_insert($node) {
  db_insert('pmorganization')
    ->fields(array(
    'vid' => $node->vid,
    'nid' => $node->nid,
    'country' => $node->country,
    'www' => $node->www,
    'phone' => $node->phone,
    'email' => $node->email,
    'currency' => $node->currency,
    'provstate' => $node->provstate,
    'zip' => $node->zip,
    'city' => $node->city,
    'address' => $node->address,
    'taxid' => $node->taxid,
    'orglanguage' => $node->orglanguage,
    'iscustomer' => $node->iscustomer,
    'isprovider' => $node->isprovider,
    'isactive' => $node->isactive,
    'pricemode' => $node->pricemode,
  ))
    ->execute();
}

/**
 * Implements hook_update().
 */
function pmorganization_update($node) {

  // if this is a new node or we're adding a new revision,
  if ($node->revision) {
    pmorganization_insert($node);
  }
  else {
    db_update('pmorganization')
      ->fields(array(
      'country' => $node->country,
      'www' => $node->www,
      'phone' => $node->phone,
      'email' => $node->email,
      'currency' => $node->currency,
      'provstate' => $node->provstate,
      'zip' => $node->zip,
      'city' => $node->city,
      'address' => $node->address,
      'taxid' => $node->taxid,
      'orglanguage' => $node->orglanguage,
      'iscustomer' => $node->iscustomer,
      'isprovider' => $node->isprovider,
      'isactive' => $node->isactive,
      'pricemode' => $node->pricemode,
    ))
      ->condition('vid', $node->vid, '=')
      ->execute();
    if ($node->title != $node->title_old) {
      module_invoke_all('pmorganization_change', $node->nid, $node->title);
    }
  }
}

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

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

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

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

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

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

/**
 * Admin settings form for pmproject module
 */
function pmorganization_admin_settings() {
  $form = array();
  $query = db_select('node', 'n')
    ->fields('n', array(
    'nid',
    'title',
  ))
    ->condition('status', 1)
    ->condition('type', 'pmorganization')
    ->addTag('node_access')
    ->orderBy('title', 'ASC');
  $result = $query
    ->execute();
  $organizations = array();
  while ($organization = $result
    ->fetchObject()) {
    $organizations[$organization->nid] = $organization->title;
  }
  if (count($organizations) > 0) {
    $form['pm_organization_nid'] = array(
      '#type' => 'select',
      '#title' => t('Host organization'),
      '#options' => $organizations,
      '#default_value' => variable_get('pm_organization_nid', 0),
      '#description' => t('The organization that owns this system'),
      '#weight' => -30,
    );
  }
  else {
    $form['pm_organization_nid'] = array(
      '#type' => 'item',
      '#title' => t('Host organization'),
      '#markup' => t('There are no Project Management Organizations in the system.') . '<br />' . l(t('Create a Project Management Organization'), 'node/add/pmorganization'),
    );
  }
  return system_settings_form($form);
}

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

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

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

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

    // Project Management Organization: view belonged
    $grants[] = array(
      'realm' => 'pmorganization_view_if_user_org',
      'gid' => $node->nid,
      'grant_view' => 1,
      'grant_update' => 0,
      'grant_delete' => 0,
      'priority' => 0,
    );

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

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

    // Project Management Organization: edit belonged
    $grants[] = array(
      'realm' => 'pmorganization_update_if_user_org',
      'gid' => $node->nid,
      'grant_view' => 0,
      'grant_update' => 1,
      'grant_delete' => 0,
      'priority' => 0,
    );

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

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

/**
 * Implements hook_node_grants().
 */
function pmorganization_node_grants($account, $op) {
  $is_pmperson = FALSE;
  if (!isset($account->pmorganization_nid) && module_exists('pmperson')) {
    _pmperson_user_load($account);
  }
  if (isset($account->pmperson_nid) and $account->pmperson_nid > 0) {
    $is_pmperson = TRUE;
  }
  $grants = array();
  switch ($op) {
    case 'view':
      if (user_access('Project Management Organization: view all', $account)) {
        $grants['pmorganization_view_all'] = array(
          0,
        );
      }
      if (user_access('Project Management Organization: view own', $account)) {
        $grants['pmorganization_view_own'] = array(
          $account->uid,
        );
      }
      if ($is_pmperson) {
        if (user_access('Project Management Organization: view belonged', $account)) {
          $grants['pmorganization_view_if_user_org'] = array(
            $account->pmorganization_nid,
          );
        }
      }
      break;
    case 'update':
      if (user_access('Project Management Organization: edit all', $account)) {
        $grants['pmorganization_update_all'] = array(
          0,
        );
      }
      if (user_access('Project Management Organization: edit own', $account)) {
        $grants['pmorganization_update_own'] = array(
          $account->uid,
        );
      }
      if ($is_pmperson) {
        if (user_access('Project Management Organization: edit belonged', $account)) {
          $grants['pmorganization_update_if_user_org'] = array(
            $account->pmorganization_nid,
          );
        }
      }
      break;
    case 'delete':
      if (user_access('Project Management Organization: delete all', $account)) {
        $grants['pmorganization_delete_all'] = array(
          0,
        );
      }
      if (user_access('Project Management Organization: delete own', $account)) {
        $grants['pmorganization_delete_own'] = array(
          $account->uid,
        );
      }
      break;
  }
  return $grants;
}

/**
 * Implements hook_pm_dashboard_links().
 */
function pmorganization_pm_dashboard_links($type) {
  $links = array();
  if ($type == 'page' || $type == 'block') {
    $links[] = array(
      'theme' => 'pm_dashboard_link',
      'title' => t('Organizations'),
      'icon' => 'pmorganizations',
      'path' => 'pm/organizations',
      'params' => array(),
      'node_type' => 'pmorganization',
      'add_type' => 'pmorganization',
      'map' => array(),
      'weight' => 1,
    );
  }
  return $links;
}