add medias to scheduled creation

This commit is contained in:
osaajani 2021-03-22 01:29:06 +01:00
parent f330312b55
commit 3fff7e0abd
5 changed files with 157 additions and 87 deletions

View File

@ -33,6 +33,36 @@ namespace controllers\internals;
return $this->get_model()->insert($data);
}
/**
* Upload and create a media
*
* @param int $id_user : Id of the user
* @param array $file : array representing uploaded file, extracted from $_FILES['yourfile']
* @return mixed bool | int : False on error, or new media id on success
*/
public function upload_and_create_for_user(int $id_user, array $file)
{
$user_media_path = PWD_DATA . '/medias/' . $id_user;
//Create user medias dir if not exists
if (!file_exists($user_media_path))
{
if (!mkdir($user_media_path, fileperms(PWD_DATA), true))
{
return false;
}
}
$upload_result = \controllers\internals\Tool::save_uploaded_file($file, $user_media_path);
if ($upload_result['success'] !== true)
{
return false;
}
$new_filepath = 'medias/' . $id_user . '/' . $upload_result['content'];
return $this->create($id_user, $new_filepath);
}
/**
* Link a media to a scheduled, a received or a sended message
* @param int $id_media : Id of the media
@ -99,7 +129,7 @@ namespace controllers\internals;
*
* @return mixed bool : false on error, true on success
*/
public function unlink_all_of(int $resource_type, int $resource_id)
public function unlink_all_of(string $resource_type, int $resource_id)
{
switch ($resource_type)
{

View File

@ -362,40 +362,12 @@ namespace controllers\publics;
{
foreach ($files_arrays as $file)
{
$user_media_path = PWD_DATA . '/medias/' . $this->user['id'];
//Create user medias dir if not exists
if (!file_exists($user_media_path))
{
if (!mkdir($user_media_path, fileperms(PWD_DATA), true))
{
$return = self::DEFAULT_RETURN;
$return['error'] = self::ERROR_CODES['CANNOT_UPLOAD_FILE'];
$return['message'] = self::ERROR_MESSAGES['CANNOT_UPLOAD_FILE'] . ' : Because cannot create medias dir on server for the user.';
$this->auto_http_code(false);
return $this->json($return);
}
}
$result = \controllers\internals\Tool::save_uploaded_file($file, $user_media_path);
if ($result['success'] !== true)
{
$return = self::DEFAULT_RETURN;
$return['error'] = self::ERROR_CODES['CANNOT_UPLOAD_FILE'];
$return['message'] = self::ERROR_MESSAGES['CANNOT_UPLOAD_FILE'] . $file['name'] . ' with error : ' . $result['content'];
$this->auto_http_code(false);
return $this->json($return);
}
$new_filepath = 'medias/' . $this->user['id'] . '/' . $result['content'];
$new_media_id = $this->internal_media->create($this->user['id'], $new_filepath);
$new_media_id = $this->internal_media->upload_and_create_for_user($this->user['id'], $file);
if (!$new_media_id)
{
$return = self::DEFAULT_RETURN;
$return['error'] = self::ERROR_CODES['CANNOT_CREATE'];
$return['message'] = self::ERROR_MESSAGES['CANNOT_CREATE'];
$return['message'] = self::ERROR_MESSAGES['CANNOT_CREATE'] . ' : Cannot upload and create media file ' . $file['name'];
$this->auto_http_code(false);
return $this->json($return);

View File

@ -212,8 +212,8 @@ namespace controllers\publics;
$scheduleds[$key]['groups'][] = (int) $group['id'];
}
$media = $this->internal_media->get_for_scheduled_and_user($id_user, $scheduled['id']);
$scheduleds[$key]['media'] = $media;
$medias = $this->internal_media->gets_for_scheduled($scheduled['id']);
$scheduleds[$key]['medias'] = $medias;
$conditional_groups = $this->internal_scheduled->get_conditional_groups($scheduled['id']);
foreach ($conditional_groups as $conditional_group)
@ -242,7 +242,7 @@ namespace controllers\publics;
* @param ?array $_POST['contacts'] : Numbers to send the message to
* @param ?array $_POST['groups'] : Numbers to send the message to
* @param ?array $_POST['conditional_groups'] : Numbers to send the message to
* @param ?array $_FILES['media'] : The media to link to a scheduled
* @param ?array $_FILES['medias'] : The media to link to a scheduled
*/
public function create($csrf)
{
@ -257,13 +257,39 @@ namespace controllers\publics;
$at = $_POST['at'] ?? false;
$text = $_POST['text'] ?? false;
$flash = (bool) ($_POST['flash'] ?? false);
$mms = $_FILES['media'] ?? false;
$id_phone = empty($_POST['id_phone']) ? null : $_POST['id_phone'];
$numbers = $_POST['numbers'] ?? [];
$contacts = $_POST['contacts'] ?? [];
$groups = $_POST['groups'] ?? [];
$conditional_groups = $_POST['conditional_groups'] ?? [];
$media = $_FILES['media'] ?? false;
$files = $_FILES['medias'] ?? false;
//Iterate over files to re-create individual $_FILES array
$files_arrays = [];
if ($files && is_array($files['name']))
{
foreach ($files as $property_name => $files_values)
{
foreach ($files_values as $file_key => $property_value)
{
if (!isset($files_arrays[$file_key]))
{
$files_arrays[$file_key] = [];
}
$files_arrays[$file_key][$property_name] = $property_value;
}
}
}
//Remove empty files input
foreach ($files_arrays as $key => $file)
{
if ($file['error'] === UPLOAD_ERR_NO_FILE)
{
unset($files_arrays[$key]);
}
}
if (empty($text))
{
@ -299,8 +325,27 @@ namespace controllers\publics;
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
}
//If mms is enable and we have medias uploaded
$media_ids = [];
if ($_SESSION['user']['settings']['mms'] && $files_arrays)
{
foreach ($files_arrays as $file)
{
$new_media_id = $this->internal_media->upload_and_create_for_user($_SESSION['user']['id'], $file);
if (!$new_media_id)
{
\FlashMessage\FlashMessage::push('danger', 'Impossible d\'upload et d\'enregistrer le fichier ' . $file['name']);
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
}
$scheduled_id = $this->internal_scheduled->create($id_user, $at, $text, $id_phone, $flash, $mms, $numbers, $contacts, $groups, $conditional_groups);
$media_ids[] = $new_media_id;
}
}
$mms = (bool) count($media_ids);
$scheduled_id = $this->internal_scheduled->create($id_user, $at, $text, $id_phone, $flash, $mms, $numbers, $contacts, $groups, $conditional_groups, $media_ids);
if (!$scheduled_id)
{
\FlashMessage\FlashMessage::push('danger', 'Impossible de créer le Sms.');
@ -308,22 +353,6 @@ namespace controllers\publics;
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
}
//If mms is disabled or no media uploaded, do not process
if (!($_SESSION['user']['settings']['mms'] ?? false) || !$media)
{
\FlashMessage\FlashMessage::push('success', 'Le Sms a bien été créé pour le ' . $at . '.');
return $this->redirect(\descartes\Router::url('Scheduled', 'list'));
}
$success = $this->internal_media->create($id_user, $scheduled_id, $media);
if (!$success)
{
\FlashMessage\FlashMessage::push('success', 'Le SMS a bien été créé mais le média n\'as pas pu être enregistré.');
return $this->redirect(\descartes\Router::url('Scheduled', 'list'));
}
\FlashMessage\FlashMessage::push('success', 'Le Sms a bien été créé pour le ' . $at . '.');
return $this->redirect(\descartes\Router::url('Scheduled', 'list'));
@ -360,13 +389,43 @@ namespace controllers\publics;
$contacts = $scheduled['contacts'] ?? [];
$groups = $scheduled['groups'] ?? [];
$conditional_groups = $scheduled['conditional_groups'] ?? [];
$files = $_FILES['scheduleds_' . $id_scheduled . '_medias'] ?? false;
$media_ids = $scheduled['media_ids'] ?? [];
//Check scheduled exists and belong to user
$scheduled = $this->internal_scheduled->get($id_scheduled);
if (!$scheduled || $scheduled['id_user'] !== $id_user)
{
continue;
}
//Iterate over files to re-create individual $_FILES array
$files_arrays = [];
if ($files && is_array($files['name']))
{
foreach ($files as $property_name => $files_values)
{
foreach ($files_values as $file_key => $property_value)
{
if (!isset($files_arrays[$file_key]))
{
$files_arrays[$file_key] = [];
}
$files_arrays[$file_key][$property_name] = $property_value;
}
}
}
//Remove empty files input
foreach ($files_arrays as $key => $file)
{
if ($file['error'] === UPLOAD_ERR_NO_FILE)
{
unset($files_arrays[$key]);
}
}
if (empty($text))
{
continue;
@ -394,32 +453,36 @@ namespace controllers\publics;
{
continue;
}
$success = $this->internal_scheduled->update_for_user($id_user, $id_scheduled, $at, $text, $id_phone, $flash, $numbers, $contacts, $groups, $conditional_groups);
//Check for media
/*
$current_media = $scheduled['current_media'] ?? false;
if (!$current_media)
//If mms is enable and we have medias uploaded
if ($_SESSION['user']['settings']['mms'] && $files_arrays)
{
$this->internal_media->delete_for_scheduled_and_user($id_user, $id_scheduled);
foreach ($files_arrays as $file)
{
$new_media_id = $this->internal_media->upload_and_create_for_user($_SESSION['user']['id'], $file);
if (!$new_media_id)
{
continue 2;
}
$media_ids[] = $new_media_id;
}
}
$media = $_FILES['media_' . $id_scheduled] ?? false;
if (!$media)
//Ensure media_ids point to medias belongings to the current user
foreach ($media_ids as $key => $media_id)
{
$nb_update += (int) $success;
continue;
$media = $this->internal_media->get($media_id);
if (!$media || $media['id_user'] !== $_SESSION['user']['id'])
{
unset($media_ids[$key]);
}
}
$success = $this->internal_media->create($id_user, $id_scheduled, $media);
if (!$success)
{
continue;
}
*/
$mms = (bool) count($media_ids);
++$nb_update;
$this->internal_scheduled->update_for_user($id_user, $id_scheduled, $at, $text, $id_phone, $flash, $mms, $numbers, $contacts, $groups, $conditional_groups, $media_ids);
$nb_update++;
}
if ($nb_update !== \count($scheduleds))

View File

@ -64,12 +64,14 @@
</div>
<?php if ($_SESSION['user']['settings']['mms'] ?? false) { ?>
<div class="form-group">
<label>Ajouter un média</label>
<div class="form-group scheduled-media-group">
<label>Ajouter un média au SMS</label>
<p class="italic small help description-scheduled-media">
Le média sera utilisé uniquement si le téléphone utilisé supporte l'envoi de MMS. Pour plus d'information, consultez la documentation sur <a href="#">l'utilisation des MMS.</a>
L'ajout d'un média nécessite un téléphone supportant l'envoi de MMS. Pour plus d'information, consultez la documentation sur <a href="#">l'utilisation des MMS.</a>.
</p>
<input class="" name="media" value="" type="file" />
<div class="form-group">
<input class="" name="medias[]" value="" type="file" multiple />
</div>
</div>
<?php } ?>
<div class="form-group">

View File

@ -64,17 +64,21 @@
<div class="form-group">
<label>Ajouter un média</label>
<p class="italic small help description-scheduled-media">
Le média sera utilisé uniquement si le téléphone utilisé supporte l'envoi de MMS. Pour plus d'information, consultez la documentation sur <a href="#">l'utilisation des MMS.</a>
L'ajout d'un média nécessite un téléphone supportant l'envoi de MMS. Pour plus d'information, consultez la documentation sur <a href="#">l'utilisation des MMS.</a>.
</p>
<?php if ($scheduled['media']) { ?>
<div class="current-media-container">
<input type="hidden" name="scheduleds[<?php $this->s($scheduled['id']); ?>][current_media]" value="1">
<p class="inline-block">Un média est déjà lié à ce message.</p>
<a href="#" class="btn btn-warning btn-delete-media">Supprimer le média</a>
<div class="form-group">
<input class="" name="scheduleds_<?php $this->s($scheduled['id']); ?>_medias[]" value="" type="file" multiple />
</div>
<?php if ($scheduled['medias']) { ?>
<div class="current-medias-container">
<label>Médias déjà attachés au SMS</label>
<?php foreach ($scheduled['medias'] as $key => $media) { ?>
<p class="current-media">
<input type="hidden" name="scheduleds[<?php $this->s($scheduled['id']); ?>][media_ids][]" value="<?php $this->s($media['id']); ?>">
<label>Fichier <?= $key + 1 ?> :</label><br/> <a href="<?php $this->s(HTTP_PWD_DATA . '/' . $media['path']); ?>" class="btn btn-info btn-sm" target="_blank">Voir le média</a> <a href="#" class="btn btn-warning btn-delete-media btn-sm">Supprimer le média</a>
</p>
<?php } ?>
</div>
<input class="hidden" name="media_<?php $this->s($scheduled['id']); ?>" type="file" />
<?php } else { ?>
<input name="media_<?php $this->s($scheduled['id']); ?>" type="file" />
<?php } ?>
</div>
<?php } ?>
@ -246,8 +250,7 @@
jQuery('body').on('click', '.btn-delete-media', function (e)
{
e.preventDefault();
jQuery(this).parents('.form-group').find('input').removeClass('hidden');
jQuery(this).parents('.form-group').find('.current-media-container').remove();
jQuery(this).parents('.current-media').remove();
});
jQuery('body').on('click', '.preview-button', function (e)