* @since 1.0 * @package MammutCMS * @subpackage Module */ class MMModNews extends MMModBaseNews { protected function getMyId() { return 'mcms.news'; } public function runIndex(Request $request, Response $response, $activeInstance) { $system = System::getInstance(); $param = $system->getRequest()->getParam(); $pagesize = $system->getSiteParam($system->getSiteId(),$activeInstance,'maxcount'); if ($pagesize <= 0) $pagesize = 5; $page = isset($param['page']) ? (int) $param['page'] : 0; if ($page < 0) $page = 0; $pagelist = new SimplePageList(); $pagelist->setCurrentPage($page); $pagelist->setWithRange(0,ReposSvc::getPageCount(1,$activeInstance,$pagesize),_SELF_.'?mod='.$activeInstance.'&page=##'); $usr = $system->getUserSvc()->getUser(); try { $entries = ReposSvc::getPageEntries(1,$activeInstance,$page,$pagesize); $layout = $this->findLayout($system->getSiteId(),$activeInstance); $template = $this->loadTemplate($this->getMyId(),$system->getTarget('action'),$layout,true); $template->setParam('title','News'); $template->setParam('entries',$entries); $template->setParam('self',_SELF_); $template->setParam('instance',$activeInstance); $template->setParam('addlink',_SELF_.'?mod='.$activeInstance.'&view=newEntry'); $template->setParam('pagelist',$pagelist->generate()); $template->setParam('isEditable', new CTemplateCallback(array($this,'checkEntryEdit'),array($entries,$activeInstance))); $usr = $system->getUserSvc()->getUser(); $template->setParam('isUser',!is_null($usr)); if (is_null($usr)) $template->setParam('isAdmin',false); else $template->setParam('isAdmin',$this->userHasPriv($usr,$system->getSiteId(),$activeInstance,'edit')); $response->setFragment('CONTENT',$template->getDocument()); } catch (FileNotFoundException $ex) { $response->setFragment('CONTENT',"file is missing: ".$tplfile); } } public function runHistory(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 MMAccessDeniedException('not privileged'); // if not, throw an exception $form = $system->createFormBuilder('\\MCMS\\_\\Modx\\MCMS\\News\\_\\Model\\Entry'); $form->setTargetLink(_SELF_); $form->setTargetParams(array('mod' => $activeInstance, 'view' => 'create')); try { $layout = $this->findLayout($system->getSiteId(),$activeInstance); $template = $this->loadTemplate($this->getMyId(),'edit',$layout,true); $template->setParam('title','Create news entry'); $template->setParam('edit',false); $template->setParam('formStart', (string) $form->genStart()); $template->setParam('editTitle', (string) $form->genElement('title')); $template->setParam('editContent', (string) $form->genElement('content')); $template->setParam('formSubmit', (string) $form->genSubmit('Create entry')); $template->setParam('formEnd',$form->genEnd()); $response->setFragment('CONTENT',$template->getDocument()); } catch (FileNotFoundException $ex) { $response->setFragment('CONTENT',"file is missing: ".$tplfile); } } public function runDetails(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 MMAccessDeniedException('not privileged'); // if not, throw an exception $form = $system->createFormBuilder('\\MCMS\\_\\Modx\\MCMS\\News\\_\\Model\\Entry'); $form->setTargetLink(_SELF_); $form->setTargetParams(array('mod' => $activeInstance, 'view' => 'create')); try { $layout = $this->findLayout($system->getSiteId(),$activeInstance); $template = $this->loadTemplate($this->getMyId(),'edit',$layout,true); $template->setParam('title','Create news entry'); $template->setParam('edit',false); $template->setParam('formStart', (string) $form->genStart()); $template->setParam('editTitle', (string) $form->genElement('title')); $template->setParam('editContent', (string) $form->genElement('content')); $template->setParam('formSubmit', (string) $form->genSubmit('Create entry')); $template->setParam('formEnd',$form->genEnd()); $response->setFragment('CONTENT',$template->getDocument()); } catch (FileNotFoundException $ex) { $response->setFragment('CONTENT',"file is missing: ".$tplfile); } } public function runAddComment(Request $request, Response $response, $activeInstance) { } /** * Displayes a form to edit the text */ public function runNewEntry(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 MMAccessDeniedException('not privileged'); // if not, throw an exception $form = $system->createFormBuilder('\\MCMS\\_\\Modx\\MCMS\\News\\_\\Model\\Entry'); $form->setTargetLink(_SELF_); $form->setTargetParams(array('mod' => $activeInstance, 'view' => 'createEntry')); try { $layout = $this->findLayout($system->getSiteId(),$activeInstance); $template = $this->loadTemplate($this->getMyId(),'edit',$layout,true); $template->setParam('title','Create news entry'); $template->setParam('edit',false); $template->setParam('formStart', (string) $form->genStart()); $template->setParam('editTitle', (string) $form->genElement('title')); $template->setParam('editContent', (string) $form->genElement('content')); $template->setParam('formSubmit', (string) $form->genSubmit('Create entry')); $template->setParam('formEnd',$form->genEnd()); $template->setParam('backlink','zurück'); $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 runCreateEntry(Request $request, Response $response, $activeInstance) { $system = System::getInstance(); $db = $system->getDB(); $usrSvc = $system->getUserSvc(); $usr = $usrSvc->getUser(); $param = $system->getRequest()->getParam(); if (is_null($usr) || (!$usrSvc->userHasPriv($usr,$system->getSiteId(),$activeInstance,'edit'))) throw new AccessDeniedException('not privileged'); // if not, throw an exception $form = $system->createFormFetcher('\\MCMS\\_\\Modx\\MCMS\\News\\_\\Model\\Entry'); $entry = $form->fetch($request); $entry->id = NULL; $entry->site_id = 1; $entry->instance = $activeInstance; $entry->created_at = new DateTime(); $entry->created_by_id = $usr->id; //$entry->langu = ''; $entry->state = 0; //$entry->category = 0; $entry->details = ''; ReposSvc::storeEntry($entry); $response->setRedirect($activeInstance,'index'); // redirect the request } /** * Displayes a form to edit the text */ public function runEditEntry(Request $request, Response $response, $activeInstance) { $system = System::getInstance(); $db = $system->getDB(); $usrSvc = $system->getUserSvc(); $usr = $usrSvc->getUser(); $param = $system->getRequest()->getParam(); if (is_null($usr) || (!$usrSvc->userHasPriv($usr,$system->getSiteId(),$activeInstance,'edit'))) throw new AccessDeniedException('not privileged'); // if not, throw an exception $entry = ReposSvc::getEntry((int) $param['id']); $form = $system->createFormBuilder($entry); $form->setTargetLink(_SELF_); $form->setTargetParams(array('mod' => $activeInstance, 'view' => 'updateEntry', 'id' => $entry->id)); try { $layout = $this->findLayout($system->getSiteId(),$activeInstance); $template = $this->loadTemplate($this->getMyId(),'edit',$layout,true); $template->setParam('title','Edit news entry'); $template->setParam('edit',false); $template->setParam('formStart', (string) $form->genStart()); $template->setParam('editTitle', (string) $form->genElement('title')); $template->setParam('editContent', (string) $form->genElement('content')); $template->setParam('formSubmit', (string) $form->genSubmit('Create entry')); $template->setParam('formEnd',$form->genEnd()); $template->setParam('backlink','zurück'); $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 runUpdateEntry(Request $request, Response $response, $activeInstance) { $system = System::getInstance(); $db = $system->getDB(); $usrSvc = $system->getUserSvc(); $usr = $usrSvc->getUser(); $param = $system->getRequest()->getParam(); if (is_null($usr) || (!$usrSvc->userHasPriv($usr,$system->getSiteId(),$activeInstance,'edit'))) throw new AccessDeniedException('not privileged'); // if not, throw an exception $form = $system->createFormFetcher('\\MCMS\\_\\Modx\\MCMS\\News\\_\\Model\\Entry'); $entry = $form->fetch($request); $dbentry = ReposSvc::getEntry($entry->id); foreach(array('title','content') as $field) $dbentry->$field = $entry->$field; ReposSvc::storeEntry($dbentry); $response->setRedirect($activeInstance,'index'); // redirect the request } /** * Ask if an entry should be deleted */ public function runDelaskEntry(Request $request, Response $response, $activeInstance) { $system = System::getInstance(); $db = $system->getDB(); $usrSvc = $system->getUserSvc(); $usr = $usrSvc->getUser(); $param = $system->getRequest()->getParam(); if (is_null($usr) || (!$usrSvc->userHasPriv($usr,$system->getSiteId(),$activeInstance,'edit'))) throw new AccessDeniedException('not privileged'); // if not, throw an exception $entry = ReposSvc::getEntry((int) $param['id']); try { $layout = $this->findLayout($system->getSiteId(),$activeInstance); $template = $this->loadTemplate($this->getMyId(),'delask',$layout,true); $template->setParam('title','Delete news entry'); $template->setParam('entry_title',$entry->title); $template->setParam('backlink', _SELF_.'?mod='.$activeInstance.'&view=index'); $template->setParam('dellink', _SELF_.'?mod='.$activeInstance.'&view=deleteEntry&id='.$entry->id); $response->setFragment('CONTENT',$template->getDocument()); } catch (FileNotFoundException $ex) { $response->setFragment('CONTENT',"file is missing: ".$tplfile); } } /** * Delete the entry and redirects to the index page */ public function runDeleteEntry(Request $request, Response $response, $activeInstance) { $system = System::getInstance(); $db = $system->getDB(); $usrSvc = $system->getUserSvc(); $usr = $usrSvc->getUser(); $param = $system->getRequest()->getParam(); if (is_null($usr) || (!$usrSvc->userHasPriv($usr,$system->getSiteId(),$activeInstance,'edit'))) throw new AccessDeniedException('not privileged'); // if not, throw an exception $dbentry = ReposSvc::getEntry($param['id']); ReposSvc::deleteEntry($dbentry); $response->setRedirect($activeInstance,'index'); // redirect the request } // views public function runDelaskComment(Request $request, Response $response, $activeInstance) { } public function runDeleteComment(Request $request, Response $response, $activeInstance) { } // Security checks public function checkEntryEdit($context,$param) { $system = System::getInstance(); $usrSvc = $system->getUserSvc(); $usr = $usrSvc->getUser(); $activeInstance = $param[1]; if (!is_null($usr)) { if ($usrSvc->userHasPriv($usr,$system->getSiteId(),$activeInstance,'admin')) return true; if ($context->e->created_by_id == $usr->id && $usrSvc->userHasPriv($usr,$system->getSiteId(),$activeInstance,'editor')) return true; } return false; } public function checkCommentEdit($context,$activeInstance) { } }