[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/audio/images/ -> audio_images.module (source)

   1  <?php
   2  // $Id: audio_images.module,v 1.9 2008/11/26 01:17:58 drewish Exp $
   3  
   4  include_once drupal_get_path('module', 'audio') .'/audio_image.inc';
   5  
   6  /**
   7   * Implementation of hook_menu().
   8   */
   9  function audio_images_menu() {
  10    $items = array();
  11    $items['admin/settings/audio/images'] = array(
  12      'title' => 'Images',
  13      'page callback' => 'drupal_get_form',
  14      'page arguments' => array('audio_images_admin_settings'),
  15      'access arguments' => array('administer site configuration'),
  16      'file' => 'audio_images.admin.inc',
  17      'type' => MENU_LOCAL_TASK,
  18    );
  19    return $items;
  20  }
  21  
  22  /**
  23   * Implementation of hook_theme
  24   */
  25  function audio_images_theme() {
  26    return array(
  27      'audio_images_form' => array(
  28        'arguments' => array('form'),
  29      ),
  30      'audio_images' => array(
  31        'arguments' => array('audio_images'),
  32      ),
  33      'audio_image' => array(
  34        'arguments' => array('images'),
  35      ),
  36    );
  37  }
  38  
  39  /**
  40   * Implementation of hook_file_download().
  41   */
  42  function audio_images_file_download($filename) {
  43    $filepath = file_create_path($filename);
  44  
  45    // Check if it's one of our files.
  46    $nid = db_result(db_query("SELECT ai.nid FROM {audio_image} ai INNER JOIN {files} f ON ai.fid = f.fid WHERE f.filepath = '%s'", $filepath));
  47    if ($nid && $node = node_load($nid)) {
  48      // Make sure they're allowed to view the node.
  49      if (node_access('view', $node)) {
  50        $info = image_get_info($filepath);
  51        return array(
  52          'Content-Type: '. mime_header_encode($info['mime_type']),
  53          'Content-Length: '. (int) $info['file_size'],
  54        );
  55      }
  56  
  57      // Access denied.
  58      return -1;
  59    }
  60  }
  61  
  62  /**
  63   * Implementation of hook_form_alter().
  64   *
  65   * Here we add our image fields to the audio node form.
  66   */
  67  function audio_images_form_alter(&$form, &$form_state, $form_id) {
  68    // We only alter audio node edit forms with a file attached.
  69    if ($form_id == 'audio_node_form' && !empty($form['#node']->audio['file']->filepath)) {
  70      drupal_add_js(drupal_get_path('module', 'audio') .'/audio.js');
  71  
  72      $form['#validate'][] = 'audio_images_node_form_validate';
  73  
  74      $form['audio_images'] = array(
  75        '#type' => 'fieldset', '#title' => t('Audio Images'),
  76        '#collapsible' => TRUE,
  77        '#description' => t('Cover art or other images.'),
  78        '#weight' => 0,
  79        '#tree' => TRUE,
  80      );
  81  
  82      if (isset($form['#node']->audio_images)) {
  83        $form['audio_images']['#theme'] = 'audio_images_form';
  84        foreach ($form['#node']->audio_images as $key => $image) {
  85          if ($key == 'delete' || $key == 'new') continue;
  86  
  87          $image = (array) $image;
  88          $form['audio_images'][$key] = array(
  89           '#type' => 'value',
  90           '#value' => $image,
  91          );
  92          $form['audio_images']['delete'][$key] = array(
  93            '#type' => 'checkbox',
  94            '#default_value' => isset($image['delete']) ? $image['delete'] : FALSE,
  95          );
  96        }
  97      }
  98  
  99      $form['audio_images']['new']['pictype'] = array(
 100        '#type' => 'select',
 101        '#title' => t('New image type'),
 102        '#default_value' => variable_get('audio_image_default_type', 0x03),
 103        '#options' => audio_image_type_dirty_array(),
 104      );
 105      $form['audio_images']['new']['audio_image_upload'] = array(
 106        '#type' => 'file',
 107        '#title' => t('Add a new image'),
 108        '#tree' => FALSE,
 109        '#attributes' => array('audio_accept' => 'gif, jpg, jpeg, png'),
 110        '#description' => t('Click "Browse..." to select an image to add to this audio file. <strong>NOTE:</strong> Supported image types include GIF, JPG, and PNG.  Suggested dimensions: 170 X 170 pixels.'),
 111      );
 112    }
 113  }
 114  
 115  /**
 116   * Theme function to format the audio image fieldset.
 117   */
 118  function theme_audio_images_form(&$form) {
 119    $pictypes = audio_image_type_dirty_array();
 120    $header = array(t('Type'), t('MIME Type'), t('Dimensions'), t('Size'), t('Delete'));
 121    $rows = array();
 122    foreach (element_children($form) as $key) {
 123      if ($key != 'new' && $key != 'delete') {
 124        $image = (array) $form[$key]['#value'];
 125        $rows[] = array(
 126          l($pictypes[$image['pictype']], $image['filepath']),
 127          $image['filemime'],
 128          format_size($image['filesize']),
 129          $image['height'] .'x'. $image['width'],
 130          drupal_render($form['delete'][$key]),
 131        );
 132      }
 133    }
 134    return (count($rows) ? theme('table', $header, $rows) : '') . drupal_render($form);
 135  }
 136  
 137  /**
 138   * Node validate handler
 139   */
 140  function audio_images_node_form_validate($form, &$form_state) {
 141    // Check for an uploaded image.
 142    $validators = array(
 143      'file_validate_is_image' => array(),
 144      'audio_image_validate_size' => array(),
 145    );
 146    if ($file = file_save_upload('audio_image_upload', $validators)) {
 147      $image = image_get_info($file->filepath);
 148      $file->height = $image['height'];
 149      $file->width = $image['width'];
 150      $file->pictype = (int) $form_state['values']['audio_images']['new']['pictype'];
 151  
 152      $form_state['values']['audio_images'][$file->pictype .'-'. $file->fid] = $file;
 153    }
 154    unset($form_state['values']['audio_images']['new']);
 155  
 156    // Move the delete checkbox values to the image
 157    if (isset($form_state['values']['audio_images']['delete'])) {
 158      foreach ($form_state['values']['audio_images']['delete'] as $key => $value) {
 159        $form_state['values']['audio_images'][$key]['delete'] = $value;
 160      }
 161      unset($form_state['values']['audio_images']['delete']);
 162    }
 163  }
 164  
 165  /**
 166   * Implementation of hook_nodeapi().
 167   */
 168  function audio_images_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
 169    if ($node->type != 'audio') {
 170      return;
 171    }
 172  
 173    switch ($op) {
 174      case 'load':
 175        return audio_images_nodeapi_load($node);
 176      case 'insert':
 177        return audio_images_nodeapi_insert($node);
 178      case 'update':
 179        return audio_images_nodeapi_update($node);
 180      case 'delete':
 181        return audio_images_nodeapi_delete($node);
 182      case 'delete revision':
 183        return audio_images_nodeapi_delete_revision($node);
 184    }
 185  }
 186  
 187  function audio_images_nodeapi_load($node) {
 188    $ret['audio_images'] = array();
 189    $result = db_query("SELECT f.fid, f.filemime, f.filepath, f.filesize, f.status, ai.nid, ai.vid, ai.pictype, ai.width, ai.height FROM {files} f INNER JOIN {audio_image} ai ON f.fid = ai.fid WHERE ai.vid = %d", $node->vid);
 190    while ($img = db_fetch_object($result)) {
 191      $ret['audio_images'][$img->pictype .'-'. $img->fid] = $img;
 192    }
 193    return $ret;
 194  }
 195  
 196  function audio_images_nodeapi_insert(&$node) {
 197    // Add new images.
 198    foreach ((array) $node->audio_images as $key => $image) {
 199      $image = (object) $image;
 200      $image->nid = $node->nid;
 201      $image->vid = $node->vid;
 202  
 203      if (($image->status & FILE_STATUS_PERMANENT) != FILE_STATUS_PERMANENT) {
 204        $newpath = _audio_image_filename($node->vid, $image->filemime, $image->pictype, FALSE);
 205        if (file_move($image, $newpath)) {
 206          $image->status |= FILE_STATUS_PERMANENT;
 207          drupal_write_record('files', $image, array('fid'));
 208        }
 209      }
 210      drupal_write_record('audio_image', $image);
 211      $node->audio_images[$key] = $image;
 212    }
 213  }
 214  
 215  function audio_images_nodeapi_update(&$node) {
 216    foreach ((array) $node->audio_images as $key => $image) {
 217      $image = (object) $image;
 218      $image->nid = $node->nid;
 219      $image->vid = $node->vid;
 220  
 221      if (!empty($image->delete)) {
 222        // Delete the image.
 223        _audio_images_delete($image);
 224        db_query('DELETE FROM {audio_image} WHERE vid = %d AND pictype = %d AND fid = %d', $node->vid, $image->pictype, $image->fid);
 225        unset($node->audio_images[$key]);
 226      }
 227      elseif (($image->status & FILE_STATUS_PERMANENT) != FILE_STATUS_PERMANENT) {
 228        // New image, was just uploaded.
 229        $newpath = _audio_image_filename($node->vid, $image->filemime, $image->pictype, FALSE);
 230        if (file_move($image, $newpath)) {
 231          $image->status |= FILE_STATUS_PERMANENT;
 232          drupal_write_record('files', $image, array('fid'));
 233        }
 234        drupal_write_record('audio_image', $image);
 235        $node->audio_images[$key] = $image;
 236      }
 237      elseif ($node->revision) {
 238        // Make copies of unchanged images when creating a new revision.
 239        drupal_write_record('audio_image', $image);
 240        $node->audio_images[$key] = $image;
 241      }
 242    }
 243  }
 244  
 245  function audio_images_nodeapi_delete(&$node) {
 246    // Delete the image files and remove them from the database.
 247    $result = db_query('SELECT ai.fid, f.filepath FROM {audio_image} ai INNER JOIN {files} f ON ai.fid = f.fid WHERE nid = %d', $node->nid);
 248    while ($file = db_fetch_object($result)) {
 249      _audio_images_delete($file);
 250    }
 251    db_query('DELETE FROM {audio_image} WHERE nid = %d', $node->nid);
 252  }
 253  
 254  function audio_images_nodeapi_delete_revision(&$node) {
 255    // Delete the image files and remove them from the database.
 256    $result = db_query('SELECT ai.fid, f.filepath FROM {audio_image} ai INNER JOIN {files} f ON ai.fid = f.fid WHERE vid = %d', $node->vid);
 257    while ($file = db_fetch_object($result)) {
 258      _audio_images_delete($file);
 259    }
 260    db_query('DELETE FROM {audio_image} WHERE vid = %d', $node->vid);
 261  }
 262  
 263  /**
 264   * If a file isn't used delete it and remove the {files} table record. The
 265   * caller needs to remove record(s) from the {audio_images} table.
 266   *
 267   * @param $file File object
 268   */
 269  function _audio_images_delete($file) {
 270    // Check if the file will be used after this revision is deleted
 271    $count = db_result(db_query('SELECT COUNT(fid) FROM {audio_image} WHERE fid = %d', $file->fid));
 272  
 273    // If the file won't be used, delete it.
 274    if ($count < 2) {
 275      db_query('DELETE FROM {files} WHERE fid = %d', $file->fid);
 276      file_delete($file->filepath);
 277    }
 278  }
 279  
 280  /**
 281   * Returns the array of the default image type.
 282   *
 283   * If no default image is found, returns a random image array.
 284   */
 285  function audio_images_get($audio_images, $pictype = NULL) {
 286    if (is_null($pictype)) {
 287      $pictype = variable_get('audio_default_image_type', 0x03);
 288    }
 289    if (!empty($audio_images)) {
 290      foreach ($audio_images as $image) {
 291        $image = (object) $image;
 292        if ($image->pictype == $pictype) {
 293          return $image;
 294        }
 295      }
 296      // The specified image was not found, return a random image.
 297      return $audio_images[array_rand($audio_images)];
 298    }
 299    return FALSE;
 300  }
 301  
 302  /**
 303   * Create a block for audio images.
 304   *
 305   * @param $audio_images
 306   *   The node's array of images
 307   */
 308  function theme_audio_images($audio_images) {
 309    if ($image = audio_images_get($audio_images)) {
 310      return "<div class='audio-image'>\n". theme('audio_image', $image) ."\n</div>\n";
 311    }
 312  }
 313  
 314  /**
 315   * Create an <img> element for an audio image.
 316   */
 317  function theme_audio_image($image) {
 318    $image = (object) $image;
 319  
 320    list($width, $height) = @getimagesize($image->filepath);
 321    $attributes = array('width' => $width, 'height' => $height);
 322  
 323    return theme('image', file_create_url($image->filepath), audio_image_type_dirty_array($image->pictype), '', $attributes, FALSE);
 324  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7