You are here

pmtimetracking.module in Drupal PM (Project Management) 8

Main module functions for PM Timetracking.

File

pmtimetracking/pmtimetracking.module
View source
<?php

/**
 * @file
 * Main module functions for PM Timetracking.
 */

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

/**
 * Implements hook_permission().
 */
function pmtimetracking_permission() {
  return array(
    'Project Management Timetracking: access' => array(
      'title' => t('Access PM Timetracking'),
      'description' => t('Allows the user to see pages and blocks associated with the PM Timetracking module, but does not control which timetrackings are shown within them.'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function pmtimetracking_menu() {
  $items = array();
  $items['admin/config/pm/timetracking'] = array(
    'title' => 'Timetrackings',
    'description' => 'Configure <em>Project Management</em> timetrackings.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'pmtimetracking_admin_settings',
    ),
    'access arguments' => array(
      'Project Management: access administration pages',
    ),
  );
  return $items;
}

/**
 * Implements hook_node_info().
 */
function pmtimetracking_node_info() {
  return array(
    'pmtimetracking' => array(
      'name' => t('Timetracking'),
      'base' => 'pmtimetracking',
      'description' => t('Use <em>timetrackings</em> for time records that can be allocated within <em>Project Management</em>.'),
      'title_label' => t('Title'),
    ),
  );
}

/**
 * Implements hook_form().
 */
function pmtimetracking_form(&$node, $form_state) {
  $breadcrumb = array(
    l(t('Project Management'), 'pm'),
    l(t('Timetrackings'), 'pm/timetrackings'),
  );
  drupal_set_breadcrumb($breadcrumb);
  return node_content_form($node, $form_state);
}

/**
 * Implements hook_view().
 */
function pmtimetracking_view($node, $view_mode) {
  if ($view_mode == 'full' && node_is_page($node)) {
    $breadcrumb = array(
      l(t('Project Management'), 'pm'),
      l(t('Timetrackings'), 'pm/timetrackings'),
    );
    drupal_set_breadcrumb($breadcrumb);
  }
  return $node;
}

/**
 * Implements hook_pm_contextual_links().
 */
function pmtimetracking_pm_contextual_links($entity, $type, $view_mode, $langcode) {
  $links = array();
  list($id, $vid, $bundle) = entity_extract_ids($type, $entity);
  if ($type == 'node') {
    $field_info = field_info_field('pmtimetracking_parent');
    $target_bundles = $field_info['settings']['handler_settings']['target_bundles'];
    if (in_array($bundle, $target_bundles)) {
      $links[t('Add Timetracking')] = array(
        'title' => t('Add Timetracking'),
        'href' => 'node/add/pmtimetracking',
        'query' => array(
          'pmtimetracking_parent' => $id,
        ),
      );
    }
  }
  return $links;
}

/**
 * Callback for the pmtimetracking admin settings page.
 */
function pmtimetracking_admin_settings() {
  $form = array();
  $form['pmtimetracking_auto_duration'] = array(
    '#type' => 'checkbox',
    '#title' => t('Automatically set duration based on start and end times.'),
    '#default_value' => variable_get('pmtimetracking_auto_duration', TRUE),
    '#description' => t('When checked, users will not be able to manually set the duration or duration unit. Instead, the duration will be set to the difference (in hours) between the start and end times.'),
  );
  $form['pmtimetracking_auto_billing_duration'] = array(
    '#type' => 'checkbox',
    '#title' => t('Automatically set billing duration equal to duration.'),
    '#default_value' => variable_get('pmtimetracking_auto_billing_duration', TRUE),
    '#description' => t('When checked, users will not be able to manually set the billing duration. Instead, the billing duration will be set equal to the duration.'),
  );
  return system_settings_form($form);
}

/**
 * Implements hook_node_presave().
 */
function pmtimetracking_node_presave($node) {

  // Only act on pmtimetracking nodes.
  if ($node->type == 'pmtimetracking') {

    // Set duration based on start and end times.
    if ((bool) variable_get('pmtimetracking_auto_duration', TRUE)) {
      $start_datetime = strtotime($node->pm_date[LANGUAGE_NONE][0]['value']);
      $end_datetime = strtotime($node->pm_date[LANGUAGE_NONE][0]['value2']);
      $duration_seconds = $end_datetime - $start_datetime;
      $duration_hours = $duration_seconds / (60 * 60);
      $node->pm_duration[LANGUAGE_NONE][0]['value'] = $duration_hours;
      $node->pm_durationunit[LANGUAGE_NONE][0]['value'] = 'hour';
    }

    // Set billing duration based on duration.
    if ((bool) variable_get('pmtimetracking_auto_billing_duration', TRUE)) {
      $node->pm_billing_duration[LANGUAGE_NONE][0]['value'] = $node->pm_duration[LANGUAGE_NONE][0]['value'];
    }
  }
}

/**
 * Implements hook_form_pmtimetracking_node_form_alter().
 */
function pmtimetracking_form_pmtimetracking_node_form_alter(&$form, &$form_state, $form_id) {

  // Hide duration and duration unit fields.
  if ((bool) variable_get('pmtimetracking_auto_duration', TRUE)) {
    $form['pm_duration']['#access'] = 0;
    $form['pm_durationunit']['#access'] = 0;
  }

  // Hide billing duration field.
  if ((bool) variable_get('pmtimetracking_auto_billing_duration', TRUE)) {
    $form['pm_billing_duration']['#access'] = 0;
  }
}

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

/**
 * Implements hook_pm_dashboard_links().
 */
function pmtimetracking_pm_dashboard_links($type) {
  $links = array();
  if ($type == 'page' || $type == 'block') {
    $links[] = array(
      'theme' => 'pm_dashboard_link',
      'title' => t('Timetrackings'),
      'icon' => 'pmtimetrackings',
      'path' => 'pm/timetrackings',
      'params' => array(),
      'node_type' => 'pmtimetracking',
      'add_type' => 'pmtimetracking',
      'map' => array(),
      'weight' => 8,
    );
  }
  return $links;
}

Functions

Namesort descending Description
pmtimetracking_admin_settings Callback for the pmtimetracking admin settings page.
pmtimetracking_form Implements hook_form().
pmtimetracking_form_pmtimetracking_node_form_alter Implements hook_form_pmtimetracking_node_form_alter().
pmtimetracking_help Implements hook_help().
pmtimetracking_menu Implements hook_menu().
pmtimetracking_node_info Implements hook_node_info().
pmtimetracking_node_presave Implements hook_node_presave().
pmtimetracking_permission Implements hook_permission().
pmtimetracking_pm_contextual_links Implements hook_pm_contextual_links().
pmtimetracking_pm_dashboard_links Implements hook_pm_dashboard_links().
pmtimetracking_view Implements hook_view().
pmtimetracking_views_api Implements hook_views_api().