diff image/ad_image.module @ 2:e5584a19768b ad

maj module ad
author sly
date Wed, 15 Apr 2009 07:58:32 +0000
parents 948362c2a207
children 6aeff3329e01
line wrap: on
line diff
--- a/image/ad_image.module	Thu Apr 02 15:28:21 2009 +0000
+++ b/image/ad_image.module	Wed Apr 15 07:58:32 2009 +0000
@@ -1,5 +1,5 @@
 <?php
-// $Id: ad_image.module,v 1.2.2.13.2.40.2.11.2.5 2009/03/11 16:12:07 jeremy Exp $
+// $Id: ad_image.module,v 1.2.2.13.2.40.2.11.2.6 2009/04/02 15:48:29 jeremy Exp $
 
 /**
  * @file
@@ -152,6 +152,14 @@
     );
 
     $format = db_fetch_object(db_query('SELECT * FROM {ad_image_format} WHERE gid = %d', $tid));
+    $form["group-$tid"]["max-size-$tid"] = array(
+      '#type' => 'textfield',
+      '#title' => t('Maximum filesize'),
+      '#size' => 5,
+      '#maxlength' => 15,
+      '#default_value' => isset($format->max_size) ? $format->max_size : 0,
+      '#description' => t('Optionally specify a maximum filesize in bytes for images in this group.  To specify no maximum filesize, enter <em>0</em>.'),
+    );
     $form["group-$tid"]["min-height-$tid"] = array(
       '#type' => 'textfield',
       '#title' => t('Minimum height'),
@@ -206,10 +214,10 @@
     // TODO: Update the database schema, convert gid to tid.
     $gid = db_result(db_query('SELECT gid FROM {ad_image_format} WHERE gid = %d', $group->tid));
     if (is_numeric($gid)) {
-      db_query("UPDATE {ad_image_format} SET min_width = %d, max_width = %d, min_height = %d, max_height = %d WHERE gid = %d", $form_state['values']["min-width-$group->tid"], $form_state['values']["max-width-$group->tid"], $form_state['values']["min-height-$group->tid"], $form_state['values']["max-height-$group->tid"], $group->tid);
+      db_query("UPDATE {ad_image_format} SET min_width = %d, max_width = %d, min_height = %d, max_height = %d, max_size = %d WHERE gid = %d", $form_state['values']["min-width-$group->tid"], $form_state['values']["max-width-$group->tid"], $form_state['values']["min-height-$group->tid"], $form_state['values']["max-height-$group->tid"], $form_state['values']["max-size-$group->tid"], $group->tid);
     }
     else {
-      db_query("INSERT INTO {ad_image_format} (gid, min_width, max_width, min_height, max_height) VALUES (%d, %d, %d, %d, %d)", $group->tid, $form_state['values']["min-width-$group->tid"], $form_state['values']["max-width-$group->tid"], $form_state['values']["min-height-$group->tid"], $form_state['values']["max-height-$group->tid"]);
+      db_query("INSERT INTO {ad_image_format} (gid, min_width, max_width, min_height, max_height, max_size) VALUES (%d, %d, %d, %d, %d, %d)", $group->tid, $form_state['values']["min-width-$group->tid"], $form_state['values']["max-width-$group->tid"], $form_state['values']["min-height-$group->tid"], $form_state['values']["max-height-$group->tid"], $form_state['values']["max-size-$group->tid"]);
     }
   }
   drupal_set_message('Image ad global settings updated.');
@@ -236,12 +244,7 @@
     case 'insert':
     case 'update':
       $fid = isset($node->files) ? (int)ad_image_active_file($node->files) : 0;
-      if ($fid) {
-        $image = ad_image_load_image($node);
-      }
-      else if (isset($node->remote_image)) {
-        list($image->width, $image->height) = getimagesize($node->remote_image);
-      }
+      $image = ad_image_load_image($node);
       // This is ugly, but as "a" comes before "u" we don't seem to be able
       // to modify the upload module's form.  Instead, we check after the fact
       // if someone is editing images when they're not allowed, and if so we
@@ -272,24 +275,22 @@
         }
       }
       if ($op == 'insert') {
-        db_query("INSERT INTO {ad_image} (aid, fid, url, tooltip, remote_image, width, height) VALUES(%d, %d, '%s', '%s', '%s', %d, %d)", $node->nid, $node->fid, $node->url, $node->tooltip, $node->remote_image, $node->width, $node->height);
+        db_query("INSERT INTO {ad_image} (aid, fid, url, tooltip, remote_image, width, height) VALUES(%d, %d, '%s', '%s', '%s', %d, %d)", $node->nid, $node->fid, $node->url, $node->tooltip, $node->remote_image, isset($node->width) ? $node->width : 0, isset($node->height) ? $node->height : 0);
       }
       else {
-        db_query("UPDATE {ad_image} SET fid = %d, url = '%s', tooltip = '%s', remote_image = '%s', width = %d, height = %d WHERE aid = %d", $fid, $node->url, $node->tooltip, $node->remote_image, $node->width, $node->height, $node->nid);
+        db_query("UPDATE {ad_image} SET fid = %d, url = '%s', tooltip = '%s', remote_image = '%s', width = %d, height = %d WHERE aid = %d", $fid, $node->url, $node->tooltip, $node->remote_image, isset($node->width) ? $node->width : 0, isset($node->height) ? $node->height : 0, $node->nid);
       }
-      if (!isset($node->remote_image) || empty($node->remote_image)) {
-        // No valid image has been uploaded, don't allow ad to be 'active'.
-        if ($image === FALSE || !ad_image_active_file(($node->files))) {
-          db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active'));
-          if (db_affected_rows()) {
-            drupal_set_message(t('Image validation failed, unable to mark ad as %active.  Setting ad as %pending.', array('%active' => t('active'), '%pending' => t('pending'))), 'error');
-          }
+      // No valid image has been uploaded, don't allow ad to be 'active'.
+      if ($image === FALSE || !ad_image_active_file(($node->files))) {
+        db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active'));
+        if (db_affected_rows()) {
+          drupal_set_message(t('Image validation failed, unable to mark ad as %active.  Setting ad as %pending.', array('%active' => t('active'), '%pending' => t('pending'))), 'error');
         }
-        else if (!$fid) {
-          db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active'));
-          if (db_affected_rows()) {
-            drupal_set_message(t('Unable to mark ad as <em>active</em> until uploaded image is validated.  If you do not see any more errors, you should now be able to set your ad as <em>active</em>.'), 'error');
-          }
+      }
+      else if (!$node->remote_image && !$fid) {
+        db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active'));
+        if (db_affected_rows()) {
+          drupal_set_message(t('Unable to mark ad as <em>active</em> until uploaded image is validated.  If you do not see any more errors, you should now be able to set your ad as <em>active</em>.'), 'error');
         }
       }
       break;
@@ -382,7 +383,7 @@
   if (isset($format[$gid])) {
     return $format[$gid];
   }
-  $format[$gid] = db_fetch_object(db_query('SELECT min_width, max_width, min_height, max_height FROM {ad_image_format} WHERE gid = %d', $gid));
+  $format[$gid] = db_fetch_object(db_query('SELECT min_width, max_width, min_height, max_height, max_size FROM {ad_image_format} WHERE gid = %d', $gid));
   return $format[$gid];
 }
 
@@ -405,21 +406,26 @@
     }
     foreach ($terms as $tid => $term) {
       list($size->width, $size->height) = getimagesize($file->filepath);
+      $size->bytes = strlen(join('', file($file->filepath)));
       if ($format = ad_image_format_load($tid)) {
         if ($size->width < $format->min_width) {
-          drupal_set_message(t('The image <em>%name</em> is only %current pixels wide, which is less than the minimum of %minimum pixels allowed in the %group ad group.', array('%name' => $file->filename, '%current' => $size->width, '%minimum' => $format->min_width, '%group' => theme_placeholder($term->name))), 'error');
+          drupal_set_message(t('The image %name is only %current pixels wide, which is less than the minimum of %minimum pixels allowed in the %group ad group.', array('%name' => $file->filename, '%current' => $size->width, '%minimum' => $format->min_width, '%group' => isset($term->name) ? $term->name : t('default'))), 'error');
           $error = TRUE;
         }
         else if ($format->max_width && ($size->width > $format->max_width)) {
-          drupal_set_message(t('The image <em>%name</em> is %current pixels wide, which is more than the maximum of %maximum pixels allowed in the %group ad group.', array('%name' => $file->filename, '%current' => $size->width, '%maximum' => $format->max_width, '%group' => theme_placeholder($term->name))), 'error');
+          drupal_set_message(t('The image %name is %current pixels wide, which is more than the maximum of %maximum pixels allowed in the %group ad group.', array('%name' => $file->filename, '%current' => $size->width, '%maximum' => $format->max_width, '%group' => isset($term->name) ? $term->name : t('default'))), 'error');
           $error = TRUE;
         }
         if ($size->height < $format->min_height) {
-          drupal_set_message(t('The image <em>%name</em> is only %current pixels high, which is less than the minimum of %minimum pixels allowed in the %group ad group.', array('%name' => $file->filename, '%current' => $size->height, '%minimum' => $format->min_height, '%group' => theme_placeholder($term->name))), 'error');
+          drupal_set_message(t('The image %name is only %current pixels high, which is less than the minimum of %minimum pixels allowed in the %group ad group.', array('%name' => $file->filename, '%current' => $size->height, '%minimum' => $format->min_height, '%group' => isset($term->name) ? $term->name : t('default'))), 'error');
           $error = TRUE;
         }
         else if ($format->max_height && $size->height > $format->max_height) {
-          drupal_set_message(t('The image <em>%name</em> is %current pixels high, which is more than the maximum of %maximum pixels allowed in the %group ad group.', array('%name' => $file->filename, '%current' => $size->height, '%maximum' => $format->max_height, '%group' => theme_placeholder($term->name))), 'error');
+          drupal_set_message(t('The image %name is %current pixels high, which is more than the maximum of %maximum pixels allowed in the %group ad group.', array('%name' => $file->filename, '%current' => $size->height, '%maximum' => $format->max_height, '%group' => isset($term->name) ? $term->name : t('default'))), 'error');
+          $error = TRUE;
+        }
+        if ($format->max_size && $size->bytes > $format->max_size) {
+          drupal_set_message(t('The image %name is %current bytes in size, which is more than the maximum of %maximum bytes allowed in the %group ad group.', array('%name' => $file->filename, '%current' => $size->bytes, '%maximum' => $format->max_size, '%group' => isset($term->name) ? $term->name : t('default'))), 'error');
           $error = TRUE;
         }
       }
@@ -441,7 +447,15 @@
  * Returns image object from given ad node.
  */
 function ad_image_load_image($node) {
-  if (isset($node->files) && is_array($node->files)) {
+  if (isset($node->remote_image) && !empty($node->remote_image)) {
+    $file->filename = $node->remote_image;
+    $file->filepath = $node->remote_image;
+    $image = ad_image_validate_size($file, $node->nid);
+    if ($image !== FALSE) {
+      return $image;
+    }
+  }
+  else if (isset($node->files) && is_array($node->files)) {
     foreach ($node->files as $file) {
       if (is_array($file)) {
         if ($file['list'] && file_exists($file['filepath'])) {
@@ -481,8 +495,13 @@
   );
 
   if (!empty($node->remote_image)) {
-    list($image->width, $image->height) = getimagesize($node->remote_image);
+    $file->filename = $node->remote_image;
+    $file->filepath = $node->remote_image;
+    $image = ad_image_validate_size($file, $node->nid);
     $path = '<img src="'. $node->remote_image .'" alt="'. t('image') .'" /> ';
+    if ($image === FALSE) {
+      $path .= t('(invalid image)'). '<br />';
+    }
   }
   else if (isset($node->files)) {
     $files = $node->files;