| 1 |
<?php
|
| 2 |
// $Id: node_images.pages.inc,v 1.0 2008/05/01 23:59:24 stefano73 Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Page callbacks for adding, editing and deleting node images.
|
| 7 |
*/
|
| 8 |
|
| 9 |
function _node_images_edit_form(&$form_state, $node) {
|
| 10 |
global $user;
|
| 11 |
|
| 12 |
$form = array(
|
| 13 |
'#cache' => FALSE,
|
| 14 |
'#prefix' => '<div id="node_images-wrapper">',
|
| 15 |
'#suffix' => '</div>',
|
| 16 |
);
|
| 17 |
|
| 18 |
if (_node_images_access('create', $node)) {
|
| 19 |
$form['new'] = array(
|
| 20 |
'#type' => 'fieldset',
|
| 21 |
'#title' => t('Upload a new image'),
|
| 22 |
'#collapsible' => TRUE,
|
| 23 |
'#collapsed' => !empty($node->node_images),
|
| 24 |
'#weight' => -30,
|
| 25 |
'#theme' => 'node_images_form_upload',
|
| 26 |
);
|
| 27 |
$limits = _node_images_file_limits($user);
|
| 28 |
$form['new']['description'] = array(
|
| 29 |
'#type' => 'textfield',
|
| 30 |
'#title' => t('Description'),
|
| 31 |
'#size' => 50,
|
| 32 |
'#maxlength' => 255,
|
| 33 |
'#default_value' => '',
|
| 34 |
'#description' => t('Enter a description for the new image, max 255 chars.'),
|
| 35 |
);
|
| 36 |
$form['new']['weight'] = array(
|
| 37 |
'#type' => 'weight',
|
| 38 |
'#title' => t('Weight'),
|
| 39 |
);
|
| 40 |
$form['new']['list'] = array(
|
| 41 |
'#type' => 'radios',
|
| 42 |
'#title' => t('List'),
|
| 43 |
'#options' => array(1 => t('Yes'), 0 => t('No')),
|
| 44 |
'#default_value' => 1,
|
| 45 |
);
|
| 46 |
$form['new']['node_images_file'] = array(
|
| 47 |
'#type' => 'file',
|
| 48 |
'#title' => t('Attach new image'),
|
| 49 |
'#size' => 40,
|
| 50 |
'#description' => ($limits['resolution'] ? t('Images larger than %resolution will be resized. ', array('%resolution' => $limits['resolution'])) : '') . t('The maximum upload size is %filesize. Only files with the following extensions may be uploaded: %extensions. ', array('%extensions' => $limits['extensions'], '%filesize' => format_size($limits['file_size']))),
|
| 51 |
);
|
| 52 |
$form['new']['attach'] = array(
|
| 53 |
'#type' => 'submit',
|
| 54 |
'#value' => t('Upload'),
|
| 55 |
'#name' => 'attach',
|
| 56 |
'#ahah' => array(
|
| 57 |
'path' => 'node_images/js',
|
| 58 |
'wrapper' => 'node_images-wrapper',
|
| 59 |
'progress' => array('type' => 'bar', 'message' => t('Please wait...')),
|
| 60 |
),
|
| 61 |
'#submit' => array('_node_images_upload_submit'),
|
| 62 |
);
|
| 63 |
// This value is used in _node_images_js().
|
| 64 |
$form['new']['nid'] = array('#type' => 'hidden', '#value' => $node->nid);
|
| 65 |
}
|
| 66 |
|
| 67 |
$form['node_images'] = array(
|
| 68 |
'#theme' => 'node_images_form_list',
|
| 69 |
'#tree' => TRUE,
|
| 70 |
'#type' => 'fieldset',
|
| 71 |
'#title' => t('Image list'),
|
| 72 |
'#weight' => 10,
|
| 73 |
);
|
| 74 |
|
| 75 |
if (!empty($node->node_images) && is_array($node->node_images)) {
|
| 76 |
// set an appropriate value for delta in weight selectbox
|
| 77 |
$delta = db_result(db_query('SELECT MAX(ABS(weight)) FROM {node_images} WHERE nid=%d', $node->nid));
|
| 78 |
$delta += count($node->node_images);
|
| 79 |
|
| 80 |
foreach ($node->node_images as $key => $file) {
|
| 81 |
$file = (object)$file;
|
| 82 |
$url = file_create_url($file->filepath);
|
| 83 |
$thumb = file_create_url($file->thumbpath);
|
| 84 |
$description = file_create_url($file->filepath);
|
| 85 |
$description = "<small>". check_plain($description) ."</small>";
|
| 86 |
$row = array();
|
| 87 |
$row['id'] = array('#type' => 'value', '#value' => $file->id);
|
| 88 |
$row['delete'] = array('#type' => 'checkbox');
|
| 89 |
$row['list'] = array('#type' => 'checkbox', '#default_value' => $file->list);
|
| 90 |
$row['thumbnail'] = array('#value' => '<img src="'.$thumb.'" alt="" />');
|
| 91 |
$row['description'] = array('#type' => 'textfield', '#default_value' => $file->description, '#maxlength' => 255, '#size' => 40);
|
| 92 |
$row['filepath'] = array('#value' => l($file->filepath, file_create_url($file->filepath), array('attributes' => array('target' => '_blank'))));
|
| 93 |
$row['filesize'] = array('#value' => format_size($file->filesize));
|
| 94 |
$row['date'] = array('#value' => format_date($file->timestamp));
|
| 95 |
$row['weight'] = array('#type' => 'weight', '#delta' => $delta, '#default_value' => $file->weight);
|
| 96 |
$form['node_images']['images'][$key] = $row;
|
| 97 |
}
|
| 98 |
|
| 99 |
$form['node_images']['submit'] = array('#type' => 'submit', '#value' => t('Save changes'), '#submit' => array('_node_images_form_submit'));
|
| 100 |
}
|
| 101 |
|
| 102 |
$form['nid'] = array('#type' => 'value', '#value' => $node->nid);
|
| 103 |
|
| 104 |
$form['#attributes']['enctype'] = 'multipart/form-data';
|
| 105 |
return $form;
|
| 106 |
}
|
| 107 |
|
| 108 |
/**
|
| 109 |
* Save changes to the image list.
|
| 110 |
*
|
| 111 |
* @param $form
|
| 112 |
* The form structure.
|
| 113 |
* @param $form_state
|
| 114 |
* An associative array containing the current state of the form.
|
| 115 |
*/
|
| 116 |
function _node_images_form_submit($form, &$form_state) {
|
| 117 |
global $user;
|
| 118 |
|
| 119 |
$node = node_load($form_state['values']['nid']);
|
| 120 |
$update_access = _node_images_access('update', $node);
|
| 121 |
$delete_access = _node_images_access('delete', $node);
|
| 122 |
|
| 123 |
foreach($form_state['values']['node_images']['images'] as $id => $values) {
|
| 124 |
if ($values['delete']) {
|
| 125 |
$r = db_fetch_object(db_query('SELECT filepath, thumbpath, uid FROM {node_images} WHERE id=%d AND nid=%d',
|
| 126 |
$id, $node->nid));
|
| 127 |
|
| 128 |
// if user has no delete access to the node, he can delete his own images only
|
| 129 |
if (!$delete_access && $user->uid != $r->uid) continue;
|
| 130 |
|
| 131 |
// delete selected image
|
| 132 |
file_delete($r->filepath);
|
| 133 |
file_delete($r->thumbpath);
|
| 134 |
db_query('DELETE FROM {node_images} WHERE id=%d AND nid=%d', $id, $node->nid);
|
| 135 |
}
|
| 136 |
else {
|
| 137 |
$object = (object) $values;
|
| 138 |
$object->nid = $node->nid;
|
| 139 |
$update = array('id', 'nid');
|
| 140 |
// if user has no delete access to the node, he can update his own images only
|
| 141 |
if (!$update_access) {
|
| 142 |
$update['uid'] = $user->uid;
|
| 143 |
}
|
| 144 |
drupal_write_record('node_images', $object, $update);
|
| 145 |
}
|
| 146 |
}
|
| 147 |
drupal_set_message(t('The changes have been saved.'));
|
| 148 |
}
|
| 149 |
|
| 150 |
/**
|
| 151 |
* Theme the upload form.
|
| 152 |
*
|
| 153 |
* @ingroup themeable
|
| 154 |
*/
|
| 155 |
function theme_node_images_form_upload(&$form) {
|
| 156 |
$output = '<div style="float:left; margin-right:20px;">';
|
| 157 |
$output .= drupal_render($form['description']);
|
| 158 |
$output .= '</div>';
|
| 159 |
$output .= '<div style="float:left; margin-right:20px;">';
|
| 160 |
$output .= drupal_render($form['weight']);
|
| 161 |
$output .= '</div>';
|
| 162 |
$output .= '<div style="float:left;">';
|
| 163 |
$output .= drupal_render($form['list']);
|
| 164 |
$output .= '</div>';
|
| 165 |
|
| 166 |
$output .= '<div style="clear:both;">';
|
| 167 |
$output .= drupal_render($form);
|
| 168 |
return $output;
|
| 169 |
}
|
| 170 |
|
| 171 |
/**
|
| 172 |
* Theme the image list.
|
| 173 |
*
|
| 174 |
* @ingroup themeable
|
| 175 |
*/
|
| 176 |
function theme_node_images_form_list(&$form) {
|
| 177 |
$header = array('', t('Delete'), t('List'), t('Thumbnail'), t('Description and info'), t('Weight'), t('Size'));
|
| 178 |
drupal_add_tabledrag('node_images_list', 'order', 'sibling', 'node_images-weight');
|
| 179 |
|
| 180 |
foreach (element_children($form['images']) as $key) {
|
| 181 |
if (!$form['images'][$key]['thumbnail']) continue;
|
| 182 |
|
| 183 |
// Add class to group weight fields for drag and drop.
|
| 184 |
$form['images'][$key]['weight']['#attributes']['class'] = 'node_images-weight';
|
| 185 |
|
| 186 |
$info = '<div class="node_images_info">'.t('Uploaded on: %date', array('%date' => drupal_render($form['images'][$key]['date']))).'</div>';
|
| 187 |
$info .= '<div class="node_images_info">'.t('Path: !path', array('!path' => drupal_render($form['images'][$key]['filepath']))).'</div>';
|
| 188 |
|
| 189 |
$row = array('');
|
| 190 |
$row[] = drupal_render($form['images'][$key]['delete']);
|
| 191 |
$row[] = drupal_render($form['images'][$key]['list']);
|
| 192 |
$row[] = drupal_render($form['images'][$key]['thumbnail']);
|
| 193 |
$row[] = array('data' => drupal_render($form['images'][$key]['description']).$info, 'width' => '100%');
|
| 194 |
$row[] = drupal_render($form['images'][$key]['weight']);
|
| 195 |
$row[] = array('data' => drupal_render($form['images'][$key]['filesize']), 'class' => 'nowrap');
|
| 196 |
$rows[] = array('data' => $row, 'class' => 'draggable');
|
| 197 |
}
|
| 198 |
|
| 199 |
$output = '';
|
| 200 |
if (!empty($rows)) $output .= theme('table', $header, $rows, array('id' => 'node_images_list'));
|
| 201 |
$output .= drupal_render($form);
|
| 202 |
return $output;
|
| 203 |
}
|
| 204 |
|
| 205 |
|
| 206 |
/************************************************************
|
| 207 |
* Gallery functions
|
| 208 |
************************************************************/
|
| 209 |
|
| 210 |
function _node_images_gallery($node) {
|
| 211 |
if (empty($node->node_images)) {
|
| 212 |
drupal_set_message(t('No images uploaded for this content.'));
|
| 213 |
if (user_access('create node images') && node_access('update', $node)) {
|
| 214 |
$output = t('Click <a href="!url">here</a> to upload new images.', array('!url' => url('node/'.$node->nid.'/images')));
|
| 215 |
}
|
| 216 |
return '<p>'.$output.'</p>';
|
| 217 |
}
|
| 218 |
|
| 219 |
$settings = array('images' => array());
|
| 220 |
|
| 221 |
$i = 1;
|
| 222 |
$thumbs = array();
|
| 223 |
foreach ($node->node_images as $id=>$image) {
|
| 224 |
$thumb_path = file_create_url($image->thumbpath);
|
| 225 |
$thumbs[$i] = array(
|
| 226 |
'src' => $thumb_path,
|
| 227 |
'title' => $image->description,
|
| 228 |
);
|
| 229 |
|
| 230 |
$path = $image->filepath;
|
| 231 |
$info = image_get_info($path);
|
| 232 |
$settings['images'][$i++] = array(
|
| 233 |
'src' => file_create_url($path),
|
| 234 |
'title' => $image->description,
|
| 235 |
'description' => $image->description,
|
| 236 |
'width' => $info['width'],
|
| 237 |
'height' => $info['height'],
|
| 238 |
'thumb' => $thumb_path,
|
| 239 |
);
|
| 240 |
}
|
| 241 |
$total = count($settings['images']);
|
| 242 |
|
| 243 |
if ($total > 0) {
|
| 244 |
$current = (int)$_GET['page'];
|
| 245 |
$current = ($current < 1 || $current > $total) ? 1 : $current;
|
| 246 |
|
| 247 |
$settings['current'] = $current;
|
| 248 |
$settings['total'] = $total;
|
| 249 |
|
| 250 |
$path = drupal_get_path('module', 'node_images');
|
| 251 |
drupal_add_js($path.'/node_images.js');
|
| 252 |
drupal_add_js(array('node_images_slideshow' => array($node->nid => $settings)), 'setting');
|
| 253 |
drupal_add_css($path.'/node_images.css');
|
| 254 |
|
| 255 |
$slideshow = array(
|
| 256 |
'#type' => 'node_images_gallery',
|
| 257 |
'#title' => $settings['images'][$current]['title'],
|
| 258 |
'#attributes' => array('id' => 'slideshow-'. $node->nid),
|
| 259 |
'#status' => array(
|
| 260 |
'current' => $current,
|
| 261 |
'total' => $total,
|
| 262 |
'previous' => $current <= 1 ? $total : $current - 1,
|
| 263 |
'next' => $current >= $total ? 1 : $current + 1,
|
| 264 |
),
|
| 265 |
'#image' => $settings['images'][$current],
|
| 266 |
'#weight' => -5,
|
| 267 |
);
|
| 268 |
}
|
| 269 |
|
| 270 |
$output = '<div class="node_images_slideshow">';
|
| 271 |
$output .= '<div class="large">'.drupal_render($slideshow).'</div>';
|
| 272 |
$output .= '<div class="thumbs">'.theme('node_images_gallery_thumbs', $thumbs).'</div>';
|
| 273 |
$output .= '</div>';
|
| 274 |
$output .= '<hr style="clear:both;" />'.node_view($node);
|
| 275 |
|
| 276 |
$title = t('Photo gallery for %title', array('%title' => $node->title));
|
| 277 |
drupal_set_title($title);
|
| 278 |
return $output;
|
| 279 |
}
|
| 280 |
|
| 281 |
function theme_node_images_gallery($element) {
|
| 282 |
$output = '<div'. drupal_attributes($element['#attributes']) .'>
|
| 283 |
<div class="header">
|
| 284 |
'. l(t('Previous'), $_GET['q'], array('attributes' => array('class' => 'previous'), 'query' => 'page='. $element['#status']['previous'])) .' |
|
| 285 |
'. t('Image !current of !total', array(
|
| 286 |
'!current' => '<span class="current">'. $element['#status']['current'] .'</span>',
|
| 287 |
'!total' => '<span class="total">'. $element['#status']['total'] .'</span>')) .' |
|
| 288 |
'. l(t('Next'), $_GET['q'], array('attributes' => array('class' => 'next'), 'query' => 'page='. $element['#status']['next'])) .'
|
| 289 |
</div>
|
| 290 |
<img src="'. url($element['#image']['src']) .'" class="polaroid" />
|
| 291 |
<p class="description">'. $element['#title'] .'</p>
|
| 292 |
</div>';
|
| 293 |
return $output;
|
| 294 |
}
|
| 295 |
|
| 296 |
function theme_node_images_gallery_thumbs($thumbs, $cols = 2) {
|
| 297 |
$output = '';
|
| 298 |
foreach($thumbs as $id => $thumb) {
|
| 299 |
$description = '<div class="thumb-description">'.truncate_utf8($thumb['title'], 40, FALSE, TRUE).'</div>';
|
| 300 |
$output .= '<div class="thumb">'.l('<img src="'.$thumb['src'].'" class="slideshow-thumb" id="thumb-'.$id.'" />', $_GET['q'],
|
| 301 |
array('title' => $thumb['title'], 'rel' => 'nofollow', 'html' => TRUE), 'page='.$id, NULL, FALSE, TRUE).$description.'</div>';
|
| 302 |
}
|
| 303 |
return $output;
|
| 304 |
}
|