<?php

/**
 * @file
 * Install, update and uninstall functions for the front page module.
 */

/**
 * Implements hook_install().
 */
function front_page_install() {

}

/**
 * Implements hook_uninstall().
 */
function front_page_uninstall() {
  variable_del('front_page_enable');
  variable_del('front_page_breadcrumb');
  variable_del('front_page_breadcrumb_redirect');
  variable_del('special_notice_time');
  variable_del('special_notice_text');
}

/**
 * Implements hook_schema().
 */
function front_page_schema() {
  $schema['front_page'] = array(
    'description' => 'Stores Front Page settings.',
    'fields' => array(
      'rid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
        'description' => 'Primary Key: Role ID.',
      ),
      'mode' => array(
        'type' => 'varchar',
        'length' => 10,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The mode the front page will operate in for the selected role.',
      ),
      'data' => array(
        'type' => 'text',
        'not null' => TRUE,
        'size' => 'big',
        'description' => 'Contains the data for the selected mode. This could be a path or html to display.',
      ),
      'filter_format' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The filter format to apply to the data.',
      ),
      'weight' => array(
        'type' => 'int',
        'default' => 0,
        'description' => 'The weight of the front page setting.',
      )
    ),
    'primary key' => array('rid'),
    'indexes' => array(
      'weight' => array('weight'),
    ),
  );

  return $schema;
}

/**
 * Update 7200 - Add table if not already added, transfer data into the table and correct special notice times.
 */
function front_page_update_7200() {
  if (!db_table_exists('front_page')) {
    $schema = drupal_get_schema_unprocessed('front_page');
    _drupal_schema_initialize($schema, 'front_page', FALSE);
    if (isset($schema['front_page'])) {
      db_create_table('front_page', $schema['front_page']);
    }

    // need to add front page data from variables table to new table.
    $formats = filter_formats();
    $formats = drupal_map_assoc(array_keys($formats));
    $default_format = array_shift($formats);
    $php_format = isset($formats['php_code']) ? 'php_code' : $default_format;

    $roles = user_roles();
    foreach ($roles as $rid => $role_name) {
      $mode = variable_get('front_' . $rid . '_type', '');
      if (!empty($mode)) {
        switch ($mode) {
          case 'themed':
          case 'full':
            $data = variable_get('front_' . $rid . '_text', '');
            $format = $default_format;
            if (variable_get('front_' . $rid . '_php', 0)) {
              $format = $php_format;
            }
            break;
          case 'redirect':
          case 'alias':
            $data = variable_get('front_' . $rid . '_redirect', '');
            $format = '';
            break;
          default:
            $mode = '';
            $format = '';
            $data = '';
            break;
        }
        $weight = $rid * -1;
        db_merge('front_page')
            ->key(array('rid' => $rid))
            ->fields(array(
              'mode' => $mode,
              'data' => $data,
              'filter_format' => $format,
              'weight' => $weight,
            ))
            ->execute();
        variable_del('front_' . $rid . '_type');
        variable_del('front_' . $rid . '_text');
        variable_del('front_' . $rid . '_php');
        variable_del('front_' . $rid . '_redirect');
      }
    }
  }

  if (variable_get('site_frontpage', 'node') == 'front_page') {
    variable_set('site_frontpage', 'node');
  }
}

function front_page_update_7201() {
  variable_del('special_notice_time');
  variable_del('special_notice_text');

  variable_del('front_page_breadcrumb');
  $path = variable_get('front_page_breadcrumb_redirect', '');
  variable_set('front_page_home_link_path', $path);
  variable_del('front_page_breadcrumb_redirect');
}