[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/image/contrib/image_attach/ -> image_attach.module (source)

   1  <?php
   2  // $Id: image_attach.module,v 1.76.2.15 2010/10/03 08:35:51 joachim Exp $
   3  
   4  /**
   5   * @file image_attach.module
   6   */
   7  
   8  define('IMAGE_ATTACH_HIDDEN', 'hidden');
   9  
  10  /**
  11   * Implementation of hook_menu()
  12   */
  13  function image_attach_menu() {
  14    $items['image_attach'] = array(
  15      'title' => 'Image attachment view',
  16      'page callback' => 'image_attach_view_image',
  17      'access arguments' => array('access content'),
  18      'type' => MENU_CALLBACK,
  19    );
  20    $items['admin/settings/image/image_attach'] = array(
  21      'title' => 'Image attach',
  22      'description' => 'Enable image attach for content.',
  23      'page callback' => 'drupal_get_form',
  24      'page arguments' => array('image_attach_admin_settings'),
  25      'access arguments' => array('administer site configuration'),
  26      'type' => MENU_LOCAL_TASK,
  27    );
  28    return $items;
  29  }
  30  
  31  /**
  32   * Implementation of hook_perm()
  33   */
  34  function image_attach_perm() {
  35    return array('attach images');
  36  }
  37  
  38  function image_attach_admin_settings() {
  39    $form['image_attach_existing'] = array(
  40      '#type' => 'radios',
  41      '#title' => t('Attach existing images'),
  42      '#default_value' => variable_get('image_attach_existing', 1),
  43      '#options' => array(
  44        0 => t('Disabled'),
  45        1 => t('Enabled'),
  46      ),
  47      '#description' => t('When enabled, will allow existing image nodes to be attached instead of uploading new images.'),
  48    );
  49  
  50    return system_settings_form($form);
  51  }
  52  
  53  /**
  54   * Implementation of hook_block().
  55   */
  56  function image_attach_block($op = 'list', $delta = 0, $edit = array()) {
  57    switch ($op) {
  58      case 'list':
  59        $blocks[0] = array(
  60          'info' => t('Attached images'),
  61          'weight' => 0,
  62          'visibility' => 1,
  63          'pages' => 'node/*',
  64        );
  65        return $blocks;
  66  
  67      case 'view':
  68        if ($delta == 0) {
  69          if (arg(0) == 'node' && is_numeric(arg(1))) {
  70            $node = node_load(arg(1));
  71            if (isset($node->iids)) {
  72              $output['subject'] = t('Attached images');
  73              foreach ($node->iids as $iid) {
  74                $image = node_load($iid);
  75                if (node_access('view', $image)) {
  76                  $image_nodes[] = $image;
  77                }
  78              }
  79              $output['content'] = theme('image_attach_attached_images_block', $node->nid, $image_nodes);
  80              return $output;
  81            }
  82          }
  83        }
  84        break;
  85  
  86      case 'configure':
  87        if ($delta == 0) {
  88          $image_sizes = array();
  89          foreach (image_get_sizes() as $key => $size) {
  90            $image_sizes[$key] = $size['label'];
  91          }
  92          $form['image_attach_block_0_size'] = array(
  93            '#type' => 'select',
  94            '#title' => t('Image size'),
  95            '#default_value' => variable_get('image_attach_block_0_size', IMAGE_THUMBNAIL),
  96            '#options' => $image_sizes,
  97            '#description' => t('This determines the size of the image that appears in the block.'),
  98          );
  99          return $form;
 100        }
 101        break;
 102  
 103      case 'save':
 104        if ($delta == 0) {
 105          variable_set('image_attach_block_0_size', $edit['image_attach_block_0_size']);
 106        }
 107        break;
 108    }
 109  }
 110  
 111  /**
 112   * Implementation of hook_form_FORM_ID_alter().
 113   *
 114   * Add settings to the content type settings form.
 115   */
 116  function image_attach_form_node_type_form_alter(&$form, $form_state) {
 117    if ($form['#node_type']->type != 'image') {
 118      _image_check_settings();
 119  
 120      $image_sizes = array(IMAGE_ATTACH_HIDDEN => t('<Hidden>'));
 121      foreach (image_get_sizes() as $key => $size) {
 122        $image_sizes[$key] = $size['label'];
 123      }
 124  
 125      $form['image_attach'] = array(
 126        '#type' => 'fieldset',
 127        '#title' => t('Image Attach settings'),
 128        '#collapsible' => TRUE,
 129        '#collapsed' => TRUE,
 130      );
 131      $form['image_attach']['image_attach'] = array(
 132        '#type' => 'radios',
 133        '#title' => t('Attach images'),
 134        '#default_value' => variable_get('image_attach_' . $form['#node_type']->type, 0),
 135        '#options' => array(0 => t('Disabled'), 1 => t('Enabled')),
 136        '#description' => t('Should this node allows users to upload an image?'),
 137      );
 138      $form['image_attach']['image_attach_maximum'] = array(
 139        '#type' => 'select',
 140        '#title' => t('Maximum number of images'),
 141        '#default_value' => variable_get('image_attach_maximum_' . $form['#node_type']->type, 0),
 142        '#options' => array(0 => t('Unlimited')) + drupal_map_assoc(range(1, 10)),
 143        '#description' => t('The maximum number of images that may be attached to nodes of this type.'),
 144      );
 145      $form['image_attach']['image_attach_size_teaser'] = array(
 146        '#type' => 'select',
 147        '#title' => t('Teaser image size'),
 148        '#default_value' => variable_get('image_attach_size_teaser_' . $form['#node_type']->type, IMAGE_THUMBNAIL),
 149        '#options' => $image_sizes,
 150        '#description' => t("This determines the size of the image that appears when the node is displayed as a teaser. 'Hidden' will not show the image."),
 151      );
 152      // Hide the weight if CCK is doing the weight for us.
 153      if (!module_exists('content')) {
 154        $form['image_attach']['image_attach_weight_teaser'] = array(
 155          '#type' => 'weight',
 156          '#title' => t('Teaser image weight'),
 157          '#default_value' => variable_get('image_attach_weight_teaser_' . $form['#node_type']->type, 0),
 158          '#description' => t('This value determines the order of the image when displayed in the teaser.'),
 159        );
 160      }
 161      $form['image_attach']['image_attach_size_body'] = array(
 162        '#type' => 'select',
 163        '#title' => t('Full node image size'),
 164        '#default_value' => variable_get('image_attach_size_body_' . $form['#node_type']->type, IMAGE_THUMBNAIL),
 165        '#options' => $image_sizes,
 166        '#description' => t("This determines the size of the image that appears when the full node is displayed. 'Hidden' will not show the image."),
 167      );
 168      if (!module_exists('content')) {
 169        $form['image_attach']['image_attach_weight_body'] = array(
 170          '#type' => 'weight',
 171          '#title' => t('Full node image weight'),
 172          '#default_value' => variable_get('image_attach_weight_body_' . $form['#node_type']->type, 0),
 173          '#description' => t('This value determines the order of the image when displayed in the body.'),
 174        );
 175      }
 176      if (module_exists('content')) {
 177        $link = l(t('Manage fields'), $_GET['q'] . '/fields');
 178        $form['image_attach']['#description'] = t('Since you installed CCK module, you can change the image weight in the !link page.', array('!link' => $link));
 179      }
 180    }
 181  }
 182  
 183  /**
 184   * Implementation of hook_form_alter().
 185   */
 186  function image_attach_form_alter(&$form, $form_state, $form_id) {
 187    // Node edit form.
 188    if (isset($form['type']['#value']) && $form['type']['#value'] != 'image') {
 189      $type = $form['type']['#value'];
 190      // If enabled adjust the form.
 191      if ($form_id == $type . '_node_form' && variable_get('image_attach_' . $type, 0)) {
 192        $node = $form['#node'];
 193        _image_check_settings();
 194        $value = !empty($node->new_image) ? '#value' : '#default_value';
 195        $form['#attributes'] = array("enctype" => "multipart/form-data");
 196        // Add a custom submit handler so we can handle image creation on-the-fly
 197        $form['#validate'][] = 'image_attach_validate';
 198        // Add a custom submit handler so we can clean up the selection box items.
 199        $form['#submit'][] = 'image_attach_node_form_submit';
 200  
 201        // Check permissions and settings
 202        $may_attach          = user_access('attach images');
 203        $may_attach_existing = variable_get('image_attach_existing', 1);
 204        $may_upload          = user_access('create images');
 205        $has_existing_images = !empty($node->iids);
 206        $maximum_images      = variable_get('image_attach_maximum_' . $type, 0);
 207  
 208        // Display the image attach form only if user can attach images, AND
 209        // it is allowed to attach existing images or the user is allowed to create new images
 210        if ($may_attach && ($may_attach_existing || $may_upload)) {
 211          $form['image_attach'] = array(
 212            '#type' => 'fieldset',
 213            '#title' => t('Attached images'),
 214            '#collapsible' => TRUE,
 215            '#collapsed' => empty($node->iids),
 216          );
 217  
 218          if ($maximum_images) {
 219            $form['image_attach']['#description'] = format_plural(
 220              $maximum_images,
 221              'You may attach 1 image.',
 222              'You may attach up to @count images.'
 223            );
 224          }
 225  
 226          if ($has_existing_images) {
 227            foreach ($node->iids as $iid) {
 228              $image = node_load($iid);
 229              $form['image_attach']['image_thumbnail'][$iid] = array(
 230                '#type' => 'item',
 231                '#title' => t('Thumbnail'),
 232                '#value' => image_display($image, 'thumbnail'),
 233              );
 234            }
 235          }
 236  
 237          // Only show selection box of image nodes if the user may attach some,
 238          // or if there are existings ones that may be removed.
 239          if ($may_attach_existing || $has_existing_images) {
 240            $form['image_attach']['iids'] = array(
 241              '#type' => 'select',
 242              $value => empty($node->iids) ? NULL : $node->iids,
 243              '#multiple' => TRUE,
 244              '#size' => 6,
 245              // Title, options and description are set just below.
 246            );
 247  
 248            // User may attach already existing images: show a selection box containing all images.
 249            if ($may_attach_existing) {
 250              $form['image_attach']['iids']['#title'] = t('Existing images');
 251              $form['image_attach']['iids']['#options'] = _image_attach_get_image_nodes();
 252              if ($may_upload) {
 253                $form['image_attach']['iids']['#description'] = t('Choose existing images if you do not upload a new one.');
 254              }
 255              else {
 256                $form['image_attach']['iids']['#description'] = t('Choose existing images to attach.');
 257              }
 258            }
 259            // User may only upload new images: show a selection box containing only attached images.
 260            else {
 261              $form['image_attach']['iids']['#title'] = t('Attached images');
 262              $form['image_attach']['iids']['#options'] = _image_attach_get_image_nodes($node->iids);
 263              $form['image_attach']['iids']['#description'] = t('You can remove a previously attached image by unselecting it.');
 264            }
 265          }
 266  
 267          // User may create images, add upload form elements.
 268          if ($may_upload) {
 269            $form['image_attach']['image'] = array(
 270              '#type' => 'file',
 271              '#size' => 40,
 272              '#title' => t('Upload image'),
 273            );
 274            $form['image_attach']['image_title'] = array(
 275              '#type' => 'textfield',
 276              '#title' => t('Image title'),
 277              $value => '',
 278              '#description' => t('The title the image will be shown with. Leave blank to use the filename.'),
 279            );
 280            // Provide an additional submit button, which adds an image and redirects
 281            // the user to the node edit form.
 282            $form['image_attach']['image_attach_multiple'] = array(
 283              '#type' => 'submit',
 284              '#value' => t('Upload'),
 285              '#validate' => array('image_attach_validate'),
 286              '#submit' => array('image_attach_image_add_submit'),
 287            );
 288          }
 289        }
 290      }
 291    }
 292  }
 293  
 294  /**
 295   * Save attached image nids and rebuild form.
 296   *
 297   * This submit function adds the new images and returns to the
 298   * node edit form directly afterwards, without creating the new node yet.
 299   */
 300  function image_attach_image_add_submit(&$form, &$form_state) {
 301    // Rebuild the attached image data.
 302    if (isset($form_state['values']['iids'])) {
 303      db_query("DELETE FROM {image_attach} WHERE nid = %d", $form['nid']['#value']);
 304      if (count($form_state['values']['iids'])) {
 305        $weight = 0;
 306        foreach ($form_state['values']['iids'] as $iid) {
 307          db_query("INSERT INTO {image_attach} (nid, iid, weight) VALUES (%d, %d, %d)", $form['nid']['#value'], $iid, $weight++);
 308        }
 309      }
 310    }
 311  
 312    // Convert taxonomy format from Preview to Object.
 313    if (module_exists('taxonomy') && !empty($form_state['values']['taxonomy'])) {
 314      $temp_node = new stdClass();
 315      $temp_node->taxonomy = $form_state['values']['taxonomy'];
 316      $form_state['values']['taxonomy'] = taxonomy_preview_terms($temp_node);
 317      unset($temp_node);
 318    }
 319  
 320    // Rebuild the node edit form.
 321    node_form_submit_build_node($form, $form_state);
 322  }
 323  
 324  /**
 325   * Image attach validation handler for node edit form.
 326   *
 327   * Check that the number of images has not exceeded the maximum.
 328   * Capture node form submission and immediately create an image if one has been
 329   * uploaded.
 330   * Note that the new image nodes are created even on preview. Taking several
 331   * attempts may create trash.
 332   */
 333  function image_attach_validate(&$form, &$form_state) {
 334    // Test for whether a file is being uploaded cribbed from file_save_upload().
 335    $uploading_new_image = isset($_FILES['files']) && $_FILES['files']['name']['image'] && is_uploaded_file($_FILES['files']['tmp_name']['image']);
 336  
 337    // Validate the number of attached images. Filter out the 'None' with array_filter.
 338    if ($maximum_images = variable_get('image_attach_maximum_' . $form['#node']->type, 0)) {
 339      if (is_array($form_state['values']['iids'])) {
 340        $num_images = count(array_filter($form_state['values']['iids']));
 341      }
 342      else {
 343        $num_images = 0;
 344      }
 345  
 346      $node_type_label = node_get_types('name', $form['#node']->type);
 347      if ($num_images >= $maximum_images && $uploading_new_image) {
 348        // This error will be set when attempting to upload a new image.
 349        // The number already selected may be equal to the maximum, in which case
 350        // the error is just to alert the user that their upload has not been performed, and allow
 351        // them to unselect an image and proceed to upload the new one.
 352        form_set_error('iids', t('There are @count_images images already attached. A new image cannot be uploaded until one or more attached images are unselected.', array(
 353          '@count_images' => format_plural($num_images, '1 image', '@count images'),
 354          '@maximum'      => $maximum_images,
 355          '%type'         => $node_type_label,
 356        )));
 357      }
 358      elseif ($num_images > $maximum_images) {
 359        form_set_error('iids', t('You have selected @count_images but the maximum for a %type is @maximum.', array(
 360          '@count_images' => format_plural($num_images, '1 image', '@count images'),
 361          '@maximum'      => $maximum_images,
 362          '%type'         => $node_type_label,
 363        )));
 364      }
 365    }
 366  
 367    // Validate and save the uploaded image, providing that there are no errors set.
 368    if (!count(form_get_errors())) {
 369      $validators = array(
 370        'file_validate_is_image' => array(),
 371      );
 372      if ($file = file_save_upload('image', $validators)) {
 373        $image_title = $_POST['image_title'] ? $_POST['image_title'] : basename($file->filepath);
 374        // Initialize an image properly.
 375        $image = image_create_node_from($file->filepath, $image_title, '');
 376        if ($image && !form_get_errors()) {
 377          drupal_set_message(t("Created new image to attach to this node. !image_link", array('!image_link' => l($image_title, 'node/' . $image->nid))));
 378          // Append image nid to array of images.
 379          $form_state['values']['iids'][$image->nid] = $image->nid;
 380        }
 381      }
 382      else {
 383        // Only raise error if user clicked specific Upload button.
 384        if ($uploading_new_image) {
 385          form_set_error('image_attach', t('Invalid or missing image file for upload and attach.'));
 386        }
 387      }
 388    }
 389  }
 390  
 391  /**
 392   * Extra submit handler for node forms.
 393   */
 394  function image_attach_node_form_submit(&$form, &$form_state) {
 395    // Clear the 0 key in the iids array that arises from selecting the 'None'
 396    // option. We do this here so image_attach_nodeapi() gets clean data.
 397    unset($form_state['values']['iids'][0]);
 398  }
 399  
 400  /**
 401   * Implementation of hook_nodeapi().
 402   */
 403  function image_attach_nodeapi(&$node, $op, $teaser, $page) {
 404    // Make sure that if an image is deleted it is detached from any nodes.
 405    if ($node->type == 'image') {
 406      switch ($op) {
 407        case 'delete':
 408          db_query("DELETE FROM {image_attach} WHERE iid = %d", $node->nid);
 409      }
 410      return;
 411    }
 412    else if (variable_get('image_attach_' . $node->type, 0) == 0) {
 413      return;
 414    }
 415    switch ($op) {
 416      case 'insert':
 417      case 'update':
 418        db_query("DELETE FROM {image_attach} WHERE nid = %d", $node->nid);
 419        if (!empty($node->iids)) {
 420          // Populate weight column with placeholder values.
 421          $weight = 0;
 422          foreach ($node->iids as $iid) {
 423            db_query("INSERT INTO {image_attach} (nid, iid, weight) VALUES (%d, %d, %d)", $node->nid, $iid, $weight++);
 424          }
 425        }
 426        break;
 427  
 428      case 'delete':
 429        db_query("DELETE FROM {image_attach} WHERE nid = %d", $node->nid);
 430        break;
 431  
 432      case 'load':
 433        $res = db_query("SELECT iid FROM {image_attach} WHERE nid = %d ORDER BY weight", $node->nid);
 434        $iids = array();
 435        while ($iid = db_fetch_array($res)) {
 436          $iids[] = $iid['iid'];
 437        }
 438        return array('iids' => $iids);
 439  
 440      // Pass the body and teaser objects to the theme again to add the images.
 441      case 'view':
 442        if (!empty($node->iids)) {
 443          $node->image_attach = _image_attach_node_load_attached($node->iids, $teaser);
 444          $teaser_or_body = $teaser ? 'teaser' : 'body';
 445          $img_size = variable_get('image_attach_size_' . $teaser_or_body . '_' . $node->type, IMAGE_THUMBNAIL);
 446  
 447          // Don't show anything if the attached images are set to hidden.
 448          if ($img_size == IMAGE_ATTACH_HIDDEN) {
 449            return;
 450          }
 451  
 452          // Set weight, either from CCK or our own settings. Cribbed from signup!
 453          if (module_exists('content')) {
 454            // Due to a bug in CCK (http://drupal.org/node/363456), we need
 455            // to call this function twice to ensure we get the real value.
 456            // The bug is present when you first enable the setting to
 457            // display in the node instead of a separate tab, or when you
 458            // first upgrade to the version that contains this code.
 459            content_extra_field_weight($node->type, 'image_attach');
 460            $weight = content_extra_field_weight($node->type, 'image_attach');
 461          }
 462          else {
 463            $weight = variable_get("image_attach_weight_{$teaser_or_body}_{$node->type}", 0);
 464          }
 465          $node->content['image_attach'] = array(
 466            '#weight' => $weight,
 467            '#value' => theme('image_attach_attached_images_node', $node->nid, $node->image_attach, $img_size, $teaser),
 468          );
 469        }
 470        break;
 471  
 472      case 'rss item':
 473        $ret = array();
 474        if (!empty($node->iids) && $image = node_load($node->iids[0])) {
 475          $info = image_get_info(file_create_path($image->images[IMAGE_PREVIEW]));
 476          $ret[] = array(
 477            'key' => 'enclosure',
 478            'attributes' => array(
 479              'url' => url("image/view/{$node->iids[0]}/" . IMAGE_PREVIEW, array('absolute' => TRUE)),
 480              'length' => $info['file_size'],
 481              'type' => $info['mime_type'],
 482            ),
 483          );
 484        }
 485        return $ret;
 486    }
 487  }
 488  
 489  /**
 490   * Helper function for hook_nodeapi: view op.
 491   * Loads all the required attached image nodes.
 492   *
 493   * @param $attached_nids
 494   *  An array of node ids.
 495   * @param $teaser
 496   *  If true, only load the first of the node ids.
 497   *
 498   * @return
 499   *  An numerical array of node objects.
 500   */
 501  function _image_attach_node_load_attached($attached_nids, $teaser = FALSE) {
 502    if ($teaser) {
 503      // For the teaser we only want the first image from $attached_nids. 
 504      // During normal node viewing the array is zero-keyed but during edit
 505      // preview it is keyed by iid. Therefore using array_shift() will return the
 506      // first image for either of these situations.
 507      $attached_nids = array(array_shift($attached_nids));
 508    }
 509    foreach ($attached_nids as $nid) {
 510      $nodes[] = node_load($nid);
 511    }
 512  
 513    return $nodes;
 514  }
 515  
 516  /**
 517   * Fetch an array of all candidate referenced nodes, for use in presenting the
 518   * selection form to the user.
 519   *
 520   * @param $nids
 521   *  A list of nids to filter on. If not passed, all image nids are returned.
 522   */
 523  function _image_attach_get_image_nodes($nids = array()) {
 524    $placeholder = '';
 525    // If $nids was passed, build placeholders to put in the query
 526    if (count($nids)) {
 527      $placeholder = 'AND n.nid IN (' . implode(', ', array_fill(0, sizeof($nids), '%d')) . ') ';
 528    }
 529  
 530    $rows = array(0 => t('- None -'));
 531  
 532    $result = db_query(db_rewrite_sql("SELECT n.nid, n.title, n.sticky FROM {node} n WHERE n.status = 1 AND n.type = 'image' " . $placeholder . "ORDER BY n.sticky DESC, n.title ASC"), $nids);
 533    while ($node = db_fetch_object($result)) {
 534      $rows[$node->nid] = $node->title;
 535    }
 536  
 537    return $rows;
 538  }
 539  
 540  /**
 541   * Views 2 API handler
 542   */
 543  function image_attach_views_api() {
 544    return array(
 545      'api' => 2,
 546      'path' => drupal_get_path('module', 'image_attach'),
 547    );
 548  }
 549  
 550  /**
 551   * Implementation of hook_theme() registry.
 552   */
 553  function image_attach_theme() {
 554    return array(
 555      'image_attach_attached_images' => array(
 556        'arguments' => array(
 557          'nid' => 0,
 558          'image_nodes' => array(),
 559          'options' => array(),
 560        ),
 561      ),
 562      'image_attach_attached_images_node' => array(
 563        'arguments' => array(
 564          'node' => new stdClass(),
 565          'image_nodes' => array(),
 566          'img_size' => IMAGE_THUMBNAIL,
 567          'teaser' => FALSE,
 568        ),
 569      ),
 570      'image_attach_attached_images_block' => array(
 571        'arguments' => array(
 572          'nid' => 0,
 573          'image_nodes' => array(),
 574        ),
 575      ),
 576    );
 577  }
 578  
 579  /**
 580   * Theme attached images shown in nodes.
 581   *
 582   * @param $nid
 583   *  The attaching node's id.
 584   * @param $image_nodes
 585   *   The node objects of the images to theme.
 586   * @param $img_size
 587   *   The size at which to show images.
 588   * @param $teaser
 589   *   Whether the $node is being shown as a teaser or not.
 590   *
 591   * Override this in template.php to include a case statement if you want different node types to appear differently.
 592   * If you have additional image sizes you defined in image.module, you can use them by theming this function as well.
 593   */
 594  function theme_image_attach_attached_images_node($nid, $image_nodes, $img_size, $teaser = FALSE) {
 595    drupal_add_css(drupal_get_path('module', 'image_attach') . '/image_attach.css');
 596  
 597    $options = array(
 598      'size' => $img_size,
 599      'link' => $teaser ? 'node' : 'image',
 600      'attributes' => array(
 601        'class' => 'image-attach-' . ($teaser ? 'teaser' : 'body'),
 602      ),
 603    );
 604  
 605    // We take the images in reverse order because they are floated to the right,
 606    // and we want the apparent left to right order to be correct.
 607    $output = theme('image_attach_attached_images', $nid, array_reverse($image_nodes), $options);
 608  
 609    // Wrap output of potentially multiple images in a DIV.
 610    if ($output && !$teaser) {
 611      $output = '<div class="all-attached-images">' . $output . '</div>';
 612    }
 613  
 614    return $output;
 615  }
 616  
 617  /**
 618   * Theme the attached images block.
 619   *
 620   * @param $nid
 621   *  The attaching node's id.
 622   * @param $image_nodes
 623   *  The attached image nodes.
 624   */
 625  function theme_image_attach_attached_images_block($nid, $image_nodes = array()) {
 626    // Only return block content if there are images.
 627    if (is_array($image_nodes) && count($image_nodes)) {
 628      $options = array(
 629        'size' => variable_get('image_attach_block_0_size', IMAGE_THUMBNAIL),
 630        'link' => 'image',
 631      );
 632  
 633      $output = theme('image_attach_attached_images', $nid, $image_nodes, $options);
 634      $output = '<div class="all-attached-images">' . $output . '</div>';
 635  
 636      return $output;
 637    }
 638  }
 639  
 640  /**
 641   * Generic theme function for any set of attached images.
 642   *
 643   * @param $nid
 644   *  The id of the attaching node.
 645   * @param $image_nodes
 646   *  The fully loaded image nodes to theme. These do not need to be checked for
 647   *  access: that happens in this function.
 648   * @param $options
 649   *  An associative array of options, with the following keys:
 650   *    - 'size' (default IMAGE_THUMBNAIL)
 651   *      The name of the image derivative size at which to show the images,
 652   *      eg 'thumbnail'.
 653   *    - 'link' (default 'image')
 654   *      Whether and where the images should be linked. This should be one of:
 655   *        - 'image': link to the image node. Default.
 656   *        - 'node': link to the attaching node.
 657   *        - 'none': no link.
 658   *    - 'attributes'
 659   *      Extra attributes for the div around each image.
 660   */
 661  function theme_image_attach_attached_images($nid, $image_nodes = array(), $options = array()) {
 662    // Merge in defaults.
 663    $options += array(
 664      'size' => IMAGE_THUMBNAIL,
 665      'link' => 'image',
 666      'attributes' => array(),
 667    );
 668  
 669    $img_size = $options['size'];
 670    $link_destination = $options['link'];
 671  
 672    // Link images to the attaching node.
 673    if ($link_destination == 'node') {
 674      $link_path = "node/$nid";
 675    }
 676  
 677    $output = '';
 678    foreach ($image_nodes as $image) {
 679      if (!node_access('view', $image)) {
 680        // If the image is restricted, don't show it as an attachment.
 681        continue;
 682      }
 683  
 684      // Link images to the image node.
 685      if ($link_destination == 'image') {
 686        $link_path = "node/$image->nid";
 687      }
 688  
 689      // Get a fresh copy of the attributes for each image node.
 690      $div_attributes = $options['attributes'];
 691  
 692      // Create CSS classes, beginning with those passed in to the function.
 693      $classes = array();
 694      if (isset($div_attributes['class'])) {
 695        $classes[] = $div_attributes['class'];
 696      }
 697      // replace with base class in DIV
 698      //$classes[] = 'image-attach-' . $teaser_or_body;
 699      $classes[] = 'image-attach-node-' . $image->nid;
 700      if (!$image->status) {
 701        $classes[] = 'image-unpublished';
 702      }
 703      $div_attributes['class'] = implode(' ', $classes);
 704  
 705      // Add the width as inline CSS.
 706      $info = image_get_info(file_create_path($image->images[$img_size]));
 707      if (!isset($div_attributes['style'])) {
 708        $div_attributes['style'] = '';
 709      }
 710      $div_attributes['style'] .= 'width: ' . $info['width'] . 'px;';
 711  
 712      $output .= '<div' . drupal_attributes($div_attributes) . '>';
 713      $image_img = image_display($image, $img_size);
 714      if (isset($link_path)) {
 715        $output .= l($image_img, $link_path, array('html' => TRUE));
 716      }
 717      else {
 718        $output .= $image_img;
 719      }
 720      $output .= "</div>\n";
 721    }
 722  
 723    return $output;
 724  }
 725  
 726  /**
 727   * Implementation of hook_content_extra_fields().
 728   */
 729  function image_attach_content_extra_fields($type_name) {
 730    if (variable_get('image_attach_' . $type_name, 0)) {
 731      $extra['image_attach'] = array(
 732        'label' => t('Attached images'),
 733        'description' => t('Image Attach module form.'),
 734        'weight' => 0,
 735      );
 736      return $extra;
 737    }
 738  }
 739  


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