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 save many item captions at once. 23 * @package GalleryCore 24 * @subpackage UserInterface 25 * @author Bharat Mediratta <bharat@menalto.com> 26 * @author Changpeng Zhao 27 * @version $Revision: 18138 $ 28 */ 29class ItemEditCaptionsController extends GalleryController { 30 31 /** 32 * @see GalleryController::handleRequest 33 */ 34 function handleRequest($form) { 35 $page = (int)GalleryUtilities::getRequestVariables('page'); 36 37 list ($ret, $item) = $this->getItem(); 38 if ($ret) { 39 return array($ret, null); 40 } 41 $itemId = $item->getId(); 42 43 $status = $error = array(); 44 if (isset($form['action']['save'])) { 45 $ids = array_keys($form['items']); 46 47 list ($ret, $lockId) = GalleryCoreApi::acquireWriteLock($ids); 48 if ($ret) { 49 return array($ret, null); 50 } 51 52 /* We'll check permissions one at a time below, but precache them now */ 53 list ($ret, $permissions) = GalleryCoreApi::fetchPermissionsForItems($ids); 54 if ($ret) { 55 return array($ret, null); 56 } 57 58 list ($ret, $items) = GalleryCoreApi::loadEntitiesById($ids, 'GalleryItem'); 59 if ($ret) { 60 return array($ret, null); 61 } 62 63 list ($ret, $markup) = 64 GalleryCoreApi::getPluginParameter('module', 'core', 'misc.markup'); 65 if ($ret) { 66 return array($ret, null); 67 } 68 69 $status['successCount'] = 0; 70 $status['errorCount'] = 0; 71 foreach ($items as $item) { 72 /* Make sure we have permission to edit this item */ 73 $id = $item->getId(); 74 if (!isset($permissions[$id]['core.view'])) { 75 /* Avoid information disclosure, act as if the item didn't exist. */ 76 return array(GalleryCoreApi::error(ERROR_MISSING_OBJECT), null); 77 } 78 if (isset($permissions[$id]['core.edit'])) { 79 if ($item->getSerialNumber() == $form['items'][$id]['serialNumber']) { 80 if ($markup == 'html') { 81 /* Strip malicious content if html markup allowed */ 82 $form['items'][$id]['title'] = 83 GalleryUtilities::htmlSafe($form['items'][$id]['title'], true); 84 $form['items'][$id]['summary'] = 85 GalleryUtilities::htmlSafe($form['items'][$id]['summary'], true); 86 $form['items'][$id]['description'] = 87 GalleryUtilities::htmlSafe($form['items'][$id]['description'], true); 88 } 89 90 $item->setTitle($form['items'][$id]['title']); 91 $item->setSummary($form['items'][$id]['summary']); 92 $item->setKeywords($form['items'][$id]['keywords']); 93 $item->setDescription($form['items'][$id]['description']); 94 95 $ret = $item->save(); 96 if ($ret) { 97 return array($ret, null); 98 } 99 $status[$id]['saved'] = 1; 100 $status['successCount']++; 101 } else { 102 $status[$id]['obsolete'] = 1; 103 $status['errorCount']++; 104 } 105 } else { 106 $status[$id]['permissionDenied'] = 1; 107 $status['errorCount']++; 108 } 109 } 110 111 $ret = GalleryCoreApi::releaseLocks($lockId); 112 if ($ret) { 113 return array($ret, null); 114 } 115 116 /* 117 * Figure out where to redirect. We always redirect even if we have 118 * an error since we may have saved some items, but not others. 119 */ 120 $redirect['view'] = 'core.ItemAdmin'; 121 $redirect['subView'] = 'core.ItemEditCaptions'; 122 $redirect['itemId'] = (int)$itemId; 123 if (!$status['errorCount'] && isset($form['action']['save']['next'])) { 124 $redirect['page'] = $page + 1; 125 } else if (!$status['errorCount'] && isset($form['action']['save']['previous'])) { 126 $redirect['page'] = max($page-1, 0); 127 } else if (!$status['errorCount'] && isset($form['action']['save']['done'])) { 128 $results['return'] = 1; 129 $redirect['page'] = (int)$page; 130 } else { 131 $redirect['page'] = (int)$page; 132 } 133 } else if (isset($form['action']['cancel'])) { 134 $results['return'] = 1; 135 } 136 137 if (!empty($redirect)) { 138 $results['redirect'] = $redirect; 139 } else { 140 $results['delegate']['view'] = 'core.ItemAdmin'; 141 $results['delegate']['subView'] = 'core.ItemEditCaptions'; 142 } 143 $results['status'] = $status; 144 $results['error'] = $error; 145 146 return array(null, $results); 147 } 148} 149 150/** 151 * This view will allow the user to edit many item captions at once. 152 */ 153class ItemEditCaptionsView extends GalleryView { 154 155 /** 156 * Returns sizes for "maxlength" in forms 157 * @todo: this function is duplicated in ItemEdit.inc, so it needs to be consolidated somehow 158 * @param integer $value is the value from 159 * @return size for "maxlength" in form 160 * @access private 161 */ 162 function _getSizesForMaxlength($value) { 163 switch ($value) { 164 case STORAGE_SIZE_SMALL: 165 $size = 32; 166 break; 167 168 case STORAGE_SIZE_MEDIUM: 169 $size = 128; 170 break; 171 172 case STORAGE_SIZE_LARGE: 173 $size = 255; 174 break; 175 } 176 177 return $size; 178 } 179 180 /** 181 * @see GalleryView::loadTemplate 182 */ 183 function loadTemplate(&$template, &$form) { 184 global $gallery; 185 186 list ($page, $selectedId, $albumPage) = 187 GalleryUtilities::getRequestVariables('page', 'selectedId', 'albumPage'); 188 189 list ($ret, $item) = $this->getItem(); 190 if ($ret) { 191 return array($ret, null); 192 } 193 $itemId = $item->getId(); 194 195 if ($form['formName'] != 'ItemEditCaption') { 196 $form['formName'] = 'ItemEditCaption'; 197 $form['numPerPage'] = 9; 198 199 list ($ret, $childIds) = GalleryCoreApi::fetchChildItemIdsWithPermission( 200 $itemId, array('core.edit', 'core.view')); 201 if ($ret) { 202 return array($ret, null); 203 } 204 205 $form['childItems'] = array(); 206 $numPages = 1; 207 $numPages = ceil(sizeof($childIds) / $form['numPerPage']); 208 if (empty($page) && !empty($selectedId)) { 209 /* No page given. Determine which page we're on from the selectedId */ 210 for ($i = 0; $i < count($childIds); $i++) { 211 if ($childIds[$i] == $selectedId) { 212 $page = ceil(($i + 1) / $form['numPerPage']); 213 } 214 } 215 } 216 if (empty($page) && !empty($albumPage)) { 217 /* Still no page. Determine which page we're on from albumPage */ 218 list ($ret, $theme) = $this->loadThemeForItem($item); 219 if ($ret) { 220 return array($ret, null); 221 } 222 list ($ret, $params) = $theme->fetchParameters($itemId); 223 if ($ret) { 224 return array($ret, null); 225 } 226 $albumPageSize = $theme->getPageSize($params); 227 if (!empty($albumPageSize)) { 228 $page = ceil((($albumPage - 1) * $albumPageSize + 1) / $form['numPerPage']); 229 } 230 } 231 if (empty($page)) { 232 $page = 1; 233 } 234 235 $start = $form['numPerPage'] * ($page - 1); 236 $childIds = array_slice($childIds, $start, $form['numPerPage']); 237 238 list ($ret, $childItems) = GalleryCoreApi::loadEntitiesById($childIds, 'GalleryItem'); 239 if ($ret) { 240 return array($ret, null); 241 } 242 243 /* Get child thumbnails and resizes */ 244 list ($ret, $derivatives) = GalleryCoreApi::fetchDerivativesByItemIds($childIds); 245 if ($ret) { 246 return array($ret, null); 247 } 248 249 /* build peers table */ 250 foreach ($childItems as $child) { 251 $childId = $child->getId(); 252 $form['items'][$childId] = (array)$child; 253 254 /* While we're at it, attach thumbnails and resizes */ 255 if (isset($derivatives[$childId])) { 256 foreach ($derivatives[$childId] as $derivative) { 257 $type = $derivative->getDerivativeType(); 258 if (empty($form['items'][$childId]['resize']) && 259 $type == DERIVATIVE_TYPE_IMAGE_RESIZE) { 260 $form['items'][$childId]['resize'] = (array)$derivative; 261 } else if ($type == DERIVATIVE_TYPE_IMAGE_THUMBNAIL) { 262 $form['items'][$childId]['thumbnail'] = (array)$derivative; 263 } 264 } 265 } 266 } 267 } 268 269 $urlGenerator =& $gallery->getUrlGenerator(); 270 271 $ItemEditCaptions = array(); 272 $ItemEditCaptions['canCancel'] = (boolean)GalleryUtilities::getRequestVariables('return'); 273 $ItemEditCaptions['page'] = $page; 274 $ItemEditCaptions['numPages'] = $numPages; 275 276 list ($ret, $entityInfo) = GalleryCoreApi::describeEntity('GalleryItem'); 277 if ($ret) { 278 return array($ret, null); 279 } 280 281 $ItemEditCaptions['fieldLengths'] = array(); 282 $ItemEditCaptions['fieldLengths']['title'] = 283 $this->_getSizesForMaxLength($entityInfo['GalleryItem']['members']['title']['size']); 284 285 $template->setVariable('ItemEditCaptions', $ItemEditCaptions); 286 $template->setVariable('controller', 'core.ItemEditCaptions'); 287 $template->javascript('lib/yui/yahoo-dom-event.js'); 288 $template->javascript('lib/yui/container-min.js'); 289 return array(null, array('body' => 'modules/core/templates/ItemEditCaptions.tpl')); 290 291 } 292 293 /** 294 * @see GalleryView::getViewDescription 295 */ 296 function getViewDescription() { 297 list ($ret, $core) = GalleryCoreApi::loadPlugin('module', 'core'); 298 if ($ret) { 299 return array($ret, null); 300 } 301 302 return array(null, $core->translate('edit captions')); 303 } 304} 305?> 306