From 5fb0a2785718160317069c87496d1602e32ea3d6 Mon Sep 17 00:00:00 2001 From: polo Date: Wed, 14 Aug 2024 17:20:10 +0200 Subject: autoload avec composer --- src/Config.php | 37 ++-- src/files.php | 5 - src/main.php | 42 +---- src/model/version 0.1/CESU.php | 66 ------- src/model/version 0.1/Clients.php | 168 ----------------- src/model/version 0.1/DB.php | 48 ----- src/model/version 0.1/DevisFactures.php | 215 --------------------- src/model/version 0.1/Locations.php | 127 ------------- src/model/version 0.1/Model.php | 313 ------------------------------- src/model/version 0.1/Prestations.php | 159 ---------------- src/model/version 0.1/StructTablesDB.php | 36 ---- 11 files changed, 27 insertions(+), 1189 deletions(-) delete mode 100644 src/model/version 0.1/CESU.php delete mode 100644 src/model/version 0.1/Clients.php delete mode 100644 src/model/version 0.1/DB.php delete mode 100644 src/model/version 0.1/DevisFactures.php delete mode 100644 src/model/version 0.1/Locations.php delete mode 100644 src/model/version 0.1/Model.php delete mode 100644 src/model/version 0.1/Prestations.php delete mode 100644 src/model/version 0.1/StructTablesDB.php (limited to 'src') diff --git a/src/Config.php b/src/Config.php index 16aa8ed..5de966f 100644 --- a/src/Config.php +++ b/src/Config.php @@ -4,26 +4,23 @@ class Config { // valeurs par défaut au cas où - static public $business_name = 'ORDIPOLO'; - static public $business_guy = 'Paul Jusot'; - static public $business_address = "2A rue de l'île de Man"; - static public $business_postcode = '29000'; - static public $business_city = 'Quimper'; - static public $db_name = "ordipolo"; - static public $db_path = 'data/'; - static public $latex_path = 'data/latex/'; - static public $pdf_path = 'data/pdf/'; - static public $pub_path = "pub/"; - static public $flyer = "flyer.xcf"; - static public $business_card = "carte.sla"; - static public $image_editor = 'gimp'; - static public $publishing = 'scribus'; - static public $sgbd = 'sqlite'; - static public $sqlite_gui = 'sqlitebrowser'; // ne pas utiliser si le sgbd n'est pas sqlite - static public $date_format = 'euro'; - - // pour que Config crée le dsn: Data Source Name (à mettre dans une autre classe!) - //~ static public $dsn = ''; + static public string $business_name = 'ORDIPOLO'; + static public string $business_guy = 'Paul Jusot'; + static public string $business_address = "2A rue de l'île de Man"; + static public string $business_postcode = '29000'; + static public string $business_city = 'Quimper'; + static public string $db_name = "ordipolo"; + static public string $db_path = 'data/'; + static public string $latex_path = 'data/latex/'; + static public string $pdf_path = 'data/pdf/'; + static public string $pub_path = "pub/"; + static public string $flyer = "flyer.xcf"; + static public string $business_card = "carte.sla"; + static public string $image_editor = 'gimp'; + static public string $publishing = 'scribus'; + static public string $sgbd = 'sqlite'; + static public string $sqlite_gui = 'sqlitebrowser'; // ne pas utiliser si le sgbd n'est pas sqlite + static public string $date_format = 'euro'; // ça pourrait être bien de founir sqlite avec l'application pour supprimer une dépendance //~ static public $sqliteBin = 'lib/sqlite_linux'; diff --git a/src/files.php b/src/files.php index ad20ee0..2a68d9d 100644 --- a/src/files.php +++ b/src/files.php @@ -25,11 +25,6 @@ function makeFile($path, $file_name, $data) //~ } } -//~ function deleteFile($path, $file_name) -//~ { - //~ unlink() -//~ } - function makeFolder(string $path) { $rights = 0755; // droits en octal diff --git a/src/main.php b/src/main.php index 45f6c43..ded3248 100644 --- a/src/main.php +++ b/src/main.php @@ -1,17 +1,18 @@ table = strtolower(__CLASS__); // cesu - } - - public function getAllWithWindowFields(): array // différent de Model::getAll() qui retourne get_object_vars($this) - { - return [ - "Tâche effectuée:" => $this->taches, - "Durée du travail:" => $this->duree_travail, - "Salaire:" => $this->salaire]; - } - public function set(string $entry, string $input) - { - $input = $this->cleanSpecialChars($input); // possibilité que $input devienne une chaine vide - switch($entry) - { - case "Tâche effectuée:": - $this->setTaches($input); - break; - case "Durée du travail:": - $this->setDureeTravail($input); - break; - case "Salaire:": - $this->setSalaire($input); - break; - } - } - - // setters - public function setIdPresta(int $value) - { - $this->id_presta = $value; - return($this); - } - public function setTaches(string $value) - { - $this->taches = $value; - return($this); - } - public function setDureeTravail(string $value) - { - $this->duree_travail = $value; - return($this); - } - public function setSalaire($value) - { - $value = str_replace(',', '.', $value); - $this->salaire = (float) $value; - return($this); - } -} diff --git a/src/model/version 0.1/Clients.php b/src/model/version 0.1/Clients.php deleted file mode 100644 index 32cf0c5..0000000 --- a/src/model/version 0.1/Clients.php +++ /dev/null @@ -1,168 +0,0 @@ -table = strtolower(__CLASS__); // clients - } - - // getters - public function getId(): int - { - return $this->id; - } - public function getCodeClient(): string - { - return $this->code_client; - } - public function getAllWithWindowFields(): array // différent de Model::getAll() qui retourne get_object_vars($this) - { - return [ - "Prénom Nom:" => $this->prenom_nom, - "Code client (J.C.Dusse):" => $this->code_client, - "Adresse:" => $this->adresse, - "Code postal:" => $this->code_postal, - "Ville:" => $this->ville, - "Telephone:" => $this->telephone, - "Courriel:" => $this->courriel, - "À propos:" => $this->apropos, - "Client ou Prospect?" => $this->type]; - } - public function set(string $entry, string $input) - { - $input = $this->cleanSpecialChars($input); // possibilité que $input devienne une chaine vide - switch($entry) - { - case "Prénom Nom:": - $this->setPrenomNom($input); - break; - case "Code client (J.C.Dusse):": - $this->setCodeClient($input); - break; - case "Adresse:": - $this->setAdresse($input); - break; - case "Code postal:": - $this->setCodePostal($input); - break; - case "Ville:": - $this->setVille($input); - break; - case "Telephone:": - $this->setTelephone($input); - break; - case "Courriel:": - $this->setCourriel($input); - break; - case "À propos:": - $this->setApropos($input); - break; - case "Client ou Prospect?": - $this->setType($input); - break; - } - } - - // setters - public function setPrenomNom($value) - { - $this->prenom_nom = (string) $value; - return $this; - } - public function setCodeClient($value) - { - $this->code_client = (string) $value; - return $this; - } - public function setAdresse($value) - { - $this->adresse = (string) $value; - return $this; - } - public function setCodePostal($value) - { - $this->code_postal = (string) $value; - return $this; - } - public function setVille($value) - { - $this->ville = (string) $value; - return $this; - } - public function setTelephone($value) - { - // type string parce que: - // - zenity renvoie une chaine - // - permet de garder le 0 au début et d'inscrire plusieurs numéros - $this->telephone = (string) $value; - return $this; - } - public function setCourriel($value) - { - $this->courriel = (string) $value; - return $this; - } - public function setApropos($value) - { - $this->apropos = (string) $value; - return $this; - } - public function setType($value) - { - $this->type = (string) $value; - return $this; - } - - public function typeToClient(): bool - { - if($this->type != 'client') - { - $this->type = 'client'; - return true; - } - else - { - return false; - } - } - - public function findByKeywords(array $keywords, string $field): array // n'hydrate pas les variables, on doit choisir un client et hydrater ensuite - { - $result = []; - for($i = 0; $i < count($keywords); $i++) - { - // tableau à deux dimensions obtenu pour un mot clé - $query_result = $this->execQuery('SELECT * FROM ' . $this->table . ' WHERE ' . $field . ' LIKE "%' . $keywords[$i] . '%"')->fetchAll(); - foreach($query_result as $one_array) // pour chaque sous tableau - { - $already_exist = false; - for($j = 0; $j < count($result); $j++) // pour chaque tableau déjà enregistré dans le tableau $result - { - if($result[$j]['id'] === $one_array['id']) - { - $already_exist = true; - } - } - if(!$already_exist) - { - $result[] = $one_array; - } - } - } - return $result; - } -} diff --git a/src/model/version 0.1/DB.php b/src/model/version 0.1/DB.php deleted file mode 100644 index 47407ba..0000000 --- a/src/model/version 0.1/DB.php +++ /dev/null @@ -1,48 +0,0 @@ - PDO::$dsn - //$this->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, 'SET NAMES utf8'); // pour mysql/mariadb - $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // $this pour la méthode du parent PDO - $this->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); // pour PDO:fetch() et PDO::fetchAll() - // avec PDO::FETCH_ASSOC on obtient un tableau associatif, marche très bien puisqu'on utilise déjà des ID avec incrémentation automatique - // avec PDO::FETCH_BOTH (par défaut) on récupère les données en double (identifiants partant de 0 + tableau associatif) - } - catch(PDOException $e) - { - die("Impossible de se connecter à la base de données.\n" . $e->getMessage()); - } - } - - // créer son objet depuis Model avec: $db = parent::getInstance(); - public static function getInstance(): self - { - if(self::$Instance === null) - { - self::$Instance = new self; - } - return self::$Instance; - } -} diff --git a/src/model/version 0.1/DevisFactures.php b/src/model/version 0.1/DevisFactures.php deleted file mode 100644 index 06a0a59..0000000 --- a/src/model/version 0.1/DevisFactures.php +++ /dev/null @@ -1,215 +0,0 @@ -table = $table; // deux tables séparées devis et factures - } - - public function getAllWithWindowFields(): array // différent de Model::getAll() qui retourne get_object_vars($this) - { - $taches = ["Tâches:" => $this->taches]; - $champs_communs = [ - "Total Main d'oeuvre:" => $this->total_main_d_oeuvre, - "Pièces:" => $this->pieces, - "Total des pièces:" => $this->total_pieces, - "Déplacement:" => $this->deplacement, - "Total HT:" => $this->total_HT]; - - if($this->table === 'factures') - { - $champs_facture = [ - "PC:" => $this->machine, - "OS:" => $this->OS, - "Données:" => $this->donnees, - "Clés de licences:" => $this->cles_licences]; - - return $taches + $champs_facture + $champs_communs; - } - elseif($this->table === 'devis') - { - $champs_devis = [ - "Delai de livraison:" => $this->delai_livraison, - "Durée de validité:" => $this->validite_devis, - "Devis signé:" => $this->signature_devis]; - - return $champs_communs + $champs_devis; - } - else - { - return []; - } - } - - public function set(string $entry, string $input) // trouve la bonne méthode - { - $input = $this->cleanSpecialChars($input); // possibilité que $input devienne une chaine vide - switch($entry) - { - case "Tâches:": - $this->setTaches($input); - break; - case "PC:": - $this->setMachine($input); - break; - case "OS:": - $this->setOS($input); - break; - case "Données:": - $this->setDonnees($input); - break; - case "Clés de licences:": - $this->setClesLicences($input); - break; - case "Total Main d'oeuvre:": - $this->setTotalMainDOeuvre($input); - break; - case "Pièces:": - $this->setPieces($input); - break; - case "Total des pièces:": - $this->setTotalPieces($input); - break; - case "Déplacement:": - $this->setDeplacement($input); - break; - case "Prix du devis:": - $this->setPrixDevis($input); - break; - case "Total HT:": - $this->setTotalHT($input); - break; - case "Delai de livraison:": - $this->setDelaiLivraison($input); - break; - case "Durée de validité:": - $this->setValiditedevis($input); - break; - case "Devis signé:": - $this->setSignatureDevis($input); - break; - } - } - - // setters - //~ public function setId(int $value = 0) - //~ { - //~ $this->id = $value; - //~ return($this); - //~ } - public function setIdPresta(int $value) - { - $this->id_presta = $value; - return($this); - } - public function setTaches(string $value) - { - $this->taches = $value; - return($this); - } - public function setMachine(string $value) - { - $this->machine = $value; - return($this); - } - public function setOS(string $value) - { - $this->OS = $value; - return($this); - } - public function setDonnees(string $value) - { - $this->donnees = $value; - return($this); - } - public function setClesLicences(string $value) - { - $this->cles_licences = $value; - return($this); - } - public function setTotalMainDOeuvre($value) - { - $value = str_replace(',', '.', $value); - $this->total_main_d_oeuvre = (float) $value; // float "nettoie" tous les caractères après le dernier chiffre trouvé (ex: 50€ => 50, abc => 0) - return($this); - } - public function setPieces(string $value) - { - $this->pieces = $value; - return($this); - } - public function setTotalPieces($value) - { - $value = str_replace(',', '.', $value); - $this->total_pieces = (float) $value; - return($this); - } - public function setDeplacement($value) - { - $value = str_replace(',', '.', $value); - $this->deplacement = (float) $value; - return($this); - } - public function setTotalHT($value) - { - $value = str_replace(',', '.', $value); - $this->total_HT = (float) $value; - return($this); - } - public function setPrixDevis($value) - { - $value = str_replace(',', '.', $value); - $this->prix_devis = (float) $value; - return($this); - } - public function setDelaiLivraison(string $value) - { - $this->delai_livraison = $value; - return($this); - } - public function setValiditedevis(string $value) - { - $this->validite_devis = $value; - return($this); - } - public function setSignatureDevis(string $value) - { - $this->signature_devis = $value; - return($this); - } - - // création d'une facture à partir d'un devis - public function hydrateReceiptWithQuotation($ReceiptDetails) - { - $ReceiptDetails->hydrate([ - 'taches' => $this->taches, - 'total_main_d_oeuvre' => $this->total_main_d_oeuvre, - 'pieces' => $this->pieces, - 'total_pieces' => $this->total_pieces, - 'deplacement' => $this->deplacement, - 'total_HT' => $this->total_HT - ]); - } -} diff --git a/src/model/version 0.1/Locations.php b/src/model/version 0.1/Locations.php deleted file mode 100644 index c6b8deb..0000000 --- a/src/model/version 0.1/Locations.php +++ /dev/null @@ -1,127 +0,0 @@ -table = strtolower(__CLASS__); // locations - } - - public function getAllWithWindowFields(): array // différent de Model::getAll() qui retourne get_object_vars($this) - { - return [ - "Désignation:" => $this->designation, - "Description du modèle:" => $this->modele_description, - "Valeur:" => $this->valeur, - "État des lieux de début:" => $this->etat_des_lieux_debut, - "État des lieux de fin:" => $this->etat_des_lieux_fin, - "Durée de la location:" => $this->duree_location, - "Loyer Mensuel:" => $this->loyer_mensuel, - "Loyers Payés:" => $this->loyers_payes, - "Caution:" => $this->caution]; - } - public function set(string $entry, string $input) - { - $input = $this->cleanSpecialChars($input); // possibilité que $input devienne une chaine vide - switch($entry) - { - case "Désignation:": - $this->setDesignation($input); - break; - case "Description du modèle:": - $this->setModeleDescription($input); - break; - case "Valeur:": - $this->setValeur($input); - break; - case "État des lieux de début:": - $this->setEtatDesLieuxDebut($input); - break; - case "État des lieux de fin:": - $this->setEtatDesLieuxFin($input); - break; - case "Durée de la location:": - $this->setDureeLocation($input); - break; - case "Loyer Mensuel:": - $this->setLoyerMensuel($input); - break; - case "Loyers Payés:": - $this->setLoyersPayes($input); - break; - case "Caution:": - $this->setCaution($input); - break; - } - } - - // setters - public function setIdPresta(int $value) - { - $this->id_presta = $value; - return($this); - } - public function setDesignation(string $value) - { - $this->designation = $value; - return($this); - } - public function setModeleDescription(string $value) - { - $this->modele_description = $value; - return($this); - } - public function setValeur($value) - { - $value = str_replace(',', '.', $value); - $this->valeur = (float) $value; - return($this); - } - public function setEtatDesLieuxDebut(string $value) - { - $this->etat_des_lieux_debut = $value; - return($this); - } - public function setEtatDesLieuxFin(string $value) - { - $this->etat_des_lieux_fin = $value; - return($this); - } - public function setDureeLocation(string $value) - { - $this->duree_location = $value; - return($this); - } - public function setLoyerMensuel($value) - { - $value = str_replace(',', '.', $value); - $this->loyer_mensuel = (float) $value; - return($this); - } - public function setLoyersPayes($value) - { - $value = str_replace(',', '.', $value); - $this->loyers_payes = (float) $value; - return($this); - } - public function setCaution($value) - { - $value = str_replace(',', '.', $value); - $this->caution = (float) $value; - return($this); - } -} diff --git a/src/model/version 0.1/Model.php b/src/model/version 0.1/Model.php deleted file mode 100644 index b3d157d..0000000 --- a/src/model/version 0.1/Model.php +++ /dev/null @@ -1,313 +0,0 @@ -table; - } - - public function getAll(): array // à améliorer pour ne pas renvoyer $db et $table - { - return get_object_vars($this); // retourne les propriétés de l'objet - } - - // setters - public function setId(int $value = 0) - { - if($value === 0) - { - $this->id = $this->db->lastInsertId(); // méthode de PDO, attention lastInsertId() ne gère pas la concurence - } - else - { - $this->id = $value; - } - return $this; - } - public function setTable(string $value) - { - $this->table = $value; - return($this); - } - - public function hydrate(array $data): bool // $data = tableau associatif en entrée: nom_du_champ => valeur - { - foreach($data as $key => $value) - { - // nom du setter - // nom_propriete => setPropriete - // on sépare les mots par des espaces, ucwords met la première lettre de chaque mot en majuscule, puis on supprime les espaces - $setter_name = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key))); // ucwords: première lettre de chaque mot en majuscule - if(method_exists($this, $setter_name)) - { - $this->$setter_name($value); - } - else - { - echo "debug: la méthode $setter_name n'existe pas\n"; - return false; - } - } - return true; - } - - // cette fonction reçoit des données d'un tableau simple, permettant d'associer des champs de formulaires aux noms différents des champs de la BDD - // méthode lancée par des objets de type enfants - function hydrateFromForm(string $data_string, Object $Presta = NULL): bool // quand l'objet est $Details, on hydrate aussi $Presta - { - $data_string = $this->cleanSpecialChars($data_string); // possibilité que $data_string devienne une chaine vide - if($data_string !== '') - { - $data_array = explode('|', $data_string); // array - $check = false; - switch($this->getTable()) - { - case 'clients'; - if($data_array[0] == '') - { - echo "debug: données insuffisantes, le nom du client doit au minimum être renseigné\n"; - return false; - } - else - { - $check = $this->hydrate(['prenom_nom' => $data_array[0], 'code_client' => $data_array[1], 'adresse' => $data_array[2], 'code_postal' => $data_array[3], 'ville' => $data_array[4], 'telephone' => $data_array[5], 'courriel' => $data_array[6], 'apropos' => $data_array[7]]); - } - break; - case 'prestations'; // inutilisé - break; - case 'devis'; - $check = $this->hydrate(['taches' => $data_array[0], 'total_main_d_oeuvre' => $data_array[1], 'pieces' => $data_array[2], 'total_pieces' => $data_array[3], 'deplacement' => $data_array[4], 'prix_devis' => $data_array[5], 'total_HT' => $data_array[6], 'delai_livraison' => $data_array[7], 'validite_devis' => $data_array[8]]); - break; - case 'factures'; - if(count($data_array) === 11) - { - $check = $Presta->hydrate(['mode_paiement' => $data_array[10]]); - if($check) - { - $check = $this->hydrate(['taches' => $data_array[0], 'machine' => $data_array[1], 'OS' => $data_array[2], 'donnees' => $data_array[3], 'cles_licences' => $data_array[4], 'total_main_d_oeuvre' => $data_array[5], 'pieces' => $data_array[6], 'total_pieces' => $data_array[7], 'deplacement' => $data_array[8], 'total_HT' => $data_array[9]]); - } - } - elseif(count($data_array) === 5) // facture à partir d'un devis - { - $check = $this->hydrate(['machine' => $data_array[0], 'OS' => $data_array[1], 'donnees' => $data_array[2], 'cles_licences' => $data_array[3]]); - if($check) - { - $check = $Presta->hydrate(['mode_paiement' => $data_array[4]]); - } - } - else - { - echo "debug: le tableau \$data_array n'a pas la taille attendue.\n"; - return false; - } - break; - case 'cesu'; - $check = $Presta->hydrate(['mode_paiement' => $data_array[3]]); - if($check) - { - $check = $this->hydrate(['taches' => $data_array[0], 'duree_travail' => $data_array[1], 'salaire' => $data_array[2]]); - } - break; - case 'locations'; - $check = $this->hydrate(['designation' => $data_array[0], 'modele_description' => $data_array[1], 'valeur' => $data_array[2], 'etat_des_lieux_debut' => $data_array[3], 'etat_des_lieux_fin' => $data_array[4], 'duree_location' => $data_array[5], 'loyer_mensuel' => $data_array[6], 'loyers_payes' => $data_array[7], 'caution' => $data_array[8]]); - break; - default: // inutilisé - echo "debug: table inconnue hydrateFromForm()"; - return false; - } - return $check; - } - else - { - echo "debug: annulation lors du formulaire\n"; - return false; - } - } - - protected function cleanSpecialChars(string $data): string - { - $search = ['"']; - return str_replace($search, '', $data); - } - - - // exécuter le SQL - // les $attributs correspondent aux ? dans les requêtes préparées - // ne pas surcharger la méthode PDO::query() qui n'est pas compatible - protected function execQuery(string $sql, array $attributes = null) - { - $this->db = parent::getInstance(); // parent::, self:: et DB:: sont équivalents - - if($attributes !== null) // requête préparée - { - //~ var_dump($sql); - //~ var_dump($attributes); - $query = $this->db->prepare($sql); - $query->execute($attributes); - return $query; - } - else // requête simple - { - return $this->db->query($sql); - } - } - - - // méthodes CRUD - - // create INSERT - public function create() // = write - { - $fields = []; - $question_marks = []; // ? - $values = []; - //~ var_dump($this); - foreach($this as $field => $value) - { - //~ var_dump($field); var_dump($value); - // champs non renseignées et variables de l'objet qui ne sont pas des champs - // note: avec le !== (au lieu de !=) une valeur 0 est différente de null - if($value !== null && $field != 'db' && $field != 'table') - { - $value = $this->clean_for_bash($value); // pour la BDD - $this->$field = $value; // pour latex - - $fields[] = $field; // push - $question_marks[] = '?'; - $values[] = $value; - } - } - $field_list = implode(', ', $fields); - $question_mark_list = implode(', ', $question_marks); - - // INSERT INTO annonces (titre, description, actif) VALUES (?, ?, ?) - return($this->execQuery('INSERT INTO ' . $this->table . ' (' . $field_list . ') VALUES (' . $question_mark_list . ')', $values)); - } - - - // read SELECT - protected function readAll(): array // obtenir une table - { - return($this->execQuery('SELECT * FROM ' . $this->table)->fetchAll()); // fonctionne aussi sans le point virgule dans le SQL!! - } - - public function findById() // obtenir une entrée grace à son ID - { - return($this->execQuery('SELECT * FROM ' . $this->table . ' WHERE id = ' . $this->id)->fetch()); - } - public function getDetailsByIdPresta() - { - if($this->table == 'prestations') - { - // à l'occaz, créer une classe NonVendue et rendre Prestations abstraite - echo 'erreur: ne pas appeler Model::getDetailsByIdPresta() si la table ciblée est "prestations".'; - return 0; - } - else - { - return $this->execQuery('SELECT * FROM ' . $this->table . ' WHERE id_Presta = ' . $this->id_presta)->fetch(); // type array - } - } - - protected function find(array $criteria): array // obtenir une entrée avec un tableau associatif 'champ' => 'valeur' - { - $fields = []; - $values = []; - - // change "'id' => 2" en "'id' = ?" et "2" - foreach($criteria as $field => $value) - { - $fields[] = "$field = ?"; // même chose que: $field . " = ?" - $values[] = $value; - } - $field_list = implode(' AND ', $fields); // créer une chaîne reliant les morceaux avec le morceau AND en paramètre: 'adresse = ? AND id = ?' - - // SELECT * FROM annonces WHERE actif = 1; - return($this->execQuery('SELECT * FROM ' . $this->table . ' WHERE ' . $field_list, $values)->fetchAll()); - } - - - // update UPDATE - public function update() - { - $fields = []; - $values = []; - foreach($this as $field => $value) - { - if($value !== null && $field != 'db' && $field != 'table') // champs non renseignées et variables de l'objet qui ne sont pas des champs - { - $value = $this->clean_for_bash($value); // pour la BDD - $this->$field = $value; // pour latex - - $fields[] = $field . ' = ?'; - $values[] = $value; - } - } - $values[] = $this->id; // cette syntaxe ajoute une valeur au tableau - $field_list = implode(', ', $fields); - - // UPDATE annonces SET titre = ?, description = ?, actif = ? WHERE id = ? - return($this->execQuery('UPDATE ' . $this->table . ' SET ' . $field_list . ' WHERE id = ?', $values)); - } - - public function updateOneValue(string $field, $value) - { - return($this->execQuery('UPDATE ' . $this->table . ' SET ' . $field . ' = ? WHERE id = ?', [$value, $this->id])); - } - - - // delete DELETE - protected function delete(int $id) - { - return($this->execQuery("DELETE FROM {$this->table} WHERE id = ?", [$id])); // double quotes "" pour insertion de variable, paramètre [$id] parce qu'on veut un tableau - } - - - private function clean_for_bash($value) - { - if(is_string($value) && $value != '') - { - do - { - $value = trim($value); - $value = preg_replace('#^-#','' , $value); // en bash une chaîne commençant par un tiret est un paramètre - } while($value[0] == ' '|| $value[0] == '-'); // chaîne commençant par un tiret puis un espace, ou même - - - - - - } - return $value; - } - - - static public function createTables() - { - static $first_time = true; - - if($first_time) // fonction normallement appelée qu'une seule fois - { - foreach(StructTablesDB::$structureOfTables as $tableName => $oneTable) - { - $query = 'CREATE TABLE IF NOT EXISTS ' . $tableName . ' ('; - foreach($oneTable as $key => $value) - { - $query .= $key . ' ' . $value . ', '; - } - $query .= 'PRIMARY KEY(ID AUTOINCREMENT));'; - parent::getInstance()->exec($query); - } - - $first_time = false; - } - else - { - echo "Model::createTables() a déjà été appelée et ne fera rien.\n"; - } - } -} diff --git a/src/model/version 0.1/Prestations.php b/src/model/version 0.1/Prestations.php deleted file mode 100644 index 18bc787..0000000 --- a/src/model/version 0.1/Prestations.php +++ /dev/null @@ -1,159 +0,0 @@ -id_client = $id_client; - $this->table = strtolower(__CLASS__); // prestations - } - - // getters - public function getId(): int - { - return $this->id; - } - public function getIdClient(): int - { - return $this->id_client; - } - public function getIdsByIdClient() // obtenir une entrée avec son id_client, comportement différent si le type est connu - { - $sql = 'SELECT id FROM ' . $this->table . ' WHERE id_client = ' . $this->id_client; - if($this->type_presta != '') - { - $sql .= " AND type_presta = '" . $this->type_presta . "'"; - } - $data = $this->execQuery($sql)->fetchAll(); // tableau de tableaux - - $IDs = []; // si $IDs reste vide, ne pas être de type NULL - for($i = 0; $i < count($data); $i++) - { - $IDs[$i] = $data[$i]['id']; // tableau simple - } - return($IDs); - } - public function getCodePresta(): string - { - return $this->code_presta; - } - public function getDate(): int // timestamp unix - { - return $this->date; - } - public function getTypePresta(): string - { - return $this->type_presta; - } - public function getAllWithWindowFields(): array // différent de Model::getAll() qui retourne get_object_vars($this) - { - $code_presta_tableau = explode('-', $this->code_presta); - $Date = new Dates($this->date); - - return [ - "Numéro prestation:" => end($code_presta_tableau), // dernière case - "Date:" => $Date->getDate(), - //"Type de Presta:" => $this->type_presta, // choix impossible pour le moment - "Mode de paiement:" => $this->mode_paiement, // non pertinent pour un devis - "Commentaires:" => $this->commentaires]; - } - public function set(string $entry, string $input) - { - $input = $this->cleanSpecialChars($input); // possibilité que $input devienne une chaine vide - switch($entry) - { - case "Numéro prestation:": - $this->setNumeroPresta($input); - break; - //~ case "Date:": // inutile, setDate() est appelé directement après choix fenêtre calendrier - //~ $this->setDate($input); - //~ break; - //~ case "Type de Presta:": // choix impossible pour le moment - //~ $this->setTypePresta($input); - //~ break; - case "Mode de paiement:": - $this->setModePaiement($input); - break; - case "Commentaires:": - $this->setCommentaires($input); - break; - } - } - - // setters - public function setIdClient(int $value) - { - $this->id_client = $value; - return $this; - } - //~ public function setCombientiemeFois(int $value) - //~ { - //~ $this->combientieme_fois = $value; - //~ return($this); - //~ } - public function setCodePresta(string $value) - { - $this->code_presta = $value; - return $this; - } - public function setDate($value, bool $set_code_presta = false) // attend un timestamp - { - $this->date = (int) $value; - - if($set_code_presta) - { - $code_presta_tableau = explode('-', $this->code_presta); - $Date = new Dates($value); - $code_presta_tableau[0] = $Date->getYear(); - $code_presta_tableau[1] = $Date->getMonth(); - $code_presta_tableau[2] = $Date->getDay(); - $this->code_presta = implode('-', $code_presta_tableau); - } - - return $this; - } - public function setTypePresta(string $value) - { - $this->type_presta = $value; - return $this; - } - public function setModePaiement(string $value) - { - $this->mode_paiement = $value; - return $this; - } - public function setCommentaires(string $value) - { - $this->commentaires = $this->cleanSpecialChars($value); // nettoyage ici parce que pas possible ailleurs - return $this; - } - public function setNumeroPresta($value) - { - // modifier le code presta, on pourrait aussi utiliser une regex - $code_presta_tableau = explode('-', $this->code_presta); - $code_presta_tableau[count($code_presta_tableau) - 1] = (int) $value; - $this->code_presta = implode('-', $code_presta_tableau); - return $this; - } - - // code presta = année-mois-jour-codeclient-typedepresta-combientièmefois - public function makeCodePresta(Dates $Date, string $code_client) - { - // on récupère un tableau contenant toutes les prestations d'un client tous types confondus (devis, facture, cesu, location, enregistrement sans vente) - // inconvénient: il peut y avoir plusieurs prestations avec le même numéro au compteur, à améliorer - $combientieme_fois = count($this->find(['id_client' => $this->id_client])) + 1; - - $array_code = [$Date->getYear(), $Date->getMonth(), $Date->getDay(), $code_client, $this->type_presta, $combientieme_fois]; - $this->code_presta = implode('-', $array_code); - } -} diff --git a/src/model/version 0.1/StructTablesDB.php b/src/model/version 0.1/StructTablesDB.php deleted file mode 100644 index 303af46..0000000 --- a/src/model/version 0.1/StructTablesDB.php +++ /dev/null @@ -1,36 +0,0 @@ - ['id' => 'INTEGER', 'prenom_nom' => 'TEXT', 'code_client' => 'TEXT', 'adresse' => 'TEXT', 'code_postal' => 'TEXT', 'ville' => 'TEXT', 'telephone' => 'TEXT', 'courriel' => 'TEXT', 'apropos' => 'TEXT', 'type' => 'TEXT DEFAULT prospect'], - 'prestations' => ['id' => 'INTEGER', 'id_client' => 'INTEGER', 'code_presta' => 'TEXT', 'date' => 'INTEGER', 'type_presta' => 'TEXT', 'mode_paiement' => 'TEXT', 'commentaires' => 'TEXT'], - 'devis' => ['id' => 'INTEGER', 'id_presta' => 'INTEGER', 'taches' => 'TEXT', 'total_main_d_oeuvre' => 'REAL', 'pieces' => 'TEXT', 'total_pieces' => 'REAL', 'deplacement' => 'REAL', 'prix_devis' => 'REAL', 'total_HT' => 'REAL', 'delai_livraison' => 'TEXT', 'validite_devis' => 'TEXT', 'signature_devis' => 'TEXT DEFAULT non'], - 'factures' => ['id' => 'INTEGER', 'id_presta' => 'INTEGER', 'taches' => 'TEXT', 'machine' => 'TEXT', 'OS' => 'TEXT', 'donnees' => 'TEXT', 'cles_licences' => 'TEXT', 'total_main_d_oeuvre' => 'REAL', 'pieces' => 'TEXT', 'total_pieces' => 'REAL', 'deplacement' => 'REAL', 'total_HT' => 'REAL'], - 'cesu' => ['id' => 'INTEGER', 'id_presta' => 'INTEGER', 'taches' => 'TEXT', 'duree_travail' => 'TEXT', 'salaire' => 'REAL'], - 'locations' => ['id' => 'INTEGER', 'id_presta' => 'INTEGER', 'designation' => 'TEXT', 'modele_description' => 'TEXT', 'valeur' => 'REAL', 'etat_des_lieux_debut' => 'TEXT', 'etat_des_lieux_fin' => 'TEXT', 'duree_location' => 'TEXT', 'loyer_mensuel' => 'REAL', 'loyers_payes' => 'INTEGER', 'caution' => 'INTEGER'] - ]; - - // les types de variables de sqlite sont peu nombreux et autorisent un typage automatique - // le "type indiqué" est indiqué dans l'instruction CREATE TABLE - // https://www.leppf.com/site/spip.php?article89 - - // || type indiqué || type choisi automatiquement || autre types possibles || - // --------------------------------------------------------------------------- - // || TEXT || TEXT || BLOB, NULL || - // || INTEGER || INTEGER (de 1 à 8 octets) || REAL, TEXT, BLOB, NULL || - // || REAL || REAL (flottant sur 9 octets) || TEXT, BLOB, NULL || - // || NUMERIC || INTEGER ou REAL || TEXT, BLOB, NULL || - // || NONE || indéfini || dépend des données || - - // du code SQL écrit pour d'autres SGBD devrait fonctionner, - // sqlite fera des conversions dans ses propres types avec les problèmes qu'on peut imaginer - - // pour les dates, on stockera à priori le timestamp -} -- cgit v1.2.3