--- /dev/null
+<?php
+
+ require_once('protected/required.php');
+
+ class PdoApplianceManager extends AbstractPdoManager {
+
+ public function createAppliance($appliance) {
+
+ $query = $this -> pdo -> prepare(
+ 'INSERT INTO appliances (
+ product,
+ manager,
+ reference,
+ changelog,
+ maintained,
+ internal_fqdn,
+ external_fqdn,
+ os,
+ disc_infos,
+ channel
+ ) VALUES (
+ :product,
+ :manager,
+ :reference,
+ :changelog,
+ :maintained,
+ :internal_fqdn,
+ :external_fqdn,
+ :os,
+ :disc_infos,
+ :channel
+ )'
+ );
+ $query -> bindValue(':product', $appliance -> getProduct());
+ $query -> bindValue(':manager', $appliance -> getManager());
+ $query -> bindValue(':reference', $appliance -> getReference());
+ $query -> bindValue(':changelog', $appliance -> getChangelog());
+ $query -> bindValue(':maintained', $appliance -> isMaintained());
+ $query -> bindValue(':internal_fqdn', $appliance -> getInternalFqdn());
+ $query -> bindValue(':external_fqdn', $appliance -> getExternalFqdn());
+ $query -> bindValue(':os', $appliance -> getOs());
+ $query -> bindValue(':disc_infos', $appliance -> getDiscInfos());
+ $query -> bindValue(':channel', $appliance -> getChannel());
+ $query -> execute();
+ $query -> closeCursor();
+
+ }
+
+ public function updateAppliance($appliance) {
+
+ $query = $this -> pdo -> prepare(
+ 'UPDATE appliances
+ SET product = :product,
+ manager = :manager,
+ reference = :reference,
+ changelog = :changelog,
+ maintained = :maintained,
+ internal_fqdn = :internal_fqdn,
+ external_fqdn = :external_fqdn,
+ os = :os,
+ disc_infos = :disc_infos,
+ channel = :channel
+ WHERE id = :id'
+ );
+ $query -> bindValue(':id', $appliance -> getId());
+ $query -> bindValue(':product', $appliance -> getProduct());
+ $query -> bindValue(':manager', $appliance -> getManager());
+ $query -> bindValue(':reference', $appliance -> getReference());
+ $query -> bindValue(':changelog', $appliance -> getChangelog());
+ $query -> bindValue(':maintained', $appliance -> isMaintained());
+ $query -> bindValue(':internal_fqdn', $appliance -> getFqdnInterne());
+ $query -> bindValue(':external_fqdn', $appliance -> getFqdnExterne());
+ $query -> bindValue(':os', $appliance -> getOs());
+ $query -> bindValue(':disc_infos', $appliance -> getDiscInfos());
+ $query -> bindValue(':channel', $appliance -> getChannel());
+ $query -> execute();
+ $query -> closeCursor();
+ }
+
+ public function updateApplianceInfos($array) {
+ $appliance = $this -> getApplianceByReference($array['reference']);
+
+ // IF APPLIANCE EXISTS
+ if(isset($appliance) && $appliance -> getId() > 0) {
+
+ // IF APPLIANCE IS MAINTAINED
+ if(!$appliance -> isMaintained()) {
+ echo "Votre application n'est plus maintainue par nos services, merci de vous renseigner auprès de votre contact ADULLACT Projet pour plus d'informations.";
+ } else {
+
+ // IN CASE OF MANAGER
+ if($array['usager_type'] === "exploitant") {
+
+ // GETTING MANAGER OF APPLIANCE
+ $managerManager = new PdoManagerManager();
+ $manager = $managerManager -> getManagerByAPId($appliance -> getManager());
+
+ // UPDATING ALL APPLIANCES OF THIS MANAGER
+ $array['usager_type'] = "client";
+ $appliances_id = $manager -> getAppliances();
+ foreach ($appliances_id as $appliance_id) {
+ $array['reference'] = $this -> getAppliance($appliance_id) -> getReference();
+ $this -> updateApplianceInfos($array);
+ }
+
+ // IN CASE OF CUSTOMER
+ } else if($array['usager_type'] === "client") {
+
+ // CHECKING APPLIANCE NAME
+ if ($array['produit'] === $appliance -> getProduct()) {
+
+ // IF VERSION DIFFERS FROM DB
+ // TO-DO : GET CURRENT VERSION OF APPLIANCE VIA CHANGELOG
+ //if($array['version'] != $appliance -> getVersion()) {
+
+ // GETTING PDO MANAGERS
+ $versionManager = new PdoVersionManager();
+ $productManager = new PdoProductManager();
+ $warningManager = new PdoWarningManager();
+
+ // GETTING VERSION ID
+ $version_id = $versionManager -> getIdByValueAndProductId($array['version'], $productManager->getProductByName($array['produit'])->getId());
+
+ // NEW WARNING CREATION, VERSION TYPE
+ $warningMessage = "Version declaree ".$array['version']." sans mise à jour par ALLO.";
+ $warning = new Warning(null, null, "version", $warningMessage, $version_id);
+ $warningManager -> createWarning($warning);
+
+ // SENDING MAIL TO ADMIN
+ $to = 'david.calmel@adullact-projet.coop';
+ $subject = 'ALLO - Nouvelle warning';
+ $message = $warningMessage;
+ $headers = 'From: ne-pas-repondre@allo-serveur';
+ mail($to, $subject, $message, $headers);
+
+ // GETTING CHANGELOG OF APPLIANCE
+ $changelogManager = new PdoChangelogManager();
+ $changelog = $changelogManager -> getChangelog($appliance -> getChangelog());
+
+ // IF CHANGELOG EXISTS, ADDING NEW WARNING TO IT
+ if(isset($changelog) && $changelog -> getId() > 0) {
+ $warnings = $changelog -> getWarnings();
+ if((array_search("", $warnings)) !== false) {
+ $warnings = array($warningManager -> getLastWarning() -> getId());
+ } else {
+ $warnings[] = $warningManager -> getLastWarning() -> getId();
+ }
+ $changelog -> setWarnings($warnings);
+ $changelogManager -> updateChangelog($changelog);
+
+ // IF CHANGELOG DOES NOT EXIST, CREATING NEW ONE
+ } else {
+ $changelog = new Changelog(null, array($warningManager -> getLastWarning() -> getId()), array(""));
+ $changelogManager -> createChangelog($changelog);
+ $appliance -> setChangelog($changelogManager -> getLastChangelog() -> getId());
+ $this -> updateAppliance($appliance);
+ }
+ //}
+
+ // USELESS ?
+ // UPDATING APPLIANCE INFORMATION
+ /*$query = $this -> pdo -> prepare(
+ 'UPDATE appliances SET version = :version WHERE reference = :reference'
+ );
+ $query -> bindValue(':reference', $array['reference']);
+ $query -> bindValue(':version', $array['version']);
+ $query -> execute();*/
+ } else {
+ echo "Le nom de l'instance applicative est incorrect";
+ }
+ } else {
+ echo "Le type d'usager est inconnu, merci de corriger la configuration.";
+ }
+ }
+
+ // IF APPLIANCE DOES NOT EXISTS
+ } else {
+
+ // GETTING PDO MANAGERS
+ $versionManager = new PdoVersionManager();
+ $productManager = new PdoProductManager();
+ $warningManager = new PdoWarningManager();
+
+ // GETTING PRODUCT ID
+ $product_id = $productManager->getProductByName($array['produit'])->getId();
+
+ // GETTING VERSION ID
+ $version_id = $versionManager->getIdByValueAndProductId($array['version'], $product_id);
+
+ // NEW WARNING CREATION, CREATION TYPE
+ $warning = new Warning(null, null, "creation",
+ "Nouvelle instance applicative declaree : ".$array['produit']." ; version : ".$array['version'], $version_id);
+ $warningManager -> createWarning($warning);
+
+ // NEW CHANGELOG CREATION
+ $changelogManager = new PdoChangelogManager();
+ $changelog = new Changelog(null, array($warningManager -> getLastWarning() -> getId()), array(""));
+ $changelogManager -> createChangelog($changelog);
+
+ // NEW APPLIANCE CREATION
+ // TO-DO : MANAGE APPLIANCE CREATION WITH MANAGER TYPE
+ $appliance = new Appliance(
+ // ID
+ null,
+ // PRODUCT
+ $product_id,
+ // MANAGER
+ null,
+ // REFERENCE
+ $array['reference'],
+ // CHANGELOG
+ $changelogManager -> getLastChangelog() -> getId(),
+ // IS MAINTAINED
+ true,
+ // FQDNs
+ $array['fqdn_interne'],
+ $array['fqdn_externe'],
+ // OS
+ $array['systeme'],
+ // UUIDs
+ $array['disques'],
+ // CHANNEL
+ $array['canal']
+ );
+ $this->createAppliance($appliance);
+ }
+
+
+ }
+
+ public function getAppliance($id) {
+
+ $query = $this -> pdo -> prepare('SELECT * FROM appliances WHERE id = :id');
+ $query -> bindValue(':id', $id);
+ $query -> execute();
+
+ $result = $query -> fetch(PDO::FETCH_ASSOC);
+ $appliance = new Appliance(
+ $result['id'],
+ $result['product'],
+ $result['manager'],
+ $result['reference'],
+ $result['changelog'],
+ $result['maintained'],
+ $result['internal_fqdn'],
+ $result['external_fqdn'],
+ $result['os'],
+ $result['disc_infos'],
+ $result['channel']
+ );
+
+ $query->closeCursor();
+
+ return $appliance;
+ }
+
+ public function getApplianceByReference($reference) {
+
+ $query = $this -> pdo -> prepare('SELECT * FROM appliances WHERE reference = :reference');
+ $query -> bindValue(':reference', $reference);
+ $query -> execute();
+
+ $result = $query -> fetch(PDO::FETCH_ASSOC);
+ $appliance = new Appliance(
+ $result['id'],
+ $result['product'],
+ $result['manager'],
+ $result['reference'],
+ $result['changelog'],
+ $result['maintained'],
+ $result['internal_fqdn'],
+ $result['external_fqdn'],
+ $result['os'],
+ $result['disc_infos'],
+ $result['channel']
+ );
+
+ $query->closeCursor();
+
+ return $appliance;
+ }
+
+ public function getAppliances() {
+
+ $query = $this -> pdo -> prepare('SELECT * FROM appliances');
+ $query -> execute();
+
+ $results = $query -> fetchAll(PDO::FETCH_ASSOC);
+ $appliances = array();
+ foreach ($results as $result) {
+ $appliances[] = new Appliance(
+ $result['id'],
+ $result['product'],
+ $result['manager'],
+ $result['reference'],
+ $result['changelog'],
+ $result['maintained'],
+ $result['internal_fqdn'],
+ $result['external_fqdn'],
+ $result['os'],
+ $result['disc_infos'],
+ $result['channel']
+ );
+ }
+
+ $query->closeCursor();
+
+ return $appliances;
+ }
+
+ public function removeAppliance($appliance) {
+
+ $query = $this -> pdo -> prepare('DELETE FROM appliances WHERE id = :id');
+ $query -> bindValue('id', $appliance -> getId());
+ $query -> execute();
+
+ }
+
+ public function checkUploadInfoValidity($array) {
+
+ // CHECKING REFERENCE FORMAT
+ $regex = '#^[A-Z]{2}/[A-Z0-9]+_[0-9]{5}_[0-9]{10}$#';
+ if (preg_match($regex, $array['reference'])) {
+
+ // CHECKING PRODUCT EXISTENCE
+ $productManager = new PdoProductManager();
+ $products = $productManager -> getNamesOfProducts();
+ if(in_array($array['produit'], $products)) {
+
+ // CHECKING VERSION VALIDITY
+ $versionManager = new PdoVersionManager();
+ $versions = $versionManager->findValuesOfVersionsByProductId($productManager->getProductByName($array['produit'])->getId());
+ if(in_array($array['version'], $versions)) {
+ $returnMsg = "ok";
+ } else {
+ $returnMsg = "Cette version n'est pas disponible pour ce produit";
+ }
+ } else {
+ $returnMsg = "Le produit n'est pas valide";
+ }
+ } else {
+ $returnMsg = 'Le format de la référence est incorrect.';
+ }
+ return $returnMsg;
+ }
+
+ public function disableMaintainedById($id) {
+ $query = $this -> pdo -> prepare(
+ 'UPDATE appliances SET maintained = :maintained WHERE id = :id'
+ );
+ $query -> bindValue(':id', $id);
+ $query -> bindValue(':maintained', false);
+ $query -> execute();
+ $query -> closeCursor();
+ }
+ }
+