IndexAction.php 4.23 KB
Newer Older
Clément Damiens's avatar
 
Clément Damiens committed
1 2 3
 <?php
class IndexAction extends CAction
{
4
    public function run($id=null, $answer=null, $mode=null, $form=null)
Clément Damiens's avatar
 
Clément Damiens committed
5 6
    {
    	$this->getController()->layout = "//layouts/empty";
7
        $params = array();
8 9
        if(!empty($form))
        	$form = PHDB::findOneById( Form::COLLECTION , $form);
Clément Damiens's avatar
 
Clément Damiens committed
10
        if(!empty($id)){
Raph El's avatar
Hotfix  
Raph El committed
11 12
            if($id == "new"){
                if(!empty(Yii::app()->session["userId"])){
13
 					$answer = Answer::generateAnswer($form);
Raph El's avatar
Hotfix  
Raph El committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27
 					$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)));
28 29 30
                //Rest::json($answer); exit;
                if(empty($form) && !empty($answer["form"]))
        			$form = PHDB::findOneById( Form::COLLECTION , $answer["form"]);
Raph El's avatar
Hotfix  
Raph El committed
31
            }
Raph El's avatar
Raph El committed
32

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

	            if(empty($canEditAnswer) && empty($canSeeAnswer)){
61
	                $canSeeAnswer = Form::canSeeAnswer(Yii::app()->session['userId'], $params["answer"], $form, @$parentForm);
Raph El's avatar
Raph El committed
62 63 64 65 66 67
	                if($canSeeAnswer === true && !empty($mode) && ($mode == "w" || $mode == "fa") )
	                    $mode = "r";
	            }
	        }
	        // var_dump($canEditAnswer);
	        // var_dump($canSeeAnswer);
Raph El's avatar
Raph El committed
68

Raph El's avatar
Raph El committed
69 70 71 72 73 74
	        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";
Raph El's avatar
Raph El committed
75
	            //Rest::json($params); exit;
Tibor Katelbach's avatar
Tibor Katelbach committed
76
	            $tpl=(!empty($this->getController()->costum["form"]["tpl"])) ? $this->getController()->costum["form"]["tpl"] : "survey.views.tpls.forms.formWizard";
Raph El's avatar
Raph El committed
77 78 79 80 81 82 83
	            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
84
        } else {
85 86 87 88
            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
89 90
        }
        
Raph El's avatar
Raph El committed
91
        
Clément Damiens's avatar
 
Clément Damiens committed
92
    }
Raph El's avatar
Modif  
Raph El committed
93
}