raspisms/models/Command.php

125 lines
3.4 KiB
PHP
Raw Normal View History

2019-10-29 14:57:13 +01:00
<?php
/*
2019-11-10 17:48:54 +01:00
* This file is part of RaspiSMS.
*
2019-11-10 17:48:54 +01:00
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
*
2019-11-10 17:48:54 +01:00
* This source file is subject to the GPL-3.0 license that is bundled
* with this source code in the file LICENSE.
*/
namespace models;
/**
* Cette classe gère les accès bdd pour les commandes.
*/
class Command extends \descartes\Model
2019-10-29 14:57:13 +01:00
{
/**
2019-11-12 19:19:55 +01:00
* Return a command by his id
* @param int $id : command id
* @return array
*/
2019-11-04 17:12:52 +01:00
public function get($id)
2019-10-29 14:57:13 +01:00
{
2019-11-12 19:19:55 +01:00
return $this->_select_one('command', ['id' => $id]);
2019-10-29 14:57:13 +01:00
}
2019-11-12 19:19:55 +01:00
/**
2019-11-12 19:19:55 +01:00
* 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
*/
2019-11-12 19:19:55 +01:00
public function list_for_user (int $id_user, $limit, $offset)
2019-10-29 14:57:13 +01:00
{
2019-11-12 19:19:55 +01:00
return $this->_select('command', ['id_user' => $id_user], null, false, $limit, $offset);
}
2019-10-29 14:57:13 +01:00
/**
2019-11-12 19:19:55 +01:00
* Return a list of commands in a group of ids and for a user
* @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
*
2019-10-29 14:57:13 +01:00
* @return array : La liste des entrées
*/
2019-11-12 19:19:55 +01:00
public function gets_in_for_user($id_user, $ids)
2019-10-29 14:57:13 +01:00
{
$query = '
2019-10-29 14:57:13 +01:00
SELECT * FROM command
2019-11-12 19:19:55 +01:00
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);
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
return $this->_run_query($query, $params);
2019-10-29 14:57:13 +01:00
}
2019-10-29 14:57:13 +01:00
/**
* Supprime une commande.
*
2019-10-29 14:57:13 +01:00
* @param array $id : l'id de l'entrée à supprimer
*
2019-10-29 14:57:13 +01:00
* @return int : Le nombre de lignes supprimées
*/
2019-11-04 17:12:52 +01:00
public function delete($id)
2019-10-29 14:57:13 +01:00
{
$query = '
2019-10-29 14:57:13 +01:00
DELETE FROM command
WHERE id = :id';
2019-10-29 14:57:13 +01:00
$params = ['id' => $id];
return $this->_run_query($query, $params, self::ROWCOUNT);
2019-10-29 14:57:13 +01:00
}
/**
* Insert une commande.
*
2019-10-29 14:57:13 +01:00
* @param array $command : La commande à insérer
*
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
*/
public function insert($command)
2019-10-29 14:57:13 +01:00
{
$result = $this->_insert('command', $command);
2019-10-29 14:57:13 +01:00
if (!$result)
{
2019-10-29 14:57:13 +01:00
return false;
}
return $this->_last_id();
2019-10-29 14:57:13 +01:00
}
/**
* Met à jour une commande par son id.
*
* @param int $id : L'id de la command à modifier
2019-10-29 14:57:13 +01:00
* @param array $command : Les données à mettre à jour pour la commande
*
2019-10-29 14:57:13 +01:00
* @return int : le nombre de ligne modifiées
*/
public function update($id, $command)
2019-10-29 14:57:13 +01:00
{
return $this->_update('command', $command, ['id' => $id]);
2019-10-29 14:57:13 +01:00
}
2019-10-29 14:57:13 +01:00
/**
* Compte le nombre d'entrées dans la table.
*
2019-10-29 14:57:13 +01:00
* @return int : Le nombre d'entrées
*/
public function count()
2019-10-29 14:57:13 +01:00
{
return $this->_count('command');
2019-10-29 14:57:13 +01:00
}
}