--- /dev/null
+<?php
+
+class PdoVersionManager extends AbstractPdoManager {
+
+ public function getNbVersionFromProductId($product_id){
+ $query = "SELECT count(*) FROM versions WHERE product = ?";
+ return $this->queryOne($query,$product_id);
+ }
+
+ public function getVersionFromProductId($product_id,$offset,$limit){
+ $query = "SELECT id,value,nature FROM versions WHERE product = ? ORDER BY value LIMIT $offset,$limit";
+ return $this->query($query,$product_id);
+ }
+
+ public function existsByValue($product_name,$version_value){
+ $sql = "SELECT count(*) FROM products " .
+ " JOIN versions ON versions.product=products.id " .
+ " WHERE products.name=? AND versions.value=? ";
+ return $this->queryOne($sql,$product_name,$version_value)?true:false;
+ }
+
+ public function getIdByValue($product_name,$version_value){
+ $sql = "SELECT versions.id FROM products " .
+ " JOIN versions ON versions.product=products.id " .
+ " WHERE products.name=? AND versions.value=? ";
+ return $this->queryOne($sql,$product_name,$version_value);
+ }
+
+ /**
+ * @param Version $version
+ * @return int version_id
+ * @throws Exception
+ */
+ public function edit(Version $version){
+ if (! $version->product){
+ throw new Exception("Aucun product_id fourni");
+ }
+ if ($version->id){
+ $this->updateVersion($version);
+ return $version->id;
+ } else {
+ return $this->createVersion($version);
+ }
+ }
+
+ public function delete($version_id){
+ $sql = "DELETE FROM versions WHERE id=?";
+ $this->query($sql,$version_id);
+ }
+
+ public function getInfo($version_id){
+ $sql = "SELECT id,value,product,nature FROM versions WHERE id=?";
+ return $this->queryOne($sql,$version_id);
+ }
+
+ public function createVersion(Version $version) {
+ $query = $this -> pdo -> prepare(
+ 'INSERT INTO versions (value, product, nature,sugar_id) VALUES (:value, :product, :nature,:sugar_id);'
+ );
+ $query -> bindValue(':value', $version -> value);
+ $query -> bindValue(':product', $version -> product);
+ $query -> bindValue(':nature', $version -> nature);
+ $query -> bindValue(':sugar_id', $version -> sugar_id);
+ $query -> execute();
+ return $this->lastInsertId();
+ }
+
+ public function updateVersion(Version $version) {
+
+ $query = $this -> pdo -> prepare(
+ 'UPDATE versions
+ SET value = :value,
+ product = :product,
+ nature = :nature,
+ sugar_id = :sugar_id
+ WHERE id = :id'
+ );
+ $query -> bindValue(':id', $version -> id);
+ $query -> bindValue(':value', $version -> value);
+ $query -> bindValue(':product', $version -> product);
+ $query -> bindValue(':nature', $version -> nature);
+ $query -> bindValue(':sugar_id', $version ->sugar_id);
+ $query -> execute();
+ }
+
+ public function getVersion($id) {
+ $query = $this -> pdo -> prepare('SELECT * FROM versions WHERE id = :id');
+ $query -> bindValue(':id', $id);
+ $query -> execute();
+
+ $result = $query -> fetch(PDO::FETCH_ASSOC);
+ $version = new Version();
+ $version->id = $result['id'];
+ $version->value = $result['value'];
+ $version->product = $result['product'];
+ $version->nature = $result['nature'];
+ $version->sugar_id = $result['sugar_id'];
+
+ $query->closeCursor();
+
+ return $version;
+ }
+
+ public function getVersions() {
+
+ $query = $this -> pdo -> prepare('SELECT * FROM versions');
+ $query -> execute();
+
+ $results = $query -> fetchAll(PDO::FETCH_ASSOC);
+ $versions = array();
+ foreach ($results as $result) {
+ $version = new Version();
+ $version->id = $result['id'];
+ $version->value = $result['value'];
+ $version->product = $result['product'];
+ $version->nature = $result['nature'];
+ $versions[] = $version;
+ }
+
+ $query->closeCursor();
+
+ return $versions;
+ }
+
+ public function findValuesOfVersionsByProductId($product_id) {
+ $query = $this -> pdo -> prepare('SELECT value FROM versions WHERE product = :product');
+ $query -> bindValue('product', $product_id);
+ $query -> execute();
+
+ $results = $query -> fetchAll(PDO::FETCH_ASSOC);
+ $values = array();
+ foreach ($results as $result) {
+ $values[] = $result['value'];
+ }
+
+ $query->closeCursor();
+
+ return $values;
+ }
+
+ public function getIdByValueAndProductId($value, $product_id) {
+ $query = $this -> pdo -> prepare('SELECT * FROM versions WHERE value = :value AND product = :product');
+ $query -> bindValue('value', $value);
+ $query -> bindValue('product', $product_id);
+ $query -> execute();
+
+ $result = $query -> fetch(PDO::FETCH_ASSOC);
+ $id = $result['id'];
+
+ $query->closeCursor();
+
+ return $id;
+ }
+
+ public function removeVersion(Version $version) {
+ $query = $this -> pdo -> prepare('DELETE FROM versions WHERE id = :id');
+ $query -> bindValue('id', $version -> id);
+ $query -> execute();
+ }
+
+ public function getNext($product_id, $nature, $currentVersion) {
+
+ $query = $this -> pdo -> prepare('SELECT * FROM versions WHERE product = :product AND nature = :nature ORDER BY value DESC');
+ $query -> bindValue('product', $product_id);
+ $query -> bindValue('nature', $nature);
+ $query -> execute();
+
+ $results = $query -> fetchAll(PDO::FETCH_ASSOC);
+ $nextPatchVersion = null;
+ foreach ($results as $result) {
+ $versionValue = (int) str_replace(".", "", $result['value']);
+ $currentVersionValue = (int) str_replace(".", "", $currentVersion);
+ if($currentVersionValue == $versionValue - 1) {
+ $nextPatchVersion = new Version();
+ $nextPatchVersion->id = $result['id'];
+ $nextPatchVersion->value = $result['value'];
+ $nextPatchVersion->product = $result['product'];
+ $nextPatchVersion->nature = $result['nature'];
+ }
+ }
+ $query->closeCursor();
+ return $nextPatchVersion;
+ }
+
+ /**
+ * @param $sugar_id
+ * @return Version|null
+ */
+ public function getBySugarId($sugar_id){
+ $sql = "SELECT * FROM versions WHERE sugar_id=?";
+ $result = $this->queryOne($sql,$sugar_id);
+ return $this->createObjectModel($result);
+ }
+}
\ No newline at end of file