* The .htaccess file is updated to set some PHP flags to sane defaults.
[pivip.git] / project / modules / html / controllers / IndexController.php
blob5431e9a6d2d98c5cdf54079c2ad1d549a132bdf3
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 return;
43 $cache = Html_Module::loadCache();
44 if(!$code = $cache->load($htmlId))
46 $row = Doctrine::getTable('Html')->find($htmlId);
47 $code = $row->html;
48 $cache->save($code, $htmlId, array('html', 'block'));
50 $this->view->code = $code;
51 $this->view->urlParams = array('html_id' => $htmlId);
54 /**
55 * Add an HTML block
57 public function addAction()
59 $section = $this->_request->getParam('section');
60 if(empty($section))
62 return;
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()))
72 return;
74 $translate = Zend_Registry::get('Zend_Translate');
75 try
77 $block = new Block();
78 $block->location = $location;
79 $block->action = $section;
80 $block->controller = 'index';
81 $block->module = 'html';
82 $block->Html->html = $form->getValue('html');
83 $block->save();
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);
95 /**
96 * Edit an HTML block
98 public function editAction()
100 $htmlId = $this->_request->getParam('html_id');
101 $translate = Zend_Registry::get('Zend_Translate');
103 if(null === $htmlId)
105 $this->_flashMessenger->addMessage($translate->_(
106 'No HTML block specified.'));
107 $this->_redirect();
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()))
124 return;
128 $html->html = $form->getValue('html');
129 $html->save();
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');
151 if(null === $htmlId)
153 $this->_flashMessenger->addMessage($translate->_(
154 'No HTML block specified.'));
155 $this->_redirect();
157 $htmlTable = Doctrine::getTable('Html');
158 $html = $htmlTable->find($htmlId);
159 $location = $html->Block->location;
162 $html->Block->delete();
163 $html->delete();
164 $this->_flashMessenger->addMessage($translate->_(
165 'Block deleted.'));
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)
185 ->setLabel('HTML')
186 ->addPrefixPath('Vogel_Filter', 'Vogel/Filter', 'filter')
187 ->addFilter('HtmlPurifier');
188 $form->addElement($html)
189 ->setMethod('post')
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);
198 return $form;