<?php

/**
 * @file
 * An installation file for Panopoly Core
 */

/**
 * Implementation of hook_requirements()
 * 
 * Adapted from system_requirements()
 */
function panopoly_core_requirements($phase) {

  // Kick us off
  $requirements = array();

  // Ensure translations don't break at install time
  $t = get_t();

  // Let's require more memory and force Drupal core to accept it
  $memory_limit = ini_get('memory_limit');
  $panopoly_memory_limit = '196M';
  $requirements['panopoly_core_memory_limit'] = array(
    'title' => $t('PHP memory limit'),
    'value' => $memory_limit == -1 ? t('-1 (Unlimited)') : $memory_limit,
    'weight' => 0,
  );

  if ($memory_limit && $memory_limit != -1 && parse_size($memory_limit) < parse_size($panopoly_memory_limit)) {
    $description = '';
    if ($phase == 'install') {
      $description = $t('Consider increasing your PHP memory limit to %memory_minimum_limit to help prevent errors in the installation process.', array('%memory_minimum_limit' => $panopoly_memory_limit));
    }
    elseif ($phase == 'update') {
      $description = $t('Consider increasing your PHP memory limit to %memory_minimum_limit to help prevent errors in the update process.', array('%memory_minimum_limit' => $panopoly_memory_limit));
    }
    elseif ($phase == 'runtime') {
      $description = $t('Depending on your configuration, Drupal can run with a %memory_limit PHP memory limit. However, a %memory_minimum_limit PHP memory limit or above is recommended, especially if your site uses additional custom or contributed modules.', array('%memory_limit' => $memory_limit, '%memory_minimum_limit' => $panopoly_memory_limit));
    }

    if (!empty($description)) {
      if ($php_ini_path = get_cfg_var('cfg_file_path')) {
        $description .= ' ' . $t('Increase the memory limit by editing the memory_limit parameter in the file %configuration-file and then restart your web server (or contact your system administrator or hosting provider for assistance).', array('%configuration-file' => $php_ini_path));
      }
      else {
        $description .= ' ' . $t('Contact your system administrator or hosting provider for assistance with increasing your PHP memory limit.');
      }

      $requirements['panopoly_core_memory_limit']['description'] = $description . ' ' . $t('See the <a href="@url">Drupal requirements</a> for more information.', array('@url' => 'http://drupal.org/requirements'));
      $requirements['panopoly_core_memory_limit']['severity'] = REQUIREMENT_WARNING;
    }
  }
  return $requirements;
}

/**
 * Implementation of hook_install()
 */
function panopoly_core_install() {

  // Add login link for users to log in
  $item = array(
    'link_path' => 'user/login',
    'link_title' => 'Log in',
    'menu_name' => 'user-menu',
  );
  menu_link_save($item);
}

/**
 * Implemenetation of hook_uninstall()
 */
function panopoly_core_uninstall() {

}

/** 
 * Panopoly Beta 5 Update: Correctly Setting install_task to "done"
 */
function panopoly_core_update_7001(&$sandbox) {
  variable_set('install_task', 'done');
}

/**
 * Ensure latest pathauto_update_7006() has run.
 */
function panopoly_core_update_7002() {
  if (function_exists('pathauto_update_7006')) {
    $res = pathauto_update_7006();
    if (db_table_exists('pathauto')) {
      db_query("INSERT INTO {pathauto_state} (entity_type, entity_id, pathauto) SELECT entity_type, entity_id, pathauto FROM {pathauto}");
      db_drop_table('pathauto');
    }
    return $res;
  }
}

