1<?php 2/* 3 * Gallery - a web based photo album viewer and editor 4 * Copyright (C) 2000-2008 Bharat Mediratta 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or (at 9 * your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, but 12 * WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. 19 */ 20 21/** 22 * This controller will handle rearranging the order of all album items at once. 23 * @package Rearrange 24 * @subpackage UserInterface 25 * @author Alan Harder <alan.harder@sun.com> 26 * @version $Revision: 17580 $ 27 */ 28class RearrangeItemsController extends GalleryController { 29 30 /** 31 * @see GalleryController::handleRequest 32 */ 33 function handleRequest($form) { 34 list ($ret, $item) = $this->getItem(); 35 if ($ret) { 36 return array($ret, null); 37 } 38 $itemId = $item->getId(); 39 40 /* Check permissions */ 41 $ret = GalleryCoreApi::assertHasItemPermission($itemId, 'core.edit'); 42 if ($ret) { 43 return array($ret, null); 44 } 45 46 $status = array(); 47 $error = array(); 48 if (isset($form['action']['save'])) { 49 $list = explode(',', $form['list']); 50 51 list ($ret, $childIds) = GalleryCoreApi::fetchChildItemIds($item); 52 if ($ret) { 53 return array($ret, null); 54 } 55 /* Verify valid input */ 56 if (count($list) != count($childIds)) { 57 return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER), null); 58 } 59 60 list ($ret, $orderWeights) = GalleryCoreApi::fetchItemOrderWeights($childIds); 61 if ($ret) { 62 return array($ret, null); 63 } 64 65 foreach ($list as $newIndex => $oldIndex) { 66 if ($newIndex == $oldIndex) { 67 continue; 68 } 69 70 $ret = GalleryCoreApi::setItemOrderWeight($childIds[$oldIndex], 71 $orderWeights[$childIds[$newIndex]]); 72 if ($ret) { 73 return array($ret, null); 74 } 75 } 76 77 $event = GalleryCoreApi::newEvent('Gallery::ItemOrder'); 78 $event->setData($itemId); 79 list ($ret) = GalleryCoreApi::postEvent($event); 80 if ($ret) { 81 return array($ret, null); 82 } 83 84 $status['saved'] = 1; 85 } /* else $form['action']['reset'] */ 86 87 $results['redirect'] = array('view' => 'core.ItemAdmin', 88 'subView' => 'rearrange.RearrangeItems', 89 'itemId' => $itemId); 90 $results['status'] = $status; 91 $results['error'] = $error; 92 93 return array(null, $results); 94 } 95} 96 97/** 98 * This view will display an interface to reorder all album items at once. 99 */ 100class RearrangeItemsView extends GalleryView { 101 102 /** 103 * @see GalleryView::loadTemplate 104 */ 105 function loadTemplate(&$template, &$form) { 106 list ($ret, $item) = $this->getItem(); 107 if ($ret) { 108 return array($ret, null); 109 } 110 $itemId = $item->getId(); 111 112 /* Check permissions */ 113 $ret = GalleryCoreApi::assertHasItemPermission($itemId, 'core.edit'); 114 if ($ret) { 115 return array($ret, null); 116 } 117 118 if ($form['formName'] != 'RearrangeItems') { 119 $form['formName'] = 'RearrangeItems'; 120 } 121 122 $RearrangeItems = array(); 123 $orderBy = $item->getOrderBy(); 124 if (empty($orderBy)) { 125 list ($ret, $orderBy) = 126 GalleryCoreApi::getPluginParameter('module', 'core', 'default.orderBy'); 127 if ($ret) { 128 return array($ret, null); 129 } 130 } 131 if ($orderBy != 'orderWeight') { 132 $RearrangeItems['automaticOrderMessage'] = 1; 133 } else { 134 list ($ret, $childIds) = GalleryCoreApi::fetchChildItemIds($item); 135 if ($ret) { 136 return array($ret, null); 137 } 138 list ($ret, $childItems) = GalleryCoreApi::loadEntitiesById($childIds, 'GalleryItem'); 139 if ($ret) { 140 return array($ret, null); 141 } 142 list ($ret, $thumbnails) = GalleryCoreApi::fetchThumbnailsByItemIds($childIds); 143 if ($ret) { 144 return array($ret, null); 145 } 146 147 $children = array(); 148 $maxWidth = $maxHeight = 0; 149 foreach ($childItems as $child) { 150 $childId = $child->getId(); 151 $child = (array)$child; 152 if (isset($thumbnails[$childId])) { 153 $thumb = $thumbnails[$childId]; 154 if (!($thumb->getWidth() && $thumb->getHeight())) { 155 list ($ret, $thumb) = 156 GalleryCoreApi::rebuildDerivativeCache($thumb->getId()); 157 if ($ret) { 158 return array($ret, null); 159 } 160 } 161 $child['thumbnail'] = (array)$thumb; 162 list ($w, $h) = GalleryUtilities::shrinkDimensionsToFit( 163 $thumb->getWidth(), $thumb->getHeight(), 100); 164 $maxWidth = max($w, $maxWidth); 165 $maxHeight = max($h, $maxHeight); 166 } 167 $children[] = $child; 168 } 169 $RearrangeItems['children'] = $children; 170 $RearrangeItems['count'] = count($children); 171 $RearrangeItems['maxWidth'] = $maxWidth + 5; 172 $RearrangeItems['maxHeight'] = $maxHeight + 5; 173 174 /* See if the album's theme has "rows" and/or "columns" */ 175 list ($ret, $themeId) = GalleryCoreApi::fetchThemeId($item); 176 if ($ret) { 177 return array($ret, null); 178 } 179 list ($ret, $theme) = GalleryCoreApi::loadPlugin('theme', $themeId); 180 if ($ret) { 181 return array($ret, null); 182 } 183 list ($ret, $params) = $theme->fetchParameters($item->getId()); 184 if ($ret) { 185 return array($ret, null); 186 } 187 $RearrangeItems['rows'] = empty($params['rows']) ? 0 : $params['rows']; 188 $RearrangeItems['columns'] = empty($params['columns']) ? 0 : $params['columns']; 189 } 190 191 $template->setVariable('RearrangeItems', $RearrangeItems); 192 $template->setVariable('controller', 'rearrange.RearrangeItems'); 193 $template->head('modules/rearrange/templates/Header.tpl'); 194 return array(null, array('body' => 'modules/rearrange/templates/RearrangeItems.tpl')); 195 } 196 197 /** 198 * @see GalleryView::getViewDescription 199 */ 200 function getViewDescription() { 201 list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'rearrange'); 202 if ($ret) { 203 return array($ret, null); 204 } 205 return array(null, $module->translate('rearrange items')); 206 } 207} 208?> 209