<?php
/*
 +--------------------------------------------------------------------+
 | Copyright CiviCRM LLC. All rights reserved.                        |
 |                                                                    |
 | This work is published under the GNU AGPLv3 license with some      |
 | permitted exceptions and without any warranty. For full license    |
 | and copyright information, see https://civicrm.org/licensing       |
 +--------------------------------------------------------------------+
 */

/**
 *
 * @package CRM
 * @copyright CiviCRM LLC https://civicrm.org/licensing
 */

/**
 * Enable CiviCRM.
 */
function civicrm_enable() {
  db_query("UPDATE {system} SET weight = 100 WHERE name = 'civicrm'");

  menu_rebuild();

  menu_link_maintain('civicrm', 'delete', 'civicrm', 'CiviCRM');

  $options = array(
    'link_title' => 'CiviCRM',
    'link_path' => 'civicrm/dashboard',
    'module' => 'civicrm',
    'options' => array('alter' => TRUE),
  );
  menu_link_save($options);

  if (!civicrm_initialize()) {
    $installLink = url('civicrm/setup');
    $message = t('<p class="error"><strong>CiviCRM is almost ready.</strong> You must <a href="@url">configure CiviCRM</a> for it to work.</p>', [
      '@url' => $installLink,
    ]);
    drupal_set_message($message);
    return;
  }

  // also invoke civicrm menu rebuild
  CRM_Core_Menu::store();

  // Update the 'blocks' DB table with the blocks.
  if (module_exists('block')) {
    _block_rehash();
  }
}

/**
 * Implements hook_uninstall().
 */
function civicrm_uninstall() {
  require_once 'civicrm.module';

  if (!civicrm_initialize()) {
    return;
  }

  require_once 'CRM/Core/Config.php';
  $config = CRM_Core_Config::singleton();

  require_once 'CRM/Core/DAO.php';
  CRM_Core_DAO::dropAllTables();
}

function civicrm_requirements($phase) {
  $requirements = array();
  $t = get_t();
  switch ($phase) {
    case 'runtime':
      if (!civicrm_initialize()) {
        $installLink = url('civicrm/setup');
        $requirements['civicrm'] = array(
          'title' => $t("CiviCRM"),
          'severity' => REQUIREMENT_ERROR,
          'value' => $t('<p class="error"><strong>CiviCRM is almost ready.</strong> You must <a href="@url">configure CiviCRM</a> for it to work.</p>', [
            '@url' => $installLink,
          ]),
        );
        return $requirements;
      }
      break;
  }
  return $requirements;
}

/**
 * Update CiviCRM module weight
 */
function civicrm_update_7400(&$sandbox) {
  db_query("UPDATE {system} SET weight = 100 WHERE name = 'civicrm'");
}

/**
 * Trigger cache clear to pick up TZ handling change from CRM-6877.
 */
function civicrm_update_7401($sandbox) {
  // This is an empty hook_update_N() so that caches will be
  // cleared when update_finished() is called.
  return t('TZ changes in CiviCRM Views picked up.');
}
