openstamanager/src/Uploads.php

148 lines
3.9 KiB
PHP
Raw Normal View History

<?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/>.
*/
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
/**
* Classe per la gestione degli upload del progetto.
*
* @since 2.4.1
*/
class Uploads
{
/**
2019-01-04 11:39:30 +01:00
* Restituisce l'elenco degli allegati registrati per un determinato modulo/plugin e record.
*
* @param array $data
*
* @return array
*/
public static function get($data)
{
2018-09-20 12:05:22 +02:00
$database = database();
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,
'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)
{
if (empty($id_plugin)) {
2024-03-05 16:01:45 +01:00
$structure = Module::find($id_module);
} else {
2024-03-05 16:01:45 +01:00
$structure = Plugin::find($id_plugin);
}
return $structure->upload_directory;
}
/**
* Effettua l'upload di un file nella cartella indicata.
*
* @param string|array $source
* @param array $data
* @param array $options
*
* @return Upload
*/
public static function upload($source, $data, $options = [])
{
return Upload::build($source, $data);
}
/**
* Elimina l'allegato indicato.
*
* @param string $filename
* @param array $data
*
* @return string Nome del file
*/
public static function delete($filename, $data)
{
2020-06-30 13:26:15 +02:00
if (!empty($filename)) {
$database = database();
2023-06-29 11:11:41 +02:00
$file = $database->selectOne('zz_files', '*', [
'filename' => $filename,
2018-09-28 18:11:42 +02:00
'id_module' => !empty($data['id_module']) && empty($data['id_plugin']) ? $data['id_module'] : null,
'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)) {
$name = $file['name'];
2024-03-22 15:52:24 +01:00
$upload = Upload::find($file['id']);
$upload->delete();
2024-03-22 15:52:24 +01:00
return $name;
2024-03-22 15:52:24 +01:00
} else {
return null;
}
}
2020-06-30 13:26:15 +02:00
return null;
}
/**
* 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)
{
$uploads = self::get($data);
2018-07-07 14:24:59 +02:00
foreach ($uploads as $upload) {
self::delete($upload['filename'], $data);
}
}
/**
* Restituisce le informazioni relative al file indicato.
*
* @param string $filepath
*
* @return array
*/
public static function fileInfo($filepath)
{
$infos = pathinfo($filepath);
$infos['extension'] = strtolower($infos['extension']);
return $infos;
}
}