<?php
// $Id$

/**
 * @file
 * Allows users to upload local files and use them as FeedAPI feed URLs.
 */

/**
 * Implementation of hook_form_alter().
 */
function feedapi_fileupload_form_alter(&$form, $form_state, $form_id) {
  if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id && feedapi_enabled_type($form['type']['#value'])) {
    $node = $form['#node'];
    $form['attachments']['#title'] = t('FeedAPI: Upload a local file as a feed');
    $form['attachments']['#weight'] = 0;
    $form['attachments']['#description'] = t('Leave the FeedAPI URL empty and upload a file here if you would like to use a local file as a feed. The FeedAPI URL field will be populated automatically after the upload.');
  }
  if ($form_id == 'upload_js') {
    $keys = array_keys($form['files']);
    $file = $form['files'][array_pop($keys)];
    $fileurl = strip_tags($file['description']['#description']);
    $form['#suffix'] = '<script type="text/javascript">$("#edit-feedapi-feedapi-url").val("'. $fileurl .'");</script>';
  }
}
