* @since 1.0 * @package MammutCMS * @subpackage Module */ class MMModStatic extends MMModBase { protected function getMyId() { return 'mcms.static'; } public function hasFilePriv(iUser $usr, array $params) { $system = System::getInstance(); return !is_null($usr) && $this->userHasPriv($usr,$system->getSiteId(),$params['target'],'edit'); } /** * Displayes the text */ public function runIndex(Request $request, Response $response, $activeInstance) { $system = System::getInstance(); $db = $system->getDB(); $usrSvc = $system->getUserSvc(); $locale = $system->getActiveLocale(); $strings = $this->loadLocaleStrings($locale,__DIR__); $textId = $system->getSiteParam($system->getSiteId(),$activeInstance,'textId'); if (is_null($textId)) { $data = array( 'site_id' => $system->getSiteId(), 'instance' => $activeInstance, 'key' => '', 'title' => 'Sample title', 'descr' => 'Static text entry', 'langu' => '', 'langukey' => '', 'state' => 0, 'revision' => 0, 'read_count' => 0, 'content' => 'Sample text' ); $db->table('content')->insert($data); unset($data); $textId = $db->getInsertId(); $system->setSiteParam($system->getSiteId(),$activeInstance,'textId',$textId); } $content = $db->getObject("SELECT * FROM content WHERE site_id=".$system->getSiteId()." AND id=".$textId,'\\MCMS\\Model\\Content'); try { $layout = $this->findLayout($system->getSiteId(),$activeInstance); $template = $this->loadTemplate($this->getMyId(),$system->getTarget('action'),$layout); $css = $this->getLayoutCSSFiles($this->getMyId(),$system->getTarget('action'),$layout); foreach ($css as $cssAddOn) $response->addons[Response::ADDON_CSS][] = $cssAddOn; $template->setParam('title',$content->title); // put the text object in the template $template->setParam('text',$content->content); // put the text object in the template $template->setParam('adminlink',_SELF_.'?mod='.$activeInstance.'&view=edit'); // put the text object in the template $template->setParam('edit_text_link', $strings['edit_text_link']); // put the text object in the template $usr = $system->getUserSvc()->getUser(); if (is_null($usr)) $template->setParam('admin',false); else $template->setParam('admin',$this->userHasPriv($usr,$system->getSiteId(),$activeInstance,'edit')); // $response->setFragment('CONTENT',$template->getDocument()); // render the template end put the result in the response // load site layout and add main content $siteLayout = $system->getSiteLayout($system->getSiteId(),iLayout::T_ERROR); $siteLayout->setParam('CONTENT', $template->getDocument()); // mark the content as a html document $document = new HTMLDoc(); $document->setContent($siteLayout->getDocument()); // put the html in a http response $response = new HTTPResponse(); $response->setBuffer($document); return $response; } catch (FileNotFoundException $ex) { // $response->setFragment('CONTENT',"file is missing: ".$ex->getContext()); $response = new HTTPResponse(); $siteLayout = $system->getSiteLayout($system->getSiteId(),iLayout::T_ERROR); $siteLayout->setFragment('CONTENT',"file is missing: ".$ex->getContext()); $response->addToBuffer($siteLayout->generate()); return $response; } } /** * Displayes a form to edit the text */ public function runEdit(Request $request, Response $response, $activeInstance) { $system = System::getInstance(); $db = $system->getDB(); $usrSvc = $system->getUserSvc(); $usr = $usrSvc->getUser(); $locale = $system->getActiveLocale(); $strings = $this->loadLocaleStrings($locale,__DIR__); if (is_null($usr) || (!$usrSvc->userHasPriv($usr,$system->getSiteId(),$activeInstance,'edit'))) throw new AccessDeniedException('not privileged'); // if not, throw an exception $textId = $system->getSiteParam($system->getSiteId(),$activeInstance,'textId'); $content = $db->getObject("SELECT * FROM content WHERE site_id=".$system->getSiteId()." AND id=".$textId,'\\MCMS\\Model\\Content'); // $form = new MMFormBuilder($content); $form = $system->createFormBuilder($content); $form->setTargetLink(_SELF_); $form->setTargetParams(array('mod' => $activeInstance, 'view' => 'update')); try { $layout = $this->findLayout($system->getSiteId(),$activeInstance); $template = $this->loadTemplate($this->getMyId(),$system->getTarget('action'),$layout); // put the text object in the template $template->setParam('title', $strings['head_edit']); $template->setParam('lbl_title', $strings['lbl_title']); $template->setParam('lbl_text', $strings['lbl_text']); $template->setParam('formStart', (string) $form->genStart()); $fp = $template->getParamParams('editTitle'); if (isset($fp['css'])) $param[\MCMS\_\Com\MCMS\Form\Builder::EP_INLINE_CSS] = $fp['css']; if (!empty($param)) $form->setEditorParams('title', $param); $template->setParam('editTitle', (string) $form->genElement('title')); $fp = $template->getParamParams('editContent'); if (isset($fp['css'])) $param[\MCMS\_\Com\MCMS\Form\Builder::EP_INLINE_CSS] = $fp['css']; if (!empty($param)) $form->setEditorParams('content', $param); $template->setParam('editContent', (string) $form->genElement('content')); $template->setParam('formSubmit', (string) $form->genSubmit($strings['btn_save'])); $template->setParam('formEnd',$form->genEnd()); $template->setParam('backlink',_SELF_.'?mod='.$activeInstance.'&view=index'); // put the text object in the template $response->setFragment('CONTENT',$template->getDocument()); } catch (FileNotFoundException $ex) { $response->setFragment('CONTENT',"file is missing: ".$tplfile); } } /** * Updates the text and redirects to the index page */ public function runUpdate(Request $request, Response $response, $activeInstance) { $system = System::getInstance(); $db = $system->getDB(); $usrSvc = $system->getUserSvc(); $usr = $usrSvc->getUser(); if (is_null($usr) || (!$usrSvc->userHasPriv($usr,$system->getSiteId(),$activeInstance,'edit'))) throw new AccessDeniedException('not privileged'); // if not, throw an exception $textId = $system->getSiteParam($system->getSiteId(),$activeInstance,'textId'); $content = $db->table('content')->getObject(array('site_id' => $system->getSiteId(),'id' => $textId),false,'\\MCMS\\Model\\Content'); $fetcher = $system->createFormFetcher($content); $data = $fetcher->fetchOnly(array('title','content')); if (!isset($data->title)) $data->title = ''; $db->table('content')->update($data,array('id' => $textId),array('id')); $response->setRedirect($activeInstance,'index'); } }