raspisms/models/Media.php

279 lines
7.5 KiB
PHP
Raw Permalink Normal View History

2019-12-04 03:04: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;
/**
* Cette classe gère les accès bdd pour les mediaes.
*/
class Media extends StandardModel
{
/**
* Return all medias for a scheduled.
2020-01-17 18:19:25 +01:00
*
* @param int $id_scheduled : scheduled id
2020-01-17 18:19:25 +01:00
*
2019-12-04 03:04:45 +01:00
* @return array
*/
public function gets_for_scheduled(int $id_scheduled)
2019-12-04 03:04:45 +01:00
{
2021-01-14 03:32:17 +01:00
$query = '
SELECT m.id as id, m.id_user as id_user, m.path as path
FROM `' . $this->get_table_name() . '` as m
INNER JOIN media_scheduled as ms
ON m.id = ms.id_media
WHERE ms.id_scheduled = :id_scheduled
2019-12-04 03:04:45 +01:00
';
$params = [
'id_scheduled' => $id_scheduled,
2019-12-04 03:04:45 +01:00
];
return $this->_run_query($query, $params);
2019-12-04 03:04:45 +01:00
}
2021-06-17 00:51:33 +02:00
2019-12-04 03:04:45 +01:00
/**
* Return all medias for a sended.
2020-01-17 18:19:25 +01:00
*
* @param int $id_sended : sended id
2020-01-17 18:19:25 +01:00
*
2019-12-04 03:04:45 +01:00
* @return array
*/
public function gets_for_sended(int $id_sended)
2019-12-04 03:04:45 +01:00
{
2021-01-14 03:32:17 +01:00
$query = '
SELECT m.id as id, m.id_user as id_user, m.path as path
FROM `' . $this->get_table_name() . '` as m
INNER JOIN media_sended as ms
ON m.id = ms.id_media
WHERE ms.id_sended = :id_sended
2019-12-04 03:04:45 +01:00
';
$params = [
'id_sended' => $id_sended,
2019-12-04 03:04:45 +01:00
];
return $this->_run_query($query, $params);
2019-12-04 03:04:45 +01:00
}
2021-06-17 00:51:33 +02:00
2019-12-04 03:04:45 +01:00
/**
* Return all medias for a received.
2020-01-17 18:19:25 +01:00
*
* @param int $id_received : received id
2020-01-17 18:19:25 +01:00
*
2019-12-04 03:04:45 +01:00
* @return array
*/
public function gets_for_received(int $id_received)
2019-12-04 03:04:45 +01:00
{
2021-01-14 03:32:17 +01:00
$query = '
SELECT m.id as id, m.id_user as id_user, m.path as path
FROM `' . $this->get_table_name() . '` as m
INNER JOIN media_received as mr
ON m.id = mr.id_media
WHERE mr.id_received = :id_received
2019-12-04 03:04:45 +01:00
';
$params = [
'id_received' => $id_received,
2019-12-04 03:04:45 +01:00
];
return $this->_run_query($query, $params);
2019-12-04 03:04:45 +01:00
}
2021-06-17 00:51:33 +02:00
2019-12-04 03:04:45 +01:00
/**
2021-06-17 00:51:33 +02:00
* Link a media to a scheduled.
*
* @param int $id_media : Media id
* @param int $id_scheduled : Scheduled id
2020-01-17 18:19:25 +01:00
*
* @return bool | int
2019-12-04 03:04:45 +01:00
*/
2021-06-17 00:51:33 +02:00
public function insert_media_scheduled(int $id_media, int $id_scheduled)
2019-12-04 03:04:45 +01:00
{
$entry = [
'id_media' => $id_media,
'id_scheduled' => $id_scheduled,
2019-12-04 03:04:45 +01:00
];
return $this->_insert('media_scheduled', $entry) ? $this->_last_id() : false;
2019-12-04 03:04:45 +01:00
}
2021-06-17 00:51:33 +02:00
2019-12-04 03:04:45 +01:00
/**
2021-06-17 00:51:33 +02:00
* Link a media to a received.
*
* @param int $id_media : Media id
* @param int $id_received : Scheduled id
2020-01-17 18:19:25 +01:00
*
* @return bool | int
2019-12-04 03:04:45 +01:00
*/
2021-06-17 00:51:33 +02:00
public function insert_media_received(int $id_media, int $id_received)
2019-12-04 03:04:45 +01:00
{
$entry = [
'id_media' => $id_media,
'id_received' => $id_received,
];
2019-12-04 03:04:45 +01:00
return $this->_insert('media_received', $entry) ? $this->_last_id() : false;
2019-12-04 03:04:45 +01:00
}
2021-06-17 00:51:33 +02:00
2019-12-04 03:04:45 +01:00
/**
2021-06-17 00:51:33 +02:00
* Link a media to a sended.
*
* @param int $id_media : Media id
* @param int $id_sended : Scheduled id
2020-01-17 18:19:25 +01:00
*
* @return bool | int
2019-12-04 03:04:45 +01:00
*/
2021-06-17 00:51:33 +02:00
public function insert_media_sended(int $id_media, int $id_sended)
2019-12-04 03:04:45 +01:00
{
$entry = [
'id_media' => $id_media,
'id_sended' => $id_sended,
];
2020-01-17 18:19:25 +01:00
return $this->_insert('media_sended', $entry) ? $this->_last_id() : false;
2019-12-04 03:04:45 +01:00
}
2021-06-17 00:51:33 +02:00
2019-12-04 03:04:45 +01:00
/**
2021-06-17 00:51:33 +02:00
* Unlink a media of a scheduled.
*
* @param int $id_media : Media id
2019-12-04 03:04:45 +01:00
* @param int $id_scheduled : Scheduled id
2020-01-17 18:19:25 +01:00
*
* @return bool | int
2019-12-04 03:04:45 +01:00
*/
2021-06-17 00:51:33 +02:00
public function delete_media_scheduled(int $id_media, int $id_scheduled)
2019-12-04 03:04:45 +01:00
{
$where = [
'id_media' => $id_media,
'id_scheduled' => $id_scheduled,
];
2020-01-17 18:19:25 +01:00
return $this->_delete('media_scheduled', $where);
2019-12-04 03:04:45 +01:00
}
/**
2021-06-17 00:51:33 +02:00
* Unlink a media of a received.
*
* @param int $id_media : Media id
* @param int $id_received : Scheduled id
2019-12-04 03:04:45 +01:00
*
* @return bool | int
2019-12-04 03:04:45 +01:00
*/
2021-06-17 00:51:33 +02:00
public function delete_media_received(int $id_media, int $id_received)
2019-12-04 03:04:45 +01:00
{
$where = [
'id_media' => $id_media,
'id_received' => $id_received,
];
2019-12-04 03:04:45 +01:00
return $this->_delete('media_received', $where);
}
2021-06-17 00:51:33 +02:00
/**
2021-06-17 00:51:33 +02:00
* Unlink a media of a sended.
*
* @param int $id_media : Media id
* @param int $id_sended : Scheduled id
*
* @return bool | int
*/
2021-06-17 00:51:33 +02:00
public function delete_media_sended(int $id_media, int $id_sended)
{
$where = [
'id_media' => $id_media,
'id_sended' => $id_sended,
];
2019-12-04 03:04:45 +01:00
return $this->_delete('media_sended', $where);
}
2021-06-17 00:51:33 +02:00
/**
2021-06-17 00:51:33 +02:00
* Unlink all medias of a scheduled.
*
* @param int $id_scheduled : Scheduled id
*
* @return bool | int
*/
2021-06-17 00:51:33 +02:00
public function delete_all_for_scheduled(int $id_scheduled)
{
$where = [
'id_scheduled' => $id_scheduled,
];
2019-12-04 03:04:45 +01:00
return $this->_delete('media_scheduled', $where);
}
2021-06-17 00:51:33 +02:00
/**
2021-06-17 00:51:33 +02:00
* Unlink all medias of a received.
*
* @param int $id_received : Scheduled id
*
* @return bool | int
*/
2021-06-17 00:51:33 +02:00
public function delete_all_for_received(int $id_received)
{
$where = [
'id_received' => $id_received,
];
2019-12-04 03:04:45 +01:00
return $this->_delete('media_received', $where);
2019-12-04 03:04:45 +01:00
}
2020-01-17 18:19:25 +01:00
2019-12-04 03:04:45 +01:00
/**
2021-06-17 00:51:33 +02:00
* Unlink all medias of a sended.
*
* @param int $id_sended : Scheduled id
2020-01-17 18:19:25 +01:00
*
* @return bool | int
2019-12-04 03:04:45 +01:00
*/
2021-06-17 00:51:33 +02:00
public function delete_all_for_sended(int $id_sended)
2019-12-04 03:04:45 +01:00
{
$where = [
'id_sended' => $id_sended,
2019-12-04 03:04:45 +01:00
];
return $this->_delete('media_sended', $where);
2019-12-04 03:04:45 +01:00
}
2020-01-17 18:19:25 +01:00
2021-03-26 18:53:20 +01:00
/**
2021-06-17 00:51:33 +02:00
* Find all unused medias.
*
2021-03-26 18:53:20 +01:00
* @return array
*/
2021-06-17 00:51:33 +02:00
public function gets_unused()
2021-03-26 18:53:20 +01:00
{
$query = '
SELECT `media`.*
FROM `media`
LEFT JOIN `media_sended`
ON `media`.id = `media_sended`.id_media
LEFT JOIN `media_received`
ON `media`.id = `media_received`.id_media
LEFT JOIN `media_scheduled`
ON `media`.id = `media_scheduled`.id_media
WHERE `media_sended`.id IS NULL
AND `media_received`.id IS NULL
AND `media_scheduled`.id IS NULL
';
2021-06-17 00:51:33 +02:00
2021-03-26 18:53:20 +01:00
return $this->_run_query($query);
}
2020-01-17 18:19:25 +01:00
/**
* Return table name.
*/
protected function get_table_name(): string
{
return 'media';
}
2019-12-04 03:04:45 +01:00
}