[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/image_fupload/image_fupload_image/ -> image_fupload_image.module (source)

   1  <?php
   2  // $Id: image_fupload_image.module,v 1.35 2009/03/21 08:42:57 grandcat Exp $
   3  
   4  /**
   5   * Implementation of hook_menu().
   6   */
   7  function image_fupload_image_menu() {
   8    $items['admin/settings/image/image_fupload'] = array(
   9      'title' => 'Image FUpload Settings',
  10      'access arguments' => array('administer site configuration'),
  11      'page callback' => 'drupal_get_form',
  12      'page arguments' => array('image_fupload_image_admin'),
  13      'description' => 'Configure FUpload Settings',
  14      'type' => MENU_LOCAL_TASK,
  15      'file' => 'image_fupload_image.admin.inc',
  16    );
  17    $items['fupload/js/image'] = array(
  18      'title' => 'Image FUpload',
  19      'page callback' => 'image_fupload_image_node_create',
  20      'access arguments' => array('mass upload images'),
  21      'type' => MENU_CALLBACK,
  22      'file' => 'node.pages.inc',
  23      'file path' => drupal_get_path('module', 'node'),
  24    );  
  25    return $items;
  26  }
  27  
  28  /**
  29   * Implementation of hook_form_alter() registry.
  30   */
  31  function image_fupload_image_form_alter(&$form, $form_state, $form_id) {
  32    global $user;  
  33    // examine whether it's a new image type & the user wants to have flash
  34    if ($form_id == "image_node_form" && !isset($form['#node']->nid) && arg(0) != 'img_assist' && arg(3) != "noflash" && arg(3) != "list_images" && user_access('mass upload images')) {    
  35      // Some needed JS & CSS - Files to be included
  36      $module_path = drupal_get_path('module', 'image_fupload');
  37        drupal_add_js($module_path .'/swfupload/swfupload.js', 'module');
  38        drupal_add_js($module_path .'/swfupload/swfupload.queue.js', 'module');
  39        drupal_add_js($module_path .'/swfupload/fileprogress.js', 'module');
  40        drupal_add_js($module_path .'/swfupload/handlers.js', 'module');
  41      /* Check if another step with editing options is provided  for this user*/
  42      $redirect_url = '';
  43      if (user_access('edit captions'))
  44        $redirect_url = url('node/add/image/list_images');
  45      /* Check END */
  46        drupal_add_js(theme('swfupload_settings', base_path() . $module_path, url('fupload/flash'), round(variable_get('image_max_upload_size', 800)), '*.jpg; *.jpeg; *.png; *.gif', (!empty($user->sid) ? $user->sid : session_id()), 100, 'image', 'images', 'true', '', $redirect_url), 'inline');
  47      drupal_add_css($module_path .'/image_fupload-style.css', 'module', 'all', FALSE);  // Style of swfUpload
  48          
  49      unset($form['title'], $form['rebuild_images'], $form['buttons']); //don't need this, title will be generated later using filepath
  50      
  51      isset($form['taxonomy']) ? $form['taxonomy']['#weight'] = -8 : ""; //adds ablility to sort better in this case    
  52      
  53      // if user is allowed to mass edit captions, so let's prepare the form to be able to perform further steps later
  54      if (user_access('edit captions')) {
  55        $form['body_field']['body']['#disabled'] = TRUE; // not accessable when editing done by caption list
  56        $form['body_field']['#prefix'] = '<div class="body-field-wrapper" style="display: none;">';  
  57        // Little Hack: Validation fails if body is hidden and wordcount > 0 ==> little hack, look at function "fupload_node_form_validate"
  58        $form['#validate'][0] = 'fupload_node_form_validate';
  59      
  60        $form['options']['status']['#disabled'] = TRUE;
  61        $form['options']['status']['#default_value'] = 0;
  62      }    
  63      
  64      $form['message'] = array(
  65        '#value' => '<div id="jsstatus"></div>',
  66        '#weight' => -7,
  67      );
  68      $form['image'] = array(
  69        '#type' => 'fieldset',
  70        '#title' => t('Images'),
  71      '#value' => '<span style="font-style:italic;">' .t('Click the "Select Images" icon on the left below to begin.') .'</span>',
  72        '#weight' => -6,
  73      '#attributes' => array('class' => 'flash', 'id' => 'fsUploadProgress'),
  74        '#collapsible' => FALSE,
  75        '#collapsed' => FALSE,
  76        );
  77      $form['upload_info'] = array(
  78        '#value' => t('0 Files uploaded.'),
  79        '#prefix' => '<div id="divStatus">',
  80        '#suffix' => '</div>',
  81        '#weight' => -5,
  82        );
  83        // Drupal 6 Bug: can't use "normal" buttons, only submit buttons are possible => workaround
  84      // Select Button has no real function, but Flash Player (swfUpload) overlays it with an transparent effect handler
  85        $form['upload_buttons'] = array(
  86          '#prefix' => '<div>',
  87          '#value' => '<span id="spanUploadButton"></span> <input id="btnCancel" type="button" value="'.t('Cancel All Uploads').'" onclick="swfu.cancelQueue();" disabled="disabled" /> ',    
  88          '#suffix' => '</div>',
  89          '#weight' => -4,
  90        );
  91      $form['upload_buttons']['node_create'] = array(
  92        '#type' => 'submit',
  93        '#value' => t('Process queued images'),
  94        '#weight' => -3,
  95        '#ahah' => array(
  96          'path' => 'fupload/js/image',
  97          'event' => 'click',
  98          'method' => 'replace',
  99          'wrapper' => 'jsstatus',
 100          'progress' => array('type' => 'bar', 'message' => t('Images in queue are processed...')),
 101        ),
 102        );
 103      $form['upload_buttons']['delete_queue'] = array(
 104        '#type' => 'submit',
 105        '#value' => t('Delete queued images'),
 106        '#weight' => -2,
 107        '#ahah' => array(
 108          'path' => 'fupload/js/deletequeue/images',
 109          'event' => 'click',
 110          'method' => 'append',
 111          'wrapper' => 'jsstatus',
 112        ),
 113        );
 114  
 115      $form['upload_buttons']['submit'] = array(
 116        '#value' => '<input type="button" value="'.t('Save').'" id="startuploadbutton" onclick="startUploadProcess()" />',
 117        '#weight' => -1,
 118        '#submit' => array('node_form_submit'),
 119      );    
 120      
 121      $form['#redirect'] = FALSE; // Important that $_POST is not empty after browser submit
 122      $form['#submit'][0] = '_image_node_form_submit'; // use our emulated version instead of original one in image.module
 123    }
 124  }
 125  
 126  function image_fupload_image_node_create() {
 127    global $user;
 128    global $file_cache;
 129    
 130    // Remove images which couldn't be processed completly (--> mostly because of memory excaustion) --> real removal done by cron
 131    db_query("UPDATE {files} SET filename = '%s' WHERE uid = %d AND status = %d AND filename = '%s'", image_fupload_image_status('images', IMAGE_PROCESSED), $user->uid, FILE_STATUS_TEMPORARY, image_fupload_image_status('images', IMAGE_HALFPROCESSED));
 132      
 133    // Get some POST Variables
 134    $form_build_id = $_POST['form_build_id'];
 135    $form_id = $_POST['form_id'];
 136    
 137    if (isset($form_build_id) && isset($form_id)) {
 138      $form_error = 0;
 139      $message = '';
 140      
 141      // Load the form from the Form API cache and check if valid
 142      $form_state = array('rebuild' => TRUE, 'values' => $_POST); // rebuild option needed to prevent that "_image_node_form_submit" gets executed by drupal_process_form
 143      if (!($form = form_get_cache($form_build_id, $form_state))) {
 144        // code based on upload.module (15/08/2008)
 145        form_set_error('form_token', t('Validation error, please try again. If this error persists, please contact the site administrator.'));
 146        $output = theme('status_messages');
 147        drupal_json(array('status' => TRUE, 'data' => $output));
 148        exit();
 149      }
 150      
 151      // Some form manipulations
 152      $form['#post'] = $_POST;
 153      // Default Value; Title will be overwritten in the next steps
 154      $form['#post']['title'] = 'Image';
 155      $form['#post']['form_id'] = $form['form_id']['#value'];
 156      
 157      // New generated fields
 158      $form['title'] = array('#type' => 'textfield', '#title' => 'Image', '#default_value' => ''); // Needed that validation is successful
 159      // for preview list: body will be replaced
 160      if (user_access('edit captions'))
 161        $form['body_field']['body'] = array('#type' => 'textarea', '#title' => 'Body', '#default_value' => image_fupload_image_status('images', IMAGE_NOT_COMPLETED));
 162      
 163      drupal_process_form($form_id, $form, $form_state); // Only validate input data
 164      if (!form_get_errors()) {
 165        $result = db_query_range("SELECT * FROM {files} WHERE uid = %d AND status = %d AND filename = '%s'", $user->uid, FILE_STATUS_TEMPORARY, image_fupload_image_status('images', IMAGE_UNMACHINED), 0, 3);
 166        while ($image = db_fetch_object($result)) {
 167          // ahh.. first image to process
 168          $file_cache['image'] = $image; // Add image to cache for image.module
 169          
 170          // Set status flag on image (work on picture can begin); if there are problems with this image, it will be kicked next time
 171          db_query("UPDATE {files} SET filename = '%s' WHERE fid = %d", image_fupload_image_status('images', IMAGE_HALFPROCESSED), $image->fid);
 172          
 173          // check the file against all validators a 2. second time because Flash could eventuelly be bypassed
 174          $validators = array(
 175            'file_validate_is_image' => array(),
 176            'file_validate_size' => array(variable_get('image_max_upload_size', 800) * 1024),
 177          );  
 178          // Validation code taken from Drupal's function "file_save_upload" (D6 24/08/2008)        
 179          // Call the validation functions.
 180          file_validate($image, $validators);
 181                  
 182          // no errors during validation, let's continue
 183          if (!form_get_errors()) {        
 184            // Create a filename out of the given image information; used a theme function so that it can be customised; mapping new title
 185            $form['title']['#value'] = theme('fupload_create_filename', $image);
 186            $form_state['values']['title'] = $form['title']['#value'];
 187  
 188            // for preview list if bodyfield is deactivated by default
 189            if ($form['body_field']['body']['#default_value'] == image_fupload_image_status('images', IMAGE_NOT_COMPLETED)) {
 190              $form['body']['#value'] = image_fupload_image_status('images', IMAGE_NOT_COMPLETED);
 191              $form_state['values']['body'] = image_fupload_image_status('images', IMAGE_NOT_COMPLETED);
 192            }
 193          
 194            node_form_submit($form, $form_state); // Submit form --> Save it
 195          
 196            // Prevent that same image is processed twice; deleted later by cron
 197            db_query("UPDATE {files} SET filename = '%s' WHERE fid = %d", image_fupload_image_status('images', IMAGE_PROCESSED), $image->fid);
 198          }
 199        } 
 200        // no files in cache, so no files have been processed yet because of empty queue
 201        if(empty($file_cache['image']) && !form_get_errors())
 202          drupal_set_message(t('No images yet in queue.'));      
 203        
 204        // Examine how many images are left in queue and inform JS by sending a hidden element
 205        $result = db_fetch_object(db_query("SELECT COUNT(*) AS img_count FROM {files} WHERE uid = %d AND status = %d AND filename = '%s'", $user->uid, FILE_STATUS_TEMPORARY, image_fupload_image_status('images', IMAGE_UNMACHINED)));
 206        $message .= '<input type="hidden" name="num_queued_images" id="num_queued_images" value="' . $result->img_count . '" />';    
 207      }
 208      if (form_get_errors()) {
 209        // Error in received form (for example a required field was not filled) or during validation; inform JS => user
 210        $form_error = 1;
 211        $message .= '<input type="hidden" name="num_queued_images" id="num_queued_images" value="0" />';
 212      }
 213      
 214      // Inform JS about errors
 215      $message .= '<input type="hidden" name="form_errors" id="form_errors" value="' . $form_error . '"  />';
 216      $message .= theme('status_messages'); // Theme all messages
 217      
 218      drupal_json(array('status' => TRUE, 'data' => $message));
 219    
 220    } else {
 221      drupal_json(array('status' => FALSE, 'data' => t('Error: No or wrong POST Data')));
 222    }  
 223  }
 224  
 225  /*
 226  * Helper function which emulates "image_node_form_submit" of image.module v 1.274 2008/06/16 18:45:58 drewish
 227  * Need this because the static variable "$upload_cache" doesn't seem to persist after execution of "form_execute_handlers" function
 228  */
 229  function _image_node_form_submit($form, &$form_state) {
 230    global $file_cache;
 231      
 232    $nid = 'new_node'; // in this case, we can define it permanentelly  
 233    $file = $file_cache['image']; // fill our object with picture information
 234      
 235    // Code from image.module v 1.274 2008/06/16 18:45:58 drewish (line 199++)
 236    $image_info = image_get_info($file->filepath);
 237    $aspect_ratio = $image_info['height'] / $image_info['width'];
 238    $original_size = image_get_sizes(IMAGE_ORIGINAL, $aspect_ratio);
 239    if (!empty($original_size['width']) && !empty($original_size['height'])) {
 240      $result = image_scale($file->filepath, $file->filepath, $original_size['width'], $original_size['height']);
 241      if ($result) {
 242        clearstatcache();
 243        $file->filesize = filesize($file->filepath);
 244        drupal_set_message(t('The original image was resized to fit within the maximum allowed resolution of %width x %height pixels.', array('%width' => $original_size['width'], '%height' => $original_size['height'])));
 245      }
 246    }
 247    
 248    // We're good to go.
 249    $form_state['values']['images'][IMAGE_ORIGINAL] = $file->filepath;
 250    $form_state['values']['rebuild_images'] = FALSE;
 251    $form_state['values']['new_file'] = TRUE;
 252    
 253    // Call hook to allow other modules to modify the original image.
 254    module_invoke_all('image_alter', $form_state['values'], $file->filepath, IMAGE_ORIGINAL);
 255    $form_state['values']['images'] = _image_build_derivatives((object) $form_state['values'], TRUE);
 256    
 257    // Store the new file into the session.
 258    $_SESSION['image_new_files'][$nid] = $form_state['values']['images'];  
 259  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7