FeedsConfigurable.inc

  1. cis7 sites/all/modules/ulmus/feeds/includes/FeedsConfigurable.inc
  2. cle7 sites/all/modules/ulmus/feeds/includes/FeedsConfigurable.inc
  3. ecd7 sites/all/modules/ulmus/feeds/includes/FeedsConfigurable.inc
  4. elmsmedia7 sites/all/modules/ulmus/feeds/includes/FeedsConfigurable.inc
  5. harmony7 sites/all/modules/ulmus/feeds/includes/FeedsConfigurable.inc
  6. icor7 sites/all/modules/ulmus/feeds/includes/FeedsConfigurable.inc
  7. meedjum_blog7 sites/all/modules/ulmus/feeds/includes/FeedsConfigurable.inc
  8. mooc7 sites/all/modules/ulmus/feeds/includes/FeedsConfigurable.inc

FeedsConfigurable and helper functions.

Functions

Namesort descending Description
feeds_form Config form callback. Don't call directly, but use feeds_get_form($configurable, 'method') instead.
feeds_form_submit Submit handler for feeds_form().
feeds_form_validate Validation handler for feeds_form().
feeds_get_form Config form wrapper. Use to render the configuration form of a FeedsConfigurable object.
_feeds_form_helper Helper for Feeds validate and submit callbacks.

Classes

Namesort descending Description
FeedsConfigurable Base class for configurable classes. Captures configuration handling, form handling and distinguishes between in-memory configuration and persistent configuration.
FeedsNotExistingException Used when an object does not exist in the DB or code but should.

File

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

Error

×

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/elmsln_community/api.elmsln.org/includes/common.inc:2791) in drupal_send_headers() (line 1499 of /var/www/html/elmsln_community/api.elmsln.org/includes/bootstrap.inc).
  • Error: Call to undefined function apc_delete() in DrupalAPCCache->clear() (line 289 of /var/www/html/elmsln_community/api.elmsln.org/sites/all/modules/apc/drupal_apc_cache.inc).
The website encountered an unexpected error. Please try again later.