* [Planet] Planets can now be edited.
[pivip.git] / project / modules / imagestream / controllers / StreamController.php
blob415f7aafeb7ea53fbe85b550ee2a22e2ca7b6246
1 <?php
3 /**
4 * Pivip
5 * Copyright (C) 2008 Vincent Tunru
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; version 2
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
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
20 * @license http://www.fsf.org/licensing/licenses/info/GPLv2.html GPL v.2
21 * @category PivipModulesDefault
22 * @package Module_Imagestream
23 * @copyright (C) 2008 Vincent Tunru
24 * @author Vincent Tunru <email@vincentt.org>
27 /**
28 * Stream images from other websites
30 class Imagestream_StreamController extends Page_Abstract
32 const API_KEY = '285259122a01117b5446fd6f5a44b09f';
34 /**
35 * Display the stream in the main section of the website
37 * @todo Cache the service search results.
39 public function mainAction()
41 $streamId = $this->_request->getParam('block_id');
42 if(null === $streamId)
44 $this->_helper->viewRenderer->setNoRender();
45 return;
47 $cache = Imagestream_Module::loadCache();
48 if(!$row = $cache->load($streamId))
50 $row = Doctrine::getTable('Imagestream')->find($streamId);
51 $cache->save($row, $streamId, array('stream', 'block'));
53 switch($row->service)
55 case 'flickr':
56 $service = new Zend_Service_Flickr(self::API_KEY);
57 break;
58 case '23':
59 default:
60 $service = new Vogel_Service_23(self::API_KEY);
61 break;
63 $options = array('per_page' => 5);
64 if(isset($row->amount))
66 $options['per_page'] = intval($row->amount);
68 switch($row->type)
70 case 'user':
71 try
73 $this->view->images = $service->userSearch($row->keywords,
74 $options);
75 } catch(Exception $e) {
76 $logger = Zend_Registry::get('logger');
77 $logger->err($e->getMessage());
78 $this->_helper->viewRenderer->setNoRender();
79 return;
81 break;
82 case 'tags':
83 default:
84 try
86 $this->view->images = $service->tagSearch($row->keywords, $options);
87 } catch(Exception $e) {
88 $logger = Zend_Registry::get('logger');
89 $logger->err($e->getMessage());
90 $this->_helper->viewRenderer->setNoRender();
91 return;
93 break;
95 switch($row->size)
97 case 'Square':
98 // Break intentionally omitted
99 case 'Thumbnail':
100 // Break intentionally omitted
101 case 'Small':
102 // Break intentionally omitted
103 case 'Medium':
104 // Break intentionally omitted
105 case 'Large':
106 // Break intentionally omitted
107 case 'Original':
108 $this->view->imageSize = $row->size;
109 break;
110 default:
111 $this->view->imageSize = 'Small';
113 $this->view->urlParams = array('stream_id' => $streamId);
114 if($this->_isAllowed('edit'))
116 $this->view->allowEdit = true;
118 if($this->_isAllowed('delete'))
120 $this->view->allowDelete = true;
125 * Add a stream
127 public function addAction()
129 $translate = Zend_Registry::get('Zend_Translate');
130 $location = '/' . $this->_request->getParam('location');
131 if(!$this->_isAllowed('add'))
133 $this->_flashMessenger->addMessage($translate->_(
134 'You are not allowed to add an image stream.'));
135 $this->_redirect($location);
137 $section = $this->_request->getParam('section');
138 if(empty($section))
140 $this->_flashMessenger->addMessage($translate->_(
141 'You need to specify a section to add an image stream to.'));
142 $this->_redirect($location);
144 $defaultRequest = Zend_Registry::get('defaultRequest');
145 $this->_helper->actionStack($defaultRequest);
146 $form = $this->_getForm();
147 $this->view->form = $form->render();
148 if(!$this->_request->isPost() ||
149 !$form->isValid($this->_request->getPost()))
151 return;
155 $block = new Block();
156 $block->location = $location;
157 $block->action = $section;
158 $block->controller = 'stream';
159 $block->module = 'imagestream';
160 $block->Imagestream->service = $form->getValue('service');
161 $block->Imagestream->keywords = $form->getValue('keywords');
162 $block->Imagestream->amount = intval($form->getValue('amount'));
163 $block->Imagestream->type = $form->getValue('type');
164 $block->Imagestream->size = $form->getValue('size');
165 $block->save();
166 $cacheId = Page_Module::urlToCacheId($location);
167 $cache = Page_Module::loadCache();
168 $cache->remove($cacheId);
169 $this->_flashMessenger->addMessage($translate->_('Block added.'));
170 } catch(Exception $e) {
171 $this->_flashMessenger->setNamespace('error')->addMessage($translate
172 ->_('Failed to add the block.'));
174 $this->_redirect($location);
178 * Edit a stream
180 public function editAction()
182 $translate = Zend_Registry::get('Zend_Translate');
184 $streamId = $this->_request->getParam('stream_id');
186 if(null === $streamId)
188 $this->_flashMessenger->addMessage($translate->_(
189 'No image stream specified.'));
190 $this->_redirect();
193 $streamTable = Doctrine::getTable('Imagestream');
194 $stream = $streamTable->find($streamId);
196 if(!$this->_isAllowed('edit'))
198 $this->_flashMessenger->addMessage($translate->_(
199 'You are not allowed to edit image stream.'));
200 $this->_redirect($html->Block->location);
203 $this->_helper->viewRenderer->setScriptAction('add');
204 $defaultRequest = Zend_Registry::get('defaultRequest');
205 $this->_helper->actionStack($defaultRequest);
207 $form = $this->_getForm();
208 $form->submit->setLabel('Edit');
209 $form->populate($stream->toArray());
210 $this->view->form = $form->render();
211 if(!$this->_request->isPost() ||
212 !$form->isValid($this->_request->getPost()))
214 return;
218 $stream->service = $form->getValue('service');
219 $stream->keywords = $form->getValue('keywords');
220 $stream->amount = intval($form->getValue('amount'));
221 $stream->type = $form->getValue('type');
222 $stream->size = $form->getValue('size');
223 $stream->save();
224 $this->_flashMessenger->addMessage($translate->_('Block updated.'));
225 } catch(Exception $e) {
226 $this->_flashMessenger->setNamespace('error')->addMessage($translate->_(
227 'An error occured while updating the block, please try again.'));
228 $this->_redirect($this->_request->getRequestUri());
230 $cache = Imagestream_Module::loadCache();
231 $cache->remove($streamId);
232 $this->_redirect($html->Block->location);
236 * Delete a stream
238 * @todo Allow the user to undo this
240 public function deleteAction()
242 $streamId = $this->_request->getParam('stream_id');
243 $translate = Zend_Registry::get('Zend_Translate');
245 if(null === $streamId)
247 $this->_flashMessenger->addMessage($translate->_(
248 'No image stream specified.'));
249 $this->_redirect();
252 $streamTable = Doctrine::getTable('Imagestream');
253 $stream = $streamTable->find($streamId);
255 if(!$this->_isAllowed('delete'))
257 $this->_flashMessenger->addMessage($translate->_(
258 'You are not allowed to delete blocks.'));
259 $this->_redirect($stream->Block->location);
262 $location = $stream->Block->location;
265 $stream->Block->delete();
266 $stream->delete();
267 $this->_flashMessenger->addMessage($translate->_(
268 'Block deleted.'));
269 $cache = Imagestream_Module::loadCache();
270 $cache->remove($streamId);
271 $cache = Page_Module::loadCache();
272 $cache->remove(Page_Module::urlToCacheId($location));
273 } catch(Exception $e) {
274 $this->_flashMessenger->setNamespace('error')
275 ->addMessage($translate->_(
276 'An error occurred while deleting the block, please try again.'));
278 $this->_redirect($location);
282 * @return Zend_Form The form to add an HTML block
284 protected function _getForm()
286 $translate = Zend_Registry::get('Zend_Translate');
287 $form = new Zend_Form;
288 $form->setMethod('post')
289 ->setAction($this->_request->getRequestUri())
290 ->addElement('select', 'service')
291 ->service->setRequired(true)
292 ->setLabel('Service')
293 ->addMultiOption('23', '23')
294 ->addMultiOption('flickr', 'Flickr');
295 $form->addElement('text', 'keywords')
296 ->keywords->setRequired(true)
297 ->setLabel('Keywords')
298 ->addValidator('NotEmpty');
299 $form->addElement('select', 'type')
300 ->type->setRequired(true)
301 ->setLabel('Search type')
302 ->addMultiOption('tags', $translate->_('Tags'))
303 ->addMultiOption('user', $translate->_('User'));
304 $form->addElement('select', 'size')
305 ->size->setRequired(true)
306 ->setLabel('Image size')
307 ->addMultiOption('Square', $translate->_('Square'))
308 ->addMultiOption('Thumbnail', $translate->_('Thumbnail'))
309 ->addMultiOption('Small', $translate->_('Small'))
310 ->addMultiOption('Medium', $translate->_('Medium'))
311 ->addMultiOption('Large', $translate->_('Large'))
312 ->addMultiOption('Original', $translate->_('Original'))
313 ->setValue('Small');
314 $form->addElement('text', 'amount')
315 ->amount->setValue(5)
316 ->setLabel('Amount');
317 $form->addDisplayGroup(array('keywords', 'type'), 'stream')
318 ->stream->setLegend('Required options');
319 $form->addDisplayGroup(array('service', 'amount', 'size'), 'advanced')
320 ->advanced->setLegend('Advanced options');
321 $submit = new Zend_Form_Element_Submit('submit');
322 $submit->setLabel('Add')
323 ->addDecorator('HtmlTag', array('tag' => 'dd'))
324 ->removeDecorator('DtDdWrapper');
325 $form->addElement($submit);
326 return $form;
330 * @param $privileges What the user needs to be allowed to do to blocks
331 * @return bool Whether the user has sufficient rights
333 protected function _isAllowed($privileges = null)
335 $auth = Pivip_Auth::getInstance();
336 $acl = Zend_Registry::get('acl');
337 $identity = $auth->getIdentityProperties();
338 if('edit' == $privileges || 'add' == $privileges)
340 if(!$acl->isAllowed('guest', 'imagestream', 'write')
341 && !$auth->hasIdentity())
343 return false;
345 if(!$acl->isAllowed($identity->aclRole, 'imagestream', 'write'))
347 return false;
350 if(!$acl->isAllowed('guest', 'block', $privileges) &&
351 !$auth->hasIdentity())
353 return false;
355 return $acl->isAllowed($identity->aclRole, 'block', $privileges);