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_Html
23 * @subpackage Controllers
24 * @copyright (C) 2008 Vincent Tunru
25 * @author Vincent Tunru <email@vincentt.org>
31 class Html_IndexController
extends Pivip_Controller_Module_Abstract
36 public function mainAction()
38 $htmlId = $this->_request
->getParam('block_id');
41 $this->_helper
->viewRenderer
->setNoRender();
44 $cache = Html_Module
::loadCache();
45 if(!$code = $cache->load($htmlId))
47 $row = Doctrine
::getTable('Html')->find($htmlId);
49 $cache->save($code, $htmlId, array('html', 'block'));
51 $filter = new Vogel_Filter_HtmlPurifier();
52 $this->view
->setEscape(array($filter, 'filter'));
53 $this->view
->code
= $code;
54 $this->view
->urlParams
= array('html_id' => $htmlId);
55 if($this->_isAllowed('edit'))
57 $this->view
->allowEdit
= true;
59 if($this->_isAllowed('delete'))
61 $this->view
->allowDelete
= true;
68 public function addAction()
70 $translate = Zend_Registry
::get('Zend_Translate');
71 $location = '/' . $this->_request
->getParam('location');
72 if(!$this->_isAllowed('add'))
74 $this->_flashMessenger
->addMessage($translate->_(
75 'You are not allowed to add a new HTML block.'));
76 $this->_redirect($location);
78 $section = $this->_request
->getParam('section');
81 $this->_flashMessenger
->addMessage($translate->_(
82 'You need to specify a section to add HTML to.'));
83 $this->_redirect($location);
85 $defaultRequest = Zend_Registry
::get('defaultRequest');
86 $this->_helper
->actionStack($defaultRequest);
87 $form = $this->_getForm();
88 $this->view
->form
= $form->render();
89 if(!$this->_request
->isPost() ||
90 !$form->isValid($this->_request
->getPost()))
97 $block->location
= $location;
98 $block->action
= $section;
99 $block->controller
= 'index';
100 $block->module
= 'html';
101 $block->Html
->html
= $form->getValue('html');
103 $cacheId = Page_Module
::urlToCacheId($location);
104 $cache = Page_Module
::loadCache();
105 $cache->remove($cacheId);
106 $this->_flashMessenger
->addMessage($translate->_('Block added.'));
107 } catch(Exception
$e) {die($e->getMessage());
108 $this->_flashMessenger
->setNamespace('error')->addMessage($translate
109 ->_('Failed to add the block.'));
111 $this->_redirect($location);
117 public function editAction()
119 $translate = Zend_Registry
::get('Zend_Translate');
121 $htmlId = $this->_request
->getParam('html_id');
125 $this->_flashMessenger
->addMessage($translate->_(
126 'No HTML block specified.'));
130 $htmlTable = Doctrine
::getTable('Html');
131 $html = $htmlTable->find($htmlId);
133 if(!$this->_isAllowed('edit'))
135 $this->_flashMessenger
->addMessage($translate->_(
136 'You are not allowed to edit HTML blocks.'));
137 $this->_redirect($html->Block
->location
);
140 $this->_helper
->viewRenderer
->setScriptAction('add');
141 $defaultRequest = Zend_Registry
::get('defaultRequest');
142 $this->_helper
->actionStack($defaultRequest);
144 $form = $this->_getForm();
145 $form->submit
->setLabel('Edit');
146 $form->block
->setLegend('Edit block');
147 $form->populate($html->toArray());
148 $this->view
->form
= $form->render();
149 if(!$this->_request
->isPost() ||
150 !$form->isValid($this->_request
->getPost()))
156 $html->html
= $form->getValue('html');
158 $this->_flashMessenger
->addMessage($translate->_('Block updated.'));
159 } catch(Exception
$e) {
160 $this->_flashMessenger
->setNamespace('error')->addMessage($translate->_(
161 'An error occured while updating the block, please try again.'));
162 $this->_redirect($this->_request
->getRequestUri());
164 $cache = Html_Module
::loadCache();
165 $cache->remove($htmlId);
166 $this->_redirect($html->Block
->location
);
170 * Delete an HTML block
172 * @todo Allow the user to undo this
174 public function deleteAction()
176 $htmlId = $this->_request
->getParam('html_id');
177 $translate = Zend_Registry
::get('Zend_Translate');
181 $this->_flashMessenger
->addMessage($translate->_(
182 'No HTML block specified.'));
186 $htmlTable = Doctrine
::getTable('Html');
187 $html = $htmlTable->find($htmlId);
189 if(!$this->_isAllowed('delete'))
191 $this->_flashMessenger
->addMessage($translate->_(
192 'You are not allowed to delete blocks.'));
193 $this->_redirect($html->Block
->location
);
196 $location = $html->Block
->location
;
199 $html->Block
->delete();
201 $this->_flashMessenger
->addMessage($translate->_(
203 $cache = Html_Module
::loadCache();
204 $cache->remove($htmlId);
205 $cache = Page_Module
::loadCache();
206 $cache->remove(Page_Module
::urlToCacheId($location));
207 } catch(Exception
$e) {
208 $this->_flashMessenger
->setNamespace('error')->addMessage($translate->_(
209 'An error occurred while deleting the block, please try again.'));
211 $this->_redirect($location);
215 * @return Zend_Form The form to add an HTML block
217 protected function _getForm()
219 $form = new Zend_Form
;
220 $html = new Zend_Form_Element_Textarea('html');
221 $html->setRequired(true)
223 $form->addElement($html)
225 ->setAction($this->_request
->getRequestUri())
226 ->addDisplayGroup(array('html'), 'block')
227 ->block
->setLegend('Add block');
228 $submit = new Zend_Form_Element_Submit('submit');
229 $submit->setLabel('Add')
230 ->addDecorator('HtmlTag', array('tag' => 'dd'))
231 ->removeDecorator('DtDdWrapper');
232 $form->addElement($submit);
237 * @param $privileges What the user needs to be allowed to do to blocks
238 * @return bool Whether the user has sufficient rights
240 protected function _isAllowed($privileges = null)
242 $auth = Pivip_Auth
::getInstance();
243 $acl = Zend_Registry
::get('acl');
244 $identity = $auth->getIdentityProperties();
245 if('edit' == $privileges ||
'add' == $privileges)
247 if(!$acl->isAllowed('guest', 'html', 'write') && !$auth->hasIdentity())
251 if(!$acl->isAllowed($identity->aclRole
, 'html', 'write'))
256 if(!$acl->isAllowed('guest', 'block', $privileges) &&
257 !$auth->hasIdentity())
261 return $acl->isAllowed($identity->aclRole
, 'block', $privileges);