You are here

Helpers.php in Calendar Systems 8.2

Contains \Drupal\calendar_systems.

File

src/Helpers.php
View source
<?php

/**
 * @file
 * Contains \Drupal\calendar_systems.
 */
namespace Drupal\calendar_systems;

use Drupal\calendar_systems\Calendar\Calendar;
class Helpers {

  /**
   * Internal helper to get all active languages.
   *
   * @return
   *   An array of active languages.
   */
  static function calendar_systems_languages() {

    // Default:
    $languages['default'] = array(
      'name' => 'Default',
    );

    // Check if there's any additional locale available:
    $locales = function_exists('locale_language_list') ? locale_language_list('name', TRUE) : NULL;

    // If found, append 'em:
    if (!is_null($locales)) {
      foreach ($locales as $name => $title) {
        $languages[$name] = array(
          'name' => $title,
        );
      }
    }
    return $languages;
  }

  /**
   * Internal helper to return site's current language.
   *
   * @return
   *   Current language.
   */
  static function calendar_systems_languages_active() {

    // only convert farsi on multi lingual sites
    $isMultiLingual = count(\Drupal::languageManager()
      ->getLanguages()) > 1;
    if ($isMultiLingual) {
      $result = \Drupal::languageManager()
        ->getCurrentLanguage()
        ->getId();
    }
    else {
      $result = 'default';
    }
    return $result;
  }

  /**
   * Internal static cache helper to get all available profiles.
   *
   * @return
   *   An array of available profiles.
   */
  static function calendar_systems_profiles() {
    $profiles = \Drupal::config('calendar_systems.settings')
      ->get('profiles');
    if (empty($profiles)) {
      $profiles = [];
    }
    return $profiles;
  }
  static function calendar_systems_profiles_update($profiles) {
    \Drupal::configFactory()
      ->getEditable('calendar_systems.settings')
      ->set('profiles', $profiles)
      ->save();
  }

  /**
   * Helper to determine the active calendar system
   * @param null $language
   * @return string
   */
  static function calendar_systems_get_calendar_system_name($language = NULL) {

    // Load all available profiles:
    $profiles = self::calendar_systems_profiles();

    // Get site's current language:
    if (is_null($language)) {
      $language = self::calendar_systems_languages_active();
    }
    if (isset($profiles[$language])) {
      $calendar_system = $profiles[$language]['calendar_system'];
    }
    elseif (isset($profiles['default'])) {
      $calendar_system = $profiles['default']['calendar_system'];
    }
    if (empty($calendar_system)) {
      $calendar_system = 'default';
    }
    return $calendar_system;
  }

  /**
   * Gets an instance of the calendar object for
   * the selected calendar system acording to the settings
   * or the passed arguments
   *
   * @param null $calendar_system
   * @param null $language
   * @return \cmfcCalendarV1 Calendar object or default on fail.
   */
  static function calendar_systems_get_calendar_instance($calendar_system = NULL, $language = NULL) {
    if (is_null($calendar_system)) {
      $calendar_system = self::calendar_systems_get_calendar_system_name($language);
    }
    if ($calendar_system == 'default') {
      $calendar_system = 'gregorian';
    }
    $calendar = Calendar::factory('v1', array(
      'name' => $calendar_system,
    ));
    return $calendar;
  }
  static function calendar_systems_add_strings() {
    $calendar = Calendar::factory('v1', array());
    $stringGroups = $calendar
      ->getAllStrings();
    foreach ($stringGroups as $stringGroupName => $stringGroup) {
      foreach ($stringGroup as $langcode => $strings) {
        foreach ($strings as $string) {
          $options = array(
            'langcode' => $langcode,
          );
          if ($stringGroupName == 'monthsName') {
            $options['context'] = 'Long month name';
          }
          if ($stringGroupName == 'weeksShortName') {
            $options['context'] = 'Week short name';
          }
          $val = t($string, array(), $options);
        }
      }
    }
  }

  /**
   * Internal helper to get all active languages.
   *
   * @return
   *   An array of active languages.
   */
  static function calendar_systems_langauges() {

    // Default:
    $languages['default'] = array(
      'name' => 'Default',
    );

    // Check if there's any additional locale available:
    $locales = function_exists('locale_language_list') ? locale_language_list('name', TRUE) : NULL;

    // If found, append 'em:
    if (!is_null($locales)) {
      foreach ($locales as $name => $title) {
        $languages[$name] = array(
          'name' => $title,
        );
      }
    }
    return $languages;
  }

  /**
   * Internal static cache helper to get all available profiles for using as form select element options.
   *
   * @return
   *   An array of available profiles.
   */
  static function calendar_systems_profiles_simple($default_options = FALSE) {
    static $cache;
    if (!$cache) {
      $calendar_systems = self::calendar_systems_plugins();
      $cache = array();
      if ($default_options !== FALSE) {
        $cache = $default_options;
      }

      // Build available calendar systems array:
      foreach ($calendar_systems as $id => $calendar_system) {
        $cache[$id] = $calendar_system['title'];
      }
    }
    return $cache;
  }

  /**
   * Internal helper which defines all available calendars manually.
   *
   * @return
   *   An array defined calendars.
   *
   * @todo
   *   Define a pluggable API, so other modules can hook the hell in.
   */
  static function calendar_systems_plugins() {
    static $result;
    if (!$result) {
      $result = array(
        'default' => array(
          'title' => t('Default'),
          'installed' => TRUE,
          'installed version' => 2,
        ),
      );
      $calendar_systems = self::calendar_systems_get_calendar_instance();
      $plugins = $calendar_systems
        ->getPlugins();
      foreach ($plugins as $plugin_name => $plugin_info) {
        $result[$plugin_name] = $plugin_info;
        $result[$plugin_name]['title'] = t(ucfirst($plugin_name));
        $result[$plugin_name]['installed'] = TRUE;
        $result[$plugin_name]['installed version'] = 2;
      }
    }
    return $result;
  }

  /**
   * Internal helper to check whether the required patch is applied or not.
   *
   * @return
   *   Boolean value.
   */
  static function calendar_systems_is_patch_applied($cache = TRUE) {
    static $result;
    if ($cache) {
      $cache_object = \Drupal::cache()
        ->get('calendar_systems_is_patch_applied');
      if (is_object($cache_object)) {
        $result = $cache_object->data;
      }
    }
    if (is_null($result)) {
      $content = file_get_contents(DRUPAL_ROOT . '/core/includes/common.inc');

      // Check against patch fingerprint:
      $very_old_patch_applied = strpos($content, 'foreach (module_implements(\'format_date\') AS $module) {') !== FALSE ? TRUE : FALSE;
      $old_patch_applied = strpos($content, 'foreach (module_implements(\'format_date_calendar_systems\') AS $module) {') !== FALSE ? TRUE : FALSE;
      $applied = strpos($content, 'drupal_alter(\'format_date\'') !== FALSE ? TRUE : FALSE;
      if ($applied) {
        $result = TRUE;
      }
      elseif ($old_patch_applied || $very_old_patch_applied) {
        $result = 'outdated';
      }
      else {
        $result = FALSE;
      }
      \Drupal::cache()
        ->set('calendar_systems_is_patch_applied', $result, time() + 60 * 60);
    }
    return $result;
  }
  static function shouldIgnoreCalendarSystemsDateConversion() {
    foreach (debug_backtrace() as $trace) {
      if (!isset($trace['file'])) {
        continue;
      }
      $isMetatagModuleCalling = strpos($trace['file'], '/metatag/') !== FALSE;
      $isRdfModuleCalling = strpos($trace['file'], '/rdf/') !== FALSE;
      if ($isMetatagModuleCalling || $isRdfModuleCalling) {
        return TRUE;
      }
    }
    return FALSE;
  }

}

Classes

Namesort descending Description
Helpers