<?php
/**
 * @file
 * Install, update and uninstall functions for the Node Subpages module.
 */

/**
 * Implements hook_enable().
 */
function node_subpages_enable() {
  // Set the weight to 2, so that it is higher than the weight on the pathauto module
  // Path Auto needs to set the node path before this module adds aliases
  db_update('system')
  ->fields(array(
    'weight' => 2,
  ))
  ->condition('name', 'node_subpages')
  ->condition('type', 'module')
  ->execute();

  // Clear the cache to get these to take effect.
  cache_clear_all();
}


/**
 * Implements hook_schema().
 */
function node_subpages_schema() {
  $schema['node_subpages'] = array(
    'description' => 'Store details about node subpage configuration',
    'export' => array(
      'key' => 'machine_name',
      'identifier' => 'path', // Exports will be as $path
      'default hook' => 'default_node_subpages_path',  // Function hook name.
      'api' => array(
        'owner' => 'node_subpages',
        'api' => 'default_node_subpages_paths',  // Base name for api include files.
        'minimum_version' => 1,
        'current_version' => 1,
      ),
    ),
    'fields' => array(
      'id' => array(
        'type' => 'serial',
      ),
      'machine_name' => array(
        'description' => t('Machine name'),
        'type' => 'varchar',
        'length' => '128',
        'not null' => TRUE,
        'default' => '',
      ),
      'node_type' => array(
        'description' => 'Machine name of node type',
        'type' => 'varchar',
        'length' => '32',
        'not null' => TRUE,
      ),
      'subpath' => array(
        'description' => 'URL subpath for this subpage',
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
      ),
      'title' => array(
        'description' => 'Title for the subpage tab.',
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE,
      ),
      'is_default' => array(
        'description' => 'Is this tab the default one?',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'source_type' => array(
        'description' => 'Name of the plugin to use as the content for this subpage',
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE,
      ),
      'source_data' => array(
        'description' => 'Serialized config for the content plugin',
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE,
        'serialize' => TRUE,
      ),
      'weight' => array(
        'description' => 'Weight of tab, for positioning',
        'type' => 'int',
        'length' => '11',
        'not null' => FALSE,
      ),
    ),
    'primary key' => array('id'),
    'unique keys' => array(
      'machine_name' => array('machine_name'),
    ),
    'indexes' => array(
      'node_type' => array('node_type'),
    ),
  );
  return $schema;
}


/**
 * Implements hook_update_N().
 */
function node_subpages_update_7200(&$sandbox) {
  $ret = array();

  // Update subpages that use fields
  $query = db_select('node_subpages', 's')
    ->fields('s', array('source_data', 'id'))
    ->condition('source_type', 'cck')
    ->execute();
  while($record = $query->fetchAssoc()) {
    $record['source_type'] = 'field';
    $record['source_data'] = serialize(array('field' => $record['source_data']));
    drupal_write_record('node_subpages', $record, 'id');
  }

  // Update subpages that use Views
  $query = db_select('node_subpages', 's')
    ->fields('s', array('source_data', 'id'))
    ->condition('source_type', 'view')
    ->execute();
  while($record = $query->fetchAssoc()) {
    list($view, $display) = explode(':', $record['source_data']);
    $record['source_data'] = serialize(array('view' => $view, 'display' => $display));
    $record['source_type'] = 'views';
    drupal_write_record('node_subpages', $record, 'id');
  }

  // Convert source_data row to be serialized
  $source_data_spec = array(
    'description' => 'Serialized config for the content plugin',
    'type' => 'varchar',
    'length' => '255',
    'not null' => FALSE,
    'serialize' => TRUE,
  );
  db_change_field('node_subpages', 'source_data', 'source_data', $source_data_spec);

  return $ret;
}

/**
 * Set the machine name field so subpage config will be exportable.
 */
function node_subpages_update_7201(&$sandbox) {
  $machine_name_schema = array(
    'description' => t('Machine name'),
    'type' => 'varchar',
    'length' => '128',
    'not null' => TRUE,
    'default' => '',
  );
  db_add_field('node_subpages', 'machine_name', $machine_name_schema);
  db_add_index('node_subpages', 'machine_name', array('machine_name'));

  $query = db_select('node_subpages', 's')
    ->fields('s', array('id', 'node_type', 'subpath'))
    ->execute();
  while($record = $query->fetchAssoc()) {
    $record['machine_name'] = $record['node_type'] . '_' . $record['subpath'];
    drupal_write_record('node_subpages', $record, 'id');
  }
}


/**
 * Implements hook_requirements().
 *
 * Check the version of CTools that's installed.
 */
function node_subpages_requirements($phase) {
  $t = get_t();
  $requirements = array();
  drupal_load('module', 'node_subpages');
  if (module_exists('ctools')) {
    if (!node_subpages_check_ctools_version()) {
       $requirements['node_subpages_ctools'] = array(
         'title' => $t('Node Subpages requires Chaos Tool Suite (CTools). Please install the latest version of CTools.'),
         'value' => $t('Between @a and @b', array('@a' => NODE_SUBPAGES_MINIMUM_CTOOLS_API_VERSION, '@b' => NODE_SUBPAGES_MAXIMUM_CTOOLS_API_VERSION)),
         'severity' => REQUIREMENT_ERROR,
       );
    }
  }
  return $requirements;
}
