/[drupal]/contributions/modules/acidfree/class_album.inc
ViewVC logotype

Contents of /contributions/modules/acidfree/class_album.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.48 - (show annotations) (download) (as text)
Sat May 5 21:32:48 2007 UTC (2 years, 6 months ago) by vhmauery
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.47: +3 -3 lines
File MIME type: text/x-php
be sure to set parent album when browsing items with multiple parents
1 <?php
2 /* $Id: class_album.inc,v 1.47 2007/04/30 14:23:56 vhmauery Exp $ */
3 /*
4 Acidfree Photo Albums for Drupal
5 Copyright (C) 2005 Vernon Mauery
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 /**
23 * @file
24 * The is the class implementation of the album. All the acidfree elements
25 * must be defined in this same manner, which makes creating a new class
26 * relatively simple -- copy one of the existing classes and modify the
27 * relevant functions.
28 */
29
30 function class_album_register() {
31 $class = new stdClass();
32 $class->class = 'album';
33 $class->name = t('album');
34 $class->addme = t('Add an album');
35 $class->mime_ext = Array();
36 $class->form_alter = '_class_album_form_alter';
37 $class->nodeapi = '_class_album_nodeapi';
38 $class->access = 'create acidfree albums';
39 return $class;
40 }
41
42 function _acidfree_get_album_thumb(&$node) {
43 if ($node->thumb) {
44 return acidfree_get_node_by_id($node->thumb);
45 }
46 $child = _acidfree_get_children($node->tid, 1, 0, false, 'RAND()');
47
48 if (!$child) {
49 $child = _acidfree_get_children($node->tid, 1, 0, true, 'RAND()');
50 if (!$child) {
51 return '';
52 }
53 return _acidfree_get_album_thumb(acidfree_get_node_by_id($child[0]));
54 } else {
55 $thumb = acidfree_get_node_by_id($child[0]);
56 }
57 if ($thumb->type == 'video') {
58 if (isset($thumb->iid)) {
59 $thumb = acidfree_get_node_by_id($thumb->iid);
60 } else {
61 $thumb = '';
62 }
63 }
64 return $thumb;
65 }
66
67 /* recursively flatten an array tree, so that all
68 * that is left is $k => $v for $v is a leaf
69 */
70 function _flatten_array($a) {
71 $b = array();
72 foreach ($a as $k => $v) {
73 if (is_array($v)) {
74 $b = array_merge($b, _flatten_array($v));
75 } else {
76 $b[$k] = $v;
77 }
78 }
79 return $b;
80 }
81
82 function acidfree_album_contents_submit($form_id, $form) {
83 $op = isset($_POST['op']) ? $_POST['op'] : '';
84 //dump($form, true);
85 $tid = $form['album'];
86 global $acidfree_types;
87 $parent = $form['parent'];
88 $targets = array();
89 $vid = acidfree_get_vocab_id();
90 foreach ($form['nodes'] as $fnode) {
91 $n = acidfree_get_node_by_id($fnode['nid']);
92 // troublesome keys -- they use an array on the form
93 // and then don't know how to handle it when it is passed back
94 $trouble_keys = array('author','options','path','og_nodeapi', 'node_privacy_byrole');
95 // this takes care of class extras and title
96 foreach ($fnode as $k => $v) {
97 if (is_array($v) && in_array(strtolower($k), $trouble_keys)) {
98 $v = _flatten_array($v);
99 foreach ($v as $k => $vv) {
100 $n->$k = $vv;
101 }
102 } else {
103 $n->$k = $v;
104 }
105 }
106 if ($fnode['checked'] == '1') {
107 // move, copy, delete the node
108 switch ($op) {
109 case t('Delete'):
110 if ($n->type != 'acidfree' && count($n->taxonomy[$vid]) > 1) {
111 // delete from this album only
112 $key = array_search($tid, $n->taxonomy[$vid]);
113 unset($n->taxonomy[$vid][$key]);
114 drupal_set_message(t("removed '%item' from album",
115 array('%item' => $n->title)));
116 } else {
117 node_delete($n->nid);
118 }
119 unset($n);
120 break;
121 case t('Copy'):
122 if ($n->type != 'acidfree') {
123 // add dest->nid to parent list
124 $n->taxonomy[$vid][] = $parent[$vid];
125 }
126 break;
127 case t('Move'):
128 // replace node->nid with dest->nid in parent list
129 if ($n->type != 'acidfree') {
130 $key = array_search($tid, $n->taxonomy[$vid]);
131 unset($n->taxonomy[$vid][$key]);
132 $n->taxonomy[$vid][] = $parent[$vid];
133 } else {
134 $parents = _acidfree_get_ancestors(acidfree_get_node_by_id($parent));
135 $awry = ($parent == $n->nid);
136 foreach ($parents as $p) {
137 if ($p->nid == $n->nid) {
138 $awry = true;
139 break;
140 }
141 }
142 if (!$awry) {
143 $n->taxonomy = $parent;
144 } else {
145 drupal_set_message(t("Could not set '%item' to be own ancestor album",
146 array('%item' => $n->title)), 'error');
147 }
148 }
149 break;
150 }
151 }
152 if (isset($n)) {
153 $n = node_submit($n);
154 node_save($n);
155 }
156 }
157 // not sure why, but even though we pass the nodes around as references,
158 // somewhere things go awry and acidfree_get_node_by_id returns the
159 // old copy that is not the one we saved. so we just reload.
160 drupal_set_message(t('Changes saved.'));
161 $pager = _acidfree_make_pager_query();
162 $nid = _nid_from_tid($tid);
163 drupal_goto("node/{$nid}/contents", $pager);
164 }
165
166 function theme_acidfree_album_contents($form) {
167 if (count($form['nodes']) == 0)
168 return t('This album is empty')."<br/>";
169 $output .= drupal_render($form['pager']);
170 $output .= '<div class="container-inline" style="padding: 5px;">';
171 foreach (element_children($form['actionstop']) as $key) {
172 $output .= drupal_render($form['actionstop'][$key]) . '&nbsp;&nbsp;';
173 }
174 $output .= '</div>';
175
176 $i=0;
177 foreach (element_children($form['nodes']) as $child) {
178 $row[] = Array('data' => drupal_render($form['nodes'][$child]['checked']));
179 $node = $form['nodes'][$child]['#node'];
180 $cell = theme("acidfree_print_thumb_{$node->type}", $node);
181 $cell .= '<div class="block-bottom"></div>';
182 foreach (array_keys(acidfree_form_elements($form['nodes'][$child])) as $key) {
183 $cell .= drupal_render($form['nodes'][$child][$key]);
184 }
185 $row[] = Array( 'data' => $cell,
186 'class' => 'album-cell');
187 unset($cell);
188 $cell .= drupal_render($form['nodes'][$child]['title']);
189 $cell .= drupal_render($form['nodes'][$child]['term']['name']);
190 $cell .= drupal_render($form['nodes'][$child]['body']);
191 $cell .= drupal_render($form['nodes'][$child]['term']['description']);
192 $cell .= drupal_render($form['nodes'][$child]['parent']);
193 $row[] = Array('data' => $cell, 'class' => 'album-cell');
194 $cell = drupal_render($form['nodes'][$child]);
195 $row[] = Array('data' => $cell, 'class' => 'album-cell');
196 $rows[] = Array('data' => $row);
197 unset($row);
198 }
199 $output .= theme('table', NULL, $rows);
200 $output .= '<div class="container-inline" style="padding: 5px;">';
201 foreach (element_children($form['actionsbot']) as $key) {
202 $output .= drupal_render($form['actionsbot'][$key]) . '&nbsp;&nbsp;';
203 }
204 $output .= '</div>';
205 // $output .= '<pre>'; $output .= dump($form); $output .= '</pre>';
206 return $output.drupal_render($form);
207 }
208
209 function acidfree_album_contents(&$term) {
210 $vid = acidfree_get_vocab_id();
211 $form = array();
212 $term = taxonomy_get_term($term);
213 drupal_set_title(t('%title Contents', array('%title' => $term->name)));
214 drupal_set_breadcrumb($bc = acidfree_make_breadcrumbs($term));
215
216 $nodes_per_page = min(15, variable_get('acidfree_num_thumbs', 15));
217 if ($nodes_per_page <= 0) {
218 $nodes_per_page = 15;
219 }
220
221 $nodes = _acidfree_get_children($term->tid, $nodes_per_page, ALBUM_PAGER, true);
222 if (count($nodes) == 0)
223 return array('empty' => array('#value' => t("This album is empty")."<br/>"));
224 $form['album'] = array(
225 '#type' => 'hidden',
226 '#value' => $term->tid,
227 );
228 $form['pager'] = array(
229 '#value' => theme('pager', NULL, $nodes_per_page, ALBUM_PAGER),
230 );
231 $form['actionstop']['select'] = array(
232 '#type' => 'select',
233 '#default_value' => '0',
234 '#options' => array('0'=>' -- ', 'all'=>t('All'), 'none'=>t('None'), 'invert'=>t('Invert')),
235 '#attributes' => array('onchange' => 'select_nodes(this);'),
236 '#prefix' => '<div class="form-item"><label for="edit-select">Select: </label></div>',
237 );
238 $form['actionstop']['parent'] = _acidfree_parent_select($term);
239 $form['actionstop']['parent'][$vid]['#attributes'] = array('onchange' => 'update_parent_selects(this);');
240 //$form['actionstop']['parent'] = _acidfree_parent_select(null, $node->nid, null, false, array(), array('onchange' => 'update_parent_selects(this);'));
241 $form['actionstop']['parent'][$vid]['#title'] = t('Destination');
242 $form['actionstop']['move'] = array(
243 '#type' => 'submit',
244 '#value' =>t('Move'),
245 );
246 $form['actionstop']['copy'] = array(
247 '#type' => 'submit',
248 '#value' =>t('Copy'),
249 );
250 $form['actionstop']['delete'] = array(
251 '#type' => 'submit',
252 '#value' =>t('Delete'),
253 );
254 $form['actionstop']['submit'] = array(
255 '#type' => 'submit',
256 '#value' =>t('Submit'),
257 );
258 $form['nodes']['#tree'] = true;
259 $i = 0;
260 $form_alter_mods = module_implements('form_alter');
261 foreach ($nodes as $child) {
262 $child = acidfree_get_node_by_id($child);
263 $form['nodes'][$i] = node_form($child, $param);
264 unset($form['nodes'][$i]['submit']);
265 unset($form['nodes'][$i]['preview']);
266 unset($form['nodes'][$i]['delete']);
267 unset($form['nodes'][$i]['#after_build']);
268 $form['nodes'][$i]['type'] = array('#value' => $child->type);
269 $form['nodes'][$i]['checked'] = array(
270 '#type' => 'checkbox',
271 '#default_value' => 0,
272 '#tree' => true,
273 );
274 $form['nodes'][$i]['nid'] = array(
275 '#type' => 'hidden',
276 '#value' => $child->nid,
277 '#tree' => true,
278 );
279 $form['nodes'][$i]['#tree'] = true;
280 $form['nodes'][$i]['#node'] = $child;
281 foreach ($form_alter_mods as $module) {
282 $function = $module .'_form_alter';
283 $form_id = $child->type.'_node_form';
284 $function($form_id, $form['nodes'][$i]);
285 }
286 if (isset($form['nodes'][$i]['comment_settings'])) {
287 $form['nodes'][$i]['comment_settings']['comment']['#parents'] = array('nodes', "$i", 'comment');
288 }
289 //dump_msg($form['nodes'][$i]);
290 unset($form['nodes'][$i]['type']);
291 $i++;
292 continue;
293 }
294 $form['actionsbot'] = $form['actionstop'];
295 return $form;
296 }
297
298 /**
299 * This views generates the customized acidfree views based on the standard view
300 *
301 * @param $view_name
302 * The name or id of the view to load
303 * @param $args
304 * The arguments from the end of the url. Usually passed from the menu system.
305 *
306 * @return
307 * The view page.
308 */
309 function acidfree_album_view() {
310 $args = func_get_args();
311 $view_name = array_shift($args);
312 $view = views_get_view($view_name);
313 if (!$view) {
314 //drupal_set_message("view $viewname not found");
315 drupal_not_found();
316 exit;
317 }
318
319 $album = _album_from_tid($args[0]);
320 if (!$album->order_by || $album->order_by == '<default>') {
321 $album->order_by = variable_get('acidfree_order', 'node.nid DESC');
322 }
323 $album_sort = explode(' ', $album->order_by);
324 $view->sort[1] = array(
325 'vid' => $view->vid,
326 'position' => 1,
327 'field' => $album_sort[0],
328 'sortorder' => $album_sort[1],
329 'options' => NULL,
330 'tablename' => NULL,
331 'id' => $album_sort[0],
332 );
333
334 $view->debug = true;
335
336 if (($node_count = variable_get('acidfree_num_thumbs', 15)) <= 0) {
337 $view->use_pager = false;
338 unset($view->nodes_per_page);
339 } else {
340 $view->use_pager = true;
341 $view->nodes_per_page = $node_count;
342 }
343
344 $output = views_build_view('page', $view, $args, $view->use_pager, $view->nodes_per_page);
345 if ($output === FALSE) {
346 drupal_not_found();
347 exit;
348 }
349
350 $size = _image_get_dimensions('thumbnail');
351 // 5px padding + 1px border on both sides of image +
352 // 5px padding and 2px border on both sides of acidfree-item
353 // gives 26. Height needs more because it contains text underneath.
354 $style = '<style type="text/css" media="all">';
355 $style .= '.acidfree-cell{width:' . ($size['width'] + 26) . 'px;}';
356 $style .= '.acidfree .acidfree-cell{height:' . ($size['height'] + 45) . 'px;}';
357 $style .= '</style>';
358 drupal_set_html_head($style);
359
360 return $output;
361 }
362
363 function theme_views_view_album_grid_view($view, $type, $nodes, $teasers = false, $links = true) {
364 $term = taxonomy_get_term($view->args[0]);
365 drupal_set_breadcrumb(acidfree_make_breadcrumbs($term));
366 drupal_set_title($term->name);
367 if (count($nodes) == 0) {
368 return "<p>".t('This album is empty')."</p>";
369 }
370 $output = "\n<p class=\"acidfree-center\">$term->description</p>\n";
371 $output .= theme('pager', NULL, 1, ALBUM_PAGER);
372 $output .= '<div class="clear-block acidfree">' . "\n";
373 foreach ($nodes as $node) {
374 $node = node_load(array('nid' => $node->nid));
375 $output .= theme("acidfree_print_thumb_{$node->type}", $node, $term)."\n";
376 }
377 $output .= "</div>\n";
378 return $output;
379 }
380
381 function theme_views_view_album_list_view($view, $type, $nodes, $teasers = false, $links = true) {
382 $term = taxonomy_get_term($view->args[0]);
383 drupal_set_breadcrumb(acidfree_make_breadcrumbs($term));
384 drupal_set_title($term->name);
385 if (count($nodes) == 0) {
386 return "<p>".t('This album is empty')."</p>";
387 }
388 $output = "\n<p class=\"acidfree-center\">$term->description</p>\n";
389 $output .= theme('pager', NULL, 1, ALBUM_PAGER);
390 $output .= "\n<div class='acidfree-list'>\n";
391
392 $nodes_per_page = variable_get('acidfree_num_thumbs', 15);
393 $i = _acidfree_get_pager_offset_from_path(ALBUM_PAGER) * $nodes_per_page;
394 foreach ($nodes as $node) {
395 $node = node_load(array('nid' => $node->nid));
396 $row_class = "acidfree-list-".($i & 0x1 ? "even" : "odd");
397 $output .= '<div class="clear-block acidfree-list-row ' . $row_class . '">';
398 $output .= '<div class="acidfree-list-number">'.(++$i)."</div>";
399 $output .= theme("acidfree_print_thumb_{$node->type}", $node, $term);
400 $output .= '<div class="acidfree-list-body">'.check_markup($node->body)."</div>";
401 $output .= "</div>\n";
402 }
403 $output .= "</div>";
404 return $output;
405 }
406
407 function theme_acidfree_print_thumb_acidfree(&$node) {
408 $album_info = _album_get_info($node);
409 $image_node = _acidfree_get_album_thumb($node);
410
411 if ($image_node != '') {
412 $info = image_get_info(file_create_path($image_node->images['thumbnail']));
413 $image = _acidfree_image_display($image_node, 'thumbnail', array('id' => "acidfree-thumb{$node->nid}", 'title' => $album_info, 'width' => $info['width'], 'height' => $info['height']));
414 }
415 else {
416 $info = _image_get_dimensions('thumbnail');
417 $info['height'] = $info['width'] * 3/4;
418 $image = '<div class="acidfree-empty">'.t('Empty').'</div>';
419 }
420
421 $info['height'] += variable_get('acidfree_extra_length',12);
422 $info['width'] += variable_get('acidfree_extra_length',12);
423
424 $overlay = l('', "node/{$node->nid}", array('title' => $album_info), NULL, NULL, true, true);
425 $image = l($image, "node/{$node->nid}", array('title' => $album_info), NULL, NULL, true, true);
426 $title = l($node->title, "node/{$node->nid}", array('title' => $album_info), NULL, NULL, true, true);
427
428 $thumbnail = '<div class="acidfree-thumbnail" style="width: ' . $info['width'] . 'px; height: ' . $info['height'] . 'px;">';
429 $thumbnail .= $image . '<div class="acidfree-overlay">' . $overlay . '</div></div>';
430 $folderdiv = '<div class="acidfree-item acidfree-folder">' . $thumbnail . "<p>$title</p></div>";
431 return '<div class="acidfree-cell">' . $folderdiv . "</div>";
432 }
433
434 function _album_get_info(&$node) {
435 $albums = count(taxonomy_get_tree(acidfree_get_vocab_id(), $node->tid));
436 $children = taxonomy_term_count_nodes($node->tid);
437 if ($children == 0) {
438 return $node->title . ": " . t('Empty');
439 }
440 // album nodes count as children too, so get rid of those
441 $children -= $albums;
442 if ($children > 0) {
443 $tag[] = "$children " . ($children == 1 ? t('Item') : t('Items'));
444 }
445 if ($albums > 0) {
446 $tag[] = "$albums " . ($albums == 1 ? t('Albums') : t('Albums'));
447 }
448 // this entire string gets check_plain'd so no need to check the title
449 return $node->title . ": " . implode(' & ', $tag);
450 }
451
452 function _album_from_tid($tid) {
453 $nid = db_result(db_query("SELECT aid FROM {acidfree_album} WHERE tid=%d", $tid));
454 return acidfree_get_node_by_id($nid);
455 }
456
457 function _nid_from_tid($tid) {
458 $nid = db_result(db_query("SELECT aid FROM {acidfree_album} WHERE tid=%d", $tid));
459 return $nid;
460 }
461
462 function _class_album_alter_parent(&$form) {
463 //dump_msg($form['parent']);
464 $form['parent']['#title'] = t('Parent album');
465 $form['parent']['#multiple'] = 0;
466 $form['parent']['#size'] = 0;
467 $form['parent']['#required'] = 1;
468 if (($arg = _path_match('node', 'add', 'acidfree', '%d'))) {
469 $album = acidfree_get_node_by_id($arg[0]);
470 $form['taxonomy'][$vid]['#default_value'] = array($album->tid);
471 }
472 unset($form['parent']['#description']);
473 if (is_array($form['parent']['#options'])) {
474 foreach ($form['parent']['#options'] as $key => $value) {
475 if ($value == '<'. t('root') .'>' && count($form['parent']['#default_value'])) {
476 unset($form['parent']['#options'][$key]);
477 continue;
478 }
479 if (is_array($value)) {
480 }
481 if (user_access('can upload to any album')) {
482 continue;
483 }
484 $node = _album_from_tid($key);
485 if ($node->share) {
486 continue;
487 }
488 if (!node_access($node)) {
489 unset($form['parent']['#options'][$key]);
490 continue;
491 }
492 }
493 }
494 }
495
496 function _class_album_form_alter($form_id, &$form) {
497 $vid = acidfree_get_vocab_id();
498 if (isset($form['taxonomy'])) {
499 $form['taxonomy'][$vid] = $form['parent'][$vid];
500 } else {
501 $form['taxonomy'] = $form['parent'];
502 }
503 unset($form['parent']);
504 _acidfree_filter_taxonomy($form['taxonomy'][$vid]);
505 }
506 ?>

  ViewVC Help
Powered by ViewVC 1.1.2