PATH:
home
/
letacommog
/
letaweb
/
protected
/
modules
/
helpSystem
/
modules
/
admin
/
controllers
<?php /** * TopicController class file. * @author Digital Mesh <info@digitalmesh.com> * @copyright Copyright © Digital Mesh 2013- * @license http://www.opensource.org/licenses/bsd-license.php New BSD License * @package HelpSystem */ /** * TopicController Class is used to mange the CURD ot topic */ class TopicController extends DMController{ /** * function to initialise the controller */ public function init(){ parent::init(); $this->setLayout(); } /** * set the layout for our controller */ private function setLayout(){ $this->layout= Yii::app()->getModule('helpSystem')->pageLayout; } /** * @return array action filters */ public function filters() { return array( 'accessControl', // perform access control for CRUD operations ); } /** * Specifies the access control rules. * This method is used by the 'accessControl' filter. * @return array access control rules */ public function accessRules() { $rules = array(); $actions = array('index','create','update', 'delete', 'getSectionTopics','reorderSection', 'reorderTopic','treeviewData','ajaxTopicTagList'); $rules = AccessRules::getRuleArray($actions); return $rules; } /** * List all topics */ public function actionIndex(){ $bootstrapClass = new BootstrapClassFilter(); $offset = Yii::app()->request->getParam("page",0); $sectionId = Yii::app()->request->getParam("sectionId",""); $sectionList = $this->getSectionList(); $topicCount = $this->getTopicCount($sectionId); $pages=new CPagination($topicCount); // results per page $pages->pageSize=$this->module->topicPageLimit; $pages->params = array("sectionId"=>$sectionId); $criteria = new CDbCriteria(); if($sectionId){ $criteria->condition = "SectionId = :sectionId"; $criteria->params = array(":sectionId"=>$sectionId); } $criteria->order = 't.Order ASC'; $pages->applyLimit($criteria); $topicList = Topic::model()->findAll($criteria); $this->render("index",array( 'bootstrapClass'=>$bootstrapClass, 'topicList'=>$topicList, 'sectionList'=>$sectionList, 'sectionId'=>$sectionId, 'pages'=>$pages, 'offset'=>$offset )); } /** * Get the entire topic list as array * @param Integer $offset * @param Integer $sectionId * @return topicList array */ private function getTopicList($offset,$sectionId){ $criteria = new CDbCriteria(); if($sectionId){ $criteria->condition = "SectionId = :sectionId"; $criteria->params = array(":sectionId"=>$sectionId); } $criteria->order = 't.Order ASC'; $criteria->limit = $this->module->topicPageLimit; $criteria->offset = $offset; $topicList = Topic::model()->findAll($criteria); return $topicList; } /** * Get the total record Count * @param Integer $sectionId * @return count integer */ private function getTopicCount($sectionId){ $criteria = new CDbCriteria(); if($sectionId){ $criteria->condition = "SectionId = :sectionId"; $criteria->params = array(":sectionId"=>$sectionId); } $count = Topic::model()->count($criteria); return $count; } /** * Creates a new model. * If creation is successful, the browser will be redirected to the 'index' page. */ public function actionCreate(){ $bootstrapClass = New BootstrapClassFilter(); $topicForm = New TopicForm(); $sectionTextModel=new SectionText(); $sectionList = $this->getSectionList(); $sectionId = Yii::app()->request->getParam("sectionId",""); $topicList = array(); $editTopic = 0; if(Yii::app()->request->isPostRequest) { $request = Yii::app()->request; $form = $request->getPost('TopicForm'); $topicForm->body = trim($form['body']); $topicForm->title = trim($form['title']); $topicForm->parent = $form['parent']; $topicForm->section = $form['section']; $topicForm->sectionText = trim($form['sectionText']); $topicModel = new Topic(); $textModel = new TopicText(); if($topicForm->validate()){ if(DataManagement::createTopic($topicForm,$topicModel,$textModel, $sectionTextModel,$topicForm->sectionText)){ $this->redirect(array("index", "sectionId"=>$sectionId)); } } } $topicForm->section = $sectionId; $this->render("create",array( 'bootstrapClass'=>$bootstrapClass, 'sectionList'=>$sectionList, 'sectionId'=>$sectionId, 'topicList'=>$topicList, 'topicForm'=>$topicForm, 'sectionTextModel'=>$sectionTextModel, 'parentId'=>'', 'editTopic'=>$editTopic )); } /** * Updates a particular model. * If update is successful, the browser will be reload the page. * @param integer $id the ID of the model to be updated */ public function actionUpdate(){ $id = Yii::app()->request->getParam("id"); $pageNo = Yii::app()->request->getParam("pno"); $bootstrapClass = New BootstrapClassFilter(); $topicForm = New TopicForm(); $topicModel = $this->loadModel($id); $topicList = array(); $editTopic = 1; $topicForm->section = $topicModel->section->sectionTexts[0]->Name; $topicForm->title = $topicModel->TopicTexts[0]->Title; $topicForm->body = $topicModel->TopicTexts[0]->Body; if(!empty($topicModel->parent)){ $topicForm->parent = $topicModel->parent->TopicTexts[0]->Title; } else{ $topicForm->parent = "No Parent"; } $topicTextId = $topicModel->TopicTexts[0]->TopicTextId; if(Yii::app()->request->isPostRequest){ $request = Yii::app()->request; $form = $request->getPost('TopicForm'); $topicForm->body = trim($form['body']); $topicForm->title = trim($form['title']); if($topicForm->validate()){ $textModel = TopicText::model()->findByPk($topicTextId); if(DataManagement::updateTopic($topicForm,$textModel)){ $this->redirect($this->createUrl('index', array('page'=>$pageNo,'sectionId'=>$topicModel->SectionId))); } } } $this->render("create",array( 'bootstrapClass'=>$bootstrapClass, 'topicList'=>$topicList, 'topicForm'=>$topicForm, 'editTopic'=>$editTopic )); } /** * displays the parent of particular topic or "No Parent" of there is no parent topic on ajax call. */ public function actionGetSectionTopics() { $form = Yii::app()->request->getParam("TopicForm"); $sectionId = $form['section']; if($sectionId) { $topicList = $this->getParentChildTopics($sectionId); if(!empty($topicList)) { echo CHtml::tag('option',array('value'=>0),CHtml::encode("No Parent"),true); foreach($topicList as $eachTopic) { echo CHtml::tag('option',array('value'=>$eachTopic['tid']),CHtml::encode($eachTopic['label']),true); } } else { echo CHtml::tag('option',array('value'=>0),CHtml::encode("No Parent"),true); } } } /** * Returns an array of Topics of a particular Section * @param integer $sectionId * @return children array */ private function getParentChildTopics($sectionId) { $topicList = $this->getTopicOrderList($sectionId); $children = array(); if(!empty($topicList)) { foreach ($topicList as $topic) { $children[] = array('label'=>$topic->TopicTexts[0]->Title,'tid'=>$topic->TopicId); if(!empty($topic->Topics)) { foreach($topic->Topics as $child) { $children[] = array('label'=>$child->TopicTexts[0]->Title,'tid'=>$child->TopicId); } } } } return $children; } /** * Deletes a particular model. * If deletion is successful, the browser will be redirected to the 'topic' page. * @param integer $id the ID of the model to be deleted * @throws CHttpException */ public function actionDelete($id) { // start transaction $transaction = Yii::app()->db->beginTransaction(); try{ if(Yii::app()->request->isPostRequest){ $this->deleteTopic($id); $transaction->commit(); $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index')); } }catch (Exception $e ) { $transaction->rollback(); throw new CHttpException($e); } } /** * deleteTopic is a private function * Here we written a recursive function * to get delete all nested topics * @param Integer $id */ private function deleteTopic($id){ // get child topics $topicChildModel = Topic::model()->findAll('ParentId =:id',array(':id'=>$id)); if(!empty($topicChildModel)){ foreach ($topicChildModel as $topicChild){ $this->deleteTopic($topicChild->TopicId); } } $topicText = TopicText::model()->deleteAll( 'TopicId =:id',array(':id'=>$id)); $topicModel = $this->loadModel($id)->delete(); } /** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer the ID of the model to be loaded * @throws CHttpException * @return $model Object */ public function loadModel($id) { $model = Topic::model()->findByPk($id); if($model===null){ throw new CHttpException(404,'The requested page does not exist.'); } return $model; } /** * The browser will be redirected to ReOrderTopic page. */ public function actionReorderSection(){ $topicTree = array(); $sectionList = $this->getSectionList(); $sectionId = Yii::app()->request->getParam("sectionId",""); $bootstrapClass = new BootstrapClassFilter(); if(Yii::app()->request->isPostRequest){ $sectionId = Yii::app()->request->getPost("sectionId",""); $topicList = $this->getTopicOrderList($sectionId); $topicTree = $this->getTopicTree($topicList); } $this->render("reorderSection",array( 'topicTree'=>$topicTree, 'sectionList'=>$sectionList, 'sectionId'=>$sectionId, 'bootstrapClass'=>$bootstrapClass )); } /** * To Reorder the Topics as per the changes made in jstree */ public function actionReorderTopic(){ $result = new stdClass(); $idList = Yii::app()->request->getParam("idList"); $ref = Yii::app()->request->getParam("ref"); $parentId=(($ref)?$ref:NULL); $sectionId = Yii::app()->request->getParam("sectionId"); if(!empty($idList)) { $idList = explode(",", $idList); foreach($idList as $key => $val) { $this->updateOrder($key,$val,$parentId); } } Yii::app()->end(); } /** * Update the order and parent of a Topic model * @param integer $order * @param integer $topicId * @param integer $parentId */ private function updateOrder($order,$topicId,$parentId) { $model = $this->loadModel($topicId); $model->Order = $order; $model->ParentId = $parentId; $model->save(); } /** * Display Topics and its child Topics of a particular Section * @param integer $sectionId * @param integer $id */ public function actionTreeviewData($sectionId,$id){ $topicList = $this->getTopicOrderList($sectionId,$id); $topicTree = $this->getTopicTree($topicList); print CJSON::encode($topicTree); Yii::app()->end(); } /** * Returns all Ordered Topic List as per Parent Topic or Section. * @param integer $sectionId * @param integer $id * @return $result array */ private function getTopicOrderList($sectionId,$id=0){ $criteria = new CDbCriteria(); if($id){ $criteria->condition = "t.ParentId =:id AND t.SectionId =:sectionId "; $criteria->params = array(":id"=>$id,":sectionId"=>$sectionId); }else{ $criteria->condition = "t.ParentId IS NULL AND t.SectionId =:sectionId "; $criteria->params = array(":sectionId"=>$sectionId); } $criteria->with = array("Topics"); $criteria->order = "t.Order ASC,Topics.Order ASC"; $result = Topic::model()->findAll($criteria); return $result; } /** * Contructs a jstree in Topic Order * @param array $topicList * @return $topicTree array */ private function getTopicTree($topicList){ $topicTree = array(); if(!empty($topicList)){ foreach ($topicList as $topic){ $children = ''; if(!empty($topic->Topics)){ foreach($topic->Topics as $child){ $children[]= array( 'attr'=>array("id"=>$child->TopicId,"rel"=>"default"), 'data' => $child->TopicTexts[0]->Title, 'tid'=>$child->TopicId, 'pid'=>$child->ParentId, 'state'=>'closed', 'children'=>$this->createChildTree($child) ); } } $topicTree[] = array( 'attr'=>array("id"=>$topic->TopicId,"rel"=>"default"), 'data'=>$topic->TopicTexts[0]->Title, 'state'=>'closed', 'tid'=> $topic->TopicId,'children'=>$children,'pid'=>$topic->ParentId); } } return $topicTree; } /** * Contructs child jstree in Topic Order * @param array $topic * @return $children array */ private function createChildTree($topic){ $children = ''; foreach($topic->Topics as $child){ $children[$child->Order]= array( 'attr'=>array("id"=>$child->TopicId,"rel"=>"default"), 'data' => $child->TopicTexts[0]->Title, 'tid'=>$child->TopicId, 'pid'=>$child->ParentId, 'state'=>'closed', 'children'=>$this->createChildTree($child) ); } return $children; } /** * Returns a list of all Section name * @return $sectionList array */ private function getSectionList(){ $sections = Section::model()->findAll(); $sectionList = array(); if(!empty($sections)){ foreach ($sections as $section){ $sectionList[$section->SectionId] = CHtml::encode($section->sectionTexts[0]->Name); } } return $sectionList; } /** * To get a topic list of a specific section on ajax call. * */ public function actionAjaxTopicTagList(){ $sectionId = Yii::app()->request->getParam("sectionId",""); echo CHtml::tag('option', array('value'=>""),CHtml::encode("Please select"),true); if($sectionId){ $criteria = new CDbCriteria(); $criteria->condition = "t.SectionId = :sectionId"; $criteria->params = array(":sectionId"=>$sectionId); $criteria->order = 't.Order'; $topics = Topic::model()->findAll($criteria); if(!empty($topics)){ foreach ($topics as $topic){ echo CHtml::tag('option', array('value'=>$topic->TopicId),CHtml::encode($topic->TopicTexts[0]->Title),true); } } } Yii::app()->end(); } }
[+]
..
[-] SectionController.php
[edit]
[-] TopicController.php
[edit]