/[drupal]/contributions/modules/condition/condition.module
ViewVC logotype

Contents of /contributions/modules/condition/condition.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.15 - (show annotations) (download) (as text)
Mon Jun 8 04:00:34 2009 UTC (5 months, 2 weeks ago) by tobiassjosten
Branch: MAIN
CVS Tags: HEAD
Changes since 1.14: +233 -247 lines
File MIME type: text/x-php
task #291012: Cleaned up as per coding style guidelines
1 <?php
2 // $Id: condition.module,v 1.14 2009/06/07 17:46:35 tobiassjosten Exp $
3
4 /**
5 * @file
6 * Conditions are sets of requirements that make the condition met or not.
7 * Other modules can use this to provide conditional actions.
8 */
9
10 /* 2DO
11 - Module die conditional block visibility doet
12 - Disablen conditions werkend maken
13 - Keuze of condition TRUE is bij geen, een of alle requirements OK
14 */
15
16 define('CONDITION_ANY', 1);
17 define('CONDITION_ALL', 2);
18 define('CONDITION_NOT_ALL', 3);
19 define('CONDITION_NEVER', 4);
20 define('CONDITION_ALWAYS', 5);
21 define('CONDITION_NOT_ANY', 6);
22
23 /**
24 * As per comment by Docc, http://drupal.org/node/278440#comment-907692, the
25 * array_intersect_key() function does not exist in PHP before version 5.1.0.
26 */
27 if (!function_exists('array_intersect_key')) {
28 function array_intersect_key($array1, $array2) {
29 foreach ($array1 as $key => $value) {
30 $argument_count = func_num_args();
31 for ($i = 1; $i < $argument_count; $i++) {
32 $array = func_get_arg($i);
33 if (!is_array($array)) {
34 return FALSE;
35 }
36 if (!array_key_exists($key, $array)) {
37 continue 2;
38 }
39 }
40 $res[$key] = $value;
41 }
42 return $res ? $res : array();
43 }
44 }
45
46 /**
47 * Implementation of hook_menu().
48 */
49 function condition_menu() {
50 $items['admin/settings/condition'] = array(
51 'title' => 'Conditions',
52 'description' => 'Configure conditions other modules use to trigger actions.',
53 'page callback' => 'drupal_get_form',
54 'page arguments' => array('condition_list_form'),
55 'access arguments' => array('administer conditions'),
56 'file' => 'condition.admin.inc',
57 );
58 $items['admin/settings/condition/list'] = array(
59 'title' => t('List'),
60 'weight' => -10,
61 'type' => MENU_DEFAULT_LOCAL_TASK,
62 );
63 $items['admin/settings/condition/add'] = array(
64 'title' => t('Add'),
65 'page callback' => 'drupal_get_form',
66 'page arguments' => array('condition_edit_form'),
67 'access arguments' => array('administer conditions'),
68 'type' => MENU_LOCAL_TASK,
69 'file' => 'condition.admin.inc',
70 );
71 $items['admin/settings/condition/%condition/edit'] = array(
72 'title' => t('Edit'),
73 'page callback' => 'drupal_get_form',
74 'page arguments' => array('condition_edit_form', 3),
75 'access arguments' => array('administer conditions'),
76 'type' => MENU_CALLBACK,
77 'file' => 'condition.admin.inc',
78 );
79 $items['admin/settings/condition/%condition/delete'] = array(
80 'title' => t('Delete'),
81 'page callback' => 'drupal_get_form',
82 'page arguments' => array('condition_delete_form', 3),
83 'access arguments' => array('administer conditions'),
84 'type' => MENU_CALLBACK,
85 'file' => 'condition.admin.inc',
86 );
87
88 return $items;
89 }
90
91 /**
92 * Implementation of hook_perm().
93 */
94 function condition_perm() {
95 return array('administer conditions');
96 }
97
98 /**
99 * Implementation of hook_theme().
100 */
101 function condition_theme() {
102 return array(
103 'condition_edit_form' => array('arguments' => array('form' => NULL)),
104 'condition_list_form' => array('arguments' => array('form' => NULL)),
105 );
106 }
107
108 /**
109 * Implementation of hook_help().
110 */
111 function condition_help($path, $arg) {
112 switch ($path) {
113 case 'admin/settings/condition':
114 return '<p>'. t('Conditions are sets of requirements that make the condition met (<em>TRUE</em>) or not (<em>FALSE</em>). Other modules can provide requirements (e.g. requested path is <em>admin/*</em>) or use the Condition API to trigger some kind of action (e.g. switch theme to <em>Garland</em>) in case a condition is met. Start by !adding a condition.', array('!adding' => l(t('adding'), 'admin/settings/condition/add'))) .'</p>';
115 }
116 }
117
118 /**
119 * Loading one, more or all conditions.
120 */
121 function condition_load($cid = NULL) {
122 static $conditions;
123
124 if (!is_array($conditions)) {
125 if (is_numeric($cid)) {
126 $condition = db_fetch_object(db_query("SELECT * FROM {conditions} WHERE cid = %d", $cid));
127 $condition->parameters = (array) unserialize($condition->parameters);
128 return $condition;
129 }
130 else {
131 $result = db_query("SELECT * FROM {conditions} ORDER BY weight ASC");
132 $conditions = array();
133
134 while ($condition = db_fetch_object($result)) {
135 $condition->parameters = (array) unserialize($condition->parameters);
136 $conditions[$condition->cid] = $condition;
137 }
138 }
139 }
140
141 if (is_array($conditions)) {
142
143 if (is_numeric($cid)) {
144 return $conditions[$cid];
145 }
146 elseif (is_array($cid)) {
147 return array_intersect_key($conditions, array_flip($cid));
148 }
149 else {
150 return $conditions;
151 }
152 }
153 }
154
155 /**
156 * Saving one or more conditions.
157 */
158 function condition_save($condition) {
159 if (is_array($condition)) {
160 foreach (array_keys($condition) as $key) {
161 $condition[$key]->saved = condition_save($condition[$key]);
162 }
163 return $condition;
164 }
165
166 if ($condition->parameters) {
167 $condition->parameters = serialize((array) $condition->parameters);
168 }
169
170 return drupal_write_record('conditions', $condition, $condition->cid ? 'cid' : array());
171 }
172
173 /**
174 * Deleting one or more conditions.
175 */
176 function condition_delete($condition) {
177 if (is_array($condition)) {
178 foreach (array_keys($condition) as $key) {
179 $condition[$key]->deleted = condition_delete($condition[$key]);
180 }
181 return $condition;
182 }
183
184 if (!is_object($condition)) {
185 $condition = condition_load($condition);
186 }
187
188 return db_query("DELETE FROM {conditions} WHERE cid = %d", $condition->cid);
189 }
190
191 /**
192 * Validating a condition.
193 */
194 function condition_validate($condition) {
195 static $validations;
196
197 if (is_array($validations) && isset($validations[$condition->cid])) {
198 return $validations[$condition->cid];
199 }
200
201 if (!$condition->status) {
202 return $validations[$condition->cid] = FALSE;
203 }
204
205 $requirements = module_invoke_all('requirement_info');
206 $valid = TRUE;
207
208 if (is_array($requirements) && count($requirements)) {
209 foreach ($requirements as $requirement => $info) {
210 if ($condition->parameters[$requirement]) {
211 if ($requirement($condition, $condition->parameters[$requirement]) === FALSE) {
212 $valid = FALSE;
213 break;
214 }
215 }
216 }
217 }
218
219 $validations[$condition->cid] = $valid;
220
221 return $valid;
222 }
223
224 /*
225 * Selecting conditions for some other module.
226 */
227 function condition_selection_form($context) {
228 $conditions = condition_load();
229
230 // We might get just the values instead of the form_state.
231 if ($form_state['conditions']) {
232 $form_state['values'] = $form_state;
233 }
234
235 $form['conditions'] = array(
236 '#type' => 'fieldset',
237 '#title' => t('Conditions'),
238 '#description' => t('Restrict to situations where selected conditions (do not) validate.'),
239 '#collapsible' => TRUE,
240 '#collapsed' => isset($context['selection']),
241 '#tree' => TRUE,
242 );
243 $form['conditions']['operator'] = array(
244 '#type' => 'select',
245 '#title' => 'Criteria',
246 '#options' => array(
247 CONDITION_NEVER => t("NEVER validate"),
248 CONDITION_ALWAYS => t("ALWAYS validate"),
249 CONDITION_ANY => t('When ANY selected condition is met'),
250 CONDITION_ALL => t('When ALL selected conditions are met'),
251 CONDITION_NOT_ANY => t('When ANY selected condition is NOT met'),
252 CONDITION_NOT_ALL => t('When ALL selected conditions are NOT met'),
253 ),
254 '#default_value' => $context['operator'],
255 );
256
257 if (count($conditions)) {
258 $form['conditions']['selection'] = array(
259 '#type' => 'checkboxes',
260 '#title' => t('Conditions'),
261 '#options' => array(),
262 '#default_value' => (array) $context['selection'],
263 );
264 foreach ($conditions as $condition) {
265 $form['conditions']['selection']['#options'][$condition->cid] = $condition->name .' ('. l(t('edit'), 'admin/settings/condition/'. $condition->cid .'/edit') .')';
266 }
267 }
268 else {
269 $form['conditions']['selection'] = array(
270 '#type' => 'item',
271 '#title' => t('Conditions'),
272 '#value' => t('!Add a condition first.', array('!Add' => l(t('Add'), 'admin/settings/condition/add'))),
273 );
274 }
275
276 return $form;
277 }
278
279 /*
280 * Submit: Selecting conditions for some other module.
281 */
282 function condition_selection_submit($form, $form_state) {
283 return array(
284 'operator' => (int) $form_state['values']['conditions']['operator'],
285 'selection' => $form_state['values']['conditions']['selection'],
286 );
287 }
288
289 /*
290 * Validating a selection of conditions for some other module.
291 */
292 function condition_selection_validate($parameters) {
293
294 // We might get all form values instead of just ours.
295 if ($parameters['conditions']) {
296 $parameters = $parameters['conditions'];
297 }
298
299 // Invalid parameters = FALSE
300 if (!$parameters['operator'] || !is_array($parameters['selection'])) {
301 return FALSE;
302 }
303
304 $parameters['operator'] = intval($parameters['operator']);
305
306 // Disabled and FALSE
307 if ($parameters['operator'] == CONDITION_NEVER) {
308 return FALSE;
309 }
310
311 // Disabled and TRUE
312 if ($parameters['operator'] == CONDITION_ALWAYS) {
313 return TRUE;
314 }
315
316 $cids = array_filter($parameters['selection'], 'intval');
317 $conditions = condition_load($cids);
318 $valid = 0;
319
320 foreach ($conditions as $condition) {
321 if (condition_validate($condition)) {
322 $valid++;
323 }
324 }
325
326 switch ($parameters['operator']) {
327 case CONDITION_ANY:
328 return ($valid > 0);
329 case CONDITION_ALL:
330 return ($valid == count($conditions));
331 case CONDITION_NOT_ANY:
332 return ($valid != count($conditions));
333 case CONDITION_NOT_ALL:
334 return ($valid == 0);
335 default:
336 return FALSE;
337 }
338 }

  ViewVC Help
Powered by ViewVC 1.1.2