| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Conditional actions overview UI.
|
| 7 |
*/
|
| 8 |
|
| 9 |
// Displays the administration page that lets you add and modify predicates.
|
| 10 |
function ca_admin() {
|
| 11 |
// Load all the module defined predicates into a temporary array.
|
| 12 |
$temp = module_invoke_all('ca_predicate');
|
| 13 |
|
| 14 |
// Assign a default weight if need be.
|
| 15 |
foreach ($temp as $key => $value) {
|
| 16 |
$temp[$key]['#pid'] = $key;
|
| 17 |
if (!isset($value['#weight'])) {
|
| 18 |
$temp[$key]['#weight'] = 0;
|
| 19 |
}
|
| 20 |
}
|
| 21 |
|
| 22 |
// Load and loop through all the database stored predicates.
|
| 23 |
$result = db_query("SELECT * FROM {ca_predicates}");
|
| 24 |
while ($predicate = db_fetch_array($result)) {
|
| 25 |
// Prepare the database data into a predicate.
|
| 26 |
$predicate = ca_prepare_db_predicate($predicate);
|
| 27 |
|
| 28 |
// Overrides the module defined predicate if it's been modified by a user.
|
| 29 |
$temp[$predicate['#pid']] = $predicate;
|
| 30 |
}
|
| 31 |
|
| 32 |
usort($temp, 'ca_weight_sort');
|
| 33 |
|
| 34 |
// Copy the temporary array of predicates into a keyed array.
|
| 35 |
$predicates = array();
|
| 36 |
foreach ($temp as $predicate) {
|
| 37 |
$predicates[$predicate['#pid']] = $predicate;
|
| 38 |
}
|
| 39 |
|
| 40 |
// Load up the trigger data so we can display them by title.
|
| 41 |
$triggers = module_invoke_all('ca_trigger');
|
| 42 |
|
| 43 |
// Build the header for the predicate tables.
|
| 44 |
$header = array(t('Title'), t('Class'), t('Status'), t('Weight'), t('Operations'));
|
| 45 |
|
| 46 |
$rows = array();
|
| 47 |
|
| 48 |
foreach ($predicates as $key => $value) {
|
| 49 |
// Build the basic operation links for each predicate.
|
| 50 |
$ops = array(
|
| 51 |
l(t('edit'), 'admin/settings/ca/'. $key .'/edit'),
|
| 52 |
);
|
| 53 |
|
| 54 |
// Add the reset link if a module defined predicate has been modified.
|
| 55 |
if (!is_numeric($key) && isset($value['#modified'])) {
|
| 56 |
$ops[] = l(t('reset'), 'admin/settings/ca/'. $key .'/reset');
|
| 57 |
}
|
| 58 |
|
| 59 |
// Add a delete link if displaying a custom predicate.
|
| 60 |
if (is_numeric($key)) {
|
| 61 |
$ops[] = l(t('delete'), 'admin/settings/ca/'. $key .'/delete');
|
| 62 |
}
|
| 63 |
|
| 64 |
// Add the predicate's row to the table.
|
| 65 |
$tables[$triggers[$value['#trigger']]['#title']]['rows'][] = array(
|
| 66 |
check_plain($value['#title']),
|
| 67 |
strpos($value['#class'], 'custom') === 0 ? check_plain(substr($value['#class'], 7)) : $value['#class'],
|
| 68 |
$value['#status'] == 0 ? t('Disabled') : t('Enabled'),
|
| 69 |
$value['#weight'],
|
| 70 |
implode(' ', $ops),
|
| 71 |
);
|
| 72 |
}
|
| 73 |
|
| 74 |
// Add the tables for each trigger to the output.
|
| 75 |
foreach ($tables as $key => $value) {
|
| 76 |
$output .= theme('table', $header, $value['rows'], array(), '<strong>'. t('Trigger') .':</strong> '. check_plain($key));
|
| 77 |
}
|
| 78 |
|
| 79 |
$output .= l(t('Add a predicate'), 'admin/settings/ca/add');
|
| 80 |
|
| 81 |
return $output;
|
| 82 |
}
|
| 83 |
|
| 84 |
// Form to allow the creation and editing of conditional action predicates.
|
| 85 |
function ca_predicate_meta_form($form_state, $pid) {
|
| 86 |
// Load the predicate if an ID is passed in.
|
| 87 |
if (!empty($pid) && $pid !== 0) {
|
| 88 |
$predicate = ca_load_predicate($pid);
|
| 89 |
|
| 90 |
// Fail out if we didn't find a predicate for that ID.
|
| 91 |
if (empty($predicate)) {
|
| 92 |
drupal_set_message(t('That predicate does not exist.'), 'error');
|
| 93 |
drupal_goto('admin/settings/ca');
|
| 94 |
}
|
| 95 |
}
|
| 96 |
|
| 97 |
$form['predicate_pid'] = array(
|
| 98 |
'#type' => 'value',
|
| 99 |
'#value' => $pid,
|
| 100 |
);
|
| 101 |
|
| 102 |
$form['predicate_title'] = array(
|
| 103 |
'#type' => 'textfield',
|
| 104 |
'#title' => t('Title'),
|
| 105 |
'#description' => t('Enter a title used for display on the overview tables.'),
|
| 106 |
'#default_value' => $pid ? $predicate['#title'] : '',
|
| 107 |
'#required' => TRUE,
|
| 108 |
);
|
| 109 |
|
| 110 |
// Create an array of triggers for the select box.
|
| 111 |
$triggers = module_invoke_all('ca_trigger');
|
| 112 |
foreach ($triggers as $key => $value) {
|
| 113 |
$options[$value['#category']][$key] = $value['#title'];
|
| 114 |
}
|
| 115 |
|
| 116 |
$form['predicate_trigger'] = array(
|
| 117 |
'#type' => 'select',
|
| 118 |
'#title' => t('Trigger'),
|
| 119 |
'#description' => t('Select the trigger for this predicate.<br />Cannot be modified if the predicate has conditions or actions.'),
|
| 120 |
'#options' => $options,
|
| 121 |
'#default_value' => $pid ? $predicate['#trigger'] : '',
|
| 122 |
'#disabled' => empty($predicate['#conditions']) && empty($predicate['#actions']) ? FALSE : TRUE,
|
| 123 |
'#required' => TRUE,
|
| 124 |
);
|
| 125 |
|
| 126 |
$form['predicate_description'] = array(
|
| 127 |
'#type' => 'textarea',
|
| 128 |
'#title' => t('Description'),
|
| 129 |
'#description' => t('Enter a description that summarizes the use and intent of the predicate.'),
|
| 130 |
'#default_value' => $pid ? $predicate['#description'] : '',
|
| 131 |
);
|
| 132 |
|
| 133 |
// Accommodate the mandatory custom prefix for predicate classes.
|
| 134 |
$class = $predicate['#class'];
|
| 135 |
if (strpos($class, 'custom') === 0) {
|
| 136 |
$class = ltrim(substr($class, 6), ':');
|
| 137 |
}
|
| 138 |
|
| 139 |
$form['predicate_class'] = array(
|
| 140 |
'#type' => 'textfield',
|
| 141 |
'#title' => t('Class'),
|
| 142 |
'#description' => t('Classes let you categorize your predicates based on the type of functionality they provide.<br />Cannot be modified if defined by a module.'),
|
| 143 |
'#field_prefix' => is_numeric($pid) ? 'custom:' : '',
|
| 144 |
'#default_value' => $class,
|
| 145 |
'#disabled' => !is_numeric($pid) ? TRUE : FALSE,
|
| 146 |
);
|
| 147 |
|
| 148 |
$form['predicate_status'] = array(
|
| 149 |
'#type' => 'radios',
|
| 150 |
'#title' => t('Status'),
|
| 151 |
'#description' => t('Disabled predicates will not be processed when their trigger is pulled.'),
|
| 152 |
'#options' => array(
|
| 153 |
1 => t('Enabled'),
|
| 154 |
0 => t('Disabled'),
|
| 155 |
),
|
| 156 |
'#default_value' => $pid ? $predicate['#status'] : 1,
|
| 157 |
);
|
| 158 |
|
| 159 |
$form['predicate_weight'] = array(
|
| 160 |
'#type' => 'weight',
|
| 161 |
'#title' => t('Weight'),
|
| 162 |
'#description' => t('Predicates will be sorted by weight and processed sequentially.'),
|
| 163 |
'#default_value' => ($pid && isset($predicate['#weight'])) ? $predicate['#weight'] : 0,
|
| 164 |
);
|
| 165 |
|
| 166 |
$form['submit'] = array(
|
| 167 |
'#type' => 'submit',
|
| 168 |
'#value' => t('Save predicate'),
|
| 169 |
'#suffix' => l(t('Cancel'), 'admin/settings/ca'),
|
| 170 |
);
|
| 171 |
|
| 172 |
return $form;
|
| 173 |
}
|
| 174 |
|
| 175 |
function ca_predicate_meta_form_submit($form, &$form_state) {
|
| 176 |
$save = FALSE;
|
| 177 |
|
| 178 |
// Load the original predicate.
|
| 179 |
if ($form_state['values']['pid'] !== 0) {
|
| 180 |
$predicate = ca_load_predicate($form_state['values']['predicate_pid']);
|
| 181 |
$predicate['#pid'] = $form_state['values']['predicate_pid'];
|
| 182 |
}
|
| 183 |
|
| 184 |
// Setup a list of fields to check for and apply changes.
|
| 185 |
$fields = array('title', 'trigger', 'description', 'class', 'status', 'weight');
|
| 186 |
|
| 187 |
// Compare the values from the form submission with what is already set.
|
| 188 |
foreach ($fields as $field) {
|
| 189 |
if ($form_state['values']['predicate_'. $field] != $predicate['#'. $field]) {
|
| 190 |
$predicate['#'. $field] = $form_state['values']['predicate_'. $field];
|
| 191 |
$save = TRUE;
|
| 192 |
}
|
| 193 |
}
|
| 194 |
|
| 195 |
// Add empty conditions and actions arrays if this is a new predicate.
|
| 196 |
if (empty($predicate['#pid'])) {
|
| 197 |
$predicate['#pid'] = db_next_id('{ca_predicates}_pid');
|
| 198 |
$predicate['#conditions'] = array();
|
| 199 |
$predicate['#actions'] = array();
|
| 200 |
}
|
| 201 |
|
| 202 |
// Check to see if any changes were made and save if necessary.
|
| 203 |
if ($save) {
|
| 204 |
ca_save_predicate($predicate);
|
| 205 |
}
|
| 206 |
|
| 207 |
drupal_set_message(t('Predicate meta data saved.'));
|
| 208 |
|
| 209 |
$form_state['redirect'] = 'admin/settings/ca';
|
| 210 |
}
|
| 211 |
|
| 212 |
// Form to reset a modified module defined predicate to its original state.
|
| 213 |
function ca_predicate_delete_form($form_state, $pid) {
|
| 214 |
$predicate = ca_load_predicate($pid);
|
| 215 |
|
| 216 |
// Fail if we received an invalid predicate ID.
|
| 217 |
if (empty($predicate)) {
|
| 218 |
drupal_set_message(t('That predicate does not exist.'), 'error');
|
| 219 |
drupal_goto('admin/settings/ca');
|
| 220 |
}
|
| 221 |
|
| 222 |
$form['predicate_pid'] = array(
|
| 223 |
'#type' => 'value',
|
| 224 |
'#value' => $pid,
|
| 225 |
);
|
| 226 |
|
| 227 |
$form['predicate_title'] = array(
|
| 228 |
'#type' => 'value',
|
| 229 |
'#value' => $predicate['#title'],
|
| 230 |
);
|
| 231 |
|
| 232 |
$description = '<p><strong>'. check_plain($predicate['#title']) .'</strong><br />'
|
| 233 |
. check_plain($predicate['#description']) .'</p><p>'
|
| 234 |
. t('This action cannot be undone.') .'</p>';
|
| 235 |
|
| 236 |
return confirm_form($form, t('Are you sure you want to !op this predicate?', array('!op' => is_numeric($pid) ? t('delete') : t('reset'))), 'admin/settings/ca', $description);
|
| 237 |
}
|
| 238 |
|
| 239 |
function ca_predicate_delete_form_submit($form_id, &$form_state) {
|
| 240 |
ca_delete_predicate($form_state['values']['predicate_pid']);
|
| 241 |
|
| 242 |
drupal_set_message(t('Predicate %title !op.', array('%title' => $form_state['values']['predicate_title'], '!op' => is_numeric($form_state['values']['predicate_pid']) ? t('deleted') : t('reset'))));
|
| 243 |
|
| 244 |
$form_state['redirect'] = 'admin/settings/ca';
|
| 245 |
}
|
| 246 |
|