<?php

/**
 * @file
 * Install, update and uninstall functions for the Groups install profile.
 */

/**
 * Implements hook_requirements().
 * Set minimum 'max_execution_time' requirement
 */
function groups_requirements($phase) {
  $requirements = array();
  // Ensure translations don't break during installation.
  $t = get_t();
  if($phase == 'install') {
    // Test PHP minimum execution time
    $requirements['php_max_execution_time'] = array(
      'title' => $t('PHP Max Execution Time'),
      'value' => ini_get('max_execution_time'),
    );
    $max_execution_time = (int)ini_get('max_execution_time');
    // Don't set the error when max_execution_time is 0 (Infinite).
    if ($max_execution_time && $max_execution_time < DRUPAL_MINIMUM_MAX_EXECUTION_TIME) {
      $requirements['php_max_execution_time']['description'] = $t('Your PHP execution time is too low, please set it greater than or equal to %time seconds.', array('%time' => DRUPAL_MINIMUM_MAX_EXECUTION_TIME));
      $requirements['php_max_execution_time']['severity'] = REQUIREMENT_ERROR;
    }
  }
  return $requirements;
}

/**
 * Implements hook_install().
 *
 * Perform actions to set up the site for this profile.
 *
 * @see system_install()
 */
function groups_install() {
  // Enable the openstack_bootstrap theme and set it as the default.
  theme_enable(array('adaptivetheme', 'adaptivetheme_admin', 'openstack', 'openstack_bootstrap'));

    // The Bartik theme is automatically enabled during installation. Disable it.
  db_update('system')
    ->fields(array('status' => 0))
    ->condition('type', 'theme')
    ->condition('name', 'bartik')
    ->execute();

  // Set the default and administration themes.
  variable_set('theme_default', 'openstack_bootstrap');
  // Set a default administrative theme:
  variable_set('admin_theme', 'adaptivetheme_admin');
  variable_set('node_admin_theme', FALSE);

  // Do not use the administration theme when editing or creating content.
  variable_set('node_admin_theme', '0');

  // Set a default user avatar.
  groups_set_default_avatar();

  // Create openstack.org menu item
  $item = array(
    'link_title' => st('OpenStack.org'),
    'link_path' => 'http://openstack.org',
    'menu_name' => 'main-menu',
    'weight' => -5,
  );
  menu_link_save($item);

  $search_block = array(
    'module' => 'search',
    'delta' => 'form',
    'theme' => 'openstack',
    'visibility' => 0,
    'region' => 'header',
    'status' => 1,
    'pages' => '',
    'weight' => 2,
    'title' => '<none>',
  );
  drupal_write_record('block', $search_block);

  _groups_enable_default_languages();
  _groups_add_language_selector_block();

  // Create a default role for site administrators, with all available permissions assigned.
  // $admin_role = new stdClass();
  // $admin_role->name = 'administrator';
  // $admin_role->weight = 2;
  // user_role_save($admin_role);
  // Set this as the administrator role.
  variable_set('user_admin_role', $admin_role->rid);

  // disable automatic cron run
  variable_set('cron_safe_threshold', 0);
  variable_set('elysia_cron_disabled', true);

  // Assign user 1 the "administrator" role.
  // db_insert('users_roles')
  //   ->fields(array('uid' => 1, 'rid' => $admin_role->rid))
  //   ->execute();

  // AdaptiveTheme requires that the system theme settings form
  // be submitted in order for its themes' settings to be properly set
  // and the resulting css files generated.
  // For more background, see http://drupal.org/node/1776730.
  module_load_include('inc', 'system', 'system.admin');
  foreach (array('adaptivetheme') as $theme_name) {
    $form_state = form_state_defaults();
    $form_state['build_info']['args'][0] = $theme_name;
    $form_state['values'] = array();
    drupal_form_submit('system_theme_settings', $form_state);
  }
  // add markdown filter format
  _groups_add_markdown_filter();
}


/**
 * Add language selector block to header region.
 *
 * @see groups_install()
 */
function _groups_add_language_selector_block() {
  $block = array(
    'module' => 'locale',
    'delta' => 'language',
    'theme' => 'openstack_bootstrap',
    'visibility' => 0,
    'region' => 'header',
    'status' => 1,
    'pages' => '',
    'weight' => 2,
    'title' => '<none>',
  );
  drupal_write_record('block', $block);
}

/**
 * Enable default languages.
 */
function _groups_enable_default_languages() {
  include_once DRUPAL_ROOT . '/includes/locale.inc';
  include_once DRUPAL_ROOT . '/includes/iso.inc';
  $default_languages = variable_get('groups_default_languages', 'hu,es,de,ru,zh-hans,ja,fi');
  $languages = explode(',', $default_languages);
  foreach ($languages as $langcode) {
    locale_add_language($langcode);
  }
}

/**
 * Enable markdown module if it is not already enabled.
 */
function groups_update_7100() {
  module_enable(array('markdown'));
  if (!module_exists('markdown')) {
    throw new DrupalUpdateException('The <a href="https://drupal.org/project/markdown">Markdown module</a> must be downloaded and available for groups updates to proceed.');
  }
  _groups_add_markdown_filter();
}

/**
 * Enable feeds_jsonpath_parser module if it is not already enabled.
 */
function groups_update_7101() {
  module_enable(array('feeds_jsonpath_parser'));
}

/**
 * Enable groups_events_pages module if it is not already enabled.
 */
function groups_update_7102() {
  module_enable(array('groups_events_pages'));
}

/**
 * Enable commons_wikis module to fix missing og_group_ref
 * fields from group contents.
 */
function groups_update_7103() {
  if (!module_exists('commons_wikis')) {
    module_enable(array('commons_wikis'));
    drupal_flush_all_caches();
  }
}

/**
 * Switch default theme to "openstack_bootstrap"
 */
function groups_update_7105() {
  theme_enable(array('openstack_bootstrap'));
  variable_set('theme_default', 'openstack_bootstrap');
}

/**
 * Enable groups_pages module.
 */
function groups_update_7104() {
  if (!module_exists('groups_pages')) {
    module_enable(array('groups_pages'));
    drupal_flush_all_caches();
  }
}

/**
 * Fix groups update order issue.
 * groups_pages module must be enabled after update #7105.
 */
function groups_update_7106() {
  groups_update_7104();
}

/**
 * Enable groups_events module.
 */
function groups_update_7107() {
  if (!module_exists('groups_events')) {
    module_enable(array('groups_events'));
    drupal_flush_all_caches();
  }
}

/**
 * Enable groups_oauth2 modules.
 */
function groups_update_7108() {
  if ((!module_exists('groups_oauth2')) && (!module_exists('groups_oauth2_picture'))) {
    module_enable(array('groups_oauth2', 'groups_oauth2_picture'));
    drupal_flush_all_caches();
  }
}

/**
 * Enable groups_comment module.
 */
function groups_update_7109() {
  if ((!module_exists('groups_comment')) && (!module_exists('groups_comment'))) {
    module_enable(array('groups_comment'));
    drupal_flush_all_caches();
  }
}

/**
 * Disable commons_wiki module.
 */
function groups_update_7110() {
  if (module_exists('commons_wiki')) {
    module_disable(array('groups_comment'));
    drupal_flush_all_caches();
  }
}

/**
 * Re-enable commons_wikis and enable groups_wikis module.
 */
function groups_update_7111() {
  if (!module_exists('commons_wikis')) {
    module_enable(array('commons_wikis'));
  }
  if (!module_exists('groups_wikis')) {
    module_enable(array('groups_wikis'));
  }
  drupal_flush_all_caches();
}

/**
 * Enable smtp module.
 */
function groups_update_7112() {
  if (!module_exists('smtp')) {
    module_enable(array('smtp'));
    drupal_flush_all_caches();
  }
}

/**
 * Add language selector as a block and enable locale, l10n_update modules.
 */
function groups_update_7113() {
  drupal_uninstall_schema('locale');
  drupal_flush_all_caches();
  module_enable(array('locale', 'l10n_update'));
  module_disable(array('groups_language_switcher'));
  _groups_add_language_selector_block();
  _groups_enable_default_languages();
  _groups_set_language_negotiation();
}

/**
 * Enable elysia_cron module.
 */
function groups_update_7114() {
  if (!module_exists('elysia_cron')) {
    module_enable(array('elysia_cron'));
  }
  // remove job_scheduler's jobs
  db_delete('job_schedule')->execute();
  // disable automatic cron run
  variable_set('cron_safe_threshold', 0);
  variable_set('elysia_cron_disabled', true);
  drupal_flush_all_caches();
}

/**
 * Enable groups_reports module.
 */
function groups_update_7115() {
  if (!module_exists('groups_reports')) {
    module_enable(array('groups_reports'));
  }
  drupal_flush_all_caches();
}

/**
 * Enable chartjs module.
 */
function groups_update_7116() {
  if (!module_exists('chartjs')) {
    module_enable(array('chartjs'));
  }
  drupal_flush_all_caches();
}

/**
 * Enable groups ambassador modules.
 */
function groups_update_7117() {
  if (!module_exists('groups_ambassador')) {
    module_enable(array('groups_ambassador'));
  }
    if (!module_exists('groups_ambassador_pages')) {
      module_enable(array('groups_ambassador_pages'));
    }
  drupal_flush_all_caches();
}

/**
 * Enable groups social link module.
 */
function groups_update_7118() {
  if (!module_exists('groups_social_link')) {
    module_enable(array('groups_social_link'));
  }
  drupal_flush_all_caches();
}

/**
 * Enable groups user profile module.
 */
function groups_update_7119() {
  if (!module_exists('groups_user_profile')) {
    module_enable(array('groups_user_profile'));
  }
  drupal_flush_all_caches();
}

/**
 * Enable groups messages and privatemsg_notify_sender contrib module.
 */
function groups_update_7120() {
  if (!module_exists('groups_messages')) {
    module_enable(array('groups_messages'));
  }
  if (!module_exists('privatemsg_notify_sender')) {
    module_enable(array('privatemsg_notify_sender'));
  }
  drupal_flush_all_caches();
}

/**
 * Disable leaflet_more_maps module.
 *
 * The site no longer depends on this module, disabled now, so the entire
 * module should be removed in a later version.
 */
function groups_update_7121() {
  if (module_exists('leaflet_more_maps')) {
    module_disable(array('leaflet_more_maps'));
    drupal_flush_all_caches();
  }
}

/**
 * Enable tzfield module related to https://review.openstack.org/#/c/341385/
 */
function groups_update_7122() {
  if (!module_exists('tzfield')) {
    module_enable(array('tzfield'));
  }
  drupal_flush_all_caches();
}

/**
 * Set language negotiation to URL based.
 */
function _groups_set_language_negotiation() {
  require_once DRUPAL_ROOT . '/includes/language.inc';
  require_once DRUPAL_ROOT . '/includes/locale.inc';
  $negotiation = array(
    LOCALE_LANGUAGE_NEGOTIATION_URL => 2,
    LANGUAGE_NEGOTIATION_DEFAULT => 10,
  );
  language_negotiation_set(LANGUAGE_TYPE_INTERFACE, $negotiation);
}

/**
 * Add markdown filter with permissions.
 */
function _groups_add_markdown_filter() {
  // add markdown format filter
  $markdown_format = array(
    'format' => 'markdown',
    'name' => 'Markdown',
    'weight' => 1,
    'filters' => array(
      // Markdown filter.
      'filter_markdown' => array(
        'weight' => 0,
        'status' => 1,
        'settings' => array(),
      ),
      // URL filter.
      'filter_url' => array(
        'weight' => 0,
        'status' => 1,
      ),
    ),
  );
  $markdown_format = (object) $markdown_format;
  filter_format_save($markdown_format);
  // assign default permissions to filter
  if ($roles = user_roles(FALSE)) {
    $permissions = array('use text format markdown');
    foreach ($roles as $rid => $name) {
      $module = 'filter';
      foreach ($permissions as $name) {
        db_merge('role_permission')->key(array(
          'rid' => $rid,
          'permission' => $name,
        ))->fields(array(
          'module' => $module,
        ))->execute();
      }
    }
  }
}