You are here

itoggle.module in iToggle 7

Same filename and directory in other branches
  1. 7.3 itoggle.module
  2. 7.2 itoggle.module

iToggle module.

File

itoggle.module
View source
<?php

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

/**
 * The default path to the iToggle directory.
 */
define('ITOGGLE_PATH', 'sites/all/libraries/itoggle');
define('ITOGGLE_MIN_PLUGIN_VERSION', '1.0');
module_load_include('inc', 'itoggle', 'includes/itoggle');

/**
 * Implements hook_permission().
 */
function itoggle_permission() {
  return array(
    'administer itoggle' => array(
      'title' => t('Administer iToggle'),
      'description' => t('Configure iToggle module'),
    ),
    'use itoggle' => array(
      'title' => t('Use itoggle'),
      'description' => t('Use iToggle module'),
    ),
  );
}

/**
 * Implements hook_help().
 */
function itoggle_help($path, $arg) {
  switch ($path) {
    case 'admin/config/itoggle':
      return t('This is the iToggle configuration page. Override iToggle default settings here. To include the iToggle plugin yourself, just call itoggle_include_itoggle() from your code.');
  }
}

/**
 * Implements hook_menu().
 */
function itoggle_menu() {
  $items = array();

  // admin settings
  $items['admin/config/itoggle'] = array(
    'title' => 'iToggle',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'itoggle_form_admin',
    ),
    'access arguments' => array(
      'administer itoggle',
    ),
    'file' => 'includes/itoggle.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );

  // itoggle js callback
  $items['js/itoggle'] = array(
    'page callback' => 'itoggle_ajax_callback',
    'access callback' => 'user_access',
    'access arguments' => array(
      'use itoggle',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Page callback
 * 
 * @see itoggle_menu().
 */
function itoggle_ajax_callback() {
  $token = filter_input(INPUT_POST, 'token');
  $type = filter_input(INPUT_POST, 'type');
  $property = filter_input(INPUT_POST, 'property');
  $id = filter_input(INPUT_POST, 'id');
  $value = filter_input(INPUT_POST, 'value');
  $scope = filter_input(INPUT_POST, 'scope');
  $token_key = "itoggle_{$type}_{$property}_{$id}_{$value}";
  if (!empty($token) && drupal_valid_token($token, $token_key)) {
    if ($scope === 'entity') {
      $ok = _itoggle_ajax_toggle_entity($type, $property, $id, $value);
    }
    else {
      if ($scope === 'field') {
        $ok = _itoggle_ajax_toggle_field($type, $property, $id, $value);
      }
    }
  }

  // let $ok be undefined so we know the difference between FALSE (error) and UNDEFINDED (invalid type)
  drupal_json_output(array(
    'ok' => $ok,
  ));
  exit;
}

/**
 * Helper function
 * Changes field values
 * 
 * @param string $type
 * @param string $property
 * @param int $id
 * @param int $value
 * @param int $delta
 */
function _itoggle_ajax_toggle_field($type, $property, $id, $value, $delta = 0) {

  // check permissions
  $access = FALSE;

  // @TODO check permissions
  $access = TRUE;
  if ($access) {
    $entity = current(entity_load($type, array(
      $id,
    )));
    $lang = $entity->language;
    $entity->{$property}[$lang][$delta] = $value;
    $return = entity_save($type, $entity);
    return $return !== FALSE;
  }
  return FALSE;
}

/**
 * Helper function
 * Changes entity properties
 * 
 * @param string $type
 * @param string $property
 * @param int $id
 * @param int $value
 */
function _itoggle_ajax_toggle_entity($type, $property, $id, $value) {

  // check permissions
  $access = FALSE;
  if (FALSE) {

    // @TODO check permissions
    // @TODO if node, check if override_node_options exists
    if (module_exists('override_node_options')) {
      $node = node_load($id);
      $type = $node->type;
      $access = user_access("override {$type} published option");
    }
  }
  else {
    $access = TRUE;
  }
  if ($access === TRUE) {
    $entity = entity_load($type, array(
      $id,
    ));
    if ($entity) {
      if ($entity->{$property} != $value) {
        $entity->{$property} = $value;
        $return = entity_save($type, $entity);
        return $return !== FALSE;
      }

      // if the value is already set just return TRUE
      return TRUE;
    }
  }
  return FALSE;
}

/**
 * Implements hook_variable_info().
 */
function itoggle_variable_info() {
  $variables = array();
  $variables['itoggle_path'] = array(
    'title' => t('iToggle Path'),
    'default' => ITOGGLE_PATH,
    'group' => 'itoggle',
    'token' => FALSE,
  );
  $variables['itoggle_compression_type'] = array(
    'title' => t('iToggle Compression Type'),
    'default' => 'min',
    'group' => 'itoggle',
    'token' => FALSE,
  );
  $variables['itoggle_css'] = array(
    'title' => t('Include Default CSS'),
    'default' => TRUE,
    'group' => 'itoggle',
    'token' => FALSE,
  );
  $variables['itoggle_easing'] = array(
    'title' => t('Easing'),
    'default' => '',
    'group' => 'itoggle',
    'token' => FALSE,
  );
  $variables['itoggle_speed'] = array(
    'title' => t('Speed'),
    'default' => 200,
    'group' => 'itoggle',
    'token' => FALSE,
  );
  $variables['itoggle_onclick'] = array(
    'title' => t('OnClick callback'),
    'default' => '',
    'group' => 'itoggle',
    'token' => FALSE,
  );
  $variables['itoggle_onclickon'] = array(
    'title' => t('OnClickOn callback'),
    'default' => '',
    'group' => 'itoggle',
    'token' => FALSE,
  );
  $variables['itoggle_onclickoff'] = array(
    'title' => t('OnClickOff callback'),
    'default' => '',
    'group' => 'itoggle',
    'token' => FALSE,
  );
  $variables['itoggle_onslide'] = array(
    'title' => t('OnSlide callback'),
    'default' => '',
    'group' => 'itoggle',
    'token' => FALSE,
  );
  $variables['itoggle_onslideon'] = array(
    'title' => t('OnSlideOn callback'),
    'default' => '',
    'group' => 'itoggle',
    'token' => FALSE,
  );
  $variables['itoggle_onslideoff'] = array(
    'title' => t('OnSlideOff callback'),
    'default' => '',
    'group' => 'itoggle',
    'token' => FALSE,
  );
  return $variables;
}

/**
 * Implements hook_theme().
 */
function itoggle_theme($existing, $type, $theme, $path) {
  return array(
    'itoggle' => array(
      'variables' => array(
        'type' => NULL,
        'id' => NULL,
        'property' => NULL,
        'checked' => NULL,
        'scope' => NULL,
      ),
    ),
  );
}

/**
 * Theme callback
 * 
 * @param array $variables
 *  An array with the folowing keys/values:
 *    - string $type
 *        The entity type
 *    - int $id
 *        The property id
 *    - string $property
 *        The property machine-name
 *    - bool $checked
 *        The property value (true/false)
 *    - string $scope
 *        The widget scope (views/field/inline)
 * @see itoggle_theme().
 */
function theme_itoggle($variables) {
  extract($variables);
  if ($checked === TRUE) {
    $checked = 'checked="checked"';

    // set inverse values because the token gets validated against the submitted value
    $value = 0;
  }
  else {
    $checked = '';

    // set inverse values because the token gets validated against the submitted value
    $value = 1;
  }
  $token_key = "itoggle_{$type}_{$property}_{$id}_{$value}";

  // @TODO find out whether there's a better place to do this
  // add an individual token for each combination
  drupal_add_js(array(
    'itoggle' => array(
      'tokens' => array(
        "itoggle_{$type}_{$property}_{$id}_{$value}" => drupal_get_token($token_key),
      ),
    ),
  ), 'setting');
  $element_id = "itoggle_{$type}_{$property}_{$id}";
  return <<<HTML
<div class="itoggle-wrapper itoggle-{<span class="php-variable">$type</span>} itoggle-{<span class="php-variable">$type</span>}_{<span class="php-variable">$property</span>}" data-id="{<span class="php-variable">$id</span>}" data-type="{<span class="php-variable">$type</span>}" data-scope="{<span class="php-variable">$scope</span>}" data-property="{<span class="php-variable">$property</span>}">
  <input type="checkbox" id="{<span class="php-variable">$element_id</span>}" {<span class="php-variable">$checked</span>} />
</div>
HTML;
}

/**
 * Implements hook_library().
 * @TODO do we need this?
 */

//function itoggle_library() {

//  $libraries = array();
//
//  $path = drupal_get_path('module', 'itoggle');
//  $libraries['itoggle'] = array(
//    'title' => 'iToggle',
//    'website' => '',
//    'version' => '1.0',
//    'js' => array(
//      $path . '/engage.itoggle.js' => array(),
//      $path . '/engage.itoggle-min.js' => array(),
//    ),
//    'css' => array(
//      $path . '/themes/jquery.timeentry.css' => array('preprocess' => FALSE),
//    ),
//  );
//  return $libraries;

//}

Functions

Namesort descending Description
itoggle_ajax_callback Page callback
itoggle_help Implements hook_help().
itoggle_menu Implements hook_menu().
itoggle_permission Implements hook_permission().
itoggle_theme Implements hook_theme().
itoggle_variable_info Implements hook_variable_info().
theme_itoggle Theme callback
_itoggle_ajax_toggle_entity Helper function Changes entity properties
_itoggle_ajax_toggle_field Helper function Changes field values

Constants

Namesort descending Description
ITOGGLE_MIN_PLUGIN_VERSION
ITOGGLE_PATH The default path to the iToggle directory.