| 1 |
<?php
|
| 2 |
// $Id: node_gallery.pages.inc,v 1.9 2009/04/26 02:45:19 kmonty Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Node gallery pages.
|
| 7 |
*
|
| 8 |
*/
|
| 9 |
|
| 10 |
function node_gallery_list($account = NULL) {
|
| 11 |
$galleries = gallery_gateway::find_by($account->uid);
|
| 12 |
|
| 13 |
if (!empty($galleries)) {
|
| 14 |
$gids = array_keys($galleries);
|
| 15 |
$image_counts = gallery_gateway::count_images($gids);
|
| 16 |
$gallery_covers = gallery_gateway::find_covers($gids);
|
| 17 |
if (!empty($gallery_covers)) {
|
| 18 |
$images = ImageGateway::get_file(array_values($gallery_covers));
|
| 19 |
}
|
| 20 |
|
| 21 |
foreach ($galleries as $k => $gallery) {
|
| 22 |
$galleries[$k]->cover = $images[$gallery_covers[$k]];
|
| 23 |
$galleries[$k]->image_count = empty($image_counts[$k]) ? 0 :$image_counts[$k];
|
| 24 |
}
|
| 25 |
|
| 26 |
foreach ($galleries as $gallery) {
|
| 27 |
$config = gallery_config_gateway::get_by($gallery->type);
|
| 28 |
$items[] = theme('gallery_cover_view', $gallery, $config);
|
| 29 |
}
|
| 30 |
}
|
| 31 |
|
| 32 |
return theme('gallery_list', $items, $account);
|
| 33 |
}
|
| 34 |
|
| 35 |
function node_gallery_upload_form($form_state, $gallery) {
|
| 36 |
$config = gallery_config_gateway::get_by($gallery->type);
|
| 37 |
//bad hack, this is for node_gallery_image item_form, since we use ahah in here;
|
| 38 |
drupal_add_tabledrag('upload-attachments', 'order', 'sibling', 'upload-weight');
|
| 39 |
drupal_add_js('misc/autocomplete.js');
|
| 40 |
|
| 41 |
$form['#attributes']['enctype'] = 'multipart/form-data';
|
| 42 |
$form['#node'] = $gallery;
|
| 43 |
|
| 44 |
$form['upload_wrapper'] = array(
|
| 45 |
'#prefix' => "<div id='gallery-upload-wrapper'>",
|
| 46 |
'#suffix' => "</div>",
|
| 47 |
);
|
| 48 |
|
| 49 |
$upload_number = 5;
|
| 50 |
if (is_numeric($config->upload_settings['number_uploads'])) {
|
| 51 |
$upload_number = $config->upload_settings['number_uploads'];
|
| 52 |
}
|
| 53 |
for ($i = 1; $i <= $upload_number; $i++) {
|
| 54 |
$form['upload_wrapper']['uploads-'. $i] = array(
|
| 55 |
'#type' => 'file',
|
| 56 |
'#title' => t('Please select a file'),
|
| 57 |
);
|
| 58 |
}
|
| 59 |
|
| 60 |
$form['upload_wrapper']['description'] = array(
|
| 61 |
'#value' => node_gallery_upload_limits(),
|
| 62 |
);
|
| 63 |
$form['upload_wrapper']['next'] = array(
|
| 64 |
'#type' => 'submit',
|
| 65 |
'#value' => t('Submit Files'),
|
| 66 |
'#weight' => 20,
|
| 67 |
'#ahah' => array(
|
| 68 |
'path' => 'node_gallery/upload/js',
|
| 69 |
'wrapper' => 'gallery-upload-wrapper',
|
| 70 |
'progress' => array('type' => 'bar', 'message' => t('Please wait...')),
|
| 71 |
),
|
| 72 |
);
|
| 73 |
|
| 74 |
return $form;
|
| 75 |
}
|
| 76 |
|
| 77 |
function node_gallery_upload_form_validate($form, $form_state) {
|
| 78 |
//bad hacks, since we use ahah, we have to validate the upload image nodes here.
|
| 79 |
if (!empty($form_state['values']['files'])) {
|
| 80 |
foreach ($form_state['values']['files'] as $key => $image_form_state) {
|
| 81 |
node_validate($image_form_state['edit_form'], $form['files'][$key]['edit_form']);
|
| 82 |
}
|
| 83 |
}
|
| 84 |
}
|
| 85 |
|
| 86 |
function node_gallery_upload_limits() {
|
| 87 |
global $user;
|
| 88 |
|
| 89 |
$limits = _upload_file_limits($user);
|
| 90 |
$items[] = t('Your total disk size is %total and you have used %used',
|
| 91 |
array('%total' => format_size($limits['user_size']), '%used' => format_size(file_space_used($user->uid))));
|
| 92 |
if ($limits['resolution']) {
|
| 93 |
$items[] = t('Images are larger than %resolution will be resized. ', array('%resolution' => $limits['resolution']));
|
| 94 |
}
|
| 95 |
$items[] = t('The maximum upload size is %filesize.', array('%filesize' => format_size($limits['file_size'])));
|
| 96 |
$items[] = t('Only files with the following extensions may be uploaded: %extensions.', array('%extensions' => $limits['extensions']));
|
| 97 |
|
| 98 |
return theme('item_list', $items, t('notes'), 'ul', array('class' => 'upload-notes'));
|
| 99 |
}
|
| 100 |
|
| 101 |
function node_gallery_upload_js() {
|
| 102 |
$cached_form_state = array();
|
| 103 |
$files = array();
|
| 104 |
|
| 105 |
// Load the form from the Form API cache.
|
| 106 |
if (!($cached_form = form_get_cache($_POST['form_build_id'], $cached_form_state)) || !isset($cached_form['#node']) || !isset($cached_form['upload_wrapper'])) {
|
| 107 |
form_set_error('form_token', t('Validation error, please try again. If this error persists, please contact the site administrator.'));
|
| 108 |
$output = theme('status_messages');
|
| 109 |
print drupal_to_js(array('status' => TRUE, 'data' => $output));
|
| 110 |
exit();
|
| 111 |
}
|
| 112 |
|
| 113 |
$form_state = array('values' => $_POST);
|
| 114 |
|
| 115 |
//this is upload form, we don't want to show the node's existing images;
|
| 116 |
unset($cached_form['#node']->images);
|
| 117 |
// Handle new uploads, and merge tmp files into node-files.
|
| 118 |
node_gallery_upload_images($cached_form, $form_state);
|
| 119 |
|
| 120 |
//if upload failed.
|
| 121 |
if (drupal_get_messages(NULL, FALSE)) {
|
| 122 |
print drupal_to_js(array('status' => TRUE, 'data' => theme('status_messages') . drupal_get_form('node_gallery_upload_form')));
|
| 123 |
exit;
|
| 124 |
}
|
| 125 |
|
| 126 |
$form = node_gallery_edit_images_form($form_state, $cached_form['#node']);
|
| 127 |
$cached_form['edit_images'] = $form;
|
| 128 |
form_set_cache($_POST['form_build_id'], $cached_form, $cached_form_state);
|
| 129 |
// Render the form for output.
|
| 130 |
$form += array(
|
| 131 |
'#post' => $_POST,
|
| 132 |
'#programmed' => FALSE,
|
| 133 |
'#tree' => FALSE,
|
| 134 |
'#parents' => array(),
|
| 135 |
);
|
| 136 |
//drupal_alter('form', $form, array(), 'upload_js');
|
| 137 |
$form_state = array('submitted' => FALSE);
|
| 138 |
$form = form_builder('node_gallery_upload_js', $form, $form_state);
|
| 139 |
|
| 140 |
print drupal_to_js(array('status' => TRUE, 'data' => theme('status_messages') . drupal_render($form)));
|
| 141 |
exit;
|
| 142 |
}
|
| 143 |
|
| 144 |
function node_gallery_upload_images(&$form, &$form_state) {
|
| 145 |
global $user;
|
| 146 |
|
| 147 |
$limits = _upload_file_limits($user);
|
| 148 |
$validators = array(
|
| 149 |
'file_validate_extensions' => array($limits['extensions']),
|
| 150 |
'file_validate_image_resolution' => array($limits['resolution']),
|
| 151 |
'file_validate_size' => array($limits['file_size'], $limits['user_size']),
|
| 152 |
);
|
| 153 |
|
| 154 |
// Save new file uploads.
|
| 155 |
if (($user->uid != 1 || user_access('upload files'))) {
|
| 156 |
$directory = node_gallery_check_directory($form['#node']);
|
| 157 |
foreach ($_FILES['files']['name'] as $id => $name) {
|
| 158 |
if (!empty($name)) {
|
| 159 |
$file = file_save_upload($id, $validators, $directory);
|
| 160 |
if (!empty($file)) {
|
| 161 |
$file->description = $file->filename;
|
| 162 |
$file->weight = 0;
|
| 163 |
$file->gid = $form['#node']->nid;
|
| 164 |
$form['#node']->images[$file->fid] = $file;
|
| 165 |
$form_state['values']['files'][$file->fid] = (array) $file;
|
| 166 |
}
|
| 167 |
}
|
| 168 |
}
|
| 169 |
}
|
| 170 |
|
| 171 |
// Order the form according to the set file weight values.
|
| 172 |
if (!empty($form_state['values']['files'])) {
|
| 173 |
$microweight = 0.001;
|
| 174 |
foreach ($form_state['values']['files'] as $fid => $file) {
|
| 175 |
if (is_numeric($fid)) {
|
| 176 |
$form_state['values']['files'][$fid]['#weight'] = $file['weight'] + $microweight;
|
| 177 |
$microweight += 0.001;
|
| 178 |
}
|
| 179 |
}
|
| 180 |
uasort($form_state['values']['files'], 'element_sort');
|
| 181 |
}
|
| 182 |
}
|
| 183 |
|
| 184 |
function node_gallery_check_directory($gallery) {
|
| 185 |
global $user;
|
| 186 |
$gallery_directory = strtr($gallery->get_config()->gallery_directory,
|
| 187 |
array('%uid' => $user->uid, '%username' => $user->name, '%gid' => $gallery->nid, '%gallery_name' => str_replace(' ', '_', $gallery->title)));
|
| 188 |
$directory = rtrim(file_directory_path() .'/'. $gallery_directory, '/\\');
|
| 189 |
//recursive mkdir;
|
| 190 |
if (!is_dir($directory)) {
|
| 191 |
mkdir($directory, 0777, TRUE);
|
| 192 |
}
|
| 193 |
file_check_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
|
| 194 |
|
| 195 |
return $directory;
|
| 196 |
}
|
| 197 |
|
| 198 |
function node_gallery_edit_images_form($form_state, $gallery) {
|
| 199 |
global $user;
|
| 200 |
|
| 201 |
$form = array(
|
| 202 |
'#theme' => 'gallery_edit_images_form',
|
| 203 |
'#cache' => TRUE,
|
| 204 |
);
|
| 205 |
//if this is new upload, then fid is file id;
|
| 206 |
//if this is edit images, then fid is node id;
|
| 207 |
//but fid is just an identifier here, so needn't care it.
|
| 208 |
foreach ($gallery->images as $fid => $f) {
|
| 209 |
$options[$fid] = '';
|
| 210 |
if ($f->is_cover) {
|
| 211 |
$cover_fid = $fid;
|
| 212 |
}
|
| 213 |
}
|
| 214 |
$form['is_cover'] = array(
|
| 215 |
'#type' => 'radios',
|
| 216 |
'#default_value' => $cover_fid,
|
| 217 |
'#options' => $options,
|
| 218 |
);
|
| 219 |
$form['gid'] = array(
|
| 220 |
'#type' => 'value',
|
| 221 |
'#value' => $gallery->nid,
|
| 222 |
);
|
| 223 |
$form['#config'] = $gallery->get_config();
|
| 224 |
|
| 225 |
if (!empty($gallery->images) && is_array($gallery->images)) {
|
| 226 |
$form['files']['#tree'] = TRUE;
|
| 227 |
|
| 228 |
foreach ($gallery->images as $key => $file) {
|
| 229 |
$file = (object)$file;
|
| 230 |
|
| 231 |
$form['files'][$key]['remove'] = array('#type' => 'checkbox', '#default_value' => $file->remove);
|
| 232 |
$form['files'][$key]['weight'] = array('#type' => 'weight', '#delta' => count($gallery->images), '#default_value' => $file->weight);
|
| 233 |
$form['files'][$key]['filename'] = array('#type' => 'value', '#value' => $file->filename);
|
| 234 |
$form['files'][$key]['filepath'] = array('#type' => 'value', '#value' => $file->filepath);
|
| 235 |
$form['files'][$key]['filemime'] = array('#type' => 'value', '#value' => $file->filemime);
|
| 236 |
$form['files'][$key]['filesize'] = array('#type' => 'value', '#value' => $file->filesize);
|
| 237 |
$form['files'][$key]['fid'] = array('#type' => 'value', '#value' => $file->fid);
|
| 238 |
$form['files'][$key]['gid'] = array('#type' => 'value', '#value' => $gallery->nid);
|
| 239 |
|
| 240 |
//get the required fields for image node edit and saving.
|
| 241 |
$file->type = empty($file->type) ? $gallery->get_config()->image_type : $file->type;
|
| 242 |
$file->gid = empty($file->gid) ? $gallery->nid : $file->gid;
|
| 243 |
$form['files'][$key]['edit_form'] = node_gallery_image_item_edit_form($file, $gallery->get_config());
|
| 244 |
}
|
| 245 |
}
|
| 246 |
|
| 247 |
$form['submit'] = array(
|
| 248 |
'#type' => 'submit',
|
| 249 |
'#value' => t('Submit'),
|
| 250 |
'#weight' => 20,
|
| 251 |
//need this because this form is used in image upload ahah also.
|
| 252 |
'#submit' => array('node_gallery_images_edit_submit'),
|
| 253 |
);
|
| 254 |
|
| 255 |
return $form;
|
| 256 |
}
|
| 257 |
|
| 258 |
function node_gallery_images_edit_submit($form, &$form_state) {
|
| 259 |
|
| 260 |
foreach ($form_state['values']['files'] as $fid => $form_values) {
|
| 261 |
$form_values += $form_values['edit_form'];
|
| 262 |
unset($form_values['edit_form']);
|
| 263 |
|
| 264 |
if ($form_values['remove']) {
|
| 265 |
$operations[] = array('images_delete_process', array(new Image($form_values)));
|
| 266 |
}
|
| 267 |
else {
|
| 268 |
if ($form_state['values']['is_cover'] == $fid) {
|
| 269 |
$form_values['is_cover'] = 1;
|
| 270 |
}
|
| 271 |
else {
|
| 272 |
$form_values['is_cover'] = 0;
|
| 273 |
}
|
| 274 |
$operations[] = array('images_upload_process', array($form['files'][$fid]['edit_form'], new Image($form_values)));
|
| 275 |
}
|
| 276 |
}
|
| 277 |
$batch = array(
|
| 278 |
'operations' => $operations,
|
| 279 |
'finished' => 'images_process_finished',
|
| 280 |
'title' => t('Processing Image Upload.'),
|
| 281 |
'init_message' => t('Images Upload is starting.'),
|
| 282 |
'progress_message' => t('Processed @current out of @total.'),
|
| 283 |
'error_message' => t('Images Upload has encountered an error.'),
|
| 284 |
);
|
| 285 |
|
| 286 |
batch_set($batch);
|
| 287 |
|
| 288 |
$form_state['redirect'] = 'node/'. $form_state['values']['gid'];
|
| 289 |
}
|
| 290 |
|
| 291 |
function node_gallery_image_item_edit_form($image, $config) {
|
| 292 |
|
| 293 |
module_load_include('inc', 'node', 'node.pages');
|
| 294 |
$form_state = array();
|
| 295 |
$display_fields = $config->display_fields;
|
| 296 |
$form = drupal_retrieve_form($image->type .'_node_form', $form_state, $image);
|
| 297 |
drupal_prepare_form($image->type ."_node_form", $form, $form_state);
|
| 298 |
foreach (array_values($display_fields) as $field_name) {
|
| 299 |
if (!empty($field_name)) {
|
| 300 |
$item_form[$field_name] = (array)get_image_form_item_recursive($form, $field_name);
|
| 301 |
}
|
| 302 |
}
|
| 303 |
$item_form += (array)get_image_form_value_items($form);
|
| 304 |
set_image_form_default_values($item_form, $image, $config);
|
| 305 |
|
| 306 |
return $item_form;
|
| 307 |
}
|
| 308 |
|
| 309 |
function get_image_form_item_recursive($form, $field) {
|
| 310 |
if ($form[$field]) {
|
| 311 |
return $form[$field];
|
| 312 |
}
|
| 313 |
elseif ($children = element_children($form)) {
|
| 314 |
foreach ($children as $child) {
|
| 315 |
get_image_form_item_recursive($form[$child], $field);
|
| 316 |
}
|
| 317 |
}
|
| 318 |
return ;
|
| 319 |
}
|
| 320 |
|
| 321 |
function get_image_form_value_items($form) {
|
| 322 |
if ($children = element_children($form)) {
|
| 323 |
foreach ($children as $child) {
|
| 324 |
//todo: didn't recursive;
|
| 325 |
if ($form[$child]['#type'] == 'value' || $form[$child]['#type'] == 'hidden') {
|
| 326 |
$value_forms[$child] = $form[$child];
|
| 327 |
}
|
| 328 |
}
|
| 329 |
}
|
| 330 |
elseif ($form['#type'] == 'value' || $form['#type'] == 'hidden') {
|
| 331 |
$value_forms[key($form)] = $form;
|
| 332 |
}
|
| 333 |
|
| 334 |
return $value_forms;
|
| 335 |
}
|
| 336 |
|
| 337 |
function set_image_form_default_values(&$form, $image, $config) {
|
| 338 |
global $user;
|
| 339 |
|
| 340 |
$form['title']['#default_value'] = empty($form['title']['#default_value']) ? $image->filename : $form['title']['#default_value'];
|
| 341 |
if (!empty($form['body_field'])) {
|
| 342 |
$tmp_item = $form['body_field']['body'];
|
| 343 |
unset($form['body_field']);
|
| 344 |
$form['body'] = $tmp_item;
|
| 345 |
$form['body']['#rows'] = 3;
|
| 346 |
$form['body']['#default_value'] = empty($form['body']['#default_value']) ? $image->filename : $form['body']['#default_value'];
|
| 347 |
}
|
| 348 |
if (!empty($form['changed']) && empty($form['changed']['#value'])) {
|
| 349 |
$form['changed']['#value'] = time();
|
| 350 |
}
|
| 351 |
if (user_access('administer nodes')) {
|
| 352 |
$form['name'] = array(
|
| 353 |
'#type' => 'value',
|
| 354 |
'#value' => $user->name,
|
| 355 |
);
|
| 356 |
}
|
| 357 |
if (!empty($form['uid']) && empty($form['uid']['#value'])) {
|
| 358 |
$form['uid']['#value'] = $user->uid;
|
| 359 |
}
|
| 360 |
$form['comment'] = array(
|
| 361 |
'#type' => 'value',
|
| 362 |
'#value' => $config->image_comment,
|
| 363 |
);
|
| 364 |
}
|