<?php

/**
 * @file
 * Installation logic and hooks.
 */

/**
 * Implement hook_install().
 */
function archibald_kickstart_install() {
  _archibald_kickstart_install_config();
  _archibald_kickstart_install_enable_blocks();
  _archibald_kickstart_install_i18n();
  // We have a function _archibald_kickstart_install_user_roles(), which is
  // supposed to install default user roles and permissions. Unfortunately, we
  // cannot run this at install time, as the content type
  // "archibald_lomch_description" doesn't exist yet. This means that
  // permissions like "create archibald_lomch_description content" don't exist,
  // so we cannot grant it. Worse, Drupal actually fails with a PDO Exception
  // if we grant a permission that doesn't exist. This is why the function is
  // only called on hook_init(), once the content type has been set up. See
  // archibald_init() and archibald_kickstart_exit().
  /* _archibald_kickstart_install_user_roles(); */
}

/**
 * Helper function to set default configuration.
 */
function _archibald_kickstart_install_config() {
  // Set the default theme.
  theme_enable(array('adminimal'));
  variable_set('theme_default', 'adminimal');
  variable_set('admin_theme', 'adminimal');
  variable_set('node_admin_theme', 1);

  // Do not allow visitor account creation.
  variable_set('user_register', USER_REGISTER_ADMINISTRATORS_ONLY);

  // Deactivate error display.
  variable_set('error_level', ERROR_REPORTING_HIDE);

  // Set front page.
  variable_set('site_frontpage', 'admin/archibald/description/pending');

  // Set 403 redirect.
  variable_set('site_403','r4032login');

  // Show/edit the latest revision by default.
  variable_set('revisioning_view_callback', REVISIONING_LOAD_LATEST);
  variable_set('revisioning_edit_callback', REVISIONING_LOAD_LATEST);

  // Don't show terms in the revisions table.
  variable_set('revisioning_show_taxonomy_terms', 0);

  // Enhance Archibald forms.
  variable_set('archibald_enhance_admin_themes', 1);
  variable_set('archibald_activate_collapsible_vtabs', 1);
  variable_set('archibald_activate_footer_links', 1);
  variable_set('archibald_activate_sticky_vtabs', 1);

  // Set language negotiation to URL.
  require_once DRUPAL_ROOT . '/includes/language.inc';
  $language_providers = language_negotiation_info();
  $language_providers['locale-url']['weight'] = -10;
  language_negotiation_set('language', array(
    'locale-url' => $language_providers['locale-url'],
    LANGUAGE_NEGOTIATION_DEFAULT => $language_providers[LANGUAGE_NEGOTIATION_DEFAULT]
  ));
  language_negotiation_set('language_content', array(
    'locale-url' => $language_providers['locale-url'],
    LANGUAGE_NEGOTIATION_DEFAULT => $language_providers[LANGUAGE_NEGOTIATION_DEFAULT]
  ));

  // Circumvent a bug with Chosen. Until it gets fixed, this is a safe default
  // value.
  variable_set('chosen_minimum_width', 150);
}

/**
 * Helper function to enable default blocks.
 */
function _archibald_kickstart_install_enable_blocks() {
  $default_theme = variable_get('theme_default', 'adminimal');
  $blocks = array(
    array(
      'module' => 'system',
      'delta' => 'main',
      'theme' => $default_theme,
      'status' => 1,
      'weight' => 0,
      'region' => 'content',
      'visibility' => 0,
      'pages' => '',
      'cache' => DRUPAL_NO_CACHE,
    ),
    array(
      'module' => 'user',
      'delta' => 'login',
      'theme' => $default_theme,
      'status' => 1,
      'weight' => 0,
      'region' => 'content',
      'visibility' => 0,
      'pages' => '',
      'cache' => DRUPAL_NO_CACHE,
    ),
    array(
      'module' => 'lang_dropdown',
      'delta' => 'language_content',
      'theme' => $default_theme,
      'status' => 1,
      'weight' => -10,
      'region' => 'help',
      'visibility' => 0,
      'pages' => 'node/add/*',
      'cache' => DRUPAL_NO_CACHE,
    ),
  );
  $query = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'visibility', 'pages', 'cache'));
  foreach ($blocks as $block) {
    $query->values($block);
  }
  $query->execute();
}

/**
 * Helper function to enable configure i18n.
 */
function _archibald_kickstart_install_i18n() {
  // Make sure the source language is English !
  variable_set('i18n_string_source_language', 'en');
}

/**
 * Helper function to enable default user roles.
 */
function _archibald_kickstart_install_user_roles() {
  $archibald_minimum_perms = array(
    // Core permissions.
    'access administration pages',
    'access dashboard',
    'administer taxonomy',
    'create archibald_lomch_description content',
    'delete own archibald_lomch_description content',
    'edit own archibald_lomch_description content',
    'view own unpublished content',
    'view revisions',
    'view the administration theme',
    // Archibald permissions.
    'access archibald_stats statistics',
    'administer archibald_partners',
    'administer archibald_vcards',
    'import archibald descriptions',
    'view archibald_vcards',
    'view archibald_partners',
    // Contrib permissions.
    'access administration menu',
    'clone node',
    'publish revisions of own archibald_lomch_description content',
    'translate field_collection_item entities',
    'translate node entities',
    'view revision status messages',
    'view revisions of own archibald_lomch_description content',
    'view revisions of any archibald_lomch_description content',
    'edit revisions',
  );

  $weight = 2;
  foreach (array(
    'site administrator' => array_keys(module_invoke_all('permission')),
    'archibald editor' => $archibald_minimum_perms,
    'archibald administrator' => array_merge($archibald_minimum_perms, array(
      // Core permissions.
      'access content overview',
      'administer entity translation',
      'administer fields',
      'administer nodes',
      'administer site configuration',
      'delete any archibald_lomch_description content',
      'edit any archibald_lomch_description content',
      'flush caches',
      'translate interface',
      // Archibald permissions.
      'administer archibald',
      // Contrib permissions.
      'publish revisions',
      'publish revisions of any archibald_lomch_description content',
      'translate admin strings',
      'translate user-defined strings',
      'unpublish current revision',
    )),
  ) as $role_name => $permissions) {
    $role = (object) array(
      'name' => $role_name,
      'weight' => $weight++,
    );
    user_role_save($role);

    if ($role_name == 'site administrator') {
      variable_set('user_admin_role', $role->rid);
    }

    user_role_grant_permissions($role->rid, $permissions);
  }

  user_role_grant_permissions(DRUPAL_AUTHENTICATED_RID, array('access content'));
}

/**
 * Implements hook_update_N().
 *
 * - Always show the latest revision, also in node edit forms.
 * - Don't show Taxonomy terms in the revisions table.
 * - Enable i18n_taxonomy, if it isn't enabled yet.
 * - Update translations.
 */
function archibald_kickstart_update_7301() {
  variable_set('revisioning_view_callback', REVISIONING_LOAD_LATEST);
  variable_set('revisioning_edit_callback', REVISIONING_LOAD_LATEST);
  variable_set('revisioning_show_taxonomy_terms', 0);

  if (!module_exists('i18n_taxonomy')) {
    module_enable(array('i18n_taxonomy'));
  }

  // Refresh translations.
  archibald_kickstart_refresh_strings_and_import(array(
    'default',
    'field',
    'taxonomy',
  ));

  return array();
}

/**
 * Implements hook_update_N().
 *
 * - Update translations.
 */
function archibald_kickstart_update_7302() {
  // Refresh translations.
  archibald_kickstart_refresh_strings_and_import(array(
    'default',
    'field',
    'taxonomy',
  ));

  return array();
}

/**
 * Implements hook_update_N().
 *
 * - Update translations.
 */
function archibald_kickstart_update_7303() {
  // Refresh translations.
  archibald_kickstart_refresh_strings_and_import(array(
    'default',
    'field',
    'taxonomy',
  ));

  return array();
}

/**
 * Implements hook_update_N().
 *
 * - Update translations.
 * - Fix permissions issue.
 */
function archibald_kickstart_update_7304() {
  // Refresh translations.
  archibald_kickstart_refresh_strings_and_import(array(
    'default',
    'field',
    'taxonomy',
  ));

  foreach (array(
    'archibald editor',
    'archibald administrator',
  ) as $role_name) {
    $role = user_role_load_by_name($role_name);
    if ($role) {
      user_role_change_permissions($role->rid, array('edit revisions' => TRUE));
    }
  }

  return array();
}
