You are here

pmexpense.install in Drupal PM (Project Management) 7

Installation file for pmexpense module.

File

pmexpense/pmexpense.install
View source
<?php

/**
 * @file
 * Installation file for pmexpense module.
 */

/**
 * Implements hook_install().
 */
function pmexpense_install() {
  variable_set('node_options_pmexpense', array(
    'status',
  ));
  variable_set('node_permissions_pmexpense', 0);

  // Expense date field.
  if (!field_info_field('pmexpense_date')) {
    field_create_field(array(
      'type' => 'datetime',
      'field_name' => 'pmexpense_date',
      'settings' => array(
        'granularity' => array(
          'year' => 'year',
          'month' => 'month',
          'day' => 'day',
          'hour' => 0,
          'minute' => 0,
          'second' => 0,
        ),
        'tz_handling' => '',
        'timezone_db' => '',
        'todate' => '',
      ),
      'cardinality' => '1',
    ));
  }
  if (!field_info_instance('node', 'pmexpense_date', 'pmexpense')) {
    field_create_instance(array(
      'field_name' => 'pmexpense_date',
      'bundle' => 'pmexpense',
      'label' => 'Expense Date',
      'required' => 1,
      'entity_type' => 'node',
      'widget' => array(
        'weight' => '-18',
        'type' => 'date_text',
        'active' => 1,
      ),
      'settings' => array(
        'default_value' => 'now',
      ),
    ));
  }

  // Set up instance of billing status field.
  if (!field_info_instance('node', 'pm_billing_status', 'pmexpense')) {
    field_create_instance(array(
      'field_name' => 'pm_billing_status',
      'label' => 'Billing status',
      'entity_type' => 'node',
      'bundle' => 'pmexpense',
      'widget' => array(
        'weight' => -17,
        '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['Expense status'] = array(
    'outstanding' => 'Outstanding',
    'paid' => 'Paid',
  );
  $attributes['Expense status search'] = array(
    'outstanding' => 'Outstanding',
    'paid' => 'Paid',
  );
  $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 pmexpense_enable() {
  node_access_needs_rebuild(TRUE);
}

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

/**
 * Implements hook_uninstall().
 */
function pmexpense_uninstall() {
  drupal_uninstall_schema('pmexpense');
  db_delete('pmattribute')
    ->condition('domain', array(
    'Expense status',
    'Expense status search',
  ), 'IN')
    ->execute();
}

/**
 * Implements hook_schema().
 */
function pmexpense_schema() {
  $schema['pmexpense'] = 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,
      ),
      'provider_nid' => array(
        'type' => 'int',
      ),
      'provider_title' => array(
        'type' => 'varchar',
        'length' => 128,
      ),
      'expensestatus' => array(
        'type' => 'varchar',
        'length' => 128,
      ),
      'amount' => array(
        'type' => 'float',
      ),
      'tax1app' => array(
        'type' => 'int',
      ),
      'tax1percent' => array(
        'type' => 'float',
      ),
      'tax1' => array(
        'type' => 'float',
      ),
      'subtotal' => array(
        'type' => 'float',
      ),
      'tax2app' => array(
        'type' => 'int',
      ),
      'tax2percent' => array(
        'type' => 'float',
      ),
      'tax2' => array(
        'type' => 'float',
      ),
      'total' => 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',
      ),
      'provider_nid' => array(
        'provider_nid',
      ),
    ),
  );
  return $schema;
}

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

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

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

/**
 * Allow Project Management to override default Expense permissions.
 */
function pmexpense_update_7100() {
  variable_set('node_permissions_pmexpense', 0);
  return 'PM Expense permissions overridden';
}

/**
 * Add billable and billed fields to the {pmexpense} table.
 */
function pmexpense_update_7101() {
  $spec = array(
    'type' => 'int',
    'default' => 0,
    'initial' => 0,
  );
  db_add_field('pmexpense', 'billable', $spec);
  db_add_field('pmexpense', 'billed', $spec);
}

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

/**
 * Convert Expense Date field to Field API.
 */
function pmexpense_update_7103() {

  // Expense date field.
  field_create_field(array(
    'type' => 'datetime',
    'field_name' => 'pmexpense_date',
    'settings' => array(
      'granularity' => array(
        'year' => 'year',
        'month' => 'month',
        'day' => 'day',
        'hour' => 0,
        'minute' => 0,
        'second' => 0,
      ),
      'tz_handling' => '',
      'timezone_db' => '',
      'todate' => '',
    ),
    'cardinality' => '1',
  ));
  field_create_instance(array(
    'field_name' => 'pmexpense_date',
    'bundle' => 'pmexpense',
    'label' => 'Expense Date',
    'required' => 1,
    'entity_type' => 'node',
    'widget' => array(
      'weight' => '-18',
      'type' => 'date_text',
      'active' => 1,
    ),
    'settings' => array(
      'default_value' => 'now',
    ),
  ));

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

  // Delete columns from existing database table.
  db_drop_field('pmexpense', 'expensedate');
}

/**
 * Adds billing status instance to PM Expense content type.
 */
function pmexpense_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' => 'pmexpense',
    'widget' => array(
      'weight' => -17,
      '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',
      ),
    ),
  ));

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

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

Functions

Namesort descending Description
pmexpense_disable Implements hook_disable().
pmexpense_enable Implements hook_enable().
pmexpense_install Implements hook_install().
pmexpense_schema Implements hook_schema().
pmexpense_uninstall Implements hook_uninstall().
pmexpense_update_7100 Allow Project Management to override default Expense permissions.
pmexpense_update_7101 Add billable and billed fields to the {pmexpense} table.
pmexpense_update_7102 Display message to admin regarding need to rebuild permission.
pmexpense_update_7103 Convert Expense Date field to Field API.
pmexpense_update_7104 Adds billing status instance to PM Expense content type.
pmexpense_update_dependencies Implements hook_update_dependencies().
pmexpense_update_last_removed Implements hook_update_last_removed().