<?php
// $Id: image_import.module,v 1.1.2.8 2007/07/24 17:34:14 drewish Exp $

/**
 * Implementation of hook_help().
 */
function image_import_help($section = '') {
  switch ($section) {
    case 'admin/content/image_import':
      $output = '<p>'. t("Import multiple image files and save them as image nodes. The files will be moved from their location into the image module's files directory. ")
        . t("Searching for image files in %dirpath.", array('%dirpath' => realpath(variable_get('image_import_path', '')))) .'</p>';
      return $output;
    case 'admin/settings/image_import':
      return t("Configure the image import module's settings.");
    default:
      return null;
  }
}

/**
 * Implementation of hook_perm().
 */
function image_import_perm() {
  return array('import images');
}

/**
 * Implementation of hook_menu().
 */
function image_import_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/content/image_import',
      'title' => t('Image import'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('image_import_form'),
      'access' => user_access('import images'),
      'type' => MENU_NORMAL_ITEM,
      'description' => t('Import images from the filesystem.')
    );
    $items[] = array(
      'path' => 'admin/settings/image_import',
      'title' => t('Image import'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('image_import_admin_settings'),
      'access' => user_access('administer site configuration'),
      'type' => MENU_NORMAL_ITEM,
      'description' => t('Change settings for the Image Import module.')
    );
  }

  return $items;
}

function image_import_form() {
  $form = array();

  $dirpath = variable_get('image_import_path', '');
  if (!file_check_directory($dirpath)) {
    drupal_set_message(t("You need to configure the import directory on the image import module's <a href='!admin-settings-image_import'>settings page</a>.", array('!admin-settings-image_import' => url('admin/settings/image_import'))), 'error');
    return $form;
  }

  $files = file_scan_directory($dirpath, '.jpg|.JPG|.JPEG|.jpeg|.flv|.FLV');
  ksort($files);

  if ($files) {
    if (module_exists('taxonomy')) {
      // here's a little hack to get the taxonmy controls onto our form
      $form['type'] = array('#type' => 'value', '#value' => 'image');
      $form['#node'] = new stdClass();
      $form['#node']->type = 'image';
      taxonomy_form_alter('image_node_form', $form);
      unset($form['type']);
      unset($form['#node']);
    }

    // Put the image files into an array for the checkboxes and gather
    // additional information like dimensions and filesizes. Make sure that
    // there's no 0th element, because a checkbox with a zero value is seen as
    // unchecked and won't be imported.
    $fields = array('filesize', 'dimensions', 'title', 'body','video');
    foreach ($fields as $field) {
      $form['files'][$field][0] = NULL;
    }
    $filelist = array(0 => NULL);

    foreach ($files as $file) {
    if (substr($file->filename, -3) == 'flv'){
      	$form['files']['video'][] = array(
          '#type' => 'hidden',
      	'#value' => 1,
        );
        $info['extension'] = substr($file->filename, -3);
        $info['width'] = '?';
        $info['height'] = '?';
        
      }else {
      	$form['files']['video'][] = array(
          '#type' => 'hidden',
          '#value' => 0,
        );
        $info = image_get_info($file->filename);
      }
      if ($info && isset($info['extension'])) {
        $filelist[] = substr($file->filename, strlen($dirpath) + 1);
        $form['files']['filesize'][] = array(
          '#type' => 'item',
          '#value' => format_size(filesize($file->filename)),
        );
        $form['files']['dimensions'][] = array(
          '#type' => 'item',
          '#value' => $info['width'] .'x'. $info['height'],
        );
        $form['files']['title'][] = array(
          '#type' => 'textfield',
          '#size' => 20,
          '#default_value' => basename($file->name),
        );
        $form['files']['body'][] = array(
          '#type' => 'textfield',
          '#size' => 20,
        );
      }
    }
    // Remove our 0 elements.
    unset($filelist[0]);
    foreach ($fields as $field) {
      $form['files'][$field][0] = NULL;
    }

    // Put the titles into an array.
    $form['files']['title']['#tree'] = TRUE;
    $form['files']['body']['#tree'] = TRUE;
    $form['files']['video']['#tree'] = TRUE;

    // Store a copy of the list into a form value so we can compare it to what
    // they submit and not have to worry about files being added or removed from
    // the filesystem.
    $form['file_list'] = array(
      '#type' => 'value',
      '#value' => $filelist,
    );
    $form['import_file'] = array(
      '#type' => 'checkboxes',
      '#options' => $filelist,
    );

    $form['buttons']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Import'),
    );
  }
  else {
    $form['import_file'] = array(
      '#type' => 'item',
      '#value' => t('No files were found'),
    );
  }

  return $form;
}

function theme_image_import_form($form) {
  $output = '';
  if (isset($form['import_file']) && $form['import_file']['#type'] == 'checkboxes') {
    $header = array(theme('table_select_header_cell'), t('Name'), t('Size'), t('Dimensions'), t('Title'), t('Body'));
    $rows = array();
    foreach (element_children($form['import_file']) as $key) {
      $filename = $form['import_file'][$key]['#title'];
      unset($form['import_file'][$key]['#title']);
      $rows[] = array(
        drupal_render($form['import_file'][$key]),
        $filename,
        drupal_render($form['files']['filesize'][$key]),
        drupal_render($form['files']['dimensions'][$key]),
        drupal_render($form['files']['title'][$key]),
        drupal_render($form['files']['body'][$key]),
        drupal_render($form['files']['video'][$key]),
      );
    }
    $output .= theme('table', $header, $rows);
  } else {
  	$output = '<a href="'.check_url($_SERVER['PHP_SELF']).'?q='.$_GET['q'].'&ftp_upload=true&keepThis=true&TB_iframe=true&height=500&width=520" title="'.$image->title.'" class="thickbox">'.t("Upload images").'</a>';
  }
if(isset ($_GET['ftp_upload'])){
  	$output = '<p align="center">
			  	<script language="Javascript" type="text/javascript">
			if(!navigator.javaEnabled()){
			alert(\''.t("You need to install the Java sun Applet.").'\');
			location.replace(\'http://www.java.com\');
			//window.open(\'instructionsforintallingjava.html\');
			}
			else{
			document.write(\'<applet code="ZUpload" archive="'.base_path().'modules/image/contrib/image_import/ZUpload.jar" width="450" height="300" border="0">\');
			document.write(\'<param name="host" value="'.variable_get('image_import_ftp-host', '').'">\');
			document.write(\'<param name="user" value="'.variable_get('image_import_ftp-user', '').'">\');
			document.write(\'<param name="pass" value="'.variable_get('image_import_ftp-password', '').'">\');
			document.write(\'<param name="path" value="'.variable_get('image_import_ftp-path', '').'">\');
			document.write(\'<param name="postscript" value="">\');
			document.write(\'</applet>\');
  			document.write(\'<p align="center"><ul><li>'.t("Click on Add Files").'</li>\');
			document.write(\'<li>'.t("Select your images or video files to upload").'</li>\');
			document.write(\'<li>'.t("Click on Upload Files").'</li></ul></p>\');
			document.write(\'<p align="center"><input style="width:200px" type="button" value="'.t("Save and close").'" onclick="top.document.location=top.document.location;" /></p>\');
			}
			</script>
			<noscript>
			Enabled javascript please
			</noscript>
			</p>';
  	return $output; 
}
  return $output . drupal_render($form);
}

function image_import_form_submit($form_id, $form_values) {
  $op = isset($form_values['op']) ? $form_values['op'] : '';
  if ($op == t('Import')) {
    $dirpath = variable_get('image_import_path', '');
    if (file_check_directory($dirpath)) {
      $nodes = array();
      $files = array();
      foreach (array_filter($form_values['import_file']) as $index) {
        // try to avoid php's script timeout with a bunch of large files or
        // a slow machine
        if (!ini_get('safe_mode')) {
          set_time_limit(0);
        }
        $origname = $form_values['file_list'][$index];
        $filename = file_check_location($dirpath .'/'. $origname, $dirpath);
      if ($filename) {
      	
        	if ($form_values['video'][$index] == 1){
        		drupal_set_message('ok');
        		$node = video_create_node_from(
	            $filename,
	            $form_values['title'][$index],
	            $form_values['body'][$index],
	            $form_values['taxonomy']
	          );
        	} else {
	          $node = image_create_node_from(
	            $filename,
	            $form_values['title'][$index],
	            $form_values['body'][$index],
	            $form_values['taxonomy']
	          );
        	}

          if ($node) {
            $nodes[] = t('%filename as <a href="!node-link">@node-title</a> <a href="!edit-link">[edit]</a>', array(
              '%filename' => $origname,
              '!node-link' => url('node/'. $node->nid),
              '@node-title' => $node->title,
              '!edit-link' => url('node/'. $node->nid .'/edit'),
            ));
          }
          else {
            drupal_set_message(t('Error importing %filename. '.$erreur, array('%filename' => $filename)));
          }
        }
      }

      // report back on our progress
      if (!empty($nodes)) {
        drupal_set_message(t('Successfully imported: ') . theme('item_list', $nodes));
      }
      else {
        drupal_set_message(t('No image files were imported.'));
      }
    }
  }
}

function image_import_admin_settings() {
  $form['image_import_path'] = array(
    '#type' => 'textfield',
    '#title' => t('Import path'),
    '#default_value' => variable_get('image_import_path', file_directory_path() .'/images/import'),
    '#after_build' => array('_image_import_settings_check_directory'),
    '#description' => t("The directory to import image nodes from. Drupal will need to have write access to this directory so we can move the file.") .'<br />'
      . t("<strong>Note:</strong> a path begining with a <kbd>/</kbd> indicates the path is relative to the server's root, not the website's root. One starting without a <kbd>/</kbd> specifies a path relative to Drupal's root. For example: <kbd>/tmp/image</kbd> would be the temp directory off the root while <kbd>tmp/image</kbd> would be inside Drupal's directory."),
    '#required' => TRUE,
  );
  $form['image_import_path_video'] = array(
    '#type' => 'textfield',
    '#title' => t('Import video Path'),
    '#default_value' => variable_get('image_import_path_video', 'files/media'),
    '#description' => t("Path for the videos"),
    '#required' => FALSE,
  );
$form['image_import_ftp-host'] = array(
    '#type' => 'textfield',
    '#title' => t('FTP Host'),
    '#default_value' => variable_get('image_import_ftp-host', ''),
    '#description' => t("Host of the ftp server (Applet Java)"),
    '#required' => FALSE,
  );
  $form['image_import_ftp-path'] = array(
    '#type' => 'textfield',
    '#title' => t('FTP path'),
    '#default_value' => variable_get('image_import_ftp-path', base_path().'files/image/import'),
    '#description' => t("Path ftp server (Applet Java): httpdocs/exemple"),
    '#required' => FALSE,
  );
  $form['image_import_ftp-user'] = array(
    '#type' => 'textfield',
    '#title' => t('FTP login'),
    '#default_value' => variable_get('image_import_ftp-user', ''),
    '#description' => t("Login for the ftp server (Applet Java)"),
    '#required' => FALSE,
  );
  $form['image_import_ftp-password'] = array(
    '#type' => 'password',
    '#title' => t('FTP password'),
    '#default_value' => variable_get('image_import_ftp-password', ''),
    '#description' => t("Password for the ftp server (Applet Java)"),
    '#required' => FALSE,
  );
  return system_settings_form($form);
}

/**
 * Checks the existence of the directory specified in $form_element.
 *
 * @param $form_element
 *   The form element containing the name of the directory to check.
 * @see system_check_directory()
 */
function _image_import_settings_check_directory($form_element) {
  $import_dir = $form_element['#value'];
  file_check_directory($import_dir, 0, $form_element['#parents'][0]);
  $image_dir = file_create_path(variable_get('image_default_path', 'images'));
  if (realpath($import_dir) == realpath($image_dir)) {
    form_set_error($form_element['#parents'][0], t("You can't import from the image module's directory. The import deletes the original files so you would just be asking for trouble."));
  }
  return $form_element;
}
