* @since 1.0 * @package MammutCMS * @subpackage Module */ class MMModStaticxt extends MMModBase { protected function getMyId() { return 'mcms.staticxt'; } /** * Load the layout info array * * @param string $modId * @param string $layout * @return array the layout info */ public static function getLayoutInfo($modId, $layout) { $ua = System::getInstance()->getRequest()->getUserAgent(); $modId = strtolower($modId); $name = strtolower($layout); $extended = false; if($layout[0] == '*') { $file = _LAYOUTPATHX_ . __DS__ . '_module_'; $layout = substr($layout, 1); } else $file = $extended ? _MODPATHX_ : _MODPATH_; $filepath = $file . __DS__ . str_replace('.', __DS__, $modId) . __DS__ . '_layout_' . __DS__ . $layout . __DS__; $result = array(); if(include_exists($filepath . '_info_.php')) $result = get_configfile_array($filepath . '_info_.php', 'info'); return $result; } public function hasFilePriv(iUser $usr, array $params) { $system = System::getInstance(); return !is_null($usr) && $this->userHasPriv($usr, $system->getSiteId(), $params['target'], 'edit'); } protected function getContentObject($siteId, $instance, $name) { $system = System::getInstance(); $db = $system->getDB(); $efn_key = $db->escapeColumnName('key'); $content = $db->getObjectP("SELECT * FROM content WHERE site_id=? AND instance=? AND {$efn_key}=? AND revision=0", array( $siteId,$instance,$name), '\\MCMS\\Model\\Content'); if(empty($content)) { $content = new Content(); $content->site_id = $siteId; $content->instance = $instance; $content->key = $name; $content->title = ''; $content->descr = ''; $content->content = ''; $content->langu = ''; $content->langukey = ''; $content->state = 0; $content->revision = 0; $content->read_count = 0; $content = ContentSvc::storeContent($content); } return $content; } /** * Fetches a content block as HTML. * * Reads the content from the database, and creates a new row if none exists atm. * * @param int $siteId * the active website * @param string $instance * the instance of this module * @param string $name * block name * @return string the content of the block */ protected function getContentHTMLBlock($siteId, $instance, $name) { return $this->getContentObject($siteId, $instance, $name)->content; } /** * Fetches a content block as text. * * Does the same as getContentHTMLBlock with the difference that it escapes critical * characters. * * @param int $siteId * the active website * @param string $instance * the instance of this module * @param string $name * block name * @return string the content of the block */ protected function getContentTextBlock($siteId, $instance, $name) { return nl2br(htmlentities($this->getContentObject($siteId, $instance, $name)->content),true); } /** * 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__); $siteId = $system->getSiteId(); $layout = $this->findLayout($system->getSiteId(), $activeInstance); $info = self::getLayoutInfo($this->getMyId(), $layout); $blocks = is_array($info['blocks']) ? $info['blocks'] : array(); $blockdata = array(); foreach($blocks as $block=>$type) { switch(strtolower($type)) { case 'string': $blockdata[$block] = $this->getContentTextBlock($siteId, $activeInstance, $block); break; case 'text': $blockdata[$block] = $this->getContentTextBlock($siteId, $activeInstance, $block); break; case 'image': $blockdata[$block] = ''; break; case 'html': $blockdata[$block] = $this->getContentHTMLBlock($siteId, $activeInstance, $block); break; case 'video': $blockdata[$block] = '?'; break; default: $blockdata[$block] = 'INVALID BLOCK TYPE ' . $type; break; } } try { $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 foreach($blocks as $block=>$type) { $template->setParam($block, $blockdata[$block]); // 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 } catch(FileNotFoundException $ex) { $response->setFragment('CONTENT', "file is missing: " . $ex->getContext()); } } /** * 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 $siteId = $system->getSiteId(); $layout = $this->findLayout($system->getSiteId(), $activeInstance); $info = self::getLayoutInfo($this->getMyId(), $layout); $blocks = is_array($info['blocks']) ? $info['blocks'] : array(); $form = $system->createFormBuilder(); $form->setTargetLink(_SELF_); $form->setTargetParams(array('mod' => $activeInstance,'view' => 'update')); foreach($blocks as $block=>$type) { switch(strtolower($type)) { case 'string': $form->addExtraEditor($block, 'string(255)', $this->getContentHTMLBlock($siteId, $activeInstance, $block)); break; case 'text': $form->addExtraEditor($block, 'text', $this->getContentHTMLBlock($siteId, $activeInstance, $block)); break; case 'image': break; case 'html': $form->addExtraEditor($block, 'html', $this->getContentHTMLBlock($siteId, $activeInstance, $block)); break; case 'video': break; default: // $blockdata[$block] = 'INVALID BLOCK TYPE ' . $type; break; } } 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')); foreach($blocks as $block=>$type) { if(in_array($type, array('image'))) // skip unfinished types continue; $blockname = 'edit' . strupperfirst($block); $fp = $template->getParamParams($blockname); if(isset($fp['css'])) $param[\MCMS\_\Com\MCMS\Form\Builder::EP_INLINE_CSS] = $fp['css']; if(!empty($param)) $form->setEditorParams($block, $param); $template->setParam($blockname, (string) $form->genElement($block)); } $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 $siteId = $system->getSiteId(); $layout = $this->findLayout($system->getSiteId(), $activeInstance); $info = self::getLayoutInfo($this->getMyId(), $layout); $blocks = is_array($info['blocks']) ? $info['blocks'] : array(); $fetcher = $system->createFormFetcher(); foreach($blocks as $block=>$type) { switch(strtolower($type)) { case 'string': $fetcher->addExtraEditor($block, 'string(255)'); break; case 'text': $fetcher->addExtraEditor($block, 'text'); break; case 'image': break; case 'html': $fetcher->addExtraEditor($block, 'html'); break; case 'video': break; default: // $blockdata[$block] = 'INVALID BLOCK TYPE ' . $type; break; } } $db->isTransactionSupported() && $db->startTransaction(); foreach($blocks as $block=>$type) { switch(strtolower($type)) { case 'string': $content = $this->getContentObject($siteId, $activeInstance, $block); $content->content = $fetcher->fetchExtraField($block); ContentSvc::storeContent($content); break; case 'text': $content = $this->getContentObject($siteId, $activeInstance, $block); $content->content = $fetcher->fetchExtraField($block); ContentSvc::storeContent($content); break; case 'image': break; case 'html': $content = $this->getContentObject($siteId, $activeInstance, $block); $content->content = $fetcher->fetchExtraField($block); ContentSvc::storeContent($content); break; case 'video': break; } } $db->isTransactionSupported() && $db->commit(); $response->setRedirect($activeInstance, 'index'); } }