Index: flexifilter.components.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/flexifilter/flexifilter.components.inc,v retrieving revision 1.12 diff -u -r1.12 flexifilter.components.inc --- flexifilter.components.inc 22 Jan 2008 00:48:28 -0000 1.12 +++ flexifilter.components.inc 3 Feb 2008 06:15:06 -0000 @@ -119,6 +119,33 @@ 'step' => 'either', ); + // Sequences + $components['flexifilter_basic_sequences'] = array( + 'label' => t('Basic sequences'), + 'description' => t('Choose from a variety of sequences to insert.'), + 'callback' => 'flexifilter_component_sequences', + 'group' => t('Sequences'), + 'step' => 'either', + ); + + // Storage + $components['flexifilter_storage_set'] = array( + 'label' => t('Store data'), + 'description' => t('Store data to be retrieved later in the filtering process.'), + 'callback' => 'flexifilter_component_storage_set', + 'group' => t('Storage'), + 'contains_components' => TRUE, + 'step' => 'either', + ); + $components['flexifilter_storage_get'] = array( + 'label' => t('Retrieve stored data'), + 'description' => t('Retrieve data stored earlier in the filtering process.'), + 'callback' => 'flexifilter_component_storage_get', + 'group' => t('Storage'), + 'contains_components' => TRUE, + 'step' => 'either', + ); + // Allow existing filters to be used as components foreach (module_list() as $module) { if ($module !== 'flexifilter') { @@ -640,3 +667,128 @@ } } +/** + * Basic sequence callback. + */ +function flexifilter_component_sequences($op, $settings, $text) { + switch ($op) { + case 'settings': + $form = array(); + $form['text'] = array( + '#type' => 'textfield', + '#title' => t('Text to replace'), + '#description' => t('Enter the text to replace. Leave blank for simple insertion.'), + '#default_value' => isset($settings['text']) ? $settings['text'] : '', + ); + $form['sequence'] = array( + '#type' => 'radios', + '#title' => t('Sequence type'), + '#options' => module_invoke_all('flexifilter_sequences'), + '#default_value' => 'flexifilter_sequence_numbers', + ); + return $form; + + case 'prepare': + case 'process': + static $_flexifilter_sequence_count = 0; + if (!empty($settings['text'])) { + while (flexifilter_sequences_replace_once($settings['text'], call_user_func($settings['sequence'], $_flexifilter_sequence_count), $text)) { + $_flexifilter_sequence_count++; + } + return $text; + } + return call_user_func($settings['sequence'], $_flexifilter_sequence_count); + } +} + + +function flexifilter_sequences_replace_once($search, $replace, &$subject) { + if (($pos = strpos($subject, $search)) !== FALSE) { + $subject = substr($subject, 0, $pos) . $replace . substr($subject, $pos + strlen($search)); + return TRUE; + } + return FALSE; +} + +/** + * Implementation of hook_flexifilter_sequences. + */ +function flexifilter_flexifilter_sequences() { + return array( + 'flexifilter_sequence_numbers' => t('Simple numbers'), + 'flexifilter_sequence_letters_uc' => t('Letters, upper case'), + 'flexifilter_sequence_letters_lc' => t('Letters, lower case'), + ); +} + +/** + * Sequence callback: simple numbers. + */ +function flexifilter_sequence_numbers($num) { + return $num + 1; +} + +function flexifilter_sequence_letters_uc($num) { + return strtoupper(flexifilter_sequence_letters_lc($num)); +} + +function flexifilter_sequence_letters_lc($num) { + $letters = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'); + return $letters[$num % 26]; +} + +/** + * Component callback. + */ +function flexifilter_component_storage_set($op, $settings, $text) { + switch ($op) { + case 'settings': + return flexifilter_storage_slot(); + + case 'prepare': + case 'process': + $data = flexifilter_invoke_components($settings['components'], $op, $text); + static $_flexifilter_stored_data = array(); + $_flexifilter_stored_data[$settings['slot']] = $data; + return $text; + } +} + +/** + * Component callback. + */ +function flexifilter_component_storage_get($op, $settings, $text) { + switch ($op) { + case 'settings': + $form = flexifilter_storage_slot(); + + case 'prepare': + case 'process': + static $_flexifilter_stored_data = array(); + if (!isset($_flexifilter_stored_data[$settings['slot']])) { + $_flexifilter_stored_data[$settings['slot']] = ''; + } + $data = flexifilter_invoke_components($settings['components'], $op, $_flexifilter_stored_data[$settings['slot']]); + return $data; + } +} + +/** + * FAPI helper function + */ +function flexifilter_storage_slot() { + $form = array(); + $num = 1; + $options = array(); + while ($num <= 100) { + $options[$num] = $num; + } + $form['slot'] = array( + '#type' => 'select', + '#title' => t('Slot'), + '#description' => t('Select the slot at which the data should be stored.'), + '#options' => $options, + '#default_value' => array(1), + ); + return $form; +} \ No newline at end of file