[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

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

   1  <?php
   2  // $Id: image_fupload.module,v 1.33 2009/04/06 21:15:41 grandcat Exp $
   3  
   4  define('IMAGE_UNMACHINED', 'image_raw');
   5  define('IMAGE_HALFPROCESSED', 'image_halfwork');
   6  define('IMAGE_PROCESSED', 'image_processed');
   7  
   8  // mark images (using body field) whose captions aren't edited yet
   9  define('IMAGE_NOT_COMPLETED', 'image_not_completed');
  10  
  11  /**
  12   * Implementation of hook_menu().
  13   */
  14  function image_fupload_menu() {
  15    $items['fupload/flash'] = array(
  16      'title' => 'Image FUpload',
  17      'page callback' => 'fupload_filetransfer',
  18      'access callback' => TRUE, // must be examined later
  19      'type' => MENU_CALLBACK,
  20    );
  21    $items['fupload/js/deletequeue/%'] = array(
  22      'title' => 'Image FUpload',
  23      'page callback' => 'fupload_empty_queue',
  24      'page arguments' => array(3), 
  25      'access arguments' => array('mass upload images'),
  26      'type' => MENU_CALLBACK,    
  27    );  
  28    $items['node/add/%image_node_type/list_images'] = array(
  29      'title' => 'Edit Captions',
  30      'access arguments' => array('edit captions'),
  31      'page callback' => 'drupal_get_form',
  32      'page arguments' => array('fupload_list_images_image', 2),    
  33      'type' => MENU_CALLBACK,
  34      'file' => 'includes/images.previewlist.image.inc',
  35    );
  36    $items['node/add/%image_node_type/list_imagefields'] = array(
  37      'title' => 'Edit Captions',
  38      'access arguments' => array('edit captions'),
  39      'page callback' => 'drupal_get_form',
  40      'page arguments' => array('fupload_list_images_imagefield', 2),    
  41      'type' => MENU_CALLBACK,
  42      'file' => 'includes/images.previewlist.imagefield.inc',
  43    );
  44    return $items;
  45  }
  46  
  47  /**
  48  * Menu callback; loads a image node type object
  49  */
  50  function image_node_type_load($node_type, $simple_check = FALSE) {
  51    $image_node_types = variable_get('image_node_types', array());
  52    // test if given node type is image (field) node type
  53    $node_type = str_replace("-", "_", $node_type);
  54    // simple check without checking url arguments
  55    if ($simple_check && isset($image_node_types[$node_type]))
  56      return $node_type;
  57      
  58    if (isset($image_node_types[$node_type])) {
  59      if (arg(3) == "list_imagefields" && $image_node_types[$node_type]['type'] == "cck") {
  60        return $node_type;
  61      } elseif (arg(3) == "list_images" && $image_node_types[$node_type]['type'] == "image") {
  62        return $node_type;
  63      } else {
  64        return FALSE;
  65      }
  66    } else {
  67      return FALSE;
  68    }
  69  }
  70  
  71  /**
  72  * returns a identifier for images so that their status (current step) can easily be set
  73  */
  74  function image_fupload_image_status($field_name, $status = IMAGE_UNMACHINED) {
  75    switch ($status) {  
  76      case IMAGE_NOT_COMPLETED:
  77        return "<!--" .$field_name ."/" .IMAGE_NOT_COMPLETED ."-->";
  78        break;
  79      default:
  80        return $field_name ."/" .$status;
  81        break;
  82    }
  83  }
  84  
  85  /**
  86   * Implementation of hook_perm().
  87   */
  88  function image_fupload_perm() {
  89    return array('mass upload images', 'edit captions');
  90  }
  91  
  92  /**
  93   * Implementation of hook_help
  94   */
  95  function image_fupload_help($path, $arg) {
  96    switch ($path) {
  97        case 'admin/help#image_fupload':
  98        $output = '<p>'. t("The Image FUpload module is used to provide an alternate upload form to image module itself.") .'</p>';
  99        $output .= '<p>'. t("This is a great advantage because multiple images can be selected with one click which are automatically uploaded and processed without any further user interaction. Additionally, this module fully integrates in image module. Consequently, all settings made by image module are observed (thumb creation, file size limit etc.).") .'</p>';
 100        $output .= '<p>' .t("Image FUpload administration allows to define some characters which are replaced in the node title by a whitespace. In addition to that, the option can be selected to show a link to the original upload form to those users whose browser doesn't support this Flash / JS solution.") . '</p>';
 101        $output .= t('<p>You can</p>
 102  <ul>
 103  <li>create images using F(lash)Upload at <a href="!node-create-image">node &gt;&gt; create &gt;&gt; image</a>.</li>
 104  <li>configure Image FUpload settings at <a href="!admin-settings-image-fupload">administer &gt;&gt; settings &gt;&gt; image &gt;&gt; image_fupload</a>.</li>
 105  ', array('!node-create-image' => url('node/add/image'), '!admin-image-galleries' => url('admin/image/galleries'), '!admin-settings-image-fupload' => url('admin/settings/image/image_fupload'))) .'</ul>';
 106        $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="!image">Image FUpload page</a>.', array('!image' => 'http://www.drupal.org/handbook/modules/image/')) .'</p>';
 107        return $output;
 108    }
 109  }
 110  
 111  /**
 112   * Implementation of hook_theme() registry.
 113   */
 114  function image_fupload_theme() {
 115    return array(
 116      'swfupload_settings' => array(
 117        'template' => 'swfupload-settings',
 118        'arguments' => array('modulepath' => NULL, 'uploadpath' => NULL, 'maxfilesize' => NULL, 'fileextensions' => NULL, 'sessionid' => NULL, 'uploadlimit' => NULL, 'nodetype' => NULL, 'fieldname' => NULL, 'field_required' => NULL, 'storage_mode' => NULL, 'redirect_url' => NULL),      
 119      ),
 120      'fupload_create_filename' => array(
 121        'arguments' => array('image' => NULL, 'replacements' => NULL),
 122      ),
 123      'fupload_imagepreview_image' => array(
 124        'arguments' => array('image' => NULL, 'image_info' => NULL, 'node_image' => NULL, 'attributes' => NULL),
 125        'file' => 'includes/images.previewlist.image.inc',
 126      ),
 127    );
 128  }
 129  
 130  function fupload_filetransfer() {
 131    // huh.. swfUpload sends some data...let's see
 132    global $user;
 133    $sid = $_POST['PHPSESSID'];
 134    $node_type = check_plain($_POST['nodetype']);
 135    $field_name = check_plain($_POST['fieldname']);  
 136    
 137    // validate given session id
 138    $result = db_query("SELECT * FROM {sessions} WHERE sid = '%s' AND hostname = '%s' LIMIT 1", $sid, ip_address());
 139    $upload_user = db_fetch_object($result);
 140    // Get users profile
 141    $user = user_load(array('uid' => $upload_user->uid));
 142    
 143    // Do some checks before upload
 144    $field_access = FALSE;
 145    if (!empty($upload_user) && !empty($node_type) && !empty($field_name)) { // user valid? node_type & field_name received?
 146      // Check if given node_type & field_name combination is possible for user
 147      switch ($node_type) {
 148        case "image":
 149          // image node type
 150          if ($field_name == "images") {
 151            $field_access = node_access('create', $node_type, $user);
 152            
 153            // get suitable validators for our upload: image module
 154            $validators = array(
 155              'file_validate_is_image' => array(),
 156              'file_validate_size' => array(variable_get('image_max_upload_size', 800) * 1024),
 157            );
 158          }
 159          break;
 160        default:
 161          // probably cck image node type
 162          if (module_exists('content')) {
 163            if (image_node_type_load($node_type, TRUE) && ($field = content_fields($field_name, $node_type))) {
 164              $field_access = node_access('create', $node_type, $user);
 165            
 166              // also get suitable validators for our upload: cck imagefield module
 167              $validators = array_merge(filefield_widget_upload_validators($field), imagefield_widget_upload_validators($field));
 168            }
 169          }
 170          break;
 171      }
 172    }
 173    
 174    if (user_access('mass upload images', $user) && $field_access) {
 175      // Adapt to drupal files structure
 176      $_FILES['files']['name']['image'] = $_FILES['Filedata']['name'];
 177      $_FILES['files']['type']['image'] = $_FILES['Filedata']['type'];
 178      $_FILES['files']['tmp_name']['image'] = $_FILES['Filedata']['tmp_name'];
 179      $_FILES['files']['error']['image'] = $_FILES['Filedata']['error'];
 180      $_FILES['files']['size']['image'] = $_FILES['Filedata']['size'];
 181      
 182      // do some checks via transliteration module if available
 183      if (module_exists('transliteration')) {
 184        require_once(drupal_get_path('module', 'transliteration') .'/transliteration.inc');
 185        $_FILES['files']['name']['image'] = transliteration_clean_filename($_FILES['Filedata']['name']);
 186      }
 187      
 188      if ($file = file_save_upload('image', $validators)) {
 189        $image = image_get_info($file->filepath); // Get real mime-type
 190        if(!db_query("UPDATE {files} SET filename = '%s', filemime = '%s' WHERE fid = %d", image_fupload_image_status($field_name, IMAGE_UNMACHINED), $image['mime_type'], $file->fid)) {
 191          drupal_json(array('status' => FALSE, 'data' => t('A database related problem appeared during upload. Please inform the site administrator if this is a permanent problem.')));
 192          exit;
 193        }
 194  
 195        // Get all status messages and add them to server message for swfUpload --> inform client
 196        $messages = drupal_get_messages('status');
 197        drupal_json(array('status' => TRUE, 'data' => t('Complete. !messages', array('!messages' => (!empty($messages['status']) ? implode(' ', $messages['status']) : ''))))); // Reply a status message to satisfy swfUpload
 198      } else {
 199        // Get responsible error messages and send it to swfUpload
 200        $messages = form_get_errors();
 201        drupal_json(array('status' => FALSE, 'data' => t('Upload failed: !errors', array('!errors' => implode(' ', $messages)))));
 202      } 
 203      
 204    } else {
 205      drupal_access_denied();
 206    }
 207  }
 208  
 209  function fupload_empty_queue($field_name) {
 210    global $user;
 211    
 212    // Set "processed" flag so that these images aren't processed again; images are deleted later by cron (--> temporary files)
 213    db_query("UPDATE {files} SET filename = '%s' WHERE uid = %d AND status = %d AND filename = '%s'", image_fupload_image_status(check_plain($field_name), IMAGE_PROCESSED), $user->uid, FILE_STATUS_TEMPORARY, image_fupload_image_status(check_plain($field_name), IMAGE_UNMACHINED));
 214    
 215    // Output message to user via AJAX
 216    drupal_set_message();
 217    drupal_set_message(t('All queued images were deleted.'), 'warning');
 218    drupal_json(array('status' => TRUE, 'data' => theme('status_messages')));
 219  }
 220  
 221  function theme_fupload_create_filename($image, $replacements = NULL) {
 222    // Get filename out of filepath
 223    $filename = trim(basename($image->filepath), ' ');
 224    $length1 = strlen(strrchr($filename, '.'));
 225    $length2 = strlen($filename);
 226    $image_name = ucfirst(substr($filename, 0, ($length2 - $length1)));
 227          
 228    // Remove some given (userdefined) elements
 229    if (is_null($replacements))
 230      $replacements = variable_get('fupload_title_replacements', '_;{;}'); 
 231    $image_name = str_replace(explode(';', $replacements), ' ', $image_name);
 232    
 233    return $image_name;
 234  }
 235  
 236  /**
 237  * Helper function which emulates "node_form_validate" of node.pages.inc
 238  * Need this because body causes validation error with some settings, but I don't need a body field, later yes =)
 239  */
 240  function fupload_node_form_validate($form, &$form_state) {
 241    $node = (object)$form_state['values'];
 242    $node->body = NULL; // Little Hack to disable body validation
 243    node_validate($node, $form);
 244  }
 245  
 246  /**
 247   * This function allows to validate a file on given $validator functions.
 248   * This code is taken from file_save_upload (D 6.4) (22.09.2008)
 249   *
 250   * @param object $file
 251   * @param array $validators
 252   * @return Boolean
 253   *   returns TRUE, if validation was successful, otherwise FALSE
 254   */
 255  function file_validate($file, $validators = array()) {
 256    // Call the validation functions.
 257    $errors = array();
 258     foreach ($validators as $function => $args) {
 259      array_unshift($args, $file);
 260      $errors = array_merge($errors, call_user_func_array($function, $args));
 261    }
 262  
 263    // Check for validation errors.
 264    if (!empty($errors)) {
 265      $message = t('The selected file %name could not be uploaded.', array('%name' => $file->filename));
 266      if (count($errors) > 1) {
 267        $message .= '<ul><li>'. implode('</li><li>', $errors) .'</li></ul>';
 268      } else {
 269        $message .= ' '. array_pop($errors);
 270      }
 271      form_set_error($source, $message);
 272      return FALSE;
 273    } else {
 274      // Validation successful =)
 275      return TRUE;
 276    }
 277  }
 278  
 279  /**
 280  * This helper function lists/read/write/delete special settings for the image previewlist 
 281  * depending on the node type
 282  *
 283  * @param string $op
 284  * @param string $node_type
 285  * @param array $data_write
 286  * @return array or NULL
 287  */
 288  
 289  function _fupload_imagepreview_settings($op, $node_type, $data_write = NULL) {
 290    // switch to the right operation: what's up?
 291    switch($op) {
 292      case 'list':
 293        // generate a list of available presets for image_preview: image module or/and imagecache
 294        $preview_presets_list = array();
 295        
 296        // image module presets
 297        if (module_exists('image') && $node_type == "image") {
 298          $image_sizes = image_get_sizes();
 299          
 300          foreach ($image_sizes as $key => $size) {
 301            $preview_presets_list['image']['0_' .$key] = t('Image size') .': ' .$size['label'];
 302          }
 303        }
 304        
 305        // imagecache module presets
 306        if (module_exists('imagecache')) {
 307          $image_sizes = imagecache_presets();
 308          
 309          foreach ($image_sizes as $key => $size) {
 310            $preview_presets_list['imagecache']['1_' .$size['presetname']] = t('Image size') .': ' .$size['presetname'];
 311          }
 312        }
 313        
 314        //output list
 315        return $preview_presets_list;
 316        
 317        break;   
 318        
 319      case 'read':
 320        // read actual setting for this node type
 321        $image_node_types = variable_get('image_node_types', array());
 322        
 323        switch($node_type) {
 324          case 'image':
 325            // image node type
 326            if (!empty($image_node_types['image']['image_selection'])) {
 327              // image module preset
 328              return '0_' .$image_node_types['image']['image_selection'];
 329            
 330            } elseif (!empty($image_node_types['image']['imagecache_preset'])) {
 331              // imagecache module preset
 332              return '1_' .$image_node_types['image']['imagecache_preset'];
 333            
 334            } else {
 335              // no setting saved yet?
 336              return '';
 337            }
 338            break;
 339          default:
 340            // node type with CCK ImageField
 341            
 342            // return imagecache preset
 343            return '1_' .$image_node_types[$node_type]['imagecache_preset'];
 344            break;
 345        }
 346        break;
 347        
 348      case 'write':
 349        if (isset($data_write)) { // is some data available?
 350        // write input data to database
 351        $image_node_types  = variable_get('image_node_types', array());
 352        
 353        // prepare type of image preview before writing into db 
 354        $preview_preset = explode('_', $data_write['preview_preset'], 2);  
 355        switch($preview_preset[0]) {
 356          case '0':    
 357            // image module preset
 358            $preview_preset_image = $preview_preset[1];    
 359            break;    
 360          case '1':
 361            // imagecache module preset
 362            $preview_preset_imagecache = $preview_preset[1];
 363            break;    
 364        }
 365              
 366        switch($node_type) {
 367          case 'image':
 368            // image node type
 369            $image_node_types['image'] = array('type' => 'image', 'fieldname' => 'images', 'image_selection' => $preview_preset_image, 'imagecache_preset' => $preview_preset_imagecache);          
 370            break;
 371            
 372          default:
 373            // node type with CCK ImageField            
 374            $image_node_types[$node_type] = array('type' => 'cck', 'fieldname' => $data_write['fieldname'], 'image_selection' => $preview_preset_image, 'imagecache_preset' => $preview_preset_imagecache);
 375            break;
 376        }
 377        // write data to database
 378        variable_set('image_node_types', $image_node_types);
 379        }
 380        break;
 381      
 382      case 'delete':
 383        // delete an entry from settings variable
 384        $image_node_types  = variable_get('image_node_types', array());
 385        unset($image_node_types[$node_type]);
 386        // write data to database
 387        variable_set('image_node_types', $image_node_types);
 388        break;
 389      
 390    }
 391  }


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