/[drupal]/contributions/modules/flexifilter/flexifilter.admin.inc
ViewVC logotype

Contents of /contributions/modules/flexifilter/flexifilter.admin.inc

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


Revision 1.16 - (show annotations) (download) (as text)
Sun Apr 13 02:35:27 2008 UTC (19 months, 2 weeks ago) by cwgordon7
Branch: MAIN
CVS Tags: HEAD
Changes since 1.15: +19 -4 lines
File MIME type: text/x-php
General cleanup, plus caching options added
1 <?php
2 // $Id: flexifilter.admin.inc,v 1.15 2008/04/12 22:56:00 cwgordon7 Exp $
3
4 /**
5 * Generates the form for the admin overview page
6 */
7 function flexifilter_filter_list_form() {
8 drupal_set_title("Flexifilters overview");
9 $path = drupal_get_path('module', 'flexifilter') .'/flexifilter.css';
10 drupal_add_css($path, 'module', 'all', FALSE);
11
12 $form = array();
13 $form['enabled_header'] = array(
14 '#value' => '<h3>'. t('Enabled Flexifilters') .'</h3>',
15 '#suffix' => t('If a flexifilter is enabled, then it can be used within input formats, however there can only be 128 enabled at any one time.') .'<br/>',
16 );
17 $form['enabled'] = flexifilter_filter_list_form_table(TRUE);
18
19 $form['disabled_header'] = array(
20 '#value' => '<h3>'. t('Disabled Flexifilters') .'</h3>',
21 '#suffix' => t('If a flexifilter is disabled, then it cannot be used within input formats, although it can be used within other flexifilters.') .'<br/>',
22 );
23 $form['disabled'] = flexifilter_filter_list_form_table(FALSE);
24
25 return $form;
26 }
27
28 /**
29 * Helper function for flexifilter_filter_list_form. Returns the table for the overview page.
30 *
31 * @param $enabled
32 * Is TRUE if we're theming the currently enabled filters.s
33 */
34 function flexifilter_filter_list_form_table($enabled) {
35 $show_disable_link = $enabled;
36 $show_enable_link = !$enabled && flexifilter_get_number_enabled_filters() < FLEXIFILTER_MAX_FILTERS;
37 $header = array(t('Label'), t('Description'), t('Edit'), t('Delete'), t('Preview'), t($enabled ? 'Disable' : 'Enable'));
38 $rows = array();
39 $filters = flexifilter_get_filters();
40 foreach ($filters as $id => $filter) {
41 if ($filter['enabled'] == $enabled) {
42 $row = array($filter['label'], $filter['description']);
43 $row[] = l(t('Edit'), 'admin/build/flexifilters/'. $id .'/edit', array(), drupal_get_destination());
44 $row[] = l(t('Delete'), 'admin/build/flexifilters/'. $id .'/delete', array(), drupal_get_destination());
45 $row[] = l(t('Preview'), 'admin/build/flexifilters/'. $id .'/preview', array(), drupal_get_destination());
46 if ($show_disable_link) {
47 $row[] = l(t('Disable'), 'admin/build/flexifilters/'. $id .'/disable', array(), drupal_get_destination());
48 }
49 else if ($show_enable_link) {
50 $row[] = l(t('Enable'), 'admin/build/flexifilters/'. $id .'/enable', array(), drupal_get_destination());
51 }
52 else {
53 $row[] = '';
54 }
55 $rows[] = $row;
56 }
57 }
58 if (count($rows)) {
59 return array('#value' => theme('table', $header, $rows, array('class' => 'flexifilters-list')));
60 }
61 else {
62 return array('#value' => t('None.'));
63 }
64 }
65
66 /**
67 * Helper function for flexifilter_filter_edit_form; removes the automatic
68 * naming of submit controls.
69 * Our forms are complex and have submit controls nested within fieldsets,
70 * and when pressed, we want the 'op' field to be set within the fieldset,
71 * not at the top level, so we have to undo the automatic naming of submit
72 * fields.
73 * This function is a brutal rip of code from _form_builder_handle_input_element
74 *
75 * @param $form A form element to reset to the normal name.
76 *
77 * @return The updated form element
78 */
79 function flexifilter_undo_submit_default_name($form) {
80 $name = array_shift($form['#parents']);
81 $form['#name'] = $name;
82 if ($form['#type'] == 'file') {
83 // To make it easier to handle $_FILES in file.inc, we place all
84 // file fields in the 'files' array. Also, we do not support
85 // nested file names.
86 $form['#name'] = 'files['. $form['#name'] .']';
87 }
88 elseif (count($form['#parents'])) {
89 $form['#name'] .= '['. implode('][', $form['#parents']) .']';
90 }
91 array_unshift($form['#parents'], $name);
92 return $form;
93 }
94
95 /**
96 * Helper function for flexifilter_filter_edit_components_data; converts a condition
97 * array from a flexifilter into a form data styled array.
98 */
99 function flexifilter_filter_edit_condition_data($data) {
100 $condition = array(
101 'class' => $data['class'],
102 );
103 foreach ($data['settings'] as $key => $value) {
104 if ($key == 'conditions') {
105 foreach ($value as $cond_key => $cond_value) {
106 $condition['condition_'. $cond_key] = flexifilter_filter_edit_condition_data($cond_value);
107 }
108 }
109 else {
110 $condition['setting_'. $key] = $value;
111 }
112 }
113 return $condition;
114 }
115
116 /**
117 * Helper function for flexifilter_filter_edit_form; converts a components
118 * array from a flexifilter into a form data styled array.
119 */
120 function flexifilter_filter_edit_components_data($components, $id_prefix = NULL) {
121 $data = array();
122 if (isset($components['id_prefix'])) {
123 $id_prefix = $components['id_prefix'];
124 $data['id_prefix'] = $id_prefix;
125 unset($components['id_prefix']);
126 }
127 if (isset($components['id_next'])) {
128 $data['id_next'] = $components['id_next'];
129 unset($components['id_next']);
130 }
131 foreach ($components as $n => $component) {
132 $us = array();
133 foreach ($component['settings'] as $key => $value) {
134 if ($key == 'components') {
135 $us['components'] = flexifilter_filter_edit_components_data($value, $id_prefix);
136 }
137 elseif ($key == 'condition') {
138 $us['condition'] = flexifilter_filter_edit_condition_data($value);
139 }
140 elseif ($key == 'step') {
141 $us[$key] = $value;
142 }
143 else {
144 $us['setting_'. $key] = $value;
145 }
146 }
147 $us['weight'] = $n;
148 $us = array_merge($us, $component);
149 unset($us['settings']);
150 $id = $us['id'];
151 unset($us['id']);
152 $data[$id_prefix . $id] = $us;
153 }
154 return $data;
155 }
156
157 /**
158 * Creates an FAPI values array for a newly created component
159 *
160 * @param $component_class The component class of the new component
161 */
162 function flexifilter_filter_edit_form_new_component_data($component_class) {
163 $components = flexifilter_get_component_list();
164 $component = $components[$component_class];
165 $data = array(
166 'class' => $component_class,
167 'weight' => 10000,
168 'is_new' => true,
169 );
170 if ($component['step'] == 'either') {
171 $data['step'] = 'process';
172 }
173 if ($component['is_container']) {
174 if ($component['contains_condition']) {
175 $data['condition'] = array('class' => '');
176 }
177 if ($component['contains_components']) {
178 $data['components'] = array();
179 }
180 }
181 return $data;
182 }
183
184 /**
185 * Creates an FAPI values array for a newly created condition
186 *
187 * @param $condition_class The condition class of the new condition
188 */
189 function flexifilter_filter_edit_form_new_condition_data($condition_class) {
190 $conditions = flexifilter_get_condition_list();
191 $condition = $conditions[$condition_class];
192 $data = array(
193 'class' => $condition_class,
194 'is_new' => TRUE,
195 'weight' => 10000,
196 );
197 return $data;
198 }
199
200 /**
201 * Helper function for flexifilter_filter_edit_form_component; creates an FAPI form table
202 * for a condition.
203 */
204 function flexifilter_filter_edit_form_condition($data, $show_advanced) {
205 if ($data['class'] == '') {
206 $form = array(
207 '#type' => 'fieldset',
208 '#collapsible' => TRUE,
209 '#collapsed' => (isset($data['op']) || isset($data['op_c']) || isset($data['is_new'])) ? FALSE : TRUE,
210 '#tree' => TRUE,
211 '#title' => t('No condition'),
212 '#description' => t('There is no condition set. This will probably cause the component to do nothing, as "No Condition" evaluates to false.'),
213 );
214 $form['class'] = array('#type' => 'hidden', '#value' => '');
215 return $form;
216 }
217
218 $conditions = flexifilter_get_condition_list();
219 $condition = $conditions[$data['class']];
220 $form = array(
221 '#type' => 'fieldset',
222 '#collapsible' => TRUE,
223 '#collapsed' => (isset($data['op']) || isset($data['is_new'])) ? FALSE : TRUE,
224 '#tree' => TRUE,
225 '#title' => $condition['label'],
226 '#description' => $condition['description'],
227 );
228 $form['class'] = array('#type' => 'hidden', '#value' => $data['class']);
229 $custom_settings = array();
230 foreach ($data as $key => $value) {
231 if (strncmp($key, 'setting_', 8) == 0) {
232 $custom_settings[substr($key, 8)] = $value;
233 }
234 }
235 $custom_elements = flexifilter_invoke_condition(array('class' => $data['class'], 'settings' => $custom_settings), 'settings');
236 if (is_array($custom_elements)) {
237 foreach ($custom_elements as $key => $element) {
238 $form['setting_'. $key] = $element;
239 }
240 }
241 if ($condition['is_container']) {
242 if ($condition['contains_conditions']) {
243 $conditions = flexifilter_get_condition_list();
244
245 $condition_max_id = 0;
246 foreach ($data as $key => $sub_condition) {
247 if (strncmp($key, 'condition_', 10) == 0) {
248 $condition_max_id = max($condition_max_id, 1 + (int)substr($key, 10));
249 if (!isset($sub_condition['op'])) {
250 $form[$key] = flexifilter_filter_edit_form_condition($sub_condition, $show_advanced);
251 }
252 else {
253 $form['#collapsed'] = FALSE;
254 }
255 }
256 }
257 if (isset($data['op_c'])) {
258 $form['condition_'. $condition_max_id] = flexifilter_filter_edit_form_condition(flexifilter_filter_edit_form_new_condition_data($data['add_condition']), $show_advanced);
259 }
260
261 $form['add_condition'] = array(
262 '#type' => 'select',
263 '#title' => t('Add condition'),
264 '#options' => flexifilter_get_grouped_labels($conditions),
265 '#description' => t('Adds a new condition wthin this one.'),
266 );
267 $form['op_c'] = array(
268 '#type' => 'submit',
269 '#value' => t('Add'),
270 '#process' => array('flexifilter_undo_submit_default_name', 'form_expand_ahah'),
271 );
272 // Add some kind of seperator, otherwise this add button and the remove button are put onto the same line
273 // which is just a little bit confusing to the user. Hence we use a horizontal rule to seperate them alot.
274 $form['end_add_cond'] = array('#type' => 'markup', '#value' => '<br/><br/><hr/><br/>');
275 }
276 }
277 $form['op'] = array(
278 '#type' => 'submit',
279 '#value' => t('Remove condition'),
280 '#process' => array('flexifilter_undo_submit_default_name', 'form_expand_ahah'),
281 );
282
283 return $form;
284 }
285
286 /**
287 * Helper function for flexifilter_filter_edit_form_components; generates an FAPI array for a
288 * single component.
289 */
290 function flexifilter_filter_edit_form_component($data, $new_weight, $is_first, $is_last, &$data_base, $show_advanced) {
291 $components = flexifilter_get_component_list();
292 $component = $components[$data['class']];
293 $form = array(
294 '#type' => 'fieldset',
295 '#collapsible' => TRUE,
296 '#collapsed' => (isset($data['op']) || isset($data['op_c']) || isset($data['is_new'])) ? FALSE : TRUE,
297 '#tree' => TRUE,
298 '#title' => $component['label'],
299 '#description' => $component['description'],
300 );
301 $form['class'] = array(
302 '#type' => 'hidden',
303 '#value' => $data['class'],
304 );
305 $form['weight'] = array(
306 '#type' => 'hidden',
307 '#value' => $new_weight,
308 );
309 $custom_settings = array();
310 foreach ($data as $key => $value) {
311 if (strncmp($key, 'setting_', 8) == 0) {
312 $custom_settings[substr($key, 8)] = $value;
313 }
314 }
315 $custom_elements = flexifilter_invoke_component($component, 'settings', $custom_settings);
316 if (is_array($custom_elements)) {
317 foreach ($custom_elements as $key => $element) {
318 $form['setting_'. $key] = $element;
319 }
320 }
321 if ($component['is_container']) {
322 if ($component['contains_condition']) {
323 if (isset($data['op_c'])) {
324 $data['condition'] = flexifilter_filter_edit_form_new_condition_data($data['set_condition']);
325 }
326 if (isset($data['condition']) && isset($data['condition']['op'])) {
327 $data['condition'] = array(
328 'class' => '',
329 );
330 $form['#collapsed'] = FALSE;
331 }
332 $form['condition'] = flexifilter_filter_edit_form_condition($data['condition'], $show_advanced);
333 if ($form['condition']['#collapsed'] == FALSE) {
334 $form['#collapsed'] = FALSE;
335 }
336 $conditions = flexifilter_get_condition_list($show_advanced);
337 $form['set_condition'] = array(
338 '#type' => 'select',
339 '#title' => t('Change condition'),
340 '#options' => flexifilter_get_grouped_labels($conditions),
341 '#description' => t('Changes the condition assocated with this component.'),
342 );
343 $form['op_c'] = array(
344 '#type' => 'submit',
345 '#value' => t('Change'),
346 '#process' => array('flexifilter_undo_submit_default_name', 'form_expand_ahah'),
347 );
348 }
349 if ($component['contains_components']) {
350 $form['components'] = flexifilter_filter_edit_form_components($data['components'], $data_base, $show_advanced);
351 if ($form['components']['#collapsed'] == FALSE) {
352 $form['#collapsed'] = FALSE;
353 }
354 }
355 }
356 if ($component['step'] == 'either') {
357 if ($show_advanced) {
358 $form['step'] = array(
359 '#type' => 'select',
360 '#title' => t('Step'),
361 '#options' => array(
362 'process' => t('Processing'),
363 'prepare' => t('Preparation'),
364 ),
365 '#description' => t('The filtering step to perform this action in.'),
366 '#default_value' => $data['step'],
367 );
368 }
369 else {
370 $form['step'] = array(
371 '#type' => 'hidden',
372 '#value' => $data['step'],
373 );
374 }
375 }
376 $form['move'] = array(
377 '#type' => 'select',
378 '#title' => t('Re/move this'),
379 '#options' => array(),
380 // If the element at the bottom is moved to the top, then the #value of 'top' will no longer be in the downdown
381 // and the FAPI throws a nasty validation error. Hence mark it already validated and we will do our own validation.
382 '#validated' => true,
383 );
384 if (!$is_first) {
385 $form['move']['#options']['top'] = t('Move to top');
386 $form['move']['#options']['up'] = t('Move up');
387 }
388 if (!$is_last) {
389 $form['move']['#options']['down'] = t('Move down');
390 $form['move']['#options']['bottom'] = t('Move to bottom');
391 }
392 $form['move']['#options']['remove'] = t('Remove');
393 $form['op'] = array(
394 '#type' => 'submit',
395 '#value' => t('Re/move'),
396 '#process' => array('flexifilter_undo_submit_default_name', 'form_expand_ahah'),
397 );
398
399 return $form;
400 }
401
402 /**
403 * Helper function; sorts components by weight.
404 */
405 function _flexifilter_components_sort_weight($a, $b) {
406 return ((float)$a['weight']) > ((float)$b['weight']);
407 }
408
409 /**
410 * Generates an FAPI array for a component list
411 *
412 * @param $data FAPI values for the component list. Use flexifilter_filter_edit_components_data to
413 * convert a components array into coresponding FAPI values.
414 * @param $data_base FAPI values for the top-level component list
415 */
416 function flexifilter_filter_edit_form_components($data, &$data_base, $show_advanced) {
417 $form = array(
418 '#type' => 'fieldset',
419 '#title' => t('Components'),
420 '#collapsed' => TRUE,
421 '#collapsible' => TRUE,
422 '#tree' => TRUE,
423 );
424 $components = array();
425 foreach ($data as $key => $component) {
426 if (strncmp($key, $data_base['id_prefix'], strlen($data_base['id_prefix'])) == 0) {
427 $components[$key] = $component;
428 }
429 }
430 if (isset($data['op'])) {
431 $components[$data_base['id_prefix'] . $data_base['id_next']] = flexifilter_filter_edit_form_new_component_data($data['new_list']);
432 $data_base['id_next'] = $data_base['id_next'] + 1;
433 }
434 foreach ($components as $key => $component) {
435 if (isset($component['op'])) {
436 switch ($component['move']) {
437 /*
438 Careful; this component is NOT VALIDATED by the FAPI. We only act upon certain
439 values ('top', 'up', 'down', 'bottom', 'remove'), which are always valid (even
440 if they have no effect). Other values are silently ignored.
441 */
442
443 case 'top':
444 $components[$key]['weight'] = -1000;
445 break;
446
447 case 'up':
448 $components[$key]['weight'] = $component['weight'] - 1.5;
449 break;
450
451 case 'down':
452 $components[$key]['weight'] = $component['weight'] + 1.5;
453 break;
454
455 case 'bottom':
456 $components[$key]['weight'] = 10001;
457 break;
458
459 case 'remove':
460 unset($components[$key]);
461 break;
462 }
463 $form['#collapsed'] = FALSE;
464 }
465 }
466 uasort($components, '_flexifilter_components_sort_weight');
467 $new_weight = 0;
468 $last_component_weight = count($components);
469 foreach ($components as $key => $component) {
470 $new_weight = $new_weight + 1;
471 $form[$key] = flexifilter_filter_edit_form_component($component, $new_weight, $new_weight == 1, $new_weight == $last_component_weight, $data_base, $show_advanced);
472 if ($form[$key]['#collapsed'] == FALSE) {
473 $form['#collapsed'] = FALSE;
474 }
475 }
476 $form['new_list'] = array(
477 '#type' => 'select',
478 '#options' => flexifilter_get_grouped_labels(flexifilter_get_component_list($show_advanced)),
479 '#title' => t('Add component'),
480 );
481 $form['op'] = array(
482 '#type' => 'submit',
483 '#value' => t('Add'),
484 '#process' => array('flexifilter_undo_submit_default_name', 'form_expand_ahah'),
485 );
486 return $form;
487 }
488
489 /**
490 * The add/edit form for flexifilters.
491 */
492 function flexifilter_filter_edit_form($form_state, $flexifilter = 'new') {
493 if (!isset($form_state['post']) || !isset($form_state['post']['fid'])) {
494 $data = array();
495 $data['fid'] = $flexifilter === 'new' ? 'new' : $flexifilter['id'];
496 $data['basic'] = array();
497 $data['basic']['label'] = $flexifilter === 'new' ? '' : $flexifilter['label'];
498 $data['basic']['description'] = $flexifilter === 'new' ? '' : $flexifilter['description'];
499 $data['basic']['advanced'] = $flexifilter === 'new' ? 0 : $flexifilter['advanced'];
500 $data['basic']['cache'] = $flexifilter === 'new' ? 1 : $flexifilter['cache'];
501 $data['components'] = $flexifilter === 'new' ? array() : flexifilter_filter_edit_components_data($flexifilter['components']);
502 if (!isset($data['components']['id_prefix']) || $data['components']['id_prefix'] === 'component_new_') {
503 $data['components']['id_prefix'] = 'component_'. $data['fid'] .'_';
504 }
505 if (!isset($data['components']['id_next'])) {
506 $data['components']['id_next'] = 0;
507 }
508 $expand_root_levels = TRUE;
509 }
510 else {
511 $data = $form_state['post'];
512 if (!isset($data['basic']['advanced'])) {
513 $data['basic']['advanced'] = 0;
514 }
515 if (!isset($data['basic']['cache'])) {
516 $data['basic']['cache'] = $flexifilter === 'new' ? 1 : $flexifilter['cache'];
517 }
518 }
519 $form = array();
520 $form['fid'] = array(
521 '#type' => 'hidden',
522 '#value' => $data['fid'],
523 );
524 $form['basic'] = array(
525 '#type' => 'fieldset',
526 '#title' => t('Basic settings'),
527 '#collapsed' => TRUE,
528 '#collapsible' => TRUE,
529 '#tree' => TRUE,
530 );
531 $form['basic']['label'] = array(
532 '#type' => 'textfield',
533 '#title' => t('Label'),
534 '#required' => TRUE,
535 '#maxlength' => 127,
536 '#description' => t('Choose an appropriate label for this flexifilter.'),
537 '#default_value' => $data['basic']['label'],
538 );
539 $form['basic']['description'] = array(
540 '#type' => 'textfield',
541 '#title' => t('Description/filter tips'),
542 '#required' => TRUE,
543 '#maxlength' => 255,
544 '#description' => t('The tips to present to the user under the input format\'s description on the content creation page. The delimiter &lt;!--break--&gt;
545 may be added to separate the short description from the long description.'),
546 '#default_value' => $data['basic']['description'],
547 );
548 $form['basic']['advanced'] = array(
549 '#type' => 'checkbox',
550 '#title' => t('Show advanced options'),
551 '#default_value' => $data['basic']['advanced'],
552 '#description' => t('Advanced options currently only include the processing step option, which allows you to place some components into the filtering preparation step. You should only do so if it is critical that they be performed in the preparation step rather than the processing step.'),
553 );
554 if ($data['basic']['advanced']) {
555 $form['basic']['cache'] = array(
556 '#type' => 'checkbox',
557 '#title' => t('Enable caching'),
558 '#description' => t('If caching is not enabled, the filter will run each time the content is viewed. It is useful for dynamic filters (e.g. things that change each time) to have this option off, but this can slow down page loads noticably.'),
559 '#default_value' => $data['basic']['cache'],
560 );
561 }
562 $form['components'] = flexifilter_filter_edit_form_components($data['components'], $data['components'], $data['basic']['advanced'] == 1);
563 $form['components']['id_prefix'] = array('#type' => 'hidden', '#value' => $data['components']['id_prefix']);
564 $form['components']['id_next'] = array('#type' => 'hidden', '#value' => $data['components']['id_next']);
565 if (isset($expand_root_levels) && $expand_root_levels) {
566 $form['basic']['#collapsed'] = FALSE;
567 $form['components']['#collapsed'] = FALSE;
568 }
569 $form['submit1'] = array(
570 '#type' => 'submit',
571 '#value' => $data['fid'] === 'new' ? t('Create') : t('Save'),
572 );
573 $form['submit2'] = array(
574 '#type' => 'submit',
575 '#value' => $data['fid'] === 'new' ? t('Create and edit') : t('Save and edit'),
576 );
577 return $form;
578 }
579
580 /**
581 * Helper function for flexifilter_filter_edit_form_components_from_data; converts a condition's
582 * FAPI values back into a condition array.
583 */
584 function flexifilter_filter_edit_form_condition_from_data($data) {
585 $condition = array(
586 'class' => $data['class'],
587 'settings' => array(),
588 );
589 foreach ($data as $key => $value) {
590 if (strncmp($key, 'setting_', 8) == 0) {
591 $condition['settings'][substr($key, 8)] = $value;
592 }
593 elseif (strncmp($key, 'condition_', 10) == 0) {
594 if (!isset($condition['conditions'])) {
595 $condition['settings']['conditions'] = array();
596 }
597 $condition['settings']['conditions'][] = flexifilter_filter_edit_form_condition_from_data($value);
598 }
599 }
600 return $condition;
601 }
602
603 /**
604 * Helper function for flexifilter_filter_edit_form_submit; converts components FAPI values
605 * back into a components array.
606 */
607 function flexifilter_filter_edit_form_components_from_data($data, $data_base = NULL) {
608 $components = array();
609 if (!isset($data_base)) {
610 $data_base = $data;
611 $components['id_prefix'] = $data_base['id_prefix'];
612 $components['id_next'] = $data_base['id_next'];
613 }
614 foreach ($data as $key => $data_us) {
615 if (strncmp($key, $data_base['id_prefix'], strlen($data_base['id_prefix'])) == 0) {
616 $component = array(
617 'class' => $data_us['class'],
618 'settings' => array(),
619 );
620 foreach ($data_us as $key => $value) {
621 if (strncmp($key, 'setting_', 8) == 0) {
622 $component['settings'][substr($key, 8)] = $value;
623 }
624 }
625 if (isset($data_us['step'])) {
626 $component['settings']['step'] = $data_us['step'];
627 }
628 if (isset($data_us['components'])) {
629 $component['settings']['components'] = flexifilter_filter_edit_form_components_from_data($data_us['components'], $data_base);
630 }
631 if (isset($data_us['condition'])) {
632 $component['settings']['condition'] = flexifilter_filter_edit_form_condition_from_data($data_us['condition']);
633 }
634 $components[] = $component;
635 }
636 }
637 return $components;
638 }
639
640 function flexifilter_filter_edit_form_submit_components($data, $fid, &$pids_to_reuse, $parent = FALSE) {
641 if ($parent === FALSE) {
642 if ($reuse_pid = array_shift($pids_to_reuse)) {
643 db_query("UPDATE {flexifilters_parts} SET fid = %d, parent_pid = 0, type = %d, class_name = '', settings = '' WHERE pid = %d",
644 $fid, FLEXIFILTER_PART_TYPE_ROOT, $reuse_pid);
645 $parent = $reuse_pid;
646 }
647 else {
648 db_query("INSERT INTO {flexifilters_parts} (fid, parent_pid, type, class_name, settings) VALUES (%d, 0, %d, '', '')",
649 $fid, FLEXIFILTER_PART_TYPE_ROOT);
650 $parent = db_last_insert_id('flexifilters_parts', 'pid');
651 }
652 }
653 foreach ($data as $key => $child) {
654 if (is_numeric($key)) {
655 if ($child_cid = array_shift($pids_to_reuse)) {
656 db_query("UPDATE {flexifilters_parts} SET fid = %d, parent_pid = %d, type = %d, class_name = '%s', settings = '%s' WHERE pid = %d",
657 $fid, $parent, FLEXIFILTER_PART_TYPE_COMPONENT, $child['class'], serialize($child['settings']), $child_cid);
658 }
659 else {
660 db_query("INSERT INTO {flexifilters_parts} (fid, parent_pid, type, class_name, settings) VALUES (%d, %d, %d, '%s', '%s')",
661 $fid, $parent, FLEXIFILTER_PART_TYPE_COMPONENT, $child['class'], serialize($child['settings']));
662 $child_cid = db_last_insert_id('flexifilters_parts', 'pid');
663 }
664 if (isset($child['components'])) {
665 flexifilter_filter_edit_form_submit_components($child['components'], $fid, $pids_to_reuse, $child_cid);
666 }
667 if (isset($child['condition'])) {
668 flexifilter_filter_edit_form_submit_condition($child['condition'], $fid, $pids_to_reuse, $child_cid);
669 }
670 }
671 }
672 return $parent;
673 }
674
675 function flexifilter_filter_edit_form_submit_condition($data, $fid, &$pids_to_reuse, $parent) {
676 if ($our_pid = array_shift($pids_to_reuse)) {
677 db_query("UPDATE {flexifilters_parts} SET fid = %d, parent_pid = %d, type = %d, class_name = '%s', settings = '%s' WHERE pid = %d",
678 $fid, $parent, FLEXIFILTER_PART_TYPE_CONDITION, $data['class'], serialize($data['settings']), $our_pid);
679 }
680 else {
681 db_query("INSERT INTO {flexifilters_parts} (fid, parent_pid, type, class_name, settings) VALUES (%d, %d, %d, '%s', '%s')",
682 $fid, $parent, FLEXIFILTER_PART_TYPE_CONDITION, $data['class'], serialize($data['settings']));
683 $our_pid = db_last_insert_id('flexifilters_parts', 'pid');
684 }
685 if (isset($data['conditions'])) {
686 foreach ($data['conditions'] as $condition) {
687 flexifilter_filter_edit_form_submit_condition($condition, $fid, $pids_to_reuse, $our_pid);
688 }
689 }
690 }
691
692 function flexifilter_filter_edit_form_submit($form, &$form_state) {
693 $values = $form_state['values'];
694 $values['components'] = flexifilter_filter_edit_form_components_from_data($values['components']);
695 $values += $values['basic'];
696 unset($values['basic']);
697 if ($values['fid'] === 'new') {
698 $enabled = 0;
699 if (flexifilter_get_number_enabled_filters() < FLEXIFILTER_MAX_FILTERS) {
700 $enabled = 1;
701 }
702 $values['enabled'] = $enabled;
703 }
704 $fid = flexifilter_save_filter($values);
705 if ($values['fid'] === 'new') {
706 drupal_set_message(t('Flexifilter created.'));
707 if ($values['op'] == $values['submit2']) {
708 unset($form_state['storage']);
709 unset($form_state['rebuild']);
710 $form_state['redirect'] = 'admin/build/flexifilters/'. $fid .'/edit';
711 }
712 }
713 else {
714 drupal_set_message(t('Flexifilter saved.'));
715 }
716 if ($values['op'] == $values['submit1']) {
717 unset($form_state['storage']);
718 unset($form_state['rebuild']);
719 $form_state['redirect'] = 'admin/build/flexifilters';
720 }
721 }
722
723 function flexifilter_filter_delete_form($form_state, $flexifilter) {
724 $form = array();
725 $form['fid'] = array('#type' => 'hidden', '#value' => $flexifilter['id']);
726
727 return confirm_form($form, t('Are you sure you want to delete the flexifilter "%flexifilter"?', array('%flexifilter' => $flexifilter['label'])), 'admin/build/flexifilters', t('This action cannot be undone.'), t('Delete'), t('Cancel'));
728 }
729
730 function flexifilter_remove_from_filters($delta) {
731 db_query("DELETE FROM {filters} WHERE module = 'flexifilter' AND delta = %d", $delta);
732 cache_clear_all('*', 'cache_filter', TRUE);
733 }
734
735 function flexifilter_filter_delete_form_submit($form, &$form_state) {
736 $fid = $form_state['values']['fid'];
737 $filters = flexifilter_get_filters();
738 $filter = $filters[$fid];
739 if ($filter['enabled']) {
740 flexifilter_remove_from_filters($filter['delta']);
741 }
742 db_query('DELETE FROM {flexifilters_parts} WHERE fid = %d', $fid);
743 db_query('DELETE FROM {flexifilters} WHERE fid = %d', $fid);
744 drupal_set_message(t('Flexifilter deleted.'));
745 $form_state['redirect'] = 'admin/build/flexifilters';
746 }
747
748 function flexifilter_filter_disable_form($form_state, $flexifilter) {
749 $form = array();
750 $form['fid'] = array('#type' => 'hidden', '#value' => $flexifilter['id']);
751
752 return confirm_form($form, t('Are you sure you want to disable the flexifilter "%flexifilter"?', array('%flexifilter' => $flexifilter['label'])), 'admin/build/flexifilters', t('If you disable the flexifilter, then it cannot be used in any Input Formats.'), t('Disable'), t('Cancel'));
753 }
754
755 function flexifilter_filter_disable_form_submit($form, &$form_state) {
756 $fid = $form_state['values']['fid'];
757 $filters = flexifilter_get_filters();
758 $filter = $filters[$fid];
759 if ($filter['enabled']) {
760 flexifilter_remove_from_filters($filter['delta']);
761 db_query('UPDATE {flexifilters} SET enabled = 0 WHERE fid = %d', $fid);
762 drupal_set_message(t('Flexifilter disabled.'));
763 }
764 $form_state['redirect'] = 'admin/build/flexifilters';
765 }
766
767 function flexifilter_filter_enable_form($form_state, $flexifilter) {
768 $form = array();
769 $form['fid'] = array('#type' => 'hidden', '#value' => $flexifilter['id']);
770
771 return confirm_form($form, t('Are you sure you want to enable the flexifilter "%flexifilter"?', array('%flexifilter' => $flexifilter['label'])), 'admin/build/flexifilters', t(''), t('Enable'), t('Cancel'));
772 }
773
774 function flexifilter_filter_enable_form_submit($form, &$form_state) {
775 $fid = $form_state['values']['fid'];
776 $filters = flexifilter_get_filters();
777 $filter = $filters[$fid];
778 if (!$filter['enabled'] && flexifilter_get_number_enabled_filters() < FLEXIFILTER_MAX_FILTERS) {
779 db_query('UPDATE {flexifilters} SET enabled = 1, delta = %d WHERE fid = %d', flexifilter_get_unused_delta(), $fid);
780 drupal_set_message(t('Flexifilter enabled.'));
781 }
782 $form_state['redirect'] = 'admin/build/flexifilters';
783 }
784
785 function flexifilter_filter_export_form($form_state, $flexifilter) {
786 $form = array();
787 $flexifilter['fid'] = 'new';
788 $form['serialized'] = array(
789 '#type' => 'textarea',
790 '#attributes' => array('readonly' => 'readonly'),
791 '#cols' => '100',
792 '#title' => t('Serialized flexifilter (for manual imports)'),
793 '#default_value' => serialize($flexifilter),
794 );
795 $form['flexifilter'] = array(
796 '#type' => 'textarea',
797 '#attributes' => array('readonly' => 'readonly'),
798 '#cols' => '100',
799 '#title' => t('Exported flexifilter (for hook_flexifilters implementations)'),
800 '#default_value' => var_export($flexifilter, TRUE),
801 );
802 return $form;
803 }
804
805 function flexifilter_filter_input_form($form_state) {
806 $form = array();
807 $form['flexifilter'] = array(
808 '#type' => 'textarea',
809 '#title' => t('Flexifilter to import'),
810 '#description' => t('Enter the serialized flexifilter as from a flexifilter export here.'),
811 '#default_value' => '',
812 '#required' => TRUE,
813 );
814 $form['submit'] = array(
815 '#type' => 'submit',
816 '#value' => t('Import'),
817 );
818 return $form;
819 }
820
821 function flexifilter_filter_input_form_submit($form, &$form_state) {
822 $fid = unserialize($form_state['values']['flexifilter']);
823 if ($fid) {
824 drupal_goto('admin/build/flexifilters/'. flexifilter_save_filter($form_state['values']['flexifilter']) .'/edit');
825 }
826 else {
827 drupal_set_message(t('It appears that the serialized flexifilter you entered was improperly formed. Please check the source.'), 'error');
828 }
829 }
830
831 function flexifilter_filter_default_form($form_state) {
832 $flexifilters = module_invoke_all('flexifilters');
833 $names = array();
834 foreach ($flexifilters as $key => $flexifilter) {
835 $names[$key] = $flexifilter['label'];
836 }
837 $form['selections'] = array(
838 '#type' => 'checkboxes',
839 '#title' => t('Default flexifilters to load'),
840 '#multiple' => TRUE,
841 '#options' => $names,
842 '#required' => TRUE,
843 '#default_value' => array(),
844 );
845 $form['flexifilters'] = array(
846 '#type' => 'value',
847 '#value' => $flexifilters,
848 );
849 $form['submit'] = array(
850 '#type' => 'submit',
851 '#value' => t('Load'),
852 );
853 return $form;
854 }
855
856 function flexifilter_filter_default_form_submit($form, &$form_state) {
857 $flexifilters = $form_state['values']['flexifilters'];
858 $selections = $form_state['values']['selections'];
859 $to_be_saved = array();
860 foreach ($selections as $selection) {
861 if (isset($flexifilters[$selection])) {
862 $to_be_saved[] = $flexifilters[$selection];
863 }
864 }
865 flexifilter_install_flexifilters('flexifilter', $to_be_saved);
866 $form_state['redirect'] = 'admin/build/flexifilters';
867 }
868
869 function flexifilter_filter_preview_form($form_state, $flexifilter) {
870 if (!isset($form_state['storage']['preview'])) {
871 $form['input'] = array(
872 '#type' => 'textarea',
873 '#title' => t('Text to test'),
874 '#default_value' => '',
875 );
876 $form['flexifilter'] = array(
877 '#type' => 'value',
878 '#value' => $flexifilter,
879 );
880 $form['submit'] = array(
881 '#type' => 'submit',
882 '#value' => t('Preview'),
883 );
884 }
885 else {
886 $form['input'] = array(
887 '#type' => 'textarea',
888 '#title' => t('Original text'),
889 '#default_value' => $form_state['storage']['input'],
890 );
891 $form['submit'] = array(
892 '#type' => 'submit',
893 '#value' => t('Preview'),
894 );
895 $form['prepare'] = array(
896 '#type' => 'fieldset',
897 '#title' => t('Prepare step'),
898 '#collapsible' => TRUE,
899 );
900 $form['process'] = array(
901 '#type' => 'fieldset',
902 '#title' => t('Process step'),
903 '#collapsible' => TRUE,
904 );
905 $form['flexifilter'] = array(
906 '#type' => 'value',
907 '#value' => $flexifilter,
908 );
909 $zebra = 'flexifilter-even';
910 drupal_add_css(drupal_get_path('module', 'flexifilter') .'/flexifilter.css');
911 foreach ($form_state['storage']['data'] as $key => $data) {
912 $settings = '';
913 foreach ($data['settings'] as $key => $value) {
914 $settings .= '<strong>'. check_plain($key) .'</strong>: '. check_plain(''. $value) ."<br />";
915 }
916 $uniqid = uniqid();
917 $form[$data['step']][$key . $uniqid]['step'] = array(
918 '#type' => 'markup',
919 '#prefix' => '<div class="'. $zebra .'"><div class="description">',
920 '#value' => t('@type: @class<p>Settings:<br />!settings</p>', array('@type' => ucfirst($data['type']), '@class' => $data['class'], '!settings' => $settings)),
921 '#suffix' => '</div>',
922 );
923 $form[$data['step']][$key . $uniqid]['text'] = array(
924 '#type' => 'markup',
925 '#title' => ucfirst($data['type']) .': '. $data['class'],
926 '#description' => t('Settings:<br />!settings', array('!settings' => $settings)),
927 '#value' => $data['value'] . '</div><strong>'. t('With visible HTML:') .'</strong><div class="flexifilter-plain-text">'. check_plain($data['value']),
928 '#prefix' => '<strong>'. t('As it appears to users:') .'</strong><div class="flexifilter-text">',
929 '#suffix' => '</div></div>',
930 );
931 $zebra = ($zebra == 'flexifilter-even') ? 'flexifilter-odd' : 'flexifilter-even';
932 }
933 }
934 return $form;
935 }
936
937 function flexifilter_filter_preview_form_submit($form, &$form_state) {
938 variable_set('flexifilter_preview', TRUE);
939 variable_set('flexifilter_preview_text', array());
940 $form_state['storage']['input'] = $form_state['values']['input'];
941 $text = flexifilter_invoke_components($form_state['values']['flexifilter']['components'], 'prepare', $form_state['values']['input']);
942 flexifilter_invoke_components($form_state['values']['flexifilter']['components'], 'process', $text);
943 $form_state['storage']['preview'] = TRUE;
944 $form_state['storage']['data'] = (variable_get('flexifilter_preview_text', array()));
945 variable_del('flexifilter_preview');
946 variable_del('flexifilter_preview_text');
947 }

  ViewVC Help
Powered by ViewVC 1.1.2