* [Planet] You can now edit a planet's settings (i.e. title and the number of entrie...
[pivip.git] / project / modules / planet / controllers / PlanetController.php
blob3a50ea81207c8af877214a1da9d9c2f4557ebf9c
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 * Create a "Planet" of RSS feeds
30 * A "Planet" is an aggregation of RSS feeds from many different sources.
32 class Planet_PlanetController extends Page_Abstract
34 /**
35 * Generate a Planet feed
37 * Generate a Planet feed from the different sources and pass that on to the
38 * feed controller.
40 * @todo Allow changing the Planet settings such as the title
41 * @todo Refactor
43 public function mainAction()
45 $planetId = $this->_request->getParam('block_id');
46 if(null === $planetId)
48 $this->_helper->viewRenderer->setNoRender();
49 return;
51 $settingsCache = Planet_Module::loadCache('settings');
52 if(!$settings = $settingsCache->load($planetId))
54 $planetTable = Doctrine::getTable('PlanetPlanet');
55 $settings = $planetTable->find($planetId);
56 $settingsCache->save($settings, $planetId,
57 array('planet', 'settings'));
59 $cache = Planet_Module::loadCache();
60 if(!$rows = $cache->load($planetId))
62 $query = Doctrine_Query::create();
63 $query->select('f.link')
64 ->from('PlanetFeed f')
65 ->where('f.block_id=?', $planetId);
66 $rows = $query->execute();
67 $cache->save($rows, $planetId, array('planet', 'feeds'));
69 $feedCache = Planet_Module::loadCache('feed');
70 $entryCache = Planet_Module::loadCache('entry');
71 $planetFeed = array();
72 $entryRoute = Zend_Controller_Front::getInstance()->getRouter()
73 ->getRoute('Planet_ViewEntry');
74 foreach($rows as $row)
76 $cacheId = $row->feed_id;
77 if(!$feed = $feedCache->load($cacheId))
79 try
81 $imported = Zend_Feed::import($row->link);
82 } catch(Exception $e) {
83 Zend_Registry::get('logger')->err($e->getMessage());
84 continue;
86 $feed = array();
87 foreach($imported as $entry)
89 $entry = new Vogel_Feed_Entry_Unified($entry);
90 $guid = $entry->id();
91 $guid = Pivip_Utility_Cache::toCacheId($guid);
92 $planetEntry = array('title' => $entry->title(),
93 'link' => $entry->link(),
94 'description' => $entry->description(),
95 'content' => $entry->content(),
96 'lastUpdate' => strtotime($entry->date()));
97 $feed[] = $guid;
98 $entryCache->save($planetEntry, $guid, array('planet', 'entry'));
99 $planetFeed['entries'][] = $planetEntry;
101 $feedCache->save($feed, $cacheId, array('planet', 'feed'));
102 } else {
103 foreach($feed as $guid)
105 if(!$planetEntry = $entryCache->load($guid))
107 continue;
109 $planetFeed['entries'][] = $planetEntry;
113 usort($planetFeed['entries'], array($this, 'compareEntriesByDate'));
114 $i = 0;
115 $entries = array();
116 foreach($planetFeed['entries'] as $k => $v)
118 if($settings->entries <= $i++)
120 break;
122 $entries[$k] = $v;
124 $planetFeed['entries'] = $entries;
125 $planetFeed['title'] = $settings->title;
126 $route = Zend_Controller_Front::getInstance()->getRouter()
127 ->getRoute('Planet_PlanetFeed');
128 $planetFeed['link'] = $this->_request->getBaseUrl() . '/'
129 . $route->assemble(array('block_id' => $planetId));
130 $planetFeed['charset'] = 'utf-8';
131 $planetFeed = Zend_Feed::importArray($planetFeed);
132 $this->view->feed = $planetFeed;
133 $this->view->urlParams = array('planet_id' => $planetId);
134 if($this->_isAllowed('edit'))
136 $this->view->allowEdit = true;
138 if($this->_isAllowed('delete'))
140 $this->view->allowDelete = true;
145 * Sort entries by date
147 public function compareEntriesByDate($a, $b)
149 if(!isset($b['lastUpdate']))
151 return -1;
152 } else if(!isset($a['lastUpdate'])) {
153 return 1;
155 if($a['lastUpdate'] > $b['lastUpdate']) {
156 return -1;
157 } else if($a['lastUpdate'] < $b['lastUpdate']) {
158 return 1;
160 return 0;
164 * Add a planet
166 public function addAction()
168 $translate = Zend_Registry::get('Zend_Translate');
169 $location = '/' . $this->_request->getParam('location');
170 if(!$this->_isAllowed('add'))
172 $this->_flashMessenger->addMessage($translate->_(
173 'You are not allowed to add a planet.'));
174 $this->_redirect($location);
176 $section = $this->_request->getParam('section');
177 if(empty($section))
179 $this->_flashMessenger->addMessage($translate->_(
180 'You need to specify a section to add a planet to.'));
181 $this->_redirect($location);
184 $defaultRequest = Zend_Registry::get('defaultRequest');
185 $this->_helper->actionStack($defaultRequest);
187 $form = $this->_getForm();
188 $this->view->form = $form->render();
189 if(!$this->_request->isPost() ||
190 !$form->isValid($this->_request->getPost()))
192 return;
196 $block = new Block();
197 $block->PlanetPlanet->title = $form->getValue('title');
198 $block->PlanetPlanet->entries = $form->getValue('entries');
199 $block->location = $location;
200 $block->action = $section;
201 $block->controller = 'planet';
202 $block->module = 'planet';
203 $block->save();
204 $cacheId = Page_Module::urlToCacheId($location);
205 $cache = Page_Module::loadCache();
206 $cache->remove($cacheId);
207 $this->_flashMessenger->addMessage($translate->_(
208 'Planet created, please add a feed.'));
209 } catch(Exception $e) {
210 $this->_flashMessenger->setNamespace('error')->addMessage($translate
211 ->_('Failed to add the planet.'));
212 $this->_redirect($location);
214 $route = Zend_Controller_Front::getInstance()->getRouter()
215 ->getRoute('Planet_AddFeed');
216 $options = array('planet_id' => $block->block_id);
217 $this->_redirect($route->assemble($options));
221 * Edit a Planet
223 public function editAction()
225 $planetId = $this->_request->getParam('planet_id');
226 $translate = Zend_Registry::get('Zend_Translate');
228 if(null === $planetId)
230 $this->_flashMessenger->addMessage($translate->_(
231 'No planet specified.'));
232 $this->_redirect('');
235 if(!$this->_isAllowed('edit'))
237 $this->_flashMessenger->addMessage($translate->_(
238 'You are not allowed to edit a planet.'));
239 $this->_redirect($html->Block->location);
242 $feedsTable = Doctrine::getTable('PlanetFeed');
243 $query = Doctrine_Query::create();
244 $query->from('PlanetFeed f')
245 ->where('block_id=?', $planetId);
246 $this->view->feeds = $query->execute();
248 $this->view->planetId = $planetId;
250 $planetTable = Doctrine::getTable('PlanetPlanet');
251 $settings = $planetTable->find($planetId);
252 $form = $this->_getForm();
253 $form->submit->setLabel('Edit planet');
254 $form->populate($settings->toArray());
255 $this->view->form = $form->render();
256 if(!$this->_request->isPost() ||
257 !$form->isValid($this->_request->getPost()))
259 return;
263 $settings->title = $form->getValue('title');
264 $settings->entries = $form->getValue('entries');
265 $settings->save();
266 $cache = Planet_Module::loadCache('settings');
267 $cache->remove($planetId);
268 $this->_flashMessenger->addMessage($translate->_(
269 'Planet updated.'));
270 $this->_redirect($settings->Block->location);
271 } catch(Exception $e) {
272 $this->_refresh('Failed to update the planet.', 'error');
277 * Delete a stream
279 * @todo Allow the user to undo this
280 * @todo Finish this function (i.e. delete all feeds and entries)
282 public function deleteAction()
284 $planetId = $this->_request->getParam('planet_id');
285 $translate = Zend_Registry::get('Zend_Translate');
287 if(null === $planetId)
289 $this->_flashMessenger->addMessage($translate->_(
290 'No Planet specified.'));
291 $this->_redirect();
299 * @return Zend_Form The form to add an HTML block
301 protected function _getForm()
303 $form = new Zend_Form;
304 $form->setMethod('post')
305 ->setAction($this->_request->getRequestUri())
306 ->addElement('text', 'title')
307 ->title->setRequired(true)
308 ->setLabel('Planet title')
309 ->addValidator('NotEmpty');
310 $form->addElement('text', 'entries')
311 ->entries->setLabel('Number of entries');
312 $form->addDisplayGroup(array('title', 'entries'), 'planet')
313 ->planet->setLegend('Planet');
314 $submit = new Zend_Form_Element_Submit('submit');
315 $submit->setLabel('Add planet')
316 ->addDecorator('HtmlTag', array('tag' => 'dd'))
317 ->removeDecorator('DtDdWrapper');
318 $form->addElement($submit);
319 return $form;
323 * @param $privileges What the user needs to be allowed to do to blocks
324 * @return bool Whether the user has sufficient rights
326 protected function _isAllowed($privileges = null)
328 $auth = Pivip_Auth::getInstance();
329 $acl = Zend_Registry::get('acl');
330 $identity = $auth->getIdentityProperties();
331 if('edit' == $privileges || 'add' == $privileges)
333 if(!$acl->isAllowed('guest', 'planet', 'write')
334 && !$auth->hasIdentity())
336 return false;
338 if(!$acl->isAllowed($identity->aclRole, 'planet', 'write'))
340 return false;
343 if(!$acl->isAllowed('guest', 'block', $privileges) &&
344 !$auth->hasIdentity())
346 return false;
348 return $acl->isAllowed($identity->aclRole, 'block', $privileges);