Prise en main du projet RT-Cloud et du micro-framework
class Exemples extends \BaseController { public function index() { $services=DAO::getAll("Service"); foreach ($services as $service){ echo $service->getNom()."<br>"; } } }
class Exemples extends \BaseController { ... public function users(){ $users=DAO::getAll("Utilisateur"); $this->loadView("Exemples/users",array("users"=>$users)); } }
<h1>Utilisateurs</h1> <?php foreach ($users as $user){ echo $user->getLogin()."<br>"; }
class Exemples extends \BaseController { ... public function disques(){ $disques=DAO::getAll("Disque"); $this->loadView("Exemples/disques.html",array("disques"=>$disques)); } }
<h1>Disques</h1> {% for disque in disques %} <b>{{ disque.getNom() }}</b> {{ disque.getUtilisateur() }}<br> {% endfor %}
class Exemples extends \BaseController { ... public function sortedUsers($field="login",$order="ASC"){ $fields=["login","mail","tel"]; if(!array_search($field,$fields)) $field=$fields[0]; $header=""; foreach ($fields as $f){ $header.="<th>"; $icon="sort-by-alphabet"; $url="Exemples/sortedUsers/".$f; if($field==$f){ if(strtoupper($order)=="ASC"){ $icon="sort-by-alphabet-alt"; $url.="/DESC"; } $header.="<a href='{$url}'>{$f} <span class='glyphicon glyphicon-{$icon}' aria-hidden='true'></span></a>"; }else{ $header.="<a href='{$url}'>{$f}</a>"; } $header.="</th>"; } $users=DAO::getAll("Utilisateur","1=1 ORDER BY ".$field." ".$order); $this->loadView("Exemples/sortedUsers.html",array("users"=>$users,"header"=>$header)); }
<table class='table table-striped'> <thead> <tr> {{header|raw}} </tr> </thead> <tbody> {% for user in users %} <tr> <td>{{user.getLogin()}}</td> <td>{{user.getMail()}}</td> <td>{{user.getTel()}}</td> </tr> {% endfor %} </tbody> </table>
class Exemples extends \BaseController { ... public function usersDisques(){ $users=DAO::getAll("Utilisateur"); foreach ($users as $user) { DAO::getOneToMany($user, "disques"); if(sizeof($user->getDisques())>0) $this->loadView("exemples/userDisques.html",array("user"=>$user)); } } }
<h4>{{user}}</h4> <ul class="list-group"> {% for disque in user.getDisques() %} <li class="list-group-item">{{disque}}</li> {% endfor %} </ul>
class Exemples extends \BaseController { ... public function displayService($id=null){ $edit=false; $service=null; if(isset($id)){ $service=DAO::getOne("Service", $id); $edit=isset($service); } if(!$edit) $service=new Service(); $this->loadView("exemples/displayService.html",array("service"=>$service)); } }
<div class="panel panel-default"> <div class="panel-heading"> <b>Service</b> </div> <div class="panel-body"><b>{{service.getNom()}}</b></div> <ul class="list-group"> <li class="list-group-item"> <h5>Description : </h5> <span>{{service.getDescription()}}</span> </li> <li class="list-group-item"> <b>Prix : </b> {{service.getPrix()}} </li> </ul> </div>
class Exemples extends \BaseController { ... public function serviceAdd($nom,$prix=0){ $service=new Service(); $service->setNom($nom); $service->setPrix($prix); DAO::insert($service); $this->forward("Exemples","displayService",$service->getId()); } }
<div class="panel panel-default"> <div class="panel-heading"> <b>Service</b> </div> <div class="panel-body"><b>{{service.getNom()}}</b></div> <ul class="list-group"> <li class="list-group-item"> <h5>Description : </h5> <span>{{service.getDescription()}}</span> </li> <li class="list-group-item"> <b>Prix : </b> {{service.getPrix()}} {%if edit %} <a class="btn btn-primary" href="exemples/updatePrix/{{service.getId()}}/1">Augmenter</a>{% endif %} </li> </ul> </div>
class Exemples extends \BaseController { ... public function updatePrix($idService,$update=1){ $service=DAO::getOne("Service", $idService); if(isset($service)){ $service->setPrix($service->getPrix()+$update); DAO::update($service); $this->forward("Exemples","displayService",$service->getId()); }else{ $this->_showMessage("Impossible de charger le service","warning"); } } }
<div class="panel panel-default"> <div class="panel-heading"> <b>Service</b> </div> <div class="panel-body"><b>{{service.getNom()}}</b></div> <ul class="list-group"> <li class="list-group-item"> <h5>Description : </h5> <span>{{service.getDescription()}}</span> </li> <li class="list-group-item"> <b>Prix : </b> {{service.getPrix()}} {%if edit %} <a class="btn btn-primary" href="exemples/updatePrix/{{service.getId()}}/1">Augmenter prix</a> <a class="btn btn-warning" href="exemples/deleteService/{{service.getId()}}/">Supprimer service</a> {% endif %} </li> </ul> </div>
class Exemples extends \BaseController { ... public function deleteService($idService){ $service=DAO::getOne("Service", $idService); if(isset($service)){ DAO::delete($service); $this->_showMessage($service." supprimé","success"); }else{ $this->_showMessage("Impossible de charger le service","warning"); } } }
Accéder à l'url /services ou /services/index pour visualiser le résultat
class Services extends \_DefaultController { public function __construct(){ parent::__construct(); $this->title="Services"; $this->model="Service"; } }
public function frm($id=NULL){ $service=$this->getInstance($id); $this->loadView("exemples/frmService.html",array("service"=>$service)); }
<form action="services/update" method="post"> <fieldset> <legend>Service</legend> <div class="form-group"> <input type="hidden" name="id" id="id" value="{{service.getId()}}"> <input placeholder="Entrez un nom" type="text" class="form-control" name="nom" id="nom" value="{{service.getNom()}}"> <input placeholder="Enrez un prix" type="number" class="form-control" name="prix" id="prix" value="{{service.getPrix()}}"> <textarea rows="5" class="form-control" name="description" id="description">{{service.getDescription()}}</textarea> </div> <div class="form-group"> <button class="btn btn-primary">Valider</button> </div> </fieldset> </form>
<div class="button-group"> <a class="btn btn-primary disques" href="exemples/disques" data-ajax="exemples/disques">Disques</a> <a class="btn btn-primary users" href="exemples/users" data-ajax="exemples/users">Utilisateurs</a> </div> <div class="alert alert-info" id="message"></div> <div id="response"></div>
public function ajaxTest(){ $this->loadView("exemples/ajaxTest.html"); Jquery::getOn("click", "a[data-ajax]", "","#response",array("attr"=>"data-ajax")); Jquery::doJqueryOn(".disques", "mouseenter", "#message", "html","Affiche les disques existants"); Jquery::doJqueryOn(".users", "mouseenter", "#message", "html","Affiche les utilisateurs existants"); Jquery::doJqueryOn(".btn", "mouseout", "#message", "html"); echo Jquery::compile(); }