You are here

pmtimetracking.install in Drupal PM (Project Management) 7

Install functions for PM Timetracking.

File

pmtimetracking/pmtimetracking.install
View source
<?php

/**
 * @file
 * Install functions for PM Timetracking.
 */

/**
 * Implements hook_install().
 */
function pmtimetracking_install() {
  variable_set('node_options_pmtimetracking', array(
    'status',
  ));
  variable_set('node_permissions_pmtimetracking', 0);

  // Uncache node types
  node_types_rebuild();

  // Add instance of date field.
  field_create_instance(array(
    'field_name' => 'pm_date',
    'label' => 'Date',
    'widget' => array(
      'weight' => '-18',
      'type' => 'date_text',
      'module' => 'date',
      'active' => 1,
      'settings' => array(
        'input_format' => 'm/d/Y - H:i:s',
        'input_format_custom' => '',
        'year_range' => '-3:+3',
        'increment' => 15,
        'label_position' => 'above',
        'text_parts' => array(),
      ),
    ),
    'settings' => array(
      'default_value' => 'now',
      'default_value_code' => '',
      'default_value2' => 'same',
      'default_value_code2' => '',
      'user_register_form' => FALSE,
    ),
    'display' => array(
      'default' => array(
        'label' => 'above',
        'type' => 'date_default',
        'settings' => array(
          'format_type' => 'long',
          'multiple_number' => '',
          'multiple_from' => '',
          'multiple_to' => '',
          'fromto' => 'both',
        ),
        'module' => 'date',
        'weight' => 1,
      ),
      'teaser' => array(
        'type' => 'hidden',
        'label' => 'above',
        'settings' => array(),
        'weight' => 0,
      ),
    ),
    'required' => 0,
    'entity_type' => 'node',
    'bundle' => 'pmtimetracking',
  ));

  // Set up instance of billing status field.
  field_create_instance(array(
    'field_name' => 'pm_billing_status',
    'label' => 'Billing status',
    'entity_type' => 'node',
    'bundle' => 'pmtimetracking',
    'widget' => array(
      'weight' => -17,
      'type' => 'options_select',
      'module' => 'options',
      'active' => 1,
      'settings' => array(),
    ),
    'required' => 1,
    'description' => '',
    'default_value' => array(
      array(
        'value' => variable_get('pmtimetracking_billable_default', 0) ? 'Billable' : 'Not billable',
      ),
    ),
  ));

  // Fetch list of current node types.
  $types = node_type_get_types();

  // Add body field to PM Timetracking content type.
  node_add_body_field($types['pmtimetracking'], 'Description');
}

/**
 * Implement hook_enable().
 */
function pmtimetracking_enable() {
  node_access_needs_rebuild(TRUE);
}

/**
 * Implements hook_disable().
 */
function pmtimetracking_disable() {
  node_access_needs_rebuild(TRUE);
  drupal_set_message(t('Nodes of type "Timetracking" have not been deleted on disabling Project Management Timetracking. Please note that they will now have reduced functionality, and will not be protected by Project Management Timetracking access controls.'), 'warning');
}

/**
 * Implements hook_uninstall().
 */
function pmtimetracking_uninstall() {
  drupal_uninstall_schema('pmtimetracking');
}

/**
 * Implements hook_schema().
 */
function pmtimetracking_schema() {
  $schema['pmtimetracking'] = array(
    'fields' => array(
      'vid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'nid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'organization_nid' => array(
        'type' => 'int',
      ),
      'organization_title' => array(
        'type' => 'varchar',
        'length' => 128,
      ),
      'project_nid' => array(
        'type' => 'int',
      ),
      'project_title' => array(
        'type' => 'varchar',
        'length' => 128,
      ),
      'task_nid' => array(
        'type' => 'int',
      ),
      'task_stepno' => array(
        'type' => 'varchar',
        'length' => 128,
      ),
      'task_title' => array(
        'type' => 'varchar',
        'length' => 128,
      ),
      'ticket_nid' => array(
        'type' => 'int',
      ),
      'ticket_title' => array(
        'type' => 'varchar',
        'length' => 128,
      ),
      'duration' => array(
        'type' => 'float',
      ),
      'billing_duration' => array(
        'type' => 'float',
      ),
    ),
    'primary key' => array(
      'vid',
    ),
    'indexes' => array(
      'nid' => array(
        'nid',
      ),
      'organization_nid' => array(
        'organization_nid',
      ),
      'project_nid' => array(
        'project_nid',
      ),
      'task_nid' => array(
        'task_nid',
      ),
      'ticket_nid' => array(
        'ticket_nid',
      ),
    ),
  );
  return $schema;
}

/**
 * Implements hook_update_last_removed().
 */
function pmtimetracking_update_last_removed() {
  return 6201;
}

/**
 * Implements hook_update_dependencies().
 */
function pmtimetracking_update_dependencies() {

  // Ensure that billing status field is created before instance.
  $dependencies['pmtimetracking'][7104] = array(
    'pm' => 7101,
  );

  // Ensure that date range field is created before instance.
  $dependencies['pmtimetracking'][7105] = array(
    'pm' => 7102,
  );
  return $dependencies;
}

/**
 * Allow Project Management to override the default content type permissions.
 */
function pmtimetracking_update_7101() {
  variable_set('node_permissions_pmtimetracking', 0);
  return 'PM Timetracking permissions overridden';
}

/**
 * Display message to admin regarding need to rebuild permission.
 */
function pmtimetracking_update_7102() {
  node_access_needs_rebuild(TRUE);
}

/**
 * Adds Drupal 7 style body field to PM Timetracking content type.
 */
function pmtimetracking_update_7103() {

  // Uncache node types
  node_types_rebuild();

  // Fetch list of current node types and add body field to pm timetracking
  $types = node_type_get_types();
  node_add_body_field($types['pmtimetracking'], 'Description');
  return 'Added D7 style body field to pm timetracking nodes';
}

/**
 * Adds billing status instance to PM Timetracking content type.
 */
function pmtimetracking_update_7104() {

  // Uncache node types
  node_types_rebuild();

  // Set up instance of billing status field.
  field_create_instance(array(
    'field_name' => 'pm_billing_status',
    'label' => 'Billing status',
    'entity_type' => 'node',
    'bundle' => 'pmtimetracking',
    'widget' => array(
      'weight' => -17,
      'type' => 'options_select',
      'module' => 'options',
      'active' => 1,
      'settings' => array(),
    ),
    'required' => 1,
    'description' => '',
    'default_value' => array(
      array(
        'value' => variable_get('pmtimetracking_billable_default', 0) ? 'Billable' : 'Not billable',
      ),
    ),
  ));

  // Delete the default field value. This is now set via the core field UI.
  variable_del('pmtimetracking_billable_default');

  // Get existing field values.
  $query = db_select('pmtimetracking', 'pmtt');
  $query
    ->join('node', 'n', 'n.vid = pmtt.vid');
  $result = $query
    ->fields('pmtt', array(
    'nid',
    'vid',
    'billable',
    'billed',
  ))
    ->execute();

  // Save existing field value into Field API field.
  foreach ($result as $record) {
    $node = node_load($record->nid);
    $new_value = $record->billed ? 'Billed' : ($record->billable ? 'Billable' : 'Not billable');
    $node->pm_billing_status[LANGUAGE_NONE][0]['value'] = $new_value;
    field_attach_presave('node', $node);
    field_attach_update('node', $node);
  }

  // Remove existing field from custom database table.
  db_drop_field('pmtimetracking', 'billable');
  db_drop_field('pmtimetracking', 'billed');
}

/**
 * Add date field to PM Timetracking.
 */
function pmtimetracking_update_7105() {

  // Create field instance.
  field_create_instance(array(
    'field_name' => 'pm_date',
    'label' => 'Date',
    'widget' => array(
      'weight' => '-18',
      'type' => 'date_text',
      'module' => 'date',
      'active' => 1,
      'settings' => array(
        'input_format' => 'm/d/Y - H:i:s',
        'input_format_custom' => '',
        'year_range' => '-3:+3',
        'increment' => 15,
        'label_position' => 'above',
        'text_parts' => array(),
      ),
    ),
    'settings' => array(
      'default_value' => 'now',
      'default_value_code' => '',
      'default_value2' => 'same',
      'default_value_code2' => '',
      'user_register_form' => FALSE,
    ),
    'display' => array(
      'default' => array(
        'label' => 'above',
        'type' => 'date_default',
        'settings' => array(
          'format_type' => 'long',
          'multiple_number' => '',
          'multiple_from' => '',
          'multiple_to' => '',
          'fromto' => 'both',
        ),
        'module' => 'date',
        'weight' => 1,
      ),
      'teaser' => array(
        'type' => 'hidden',
        'label' => 'above',
        'settings' => array(),
        'weight' => 0,
      ),
    ),
    'required' => 0,
    'entity_type' => 'node',
    'bundle' => 'pmtimetracking',
  ));

  // Migrate data
  $query = db_select('pmtimetracking', 'pmtt');
  $query
    ->join('node', 'n', 'n.vid = pmtt.vid');
  $result = $query
    ->fields('pmtt', array(
    'nid',
    'vid',
    'trackingdate',
    'timebegin',
    'timeend',
  ))
    ->execute();
  foreach ($result as $record) {
    $node = node_load($record->nid);
    $datebegin = strtotime($record->timebegin, $record->trackingdate);
    $dateend = strtotime($record->timeend, $record->trackingdate);
    $node->pm_date[LANGUAGE_NONE][0]['value'] = date('Y-m-d H:i:s', $datebegin);
    $node->pm_date[LANGUAGE_NONE][0]['value2'] = date('Y-m-d H:i:s', $dateend);
    field_attach_presave('node', $node);
    field_attach_update('node', $node);
  }

  // Delete columns from existing database table.
  db_drop_field('pmtimetracking', 'trackingdate');
  db_drop_field('pmtimetracking', 'timebegin');
  db_drop_field('pmtimetracking', 'timeend');
}

Functions

Namesort descending Description
pmtimetracking_disable Implements hook_disable().
pmtimetracking_enable Implement hook_enable().
pmtimetracking_install Implements hook_install().
pmtimetracking_schema Implements hook_schema().
pmtimetracking_uninstall Implements hook_uninstall().
pmtimetracking_update_7101 Allow Project Management to override the default content type permissions.
pmtimetracking_update_7102 Display message to admin regarding need to rebuild permission.
pmtimetracking_update_7103 Adds Drupal 7 style body field to PM Timetracking content type.
pmtimetracking_update_7104 Adds billing status instance to PM Timetracking content type.
pmtimetracking_update_7105 Add date field to PM Timetracking.
pmtimetracking_update_dependencies Implements hook_update_dependencies().
pmtimetracking_update_last_removed Implements hook_update_last_removed().