2019-11-12 20:46:45 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of RaspiSMS.
|
|
|
|
*
|
|
|
|
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
2019-11-13 03:24:22 +01:00
|
|
|
abstract protected function get_table_name() : string;
|
2019-11-12 20:46:45 +01:00
|
|
|
|
2019-12-12 00:56:30 +01:00
|
|
|
/**
|
|
|
|
* Return all the entries
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_all ()
|
|
|
|
{
|
|
|
|
return $this->_select($this->get_table_name());
|
|
|
|
}
|
|
|
|
|
2019-11-12 20:46:45 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an entry by his id
|
|
|
|
* @param int $id : entry id
|
|
|
|
* @return array
|
|
|
|
*/
|
2019-11-13 20:23:12 +01:00
|
|
|
public function get(int $id)
|
|
|
|
{
|
|
|
|
return $this->_select_one($this->get_table_name(), ['id' => $id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an entry by his id for a user
|
|
|
|
* @param int $id_user : user id
|
|
|
|
* @param int $id : entry id
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_for_user(int $id_user, int $id)
|
2019-11-12 20:46:45 +01:00
|
|
|
{
|
2019-11-15 06:30:23 +01:00
|
|
|
return $this->_select_one($this->get_table_name(), ['id' => $id, 'id_user' => $id_user]);
|
2019-11-12 20:46:45 +01:00
|
|
|
}
|
2019-11-14 21:44:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all entries for a user
|
|
|
|
* @param int $id_user : user id
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function gets_for_user(int $id_user)
|
|
|
|
{
|
|
|
|
return $this->_select($this->get_table_name(), ['id_user' => $id_user]);
|
|
|
|
}
|
2019-11-12 20:46:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2019-11-29 00:19:34 +01:00
|
|
|
if (!$ids)
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-11-12 20:46:45 +01:00
|
|
|
$query = '
|
2019-11-15 06:30:23 +01:00
|
|
|
SELECT * FROM `' . $this->get_table_name() . '`
|
2019-11-29 00:19:34 +01:00
|
|
|
WHERE id_user = :id_user
|
2019-11-12 20:46:45 +01:00
|
|
|
AND id ';
|
|
|
|
|
2019-11-29 00:19:34 +01:00
|
|
|
$params = [];
|
|
|
|
|
2019-11-12 20:46:45 +01:00
|
|
|
$generated_in = $this->_generate_in_from_array($ids);
|
|
|
|
$query .= $generated_in['QUERY'];
|
|
|
|
$params = $generated_in['PARAMS'];
|
2019-11-29 00:19:34 +01:00
|
|
|
|
|
|
|
//On génère la clause IN et les paramètres adaptés depuis le tableau des id
|
2019-11-12 20:46:45 +01:00
|
|
|
$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
|
|
|
|
*/
|
2019-11-14 22:33:00 +01:00
|
|
|
public function delete_for_user(int $id_user, int $id)
|
2019-11-12 20:46:45 +01:00
|
|
|
{
|
|
|
|
return $this->_delete($this->get_table_name(), ['id_user' => $id_user, 'id' => $id]);
|
|
|
|
}
|
2019-12-20 18:31:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a entry by his id
|
|
|
|
* @param int $id : Entry id
|
|
|
|
* @return int : Number of removed rows
|
|
|
|
*/
|
|
|
|
public function delete(int $id)
|
|
|
|
{
|
|
|
|
return $this->_delete($this->get_table_name(), ['id' => $id]);
|
|
|
|
}
|
2019-11-12 20:46:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2019-11-14 22:33:00 +01:00
|
|
|
public function update_for_user(int $id_user, int $id, array $entry)
|
2019-11-12 20:46:45 +01:00
|
|
|
{
|
|
|
|
return $this->_update($this->get_table_name(), $entry, ['id_user' => $id_user, 'id' => $id]);
|
|
|
|
}
|
|
|
|
|
2019-11-13 03:24:22 +01:00
|
|
|
|
2019-11-12 20:46:45 +01:00
|
|
|
/**
|
|
|
|
* 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]);
|
|
|
|
}
|
|
|
|
}
|