mirror of
https://github.com/RaspbianFrance/raspisms.git
synced 2025-06-06 06:46:25 +02:00
remove makefile
This commit is contained in:
parent
f955cf3db0
commit
38a8b023cb
276 changed files with 0 additions and 138 deletions
25
models/Command.php
Executable file
25
models/Command.php
Executable file
|
@ -0,0 +1,25 @@
|
|||
<?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;
|
||||
|
||||
class Command extends StandardModel
|
||||
{
|
||||
/**
|
||||
* Return table name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_table_name(): string
|
||||
{
|
||||
return 'command';
|
||||
}
|
||||
}
|
38
models/ConditionalGroup.php
Executable file
38
models/ConditionalGroup.php
Executable file
|
@ -0,0 +1,38 @@
|
|||
<?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;
|
||||
|
||||
class ConditionalGroup extends StandardModel
|
||||
{
|
||||
/**
|
||||
* Return a conditional group by his name for a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param string $name : Group name
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_by_name_for_user(int $id_user, string $name)
|
||||
{
|
||||
return $this->_select_one($this->get_table_name(), ['id_user' => $id_user, 'name' => $name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return table name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_table_name(): string
|
||||
{
|
||||
return 'conditional_group';
|
||||
}
|
||||
}
|
51
models/Contact.php
Executable file
51
models/Contact.php
Executable file
|
@ -0,0 +1,51 @@
|
|||
<?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;
|
||||
|
||||
class Contact extends StandardModel
|
||||
{
|
||||
/**
|
||||
* Return a contact by his number for a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param string $number : Contact number
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_by_number_and_user(int $id_user, string $number)
|
||||
{
|
||||
return $this->_select_one($this->get_table_name(), ['id_user' => $id_user, 'number' => $number]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a contact by his name for a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param string $name : Contact name
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_by_name_and_user(int $id_user, string $name)
|
||||
{
|
||||
return $this->_select($this->get_table_name(), ['id_user' => $id_user, 'name' => $name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return table name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_table_name(): string
|
||||
{
|
||||
return 'contact';
|
||||
}
|
||||
}
|
38
models/Event.php
Executable file
38
models/Event.php
Executable file
|
@ -0,0 +1,38 @@
|
|||
<?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;
|
||||
|
||||
class Event extends StandardModel
|
||||
{
|
||||
/**
|
||||
* Gets lasts x events for a user order by date.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param int $nb_entry : Number of events to return
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_lasts_by_date_for_user(int $id_user, int $nb_entry)
|
||||
{
|
||||
return $this->_select('event', ['id_user' => $id_user], 'at', true, $nb_entry);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return table name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_table_name(): string
|
||||
{
|
||||
return 'event';
|
||||
}
|
||||
}
|
85
models/Group.php
Executable file
85
models/Group.php
Executable file
|
@ -0,0 +1,85 @@
|
|||
<?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;
|
||||
|
||||
class Group extends StandardModel
|
||||
{
|
||||
/**
|
||||
* Return a group by his name for a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param string $name : Group name
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_by_name_for_user(int $id_user, string $name)
|
||||
{
|
||||
return $this->_select_one($this->get_table_name(), ['id_user' => $id_user, 'name' => $name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete relations between group and contacts for a group.
|
||||
*
|
||||
* @param int $id_group : Group id
|
||||
*
|
||||
* @return int : Number of deleted rows
|
||||
*/
|
||||
public function delete_group_contact_relations(int $id_group)
|
||||
{
|
||||
return $this->_delete('group_contact', ['id_group' => $id_group]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a relation between a group and a contact.
|
||||
*
|
||||
* @param int $id_group : Group id
|
||||
* @param int $id_contact : Contact id
|
||||
*
|
||||
* @return mixed (bool|int) : False on error, new row id else
|
||||
*/
|
||||
public function insert_group_contact_relation(int $id_group, int $id_contact)
|
||||
{
|
||||
$success = (bool) $this->_insert('group_contact', ['id_group' => $id_group, 'id_contact' => $id_contact]);
|
||||
|
||||
return $success ? $this->_last_id() : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get groups contacts.
|
||||
*
|
||||
* @param int $id_group : Group id
|
||||
*
|
||||
* @return array : Contacts of the group
|
||||
*/
|
||||
public function get_contacts(int $id_group)
|
||||
{
|
||||
$query = '
|
||||
SELECT *
|
||||
FROM `contact`
|
||||
WHERE id IN (SELECT id_contact FROM `group_contact` WHERE id_group = :id_group)
|
||||
';
|
||||
|
||||
$params = ['id_group' => $id_group];
|
||||
|
||||
return $this->_run_query($query, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return table name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_table_name(): string
|
||||
{
|
||||
return 'group';
|
||||
}
|
||||
}
|
251
models/Media.php
Executable file
251
models/Media.php
Executable file
|
@ -0,0 +1,251 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* Cette classe gère les accès bdd pour les mediaes.
|
||||
*/
|
||||
class Media extends StandardModel
|
||||
{
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
$query = '
|
||||
SELECT * FROM `' . $this->get_table_name() . '`
|
||||
WHERE id_scheduled IN (SELECT id FROM scheduled WHERE id_user = :id_user)
|
||||
AND id = :id
|
||||
';
|
||||
|
||||
$params = [
|
||||
'id' => $id,
|
||||
'id_user' => $id_user,
|
||||
];
|
||||
|
||||
$receiveds = $this->_run_query($query, $params);
|
||||
|
||||
return $receiveds[0] ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all entries for a user.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function gets_for_user(int $id_user)
|
||||
{
|
||||
$query = '
|
||||
SELECT * FROM `' . $this->get_table_name() . '`
|
||||
WHERE id_scheduled IN (SELECT id FROM scheduled WHERE id_user = :id_user)
|
||||
';
|
||||
|
||||
$params = [
|
||||
'id_user' => $id_user,
|
||||
];
|
||||
|
||||
$receiveds = $this->_run_query($query, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a media for a user and a scheduled.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
* @param int $id_scheduled : scheduled id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_for_scheduled_and_user(int $id_user, int $id_scheduled)
|
||||
{
|
||||
$query = '
|
||||
SELECT * FROM `' . $this->get_table_name() . '`
|
||||
WHERE id_scheduled IN (SELECT id FROM scheduled WHERE id_user = :id_user)
|
||||
AND id_scheduled = :id_scheduled
|
||||
';
|
||||
|
||||
$params = [
|
||||
'id_user' => $id_user,
|
||||
'id_scheduled' => $id_scheduled,
|
||||
];
|
||||
|
||||
$receiveds = $this->_run_query($query, $params);
|
||||
if (!$receiveds)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return $receiveds[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of media for a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param int $limit : Max results to return
|
||||
* @param int $offset : Number of results to ignore
|
||||
*/
|
||||
public function list_for_user($id_user, $limit, $offset)
|
||||
{
|
||||
$limit = (int) $limit;
|
||||
$offset = (int) $offset;
|
||||
|
||||
$query = '
|
||||
SELECT * FROM media
|
||||
WHERE id_scheduled IN (SELECT id FROM scheduled WHERE id_user = :id_user)
|
||||
LIMIT ' . $limit . ' OFFSET ' . $offset;
|
||||
|
||||
$params = [
|
||||
'id_user' => $id_user,
|
||||
];
|
||||
|
||||
return $this->_run_query($query, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of medias in a group of ids and for a user.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
* @param array $ids : ids of medias to find
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function gets_in_for_user(int $id_user, $ids)
|
||||
{
|
||||
$query = '
|
||||
SELECT * FROM media
|
||||
WHERE id_scheduled IN (SELECT id FROM scheduled 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, int $id)
|
||||
{
|
||||
$query = '
|
||||
DELETE FROM media
|
||||
WHERE id = :id
|
||||
AND id_scheduled IN (SELECT id FROM scheduled WHERE id_user = :id_user)
|
||||
';
|
||||
|
||||
$params = ['id_user' => $id_user, 'id' => $id];
|
||||
|
||||
return $this->_run_query($query, $params, self::ROWCOUNT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a entry by his id for a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param int $id_scheduled : Scheduled id
|
||||
*
|
||||
* @return int : Number of removed rows
|
||||
*/
|
||||
public function delete_for_scheduled_and_user(int $id_user, int $id_scheduled)
|
||||
{
|
||||
$query = '
|
||||
DELETE FROM media
|
||||
WHERE id_scheduled = :id_scheduled
|
||||
AND id_scheduled IN (SELECT id FROM scheduled WHERE id_user = :id_user)
|
||||
';
|
||||
|
||||
$params = ['id_user' => $id_user, 'id_scheduled' => $id_scheduled];
|
||||
|
||||
return $this->_run_query($query, $params, self::ROWCOUNT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a media sms 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, int $id, array $datas)
|
||||
{
|
||||
$params = [];
|
||||
$sets = [];
|
||||
|
||||
foreach ($datas as $label => $value)
|
||||
{
|
||||
$label = preg_replace('#[^a-zA-Z0-9_]#', '', $label);
|
||||
$params['set_' . $label] = $value;
|
||||
$sets[] = '`' . $label . '` = :set_' . $label . ' ';
|
||||
}
|
||||
|
||||
$query = '
|
||||
UPDATE `media`
|
||||
SET ' . implode(', ', $sets) . '
|
||||
WHERE id = :id
|
||||
AND id_scheduled IN (SELECT id FROM scheduled WHERE id_user = :id_user)
|
||||
';
|
||||
|
||||
$params['id'] = $id;
|
||||
$params['id_user'] = $id_user;
|
||||
|
||||
return $this->_run_query($query, $params, self::ROWCOUNT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Count number of media sms for user.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
*
|
||||
* @return int : Number of media SMS for user
|
||||
*/
|
||||
public function count_for_user($id_user)
|
||||
{
|
||||
$query = '
|
||||
SELECT COUNT(id) as nb
|
||||
FROM media
|
||||
WHERE id_scheduled IN (SELECT id FROM scheduled WHERE id_user = :id_user)
|
||||
';
|
||||
|
||||
$params = [
|
||||
'id_user' => $id_user,
|
||||
];
|
||||
|
||||
return $this->_run_query($query, $params)[0]['nb'] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return table name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_table_name(): string
|
||||
{
|
||||
return 'media';
|
||||
}
|
||||
}
|
50
models/Phone.php
Executable file
50
models/Phone.php
Executable file
|
@ -0,0 +1,50 @@
|
|||
<?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;
|
||||
|
||||
class Phone extends StandardModel
|
||||
{
|
||||
/**
|
||||
* Return a phone by his number and user.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
* @param string $number : phone number
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_by_number_and_user(int $id_user, string $number)
|
||||
{
|
||||
return $this->_select_one('phone', ['number' => $number, 'id_user' => $id_user]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a phone by his number.
|
||||
*
|
||||
* @param string $number : phone number
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_by_number(string $number)
|
||||
{
|
||||
return $this->_select_one('phone', ['number' => $number]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return table name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_table_name(): string
|
||||
{
|
||||
return 'phone';
|
||||
}
|
||||
}
|
442
models/Received.php
Executable file
442
models/Received.php
Executable file
|
@ -0,0 +1,442 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* Cette classe gère les accès bdd pour les receivedes.
|
||||
*/
|
||||
class Received extends StandardModel
|
||||
{
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
$query = '
|
||||
SELECT * FROM `' . $this->get_table_name() . '`
|
||||
WHERE destination IN (SELECT number FROM phone WHERE id_user = :id_user)
|
||||
AND id = :id
|
||||
';
|
||||
|
||||
$params = [
|
||||
'id_user' => $id_user,
|
||||
'id' => $id,
|
||||
];
|
||||
|
||||
$receiveds = $this->_run_query($query, $params);
|
||||
|
||||
return $receiveds[0] ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all entries for a user.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function gets_for_user(int $id_user)
|
||||
{
|
||||
$query = '
|
||||
SELECT * FROM `' . $this->get_table_name() . '`
|
||||
WHERE destination IN (SELECT number FROM phone WHERE id_user = :id_user)
|
||||
';
|
||||
|
||||
$params = [
|
||||
'id_user' => $id_user,
|
||||
];
|
||||
|
||||
$receiveds = $this->_run_query($query, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of received for a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param int $limit : Max results to return
|
||||
* @param int $offset : Number of results to ignore
|
||||
*/
|
||||
public function list_for_user($id_user, $limit, $offset)
|
||||
{
|
||||
$limit = (int) $limit;
|
||||
$offset = (int) $offset;
|
||||
|
||||
$query = '
|
||||
SELECT * FROM received
|
||||
WHERE destination IN (SELECT number FROM phone WHERE id_user = :id_user)
|
||||
LIMIT ' . $limit . ' OFFSET ' . $offset;
|
||||
|
||||
$params = [
|
||||
'id_user' => $id_user,
|
||||
];
|
||||
|
||||
return $this->_run_query($query, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of unread received for a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param int $limit : Max results to return
|
||||
* @param int $offset : Number of results to ignore
|
||||
*/
|
||||
public function list_unread_for_user($id_user, $limit, $offset)
|
||||
{
|
||||
$limit = (int) $limit;
|
||||
$offset = (int) $offset;
|
||||
|
||||
$query = '
|
||||
SELECT * FROM received
|
||||
WHERE destination IN (SELECT number FROM phone WHERE id_user = :id_user)
|
||||
AND status = \'unread\'
|
||||
LIMIT ' . $limit . ' OFFSET ' . $offset;
|
||||
|
||||
$params = [
|
||||
'id_user' => $id_user,
|
||||
];
|
||||
|
||||
return $this->_run_query($query, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of receiveds in a group of ids and for a user.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
* @param array $ids : ids of receiveds to find
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function gets_in_for_user(int $id_user, $ids)
|
||||
{
|
||||
$query = '
|
||||
SELECT * FROM received
|
||||
WHERE destination IN (SELECT number FROM phone 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, int $id)
|
||||
{
|
||||
$query = '
|
||||
DELETE FROM received
|
||||
WHERE id = :id
|
||||
AND destination IN (SELECT number FROM phone WHERE id_user = :id_user)
|
||||
';
|
||||
|
||||
$params = ['id_user' => $id_user, 'id' => $id];
|
||||
|
||||
return $this->_run_query($query, $params, self::ROWCOUNT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a received sms 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, int $id, array $datas)
|
||||
{
|
||||
$params = [];
|
||||
$sets = [];
|
||||
|
||||
foreach ($datas as $label => $value)
|
||||
{
|
||||
$label = preg_replace('#[^a-zA-Z0-9_]#', '', $label);
|
||||
$params['set_' . $label] = $value;
|
||||
$sets[] = '`' . $label . '` = :set_' . $label . ' ';
|
||||
}
|
||||
|
||||
$query = '
|
||||
UPDATE `received`
|
||||
SET ' . implode(', ', $sets) . '
|
||||
WHERE id = :id
|
||||
AND destination IN (SELECT number FROM phone WHERE id_user = :id_user)
|
||||
';
|
||||
|
||||
//If try to update destination, also check it does belong to user
|
||||
if ($sets['set_destination'] ?? false)
|
||||
{
|
||||
$query .= ' AND :set_destination IN (SELECT number FROM phone WHERE id_user = :id_user)';
|
||||
}
|
||||
|
||||
$params['id'] = $id;
|
||||
$params['id_user'] = $id_user;
|
||||
|
||||
return $this->_run_query($query, $params, self::ROWCOUNT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Count number of received sms for user.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
*
|
||||
* @return int : Number of received SMS for user
|
||||
*/
|
||||
public function count_for_user(int $id_user)
|
||||
{
|
||||
$query = '
|
||||
SELECT COUNT(id) as nb
|
||||
FROM received
|
||||
WHERE destination IN (SELECT number FROM phone WHERE id_user = :id_user)
|
||||
';
|
||||
|
||||
$params = [
|
||||
'id_user' => $id_user,
|
||||
];
|
||||
|
||||
return $this->_run_query($query, $params)[0]['nb'] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Count number of unread received sms for user.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
*
|
||||
* @return int : Number of received SMS for user
|
||||
*/
|
||||
public function count_unread_for_user(int $id_user)
|
||||
{
|
||||
$query = '
|
||||
SELECT COUNT(id) as nb
|
||||
FROM received
|
||||
WHERE destination IN (SELECT number FROM phone WHERE id_user = :id_user)
|
||||
AND status = \'unread\'
|
||||
';
|
||||
|
||||
$params = [
|
||||
'id_user' => $id_user,
|
||||
];
|
||||
|
||||
return $this->_run_query($query, $params)[0]['nb'] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return x last receiveds message for a user, order by date.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param int $nb_entry : Number of receiveds messages to return
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_lasts_by_date_for_user(int $id_user, int $nb_entry)
|
||||
{
|
||||
$nb_entry = (int) $nb_entry;
|
||||
|
||||
$query = '
|
||||
SELECT *
|
||||
FROM received
|
||||
WHERE destination IN (SELECT number FROM phone WHERE id_user = :id_user)
|
||||
ORDER BY at ASC
|
||||
LIMIT ' . $nb_entry;
|
||||
|
||||
$params = [
|
||||
'id_user' => $id_user,
|
||||
];
|
||||
|
||||
return $this->_run_query($query, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return receiveds for an origin and a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param string $origin : Number who sent the message
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function gets_by_origin_and_user(int $id_user, string $origin)
|
||||
{
|
||||
$query = '
|
||||
SELECT *
|
||||
FROM received
|
||||
WHERE destination IN (SELECT number FROM phone WHERE id_user = :id_user)
|
||||
AND origin = :origin
|
||||
';
|
||||
|
||||
$params = [
|
||||
'id_user' => $id_user,
|
||||
'origin' => $origin,
|
||||
];
|
||||
|
||||
return $this->_run_query($query, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get number of sended SMS for every date since a date for a specific user.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
* @param \DateTime $date : Date since which we want the messages
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function count_by_day_since_for_user(int $id_user, $date)
|
||||
{
|
||||
$query = "
|
||||
SELECT COUNT(id) as nb, DATE_FORMAT(at, '%Y-%m-%d') as at_ymd
|
||||
FROM received
|
||||
WHERE at > :date
|
||||
AND destination IN (SELECT number FROM phone WHERE id_user = :id_user)
|
||||
GROUP BY at_ymd
|
||||
";
|
||||
|
||||
$params = [
|
||||
'date' => $date,
|
||||
'id_user' => $id_user,
|
||||
];
|
||||
|
||||
return $this->_run_query($query, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all discussions (ie : numbers we have a message received from or sended to) for a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_discussions_for_user(int $id_user)
|
||||
{
|
||||
$query = '
|
||||
SELECT at, number
|
||||
FROM (
|
||||
SELECT at, destination as number FROM sended
|
||||
WHERE origin IN (SELECT number FROM phone WHERE id_user = :id_user)
|
||||
UNION (
|
||||
SELECT at, origin as number FROM received
|
||||
WHERE destination IN (SELECT number FROM phone WHERE id_user = :id_user)
|
||||
)
|
||||
) as discussions
|
||||
GROUP BY number
|
||||
ORDER BY at DESC
|
||||
';
|
||||
|
||||
$params = ['id_user' => $id_user];
|
||||
|
||||
return $this->_run_query($query, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get SMS received since a date for a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param $date : La date depuis laquelle on veux les SMS (au format 2014-10-25 20:10:05)
|
||||
*
|
||||
* @return array : Tableau avec tous les SMS depuis la date
|
||||
*/
|
||||
public function get_since_by_date_for_user(int $id_user, $date)
|
||||
{
|
||||
$query = "
|
||||
SELECT *
|
||||
FROM received
|
||||
WHERE at > STR_TO_DATE(:date, '%Y-%m-%d %h:%i:%s')
|
||||
AND destination IN (SELECT number FROM phone WHERE id_user = :id_user)
|
||||
ORDER BY at ASC";
|
||||
|
||||
$params = [
|
||||
'date' => $date,
|
||||
'id_user' => $id_user,
|
||||
];
|
||||
|
||||
return $this->_run_query($query, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find messages received since a date for a certain origin and user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param $date : Date we want messages sinces
|
||||
* @param string $origin : Origin number
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_since_by_date_for_origin_and_user(int $id_user, $date, string $origin)
|
||||
{
|
||||
$query = "
|
||||
SELECT *
|
||||
FROM received
|
||||
WHERE at > STR_TO_DATE(:date, '%Y-%m-%d %h:%i:%s')
|
||||
AND origin = :origin
|
||||
AND destination IN (SELECT number FROM phone WHERE id_user = :id_user)
|
||||
ORDER BY at ASC
|
||||
";
|
||||
|
||||
$params = [
|
||||
'id_user' => $id_user,
|
||||
'date' => $date,
|
||||
'origin' => $origin,
|
||||
];
|
||||
|
||||
return $this->_run_query($query, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find destination of last received message for an origin and user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param string $origin : Origin number
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_last_for_origin_and_user(int $id_user, string $origin)
|
||||
{
|
||||
$query = '
|
||||
SELECT *
|
||||
FROM received
|
||||
WHERE origin = :origin
|
||||
AND destination IN (SELECT number FROM phone WHERE id_user = :id_user)
|
||||
ORDER BY at DESC
|
||||
LIMIT 0,1
|
||||
';
|
||||
|
||||
$params = [
|
||||
'origin' => $origin,
|
||||
'id_user' => $id_user,
|
||||
];
|
||||
|
||||
$result = $this->_run_query($query, $params);
|
||||
|
||||
return $result[0] ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return table name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_table_name(): string
|
||||
{
|
||||
return 'received';
|
||||
}
|
||||
}
|
258
models/Scheduled.php
Executable file
258
models/Scheduled.php
Executable file
|
@ -0,0 +1,258 @@
|
|||
<?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;
|
||||
|
||||
class Scheduled extends StandardModel
|
||||
{
|
||||
/**
|
||||
* Return numbers for a scheduled message.
|
||||
*
|
||||
* @param int $id_scheduled : Scheduled id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_numbers(int $id_scheduled)
|
||||
{
|
||||
return $this->_select('scheduled_number', ['id_scheduled' => $id_scheduled]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return contacts for a scheduled message.
|
||||
*
|
||||
* @param int $id_scheduled : Scheduled id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_contacts(int $id_scheduled)
|
||||
{
|
||||
$query = 'SELECT * FROM contact WHERE id IN (SELECT id_contact FROM scheduled_contact WHERE id_scheduled = :id_scheduled)';
|
||||
$params = ['id_scheduled' => $id_scheduled];
|
||||
|
||||
return $this->_run_query($query, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return groups for a scheduled message.
|
||||
*
|
||||
* @param int $id_scheduled : Scheduled id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_groups(int $id_scheduled)
|
||||
{
|
||||
$query = 'SELECT * FROM `group` WHERE id IN (SELECT id_group FROM scheduled_group WHERE id_scheduled = :id_scheduled)';
|
||||
$params = ['id_scheduled' => $id_scheduled];
|
||||
|
||||
return $this->_run_query($query, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return conitional groups for a scheduled message.
|
||||
*
|
||||
* @param int $id_scheduled : Scheduled id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_conditional_groups(int $id_scheduled)
|
||||
{
|
||||
$query = 'SELECT * FROM `conditional_group` WHERE id IN (SELECT id_conditional_group FROM scheduled_conditional_group WHERE id_scheduled = :id_scheduled)';
|
||||
$params = ['id_scheduled' => $id_scheduled];
|
||||
|
||||
return $this->_run_query($query, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a number for a scheduled.
|
||||
*
|
||||
* @param int $id_scheduled : Scheduled id
|
||||
* @param string $number : Number
|
||||
*
|
||||
* @return mixed (bool|int) : False on error, new row id else
|
||||
*/
|
||||
public function insert_scheduled_number(int $id_scheduled, string $number)
|
||||
{
|
||||
$success = $this->_insert('scheduled_number', ['id_scheduled' => $id_scheduled, 'number' => $number]);
|
||||
|
||||
return $success ? $this->_last_id() : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a relation between a scheduled and a contact.
|
||||
*
|
||||
* @param int $id_scheduled : Scheduled id
|
||||
* @param int $id_contact : Group id
|
||||
*
|
||||
* @return mixed (bool|int) : False on error, new row id else
|
||||
*/
|
||||
public function insert_scheduled_contact_relation(int $id_scheduled, int $id_contact)
|
||||
{
|
||||
$success = $this->_insert('scheduled_contact', ['id_scheduled' => $id_scheduled, 'id_contact' => $id_contact]);
|
||||
|
||||
return $success ? $this->_last_id() : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a relation between a scheduled and a group.
|
||||
*
|
||||
* @param int $id_scheduled : Scheduled id
|
||||
* @param int $id_group : Group id
|
||||
*
|
||||
* @return mixed (bool|int) : False on error, new row id else
|
||||
*/
|
||||
public function insert_scheduled_group_relation(int $id_scheduled, int $id_group)
|
||||
{
|
||||
$success = $this->_insert('scheduled_group', ['id_scheduled' => $id_scheduled, 'id_group' => $id_group]);
|
||||
|
||||
return $success ? $this->_last_id() : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a relation between a scheduled and a conditional group.
|
||||
*
|
||||
* @param int $id_scheduled : Scheduled id
|
||||
* @param int $id_conditional_group : Group id
|
||||
*
|
||||
* @return mixed (bool|int) : False on error, new row id else
|
||||
*/
|
||||
public function insert_scheduled_conditional_group_relation(int $id_scheduled, int $id_conditional_group)
|
||||
{
|
||||
$success = $this->_insert('scheduled_conditional_group', ['id_scheduled' => $id_scheduled, 'id_conditional_group' => $id_conditional_group]);
|
||||
|
||||
return $success ? $this->_last_id() : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete numbers for a scheduled.
|
||||
*
|
||||
* @param int $id_scheduled : Scheduled id
|
||||
*
|
||||
* @return mixed int : Number of deleted rows
|
||||
*/
|
||||
public function delete_scheduled_numbers(int $id_scheduled)
|
||||
{
|
||||
return $this->_delete('scheduled_number', ['id_scheduled' => $id_scheduled]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete contact scheduled relations for a scheduled.
|
||||
*
|
||||
* @param int $id_scheduled : Scheduled id
|
||||
*
|
||||
* @return mixed int : Number of deleted rows
|
||||
*/
|
||||
public function delete_scheduled_contact_relations(int $id_scheduled)
|
||||
{
|
||||
return $this->_delete('scheduled_contact', ['id_scheduled' => $id_scheduled]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete group scheduled relations for a scheduled.
|
||||
*
|
||||
* @param int $id_scheduled : Scheduled id
|
||||
*
|
||||
* @return mixed int : Number of deleted rows
|
||||
*/
|
||||
public function delete_scheduled_group_relations(int $id_scheduled)
|
||||
{
|
||||
return $this->_delete('scheduled_group', ['id_scheduled' => $id_scheduled]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete conditional group scheduled relations for a scheduled.
|
||||
*
|
||||
* @param int $id_scheduled : Scheduled id
|
||||
*
|
||||
* @return mixed int : Number of deleted rows
|
||||
*/
|
||||
public function delete_scheduled_conditional_group_relations(int $id_scheduled)
|
||||
{
|
||||
return $this->_delete('scheduled_conditional_group', ['id_scheduled' => $id_scheduled]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get messages scheduled before a date for a number and a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param $date : Date before which we want messages
|
||||
* @param string $number : Number for which we want messages
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function gets_before_date_for_number_and_user(int $id_user, $date, string $number)
|
||||
{
|
||||
$query = '
|
||||
SELECT *
|
||||
FROM scheduled
|
||||
WHERE at <= :date
|
||||
AND id_user = :id_user
|
||||
AND (
|
||||
id IN (
|
||||
SELECT id_scheduled
|
||||
FROM scheduled_number
|
||||
WHERE number = :number
|
||||
)
|
||||
OR id IN (
|
||||
SELECT id_scheduled
|
||||
FROM scheduled_contact
|
||||
WHERE id_contact IN (
|
||||
SELECT id
|
||||
FROM contact
|
||||
WHERE number = :number
|
||||
)
|
||||
)
|
||||
OR id IN (
|
||||
SELECT id_scheduled
|
||||
FROM scheduled_group
|
||||
WHERE id_group IN (
|
||||
SELECT id_group
|
||||
FROM `group_contact`
|
||||
WHERE id_contact IN (
|
||||
SELECT id
|
||||
FROM contact
|
||||
WHERE number = :number
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
';
|
||||
|
||||
$params = [
|
||||
'id_user' => $id_user,
|
||||
'date' => $date,
|
||||
'number' => $number,
|
||||
];
|
||||
|
||||
return $this->_run_query($query, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get scheduleds before a date.
|
||||
*
|
||||
* @param string $date : Date to get scheduleds before
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function gets_before_date(string $date)
|
||||
{
|
||||
return $this->_select($this->get_table_name(), ['<=at' => $date]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return table name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_table_name(): string
|
||||
{
|
||||
return 'scheduled';
|
||||
}
|
||||
}
|
350
models/Sended.php
Executable file
350
models/Sended.php
Executable file
|
@ -0,0 +1,350 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* Cette classe gère les accès bdd pour les sendedes.
|
||||
*/
|
||||
class Sended extends StandardModel
|
||||
{
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
$query = '
|
||||
SELECT * FROM `' . $this->get_table_name() . '`
|
||||
WHERE origin IN (SELECT number FROM phone WHERE id_user = :id_user)
|
||||
AND id = :id
|
||||
';
|
||||
|
||||
$params = [
|
||||
'id' => $id,
|
||||
'id_user' => $id_user,
|
||||
];
|
||||
|
||||
$receiveds = $this->_run_query($query, $params);
|
||||
|
||||
return $receiveds[0] ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all entries for a user.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function gets_for_user(int $id_user)
|
||||
{
|
||||
$query = '
|
||||
SELECT * FROM `' . $this->get_table_name() . '`
|
||||
WHERE origin IN (SELECT number FROM phone WHERE id_user = :id_user)
|
||||
';
|
||||
|
||||
$params = [
|
||||
'id_user' => $id_user,
|
||||
];
|
||||
|
||||
$receiveds = $this->_run_query($query, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of sended for a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param int $limit : Max results to return
|
||||
* @param int $offset : Number of results to ignore
|
||||
*/
|
||||
public function list_for_user($id_user, $limit, $offset)
|
||||
{
|
||||
$limit = (int) $limit;
|
||||
$offset = (int) $offset;
|
||||
|
||||
$query = '
|
||||
SELECT * FROM sended
|
||||
WHERE origin IN (SELECT number FROM phone WHERE id_user = :id_user)
|
||||
LIMIT ' . $limit . ' OFFSET ' . $offset;
|
||||
|
||||
$params = [
|
||||
'id_user' => $id_user,
|
||||
];
|
||||
|
||||
return $this->_run_query($query, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of sendeds in a group of ids and for a user.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
* @param array $ids : ids of sendeds to find
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function gets_in_for_user(int $id_user, $ids)
|
||||
{
|
||||
$query = '
|
||||
SELECT * FROM sended
|
||||
WHERE origin IN (SELECT number FROM phone 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, int $id)
|
||||
{
|
||||
$query = '
|
||||
DELETE FROM sended
|
||||
WHERE id = :id
|
||||
AND origin IN (SELECT number FROM phone WHERE id_user = :id_user)
|
||||
';
|
||||
|
||||
$params = ['id_user' => $id_user, 'id' => $id];
|
||||
|
||||
return $this->_run_query($query, $params, self::ROWCOUNT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a sended sms 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, int $id, array $datas)
|
||||
{
|
||||
$params = [];
|
||||
$sets = [];
|
||||
|
||||
foreach ($datas as $label => $value)
|
||||
{
|
||||
$label = preg_replace('#[^a-zA-Z0-9_]#', '', $label);
|
||||
$params['set_' . $label] = $value;
|
||||
$sets[] = '`' . $label . '` = :set_' . $label . ' ';
|
||||
}
|
||||
|
||||
$query = '
|
||||
UPDATE `sended`
|
||||
SET ' . implode(', ', $sets) . '
|
||||
WHERE id = :id
|
||||
AND origin IN (SELECT number FROM phone WHERE id_user = :id_user)
|
||||
';
|
||||
|
||||
//If try to update origin, also check it does belong to user
|
||||
if ($sets['set_origin'] ?? false)
|
||||
{
|
||||
$query .= ' AND :set_origin IN (SELECT number FROM phone WHERE id_user = :id_user)';
|
||||
}
|
||||
|
||||
$params['id'] = $id;
|
||||
$params['id_user'] = $id_user;
|
||||
|
||||
return $this->_run_query($query, $params, self::ROWCOUNT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Count number of sended sms for user.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
*
|
||||
* @return int : Number of sended SMS for user
|
||||
*/
|
||||
public function count_for_user($id_user)
|
||||
{
|
||||
$query = '
|
||||
SELECT COUNT(id) as nb
|
||||
FROM sended
|
||||
WHERE origin IN (SELECT number FROM phone WHERE id_user = :id_user)
|
||||
';
|
||||
|
||||
$params = [
|
||||
'id_user' => $id_user,
|
||||
];
|
||||
|
||||
return $this->_run_query($query, $params)[0]['nb'] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return x last sendeds message for a user, order by date.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param int $nb_entry : Number of sendeds messages to return
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_lasts_by_date_for_user($id_user, $nb_entry)
|
||||
{
|
||||
$nb_entry = (int) $nb_entry;
|
||||
|
||||
$query = '
|
||||
SELECT *
|
||||
FROM sended
|
||||
WHERE origin IN (SELECT number FROM phone WHERE id_user = :id_user)
|
||||
ORDER BY at ASC
|
||||
LIMIT ' . $nb_entry;
|
||||
|
||||
$params = [
|
||||
'id_user' => $id_user,
|
||||
];
|
||||
|
||||
return $this->_run_query($query, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return sendeds for an destination and a user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param string $destination : Number who sent the message
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function gets_by_destination_and_user(int $id_user, string $destination)
|
||||
{
|
||||
$query = '
|
||||
SELECT *
|
||||
FROM sended
|
||||
WHERE origin IN (SELECT number FROM phone WHERE id_user = :id_user)
|
||||
AND destination = :destination
|
||||
';
|
||||
|
||||
$params = [
|
||||
'id_user' => $id_user,
|
||||
'destination' => $destination,
|
||||
];
|
||||
|
||||
return $this->_run_query($query, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return sended for an uid and an adapter.
|
||||
*
|
||||
* @param string $uid : Uid of the sended
|
||||
* @param string $adapter : Adapter used to send the message
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_by_uid_and_adapter(string $uid, string $adapter)
|
||||
{
|
||||
return $this->_select_one('sended', ['uid' => $uid, 'adapter' => $adapter]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get number of sended SMS for every date since a date for a specific user.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
* @param \DateTime $date : Date since which we want the messages
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function count_by_day_since_for_user($id_user, $date)
|
||||
{
|
||||
$query = "
|
||||
SELECT COUNT(id) as nb, DATE_FORMAT(at, '%Y-%m-%d') as at_ymd
|
||||
FROM sended
|
||||
WHERE at > :date
|
||||
AND origin IN (SELECT number FROM phone WHERE id_user = :id_user)
|
||||
GROUP BY at_ymd
|
||||
";
|
||||
|
||||
$params = [
|
||||
'date' => $date,
|
||||
'id_user' => $id_user,
|
||||
];
|
||||
|
||||
return $this->_run_query($query, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get SMS sended since a date for a user.
|
||||
*
|
||||
* @param $date : La date depuis laquelle on veux les SMS (au format 2014-10-25 20:10:05)
|
||||
* @param int $id_user : User id
|
||||
*
|
||||
* @return array : Tableau avec tous les SMS depuis la date
|
||||
*/
|
||||
public function get_since_by_date_for_user($date, $id_user)
|
||||
{
|
||||
$query = "
|
||||
SELECT *
|
||||
FROM sended
|
||||
WHERE at > STR_TO_DATE(:date, '%Y-%m-%d %h:%i:%s')
|
||||
AND origin IN (SELECT number FROM phone WHERE id_user = :id_user)
|
||||
ORDER BY at ASC";
|
||||
|
||||
$params = [
|
||||
'date' => $date,
|
||||
'id_user' => $id_user,
|
||||
];
|
||||
|
||||
return $this->_run_query($query, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find last sended message for a destination and user.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param string $destination : Destination number
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_last_for_destination_and_user(int $id_user, string $destination)
|
||||
{
|
||||
$query = '
|
||||
SELECT *
|
||||
FROM sended
|
||||
WHERE destination = :destination
|
||||
AND origin IN (SELECT number FROM phone WHERE id_user = :id_user)
|
||||
ORDER BY at DESC
|
||||
LIMIT 0,1
|
||||
';
|
||||
|
||||
$params = [
|
||||
'destination' => $destination,
|
||||
'id_user' => $id_user,
|
||||
];
|
||||
|
||||
$result = $this->_run_query($query, $params);
|
||||
|
||||
return $result[0] ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return table name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_table_name(): string
|
||||
{
|
||||
return 'sended';
|
||||
}
|
||||
}
|
39
models/Setting.php
Executable file
39
models/Setting.php
Executable file
|
@ -0,0 +1,39 @@
|
|||
<?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;
|
||||
|
||||
class Setting extends StandardModel
|
||||
{
|
||||
/**
|
||||
* Update a setting for a user by his name.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
* @param string $name : setting name
|
||||
* @param mixed $value : new value of the setting
|
||||
*
|
||||
* @return int : number of modified settings
|
||||
*/
|
||||
public function update_by_name_for_user(int $id_user, string $name, $value)
|
||||
{
|
||||
return $this->_update($this->get_table_name(), ['value' => $value], ['id_user' => $id_user, 'name' => $name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return table name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_table_name(): string
|
||||
{
|
||||
return 'setting';
|
||||
}
|
||||
}
|
38
models/SmsStop.php
Executable file
38
models/SmsStop.php
Executable file
|
@ -0,0 +1,38 @@
|
|||
<?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;
|
||||
|
||||
class SmsStop extends StandardModel
|
||||
{
|
||||
/**
|
||||
* Return a smsstop by his number and user.
|
||||
*
|
||||
* @param int $id_user : user id
|
||||
* @param string $number : phone number
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_by_number_for_user(int $id_user, string $number)
|
||||
{
|
||||
return $this->_select_one($this->get_table_name(), ['number' => $number, 'id_user' => $id_user]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return table name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_table_name(): string
|
||||
{
|
||||
return 'smsstop';
|
||||
}
|
||||
}
|
197
models/StandardModel.php
Executable file
197
models/StandardModel.php
Executable file
|
@ -0,0 +1,197 @@
|
|||
<?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 all the entries.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_all()
|
||||
{
|
||||
return $this->_select($this->get_table_name());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an entry by his id.
|
||||
*
|
||||
* @param int $id : entry id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
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)
|
||||
{
|
||||
return $this->_select_one($this->get_table_name(), ['id' => $id, 'id_user' => $id_user]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
if (!$ids)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
$query = '
|
||||
SELECT * FROM `' . $this->get_table_name() . '`
|
||||
WHERE id_user = :id_user
|
||||
AND id ';
|
||||
|
||||
$params = [];
|
||||
|
||||
$generated_in = $this->_generate_in_from_array($ids);
|
||||
$query .= $generated_in['QUERY'];
|
||||
$params = $generated_in['PARAMS'];
|
||||
|
||||
//On génère la clause IN et les paramètres adaptés depuis le tableau des id
|
||||
$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, int $id)
|
||||
{
|
||||
return $this->_delete($this->get_table_name(), ['id_user' => $id_user, 'id' => $id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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, int $id, array $entry)
|
||||
{
|
||||
return $this->_update($this->get_table_name(), $entry, ['id_user' => $id_user, 'id' => $id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a entry by his id.
|
||||
*
|
||||
* @param int $id : Entry id
|
||||
* @param array $datas : datas to update
|
||||
*
|
||||
* @return int : number of modified rows
|
||||
*/
|
||||
public function update(int $id, array $entry)
|
||||
{
|
||||
return $this->_update($this->get_table_name(), $entry, ['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]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return table name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract protected function get_table_name(): string;
|
||||
}
|
131
models/User.php
Executable file
131
models/User.php
Executable file
|
@ -0,0 +1,131 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* Class for user table administration. Not a standard model because has absolutly no user based restrictions.
|
||||
*/
|
||||
class User extends \descartes\Model
|
||||
{
|
||||
/**
|
||||
* Find a user by his id.
|
||||
*
|
||||
* @param string $id : User id
|
||||
*
|
||||
* @return mixed array
|
||||
*/
|
||||
public function get($id)
|
||||
{
|
||||
return $this->_select_one('user', ['id' => $id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a user using his email.
|
||||
*
|
||||
* @param string $email : User email
|
||||
*
|
||||
* @return mixed array
|
||||
*/
|
||||
public function get_by_email($email)
|
||||
{
|
||||
return $this->_select_one('user', ['email' => $email]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a user by his api_key address.
|
||||
*
|
||||
* @param string $api_key : User api key
|
||||
*
|
||||
* @return mixed boolean | array : false if cannot find user for this api key, the user else
|
||||
*/
|
||||
public function get_by_api_key(string $api_key)
|
||||
{
|
||||
return $this->_select_one('user', ['api_key' => $api_key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return list of user.
|
||||
*
|
||||
* @param int $limit : Number of user to return
|
||||
* @param int $offset : Number of user to skip
|
||||
*/
|
||||
public function list($limit, $offset)
|
||||
{
|
||||
return $this->_select('user', [], null, false, $limit, $offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retourne une liste de useres sous forme d'un tableau.
|
||||
*
|
||||
* @param array $ids : un ou plusieurs id d'entrées à supprimer
|
||||
* @param mixed $id
|
||||
*
|
||||
* @return int : Le nombre de lignes supprimées
|
||||
*/
|
||||
public function remove($id)
|
||||
{
|
||||
return $this->_delete('user', ['id' => $id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a new user.
|
||||
*
|
||||
* @param array $user : User to insert
|
||||
*
|
||||
* @return mixed bool|int : false if fail, new user id else
|
||||
*/
|
||||
public function insert($user)
|
||||
{
|
||||
$success = $this->_insert('user', $user);
|
||||
|
||||
return $success ? $this->_last_id() : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a user using his is.
|
||||
*
|
||||
* @param int $id : User id
|
||||
* @param array $datas : Datas to update
|
||||
*
|
||||
* @return int : number of modified rows
|
||||
*/
|
||||
public function update($id, $datas)
|
||||
{
|
||||
return $this->_update('user', $datas, ['id' => $id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a user password by his id.
|
||||
*
|
||||
* @param int $id : User id
|
||||
* @param array $password : The new password of the user
|
||||
*
|
||||
* @return int : Number of modified lines
|
||||
*/
|
||||
public function update_password($id, $password)
|
||||
{
|
||||
return $this->_update('user', ['password' => $password], ['id' => $id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a user email by his id.
|
||||
*
|
||||
* @param int $id : User id
|
||||
* @param array $email : The new email
|
||||
*
|
||||
* @return int : Number of modified lines
|
||||
*/
|
||||
public function update_email($id, $email)
|
||||
{
|
||||
return $this->_update('user', ['email' => $email], ['id' => $id]);
|
||||
}
|
||||
}
|
38
models/Webhook.php
Executable file
38
models/Webhook.php
Executable file
|
@ -0,0 +1,38 @@
|
|||
<?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;
|
||||
|
||||
class Webhook extends StandardModel
|
||||
{
|
||||
/**
|
||||
* Find all webhooks for a user and for a type of webhook.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param string $type : Webhook type
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function gets_for_type_and_user(int $id_user, string $type)
|
||||
{
|
||||
return $this->_select($this->get_table_name(), ['id_user' => $id_user, 'type' => $type]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return table name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_table_name(): string
|
||||
{
|
||||
return 'webhook';
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue