You are here

pmticket.install in Drupal PM (Project Management) 7

Installation functions for the Project Management project module

File

pmticket/pmticket.install
View source
<?php

/**
 * @file
 * Installation functions for the Project Management project module
 */

/**
 * Implements hook_install().
 */
function pmticket_install() {
  variable_set('node_options_pmticket', array(
    'status',
  ));
  variable_set('node_permissions_pmticket', 0);

  // Uncache node types
  node_types_rebuild();

  // Fetch list of current node types and add body field to pmticket
  $types = node_type_get_types();
  node_add_body_field($types['pmticket'], 'Description');

  // 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' => 'pmticket',
  ));

  // Set up instance of billing status field.
  field_create_instance(array(
    'field_name' => 'pm_billing_status',
    'label' => 'Billing status',
    'entity_type' => 'node',
    'bundle' => 'pmticket',
    'widget' => array(
      'weight' => -13,
      'type' => 'options_select',
      'module' => 'options',
      'active' => 1,
      'settings' => array(),
    ),
    'required' => 1,
    'description' => '',
    'default_value' => array(
      array(
        'value' => variable_get('pmticket_billable_default', 0) ? 'Billable' : 'Not billable',
      ),
    ),
  ));
  $attributes = array();
  $attributes['Ticket status'] = array(
    'inserted' => 'inserted',
    'in progress' => 'in progress',
    'on hold' => 'on hold',
    'completed' => 'completed',
  );
  $attributes['Ticket status search'] = array(
    '-' => 'all',
    'inserted,in progress,on hold' => 'open',
    'inserted' => '-- inserted',
    'in progress' => '-- in progress',
    'on hold' => '-- on hold',
    'completed' => 'completed',
  );
  $attributes['Ticket category'] = array(
    'estimate' => 'estimate',
    'bug' => 'bug',
    'feature request' => 'feature request',
    'support' => 'support',
    'task' => 'task',
  );
  $attributes['Ticket category search'] = array(
    '-' => 'all',
    'estimate' => 'estimate',
    'bug' => 'bug',
    'feature request' => 'feature request',
    'support' => 'support',
    'task' => 'task',
  );
  $attributes['Ticket priority'] = array(
    '1-low' => 'low',
    '2-normal' => 'normal',
    '3-high' => 'high',
    '4-urgent' => 'urgent',
  );
  $attributes['Ticket priority search'] = array(
    '-' => 'all',
    '1-low' => 'low',
    '2-normal' => 'normal',
    '3-high' => 'high',
    '4-urgent' => 'urgent',
  );
  $s = "INSERT INTO {pmattribute} (domain, akey, avalue, weight) VALUES ('%s', '%s', '%s', %d)";
  $prevdomain = '';
  $weight = 0;
  foreach ($attributes as $domain => $attribute) {
    if ($domain != $prevdomain) {
      $weight = 0;
    }
    foreach ($attribute as $key => $value) {
      db_insert('pmattribute')
        ->fields(array(
        'domain' => $domain,
        'akey' => $key,
        'avalue' => $value,
        'weight' => $weight,
      ))
        ->execute();
      $weight++;
    }
    $prevdomain = $domain;
  }
}

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

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

/**
 * Implements hook_uninstall().
 */
function pmticket_uninstall() {
  drupal_uninstall_schema('pmticket');
  db_delete('pmattribute')
    ->condition('domain', array(
    'Ticket status',
    'Ticket status search',
    'Ticket category',
    'Ticket category search',
    'Ticket priority',
    'Ticket priority search',
  ), 'IN')
    ->execute();
}

/**
 * Implements hook_schema().
 */
function pmticket_schema() {
  $schema['pmticket'] = 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,
      ),
      'ticketcategory' => array(
        'type' => 'varchar',
        'length' => 100,
      ),
      'ticketstatus' => array(
        'type' => 'varchar',
        'length' => 100,
      ),
      'ticketpriority' => array(
        'type' => 'varchar',
        'length' => 100,
      ),
      'pricemode' => array(
        'type' => 'varchar',
        'length' => 100,
      ),
      'price' => array(
        'type' => 'float',
      ),
      'currency' => array(
        'type' => 'varchar',
        'length' => 100,
      ),
      'durationunit' => array(
        'type' => 'varchar',
        'length' => 100,
      ),
      'duration' => array(
        'type' => 'float',
        'default' => 0,
      ),
      'assigned_nid' => array(
        'type' => 'int',
      ),
      'assigned_title' => array(
        'type' => 'varchar',
        'length' => 100,
      ),
    ),
    'primary key' => array(
      'vid',
    ),
    'indexes' => array(
      'nid' => array(
        'nid',
      ),
      'organization_nid' => array(
        'organization_nid',
      ),
      'project_nid' => array(
        'project_nid',
      ),
      'task_nid' => array(
        'task_nid',
      ),
      'assigned_nid' => array(
        'assigned_nid',
      ),
    ),
  );
  return $schema;
}

/**
 * Implements hook_update_last_removed().
 */
function pmticket_update_last_removed() {
  return 6202;
}

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

  // Ensure that date field is created before instance.
  $dependencies['pmticket'][7103] = array(
    'pm' => 7102,
  );

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

/**
 * Adds Drupal 7 style body field to Project Management Ticket nodes
 */
function pmticket_update_7100() {

  // Uncache node types
  node_types_rebuild();

  // Fetch list of current node types and add body field to Project Management Task
  $types = node_type_get_types();
  node_add_body_field($types['pmticket'], 'Description');
  return 'Added D7 style body field to Project Management Ticket nodes';
}

/**
 * Allow pm to override the default Ticket content type CRUD permissions
 */
function pmticket_update_7101() {
  variable_set('node_permissions_pmticket', 0);
  return 'PM Ticket permissions overridden';
}

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

/**
 * Add date field to PM Ticket.
 */
function pmticket_update_7103() {

  // 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' => 'pmticket',
  ));

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

  // Delete columns from existing database table.
  db_drop_field('pmticket', 'datebegin');
  db_drop_field('pmticket', 'dateend');
}

/**
 * Adds billing status instance to PM Ticket content type.
 */
function pmticket_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' => 'pmticket',
    'widget' => array(
      'weight' => -13,
      'type' => 'options_select',
      'module' => 'options',
      'active' => 1,
      'settings' => array(),
    ),
    'required' => 1,
    'description' => '',
    'default_value' => array(
      array(
        'value' => variable_get('pmticket_billable_default', 0) ? 'Billable' : 'Not billable',
      ),
    ),
  ));

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

  // Get existing field values.
  $query = db_select('pmticket', 'pmti');
  $query
    ->join('node', 'n', 'n.vid = pmti.vid');
  $result = $query
    ->fields('pmti', 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('pmticket', 'billable');
  db_drop_field('pmticket', 'billed');
}

Functions

Namesort descending Description
pmticket_disable Implements hook_disable().
pmticket_enable Implement hook_enable().
pmticket_install Implements hook_install().
pmticket_schema Implements hook_schema().
pmticket_uninstall Implements hook_uninstall().
pmticket_update_7100 Adds Drupal 7 style body field to Project Management Ticket nodes
pmticket_update_7101 Allow pm to override the default Ticket content type CRUD permissions
pmticket_update_7102 Display message to admin regarding need to rebuild permission.
pmticket_update_7103 Add date field to PM Ticket.
pmticket_update_7104 Adds billing status instance to PM Ticket content type.
pmticket_update_dependencies Implements hook_update_dependencies().
pmticket_update_last_removed Implements hook_update_last_removed().