View source
<?php
define('ITOGGLE_PATH', 'sites/all/libraries/itoggle');
define('ITOGGLE_MIN_PLUGIN_VERSION', '1.0');
module_load_include('inc', 'itoggle', 'includes/itoggle');
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'),
),
);
}
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.');
}
}
function itoggle_menu() {
$items = array();
$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,
);
$items['js/itoggle'] = array(
'page callback' => 'itoggle_ajax_callback',
'access callback' => 'user_access',
'access arguments' => array(
'use itoggle',
),
'type' => MENU_CALLBACK,
);
return $items;
}
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);
}
}
}
drupal_json_output(array(
'ok' => $ok,
));
exit;
}
function _itoggle_ajax_toggle_field($type, $property, $id, $value, $delta = 0) {
$access = FALSE;
$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;
}
function _itoggle_ajax_toggle_entity($type, $property, $id, $value) {
$access = FALSE;
if (FALSE) {
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;
}
return TRUE;
}
}
return FALSE;
}
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;
}
function itoggle_theme($existing, $type, $theme, $path) {
return array(
'itoggle' => array(
'variables' => array(
'type' => NULL,
'id' => NULL,
'property' => NULL,
'checked' => NULL,
'scope' => NULL,
),
),
);
}
function theme_itoggle($variables) {
extract($variables);
if ($checked === TRUE) {
$checked = 'checked="checked"';
$value = 0;
}
else {
$checked = '';
$value = 1;
}
$token_key = "itoggle_{$type}_{$property}_{$id}_{$value}";
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;
}