* [Planet] Planets can now be edited.
[pivip.git] / project / modules / planet / controllers / FeedController.php
blobb1325d57bc2ead548c864a0fbcf73bd8e244ab54
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_Planet
23 * @copyright (C) 2008 Vincent Tunru
24 * @author Vincent Tunru <email@vincentt.org>
27 /**
28 * Manage feeds in planets
30 class Planet_FeedController extends Page_Abstract
32 /**
33 * Display the stream in the main section of the website
35 * @todo Allow for displaying of a feed that is not pre-configured
37 public function mainAction()
39 $this->view->feed = $feed;
42 /**
43 * Render a feed as HTML
45 public function feedAction()
47 $feed = $this->_request->getParam('feed');
48 if(null === $feed)
50 $this->_helper->viewRenderer->setNoRender();
51 return;
53 $feed = new Vogel_Feed_Unified($feed);
54 $this->view->title = $feed->title();
55 $this->view->link = $feed->link();
56 $entries = array();
57 foreach($feed as $entry)
59 $entry = new Vogel_Feed_Entry_Unified($entry);
60 $entries[] = array('title' => $entry->title(),
61 'content' => $entry->content(),
62 'link' => $entry->link());
64 $this->view->entries = $entries;
65 $this->view->setEscape('strip_tags');
68 /**
69 * Add a feed
71 public function addAction()
73 $translate = Zend_Registry::get('Zend_Translate');
74 if(!$this->_isAllowed('add'))
76 $this->_flashMessenger->addMessage($translate->_(
77 'You are not allowed to add a feed to a planet.'));
78 $this->_redirect('');
80 $planetId = $this->_request->getParam('planet_id');
81 if(null === $planetId)
83 $this->_flashMessenger->addMessage($translate->_(
84 'No planet to add a feed to specified.'));
85 $this->_redirect('');
88 $defaultRequest = Zend_Registry::get('defaultRequest');
89 $this->_helper->actionStack($defaultRequest);
90 $form = $this->_getForm();
91 $this->view->form = $form->render();
92 if(!$this->_request->isPost() ||
93 !$form->isValid($this->_request->getPost()))
95 return;
97 try
99 $feed = new PlanetFeed();
100 $feed->block_id = $planetId;
101 $feed->link = $form->getValue('link');
104 $imported = Zend_Feed::import($feed->link);
105 $feed->title = $imported->title();
106 } catch(Exception $e) {
108 $feed->save();
109 $cache = Planet_Module::loadCache();
110 $cache->remove($planetId);
111 $this->_helper->flashMessenger
112 ->addMessage($translate->_('Feed added.'));
113 $this->_redirect($feed->Block->location);
114 } catch(Exception $e) {
115 $logger = Zend_Registry::get('logger');
116 $logger->err($e->getMessage());
117 $this->_refresh('Failed to add the feed.', 'error');
122 * Edit a feed
124 public function editAction()
126 $translate = Zend_Registry::get('Zend_Translate');
128 $feedId = $this->_request->getParam('feed_id');
130 if(null === $feedId)
132 $this->_flashMessenger->addMessage($translate->_(
133 'No feed specified.'));
134 $this->_redirect();
137 $feedTable = Doctrine::getTable('PlanetFeed');
138 $feed = $feedTable->find($feedId);
139 $planetId = $feed->block_id;
141 if(!$this->_isAllowed('edit'))
143 $this->_flashMessenger->addMessage($translate->_(
144 'You are not allowed to edit feeds.'));
145 $this->_redirect($feed->Block->location);
148 $this->_helper->viewRenderer->setScriptAction('add');
149 $defaultRequest = Zend_Registry::get('defaultRequest');
150 $this->_helper->actionStack($defaultRequest);
152 $form = $this->_getForm();
153 $form->submit->setLabel('Edit');
154 $form->populate($feed->toArray());
155 $this->view->form = $form->render();
156 if(!$this->_request->isPost() ||
157 !$form->isValid($this->_request->getPost()))
159 return;
164 $feed->link = $form->getValue('link');
167 $imported = Zend_Feed::import($feed->link);
168 $feed->title = $imported->title();
169 } catch(Exception $e) {
171 $feed->save();
172 $cache = Planet_Module::loadCache();
173 $cache->remove($planetId);
174 $cache = Planet_Module::loadCache('feed');
175 $cache->remove($feedId);
176 $this->_helper->flashMessenger
177 ->addMessage($translate->_('Feed edited.'));
178 $this->_redirect($feed->Block->location);
179 } catch(Exception $e) {
180 $logger = Zend_Registry::get('logger');
181 $logger->err($e->getMessage());
182 $this->_refresh('Failed to edit the feed.', 'error');
187 $stream->service = $form->getValue('service');
188 $stream->keywords = $form->getValue('keywords');
189 $stream->amount = intval($form->getValue('amount'));
190 $stream->type = $form->getValue('type');
191 $stream->size = $form->getValue('size');
192 $stream->save();
193 $this->_flashMessenger->addMessage($translate->_('Block updated.'));
194 } catch(Exception $e) {
195 $this->_flashMessenger->setNamespace('error')->addMessage($translate->_(
196 'An error occured while updating the block, please try again.'));
197 $this->_redirect($this->_request->getRequestUri());
199 $cache = Imagestream_Module::loadCache();
200 $cache->remove($streamId);
201 $this->_redirect($html->Block->location);
205 * Delete a feed
207 * @todo Allow the user to undo this
209 public function deleteAction()
211 $feedId = $this->_request->getParam('feed_id');
212 $translate = Zend_Registry::get('Zend_Translate');
214 if(null === $feedId)
216 $this->_flashMessenger->addMessage($translate->_(
217 'No feed specified.'));
218 $this->_redirect();
220 $feedsTable = Doctrine::getTable('PlanetFeed');
221 $feed = $feedsTable->find($feedId);
222 $location = $feed->Block->location;
223 $planetId = $feed->block_id;
224 $planetCache = Planet_Module::loadCache();
225 $feedCache = Planet_Module::loadCache('feed');
228 $feed->delete();
229 $planetCache->remove($planetId);
230 $feedCache->remove($feedId);
231 } catch(Exception $e) {
232 $logger = Zend_Registry::get('logger');
233 $logger->err($e->getMessage());
234 $this->_flashMessenger->setNamespace('error')
235 ->addMessage($translate->_(
236 'An error occurred while deleting the feed, please try again.'));
237 $this->_redirect($location);
239 $this->_flashMessenger->resetNamespace()->addMessage($translate->_(
240 'The feed was successfully deleted.'));
241 $this->_redirect($location);
245 * @return Zend_Form The form to add an HTML block
247 protected function _getForm()
249 $form = new Zend_Form;
250 $form->setMethod('post')
251 ->setAction($this->_request->getRequestUri())
252 ->addElement('text', 'link')
253 ->link->setRequired(true)
254 ->setLabel('Link to feed')
255 ->addValidator('NotEmpty');
256 $form->addDisplayGroup(array('link'), 'feed')
257 ->feed->setLegend('Feed');
258 $submit = new Zend_Form_Element_Submit('submit');
259 $submit->setLabel('Add feed')
260 ->addDecorator('HtmlTag', array('tag' => 'dd'))
261 ->removeDecorator('DtDdWrapper');
262 $form->addElement($submit);
263 return $form;
267 * @param $privileges What the user needs to be allowed to do to blocks
268 * @return bool Whether the user has sufficient rights
270 protected function _isAllowed($privileges = null)
272 $auth = Pivip_Auth::getInstance();
273 $acl = Zend_Registry::get('acl');
274 $identity = $auth->getIdentityProperties();
275 if('edit' == $privileges || 'add' == $privileges)
277 if(!$acl->isAllowed('guest', 'planet', 'write')
278 && !$auth->hasIdentity())
280 return false;
282 if(!$acl->isAllowed($identity->aclRole, 'planet', 'write'))
284 return false;
287 if(!$acl->isAllowed('guest', 'block', $privileges) &&
288 !$auth->hasIdentity())
290 return false;
292 return $acl->isAllowed($identity->aclRole, 'block', $privileges);