[177a560] | 1 | <?php |
---|
| 2 | |
---|
| 3 | /** |
---|
| 4 | * @file |
---|
| 5 | * FeedsConfigurable and helper functions. |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | /** |
---|
| 9 | * Used when an object does not exist in the DB or code but should. |
---|
| 10 | */ |
---|
| 11 | class FeedsNotExistingException extends Exception { |
---|
| 12 | } |
---|
| 13 | |
---|
| 14 | /** |
---|
| 15 | * Base class for configurable classes. Captures configuration handling, form |
---|
| 16 | * handling and distinguishes between in-memory configuration and persistent |
---|
| 17 | * configuration. |
---|
| 18 | */ |
---|
| 19 | abstract class FeedsConfigurable { |
---|
| 20 | |
---|
| 21 | // Holds the actual configuration information. |
---|
| 22 | protected $config; |
---|
| 23 | |
---|
| 24 | // A unique identifier for the configuration. |
---|
| 25 | protected $id; |
---|
| 26 | |
---|
| 27 | /* |
---|
| 28 | CTools export type of this object. |
---|
| 29 | |
---|
| 30 | @todo Should live in FeedsImporter. Not all child classes |
---|
| 31 | of FeedsConfigurable are exportable. Same goes for $disabled. |
---|
| 32 | |
---|
| 33 | Export type can be one of |
---|
| 34 | FEEDS_EXPORT_NONE - the configurable only exists in memory |
---|
| 35 | EXPORT_IN_DATABASE - the configurable is defined in the database. |
---|
| 36 | EXPORT_IN_CODE - the configurable is defined in code. |
---|
| 37 | EXPORT_IN_CODE | EXPORT_IN_DATABASE - the configurable is defined in code, but |
---|
| 38 | overridden in the database.*/ |
---|
| 39 | protected $export_type; |
---|
| 40 | |
---|
| 41 | /** |
---|
| 42 | * CTools export enabled status of this object. |
---|
| 43 | */ |
---|
| 44 | protected $disabled; |
---|
| 45 | |
---|
| 46 | /** |
---|
| 47 | * Instantiate a FeedsConfigurable object. |
---|
| 48 | * |
---|
| 49 | * Don't use directly, use feeds_importer() or feeds_plugin_instance() |
---|
| 50 | * instead. |
---|
| 51 | */ |
---|
| 52 | public static function instance($class, $id) { |
---|
| 53 | // This is useful at least as long as we're developing. |
---|
| 54 | if (empty($id)) { |
---|
| 55 | throw new Exception(t('Empty configuration identifier.')); |
---|
| 56 | } |
---|
| 57 | static $instances = array(); |
---|
| 58 | if (!isset($instances[$class][$id])) { |
---|
| 59 | $instances[$class][$id] = new $class($id); |
---|
| 60 | } |
---|
| 61 | return $instances[$class][$id]; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | /** |
---|
| 65 | * Constructor, set id and load default configuration. |
---|
| 66 | */ |
---|
| 67 | protected function __construct($id) { |
---|
| 68 | // Set this object's id. |
---|
| 69 | $this->id = $id; |
---|
| 70 | // Per default we assume that a Feeds object is not saved to |
---|
| 71 | // database nor is it exported to code. |
---|
| 72 | $this->export_type = FEEDS_EXPORT_NONE; |
---|
| 73 | // Make sure configuration is populated. |
---|
| 74 | $this->config = $this->configDefaults(); |
---|
| 75 | $this->disabled = FALSE; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | /** |
---|
| 79 | * Override magic method __isset(). This is needed due to overriding __get(). |
---|
| 80 | */ |
---|
| 81 | public function __isset($name) { |
---|
| 82 | return isset($this->$name) ? TRUE : FALSE; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | /** |
---|
| 86 | * Determine whether this object is persistent and enabled. I. e. it is |
---|
| 87 | * defined either in code or in the database and it is enabled. |
---|
| 88 | */ |
---|
| 89 | public function existing() { |
---|
| 90 | if ($this->export_type == FEEDS_EXPORT_NONE) { |
---|
| 91 | throw new FeedsNotExistingException(t('Object is not persistent.')); |
---|
| 92 | } |
---|
| 93 | if ($this->disabled) { |
---|
| 94 | throw new FeedsNotExistingException(t('Object is disabled.')); |
---|
| 95 | } |
---|
| 96 | return $this; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /** |
---|
| 100 | * Save a configuration. Concrete extending classes must implement a save |
---|
| 101 | * operation. |
---|
| 102 | */ |
---|
| 103 | public abstract function save(); |
---|
| 104 | |
---|
| 105 | /** |
---|
| 106 | * Copy a configuration. |
---|
| 107 | */ |
---|
| 108 | public function copy(FeedsConfigurable $configurable) { |
---|
| 109 | $this->setConfig($configurable->config); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | /** |
---|
| 113 | * Set configuration. |
---|
| 114 | * |
---|
| 115 | * @param $config |
---|
| 116 | * Array containing configuration information. Config array will be filtered |
---|
| 117 | * by the keys returned by configDefaults() and populated with default |
---|
| 118 | * values that are not included in $config. |
---|
| 119 | */ |
---|
| 120 | public function setConfig($config) { |
---|
| 121 | $defaults = $this->configDefaults(); |
---|
| 122 | $this->config = array_intersect_key($config, $defaults) + $defaults; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | /** |
---|
| 126 | * Similar to setConfig but adds to existing configuration. |
---|
| 127 | * |
---|
| 128 | * @param $config |
---|
| 129 | * Array containing configuration information. Will be filtered by the keys |
---|
| 130 | * returned by configDefaults(). |
---|
| 131 | */ |
---|
| 132 | public function addConfig($config) { |
---|
| 133 | $this->config = is_array($this->config) ? array_merge($this->config, $config) : $config; |
---|
| 134 | $default_keys = $this->configDefaults(); |
---|
| 135 | $this->config = array_intersect_key($this->config, $default_keys); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | /** |
---|
| 139 | * Override magic method __get(). Make sure that $this->config goes |
---|
| 140 | * through getConfig() |
---|
| 141 | */ |
---|
| 142 | public function __get($name) { |
---|
| 143 | if ($name == 'config') { |
---|
| 144 | return $this->getConfig(); |
---|
| 145 | } |
---|
| 146 | return $this->{$name}; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | /** |
---|
| 150 | * Implementation of getConfig(). |
---|
| 151 | */ |
---|
| 152 | public function getConfig() { |
---|
| 153 | return $this->config; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | /** |
---|
| 157 | * Return default configuration. |
---|
| 158 | * |
---|
| 159 | * @todo rename to getConfigDefaults(). |
---|
| 160 | * |
---|
| 161 | * @return |
---|
| 162 | * Array where keys are the variable names of the configuration elements and |
---|
| 163 | * values are their default values. |
---|
| 164 | */ |
---|
| 165 | public function configDefaults() { |
---|
| 166 | return array(); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | /** |
---|
| 170 | * Return configuration form for this object. The keys of the configuration |
---|
| 171 | * form must match the keys of the array returned by configDefaults(). |
---|
| 172 | * |
---|
| 173 | * @return |
---|
| 174 | * FormAPI style form definition. |
---|
| 175 | */ |
---|
| 176 | public function configForm(&$form_state) { |
---|
| 177 | return array(); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | /** |
---|
| 181 | * Validation handler for configForm(). |
---|
| 182 | * |
---|
| 183 | * Set errors with form_set_error(). |
---|
| 184 | * |
---|
| 185 | * @param $values |
---|
| 186 | * An array that contains the values entered by the user through configForm. |
---|
| 187 | */ |
---|
| 188 | public function configFormValidate(&$values) { |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | /** |
---|
| 192 | * Submission handler for configForm(). |
---|
| 193 | * |
---|
| 194 | * @param $values |
---|
| 195 | */ |
---|
| 196 | public function configFormSubmit(&$values) { |
---|
| 197 | $this->addConfig($values); |
---|
| 198 | $this->save(); |
---|
| 199 | drupal_set_message(t('Your changes have been saved.')); |
---|
| 200 | feeds_cache_clear(FALSE); |
---|
| 201 | } |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | /** |
---|
| 205 | * Config form wrapper. Use to render the configuration form of |
---|
| 206 | * a FeedsConfigurable object. |
---|
| 207 | * |
---|
| 208 | * @param $configurable |
---|
| 209 | * FeedsConfigurable object. |
---|
| 210 | * @param $form_method |
---|
| 211 | * The form method that should be rendered. |
---|
| 212 | * |
---|
| 213 | * @return |
---|
| 214 | * Rendered config form, if available. Empty string otherwise. |
---|
| 215 | */ |
---|
| 216 | function feeds_get_form($configurable, $form_method) { |
---|
| 217 | $form_state = array(); |
---|
| 218 | if (method_exists($configurable, $form_method)) { |
---|
| 219 | return drupal_get_form(get_class($configurable) .'_feeds_form', $configurable, $form_method); |
---|
| 220 | } |
---|
| 221 | return ''; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | /** |
---|
| 225 | * Config form callback. Don't call directly, but use |
---|
| 226 | * feeds_get_form($configurable, 'method') instead. |
---|
| 227 | * |
---|
| 228 | * @param |
---|
| 229 | * FormAPI $form_state. |
---|
| 230 | * @param |
---|
| 231 | * FeedsConfigurable object. |
---|
| 232 | * @param |
---|
| 233 | * The object to perform the save() operation on. |
---|
| 234 | * @param $form_method |
---|
| 235 | * The $form_method that should be rendered. |
---|
| 236 | */ |
---|
| 237 | function feeds_form(&$form_state, $configurable, $form_method) { |
---|
| 238 | $form = $configurable->$form_method($form_state); |
---|
| 239 | $form['#configurable'] = $configurable; |
---|
| 240 | $form['#feeds_form_method'] = $form_method; |
---|
| 241 | $form['#validate'] = array('feeds_form_validate'); |
---|
| 242 | $form['#submit'] = array('feeds_form_submit'); |
---|
| 243 | $form['submit'] = array( |
---|
| 244 | '#type' => 'submit', |
---|
| 245 | '#value' => t('Save'), |
---|
| 246 | '#weight' => 100, |
---|
| 247 | ); |
---|
| 248 | return $form; |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | /** |
---|
| 252 | * Validation handler for feeds_form(). |
---|
| 253 | */ |
---|
| 254 | function feeds_form_validate($form, &$form_state) { |
---|
| 255 | $validate_method = $form['#feeds_form_method'] .'Validate'; |
---|
| 256 | if (method_exists($form['#configurable'], $validate_method)) { |
---|
| 257 | $form['#configurable']->$validate_method($form_state['values']); |
---|
| 258 | } |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | /** |
---|
| 262 | * Submit handler for feeds_config_form(). |
---|
| 263 | */ |
---|
| 264 | function feeds_form_submit($form, &$form_state) { |
---|
| 265 | $submit_method = $form['#feeds_form_method'] .'Submit'; |
---|
| 266 | if (method_exists($form['#configurable'], $submit_method)) { |
---|
| 267 | $form['#configurable']->$submit_method($form_state['values']); |
---|
| 268 | } |
---|
| 269 | } |
---|