You are here

function pmtimetracking_update_7104 in Drupal PM (Project Management) 7

Adds billing status instance to PM Timetracking content type.

File

pmtimetracking/pmtimetracking.install, line 208
Install functions for PM Timetracking.

Code

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');
}