You are here

FeedImportController.php in Feed Import 8

File

src/Controller/FeedImportController.php
View source
<?php

/**
 * @file
 * Contains \Drupal\feed_import\Controller\FeedImportController.
 */
namespace Drupal\feed_import\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Link;
use Drupal\feed_import_base\FeedImport;

/**
 * Controller routines for feed_import routes.
 */
class FeedImportController extends ControllerBase {

  /**
   * Export feed.
   *
   * @param string $feed
   *   Feed ID
   *
   * @return array
   *   Renderable array with a textarea containing the JSON used for an import.
   */
  function exportFeed($fid) {
    $feed = FeedImport::loadFeed($fid);
    unset($feed->id, $feed->last_run, $feed->last_run_duration, $feed->last_run_items, $feed->name, $feed->machine_name, $feed->cron_import);
    $opt = 0;
    if (defined('JSON_PRETTY_PRINT')) {
      $opt |= JSON_PRETTY_PRINT;
    }
    if (defined('JSON_UNESCAPED_SLASHES')) {
      $opt |= JSON_UNESCAPED_SLASHES;
    }
    $feed = json_encode($feed, $opt);
    return array(
      '#theme' => 'textarea',
      '#value' => $feed,
      '#rows' => 15,
    );
  }

  /**
   * Process a feed
   *
   * @param string $feed
   *   Feed machine name
   */
  function processFeed($fid) {
    $feed = FeedImport::loadFeed($fid);
    $status = _feed_import_base_process_feed($feed, TRUE);
    switch ($status) {
      case FeedImport::FEED_OK:
        drupal_set_message(t('Feed @name processed!', array(
          '@name' => $feed->name,
        )));
        break;
      case FeedImport::FEED_OVERLAP_ERR:
        drupal_set_message(t('Cannot process feed because another one is running!'), 'error');
        break;
      case FeedImport::FEED_CONFIG_ERR:
        drupal_set_message(t('Cannot process feed because source is misconfigured!'), 'error');
        break;
      case FeedImport::FEED_SOURCE_ERR:
        drupal_set_message(t('Cannot process feed because there is a source error!'), 'error');
        drupal_set_message(t('All items were rescheduled due to delete protection!'), 'warning');
        break;
      case FeedImport::FEED_ITEMS_ERR:
        drupal_set_message(t('Feed @name processed!', array(
          '@name' => $feed->name,
        )));
        drupal_set_message(t('All items were rescheduled due to delete protection!'), 'warning');
        break;
    }
    return $this
      ->redirect('feed_import.admin');
  }

  /**
   * Returns the name from a feed ID.
   *
   * @param string
   *   The machine name for a feed.
   *
   * @return string
   *   The human friendly name of a feed.
   */
  public function getTitle($feed_importer) {
    if ($feed_importer) {
      return $feed_importer
        ->label();
    }
    return 'Feed Import';
  }

}

Classes

Namesort descending Description
FeedImportController Controller routines for feed_import routes.