<?php
// $Id: ca.install,v 1.1.2.8 2009/07/21 14:37:19 islandusurper Exp $

/**
 * @file
 * Install hooks for ca.module.
 */

/**
 * Implementation of hook_schema().
 */
function ca_schema() {
  $schema = array();

  $schema['ca_predicates'] = array(
    'fields' => array(
      'pid' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'title' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'description' => array(
        'type' => 'text',
      ),
      'class' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'status' => array(
        'type' => 'int',
        'size' => 'tiny',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'weight' => array(
        'type' => 'int',
        'size' => 'small',
        'not null' => TRUE,
        'default' => 0,
      ),
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'ca_trigger' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'conditions' => array(
        'type' => 'text',
        'size' => 'big',
        'serialize' => TRUE,
      ),
      'actions' => array(
        'type' => 'text',
        'size' => 'big',
        'serialize' => TRUE,
      ),
      'created' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'modified' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'indexes' => array(
      'ca_predicates_class' => array('class'),
      'ca_predicates_status' => array('status'),
      'ca_predicates_ca_trigger' => array('ca_trigger'),
    ),
    'primary key' => array('pid'),
  );

  return $schema;
}

/**
 * Implementation of hook_install().
 */
function ca_install() {
  drupal_install_schema('ca');
}

/**
 * Implementation of hook_uninstall().
 */
function ca_uninstall() {
  drupal_uninstall_schema('ca');
}

/**
 * Update argument IDs in saved predicates.
 */
function ca_update_6001() {
  $ret = array();

  $result = db_query("SELECT pid, conditions, actions FROM {ca_predicates}");
  while ($predicate = db_fetch_array($result)) {
    foreach (array('conditions', 'actions') as $section) {
      $data = $predicate[$section];
      $data = str_replace('s:5:"order";s:8:"uc_order";', 's:5:"order";s:5:"order";', $data);
      $data = str_replace('s:4:"user";', 's:7:"account";', $data);
      $predicate[$section] = $data;
    }
    db_query("UPDATE {ca_predicates} SET conditions = '%s', actions = '%s' WHERE pid = '%s'", $predicate['conditions'], $predicate['actions'], $predicate['pid']);
  }

  $ret[] = array('success' => TRUE, 'query' => 'Updated argument IDs in saved predicates.');

  return $ret;
}
