* [Html] You can now only add an HTML block if you have the rights to write HTML...
[pivip.git] / project / modules / html / controllers / IndexController.php
blobeda62e7b0c21473f90bbcb34cdf923b014a7867f
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_Html
23 * @subpackage Controllers
24 * @copyright (C) 2008 Vincent Tunru
25 * @author Vincent Tunru <email@vincentt.org>
28 /**
29 * Load an HTML block
31 class Html_IndexController extends Pivip_Controller_Module_Abstract
33 /**
34 * Load an HTML block
36 public function mainAction()
38 $htmlId = $this->_request->getParam('block_id');
39 if(null === $htmlId)
41 $this->_helper->viewRenderer->setNoRender();
42 return;
44 $cache = Html_Module::loadCache();
45 if(!$code = $cache->load($htmlId))
47 $row = Doctrine::getTable('Html')->find($htmlId);
48 $code = $row->html;
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;
65 /**
66 * Add an HTML block
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');
79 if(empty($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()))
92 return;
94 try
96 $block = new Block();
97 $block->location = $location;
98 $block->action = $section;
99 $block->controller = 'index';
100 $block->module = 'html';
101 $block->Html->html = $form->getValue('html');
102 $block->save();
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);
115 * Edit an HTML block
117 public function editAction()
119 $translate = Zend_Registry::get('Zend_Translate');
121 $htmlId = $this->_request->getParam('html_id');
123 if(null === $htmlId)
125 $this->_flashMessenger->addMessage($translate->_(
126 'No HTML block specified.'));
127 $this->_redirect();
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()))
152 return;
156 $html->html = $form->getValue('html');
157 $html->save();
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');
179 if(null === $htmlId)
181 $this->_flashMessenger->addMessage($translate->_(
182 'No HTML block specified.'));
183 $this->_redirect();
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();
200 $html->delete();
201 $this->_flashMessenger->addMessage($translate->_(
202 'Block deleted.'));
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)
222 ->setLabel('HTML');
223 $form->addElement($html)
224 ->setMethod('post')
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);
233 return $form;
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())
249 return false;
251 if(!$acl->isAllowed($identity->aclRole, 'html', 'write'))
253 return false;
256 if(!$acl->isAllowed('guest', 'block', $privileges) &&
257 !$auth->hasIdentity())
259 return false;
261 return $acl->isAllowed($identity->aclRole, 'block', $privileges);