raspisms/controllers/internals/Media.php

142 lines
4.2 KiB
PHP
Raw 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 controllers\internals;
class Media extends StandardController
{
2020-01-17 18:19:25 +01:00
protected $model;
2019-12-04 03:04:45 +01:00
/**
2020-01-17 18:19:25 +01:00
* Create a media.
*
* @param int $id_user : Id of the user
* @param int $id_scheduled : Id of the scheduled
* @param array $media : $_FILES media array
*
2019-12-04 03:04:45 +01:00
* @return bool : false on error, new media id else
*/
2020-01-17 18:19:25 +01:00
public function create(int $id_user, int $id_scheduled, array $media): bool
2019-12-04 03:04:45 +01:00
{
$internal_scheduled = new Scheduled($this->bdd);
$scheduled = $internal_scheduled->get_for_user($id_user, $id_scheduled);
if (!$scheduled)
{
return false;
}
2020-01-17 18:19:25 +01:00
2019-12-04 03:04:45 +01:00
$result_upload_media = \controllers\internals\Tool::upload_file($media);
2020-01-17 18:19:25 +01:00
if (false === $result_upload_media['success'])
2019-12-04 03:04:45 +01:00
{
return false;
}
2020-01-17 18:19:25 +01:00
$datas = [
2019-12-04 03:04:45 +01:00
'id_scheduled' => $id_scheduled,
'path' => $result_upload_media['content'],
];
return (bool) $this->get_model()->insert($datas);
}
/**
2020-01-17 18:19:25 +01:00
* Update a media for a user.
*
* @param int $id_user : user id
* @param int $id_media : Media id
* @param int $id_scheduled : Id of the scheduled
* @param string $path : Path of the file
*
2019-12-04 03:04:45 +01:00
* @return bool : false on error, true on success
*/
2020-01-17 18:19:25 +01:00
public function update_for_user(int $id_user, int $id_media, int $id_scheduled, string $path): bool
2019-12-04 03:04:45 +01:00
{
2020-01-17 18:19:25 +01:00
$media = [
2019-12-04 03:04:45 +01:00
'id_scheduled' => $id_scheduled,
'path' => $path,
];
2020-01-17 18:19:25 +01:00
2019-12-04 03:04:45 +01:00
$internal_scheduled = new Scheduled($this->bdd);
$scheduled = $this->get_for_user($id_user, $id_scheduled);
if (!$scheduled)
{
return false;
}
return (bool) $this->get_model()->update_for_user($id_user, $id_media, $media);
}
2020-01-17 18:19:25 +01:00
2019-12-04 03:04:45 +01:00
/**
2020-01-17 18:19:25 +01:00
* Delete a media for a user.
*
2019-12-04 03:04:45 +01:00
* @param int $id_user : User id
2020-01-17 18:19:25 +01:00
* @param int $id : Entry id
*
2019-12-04 03:04:45 +01:00
* @return int : Number of removed rows
*/
2020-01-17 18:19:25 +01:00
public function delete_for_user(int $id_user, int $id_media): bool
2019-12-04 03:04:45 +01:00
{
$media = $this->get_model()->get_for_user($id_user, $id_media);
if (!$media)
{
return false;
}
unlink($media['path']);
return $this->get_model()->delete_for_user($id_user, $id);
}
2020-01-17 18:19:25 +01:00
2019-12-04 03:04:45 +01:00
/**
2020-01-17 18:19:25 +01:00
* Delete a media for a scheduled and a user.
*
* @param int $id_user : User id
2019-12-04 03:04:45 +01:00
* @param int $id_scheduled : Scheduled id to delete medias for
2020-01-17 18:19:25 +01:00
*
2019-12-04 03:04:45 +01:00
* @return int : Number of removed rows
*/
2020-01-17 18:19:25 +01:00
public function delete_for_scheduled_and_user(int $id_user, int $id_scheduled): bool
2019-12-04 03:04:45 +01:00
{
$media = $this->get_model()->get_for_scheduled_and_user($id_user, $id_scheduled);
if ($media)
{
unlink($media['path']);
}
return $this->get_model()->delete_for_scheduled_and_user($id_user, $id_scheduled);
}
/**
2020-01-17 18:19:25 +01:00
* Find medias for a scheduled and a user.
*
* @param int $id_user : User id
2019-12-04 03:04:45 +01:00
* @param int $id_scheduled : Scheduled id to delete medias for
2020-01-17 18:19:25 +01:00
*
2019-12-04 03:04:45 +01:00
* @return mixed : Medias || false
*/
2020-01-17 18:19:25 +01:00
public function get_for_scheduled_and_user(int $id_user, int $id_scheduled)
2019-12-04 03:04:45 +01:00
{
return $this->get_model()->get_for_scheduled_and_user($id_user, $id_scheduled);
}
2020-01-17 18:19:25 +01:00
/**
* Get the model for the Controller.
*
* @return \descartes\Model
*/
protected function get_model(): \descartes\Model
{
$this->model = $this->model ?? new \models\Media($this->bdd);
return $this->model;
}
2019-12-04 03:04:45 +01:00
}