2018-07-03 11:12:32 +02:00
|
|
|
<?php
|
2020-09-07 15:04:06 +02:00
|
|
|
/*
|
|
|
|
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
|
2021-01-20 15:08:51 +01:00
|
|
|
* Copyright (C) DevCode s.r.l.
|
2020-09-07 15:04:06 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2018-07-03 11:12:32 +02:00
|
|
|
|
2024-03-05 16:01:45 +01:00
|
|
|
use Models\Module;
|
|
|
|
use Models\Plugin;
|
2024-03-22 15:52:24 +01:00
|
|
|
use Models\Upload;
|
2020-12-29 15:45:39 +01:00
|
|
|
|
2018-07-03 11:12:32 +02:00
|
|
|
/**
|
|
|
|
* Classe per la gestione degli upload del progetto.
|
|
|
|
*
|
|
|
|
* @since 2.4.1
|
|
|
|
*/
|
|
|
|
class Uploads
|
|
|
|
{
|
2018-07-10 12:07:48 +02:00
|
|
|
/**
|
2019-01-04 11:39:30 +01:00
|
|
|
* Restituisce l'elenco degli allegati registrati per un determinato modulo/plugin e record.
|
2018-07-10 12:07:48 +02:00
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function get($data)
|
|
|
|
{
|
2018-09-20 12:05:22 +02:00
|
|
|
$database = database();
|
2018-07-03 11:12:32 +02:00
|
|
|
|
2023-09-15 18:06:15 +02:00
|
|
|
$uploads = $database->select('zz_files', '*', [], [
|
2018-08-31 11:39:38 +02:00
|
|
|
'id_module' => !empty($data['id_module']) && empty($data['id_plugin']) ? $data['id_module'] : null,
|
2018-07-10 12:07:48 +02:00
|
|
|
'id_plugin' => !empty($data['id_plugin']) ? $data['id_plugin'] : null,
|
|
|
|
'id_record' => $data['id_record'],
|
|
|
|
]);
|
|
|
|
|
|
|
|
return $uploads;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce il nome della cartella per l'upload degli allegati per un determinato modulo/plugin.
|
|
|
|
*
|
|
|
|
* @param string|int $id_module
|
|
|
|
* @param string|int $id_plugin
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-07-09 12:57:55 +02:00
|
|
|
public static function getDirectory($id_module, $id_plugin = null)
|
2018-07-03 21:22:29 +02:00
|
|
|
{
|
|
|
|
if (empty($id_plugin)) {
|
2024-03-05 16:01:45 +01:00
|
|
|
$structure = Module::find($id_module);
|
2018-07-03 21:22:29 +02:00
|
|
|
} else {
|
2024-03-05 16:01:45 +01:00
|
|
|
$structure = Plugin::find($id_plugin);
|
2018-07-03 21:22:29 +02:00
|
|
|
}
|
|
|
|
|
2019-07-18 18:33:56 +02:00
|
|
|
return $structure->upload_directory;
|
2018-07-03 21:22:29 +02:00
|
|
|
}
|
|
|
|
|
2018-07-03 11:12:32 +02:00
|
|
|
/**
|
|
|
|
* Effettua l'upload di un file nella cartella indicata.
|
|
|
|
*
|
2021-03-08 11:20:04 +01:00
|
|
|
* @param string|array $source
|
2021-03-15 16:39:32 +01:00
|
|
|
* @param array $data
|
|
|
|
* @param array $options
|
2018-07-03 11:12:32 +02:00
|
|
|
*
|
2021-03-08 11:20:04 +01:00
|
|
|
* @return Upload
|
2018-07-03 11:12:32 +02:00
|
|
|
*/
|
2018-07-03 21:22:29 +02:00
|
|
|
public static function upload($source, $data, $options = [])
|
2018-07-03 11:12:32 +02:00
|
|
|
{
|
2021-03-08 11:20:04 +01:00
|
|
|
return Upload::build($source, $data);
|
2018-07-03 11:12:32 +02:00
|
|
|
}
|
|
|
|
|
2018-07-10 12:07:48 +02:00
|
|
|
/**
|
|
|
|
* Elimina l'allegato indicato.
|
|
|
|
*
|
|
|
|
* @param string $filename
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return string Nome del file
|
|
|
|
*/
|
2018-07-03 21:22:29 +02:00
|
|
|
public static function delete($filename, $data)
|
|
|
|
{
|
2020-06-30 13:26:15 +02:00
|
|
|
if (!empty($filename)) {
|
2020-06-25 12:53:12 +02:00
|
|
|
$database = database();
|
2018-07-03 21:22:29 +02:00
|
|
|
|
2023-06-29 11:11:41 +02:00
|
|
|
$file = $database->selectOne('zz_files', '*', [
|
2020-06-25 12:53:12 +02:00
|
|
|
'filename' => $filename,
|
2018-09-28 18:11:42 +02:00
|
|
|
'id_module' => !empty($data['id_module']) && empty($data['id_plugin']) ? $data['id_module'] : null,
|
2018-07-03 21:22:29 +02:00
|
|
|
'id_plugin' => !empty($data['id_plugin']) ? $data['id_plugin'] : null,
|
|
|
|
'id_record' => $data['id_record'],
|
2023-06-29 11:11:41 +02:00
|
|
|
]);
|
2024-03-22 15:52:24 +01:00
|
|
|
|
|
|
|
if (!empty($file)) {
|
2024-03-19 13:23:08 +01:00
|
|
|
$name = $file['name'];
|
2024-03-22 15:52:24 +01:00
|
|
|
|
2024-03-19 13:23:08 +01:00
|
|
|
$upload = Upload::find($file['id']);
|
|
|
|
$upload->delete();
|
2024-03-22 15:52:24 +01:00
|
|
|
|
2020-06-25 12:53:12 +02:00
|
|
|
return $name;
|
2024-03-22 15:52:24 +01:00
|
|
|
} else {
|
2024-03-19 13:23:08 +01:00
|
|
|
return null;
|
2020-06-25 12:53:12 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-30 13:26:15 +02:00
|
|
|
|
2018-07-03 21:22:29 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-07-10 12:07:48 +02:00
|
|
|
/**
|
|
|
|
* Rimuove tutti gli allegati di un determinato modulo/plugin e record.
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
*/
|
2018-07-07 14:24:59 +02:00
|
|
|
public static function deleteLinked($data)
|
|
|
|
{
|
2018-07-10 12:07:48 +02:00
|
|
|
$uploads = self::get($data);
|
2018-07-07 14:24:59 +02:00
|
|
|
|
|
|
|
foreach ($uploads as $upload) {
|
|
|
|
self::delete($upload['filename'], $data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 12:07:48 +02:00
|
|
|
/**
|
|
|
|
* Restituisce le informazioni relative al file indicato.
|
|
|
|
*
|
|
|
|
* @param string $filepath
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-07-03 21:22:29 +02:00
|
|
|
public static function fileInfo($filepath)
|
|
|
|
{
|
|
|
|
$infos = pathinfo($filepath);
|
|
|
|
$infos['extension'] = strtolower($infos['extension']);
|
|
|
|
|
|
|
|
return $infos;
|
|
|
|
}
|
2018-07-03 11:12:32 +02:00
|
|
|
}
|