id][$feed_nid])) { $instances[$class][$importer->id][$feed_nid] = new $class($importer, $feed_nid); } return $instances[$class][$importer->id][$feed_nid]; } /** * Constructor. */ protected function __construct(FeedsImporter $importer, $feed_nid) { $this->feed_nid = $feed_nid; $this->importer = $importer; parent::__construct($importer->id); $this->load(); } /** * Save configuration. */ public function save() { $config = $this->getConfig(); // Store the source property of the fetcher in a separate column so that we // can do fast lookups on it. $source = ''; if (isset($config[get_class($this->importer->fetcher)]['source'])) { $source = $config[get_class($this->importer->fetcher)]['source']; } $object = array( 'id' => $this->id, 'feed_nid' => $this->feed_nid, 'config' => $config, 'source' => $source, ); // Make sure a source record is present at all time, try to update first, // then insert. drupal_write_record('feeds_source', $object, array('id', 'feed_nid')); if (!db_affected_rows()) { drupal_write_record('feeds_source', $object); } } /** * Load configuration and unpack. */ public function load() { if ($config = db_result(db_query('SELECT config FROM {feeds_source} WHERE id = "%s" AND feed_nid = %d', $this->id, $this->feed_nid))) { // While FeedsSource cannot be exported, we still use CTool's export.inc // export definitions. // @todo: patch CTools to move constants from export.inc to ctools.module. ctools_include('export'); $this->export_type = EXPORT_IN_DATABASE; $this->config = unserialize($config); } } /** * Delete configuration. Removes configuration information * from database, does not delete configuration itself. */ public function delete() { db_query('DELETE FROM {feeds_source} WHERE id = "%s" AND feed_nid = %d', $this->id, $this->feed_nid); } /** * Convenience function. Returns the configuration for a specific class. * * @param FeedsSourceInterface $client * An object that is an implementer of FeedsSourceInterface. * * @return * An array stored for $client. */ public function getConfigFor(FeedsSourceInterface $client) { return $this->config[get_class($client)]; } /** * Return defaults for feed configuration. */ public function configDefaults() { // Collect information from plugins. $defaults = array(); foreach ($this->importer->plugin_types as $type) { if ($this->importer->$type->hasSourceConfig()) { $defaults[get_class($this->importer->$type)] = $this->importer->$type->sourceDefaults(); } } return $defaults; } /** * Override parent::configForm(). */ public function configForm(&$form_state) { // Collect information from plugins. $form = array(); foreach ($this->importer->plugin_types as $type) { if ($this->importer->$type->hasSourceConfig()) { $class = get_class($this->importer->$type); $form[$class] = $this->importer->$type->sourceForm($this->config[$class]); $form[$class]['#tree'] = TRUE; } } return $form; } /** * Override parent::configFormValidate(). */ public function configFormValidate(&$values) { foreach ($this->importer->plugin_types as $type) { $class = get_class($this->importer->$type); if (isset($values[$class]) && $this->importer->$type->hasSourceConfig()) { $this->importer->$type->sourceFormValidate($values[$class]); } } } }