View source
<?php
function _token_eval($code, $type, $object = null) {
${$type} = $object;
ob_start();
print eval($code);
$output = ob_get_contents();
ob_end_clean();
return $output;
}
function _token_custom_load($tkid) {
$row = db_fetch_object(db_query("SELECT * FROM {token_custom} WHERE tkid =%d", $tkid));
return $row;
}
function token_custom_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/build/tokens',
'title' => t('Tokens'),
'callback' => 'token_custom_page',
'access' => user_access('create PHP code for execution by Custom Tokens'),
);
$items[] = array(
'path' => 'admin/build/tokens/list',
'title' => t('List'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -1,
);
$items[] = array(
'path' => 'admin/build/tokens/create',
'callback' => 'token_custom_create_page',
'title' => t('Create'),
'type' => MENU_LOCAL_TASK,
);
$items[] = array(
'path' => 'admin/build/tokens/help',
'callback' => 'theme',
'callback arguments' => array(
'token_help',
),
'title' => t('Tokens'),
'type' => MENU_LOCAL_TASK,
);
$items[] = array(
'path' => 'admin/build/tokens/edit',
'callback' => 'token_custom_edit_page',
'title' => t('Edit'),
'type' => MENU_CALLBACK,
);
$items[] = array(
'path' => 'admin/build/tokens/delete',
'callback' => 'token_custom_delete_page',
'title' => t('Edit'),
'type' => MENU_CALLBACK,
);
}
return $items;
}
function token_custom_perm() {
return array(
'create PHP code for execution by Custom Tokens',
);
}
function token_custom_token_list($type = 'all') {
$result = db_query("SELECT * FROM {token_custom}");
while ($row = db_fetch_object($result)) {
if ($type == $row->type || $type == 'all') {
$tokens['token_custom'][$row->id] = $row->description;
}
}
return $tokens;
}
function token_custom_token_values($type, $object = NULL, $options = array()) {
$result = db_query("SELECT * FROM {token_custom} WHERE type = '%s'", $type);
while ($row = db_fetch_object($result)) {
switch ($row->type) {
case 'taxonomy':
case 'node':
case 'comment':
case 'user':
$tokens[$row->id] = _token_eval($row->php, $row->type, $object);
break;
case 'global':
$tokens[$row->id] = _token_eval($row->php, $row->type);
break;
}
}
return $tokens;
}
function token_custom_page() {
$result = db_query("SELECT * FROM {token_custom}");
$headers = array();
$headers[] = t('Token ID');
$headers[] = t('Description');
$headers[] = t('Type');
$headers[] = t('Demo (if available)');
$headers[] = '';
$rows = array();
while ($row = db_fetch_object($result)) {
$r = array();
$r[] = $row->id;
$r[] = $row->description;
$r[] = $row->type;
$text = "[{$row->id}]";
switch ($row->type) {
case 'user':
global $user;
$r[] = token_replace($text, $row->type, $user);
break;
default:
$r[] = token_replace($text, $row->type);
break;
}
$r[] = l('edit', 'admin/build/tokens/edit/' . $row->tkid);
$rows[] = $r;
}
$demo_notes = t('<p>Available demos and notes:<ul><li>Global</li><li>User: Using your current user</li></ul></p>');
return $demo_notes . theme('table', $headers, $rows);
}
function token_custom_create_page() {
return drupal_get_form('token_custom_edit_form');
}
function token_custom_edit_page($tkid) {
if ($_POST['op'] == t('Delete')) {
if ($_REQUEST['destination']) {
$destination = drupal_get_destination();
unset($_REQUEST['destination']);
}
drupal_goto('admin/build/tokens/delete/' . $tkid, $destination);
}
return drupal_get_form('token_custom_edit_form', $tkid);
}
function token_custom_edit_form_validate($form_id, $form_values) {
if (substr($form_values['token_custom_id'], 0, 13) != 'token_custom_') {
form_set_error('token_custom_id', t('Token ID must start with <b><i>token_custom_</i></b>'));
}
$args = array();
$args[] = $form_values['token_custom_id'];
if ($form_values['token_custom_tkid']) {
$where = ' AND tkid != %d ';
$args[] = $form_values['token_custom_tkid'];
}
$tkid = db_result(db_query("SELECT tkid FROM {token_custom} WHERE id = '%s' {$where}", $args));
if ($tkid) {
form_set_error('token_custom_id', t('Token ID already exists and must be unique, please change it'));
}
}
function token_custom_edit_form_submit($form_id, $form_values) {
if (!$form_values['token_custom_tkid']) {
$tkid = db_next_id('token_custom_tkid');
db_query("INSERT INTO {token_custom} (tkid, id, description, type, php) VALUES (%d, '%s', '%s', '%s', '%s')", $tkid, $form_values['token_custom_id'], $form_values['token_custom_description'], $form_values['token_custom_type'], $form_values['token_custom_php']);
}
else {
$tkid = $form_values['token_custom_tkid'];
db_query("UPDATE {token_custom} SET id = '%s', description = '%s', type = '%s', php = '%s' WHERE tkid = %d", $form_values['token_custom_id'], $form_values['token_custom_description'], $form_values['token_custom_type'], $form_values['token_custom_php'], $tkid);
}
drupal_set_message(t('%id saved.', array(
'%id' => $form_values['token_custom_id'],
)));
return 'admin/build/tokens';
}
function token_custom_edit_form($tkid = null) {
if ($tkid) {
$token = _token_custom_load($tkid);
$form['token_custom_tkid'] = array(
'#type' => 'value',
'#value' => $tkid,
);
}
$form['token_custom_id'] = array(
'#type' => 'textfield',
'#title' => t('Token ID'),
'#description' => t('Machine name of the token ID. It must start with token_custom_'),
'#default_value' => $tkid ? $token->id : 'token_custom_',
'#required' => TRUE,
);
$form['token_custom_description'] = array(
'#type' => 'textfield',
'#title' => t('Description'),
'#description' => t('Description that will appear in the token\'s help.'),
'#default_value' => $tkid ? $token->description : null,
'#required' => TRUE,
);
$options = array();
$options['global'] = t('Global');
$options['node'] = t('Node');
$options['user'] = t('User');
$options['taxonomy'] = t('Taxonomy');
$options['comment'] = t('Comment');
$form['token_custom_type'] = array(
'#type' => 'radios',
'#title' => t('Type'),
'#description' => t('Select the type of the token you would like to add. Depending on the type you\'ll have access to the specific object in your php code.'),
'#required' => TRUE,
'#options' => $options,
'#default_value' => $tkid ? $token->type : null,
);
$form['token_custom_php'] = array(
'#type' => 'textarea',
'#title' => t('PHP replacement'),
'#description' => t('Enter the php code that will be evaluated. You do not need to enclose the code between %php. You have $user, $comment, $node and $taxonomy available depending on the type. You have also the variable $type which the actual type of token. Global has no particular object to use. The code should return a string.', array(
'%php' => '<?php ?>',
)),
'#required' => TRUE,
'#default_value' => $tkid ? $token->php : null,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
if ($tkid) {
$form['delete'] = array(
'#type' => 'button',
'#value' => t('Delete'),
'#weight' => 50,
);
}
return $form;
}
function token_custom_delete_form_submit($form_id, $form_values) {
$token = _token_custom_load($form_values['token_custom_tkid']);
db_query("DELETE FROM {token_custom} WHERE tkid = %d", $form_values['token_custom_tkid']);
drupal_set_message(t('%id deleted.', array(
'%id' => $token->id,
)));
return 'admin/build/tokens';
}
function token_custom_delete_form($tkid) {
$token = _token_custom_load($tkid);
$form['token_custom_tkid'] = array(
'#type' => 'value',
'#value' => $tkid,
);
return confirm_form($form, t('Are you sure you want to delete %id?', array(
'%id' => $token->id,
)), isset($_GET['destination']) ? $_GET['destination'] : 'admin/build/tokens', t('This action cannot be undone.'), t('Delete'), t('Cancel'));
}
function token_custom_delete_page($tkid) {
return drupal_get_form('token_custom_delete_form', $tkid);
}