2019-10-29 14:57:13 +01:00
|
|
|
<?php
|
2019-10-30 00:30:39 +01:00
|
|
|
|
|
|
|
/*
|
2019-11-10 17:48:54 +01:00
|
|
|
* This file is part of RaspiSMS.
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-11-10 17:48:54 +01:00
|
|
|
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-11-10 17:48:54 +01:00
|
|
|
* This source file is subject to the GPL-3.0 license that is bundled
|
2019-10-30 00:30:39 +01:00
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace models;
|
2019-10-29 18:33:49 +01:00
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette classe gère les accès bdd pour les contactes.
|
2019-10-29 18:33:49 +01:00
|
|
|
*/
|
|
|
|
class Contact extends \descartes\Model
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Retourne une entrée par son id.
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param int $id : L'id de l'entrée
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return array : L'entrée
|
|
|
|
*/
|
2019-11-04 17:12:52 +01:00
|
|
|
public function get($id)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-29 18:33:49 +01:00
|
|
|
$contacts = $this->_select('contact', ['id' => $id]);
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
return isset($contacts[0]) ? $contacts[0] : false;
|
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Retourne une entrée par son numéro de tel.
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param string $number : Le numéro de tél
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return array : L'entrée
|
|
|
|
*/
|
2019-10-29 18:33:49 +01:00
|
|
|
public function get_by_number($number)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-29 18:33:49 +01:00
|
|
|
$contacts = $this->_select('contact', ['number' => $number]);
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
return isset($contacts[0]) ? $contacts[0] : false;
|
|
|
|
}
|
2019-11-12 17:37:20 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a contact for a user by a number
|
|
|
|
* @param int $id_user : user id
|
|
|
|
* @param string $number : Contact number
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_by_number_and_user($number, $id_user)
|
|
|
|
{
|
|
|
|
return $this->_select_one('contact', ['number' => $number, 'id_user' => $id_user]);
|
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Retourne une entrée par son numéro de tel.
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param string $name : Le numéro de tél
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return array : L'entrée
|
|
|
|
*/
|
2019-10-29 18:33:49 +01:00
|
|
|
public function get_by_name($name)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-29 18:33:49 +01:00
|
|
|
$contacts = $this->_select('contact', ['name' => $name]);
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
return isset($contacts[0]) ? $contacts[0] : false;
|
|
|
|
}
|
2019-11-06 20:34:26 +01:00
|
|
|
|
2019-11-06 18:32:07 +01:00
|
|
|
/**
|
2019-11-06 20:34:26 +01:00
|
|
|
* Get contacts of a particular group.
|
2019-11-06 18:32:07 +01:00
|
|
|
*
|
|
|
|
* @param int $id_group : Id of the group we want contacts for
|
2019-11-06 20:34:26 +01:00
|
|
|
*
|
2019-11-06 18:32:07 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_by_group($id_group)
|
|
|
|
{
|
2019-11-06 20:34:26 +01:00
|
|
|
return $this->_select('contact', ['id_group' => $id_group]);
|
2019-11-06 18:32:07 +01:00
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-29 18:33:49 +01:00
|
|
|
/**
|
2019-11-12 19:19:55 +01:00
|
|
|
* List contacts for a user
|
|
|
|
* @param int $id_user : user id
|
|
|
|
* @param mixed(int|bool) $nb_entry : Number of entry to return
|
|
|
|
* @param mixed(int|bool) $page : Pagination, will offset $nb_entry * $page results
|
|
|
|
* @return array
|
2019-10-29 18:33:49 +01:00
|
|
|
*/
|
2019-11-12 19:19:55 +01:00
|
|
|
public function list_for_user($id_user, $limit, $offset)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-11-12 19:19:55 +01:00
|
|
|
return $this->_select('contact', ['id_user' => $id_user], null, false, $limit, $offset);
|
2019-10-29 18:33:49 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Retourne une liste de contactes sous forme d'un tableau.
|
2019-11-12 19:19:55 +01:00
|
|
|
* @param int $id_user : user id
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param array $ids : un ou plusieurs id d'entrées à récupérer
|
|
|
|
* @return array : La liste des entrées
|
2019-10-29 18:33:49 +01:00
|
|
|
*/
|
2019-11-12 19:19:55 +01:00
|
|
|
public function gets_for_user($id_user, $ids)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-30 00:30:39 +01:00
|
|
|
$query = '
|
2019-10-29 14:57:13 +01:00
|
|
|
SELECT * FROM contact
|
2019-11-12 19:19:55 +01:00
|
|
|
WHERE id_user = :id_user
|
|
|
|
AND ';
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 18:33:49 +01:00
|
|
|
//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);
|
2019-10-29 14:57:13 +01:00
|
|
|
$query .= $generated_in['QUERY'];
|
|
|
|
$params = $generated_in['PARAMS'];
|
2019-11-12 19:19:55 +01:00
|
|
|
$params['id_user'] = $id_user;
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-29 18:33:49 +01:00
|
|
|
return $this->_run_query($query, $params);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Supprimer un contact par son id.
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param array $id : un ou plusieurs id d'entrées à supprimer
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return int : Le nombre de lignes supprimées
|
2019-10-29 18:33:49 +01:00
|
|
|
*/
|
2019-11-04 17:12:52 +01:00
|
|
|
public function delete($id)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-30 00:30:39 +01:00
|
|
|
$query = '
|
2019-10-29 14:57:13 +01:00
|
|
|
DELETE FROM contact
|
2019-10-30 00:30:39 +01:00
|
|
|
WHERE id = :id';
|
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
$params = ['id' => $id];
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 18:33:49 +01:00
|
|
|
return $this->_run_query($query, $params, self::ROWCOUNT);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Insert une contacte.
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param array $contact : La contacte à insérer avec les champs name, script, admin & admin
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return mixed bool|int : false si echec, sinon l'id de la nouvelle lignée insérée
|
|
|
|
*/
|
2019-10-29 18:33:49 +01:00
|
|
|
public function insert($contact)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-29 18:33:49 +01:00
|
|
|
$result = $this->_insert('contact', $contact);
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$result)
|
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-29 18:33:49 +01:00
|
|
|
return $this->_last_id();
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Met à jour une contacte par son id.
|
|
|
|
*
|
|
|
|
* @param int $id : L'id de la contact à modifier
|
2019-10-29 14:57:13 +01:00
|
|
|
* @param array $contact : Les données à mettre à jour pour la contacte
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return int : le nombre de ligne modifiées
|
|
|
|
*/
|
2019-10-29 18:33:49 +01:00
|
|
|
public function update($id, $contact)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-29 18:33:49 +01:00
|
|
|
return $this->_update('contact', $contact, ['id' => $id]);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Compte le nombre d'entrées dans la table contact.
|
|
|
|
*
|
2019-10-29 14:57:13 +01:00
|
|
|
* @return int : Le nombre de contact
|
|
|
|
*/
|
2019-10-29 18:33:49 +01:00
|
|
|
public function count()
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-29 18:33:49 +01:00
|
|
|
return $this->_count('contact');
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
}
|