[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

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

   1  <?php
   2  // $Id: imagex.module,v 1.8 2009/08/12 09:21:51 sdrycroft Exp $
   3  /**
   4   * This is a complete rewrite of the imagex module.  The module
   5   * allows a user to upload multiple images in one simple step.
   6   * The module also provides an editing interface that makes 
   7   * editing a large number of images also much easier.
   8   */
   9  /***********************************************************************************
  10   * HOOKS
  11   ***********************************************************************************/
  12  function imagex_menu() {
  13    $items['imagex/upload'] = array(
  14      'access arguments' => array('create images'),
  15      'title' => 'Imagex upload',
  16      'page callback' => 'imagex_upload_image',
  17      'type' => MENU_CALLBACK);
  18    $items['imagex/get'] = array(
  19      'title' => 'Imagex thumbs',
  20      'access arguments' => array('create images'),
  21      'type' => MENU_CALLBACK,
  22      'page callback' => 'imagex_js'
  23    );
  24    $items['imagex'] = array(
  25      'title' => 'Upload images',
  26      'access arguments' => array('create images'),
  27      'type' => MENU_CALLBACK,
  28      'page callback' => 'imagex_upload_page'
  29    );
  30    return $items;
  31  }
  32  function imagex_form_alter(&$form, &$form_state, $form_id){
  33    // Lets add a small message to the top of the node/add/image form to point to the 
  34    // multiple add image page
  35    if($form_id == 'image_node_form'){
  36      $form['imagex'] = array(
  37        '#weight' => -100, // make sure it is at the top
  38        '#value' => '<div class="messages"><p>'.t('You can add multiple images quickly to the site from the <a href="@uploadpage">multiple upload page</a>', array('@uploadpage'=>url('imagex/imagex'))).'</p></div>'
  39      );
  40    }
  41  }
  42  /***********************************************************************************
  43   * CALLBACKS
  44   ***********************************************************************************/
  45  /**
  46   * Callback for the imagex/imagex page which simply displays the applet for uploading
  47   * of images
  48   *
  49   * @return String (Contains HTML for displaying of the applet)
  50   */
  51  function imagex_upload_page(){
  52    // If we're lookin' at this page, then chuffin heck, we need t'JavaScript & CSS
  53    drupal_add_js(drupal_get_path('module','imagex').'/imagex.js');
  54    drupal_add_css(drupal_get_path('module','imagex').'/imagex.css');
  55    return '<div class="messages imagex header"><p>'.t('Drop images into the box below, or click the "Add" button, and select the images you\'d like to upload').'</p></div><div class="imagex postlet">
  56      <applet name="postlet" code="Main.class" archive="'.url(drupal_get_path('module','imagex').'/postlet/postlet.jar', array('absolute' => TRUE)).'" width="100%" height="300" mayscript>
  57          <param name = "maxthreads"        value = "5" />
  58          <param name = "language"        value = "EN" />
  59          <param name = "type"            value = "application/x-java-applet;version=1.3.1" />
  60          <param name = "destination"        value = "'.url('imagex/upload', array('absolute' => TRUE)).'" />
  61          <param name = "backgroundcolour" value = "16777215" />
  62          <param name = "tableheaderbackgroundcolour" value = "14079989" />
  63          <param name = "tableheadercolour" value = "0" />
  64          <param name = "warnmessage" value = "false" />
  65          <param name = "autoupload" value = "false"/>
  66          <param name = "helpbutton" value = "false"/>
  67          <param name = "removebutton" value = "true" />
  68          <param name = "addbutton" value = "true"/>
  69          <param name = "uploadbutton" value = "true" />
  70          <param name = "fileextensions" value = "Image Files,jpg,gif,jpeg,bmp,png" />
  71      </applet>
  72      <script type="text/javascript">
  73        imagesreturnurl ="'.url('imagex/get', array('absolute' => TRUE)).'";
  74      </script>
  75  </div>
  76  <div id="imagexthumbs" class="imagex thumbs"></div>';
  77  }
  78  /**
  79   * Callback for the upload destination.  This is where images are sent to by the applet.
  80   * This page is NOT designed to be viewed by humans/browsers.
  81   */
  82  function imagex_upload_image(){
  83    // Output a postlet error if the user isn't allowed to upload.
  84    if (!user_access('create images')){
  85      ?>
  86  POSTLET REPLY
  87  POSTLET:NO
  88  POSTLET:SERVER ERROR
  89  POSTLET:ABORT ALL
  90  END POSTLET REPLY
  91      <?php
  92      exit;
  93    }
  94    else {
  95      // User is allowed to upload, well lets do it    
  96      $uploaded = 0;
  97      // The image module doesn't rename the files when it moves them, so we need to do that first
  98      $temp_filename = dirname($_FILES['userfile']['tmp_name']).'/'.$_FILES['userfile']['name'];
  99      file_move($_FILES['userfile']['tmp_name'],$temp_filename);
 100      $image_node = image_create_node_from($temp_filename, $_FILES['userfile']['name']);
 101      if (!$image_node){
 102        // Lets work out why it wasn't uploaded and change the error accordingly.
 103        
 104        // For now we'll say this type isn't allowed
 105        $uploaded = 2;
 106      } else {
 107        // Image has been added, lets add it to the image_gallery
 108        $gallery_tid = variable_get('imagex_gallery_tid', 0);
 109        if($gallery_tid){
 110          taxonomy_node_save($image_node, array($gallery_tid));
 111        }
 112      }
 113      switch($uploaded){
 114        case 0:
 115          ?>
 116  POSTLET REPLY
 117  POSTLET:YES
 118  END POSTLET REPLY
 119          <?php
 120          break;
 121        case 1:
 122          ?>
 123  POSTLET REPLY
 124  POSTLET:NO
 125  POSTLET:TOO LARGE
 126  POSTLET:ABORT THIS
 127  END POSTLET REPLY
 128          <?php
 129          break;
 130        case 2:
 131          ?>
 132  POSTLET REPLY
 133  POSTLET:NO
 134  POSTLET:FILE TYPE NOT ALLOWED
 135  POSTLET:ABORT THIS
 136  END POSTLET REPLY
 137          <?php
 138          break;
 139        case 3:
 140          ?>
 141  POSTLET REPLY
 142  POSTLET:NO
 143  POSTLET:SERVER ERROR
 144  POSTLET:ABORT ALL
 145  END POSTLET REPLY
 146          <?php
 147          break;        
 148      }
 149    }
 150    exit;
 151  }
 152  /**
 153   * Menu callback, ajax call to populate the page with the thumbnails
 154   */
 155  function imagex_js(){
 156    // Reurns HTML as JS for the thumbs of images uploaded
 157    // Does this for the user logged in only
 158    if(function_exists('matrix_editor_menu')){
 159      $matrix = " ".l('Edit your most recent images', 'matrix/image');
 160    }
 161    print drupal_to_js(array('html' => '<p>'.t('The following images have been uploaded by you in the last hour.') . $matrix .'</p>'.imagex_get_thumbs_html(60)));
 162    exit();
 163  }
 164  /***********************************************************************************
 165   * HELPER FUNCTIONS
 166   ***********************************************************************************/
 167  /**
 168   * Following gets an HTML formatted list of images that the current user has uploaded,
 169   * and not edited.  It can also optionally show only images uploaded in the last x minutes
 170   *
 171   * @return String (Formatted HTML)
 172   */
 173  function imagex_get_thumbs_html($minutes = NULL){
 174    global $user;
 175    if(is_null($minutes)){
 176      $result = db_query("SELECT nid FROM {node} WHERE uid = %d AND created=changed AND type='image'",$user->uid);
 177    }else{
 178      $result = db_query("SELECT nid FROM {node} WHERE uid = %d AND created=changed AND type='image' AND created>%d",$user->uid , time()-($minutes*60));
 179    }
 180    $html = '';
 181    while($node = db_fetch_object($result)){
 182      $node = node_load($node->nid);
 183      $html .= '<div class="imagexthumb" id="imagexthumb-'.$node->nid.'">'.l(image_display($node,  IMAGE_THUMBNAIL, array('alt'=>$node->title)),'node/'.$node->nid.'/edit', array('html'=>TRUE, 'attributes'=>array('target'=>'_blank'))).'</div>';
 184    }
 185    return $html;
 186  }


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