function tracking_code_ajax_disable in Tracking Code 7
Page callback for AJAX enable/disable request on a codeblock.
Parameters
(string) $ajax: a string containing 'ajax' or 'nojs' to show origin of callback
(int) $tcid: the primary key of the tracking code to enable/disable
Return value
string a JSON object containing the status and label replacement
1 string reference to 'tracking_code_ajax_disable'
- tracking_code_menu in ./
tracking_code.module - Implements hook_menu().
File
- ./
tracking_code.admin.inc, line 343 - admin page callbacks and form handlers for the tracking code module
Code
function tracking_code_ajax_disable($ajax, $tcid) {
//Check for a proper token
if (empty($_GET['token']) || !drupal_valid_token($_GET['token'], 'disable_tc_snippet' . $tcid)) {
return MENU_ACCESS_DENIED;
}
$snippet = _tracking_code_read($tcid);
$status = $snippet['status'] ? 0 : 1;
$is_ajax = $ajax == 'ajax';
db_update('tracking_code')
->fields(array(
'status' => $status,
))
->condition('tcid', $tcid, '=')
->execute();
if ($is_ajax) {
$commands = array();
$enabled = $status ? t('Disable') : t('Enable');
//build the proper ajax commands
$commands[] = ajax_command_invoke('#tcid-' . $tcid . ' a.tracking-code-toggle-link', 'html', array(
$enabled,
));
if ($status) {
$commands[] = ajax_command_invoke('#tcid-' . $tcid, 'removeClass', array(
'tracking-code-disabled',
));
}
else {
$commands[] = ajax_command_invoke('#tcid-' . $tcid, 'addClass', array(
'tracking-code-disabled',
));
}
//return the commands
return array(
'#type' => 'ajax',
'#commands' => $commands,
);
}
else {
drupal_set_message(t('Disabled snippet: @name', array(
'@name' => $snippet['name'],
)));
drupal_goto();
}
}