| 1 |
<?php
|
| 2 |
// $Id: cck_multimage.module,v 1.3 2007/08/18 06:27:17 silviogutierrez Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Defines an image field type.
|
| 7 |
* imagefield uses content.module to store the fid, and the drupal files
|
| 8 |
* table to store the actual file data.
|
| 9 |
*
|
| 10 |
*/
|
| 11 |
|
| 12 |
|
| 13 |
/**
|
| 14 |
* Implementation of hook_perm() in order implement access control to CCK Multimage administration.
|
| 15 |
*/
|
| 16 |
function cck_multimage_perm() {
|
| 17 |
return array('administer CCK Multimage');
|
| 18 |
}
|
| 19 |
|
| 20 |
|
| 21 |
/**
|
| 22 |
* Implemenation of hook_menu().
|
| 23 |
*/
|
| 24 |
function cck_multimage_menu($maycache) {
|
| 25 |
$items = array();
|
| 26 |
if (!$maycache) {
|
| 27 |
$items[] = array(
|
| 28 |
'path' => 'admin/content/cck-multimages',
|
| 29 |
'title' => t('CCK Multimage'),
|
| 30 |
'description' => t('View, Edit and Administer your Multimages.'),
|
| 31 |
'callback' => 'cck_multimage_admin',
|
| 32 |
'access' => user_access('administer CCK Multimage'),
|
| 33 |
|
| 34 |
);
|
| 35 |
}
|
| 36 |
return $items;
|
| 37 |
|
| 38 |
}
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
/**
|
| 44 |
* Implementaiton of hook_help().
|
| 45 |
*/
|
| 46 |
function cck_multimage_help($section) {
|
| 47 |
switch ($section) {
|
| 48 |
case 'admin/content/cck-multimages':
|
| 49 |
return t('The fields listed below are candidates for displaying as a multimage. In order display a multimage, a node type must contain an image field with multiple values.');
|
| 50 |
}
|
| 51 |
}
|
| 52 |
|
| 53 |
|
| 54 |
/**
|
| 55 |
* Menu callback for displaying the cck multimage settings page.
|
| 56 |
*/
|
| 57 |
function cck_multimage_admin($node_type = NULL, $field_name = NULL) {
|
| 58 |
if ($node_type && $field_name) {
|
| 59 |
drupal_set_title(t('Edit multimage for %multimage', array('%multimage' => $node_type)));
|
| 60 |
return drupal_get_form('cck_multimage_edit_form', $node_type, $field_name);
|
| 61 |
}
|
| 62 |
else {
|
| 63 |
drupal_set_title('CCK Multimage Settings');
|
| 64 |
return cck_multimage_settings();
|
| 65 |
}
|
| 66 |
}
|
| 67 |
|
| 68 |
function cck_multimage_settings() {
|
| 69 |
$types = content_types();
|
| 70 |
|
| 71 |
$header = array(
|
| 72 |
array('data' => t('CCK Type and Field'), 'colspan' => 2),
|
| 73 |
array('data' => t('Teaser Display Status'), 'colspan' => 1),
|
| 74 |
array('data' => t('Full Display Status'), 'colspan' => 1),
|
| 75 |
array('data' => t('Operations'), 'colspan' => 2),
|
| 76 |
);
|
| 77 |
|
| 78 |
$rows = array();
|
| 79 |
foreach ($types as $type_name => $type) {
|
| 80 |
$multimage_fields = array();
|
| 81 |
foreach($type['fields'] as $field_name => $field) {
|
| 82 |
// If the type contains an image field add the field right away
|
| 83 |
if ($field['type'] == 'image') {
|
| 84 |
$multimage_fields[$field_name] = $field;
|
| 85 |
}
|
| 86 |
// If the type contains a node reference field, check if the referenced
|
| 87 |
// node type contains an image field.
|
| 88 |
elseif ($field['type'] == 'nodereference') {
|
| 89 |
$referenced_contains_image = false;
|
| 90 |
foreach ((array)$field['referenceable_types'] as $referenceable_type) {
|
| 91 |
if ($referenceable_type && $types[$referenceable_type]) {
|
| 92 |
foreach ($types[$referenceable_type]['fields'] as $referenceable_field) {
|
| 93 |
if ($referenceable_field['type'] == 'image') {
|
| 94 |
$referenced_contains_image = true;
|
| 95 |
}
|
| 96 |
}
|
| 97 |
}
|
| 98 |
}
|
| 99 |
if ($referenced_contains_image) {
|
| 100 |
$multimage_fields[$field_name] = $field;
|
| 101 |
}
|
| 102 |
}
|
| 103 |
}
|
| 104 |
if (!empty($multimage_fields)) {
|
| 105 |
$rows[] = array(array('data' => $type['name'], 'colspan' => 6));
|
| 106 |
}
|
| 107 |
foreach ($multimage_fields as $field_name => $field) {
|
| 108 |
$row = array();
|
| 109 |
$row[] = '';
|
| 110 |
$row[] = $field['field_name'];
|
| 111 |
$row[] = $field['display_settings']['teaser']['format'] == 'cck_multimage' ? '<strong>'. $field['display_settings']['teaser']['format'] .'<strong>' : $field['display_settings']['teaser']['format'];
|
| 112 |
$row[] = $field['display_settings']['full']['format'] == 'cck_multimage' ? '<strong>'. $field['display_settings']['full']['format'] .'<strong>' : $field['display_settings']['full']['format'];
|
| 113 |
$row[] = l(t('Change display settings'), 'admin/content/types/'. str_replace('_', '-', $type_name) .'/display', NULL, 'destination=admin/content/cck-multimages');
|
| 114 |
$row[] = l(t('Edit multimage'), 'admin/content/cck-multimages/'. $type_name .'/'. $field_name);
|
| 115 |
$rows[] = $row;
|
| 116 |
}
|
| 117 |
}
|
| 118 |
|
| 119 |
return theme('table', $header, $rows);
|
| 120 |
}
|
| 121 |
|
| 122 |
function cck_multimage_edit_form($node_type, $field_name) {
|
| 123 |
$settings = cck_multimage_get_settings($node_type, $field_name);
|
| 124 |
|
| 125 |
$form = array();
|
| 126 |
|
| 127 |
$form['node_type'] = array(
|
| 128 |
'#type' => 'value',
|
| 129 |
'#value' => $node_type,
|
| 130 |
);
|
| 131 |
|
| 132 |
$form['field_name'] = array(
|
| 133 |
'#type' => 'value',
|
| 134 |
'#value' => $field_name,
|
| 135 |
);
|
| 136 |
|
| 137 |
$form['preset'] = array(
|
| 138 |
'#type' => 'select',
|
| 139 |
'#title' => t('Imagecache Preset'),
|
| 140 |
'#description' => t('The imagecache module is used to generate the pictures for the multimage. Select an existing preset or configure a preset in the <a href="!imagecache">imagecache settings</a>.' , array('!imagecache' => url('admin/content/imagecache'))),
|
| 141 |
'#options' => array('' => 'Full size image') + drupal_map_assoc(_imagecache_get_presets()),
|
| 142 |
'#default_value' => $settings['preset'],
|
| 143 |
);
|
| 144 |
$form['teaser-preset'] = array(
|
| 145 |
'#type' => 'select',
|
| 146 |
'#title' => t('Imagecache Teaser Preset'),
|
| 147 |
'#description' => t('If you want to have multimage display on teasers, it is highly recommended you create a preset just for teasers with square dimensions, for example 100px x 100px. Select an existing preset or configure a preset in the <a href="!imagecache">imagecache settings</a>.' , array('!imagecache' => url('admin/content/imagecache'))),
|
| 148 |
// '#options' => array('' => 'Full size image') + drupal_map_assoc(_imagecache_get_presets()),
|
| 149 |
'#options' => array('' => 'Full size image') + drupal_map_assoc(_imagecache_get_presets()),
|
| 150 |
'#default_value' => $settings['teaser-preset'],
|
| 151 |
);
|
| 152 |
|
| 153 |
$form['detailed_css'] = array(
|
| 154 |
'#type' => 'checkbox',
|
| 155 |
'#title' => t('Use detailed CSS.'),
|
| 156 |
'#description' => t(' Each image field will have its own class distinguishing it from other image fields or even the same image field in other node types. Recommended if you want to apply a separate style to each image field depending on the content type or the field type.'),
|
| 157 |
'#default_value' => $settings['detailed_css'],
|
| 158 |
);
|
| 159 |
|
| 160 |
$form['captions'] = array(
|
| 161 |
'#type' => 'checkbox',
|
| 162 |
'#title' => t('Show captions.'),
|
| 163 |
'#description' => t('When uploading images, you can assign each one a caption to display. Make sure that the option "Enable Custom Title Text" is enabled in your imagefield options.'),
|
| 164 |
'#default_value' => $settings['captions'],
|
| 165 |
);
|
| 166 |
|
| 167 |
$form['compatibility'] = array(
|
| 168 |
'#type' => 'checkbox',
|
| 169 |
'#title' => t('IE6 Compatibility (Recommended)'),
|
| 170 |
'#description' => t('Sometimes, IE6\'s use of Fade In and Fade Out can behave strangely, flickering or misaligning. Since all testing for IE6 was done on a virtual machine, this may be why the effect behaved strangely. In compatibility mode, the effect will be absent in IE6. Experiment with either mode in a true installation of IE6 and provide feekdback. Important: This setting MUST be the same across all your Multimages, otherwise the two Javascript files will act at once, causing erratic behaviour.'),
|
| 171 |
'#default_value' => $settings['compatibility'],
|
| 172 |
);
|
| 173 |
|
| 174 |
$form['destination'] = array(
|
| 175 |
'#type' => 'hidden',
|
| 176 |
'#value' => 'admin/content/cck-multimages',
|
| 177 |
);
|
| 178 |
|
| 179 |
$form['op'] = array(
|
| 180 |
'#type' => 'submit',
|
| 181 |
'#value' => t('Save'),
|
| 182 |
);
|
| 183 |
|
| 184 |
return $form;
|
| 185 |
}
|
| 186 |
|
| 187 |
/**
|
| 188 |
* Validation handler for multimage edit form.
|
| 189 |
*/
|
| 190 |
function cck_multimage_edit_form_validate($form_id, $form_values) {
|
| 191 |
|
| 192 |
}
|
| 193 |
|
| 194 |
/**
|
| 195 |
* Submit handler for multimage edit form.
|
| 196 |
*/
|
| 197 |
function cck_multimage_edit_form_submit($form_id, $form_values) {
|
| 198 |
unset($form_values['op'], $form_values['form_id'], $form_values['form_token']);
|
| 199 |
variable_set('cck_multimage_'. $form_values['node_type'] .'_'. $form_values['field_name'], $form_values);
|
| 200 |
drupal_set_message(t('Multimage settings for %type saved.', array('%type' => $form_values['node_type'])));
|
| 201 |
}
|
| 202 |
|
| 203 |
|
| 204 |
/**
|
| 205 |
* Implementation of hook_field_formatter_info().
|
| 206 |
*/
|
| 207 |
function cck_multimage_field_formatter_info() {
|
| 208 |
$formatters = array(
|
| 209 |
'cck_multimage' => array(
|
| 210 |
'label' => 'CCK Multimage',
|
| 211 |
'field types' => array('image', 'nodereference'),
|
| 212 |
),
|
| 213 |
);
|
| 214 |
return $formatters;
|
| 215 |
}
|
| 216 |
|
| 217 |
|
| 218 |
/**
|
| 219 |
* Implementation of hook_nodeapi().
|
| 220 |
*/
|
| 221 |
function cck_multimage_nodeapi(&$node, $op, $teaser, $page) {
|
| 222 |
|
| 223 |
if ($op == 'view') {
|
| 224 |
$display_type = $teaser ? 'teaser' : 'full';
|
| 225 |
$type = content_types($node->type);
|
| 226 |
foreach ($type['fields'] as $field_name => $field) {
|
| 227 |
if ($field['display_settings'][$display_type]['format'] == 'cck_multimage') {
|
| 228 |
$settings = cck_multimage_get_settings($node->type, $field_name);
|
| 229 |
$node->content[$field_name]['#value'] = theme('cck_multimage', $node->{$field['field_name']}, $settings, $node, $field, $teaser);
|
| 230 |
}
|
| 231 |
}
|
| 232 |
}
|
| 233 |
}
|
| 234 |
|
| 235 |
function theme_cck_multimage($images, $settings, $node, $field, $teaser) {
|
| 236 |
|
| 237 |
if(!isset($images[0]['nid'])) {
|
| 238 |
//There seems to be some error with imagecache, when you delete all images from a node, it wont fully delete the first image info.
|
| 239 |
//This checks against this bug, makes sure it does not print out anything if there are no images.
|
| 240 |
return;
|
| 241 |
}
|
| 242 |
|
| 243 |
if ($teaser) {
|
| 244 |
$imagecache_directory = $settings['teaser-preset'];
|
| 245 |
$css_root = 'multimage-teaser';
|
| 246 |
}
|
| 247 |
else {
|
| 248 |
$imagecache_directory = $settings['preset'];
|
| 249 |
$css_root = 'multimage';
|
| 250 |
}
|
| 251 |
|
| 252 |
|
| 253 |
$multimage_image_id = $css_root . '-' . $node->nid .'-'. $field['field_name'];
|
| 254 |
$multimage_container = $css_root;
|
| 255 |
$multimage_image_class = 'multimage-image';
|
| 256 |
$multimage_top = $css_root . '-top';
|
| 257 |
$multimage_bottom = $css_root . '-bottom';
|
| 258 |
|
| 259 |
if ($settings['detailed_css']) {
|
| 260 |
$multimage_container .= ' ' . $css_root . '-' . $node->type . '-' . $field['field_name'];
|
| 261 |
$multimage_image_class .= ' ' . $css_root . '-' . $node->type . '-image' . '-' . $field['field_name'];
|
| 262 |
$multimage_top .= ' ' . $css_root . '-top-' . $node->type . '-' . $field['field_name'];
|
| 263 |
$multimage_bottom .= ' ' . $css_root . '-bottom-' . $node->type . '-' . $field['field_name'];
|
| 264 |
}
|
| 265 |
|
| 266 |
$max_height = 0;
|
| 267 |
$fileNames = array();
|
| 268 |
$fileCaptions = array();
|
| 269 |
|
| 270 |
foreach ($images as $image) {
|
| 271 |
$image_path = file_directory_path() .'/imagecache/'. $imagecache_directory .'/'. $image['filepath'];
|
| 272 |
$image_info = image_get_info($image_path);
|
| 273 |
$height = $image_info['height'];
|
| 274 |
|
| 275 |
if ($height > $max_height) {
|
| 276 |
$max_height = $height;
|
| 277 |
$max_height += 30; //Account for the fact that the bottom might run into the text.
|
| 278 |
}
|
| 279 |
|
| 280 |
array_push($fileNames, file_create_url(file_directory_path() .'/imagecache/'. $imagecache_directory .'/'. $image['filepath']));
|
| 281 |
if ($settings['captions'] && !$teaser) {
|
| 282 |
array_push($fileCaptions, $image['title']);
|
| 283 |
}
|
| 284 |
else {
|
| 285 |
array_push($fileCaptions, '');
|
| 286 |
}
|
| 287 |
|
| 288 |
}
|
| 289 |
|
| 290 |
$firstCaption = '';
|
| 291 |
if ($settings['captions'] && !$teaser) {
|
| 292 |
$firstCaption = $images[0]['title'];
|
| 293 |
}
|
| 294 |
|
| 295 |
|
| 296 |
$output = '';
|
| 297 |
|
| 298 |
$output .= '<div class="'. $multimage_container . '" style="height:' . $max_height . 'px;">';
|
| 299 |
$output .= '<div class="' . $multimage_top . '">';
|
| 300 |
$output .= theme('imagecache', $imagecache_directory, $images[0]['filepath'], '', $images[0]['title'], array('id' => $multimage_image_id, 'class' => $multimage_image_class));
|
| 301 |
$output .= '</div>';
|
| 302 |
$output .= '<div class="' . $multimage_bottom . '">' . $firstCaption . '</div>';
|
| 303 |
$output .= '<img class="multimage-buffer" src= "" alt="" style="display: none;" />';
|
| 304 |
$output .= '</div>';
|
| 305 |
|
| 306 |
drupal_add_js(array($multimage_image_id => $fileNames, $multimage_image_id . '-captions' => $fileCaptions, $multimage_image_id . '-mode' => (isset($images[1]) ? 'multiple' : 'single')), 'setting');
|
| 307 |
|
| 308 |
if ($settings['compatibility']) { //Add a dynamic JS file that detects the browser and removes the fade effect from IE6.
|
| 309 |
drupal_add_js(drupal_get_path('module', 'cck_multimage') .'/cck_multimage_javascript.php');
|
| 310 |
}
|
| 311 |
else { //Static JS File.
|
| 312 |
drupal_add_js(drupal_get_path('module', 'cck_multimage') .'/cck_multimage.js');
|
| 313 |
}
|
| 314 |
|
| 315 |
|
| 316 |
return $output;
|
| 317 |
}
|
| 318 |
|
| 319 |
/**
|
| 320 |
* Retreive the settings for a specific multimage.
|
| 321 |
*/
|
| 322 |
function cck_multimage_get_settings($node_type, $field_name) {
|
| 323 |
$settings = variable_get('cck_multimage_'. $node_type .'_'. $field_name, array());
|
| 324 |
$settings['detailed_css'] = $settings['detailed_css'];
|
| 325 |
$settings['captions'] = $settings['captions'];
|
| 326 |
$settings['compatibility'] = $settings['compatibility'];
|
| 327 |
return $settings;
|
| 328 |
}
|