financement.php 11.7 KB
Newer Older
Clément Damiens's avatar
 
Clément Damiens committed
1 2 3 4 5 6 7 8
<?php if($answer){ 
	?>
<div class="form-group">
	<table class="table table-bordered table-hover  directoryTable" id="<?php echo $kunik?>">
		
	<?php 
		$editBtnL = (Yii::app()->session["userId"] == $answer["user"]) ? " <a href='javascript:;' data-id='".$answer["_id"]."' data-collection='".Form::ANSWER_COLLECTION."' data-path='".$answerPath."' class='add".$kunik." btn btn-default'><i class='fa fa-plus'></i> Ajouter une ligne </a>" : "";
		
9
		$editParamsBtn = ($canEdit) ? " <a href='javascript:;' data-id='".$el["_id"]."' data-collection='".$this->costum["contextType"]."' data-path='costum.form.params.".$kunik."' class='previewTpl edit".$kunik."Params btn btn-xs btn-danger'><i class='fa fa-cog'></i> </a>" : "";
Clément Damiens's avatar
 
Clément Damiens committed
10

11
		$communityLinks = Element::getCommunityByTypeAndId($this->costum["contextType"],$this->costum["contextId"]);
Clément Damiens's avatar
 
Clément Damiens committed
12 13 14 15 16
		$persons = Link::groupFindByType( Person::COLLECTION,$communityLinks,["name","links"] );
		$organizations = Link::groupFindByType( Organization::COLLECTION,$communityLinks,["name","links"] );
		$financers = array_merge( $persons, $organizations );

		$orgs = [];
17
		if( !empty($or["links"]["memberOf"][$this->costum["contextId"]]["roles"]) ) {
Clément Damiens's avatar
 
Clément Damiens committed
18
			foreach ($financers as $id => $or) {
19 20
				$roles = $or["links"]["memberOf"][$this->costum["contextId"]]["roles"];
				if( isset($this->costum["form"]["params"][$kunik]["limitRoles"]) && !empty($roles))
Clément Damiens's avatar
 
Clément Damiens committed
21 22
				{
					foreach ($roles as $i => $r) {
23
						if( in_array($r, $this->costum["form"]["params"][$kunik]["limitRoles"]) )
Clément Damiens's avatar
 
Clément Damiens committed
24 25 26 27 28 29 30 31 32 33 34 35 36
							$orgs[$id] = $or["name"];
					}		
				}
			}
		}
		//var_dump($orgs);exit;
		$listLabels = array_merge(Ctenat::$financerTypeList,$orgs);

		$paramsData = [ 
			"financerTypeList" => Ctenat::$financerTypeList,
			"limitRoles" =>["Financeur"]
		];

37 38 39 40 41 42 43
		if( isset($this->costum["form"]["params"][$kunik]) ) {
			if( isset($this->costum["form"]["params"][$kunik]["tpl"]) ) 
				$paramsData["tpl"] =  $this->costum["form"]["params"][$kunik]["tpl"];
			if( isset($this->costum["form"]["params"][$kunik]["financerTypeList"]) ) 
				$paramsData["financerTypeList"] =  $this->costum["form"]["params"][$kunik]["financerTypeList"];
			if( isset($this->costum["form"]["params"][$kunik]["limitRoles"]) ) 
				$paramsData["financersList"] =  $this->costum["form"]["params"][$kunik]["limitRoles"];
Clément Damiens's avatar
 
Clément Damiens committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
		}

		$properties = [
              "financerType" => [
                "placeholder" => "Type de Financement",
                    "inputType" => "select",
                    "list" => "financerTypeList",
                    "rules" => [
                        "required" => true
                    ]
                ],
                "financer" => [
                    "placeholder" => "Quel Financeur",
                    "inputType" => "select",
                    "list" => "financersList",
                    "subLabel" => "Si financeur public, l’inviter dans la liste ci-dessous (au cas où il n’apparait pas demandez à votre référent territoire de le déclarer comme partenaire financeur"
                ],
                "title" => [
                    "inputType" => "text",
                    "label" => "Fonds, enveloppe ou budget mobilisé",
                    "placeholder" => "Fonds, enveloppe ou budget mobilisé",
                    "subLabel" => "préciser l’intitulé et la nature du financement",
                    "rules" => [ "required" => true ]
                ],
                "amount2019" => [
                    "inputType" => "text",
                    "label" => "2019 (euros HT)",
                    "placeholder" => "2019 (euros HT)",
                    "rules" => [ "required" => true,"number" => true ]
                ],
                "amount2020" => [
                    "inputType" => "text",
                    "label" => "2020 (euros HT)",
                    "placeholder" => "2020 (euros HT)",
                    "rules" => [ "required" => true,"number" => true  ]
                ],
                "amount2021" => [
                    "inputType" => "text",
                    "label" => "2021 (euros HT)",
                    "placeholder" => "2021 (euros HT)",
                    "rules" => [ "number" => true  ]
                ],
                "amount2022" => [
                    "inputType" => "text",
                    "label" => "2022 (euros HT)",
                    "placeholder" => "2022 (euros HT)",
                    "rules" => [ "number" => true  ]
                ]
        ];

	?>	
	<thead>
		<tr>
			<td colspan='<?php echo count( $properties)+2?>' ><h4 style="color:<?php echo ($titleColor) ? $titleColor : "black"; ?>"><?php echo $label.$editQuestionBtn.$editParamsBtn.$editBtnL?></h4>
				<?php echo $info ?>
			</td>
		</tr>	
		<?php if(isset($answer["answers"][$kunik]) && count($answer["answers"][$kunik])>0){ ?>
		<tr>
			</th>
			<?php 
			
			foreach ($properties as $i => $inp) {
				echo "<th>".$inp["placeholder"]."</th>";
			} ?>
			<th></th>
		</tr>
		<?php } ?>
	</thead>
	<tbody class="directoryLines">	
		<?php 
		$ct = 0;
		
		if(isset($answer["answers"][$kunik])){
			foreach ($answer["answers"][$kunik] as $q => $a) {

				echo "<tr id='".$kunik.$q."' class='".$kunik."Line'>";
				foreach ($properties as $i => $inp) {
					echo "<td>";
					if(isset($a[$i])) {
						if(is_array($a[$i]))
							echo implode(",", $a["role"]);
						else
							echo $a[$i];
					}
					echo "</td>";
				}
					
			?>
			<td>
				<?php 
					$this->renderPartial( "survey.views.tpls.forms.cplx.editDeleteLineBtn" , [
						"canEdit"=>($canEdit||Yii::app()->session["userId"] == $answer["user"]),
						"id" => $answer["_id"],
						"collection" => Form::ANSWER_COLLECTION,
						"q" => $q,
						"path" => "answers.".$kunik.".".$q,
						"kunik"=>$kunik
						] );
					?>
				<a href="javascript:;" class="btn btn-xs btn-primary openAnswersComment" onclick="commentObj.openPreview('answers','<?php echo $answer["_id"]?>','<?php echo $answer["_id"].$key.$q ?>', '<?php echo @$a['step'] ?>')"><?php echo PHDB::count(Comment::COLLECTION, array("contextId"=>$answer["_id"],"contextType"=>"answers", "path"=>$answer["_id"].$key.$q))?> <i class='fa fa-commenting'></i></a>
			</td>
			<?php 
				$ct++;
				echo "</tr>";
			}
		}

$totalMap = [];
foreach ( $properties as $i => $inp ) {
	if(stripos($i, "amount") !== false )
		$totalMap[$i] = 0;
}

if(isset($answer["answers"][$kunik])){
	foreach ( $answer["answers"][$kunik] as $q => $a ) {	
		foreach ($totalMap as $i => $tot) {
			if(isset($a[$i]))
				$totalMap[$i] = $tot + $a[$i];
		}
	}
}

$total = 0;
foreach ( $totalMap as $i => $tot ) {
	if( $tot != 0 )
		$total = $total + $tot ;
}

if($total > 0){

	echo "<tr class='bold'>";
		echo "<td></td>";
		echo "<td>TOTAL : </td>";
		foreach ($totalMap as $i => $tot) {
			if( $tot != 0 )
				echo "<td>".(($tot == 0) ? "" : trim(strrev(chunk_split(strrev($tot),3, ' ')))."€")."</td>";
		}
		echo "<td></td>";
	echo "</tr>";

	echo "<tr class='bold'>";
Raph El's avatar
Raph El committed
186 187
	echo 	"<td colspan='5' style='text-align:right'>FINANCEMENT TOTAL : </td>";
	echo 	"<td colspan='2'>".trim(strrev(chunk_split(strrev($total),3, ' ')))." €</td>";
Clément Damiens's avatar
 
Clément Damiens committed
188 189 190 191 192 193 194 195 196 197 198 199
	echo "</tr>";

}

	

?>
		</tbody>
	</table>
</div>

<?php 
200 201 202
if( isset($this->costum["form"]["params"]["financement"]["tpl"])){
	//if( $this->costum["form"]["params"]["financement"]["tpl"] == "tpls.forms.equibudget" )
		$this->renderPartial( "costum.views.".$this->costum["form"]["params"]["financement"]["tpl"] , 
Clément Damiens's avatar
 
Clément Damiens committed
203 204 205
		[ "totalFin"   => $total,
		  "totalBudg" => Yii::app()->session["totalBudget"]["totalBudget"] ] );
	// else 
206
	// 	$this->renderPartial( "costum.views.".$this->costum["form"]["params"]["financement"]["tpl"]);
Clément Damiens's avatar
 
Clément Damiens committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
}
 ?>

<script type="text/javascript">
if(typeof costum.lists == "undefined")
	costum.lists = {};
costum.lists.financerTypeList = <?php echo json_encode(Ctenat::$financerTypeList); ?>;
costum.lists.financersList = <?php echo json_encode($orgs); ?>;

var <?php echo $kunik ?>Data = <?php echo json_encode( (isset($answer["answers"][$kunik])) ? $answer["answers"][$kunik] : null ); ?>;
sectionDyf.<?php echo $kunik ?>ParamsData = <?php echo json_encode( $paramsData ); ?>;

$(document).ready(function() { 
	
	sectionDyf.<?php echo $kunik ?> = {
		"jsonSchema" : {	
	        "title" : "Plan de Financement",
            "icon" : "fa-money",
            "text" : "Décrire ici les financements mobilisés ou à mobiliser. Les coûts doivent être en <b>hors taxe</b>.",
	        "properties" : <?php echo json_encode( $properties ); ?>,
	        save : function () {  
	            tplCtx.value = {};
	            $.each( sectionDyf.<?php echo $kunik ?>.jsonSchema.properties , function(k,val) { 
	        		tplCtx.value[k] = $("#"+k).val();
	        	 });
	            mylog.log("save tplCtx",tplCtx);
	            if(typeof tplCtx.value == "undefined")
	            	toastr.error('value cannot be empty!');
	            else {
	                dataHelper.path2Value( tplCtx, function(params) { 
237
	                    urlCtrl.loadByHash(location.hash);
Clément Damiens's avatar
 
Clément Damiens committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
	                } );
	            }

	    	}
	    }
	};

	sectionDyf.<?php echo $kunik ?>Params = {
		"jsonSchema" : {	
	        "title" : "<?php echo $kunik ?> config",
	        "icon" : "fa-cog",
	        "properties" : {
	            financerTypeList : {
	                inputType : "properties",
	                labelKey : "Clef",
	                labelValue : "Label affiché",
	                label : "Liste des type de Fiannceurs",
	                values :  sectionDyf.<?php echo $kunik ?>ParamsData.financerTypeList
	            } , 
	            limitRoles : {
	                inputType : "array",
	                label : "Liste des roles financeurs",
	                values :  sectionDyf.<?php echo $kunik ?>ParamsData.limitRoles
	            },
	            tpl : {
	                label : "Sub Template",
	                values :  sectionDyf.<?php echo $kunik ?>ParamsData.tpl
	            }
	        },
	        save : function () {  
	            tplCtx.value = {};
	            $.each( sectionDyf.<?php echo $kunik ?>Params.jsonSchema.properties , function(k,val) { 
	        		if(val.inputType == "array")
	        		 	tplCtx.value[k] = getArray('.'+k+val.inputType);
	        		else if(val.inputType == "properties")
	        		 	tplCtx.value[k] = getPairsObj('.'+k+val.inputType);
	        		else
	        		 	tplCtx.value[k] = $("#"+k).val();
	        	 });
	            mylog.log("save tplCtx",tplCtx);
	            
	            if(typeof tplCtx.value == "undefined")
	            	toastr.error('value cannot be empty!');
	            else {
	                dataHelper.path2Value( tplCtx, function(params) { 
283
	                    urlCtrl.loadByHash(location.hash);
Clément Damiens's avatar
 
Clément Damiens committed
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
	                } );
	            }

	    	}
	    }
	};


    mylog.log("render","/modules/costum/views/tpls/forms/<?php echo $kunik ?>.php");

    //adds a line into answer
    $(".add<?php echo $kunik ?>").off().on("click",function() {  
        tplCtx.id = $(this).data("id");
        tplCtx.collection = $(this).data("collection");            
        tplCtx.path = $(this).data("path")+((notNull(<?php echo $kunik ?>Data) ? <?php echo $kunik ?>Data.length : "0"));
        dyFObj.openForm( sectionDyf.<?php echo $kunik ?> );
    });

    $(".edit<?php echo $kunik ?>").off().on("click",function() {  
        tplCtx.id = $(this).data("id");
        tplCtx.collection = $(this).data("collection"); 
        tplCtx.path = $(this).data("path");
        dyFObj.openForm( sectionDyf.<?php echo $kunik ?>,null, <?php echo $kunik ?>Data[$(this).data("key")]);
    });

    $(".edit<?php echo $kunik ?>Params").off().on("click",function() {  
        tplCtx.id = $(this).data("id");
        tplCtx.collection = $(this).data("collection");
        tplCtx.path = $(this).data("path");
        dyFObj.openForm( sectionDyf.<?php echo $kunik ?>Params,null, sectionDyf.<?php echo $kunik ?>ParamsData);
    });

    
});
</script>
<?php } else {
	//echo "<h4 class='text-red'>CALENDAR works with existing answers</h4>";
} ?>