--- /dev/null
+<?php
+
+class AppliancesController extends ApplicationController {
+
+ /** @var PdoApplianceManager */
+ private $pdoApplianceManager;
+
+ private function getPdoApplianceManager(){
+ if (! $this->pdoApplianceManager){
+ $this->pdoApplianceManager = new PdoApplianceManager();
+ }
+ return $this->pdoApplianceManager;
+ }
+
+ /**
+ * @api {get} /index.php?action=appliances_nb Récupération du nombre d'instance
+ * @apiGroup Instances
+ * @apiVersion 1.0.0
+ *
+ *
+ * @apiSuccessExample {json} Success-Reponse:
+ * {"retour":"OK","message":"","result":"42"}
+ */
+ public function nbAction(){
+ return $this->getPdoApplianceManager()->getNbAppliances();
+ }
+
+ /**
+ * @api {get} /index.php?action=appliances_list Récupération de la liste des instances
+ * @apiGroup Instances
+ * @apiVersion 1.0.0
+ *
+ *
+ * @apiSuccessExample {json} Success-Reponse:
+ * en cours d'élaboration
+ */
+ public function listAction(){
+ $offset = $this->getOffsetParameter();
+ $limit = $this->getLimitParameter();
+ $appliances = $this->getPdoApplianceManager()->getApplianceForOutput($offset,$limit);
+ return $appliances;
+ }
+
+ /**
+ * @api {get} /index.php?action=appliances_create Crée un instance
+ * @apiGroup Instances
+ * @apiVersion 1.0.0
+ * @apiParam {String} contract_number Identifiant AP du contrat
+ * @apiParam {String} fqdn FQDN interne de l'instance
+ * @apiParam {String} channel_key Canal de l'instance
+ * @apiParam {String} produit Nom du produit
+ *
+ * @apiSuccessExample {json} Success-Reponse:
+ * a faire
+ */
+ public function createAction(){
+
+ $contract_number = $this->getParameters()->get("contract_number");
+ $fqdn = $this->getParameters()->get("fqdn");
+ $channel_key = $this->getParameters()->get("channel_key");
+ $produit_name = $this->getParameters()->get("produit");
+ $type_hebergement_key = $this->getParameters()->get("type_hebergement_key");
+
+ $pdoContractManager = new PdoContractManager();
+ $contract = $pdoContractManager->getContractByNumber($contract_number);
+ if (! $contract->id){
+ throw new Exception("Le contrat {$contract_number} n'existe pas sur cette plateforme.");
+ }
+
+ $pdoProductManager = new PdoProductManager();
+ $product = $pdoProductManager->getProductByName($produit_name);
+ if (! $product->id){
+ throw new Exception("Le produit {$produit_name} n'existe pas sur cette plateforme.");
+ }
+
+ $pdoChannelManager = new PdoChannelManager();
+ $channel = $pdoChannelManager->getChannelByChannelKey($channel_key);
+
+ if (! $channel){
+ throw new Exception("Le canal indiqué {$channel_key} n'existe pas");
+ }
+
+ $pdoTypeHebergementManager = new PdoTypeHebergementManager();
+ $typeHebergement = $pdoTypeHebergementManager->getTypeHebergementByKey($type_hebergement_key);
+ if (! $typeHebergement){
+ throw new Exception("Le type d'hébergement indiqué {$type_hebergement_key} n'existe pas");
+ }
+
+
+ $pdoAppliancesManager = new PdoApplianceManager();
+ if ($pdoAppliancesManager->getByFQDN($fqdn)){
+ throw new Exception("Cette intance ($fqdn) existe déjà sur cette plateforme.");
+ }
+
+ $pdoCustomersManager = new PdoCustomerManager();
+ $customer = $pdoCustomersManager->getCustomer($contract->customer_id);
+
+ $uuid = new UUID();
+ $appliance_uuid = $uuid->generate();
+
+
+ $appliance = new Appliance();
+ $appliance->channel_id = $channel->id;
+ $appliance->internal_fqdn = $fqdn;
+ $appliance->contract_id = $contract->id;
+ $appliance->reference = "{$product->diminutif}/{$customer->ap_id}_".date("YmdHis");
+ $appliance->product = $product->id;
+ $appliance->manager = "";
+ $appliance->uuid = $appliance_uuid;
+ $appliance->type_hebergement_id = $typeHebergement->id;
+
+ $appliance->id = $pdoAppliancesManager->createAppliance($appliance);
+
+ return $appliance->uuid;
+
+ }
+
+ /**
+ * @api {get} /index.php?action=appliances_edit Création ou mise à jour d'une instance
+ * @apiGroup Instances
+ * @apiVersion 1.0.0
+ *
+ * @apiParam {Number} id identifiant de l'instance (0 ou vide pour créer)
+ * @apiParam {Number} version_id identifiant de la version
+ *
+ * @apiSuccessExample {json} Success-Reponse:
+ * {"retour":"OK","message":"","result":"42"}
+ */
+ public function editAction(){
+ $appliance = new Appliance();
+ $appliance->importFromArray($this->getParameters()->getTableauInput());
+ return $this->getPdoApplianceManager()->editAppliance($appliance);
+ }
+
+
+ public function allinfoAction(){
+ $sugarAppliance = new SugarAppliance(SugarConnector::getInstance());
+ $sugarAppliance->checkAll();
+ return true;
+ }
+
+ public function checkUpdateFromAgentAction(){
+ $array = $this->getInputData();
+ $applianceManager = new PdoApplianceManager();
+
+ $versionManager = new PdoVersionManager();
+ $appliance = $applianceManager->getApplianceByReference($array['reference']);
+
+ $pdoApplianceVersions = new PdoApplianceVersionManager();
+ $applianceVersion = $pdoApplianceVersions->getLast($appliance->id);
+
+ $currentVersion = $versionManager->getVersion($applianceVersion->version_id);
+
+ $nextPatchVersion = $versionManager->getNext($appliance->product, 'patch', $currentVersion->value)->value;
+ if (! isset($nextPatchVersion)) {
+ throw new Exception("Aucun nouveau patch n'est disponible pour votre version");
+ }
+ return "Nouveau patch disponible : " . $nextPatchVersion;
+ }
+
+ /**
+ * @api {get} /index.php?action=appliances_listByCustomer Récupération des instances pour un client
+ * @apiGroup Instances
+ * @apiVersion 1.0.0
+ * @apiParam {int} customer_id Identifiant du client
+ */
+ public function listByCustomerAction(){
+ $customer_id = $this->getParameters()->getInt('customer_id');
+ return $this->getPdoApplianceManager()->getInfoByCustomerId($customer_id);
+ }
+
+ /**
+ * @api {get} /index.php?action=appliances_info Information sur une instance
+ * @apiGroup Instances
+ * @apiVersion 1.0.0
+ * @apiParam {int} id Identifiant de l'instance
+ */
+ public function infoAction(){
+ $id = $this->getParameters()->getInt('id');
+ return $this->getPdoApplianceManager()->getInfo($id);
+ }
+
+ /**
+ * @api {get} /index.php?action=appliances_historique Liste de toutes les versions installé sur l'instance
+ * @apiGroup Instances
+ * @apiVersion 1.0.0
+ * @apiParam {int} id Identifiant de l'instance
+ */
+ public function historiqueAction(){
+ $id = $this->getParameters()->getInt('id');
+ return $this->getPdoApplianceManager()->getHistorique($id);
+ }
+
+ /**
+ * @api {get} /index.php?action=appliances_nbAValider Nombre d'instance à valider
+ * @apiGroup Instances
+ * @apiVersion 1.0.0
+ */
+ public function nbAValiderAction(){
+ return $this->getPdoApplianceManager()->nbAValider();
+ }
+
+ /**
+ * @api {get} /index.php?action=appliances_listAValider Liste des instances à valider
+ * @apiGroup Instances
+ * @apiVersion 1.0.0
+ * @apiParam {Number} limit Nombre d'élements à récupérer
+ * @apiParam {Number} offset Numéro du premier élement à récupérer
+ */
+ public function listAValiderAction(){
+ $offset = $this->getOffsetParameter();
+ $limit = $this->getLimitParameter();
+ return $this->getPdoApplianceManager()->listAValider($offset,$limit);
+ }
+
+ /**
+ * @api {get} /index.php?action=appliances_setValider Valide (ou pas) une instance
+ * @apiGroup Instances
+ * @apiVersion 1.0.0
+ * @apiParam {int} id identifiant de l'instance
+ * @apiParam {boolean} is_valide indique si l'instance est valide ou non
+ */
+ public function setValiderAction(){
+ $id = $this->getParameters()->getInt('id');
+ $is_valide = $this->getParameters()->get('is_valide')?1:0;
+ $this->getPdoApplianceManager()->setValider($id,$is_valide);
+ return true;
+ }
+
+ /**
+ * @api {get} /index.php?action=appliances_delete supprime une instance
+ * @apiGroup Instances
+ * @apiVersion 1.0.0
+ * @apiParam {int} id identifiant de l'instance
+ */
+ public function deleteAction(){
+ $id = $this->getParameters()->getInt('id');
+ $this->getPdoApplianceManager()->delete($id);
+ return true;
+ }
+
+ /**
+ * @api {get} /index.php?action=appliances_deleteByUUIDAction supprime une instance
+ * @apiGroup Instances
+ * @apiVersion 1.0.0
+ * @apiParam {uuid} uuid uuid de l'instance
+ */
+ public function deleteByUUIDAction(){
+ $uuid = $this->getParameters()->get('uuid');
+ $appliance = $this->getPdoApplianceManager()->getByUUID($uuid);
+ $this->getPdoApplianceManager()->delete($appliance->id);
+ return true;
+ }
+
+ /**
+ * @api {get} /index.php?action=appliances_editFicheVersion Crée ou met à jour une fiche version
+ * @apiGroup Instances
+ * @apiVersion 1.0.0
+ * @apiParam {int} id Identifiant de l'appliance_version (et pas de l'appliance !)
+ */
+ public function editFicheVersionAction(){
+ $id = $this->getParameters()->getInt('id');
+ $pdoApplianceVersionManager = new PdoApplianceVersionManager();
+ $applianceVersion = $pdoApplianceVersionManager->get($id);
+ if (! $applianceVersion){
+ throw new Exception("L'id (appliance_version) ne correspond à aucune version installée");
+ }
+ $sugarFicheVersion = new SugarFicheInstallation($this->getSugarWrapper());
+ return $sugarFicheVersion->edit($applianceVersion);
+ }
+
+}
\ No newline at end of file