--- /dev/null
+<?php
+
+
+class PatchController extends ApplicationController {
+
+ /** @var PdoPatchManager */
+ private $pdoPatchManager;
+
+ private function getPdoPatchManager(){
+ if (! $this->pdoPatchManager){
+ $this->pdoPatchManager = new PdoPatchManager();
+ }
+ return $this->pdoPatchManager;
+ }
+
+ /**
+ * @api {post} /index.php?action=patch_edit Edite un patch
+ * @apiGroup Patch
+ * @apiVersion 1.0.0
+ * @apiParam {Int} id identifiant du patch (0 pour créer)
+ * @apiParam {Int} version_id_from Identifiant de la version dans laquelle est l'application
+ * @apiParam {Int} version_id_to Identifiant de la version cible
+ * @apiParam {File} script_content Contenu du script à executer sur l'agent
+ *
+ * @apiSuccessExample {json} Success-Reponse:
+ * {"retour":"OK","message":"","result":patch_id}
+ */
+ public function editAction(){
+ $patch = new Patch();
+ $patch->id = $this->getPostParameters()->getInt('id');
+
+ $patch->version_id_from = $this->getPostParameters()->getInt('version_id_from');
+ $patch->version_id_to = $this->getPostParameters()->getInt('version_id_to');
+ $patch->script_name = $this->getPostParameters()->get('script_name');
+
+ $pdoVersionManager = new PdoVersionManager();
+ $version_info_from = $pdoVersionManager->getInfo($patch->version_id_from );
+ if (! $version_info_from){
+ throw new Exception("Cette version_id (from) est inconnue sur le serveur");
+ }
+ $version_info_to = $pdoVersionManager->getInfo($patch->version_id_to );
+ if (! $version_info_to){
+ throw new Exception("Cette version_id (to) est inconnue sur le serveur");
+ }
+ if ($version_info_from['product'] != $version_info_to['product']){
+ throw new Exception("Ces versions ne concernent pas le même produit !");
+ }
+
+ if ($this->getPdoPatchManager()->patchExists($patch->version_id_from,$patch->version_id_to)){
+ throw new Exception("Un patch existe déjà pour ce couple de versions.");
+ }
+
+ if (empty($_FILES['script_content']['tmp_name'])){
+ $patch->script_content = '';
+ } else {
+ $patch->script_content = file_get_contents($_FILES['script_content']['tmp_name']);
+ }
+
+ return $this->getPdoPatchManager()->edit($patch);
+ }
+
+ /**
+ * @api {get} /index.php?action=patch_delete Supprime un patch
+ * @apiGroup Patch
+ * @apiVersion 1.0.0
+ * @apiParam {Int} id identifiant du patch
+ *
+ * @apiSuccessExample {json} Success-Reponse:
+ * {"retour":"OK","message":"","result":true}
+ */
+ public function deleteAction(){
+ $id = $this->getParameters()->getInt('id');
+ $this->getPdoPatchManager()->delete($id);
+ return true;
+ }
+
+ /**
+ * @api {get} /index.php?action=patch_getPatch Renvoi le contenu d'un patch
+ * @apiGroup Patch
+ * @apiVersion 1.0.0
+ * @apiParam {Int} id identifiant du patch
+ *
+ * @apiSuccessExample {json} Success-Reponse:
+ * le contenu du patch (text/plain)
+ */
+ public function getPatchAction(){
+ $id = $this->getParameters()->getInt('id');
+ $result = $this->getPdoPatchManager()->getInfo($id);
+
+ header("Content-type: text/plain");
+ echo $result['script_content'];
+ exit;
+ }
+
+ /**
+ * @api {get} /index.php?action=patch_listByVersionIdTo
+ * @apiGroup Patch
+ * @apiVersion 1.0.0
+ * @apiParam {Int} version_id_to identifiant de la version cible
+ *
+ * @apiSuccessExample {json} Success-Reponse:
+ * {"retour":"OK","message":"","result":[{"id":"1","version_id_from":"1","version_id_to":"1"}]}
+ */
+ public function listByVersionIdToAction(){
+ $version_id_to = $this->getParameters()->getInt('version_id_to');
+ return $this->getPdoPatchManager()->getListByVersionTo($version_id_to);
+ }
+
+}
\ No newline at end of file