You are here

itoggle.inc in iToggle 7

iToggle module include.

File

includes/itoggle.inc
View source
<?php

/**
 * @file
 * iToggle module include.
 */

/**
 * Helper function
 * Includes itoggle plugin and default style
 */
function itoggle_include_itoggle() {
  if (variable_get_value('itoggle_css')) {
    drupal_add_css(drupal_get_path('module', 'itoggle') . '/css/itoggle.css');
  }
  drupal_add_js(itoggle_get_js());
}

/**
 * Helper function
 * Adds itoggle settings to Drupal.settings
 */
function itoggle_include_settings() {
  drupal_add_js(array(
    'itoggle' => array(
      'easing' => variable_get_value('itoggle_easing'),
      'speed' => variable_get_value('itoggle_speed'),
      'onclick' => variable_get_value('itoggle_onclick'),
      'onclickon' => variable_get_value('itoggle_onclickon'),
      'onclickoff' => variable_get_value('itoggle_onclickoff'),
      'onslide' => variable_get_value('itoggle_onslide'),
      'onslideon' => variable_get_value('itoggle_onslideon'),
      'onslideoff' => variable_get_value('itoggle_onslideoff'),
      'tokens' => array(),
    ),
  ), 'setting');
}

/**
 * Return the JS filename for iToggle plugin.
 * This was borrowed from the Colorbox module
 * 
 * @return
 *   String the JS filename for iToggle plugin
 * @link http://drupal.org/project/colorbox
 */
function itoggle_get_js() {
  $library_path = itoggle_get_path();
  if (file_exists($library_path . '/engage.itoggle.js') && file_exists($library_path . '/engage.itoggle-min.js')) {
    $itoggle_js_map = array(
      'none' => 'engage.itoggle.js',
      'min' => 'engage.itoggle-min.js',
    );
    $itoggle_js = $itoggle_js_map[variable_get_value('itoggle_compression_type')];
    return "{$library_path}/{$itoggle_js}";
  }
}

/**
 * Return the path to the iToggle plugin.
 * This was borrowed from the Colorbox module
 * 
 * @link http://drupal.org/project/colorbox
 */
function itoggle_get_path() {
  static $library_path = NULL;

  // Try to locate the library path in any possible setup.
  if ($library_path == NULL) {

    // First check the default location.
    $path = variable_get_value('itoggle_path');
    if (is_dir($path . '/itoggle')) {
      $library_path = $path;
    }
    elseif ($library_path == NULL && module_exists('libraries')) {
      if ($path = libraries_get_path('itoggle')) {
        $library_path = $path;
        variable_set('itoggle_path', $library_path);
      }
    }
    elseif ($library_path == NULL && file_exists(dirname(__FILE__) . '/../libraries/libraries.module')) {
      require_once dirname(__FILE__) . '/../libraries/libraries.module';
      if ($path = libraries_get_path('itoggle')) {
        $library_path = $path;
        variable_set('itoggle_path', $library_path);
      }
    }
    elseif ($library_path == NULL) {
      $library_path = ITOGGLE_PATH;
    }
  }
  return $library_path;
}

/**
 * Return the version of iToggle plugin that is installed.
 *
 * This can be used by other modules' hook_requirements() to ensure that the
 * proper version of iToggle plugin is installed.
 * 
 * This was borrowed from the Colorbox module
 * 
 * @see version_compare()
 * @link http://drupal.org/project/colorbox
 */
function itoggle_get_version($itoggle_js = NULL) {
  $version = 0;
  $pattern = '#Version: ([0-9\\.a-z]+)#';

  // No file is passed in so use the default location.
  if (is_null($itoggle_js)) {
    $itoggle_js = itoggle_get_js();
  }

  // Return the version of iToggle plugin, it it exists.
  if (file_exists($itoggle_js)) {
    $itoggle_plugin = file_get_contents($itoggle_js, NULL, NULL, 0, 256);
    if (preg_match($pattern, $itoggle_plugin, $matches)) {
      $version = $matches[1];
    }
  }
  return $version;
}

/**
 * Loads information about defined entities and returns an array of possible boolean fields for toggling
 * 
 * @return array
 */
function itoggle_get_entity_info() {

  // Use the advanced drupal_static() pattern, since this is called very often.
  static $drupal_static_fast;
  if (!isset($drupal_static_fast)) {
    $drupal_static_fast['itoggle_get_entity_info'] =& drupal_static(__FUNCTION__);
  }
  $allowed =& $drupal_static_fast['itoggle_get_entity_info'];
  if (!is_array($allowed)) {
    $cache = cache_get('itoggle_get_entity_info');
    if (isset($cache->data)) {
      $allowed = $cache->data;
    }
    else {
      $entities = entity_get_info();
      $allowed = array();
      foreach ($entities as $key => $info) {
        $keys = array();
        $schema = drupal_get_schema($info['base table']);
        foreach ($schema['fields'] as $cid => $val) {
          if ($val['type'] === 'int') {

            // avoid fields we know aren't boolean
            $avoid = array(
              'created',
              'changed',
              'filesize',
              'timestamp',
              'translate',
              'weight',
            );
            if ($key === 'user') {
              $avoid[] = 'access';
              $avoid[] = 'login';
              $avoid[] = 'picture';
            }
            else {
              if ($key === 'taxonomy_vocabulary') {
                $avoid[] = 'hierarchy';
              }
            }
            if (!in_array($cid, $avoid)) {

              // avoid keys ending in 'id'
              $pos = strpos($cid, 'id');
              $len = strlen($cid);
              if ($pos === FALSE || $len - $pos > 2) {
                $keys[] = $cid;
              }
            }
          }
        }
        if (!empty($keys)) {
          $allowed[$key] = array(
            'properties' => $keys,
            'base table' => $info['base table'],
            'entity keys' => $info['entity keys'],
          );
        }
      }

      // ok we have allowed entities
      // we still need to know for sure whether they are boolean or not
      // @TODO more entity introspection?
      cache_set('itoggle_get_entity_info', $allowed, 'cache', CACHE_TEMPORARY);
    }
  }
  return $allowed;
}

Functions

Namesort descending Description
itoggle_get_entity_info Loads information about defined entities and returns an array of possible boolean fields for toggling
itoggle_get_js Return the JS filename for iToggle plugin. This was borrowed from the Colorbox module
itoggle_get_path Return the path to the iToggle plugin. This was borrowed from the Colorbox module
itoggle_get_version Return the version of iToggle plugin that is installed.
itoggle_include_itoggle Helper function Includes itoggle plugin and default style
itoggle_include_settings Helper function Adds itoggle settings to Drupal.settings