From cf3553fef0a03619cc999821155c7bb841551ab7 Mon Sep 17 00:00:00 2001 From: osaajani Date: Sun, 10 Nov 2019 22:56:26 +0100 Subject: [PATCH] Add phone --- controllers/internals/Phone.php | 142 ++++++++++++++++++++++++++++++++ createDatabase.sql | 4 +- models/Phone.php | 87 +++++++++++++++++++ 3 files changed, 231 insertions(+), 2 deletions(-) create mode 100755 controllers/internals/Phone.php create mode 100755 models/Phone.php diff --git a/controllers/internals/Phone.php b/controllers/internals/Phone.php new file mode 100755 index 0000000..d3f78df --- /dev/null +++ b/controllers/internals/Phone.php @@ -0,0 +1,142 @@ + + * + * 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; + + class Phone extends \descartes\InternalController + { + private $model_phone; + private $internal_event; + + public function __construct(\PDO $bdd) + { + $this->model_phone = new \models\Phone($bdd); + $this->internal_event = new \controllers\internals\Event($bdd); + } + + /** + * Return a phone + * @param int $id : id of the phone + * @return array + */ + public function get (int $id) + { + $phone = $this->model_phone->get($id); + + if (!$phone) + { + return false; + } + + $phone['plateform_datas'] = json_decode($phone['plateform_datas'], true); + return $phone; + } + + + /** + * Return phones of a user + * @param int $id_user : id of the user + * @return array + */ + public function gets_for_user (int $id_user) + { + $phones = $this->model_phone->gets($id_user); + + if (!$phone) + { + return false; + } + + foreach ($phones as &$phone) + { + $phone['plateform_datas'] = json_decode($phone['plateform_datas'], true); + } + + return $phones; + } + + + /** + * Return all phones + * @return array + */ + public function get_all () + { + $phones = $this->model_phone->get_all(); + + if (!$phone) + { + return false; + } + + foreach ($phones as &$phone) + { + $phone['plateform_datas'] = json_decode($phone['plateform_datas'], true); + } + + return $phones; + } + + + /** + * Delete a phone + * @param int $id : Phone id + * @return bool + */ + public function delete (int $id) : boolean + { + return (bool) $this->model_phone->delete($id); + } + + + /** + * Create a phone + * @param int $id_user : User to insert phone for + * @param string $number : The number of the phone + * @param string $platform : The platform to use the phone + * @param array $platform_datas : An array of the datas of the platform (for example credentials for an api) + * @return bool : false on error, true on success + */ + public function insert (int $id_user, string $number, string $platform, array $platform_datas) : boolean + { + $phone = [ + 'id_user' => $id_user, + 'number' => $number, + 'platform' => $platform, + 'platform_datas' => json_encode($platform_datas), + ]; + + return (bool) $this->model_phone->insert($phone); + } + + + /** + * Update a phone + * @param int $id : Phone id + * @param int $id_user : User to insert phone for + * @param string $number : The number of the phone + * @param string $platform : The platform to use the phone + * @param array $platform_datas : An array of the datas of the platform (for example credentials for an api) + * @return bool : false on error, true on success + */ + public function update (int $id, int $id_user, string $number, string $platform, array $platform_datas) : boolean + { + $phone = [ + 'id_user' => $id_user, + 'number' => $number, + 'platform' => $platform, + 'platform_datas' => json_encode($platform_datas), + ]; + + return (bool) $this->model_phone->update($id, $phone); + } + + } diff --git a/createDatabase.sql b/createDatabase.sql index 77e879c..29954ea 100755 --- a/createDatabase.sql +++ b/createDatabase.sql @@ -144,11 +144,11 @@ CREATE TABLE IF NOT EXISTS user UNIQUE (email) ); -CREATE TABLE IF NOT EXISTS user_number +CREATE TABLE IF NOT EXISTS phone ( id INT NOT NULL AUTO_INCREMENT, id_user INT NOT NULL, - phone_number VARCHAR(25) NOT NULL, + number VARCHAR(25) NOT NULL, platform VARCHAR(100) NOT NULL, platform_datas JSON NOT NULL, CHECK (JSON_VALID(platform_datas)), diff --git a/models/Phone.php b/models/Phone.php new file mode 100755 index 0000000..d46ae3d --- /dev/null +++ b/models/Phone.php @@ -0,0 +1,87 @@ + + * + * This source file is subject to the GPL-3.0 license that is bundled + * with this source code in the file LICENSE. + */ + +namespace models; + + /** + * Allow phone database management + */ + class Phone extends \descartes\Model + { + /** + * Return a phone by his id + * @param int $id : Phone id + * @return array + */ + public function get (int $id) + { + return $this->_select_one('phone', ['id' => $id]); + } + + /** + * Find phones of a user + * @param string $id_user : user's id + * @return array + */ + public function gets_for_user (int $id_user) + { + return $this->_select('phone', ['id_user' => $id_user]); + } + + + /** + * Find all phones + * @return array + */ + public function get_all () + { + return $this->_select('phone'); + } + + /** + * Delete a phone + * @param int $id : phone id + * @return array + */ + public function delete ($id) + { + return $this->_delete('phone', ['id' => $id]); + } + + + /** + * Create a phone + * @param int $id_user : User to insert phone for + * @param string $number : The number of the phone + * @param string $platform : The platform to use the phone + * @param string JSON $platform_datas : A json string representing the datas of the platform (for exemple credentials of an api) + * @return mixed bool : false on error, true on success + */ + public function insert($phone) + { + return (bool) $this->_insert('phone', $phone); + } + + + /** + * Update a phone + * @param int $id : Id of the phone + * @param int $id_user : User to insert phone for + * @param string $number : The number of the phone + * @param string $platform : The platform to use the phone + * @param string JSON $platform_datas : A json string representing the datas of the platform (for exemple credentials of an api) + * @return mixed bool : false on error, true on success + */ + public function update ($id, $phone) + { + return (bool) $this->_update('phone', $phone, ['id' => $id]); + } + }