From 2d802fe2bf39ec4a701670fbe262ca11d19b1e36 Mon Sep 17 00:00:00 2001 From: Scott Rigby Date: Thu, 19 Jan 2012 17:01:37 -0500 Subject: [PATCH] Issue #1311930 by lesergi, scottrigby: Features support, including module defaults --- captcha.module | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 83 insertions(+), 0 deletions(-) diff --git a/captcha.module b/captcha.module index 19744a1..a6b822e 100644 --- a/captcha.module +++ b/captcha.module @@ -741,3 +741,86 @@ function captcha_captcha($op, $captcha_type = '') { break; } } + +/** + * Implementation of hook_features_api(). + */ +function captcha_features_api() { + return array( + 'captcha' => array( + 'name' => t('Captcha points'), + 'default_hook' => 'captcha_default_points', + 'default_file' => FEATURES_DEFAULTS_INCLUDED, + ), + ); +} + +/** + * Implementation of hook_features_export_options(). + */ +function captcha_features_export_options() { + $options = array(); + + $result = db_query("SELECT form_id FROM {captcha_points} ORDER BY form_id ASC"); + while($form_id = db_result($result)) { + $options[$form_id] = $form_id; + } + + return $options; +} + +/** + * Get captcha point given form_id. + */ +function _captcha_features_get_point($form_id) { + return db_fetch_array(db_query("SELECT * FROM {captcha_points} WHERE form_id = '%s'", $form_id)); +} + +/** + * Implementation of hook_features_export(). + */ +function captcha_features_export($data, &$export, $module_name = '') { + $export['dependencies']['captcha'] = 'captcha'; + + foreach($data as $form_id) { + $export['features']['captcha'][$form_id] = $form_id; + } +} + +/** + * Implementation of hook_features_export_render(). + */ +function captcha_features_export_render($module_name, $data) { + $code = array(); + + // Add module defaults to the $code array, since through + // captcha_captcha_default_points_alter() features expects these defaults to + // be rendered, and will complain if they're not. + drupal_alter('captcha_default_points', $code); + + // Add module default form_ids to the $data array, so we can overwrite each + // form_id if the module defaults are overidden in the database. + foreach (array_keys($code) as $form_id) { + $data[$form_id] = $form_id; + } + + foreach ($data as $form_id) { + $point = _captcha_features_get_point($form_id); + $code[$form_id] = $point; + } + + $code = ' return ' . features_var_export($code, ' ') . ';'; + return array('captcha_default_points' => $code); +} + +/** + * Implementation of hook_features_revert(). + */ +function captcha_features_revert($module) { + if ($defaults = features_get_default('captcha', $module)) { + foreach ($defaults as $form_id => $point) { + db_query("DELETE FROM {captcha_points} WHERE form_id = '%s'", $form_id); + drupal_write_record('captcha_points', $point); + } + } +} -- 1.7.5.4