IndexAction.php 3.97 KB
Newer Older
Clément Damiens's avatar
 
Clément Damiens committed
1 2 3
 <?php
class IndexAction extends CAction
{
Tibor Katelbach's avatar
Tibor Katelbach committed
4
    public function run($id=null, $answer=null, $mode=null)
Clément Damiens's avatar
 
Clément Damiens committed
5 6
    {
    	$this->getController()->layout = "//layouts/empty";
7
        $params = array();
Clément Damiens's avatar
 
Clément Damiens committed
8
        if(!empty($id)){
Raph El's avatar
Hotfix  
Raph El committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
            if($id == "new"){
                if(!empty(Yii::app()->session["userId"])){
 					$answer = Answer::generateAnswer();
 					$params["answerId"] = $answer["_id"];
 					$mode = "w" ;
				} else {
					if(Yii::app()->request->isAjaxRequest)
		                echo $this->getController()->renderPartial("co2.views.default.unTpl",array("msg"=>Yii::t("common", "You are not allow to access to this answer"),"icon"=>"fa-lock"));
		            else
		                $this->getController()->render("co2.views.default.unTpl",array("msg"=>Yii::t("common", "You are not allow to access to this answer"),"icon"=>"fa-lock"));

		            exit;
				}
                
            } else {
                $params["answerId"]=$id;
                $answer = PHDB::findOne( Form::ANSWER_COLLECTION, array("_id"=>new MongoId($id)));
            }
Raph El's avatar
Raph El committed
27

Raph El's avatar
Raph El committed
28 29 30

            //Rest::json($params); exit;
	        $params = Form::getDataForAnswer($params);
31
      		//Rest::json($params); exit;
Raph El's avatar
Raph El committed
32 33 34 35 36 37 38 39 40 41
	        $params["answer"] = $answer;
	        // TODO améliorer getdataForAnser pour soit viré la création de lanswers soit charger la bonne answer
	       
	        
	        $form = PHDB::findOne( Form::COLLECTION , array("id"=>$answer["formId"]));
	        $parentForm = Slug::getElementBySlug($form["id"]);
	        $canEditForm = false;
	        $canEditAnswer = false;
	        $canSeeAnswer = false;
	        if(!empty(Yii::app()->session['userId'])){
Raph El's avatar
Test  
Raph El committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55
	        	if($mode == "fa"){
	        		$canEditForm = Form::canAdmin(Yii::app()->session['userId'], $form);
		        	if(empty($canEditForm))
		        		$mode = "w";
	        	}
	        	if( $mode == "w" || $mode == "fa" ){
	        		$canEditAnswer = Form::canEditAnswer(Yii::app()->session['userId'], $params["answer"], $form, $parentForm);
	        		if(empty($canEditAnswer))
		        		$mode = "r";
		        	else
		        		$canSeeAnswer = true;
	        	}

	            if(empty($canEditAnswer) && empty($canSeeAnswer)){
Raph El's avatar
Raph El committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
	                $canSeeAnswer = Form::canSeeAnswer(Yii::app()->session['userId'], $params["answer"], $form, $parentForm);
	                if($canSeeAnswer === true && !empty($mode) && ($mode == "w" || $mode == "fa") )
	                    $mode = "r";
	            }
	        }
	        // var_dump($canEditAnswer);
	        // var_dump($canSeeAnswer);
	        if($canEditAnswer === true || $canSeeAnswer === true){
	        	$params["canEditForm"] = $canEditForm;
	            $params["canEdit"] = $canEditAnswer;
	            $params["canSee"] = $canSeeAnswer;
	            $params["mode"] = (!empty($mode) ? $mode : "r");
	            //$tpl=(!empty($tpl)) ? $tpl : "survey.views.tpls.forms.formWizard";
	            $tpl=(!empty($this->getController()->costum["form"]["tpl"])) ? $this->getController()->costum["form"]["tpl"] : "survey.views.tpls.forms.formWizard";
	            echo $this->getController()->renderPartial($tpl,$params );
	        } else {
	            if(Yii::app()->request->isAjaxRequest)
	                echo $this->getController()->renderPartial("co2.views.default.unTpl",array("msg"=>Yii::t("common", "You are not allow to access to this answer"),"icon"=>"fa-lock"));
	            else
	                $this->getController()->render("co2.views.default.unTpl",array("msg"=>Yii::t("common", "You are not allow to access to this answer"),"icon"=>"fa-lock"));
	        }
Raph El's avatar
Raph El committed
77
        } else {
78 79 80 81
            if(Yii::app()->request->isAjaxRequest)
                echo $this->getController()->renderPartial("co2.views.default.unTpl",array("msg"=>Yii::t("common", "You are not allow to access to this answer"),"icon"=>"fa-lock"));
            else
                $this->getController()->render("co2.views.default.unTpl",array("msg"=>Yii::t("common", "You are not allow to access to this answer"),"icon"=>"fa-lock"));
Raph El's avatar
Raph El committed
82 83
        }
        
Raph El's avatar
Raph El committed
84
        
Clément Damiens's avatar
 
Clément Damiens committed
85
    }
Raph El's avatar
Modif  
Raph El committed
86
}