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-11-13 03:24:22 +01:00
class Scheduled extends StandardModel
2019-10-29 14:57:13 +01:00
{
/**
2020-01-17 18:19:25 +01:00
* Return numbers for a scheduled message .
*
2019-11-13 03:24:22 +01:00
* @ param int $id_scheduled : Scheduled id
2020-01-17 18:19:25 +01:00
*
2019-11-13 03:24:22 +01:00
* @ return array
*/
2019-11-14 02:02:50 +01:00
public function get_numbers ( int $id_scheduled )
2019-11-13 03:24:22 +01:00
{
return $this -> _select ( 'scheduled_number' , [ 'id_scheduled' => $id_scheduled ]);
2019-10-29 14:57:13 +01:00
}
2019-10-29 18:33:49 +01:00
/**
2020-01-17 18:19:25 +01:00
* Return contacts for a scheduled message .
*
2019-11-13 03:24:22 +01:00
* @ param int $id_scheduled : Scheduled id
2020-01-17 18:19:25 +01:00
*
2019-11-13 03:24:22 +01:00
* @ return array
2019-10-29 18:33:49 +01:00
*/
2019-11-14 02:02:50 +01:00
public function get_contacts ( int $id_scheduled )
2019-10-29 14:57:13 +01:00
{
2019-11-13 03:24:22 +01:00
$query = 'SELECT * FROM contact WHERE id IN (SELECT id_contact FROM scheduled_contact WHERE id_scheduled = :id_scheduled)' ;
$params = [ 'id_scheduled' => $id_scheduled ];
2020-01-17 18:19:25 +01:00
2019-11-13 03:24:22 +01:00
return $this -> _run_query ( $query , $params );
2019-10-29 18:33:49 +01:00
}
2019-10-30 00:30:39 +01:00
2019-10-29 14:57:13 +01:00
/**
2020-01-17 18:19:25 +01:00
* Return groups for a scheduled message .
*
2019-11-13 03:24:22 +01:00
* @ param int $id_scheduled : Scheduled id
2020-01-17 18:19:25 +01:00
*
2019-11-13 03:24:22 +01:00
* @ return array
2019-10-29 18:33:49 +01:00
*/
2019-11-14 02:02:50 +01:00
public function get_groups ( int $id_scheduled )
2019-10-29 14:57:13 +01:00
{
2019-11-13 03:24:22 +01:00
$query = 'SELECT * FROM `group` WHERE id IN (SELECT id_group FROM scheduled_group WHERE id_scheduled = :id_scheduled)' ;
$params = [ 'id_scheduled' => $id_scheduled ];
2020-01-17 18:19:25 +01:00
2019-11-13 03:24:22 +01:00
return $this -> _run_query ( $query , $params );
}
2020-01-17 18:19:25 +01:00
2019-11-28 01:55:11 +01:00
/**
2020-01-17 18:19:25 +01:00
* Return conitional groups for a scheduled message .
*
2019-11-28 01:55:11 +01:00
* @ param int $id_scheduled : Scheduled id
2020-01-17 18:19:25 +01:00
*
2019-11-28 01:55:11 +01:00
* @ 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 ];
2020-01-17 18:19:25 +01:00
2019-11-28 01:55:11 +01:00
return $this -> _run_query ( $query , $params );
}
2019-10-30 00:30:39 +01:00
2019-11-13 03:24:22 +01:00
/**
2020-01-17 18:19:25 +01:00
* Insert a number for a scheduled .
*
* @ param int $id_scheduled : Scheduled id
* @ param string $number : Number
2022-03-15 02:24:28 +01:00
* @ param string $data : Data to link to number
2020-01-17 18:19:25 +01:00
*
2019-11-13 03:24:22 +01:00
* @ return mixed ( bool | int ) : False on error , new row id else
*/
2022-03-15 02:24:28 +01:00
public function insert_scheduled_number ( int $id_scheduled , string $number , string $data )
2019-11-13 03:24:22 +01:00
{
2022-03-15 02:24:28 +01:00
$success = $this -> _insert ( 'scheduled_number' , [ 'id_scheduled' => $id_scheduled , 'number' => $number , 'data' => $data ]);
2020-01-17 18:19:25 +01:00
return $success ? $this -> _last_id () : false ;
2019-11-13 03:24:22 +01:00
}
2020-01-17 18:19:25 +01:00
2019-11-13 03:24:22 +01:00
/**
2020-01-17 18:19:25 +01:00
* Insert a relation between a scheduled and a contact .
*
2019-11-13 03:24:22 +01:00
* @ param int $id_scheduled : Scheduled id
2020-01-17 18:19:25 +01:00
* @ param int $id_contact : Group id
*
2019-11-13 03:24:22 +01:00
* @ 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 ]);
2019-10-29 14:57:13 +01:00
2020-01-17 18:19:25 +01:00
return $success ? $this -> _last_id () : false ;
}
2019-11-13 03:24:22 +01:00
2019-11-06 18:32:07 +01:00
/**
2020-01-17 18:19:25 +01:00
* Insert a relation between a scheduled and a group .
*
2019-11-13 03:24:22 +01:00
* @ param int $id_scheduled : Scheduled id
2020-01-17 18:19:25 +01:00
* @ param int $id_group : Group id
*
2019-11-13 03:24:22 +01:00
* @ 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 ]);
2020-01-17 18:19:25 +01:00
return $success ? $this -> _last_id () : false ;
2019-11-13 03:24:22 +01:00
}
2020-01-17 18:19:25 +01:00
2019-11-28 01:55:11 +01:00
/**
2020-01-17 18:19:25 +01:00
* Insert a relation between a scheduled and a conditional group .
*
* @ param int $id_scheduled : Scheduled id
2019-11-28 01:55:11 +01:00
* @ param int $id_conditional_group : Group id
2020-01-17 18:19:25 +01:00
*
2019-11-28 01:55:11 +01:00
* @ 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 ]);
2020-01-17 18:19:25 +01:00
return $success ? $this -> _last_id () : false ;
2019-11-28 01:55:11 +01:00
}
2020-01-17 18:19:25 +01:00
2019-11-13 03:24:22 +01:00
/**
2020-01-17 18:19:25 +01:00
* Delete numbers for a scheduled .
*
2019-11-13 03:24:22 +01:00
* @ param int $id_scheduled : Scheduled id
2020-01-17 18:19:25 +01:00
*
2019-11-13 03:24:22 +01:00
* @ return mixed int : Number of deleted rows
2019-11-06 18:32:07 +01:00
*/
2019-11-13 03:24:22 +01:00
public function delete_scheduled_numbers ( int $id_scheduled )
2019-11-06 18:32:07 +01:00
{
2019-11-13 03:24:22 +01:00
return $this -> _delete ( 'scheduled_number' , [ 'id_scheduled' => $id_scheduled ]);
}
2020-01-17 18:19:25 +01:00
2019-11-13 03:24:22 +01:00
/**
2020-01-17 18:19:25 +01:00
* Delete contact scheduled relations for a scheduled .
*
2019-11-13 03:24:22 +01:00
* @ param int $id_scheduled : Scheduled id
2020-01-17 18:19:25 +01:00
*
2019-11-13 03:24:22 +01:00
* @ 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 ]);
}
2020-01-17 18:19:25 +01:00
2019-11-13 03:24:22 +01:00
/**
2020-01-17 18:19:25 +01:00
* Delete group scheduled relations for a scheduled .
*
2019-11-13 03:24:22 +01:00
* @ param int $id_scheduled : Scheduled id
2020-01-17 18:19:25 +01:00
*
2019-11-13 03:24:22 +01:00
* @ 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 ]);
2019-11-06 18:32:07 +01:00
}
2020-01-17 18:19:25 +01:00
2019-11-28 01:55:11 +01:00
/**
2020-01-17 18:19:25 +01:00
* Delete conditional group scheduled relations for a scheduled .
*
2019-11-28 01:55:11 +01:00
* @ param int $id_scheduled : Scheduled id
2020-01-17 18:19:25 +01:00
*
2019-11-28 01:55:11 +01:00
* @ 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 ]);
}
2019-11-06 18:32:07 +01:00
2019-10-29 18:33:49 +01:00
/**
2020-01-17 18:19:25 +01:00
* Get messages scheduled before a date for a number and a user .
*
2019-11-13 03:24:22 +01:00
* @ param int $id_user : User id
* @ param $date : Date before which we want messages
* @ param string $number : Number for which we want messages
2020-01-17 18:19:25 +01:00
*
2019-11-13 03:24:22 +01:00
* @ return array
2019-10-29 14:57:13 +01:00
*/
2020-01-17 18:19:25 +01:00
public function gets_before_date_for_number_and_user ( int $id_user , $date , string $number )
2019-10-29 18:33:49 +01:00
{
2019-11-13 03:24:22 +01:00
$query = '
2019-10-29 14:57:13 +01:00
SELECT *
FROM scheduled
WHERE at <= : date
2019-11-13 03:24:22 +01:00
AND id_user = : id_user
2019-10-29 14:57:13 +01:00
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
2019-10-30 00:17:10 +01:00
FROM scheduled_group
2019-10-29 14:57:13 +01:00
WHERE id_group IN (
SELECT id_group
2019-11-10 17:32:59 +01:00
FROM `group_contact`
2021-04-13 02:01:03 +02:00
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 );
}
2021-06-17 00:51:33 +02:00
2021-04-13 02:01:03 +02:00
/**
* Get messages scheduled after a date for a number and a user .
*
* @ param int $id_user : User id
* @ param $date : Date after which we want messages
* @ param string $number : Number for which we want messages
*
* @ return array
*/
public function gets_after_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`
2019-10-29 14:57:13 +01:00
WHERE id_contact IN (
SELECT id
FROM contact
WHERE number = : number
)
)
)
)
2019-10-30 00:30:39 +01:00
' ;
$params = [
2019-11-13 03:24:22 +01:00
'id_user' => $id_user ,
2019-10-29 14:57:13 +01:00
'date' => $date ,
'number' => $number ,
2019-10-30 00:30:39 +01:00
];
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-12-12 00:56:30 +01:00
/**
2020-01-17 18:19:25 +01:00
* Get scheduleds before a date .
*
2019-12-12 00:56:30 +01:00
* @ param string $date : Date to get scheduleds before
2020-01-17 18:19:25 +01:00
*
2019-12-12 00:56:30 +01:00
* @ return array
*/
2020-01-17 18:19:25 +01:00
public function gets_before_date ( string $date )
2019-12-12 00:56:30 +01:00
{
return $this -> _select ( $this -> get_table_name (), [ '<=at' => $date ]);
}
2020-01-17 18:19:25 +01:00
/**
* Return table name .
*/
protected function get_table_name () : string
{
return 'scheduled' ;
}
2019-10-29 14:57:13 +01:00
}