You are here

pmproject.install in Drupal PM (Project Management) 7

Installation functions for the Project Management project module

File

pmproject/pmproject.install
View source
<?php

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

/**
 * Implements hook_install().
 */
function pmproject_install() {
  variable_set('node_options_pmproject', array(
    'status',
  ));
  variable_set('node_permissions_pmproject', 0);

  // Uncache node types
  node_types_rebuild();

  // Fetch list of current node types and add body field to pmproject
  $types = node_type_get_types();
  node_add_body_field($types['pmproject'], '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' => 'pmproject',
  ));

  // Set up instance of billing status field.
  field_create_instance(array(
    'field_name' => 'pm_billing_status',
    'label' => 'Billing status',
    'entity_type' => 'node',
    'bundle' => 'pmexpense',
    'widget' => array(
      'weight' => -14,
      'type' => 'options_select',
      'module' => 'options',
      'active' => 1,
      'settings' => array(),
    ),
    'required' => 1,
    'description' => '',
    'default_value' => array(
      array(
        'value' => variable_get('pmexpense_billable_default', 0) ? 'Billable' : 'Not billable',
      ),
    ),
  ));
  $attributes = array();
  $attributes['Project status'] = array(
    'inserted' => 'inserted',
    'in progress' => 'in progress',
    'on hold' => 'on hold',
    'completed' => 'completed',
  );
  $attributes['Project status search'] = array(
    '-' => 'all',
    'inserted,in progress,on hold' => 'open',
    'inserted' => '-- inserted',
    'in progress' => '-- in progress',
    'on hold' => '-- on hold',
    'completed' => 'completed',
  );
  $attributes['Project category'] = array(
    'development' => 'development',
    'support' => 'support',
  );
  $attributes['Project category search'] = array(
    '-' => 'all',
    'development' => 'development',
    'support' => 'support',
  );
  $attributes['Project priority'] = array(
    '1-low' => 'low',
    '2-normal' => 'normal',
    '3-high' => 'high',
    '4-urgent' => 'urgent',
  );
  $attributes['Project priority search'] = array(
    '-' => 'all',
    '1-low' => 'low',
    '2-normal' => 'normal',
    '3-high' => 'high',
    '4-urgent' => 'urgent',
  );
  $attributes['Duration unit'] = array(
    'hour' => 'Hour',
    'day' => 'Day',
  );
  $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;
  }
}

/**
 * Implements hook_enable().
 */
function pmproject_enable() {
  node_access_needs_rebuild(TRUE);
}

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

/**
 * Implements hook_uninstall().
 */
function pmproject_uninstall() {
  drupal_uninstall_schema('pmproject');
  db_delete('pmattribute')
    ->condition('domain', array(
    'Project status',
    'Project status search',
    'Project category',
    'Project category search',
    'Project priority',
    'Project priority search',
    'Duration unit',
  ), 'IN')
    ->execute();
}

/**
 * Implements hook_schema().
 */
function pmproject_schema() {
  $schema['pmproject'] = 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' => 100,
      ),
      'projectstatus' => array(
        'type' => 'varchar',
        'length' => 100,
      ),
      'projectcategory' => array(
        'type' => 'varchar',
        'length' => 100,
      ),
      'projectpriority' => 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,
      ),
      'manager_nid' => array(
        'type' => 'int',
      ),
      'manager_title' => array(
        'type' => 'varchar',
        'length' => 100,
      ),
      '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',
      ),
      'manager_nid' => array(
        'manager_nid',
      ),
      'assigned_nid' => array(
        'assigned_nid',
      ),
    ),
  );
  return $schema;
}

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

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

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

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

/**
 * Adds Drupal 7 style body field to pm project node
 */
function pmproject_update_7100() {

  // Uncache node types
  node_types_rebuild();

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

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

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

/**
 * Add date field to PM Project.
 */
function pmproject_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' => 'pmproject',
  ));

  // Migrate data
  $query = db_select('pmproject', 'pmpr');
  $query
    ->join('node', 'n', 'n.vid = pmpr.vid');
  $result = $query
    ->fields('pmpr', 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('pmproject', 'datebegin');
  db_drop_field('pmproject', 'dateend');
}

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

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

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

Functions

Namesort descending Description
pmproject_disable Implements hook_disable().
pmproject_enable Implements hook_enable().
pmproject_install Implements hook_install().
pmproject_schema Implements hook_schema().
pmproject_uninstall Implements hook_uninstall().
pmproject_update_7100 Adds Drupal 7 style body field to pm project node
pmproject_update_7101 Allow pm to override the default Project content type CRUD permissions
pmproject_update_7102 Display message to admin regarding need to rebuild permission.
pmproject_update_7103 Add date field to PM Project.
pmproject_update_7104 Adds billing status instance to PM Project content type.
pmproject_update_dependencies Implements hook_update_dependencies().
pmproject_update_last_removed Implements hook_update_last_removed().