| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Page callback for Hooker's main admin page.
|
| 6 |
*/
|
| 7 |
function hooker_admin_form() {
|
| 8 |
$path = drupal_get_path('module', 'hooker') .'/';
|
| 9 |
$form = array();
|
| 10 |
$form['#tree'] = TRUE;
|
| 11 |
foreach (_hooker_hooks(TRUE) as $hook => $data) {
|
| 12 |
$form['hooks'][$hook]['edit'] = array(
|
| 13 |
'#type' => 'image_button',
|
| 14 |
'#title' => t('Edit hook'),
|
| 15 |
'#src' => $path .'text-editor.png',
|
| 16 |
'#submit' => array('hooker_admin_edit_button'),
|
| 17 |
'#hook' => $hook,
|
| 18 |
);
|
| 19 |
$form['hooks'][$hook]['delete'] = array(
|
| 20 |
'#type' => 'image_button',
|
| 21 |
'#title' => t('Delete hook'),
|
| 22 |
'#src' => $path .'edit-delete.png',
|
| 23 |
'#submit' => array('hooker_admin_delete_button'),
|
| 24 |
'#hook' => $hook,
|
| 25 |
);
|
| 26 |
}
|
| 27 |
return $form;
|
| 28 |
}
|
| 29 |
|
| 30 |
/**
|
| 31 |
* Edit button callback for the hook listing form.
|
| 32 |
*/
|
| 33 |
function hooker_admin_edit_button($form, &$form_state) {
|
| 34 |
$button = $form_state['clicked_button'];
|
| 35 |
$hook = $button['#hook'];
|
| 36 |
$form_state['redirect'] = 'admin/settings/hooker/edit/'. $hook;
|
| 37 |
}
|
| 38 |
|
| 39 |
/**
|
| 40 |
* Delete button callback for the hook listing form.
|
| 41 |
* Redirects the user to the confirmation form.
|
| 42 |
*/
|
| 43 |
function hooker_admin_delete_button($form, &$form_state) {
|
| 44 |
$button = $form_state['clicked_button'];
|
| 45 |
$hook = $button['#hook'];
|
| 46 |
$form_state['redirect'] = 'admin/settings/hooker/delete/'. $hook;
|
| 47 |
}
|
| 48 |
|
| 49 |
/**
|
| 50 |
* Theme function for the Hooker admin overview form.
|
| 51 |
*/
|
| 52 |
function theme_hooker_admin_form(&$form) {
|
| 53 |
$rows = array();
|
| 54 |
if (!empty($form['hooks'])) {
|
| 55 |
foreach (element_children($form['hooks']) as $hook) {
|
| 56 |
$row = array();
|
| 57 |
$row[] = check_plain('hook_'. $hook);
|
| 58 |
$row[] = l(t('API documentation'), 'http://api.drupal.org/api/function/hook_'. $hook, array('absolute' => TRUE));
|
| 59 |
$row[] = drupal_render($form['hooks'][$hook]);
|
| 60 |
$rows[] = $row;
|
| 61 |
}
|
| 62 |
}
|
| 63 |
|
| 64 |
$link = l(t('Click here'), 'admin/settings/hooker/add');
|
| 65 |
$row = array();
|
| 66 |
if (empty($rows)) {
|
| 67 |
$row[] = array(
|
| 68 |
'data' => t('No hooks have been added. !url to create one.', array('!url' => $link)),
|
| 69 |
'colspan' => 3,
|
| 70 |
);
|
| 71 |
}
|
| 72 |
else {
|
| 73 |
$row[] = array(
|
| 74 |
'data' => t('!url to add a new hook implementation.', array('!url' => $link)),
|
| 75 |
'colspan' => 3,
|
| 76 |
);
|
| 77 |
}
|
| 78 |
$rows[] = $row;
|
| 79 |
|
| 80 |
$link = l('api.drupal.org', 'http://api.drupal.org/api/group/hooks', array('absolute' => TRUE));
|
| 81 |
$output = '<p>'. t("Use Hooker to execute snippets of PHP when Drupal hooks are triggered. See !url for more information about Drupal hooks.", array('!url' => $link)) .'</p>';
|
| 82 |
$output .= theme('table', array(), $rows);
|
| 83 |
$output .= drupal_render($form);
|
| 84 |
return $output;
|
| 85 |
}
|
| 86 |
|
| 87 |
/**
|
| 88 |
* Constructor for the hook edit form.
|
| 89 |
*/
|
| 90 |
function hooker_edit($form_state, $hookname = NULL) {
|
| 91 |
$hooks = _hooker_hooks(TRUE);
|
| 92 |
if (isset($hookname)) {
|
| 93 |
if (!empty($hooks[$hookname])) {
|
| 94 |
$hook = $hooks[$hookname];
|
| 95 |
$form['hook'] = array(
|
| 96 |
'#type' => 'value',
|
| 97 |
'#value' => $hookname,
|
| 98 |
);
|
| 99 |
$link = l('hook_'. $hookname, 'http://api.drupal.org/api/group/hooks', array('absolute' => TRUE));
|
| 100 |
$form['hookname'] = array(
|
| 101 |
'#type' => 'item',
|
| 102 |
'#title' => t('Hook'),
|
| 103 |
'#value' => $link,
|
| 104 |
);
|
| 105 |
}
|
| 106 |
else {
|
| 107 |
$form['hook'] = array(
|
| 108 |
'#type' => 'textfield',
|
| 109 |
'#title' => t('Hook'),
|
| 110 |
'#default_value' => $hookname,
|
| 111 |
'#required' => TRUE,
|
| 112 |
);
|
| 113 |
$hook = new stdClass();
|
| 114 |
$hook->hook = $hookname;
|
| 115 |
$hook->params = '';
|
| 116 |
$hook->code = '';
|
| 117 |
}
|
| 118 |
}
|
| 119 |
else {
|
| 120 |
$form['hook'] = array(
|
| 121 |
'#type' => 'textfield',
|
| 122 |
'#title' => t('Hook name'),
|
| 123 |
'#default_value' => '',
|
| 124 |
'#required' => TRUE,
|
| 125 |
);
|
| 126 |
$form['new_hook'] = array(
|
| 127 |
'#type' => 'value',
|
| 128 |
'#value' => TRUE,
|
| 129 |
);
|
| 130 |
$hook = new stdClass();
|
| 131 |
$hook->hook = '';
|
| 132 |
$hook->params = '';
|
| 133 |
$hook->code = '';
|
| 134 |
}
|
| 135 |
|
| 136 |
$form['params'] = array(
|
| 137 |
'#type' => 'textfield',
|
| 138 |
'#title' => t('Function parameters'),
|
| 139 |
'#default_value' => $hook->params,
|
| 140 |
'#description' => t('If the hoook in question requires parameters, they must be entered here just as they would appear in a normal module implementation.'),
|
| 141 |
'#required' => FALSE,
|
| 142 |
);
|
| 143 |
|
| 144 |
$form['code'] = array(
|
| 145 |
'#type' => 'textarea',
|
| 146 |
'#title' => t('PHP code'),
|
| 147 |
'#rows' => 20,
|
| 148 |
'#default_value' => $hook->code,
|
| 149 |
'#required' => TRUE,
|
| 150 |
);
|
| 151 |
|
| 152 |
$form['buttons']['save'] = array(
|
| 153 |
'#type' => 'submit',
|
| 154 |
'#value' => t('Save'),
|
| 155 |
'#submit' => array('hooker_edit_save'),
|
| 156 |
);
|
| 157 |
|
| 158 |
if (!empty($hooks[$hookname])) {
|
| 159 |
$form['buttons']['delete'] = array(
|
| 160 |
'#type' => 'submit',
|
| 161 |
'#value' => t('Delete'),
|
| 162 |
'#submit' => array('hooker_admin_delete_button'),
|
| 163 |
'#hook' => $hookname,
|
| 164 |
);
|
| 165 |
}
|
| 166 |
|
| 167 |
return $form;
|
| 168 |
}
|
| 169 |
|
| 170 |
/**
|
| 171 |
* Validation callback for the hook edit form.
|
| 172 |
*/
|
| 173 |
function hooker_edit_validate($form, &$form_state) {
|
| 174 |
$values = $form_state['values'];
|
| 175 |
$hooks = _hooker_hooks();
|
| 176 |
if (!empty($values['new_hook'])) {
|
| 177 |
if (isset($hooks[$values['hook']])) {
|
| 178 |
form_set_error('hook', t('The hook %hookname is already defined.', array('%hookname' => $values['hook'])));
|
| 179 |
}
|
| 180 |
}
|
| 181 |
}
|
| 182 |
|
| 183 |
/**
|
| 184 |
* Submit button callback for the hook edit form.
|
| 185 |
*/
|
| 186 |
function hooker_edit_save($form, &$form_state) {
|
| 187 |
if (empty($form_state['values']['new_hook'])) {
|
| 188 |
drupal_write_record('hooker', $form_state['values'], array('hook'));
|
| 189 |
}
|
| 190 |
else {
|
| 191 |
drupal_write_record('hooker', $form_state['values']);
|
| 192 |
}
|
| 193 |
|
| 194 |
drupal_set_message('Your hook was saved.');
|
| 195 |
$form_state['redirect'] = 'admin/settings/hooker';
|
| 196 |
_hooker_hooks(TRUE);
|
| 197 |
}
|
| 198 |
|
| 199 |
|
| 200 |
/**
|
| 201 |
* Menu callback -- ask for confirmation of rule deletion
|
| 202 |
*/
|
| 203 |
function hooker_delete_confirm(&$form_state, $hook) {
|
| 204 |
$form['hook'] = array(
|
| 205 |
'#type' => 'value',
|
| 206 |
'#value' => $hook,
|
| 207 |
);
|
| 208 |
|
| 209 |
return confirm_form($form,
|
| 210 |
t('Are you sure you want to delete %title?', array('%title' => 'hook_'. $hook)),
|
| 211 |
isset($_GET['destination']) ? $_GET['destination'] : 'admin/settings/hooker',
|
| 212 |
t('This action cannot be undone.'),
|
| 213 |
t('Delete'),
|
| 214 |
t('Cancel')
|
| 215 |
);
|
| 216 |
}
|
| 217 |
|
| 218 |
/**
|
| 219 |
* Execute node deletion
|
| 220 |
*/
|
| 221 |
function hooker_delete_confirm_submit($form, &$form_state) {
|
| 222 |
if ($form_state['values']['confirm']) {
|
| 223 |
db_query("DELETE FROM {hooker} WHERE hook = '%s'", $form_state['values']['hook']);
|
| 224 |
}
|
| 225 |
$form_state['redirect'] = 'admin/settings/hooker';
|
| 226 |
_hooker_hooks(TRUE);
|
| 227 |
return;
|
| 228 |
}
|