You are here

function feedapi_node_validate in FeedAPI 6

Build feed object on validate and submit. See feedapi_form_alter on finding out how it is called (via FormAPI)

1 string reference to 'feedapi_node_validate'
feedapi_form_alter in ./feedapi.module
Implementation of hook_form_alter().

File

./feedapi.module, line 584
Handle the submodules (for feed and item processing) Provide a basic management of feeds

Code

function feedapi_node_validate($form, &$form_state) {

  // Don't validate when deleting.
  if ($form_state['values']['op'] == t('Delete')) {
    return TRUE;
  }

  // Upload file.
  $feed_dir = file_directory_path() . '/feeds';
  file_check_directory($feed_dir, TRUE);
  $file = file_save_upload('feedapi', array(), $feed_dir);
  $has_upload = is_object($file);

  // Validate and transform settings for submission.
  if (empty($form_state['values']['feedapi']['feedapi_url']) && !$has_upload) {
    form_set_error('source', t('The Feed URL or uploading a file is required.'));
  }
  else {
    if (!empty($form_state['values']['feedapi']['feedapi_url']) && !$has_upload && strpos($form_state['values']['feedapi']['feedapi_url'], 'file://') === 0 && !user_access('use local files as feeds')) {
      form_set_error('source', t('You do not have sufficient permissions to use local files as feeds.'));
    }
    else {
      if (strpos($form_state['values']['feedapi']['feedapi_url'], 'file://') === 0 && !file_check_location(substr($form_state['values']['feedapi']['feedapi_url'], 7), file_directory_path())) {
        drupal_set_message(file_check_location(substr($form_state['values']['feedapi']['feedapi_url'], 7), file_directory_path()));
        form_set_error('source', t('file:// is only allowed for files under the files directory.'));
      }
      else {
        if ($has_upload) {
          $form_state['values']['feedapi']['feedapi_url'] = file_create_url($file->filepath);
        }
        $feed = _feedapi_build_feed_object($form_state['values']['type'], $form_state['values']['feedapi']['feedapi_url']);
        if (!isset($feed->title) && $has_upload) {
          $form_state['values']['feedapi']['feedapi_url'] = NULL;
        }

        // Stick feed object into feedapi form snippet - store it in submit.
        $form_state['values']['feedapi_object'] = $feed;
        if ($has_upload) {
          $form_state['values']['feedapi_object']->url = str_replace($GLOBALS['base_url'], '', $form_state['values']['feedapi_object']->url);
        }
        if (empty($form_state['values']['title']) && isset($feed->title)) {
          form_set_value($form['title'], $feed->title, $form_state);
        }
        if (isset($form['body_field']) && empty($form_state['values']['body']) && isset($feed->description)) {
          form_set_value($form['body_field']['body'], $feed->description, $form_state);
        }
        if (empty($form_state['values']['title'])) {
          if (!$has_upload) {
            form_set_error('title', t('Title could not be retrieved from feed.'));
          }
          else {
            form_set_error('title', t('Title could not be detected. Make sure that the uploaded file is a valid feed.'));
          }
        }
        elseif ($has_upload) {
          file_set_status($file, FILE_STATUS_PERMANENT);
        }
      }
    }
  }
}