You are here

class FeedsFileFetcher in Feeds 7

Same name and namespace in other branches
  1. 6 plugins/FeedsFileFetcher.inc \FeedsFileFetcher
  2. 7.2 plugins/FeedsFileFetcher.inc \FeedsFileFetcher

Fetches data via HTTP.

Hierarchy

Expanded class hierarchy of FeedsFileFetcher

11 string references to 'FeedsFileFetcher'
FeedsCSVtoTermsTest::test in tests/feeds_processor_term.test
Test node creation, refreshing/deleting feeds and feed items.
FeedsCSVtoUsersTest::test in tests/feeds_processor_user.test
Test node creation, refreshing/deleting feeds and feed items.
FeedsMapperContentTestCase::test in tests/feeds_mapper_content.test
Basic test loading a doulbe entry CSV file.
FeedsMapperDateTestCase::test in tests/feeds_mapper_date.test
Basic test loading a single entry CSV file.
FeedsMapperEmfieldTestCase::test in tests/feeds_mapper_emfield.test
Basic test loading a doulbe entry CSV file.

... See full list

File

plugins/FeedsFileFetcher.inc, line 42
Home of the FeedsFileFetcher and related classes.

View source
class FeedsFileFetcher extends FeedsFetcher {

  /**
   * Implements FeedsFetcher::fetch().
   */
  public function fetch(FeedsSource $source) {
    $source_config = $source
      ->getConfigFor($this);
    return new FeedsFileBatch($source_config['source'], $source->feed_nid);
  }

  /**
   * Source form.
   */
  public function sourceForm($source_config) {
    $form = array();
    $form['fid'] = array(
      '#type' => 'value',
      '#value' => empty($source_config['fid']) ? 0 : $source_config['fid'],
    );
    $form['source'] = array(
      '#type' => empty($this->config['direct']) ? 'value' : 'textfield',
      '#title' => t('File'),
      '#description' => t('Specify a file in the site\'s file system path or upload a file below.'),
      '#default_value' => empty($source_config['source']) ? '' : $source_config['source'],
    );
    $form['upload'] = array(
      '#type' => 'file',
      '#title' => empty($this->config['direct']) ? t('File') : NULL,
      '#description' => empty($source_config['source']) ? t('Select the file to be imported from your local system.') : t('Select a different file to be imported from your local system.'),
      '#theme' => 'feeds_upload',
      '#file_info' => empty($source_config['fid']) ? NULL : file_load($source_config['fid']),
      '#size' => 10,
    );
    return $form;
  }

  /**
   * Override parent::sourceFormValidate().
   */
  public function sourceFormValidate(&$values) {
    $feed_dir = 'public://feeds';
    file_prepare_directory($feed_dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);

    // If there is a file uploaded, save it, otherwise validate input on
    // file.
    // @todo: Track usage of file, remove file when removing source.
    if ($file = file_save_upload('feeds', array(
      'file_validate_extensions' => array(
        0 => $this->config['allowed_extensions'],
      ),
    ), $feed_dir)) {
      $values['source'] = $file->uri;
      $values['file'] = $file;
    }
    elseif (empty($values['source'])) {
      form_set_error('feeds][source', t('Upload a file first.'));
    }
    elseif (strpos($values['source'], 'public://') !== 0) {
      form_set_error('feeds][source', t('File needs to reside within the site\'s file directory, its path needs to start with public://.'));
    }
  }

  /**
   * Override parent::sourceSave().
   */
  public function sourceSave(FeedsSource $source) {
    $source_config = $source
      ->getConfigFor($this);

    // If a new file is present, delete the old one and replace it with the new
    // one.
    if (isset($source_config['file'])) {
      $file = $source_config['file'];
      if (isset($source_config['fid'])) {
        $this
          ->deleteFile($source_config['fid'], $source->feed_nid);
      }
      $file->status = FILE_STATUS_PERMANENT;
      file_save($file);
      file_usage_add($file, 'feeds', get_class($this), $source->feed_nid);
      $source_config['fid'] = $file->fid;
      unset($source_config['file']);
      $source
        ->setConfigFor($this, $source_config);
    }
  }

  /**
   * Override parent::sourceDelete().
   */
  public function sourceDelete(FeedsSource $source) {
    $source_config = $source
      ->getConfigFor($this);
    if (isset($source_config['fid'])) {
      $this
        ->deleteFile($source_config['fid'], $source->feed_nid);
    }
  }

  /**
   * Override parent::configDefaults().
   */
  public function configDefaults() {
    return array(
      'allowed_extensions' => 'txt csv xml',
      'direct' => FALSE,
    );
  }

  /**
   * Override parent::configForm().
   */
  public function configForm(&$form_state) {
    $form = array();
    $form['allowed_extensions'] = array(
      '#type' => 'textfield',
      '#title' => t('Allowed file extensions'),
      '#description' => t('Allowed file extensions for upload.'),
      '#default_value' => $this->config['allowed_extensions'],
    );
    $form['direct'] = array(
      '#type' => 'checkbox',
      '#title' => t('Supply path to file directly'),
      '#description' => t('For experts. If checked users can specify a path to a file when importing rather than uploading a file. This is useful when files to be imported are already present on server.'),
      '#default_value' => $this->config['direct'],
    );
    return $form;
  }

  /**
   * Helper. Deletes a file.
   */
  protected function deleteFile($fid, $feed_nid) {
    if ($file = file_load($fid)) {
      file_usage_delete($file, 'feeds', get_class($this), $feed_nid);
      file_delete($file);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FeedsConfigurable::$config protected property
FeedsConfigurable::$disabled protected property CTools export enabled status of this object.
FeedsConfigurable::$export_type protected property
FeedsConfigurable::$id protected property
FeedsConfigurable::addConfig public function Similar to setConfig but adds to existing configuration.
FeedsConfigurable::configFormSubmit public function Submission handler for configForm(). 2
FeedsConfigurable::configFormValidate public function Validation handler for configForm(). 3
FeedsConfigurable::copy public function Copy a configuration. 1
FeedsConfigurable::existing public function Determine whether this object is persistent and enabled. I. e. it is defined either in code or in the database and it is enabled. 1
FeedsConfigurable::getConfig public function Implements getConfig(). 1
FeedsConfigurable::instance public static function Instantiate a FeedsConfigurable object. 1
FeedsConfigurable::setConfig public function Set configuration.
FeedsConfigurable::__get public function Override magic method __get(). Make sure that $this->config goes through getConfig()
FeedsFetcher::clear public function Clear all caches for results for given source. 1
FeedsFetcher::importPeriod public function Override import period settings. This can be used to force a certain import interval. 1
FeedsFetcher::menuItem public function Menu item definition for fetchers of this class. Note how the path component in the item definition matches the return value of FeedsFetcher::path();
FeedsFetcher::path public function Construct a path for a concrete fetcher/source combination. The result of this method matches up with the general path definition in FeedsFetcher::menuItem(). For example usage look at FeedsHTTPFetcher.
FeedsFetcher::request public function Request handler invoked if callback URL is requested. Locked down by default. For a example usage see FeedsHTTPFetcher. 1
FeedsFetcher::subscribe public function Subscribe to a source. Only implement if fetcher requires subscription. 1
FeedsFetcher::unsubscribe public function Unsubscribe from a source. Only implement if fetcher requires subscription. 1
FeedsFileFetcher::configDefaults public function Override parent::configDefaults(). Overrides FeedsConfigurable::configDefaults
FeedsFileFetcher::configForm public function Override parent::configForm(). Overrides FeedsConfigurable::configForm
FeedsFileFetcher::deleteFile protected function Helper. Deletes a file.
FeedsFileFetcher::fetch public function Implements FeedsFetcher::fetch(). Overrides FeedsFetcher::fetch
FeedsFileFetcher::sourceDelete public function Override parent::sourceDelete(). Overrides FeedsPlugin::sourceDelete
FeedsFileFetcher::sourceForm public function Source form. Overrides FeedsPlugin::sourceForm
FeedsFileFetcher::sourceFormValidate public function Override parent::sourceFormValidate(). Overrides FeedsPlugin::sourceFormValidate
FeedsFileFetcher::sourceSave public function Override parent::sourceSave(). Overrides FeedsPlugin::sourceSave
FeedsPlugin::all public static function Get all available plugins.
FeedsPlugin::byType public static function Gets all available plugins of a particular type.
FeedsPlugin::child public static function Determines whether given plugin is derived from given base plugin.
FeedsPlugin::hasSourceConfig public function Returns TRUE if $this->sourceForm() returns a form. Overrides FeedsSourceInterface::hasSourceConfig
FeedsPlugin::loadMappers protected static function Loads on-behalf implementations from mappers/ directory.
FeedsPlugin::save public function Save changes to the configuration of this object. Delegate saving to parent (= Feed) which will collect information from this object by way of getConfig() and store it. Overrides FeedsConfigurable::save
FeedsPlugin::sourceDefaults public function Implements FeedsSourceInterface::sourceDefaults(). Overrides FeedsSourceInterface::sourceDefaults 1
FeedsPlugin::typeOf public static function Determines the type of a plugin.
FeedsPlugin::__construct protected function Constructor. Overrides FeedsConfigurable::__construct