From 0e22f3d02c217751255dfbdd3acf4b25e1eb94a4 Mon Sep 17 00:00:00 2001 From: osaajani Date: Tue, 12 Nov 2019 20:46:45 +0100 Subject: [PATCH] Implement standard controller and model and update commands to use them --- .../internals/.StandardController.php.swp | Bin 0 -> 12288 bytes controllers/internals/Command.php | 118 +++++------------ controllers/internals/StandardController.php | 111 ++++++++++++++++ models/Command.php | 108 +--------------- models/StandardModel.php | 120 ++++++++++++++++++ 5 files changed, 264 insertions(+), 193 deletions(-) create mode 100644 controllers/internals/.StandardController.php.swp create mode 100755 controllers/internals/StandardController.php create mode 100755 models/StandardModel.php diff --git a/controllers/internals/.StandardController.php.swp b/controllers/internals/.StandardController.php.swp new file mode 100644 index 0000000000000000000000000000000000000000..5cd195fd15e66712fa90cde68b12631d30462d84 GIT binary patch literal 12288 zcmeI2O>7%Q6vrp!qa?I^sx1&_vXz+nd+fu7f#a0Cl3*ndkFb(o3Ey?OkVVH+TzJft#j;|a&4fLhJe_Q; zH!^xQw~Ycuft?lDOG=L(4yXD<2S@4s_pR*Q#MCef7zK<1MggOMQNSo*6fg=H1^zP% zsKyYv0Bd%|7WwgYsOuX4G#8_QQNSo*6fg=H1&jhl0i%FXz$jo8FbWt2{)GxK8-vCl z|3BV>EB^oA`uqQny9xOn{06=Q--0dh1}KAv!6=~Mue%7j0X_jAfb&3tW$-ju07t>S z;ObsNeg<3ME$}9I6*Ry;Fbpp5A>@1THTVp?2j;;v*bQ#nNyv5Z3-|_H0i!Rw$2Y)}GMZ-=emQ}7W8!1G`OFt7q10oQMX{@@bW0%t%O>;b#L5Fp^%t%Q6D zE&>JCK?#h5EAZcCp#AuR_N6KRYXvCEd7?jcX@Aqd1XqFZ=$Kk}WZ~EvSCzVNb2m3$ zIA-~Qrz$mHR07FGZp^X6FXPihH`L_?5?=7p2&%4Q(OTeH%JDtg)fPv2Xwp7uMDxYe zcRCY~MOU7pUQn-cLH!!#I1=P=*-^E@1gq0@w`j70VN%CVSDe*0boI4KVH0IkTb0x; z=JSXV8kd1Jf>d1bUXdoWxdSQH-9y@@iI#=q$;h_V>{Oze;C25jx2f=(+Xx5E<{<87 zmuNGCs(T`|9#U!-ZPMx{MHHd4EP-3{W(hqexC~rHkM@m6$AiOEZY+G+K-yY@GsS7E z$xb5HopX?^>ih1CQ4H2__>5D8GZ(@a6V&6)*i_ia@hsINja6epuuU2bGSOH0@T4An zp9bk$3t^>}pvjYe7@U~Rvf_9d(J2R#u)7bM>kQD?T*sC>#%NeIv5Drp6hjx#OMkZK zJcV#I$LpBV{i9P)=B_Y49i=u#T}P^Pr;Z1O*MuK5^j-Z%952j@*{oRICUNTb^%dQ# zR>I&#c4&Qt3}X$p#s_jnN5|csI37AC>GvqIp6gD~=(c_Zrj@=mn4emqD?|1AcrDd0 zMVX>YY|ZhQ&i)fRwc6Tu7IUq@g~~O_RUfM}`%TQ4qV1#>Wf^oEY2T-DIyNz+J_|w| z6l$IKgvHHU6)7`?d@(bvHR;^x*-71wSnAwI#hc8kZR-^t zm-dLVcaoI~&V(Zn154#rmrgtrHjE}p)F@HJa+#F0_pCVO=M?vl1`vrO89cNEXKq0QZ=ps8$U;+vQmO^17p(HBOftY(;os z5ifhv_dH%_j>P`tNWt1 F{%_yI-aG&R literal 0 HcmV?d00001 diff --git a/controllers/internals/Command.php b/controllers/internals/Command.php index e82e33e..4970b8c 100755 --- a/controllers/internals/Command.php +++ b/controllers/internals/Command.php @@ -11,120 +11,60 @@ namespace controllers\internals; - /** - * Classe des commandes. - */ - class Command extends \descartes\InternalController + class Command extends StandardController { - private $model_command; - private $internal_event; - - public function __construct(\PDO $bdd) - { - $this->model_command = new \models\Command($bdd); - $this->internal_event = new \controllers\internals\Event($bdd); - } + protected $model = false; /** - * Return the list of commands as an array. - * - * @param mixed(int|bool) $nb_entry : Le nombre d'entrées à retourner par page - * @param mixed(int|bool) $page : Le numéro de page en cours - * - * @return array : La liste des commandes + * Get the model for the Controller + * @return \descartes\Model */ - public function list($nb_entry = null, $page = null) + protected function get_model () : \descartes\Model { - return $this->model_command->list($nb_entry, $nb_entry * $page); + $this->model = $this->model ?? new \models\Command($this->$bdd); + return $this->model; } + /** - * Get all commands. - * - * @return array + * Create a new command + * @param int $id_user : User id + * @param string $name : Command name + * @param string $script : Script file + * @param bool $admin : Is command admin only + * @return mixed bool|int : False if cannot create command, id of the new command else */ - public function get_all() - { - //Recupération des commandes - return $this->model_command->get_all(); - } - - /** - * Cette fonction retourne une liste des commandes sous forme d'un tableau. - * - * @param array int $ids : Les ids des entrées à retourner - * - * @return array : La liste des commandes - */ - public function gets($ids) - { - //Recupération des commandes - return $this->model_command->gets($ids); - } - - /** - * Cette fonction permet de compter le nombre de scheduleds. - * - * @return int : Le nombre d'entrées dans la table - */ - public function count() - { - return $this->model_command->count(); - } - - /** - * Cette fonction va supprimer une commande. - * - * @param array $id : L'id de la commande à supprimer - * - * @return int : Le nombre de commandes supprimées; - */ - public function delete($id) - { - return $this->model_command->delete($id); - } - - /** - * Cette fonction insert une nouvelle commande. - * - * @param array $command : La commande à insérer - * @param mixed $name - * @param mixed $script - * @param mixed $admin - * - * @return mixed bool|int : false si echec, sinon l'id de la nouvelle commande insérée - */ - public function create($name, $script, $admin) + public function create(int $id_user, string $name, string $script, bool $admin) { $command = [ + 'id_user' => $id_user, 'name' => $name, 'script' => $script, 'admin' => $admin, ]; $result = $this->model_command->insert($command); - if (!$result) { return false; } - $this->internal_event->create($_SESSION['user']['id'], 'COMMAND_ADD', 'Ajout commande : '.$name.' => '.$script); - + $this->internal_event->create($id_user, 'COMMAND_ADD', 'Ajout commande : ' . $name . ' => ' . $script); + return $result; } - + + /** - * Cette fonction met à jour un commande. - * - * @param mixed $id - * @param mixed $name - * @param mixed $script - * @param mixed $admin - * - * @return int : le nombre de ligne modifiées + * Update a command + * @param int $id_user : User id + * @param int $id : Command id + * @param string $name : Command name + * @param string $script : Script file + * @param bool $admin : Is command admin only + * @return mixed bool|int : False if cannot create command, id of the new command else */ - public function update($id, $name, $script, $admin) + public function update_for_user(int $id_user, int $id, string $name, string $script, bool $admin) { $command = [ 'name' => $name, @@ -132,6 +72,6 @@ namespace controllers\internals; 'admin' => $admin, ]; - return $this->model_command->update($id, $command); + return $this->model_command->update_for_user($id_user, $id, $command); } } diff --git a/controllers/internals/StandardController.php b/controllers/internals/StandardController.php new file mode 100755 index 0000000..118bcc2 --- /dev/null +++ b/controllers/internals/StandardController.php @@ -0,0 +1,111 @@ + + * + * This source file is subject to the GPL-3.0 license that is bundled + * with this source code in the file LICENSE. + */ + +namespace controllers\internals; + + abstract class StandardController extends \descartes\InternalController + { + public function __construct(\PDO $bdd) + { + $this->bdd = $bdd; + } + + /** + * Get the model for the Controller + * @return \descartes\Model + */ + abstract protected function get_model () : \descartes\Model; + + + /** + * Create a new entry + * @return mixed bool|int : False if cannot create entry, id of the new entry else + */ + abstract public function create(); + + + /** + * Update a entry + * @return mixed bool|int : False if cannot update entry, number of modified rows else + */ + abstract public function update_for_user(); + + + /** + * Return a entry by his id + * @param int $id : Entry id + * @return array + */ + public function get (int $id) + { + return $this->get_model()->get($id); + } + + + /** + * Return the list of entries for a user + * @param int $id_user : User id + * @param ?int $nb_entry : Number of entry to return + * @param ?int $page : Pagination, used to calcul offset, $nb_entry * $page + * @return array : Entrys list + */ + public function list_for_user (int $id_user, ?int $nb_entry = null, ?int $page = null) + { + return $this->get_model()->list_for_user($id_user, $nb_entry, $nb_entry * $page); + } + + + /** + * Return a list of entries in a group of ids and for a user + * @param int $id_user : user id + * @param array $ids : ids of entries to find + * @return array + */ + public function gets_in_for_user (int $id_user, array $ids) + { + return $this->get_model()->gets_in_for_user($id_user, $ids); + } + + + /** + * Insert a entry + * @param + * @param array $entry : Entry to insert + * @return mixed bool|int : false on error, new entry id else + */ + public function create ($entry) + { + $result = $this->get_model()->insert($entry); + } + + + /** + * Delete a entry by his id for a user + * @param int $id_user : User id + * @param int $id : Entry id + * @return int : Number of removed rows + */ + public function delete_for_user(int $id_user, int $id) + { + return $this->get_model()->delete_for_user($id_user, $id); + } + + + /** + * Count number of entry for a user + * @param int $id_user : User id + * @return int : number of entries + */ + public function count_for_user(int $id_user) + { + return $this->get_model()->count_for_user($id_user); + } + } diff --git a/models/Command.php b/models/Command.php index f0c7c94..d12c7b4 100755 --- a/models/Command.php +++ b/models/Command.php @@ -14,111 +14,11 @@ namespace models; /** * Cette classe gère les accès bdd pour les commandes. */ - class Command extends \descartes\Model + class Command extends StandardModel { /** - * Return a command by his id - * @param int $id : command id - * @return array + * Return table name + * @return string */ - public function get($id) - { - return $this->_select_one('command', ['id' => $id]); - } - - - /** - * Return a list of commands for a user - * @param int $id_user : user id - * @param int $limit : Number of command to return - * @param int $offset : Number of command to ignore - * @return array - */ - public function list_for_user (int $id_user, $limit, $offset) - { - return $this->_select('command', ['id_user' => $id_user], null, false, $limit, $offset); - } - - /** - * Return a list of commands in a group of ids and for a user - * @param int $id_user : user id - * @param array $ids : un ou plusieurs id d'entrées à récupérer - * - * @return array : La liste des entrées - */ - public function gets_in_for_user($id_user, $ids) - { - $query = ' - SELECT * FROM command - WHERE id_user = :id_user - AND id '; - - //On génère la clause IN et les paramètres adaptés depuis le tableau des id - $generated_in = $this->_generate_in_from_array($ids); - $query .= $generated_in['QUERY']; - $params = $generated_in['PARAMS']; - $params['id_user'] = $id_user; - - return $this->_run_query($query, $params); - } - - /** - * Supprime une commande. - * - * @param array $id : l'id de l'entrée à supprimer - * - * @return int : Le nombre de lignes supprimées - */ - public function delete($id) - { - $query = ' - DELETE FROM command - WHERE id = :id'; - - $params = ['id' => $id]; - - return $this->_run_query($query, $params, self::ROWCOUNT); - } - - /** - * Insert une commande. - * - * @param array $command : La commande à insérer - * - * @return mixed bool|int : false si echec, sinon l'id de la nouvelle lignée insérée - */ - public function insert($command) - { - $result = $this->_insert('command', $command); - - if (!$result) - { - return false; - } - - return $this->_last_id(); - } - - /** - * Met à jour une commande par son id. - * - * @param int $id : L'id de la command à modifier - * @param array $command : Les données à mettre à jour pour la commande - * - * @return int : le nombre de ligne modifiées - */ - public function update($id, $command) - { - return $this->_update('command', $command, ['id' => $id]); - } - - /** - * Compte le nombre d'entrées dans la table. - * - * @return int : Le nombre d'entrées - */ - public function count() - { - return $this->_count('command'); - } + protected function get_table_name() : string { return 'command'; } } diff --git a/models/StandardModel.php b/models/StandardModel.php new file mode 100755 index 0000000..969145c --- /dev/null +++ b/models/StandardModel.php @@ -0,0 +1,120 @@ + + * + * This source file is subject to the GPL-3.0 license that is bundled + * with this source code in the file LICENSE. + */ + +namespace models; + + /** + * Abstract class reprensenting the Standard Model + * This class implement/define most common methods for models + */ + abstract class StandardModel extends \descartes\Model + { + /** + * Return table name + * @return string + */ + protected function get_table_name() : string; + + + /** + * Return an entry by his id + * @param int $id : entry id + * @return array + */ + public function get($id) + { + return $this->_select_one($this->get_table_name(), ['id' => $id]); + } + + + /** + * Return a list of entries for a user + * @param int $id_user : user id + * @param int $limit : Number of entry to return + * @param int $offset : Number of entry to ignore + * @return array + */ + public function list_for_user (int $id_user, $limit, $offset) + { + return $this->_select($this->get_table_name(), ['id_user' => $id_user], null, false, $limit, $offset); + } + + + /** + * Return a list of entries in a group of ids and for a user + * @param int $id_user : user id + * @param array $ids : ids of entries to find + * @return array + */ + public function gets_in_for_user(int $id_user, $ids) + { + $query = ' + SELECT * FROM ' . $this->get_table_name() . ' + WHERE id_user = :id_user + AND id '; + + //On génère la clause IN et les paramètres adaptés depuis le tableau des id + $generated_in = $this->_generate_in_from_array($ids); + $query .= $generated_in['QUERY']; + $params = $generated_in['PARAMS']; + $params['id_user'] = $id_user; + + return $this->_run_query($query, $params); + } + + + /** + * Delete a entry by his id for a user + * @param int $id_user : User id + * @param int $id : Entry id + * @return int : Number of removed rows + */ + public function delete_for_user(int $id_user, $id) + { + return $this->_delete($this->get_table_name(), ['id_user' => $id_user, 'id' => $id]); + } + + + /** + * Insert a entry + * @param array $entry : Entry to insert + * @return mixed bool|int : false on error, new entry id else + */ + public function insert($entry) + { + $result = $this->_insert($this->get_table_name(), $entry); + return ($result ? $this->_last_id() : false); + } + + + /** + * Update a entry for a user + * @param int $id_user : User id + * @param int $id : Entry id + * @param array $datas : datas to update + * + * @return int : number of modified rows + */ + public function update_for_user(int $id_user, $id, $entry) + { + return $this->_update($this->get_table_name(), $entry, ['id_user' => $id_user, 'id' => $id]); + } + + /** + * Count number of entry for a user + * @param int $id_user : User id + * @return int : number of entries + */ + public function count_for_user(int $id_user) + { + return $this->_count($this->get_table_name(), ['id_user' => $id_user]); + } + }