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');
43 $cache = Html_Module
::loadCache();
44 if(!$code = $cache->load($htmlId))
46 $row = Doctrine
::getTable('Html')->find($htmlId);
48 $cache->save($code, $htmlId, array('html', 'block'));
50 $this->view
->code
= $code;
51 $this->view
->urlParams
= array('html_id' => $htmlId);
57 public function addAction()
59 $section = $this->_request
->getParam('section');
64 $location = '/' . $this->_request
->getParam('location');
65 $defaultRequest = Zend_Registry
::get('defaultRequest');
66 $this->_helper
->actionStack($defaultRequest);
67 $form = $this->_getForm();
68 $this->view
->form
= $form->render();
69 if(!$this->_request
->isPost() ||
70 !$form->isValid($this->_request
->getPost()))
74 $translate = Zend_Registry
::get('Zend_Translate');
78 $block->location
= $location;
79 $block->action
= $section;
80 $block->controller
= 'index';
81 $block->module
= 'html';
82 $block->Html
->html
= $form->getValue('html');
84 $cacheId = str_replace('/', '_', $location);
85 $cache = Page_Module
::loadCache();
86 $cache->remove($cacheId);
87 $this->_flashMessenger
->addMessage($translate->_('Block added.'));
88 } catch(Exception
$e) {die($e->getMessage());
89 $this->_flashMessenger
->setNamespace('error')->addMessage($translate
90 ->_('Failed to add the block.'));
92 $this->_redirect($location);
98 public function editAction()
100 $htmlId = $this->_request
->getParam('html_id');
101 $translate = Zend_Registry
::get('Zend_Translate');
105 $this->_flashMessenger
->addMessage($translate->_(
106 'No HTML block specified.'));
110 $this->_helper
->viewRenderer
->setScriptAction('add');
111 $defaultRequest = Zend_Registry
::get('defaultRequest');
112 $this->_helper
->actionStack($defaultRequest);
114 $htmlTable = Doctrine
::getTable('Html');
115 $html = $htmlTable->find($htmlId);
116 $form = $this->_getForm();
117 $form->submit
->setLabel('Edit');
118 $form->block
->setLegend('Edit block');
119 $form->populate($html->toArray());
120 $this->view
->form
= $form->render();
121 if(!$this->_request
->isPost() ||
122 !$form->isValid($this->_request
->getPost()))
128 $html->html
= $form->getValue('html');
130 $this->_flashMessenger
->addMessage($translate->_('Block updated.'));
131 } catch(Exception
$e) {
132 $this->_flashMessenger
->setNamespace('error')->addMessage($translate->_(
133 'An error occured while updating the block, please try again.'));
134 $this->_redirect($this->_request
->getRequestUri());
136 $cache = Html_Module
::loadCache();
137 $cache->remove($htmlId);
138 $this->_redirect($html->Block
->location
);
142 * Delete an HTML block
144 * @todo Allow the user to undo this
146 public function deleteAction()
148 $htmlId = $this->_request
->getParam('html_id');
149 $translate = Zend_Registry
::get('Zend_Translate');
153 $this->_flashMessenger
->addMessage($translate->_(
154 'No HTML block specified.'));
157 $htmlTable = Doctrine
::getTable('Html');
158 $html = $htmlTable->find($htmlId);
159 $location = $html->Block
->location
;
162 $html->Block
->delete();
164 $this->_flashMessenger
->addMessage($translate->_(
166 $cache = Html_Module
::loadCache();
167 $cache->remove($htmlId);
168 $cache = Page_Module
::loadCache();
169 $cache->remove(str_replace('/', '_', $location));
170 } catch(Exception
$e) {
171 $this->_flashMessenger
->setNamespace('error')->addMessage($translate->_(
172 'An error occurred while deleting the block, please try again.'));
174 $this->_redirect($location);
178 * @return Zend_Form The form to add an HTML block
180 protected function _getForm()
182 $form = new Zend_Form
;
183 $html = new Zend_Form_Element_Textarea('html');
184 $html->setRequired(true)
186 ->addPrefixPath('Vogel_Filter', 'Vogel/Filter', 'filter')
187 ->addFilter('HtmlPurifier');
188 $form->addElement($html)
190 ->setAction($this->_request
->getRequestUri())
191 ->addDisplayGroup(array('html'), 'block')
192 ->block
->setLegend('Add block');
193 $submit = new Zend_Form_Element_Submit('submit');
194 $submit->setLabel('Add')
195 ->addDecorator('HtmlTag', array('tag' => 'dd'))
196 ->removeDecorator('DtDdWrapper');
197 $form->addElement($submit);