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 * Dynamic album view using a query on keywords 23 * @package KeyAlbum 24 * @subpackage UserInterface 25 * @author Alan Harder <alan.harder@sun.com> 26 * @version $Revision: 17580 $ 27 */ 28class KeywordAlbumView extends GalleryView { 29 30 /** 31 * @see GalleryView::getViewType 32 */ 33 function getViewType() { 34 return VIEW_TYPE_SHOW_ITEM; 35 } 36 37 /** 38 * @see GalleryView::loadThemeAndParameters 39 */ 40 function loadThemeAndParameters() { 41 list ($ret, $item) = GalleryCoreApi::newFactoryInstance('GalleryDynamicAlbum'); 42 if ($ret) { 43 return array($ret, null, null, null); 44 } 45 if (!isset($item)) { 46 return array(GalleryCoreApi::error(ERROR_MISSING_OBJECT), null, null, null); 47 } 48 list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'keyalbum'); 49 if ($ret) { 50 return array($ret, null, null, null); 51 } 52 list ($keyword, $itemId) = GalleryUtilities::getRequestVariables('keyword', 'itemId'); 53 $item->create( 54 $module->translate(array('text' => 'Keyword Album: %s', 'arg1' => $keyword)), 55 array(array('Keyword Album', 'keyword album'), 56 array($module->translate('Keyword Album'), $module->translate('keyword album'))) 57 ); 58 59 list ($ret, $moduleParams) = 60 GalleryCoreApi::fetchAllPluginParameters('module', 'keyalbum'); 61 if ($ret) { 62 return array($ret, null, null, null); 63 } 64 $item->setDescription($moduleParams['description']); 65 66 if (!empty($itemId)) { 67 /* Viewing an item in this dynamic album */ 68 list ($ret, $viewItem) = GalleryCoreApi::loadEntitiesById($itemId, 'GalleryItem'); 69 if ($ret) { 70 return array($ret, null, null, null); 71 } 72 list ($ret, $hasPermission) = GalleryCoreApi::hasItemPermission($itemId, 'core.view'); 73 if ($ret) { 74 return array($ret, null, null, null); 75 } 76 if (!$hasPermission) { 77 /* Avoid information disclosure, act as if the item didn't exist. */ 78 return array(GalleryCoreApi::error(ERROR_MISSING_OBJECT), null, null, null); 79 } 80 81 /* Provide parent, parent URL and get-children function to Theme API */ 82 $item->urlParams = array('view' => 'keyalbum.KeywordAlbum', 'keyword' => $keyword, 83 'highlightId' => $itemId); 84 $item->getChildrenFunction = array('KeywordAlbumView', 'getChildIds'); 85 $viewItem->parent = $item; 86 $item = $viewItem; 87 } 88 89 if (empty($moduleParams['themeId'])) { 90 list ($ret, $theme) = $this->loadThemeForItem(); 91 if ($ret || !isset($theme)) { 92 /* Ignore errors here so fallback theme can be used */ 93 return array(null, null, array(), $item); 94 } 95 } else { 96 list ($ret, $theme) = GalleryView::_loadTheme($moduleParams['themeId']); 97 if ($ret || !isset($theme)) { 98 /* Ignore errors here so fallback theme can be used */ 99 return array(null, null, array(), $item); 100 } 101 } 102 103 list ($ret, $params) = $theme->fetchParameters($moduleParams['themeSettingsId']); 104 if ($ret) { 105 return array($ret, null, null, null); 106 } 107 108 return array(null, $theme, $params, $item); 109 } 110 111 /** 112 * @see GalleryView::loadTemplate 113 */ 114 function loadTemplate(&$template, &$form) { 115 $theme =& $template->getVariableByReference('theme'); 116 117 list ($keyword, $itemId) = GalleryUtilities::getRequestVariables('keyword', 'itemId'); 118 $rawKeyword = $keyword; 119 GalleryUtilities::unsanitizeInputValues($rawKeyword, false); 120 $theme['pageUrl'] = array('view' => 'keyalbum.KeywordAlbum', 'keyword' => $rawKeyword); 121 122 if (empty($itemId)) { 123 /* Perform query for this dynamic album */ 124 list ($ret, $theme['allChildIds']) = 125 $this->getChildIds($theme['actingUserId'], $keyword); 126 if ($ret) { 127 return array($ret, null); 128 } 129 } else { 130 /* Item in dynamic album; use core.ShowItem to check permission, increment view count */ 131 list ($ret, $showItem) = GalleryView::loadView('core.ShowItem'); 132 if ($ret) { 133 return array($ret, null); 134 } 135 list ($ret, $result) = $showItem->loadTemplate($template, $form); 136 if ($ret) { 137 return array($ret, null); 138 } 139 if (isset($result['redirect'])) { 140 return array(null, $result); 141 } 142 } 143 144 return array(null, array()); 145 } 146 147 /** 148 * Dynamic query for items 149 * @param int $userId 150 * @param string $keyword (optional) keyword for query; get from request if not specified 151 * @return array GalleryStatus a status code 152 * array of item ids 153 * @static 154 */ 155 function getChildIds($userId, $keyword=null) { 156 global $gallery; 157 $storage =& $gallery->getStorage(); 158 159 if (!isset($keyword)) { 160 $keyword = GalleryUtilities::getRequestVariables('keyword'); 161 } 162 if (empty($keyword)) { 163 return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER), null); 164 } 165 166 list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'keyalbum'); 167 if ($ret) { 168 return array($ret, null); 169 } 170 list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters('module', 'keyalbum'); 171 if ($ret) { 172 return array($ret, null); 173 } 174 175 $keywords = $where = array(); 176 foreach ($module->splitKeywords($keyword, $params['split']) as $k) { 177 $keywords[] = '%' . $k . '%'; 178 $where[] = '[GalleryItem::keywords] LIKE ?'; 179 } 180 181 list ($ret, $query, $data) = GalleryCoreApi::buildItemQuery( 182 'GalleryItem', 'id', implode(' AND ', $where), 183 $params['orderBy'], $params['orderDirection'], null, 'core.view', false, $userId); 184 if ($ret) { 185 return array($ret, null); 186 } 187 if (empty($query)) { 188 return array(null, array()); 189 } 190 191 list ($ret, $searchResults) = $gallery->search($query, array_merge($keywords, $data)); 192 if ($ret) { 193 return array($ret, null); 194 } 195 $itemIds = array(); 196 while ($result = $searchResults->nextResult()) { 197 $itemIds[] = $result[0]; 198 } 199 200 return array(null, $itemIds); 201 } 202 203 /** 204 * @see GalleryView::getViewDescription 205 */ 206 function getViewDescription() { 207 list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'keyalbum'); 208 if ($ret) { 209 return array($ret, null); 210 } 211 return array(null, $module->translate('keyword album')); 212 } 213} 214?> 215