264 lines
9.8 KiB
PHP
264 lines
9.8 KiB
PHP
<?php
|
|
|
|
class StbActivityFile {
|
|
|
|
public static function get_items($activityId, $fileName = null, $profileDb = null) {
|
|
$Query = new Query;
|
|
|
|
$fields = array("file_name", "original_size", "descrizione", "id AS activity_id", "last_upd");
|
|
|
|
$Query->select($fields)
|
|
->from("stb_activity_file")
|
|
->where("id", $activityId)
|
|
->orderBy("file_name");
|
|
|
|
if (!is_null($fileName)) {
|
|
$Query->where("file_name", $fileName);
|
|
}
|
|
|
|
if ($profileDb) {
|
|
$Query->profileDB($profileDb);
|
|
}
|
|
|
|
$Ret = $Query->toRet()->execute();
|
|
if ($Ret->is_OK()) {
|
|
$arr_rows = $Ret->get_data();
|
|
foreach ($arr_rows as $i => $row) {
|
|
$mime = Utility\File::getMimeTypeFromName($row["file_name"]);
|
|
$icon = Utility\File::getFaIcoFromName($row["file_name"]);
|
|
$content = !is_null($fileName) ? self::downloadStbActivityFileAttachment($activityId, $fileName, $profileDb) : null;
|
|
|
|
$arr_rows[$i] = array_merge($arr_rows[$i], array(
|
|
"size" => $row["original_size"], // unsettare 'original_size'
|
|
"file_size" => $row["original_size"],
|
|
"content" => $content,
|
|
"mime_type" => $mime,
|
|
"user_creator" => null,
|
|
"icon" => $icon["name"],
|
|
"icon_style" => $icon["style"]
|
|
));
|
|
}
|
|
|
|
$Ret->set_data($arr_rows);
|
|
}
|
|
|
|
return $Ret;
|
|
}
|
|
|
|
public static function get_item($activityId, $fileName, $profileDb = null) {
|
|
$Ret = self::get_items($activityId, $fileName, $profileDb);
|
|
if ($Ret->is_OK()) {
|
|
$retData = $Ret->get_data();
|
|
if (count($retData) > 0) {
|
|
$Ret->set_data($retData[0]);
|
|
} else {
|
|
$Ret->set_error("Allegato non valido");
|
|
}
|
|
}
|
|
return $Ret;
|
|
}
|
|
|
|
public static function upload($activityId, $fileItems, $profileDb = null, $getItem = true) {
|
|
$Ret = new Ret;
|
|
$fileItems = \Utility\File::rearrangeFilesArray($fileItems["files"]);
|
|
$attachments = array();
|
|
|
|
foreach ($fileItems as $fileItem) {
|
|
if (file_exists($fileItem["tmp_name"])) {
|
|
if (is_array($activityId)) {
|
|
foreach ($activityId as $id) {
|
|
if ($Ret->is_KO()) {
|
|
break;
|
|
}
|
|
|
|
$Ret = self::saveFile($id, $fileItem, true, $profileDb, $getItem);
|
|
}
|
|
} else {
|
|
$Ret = self::saveFile($activityId, $fileItem, true, $profileDb, $getItem);
|
|
}
|
|
|
|
if ($Ret->is_OK()) {
|
|
$retData = $Ret->get_data();
|
|
$attachments[] = $retData;
|
|
@unlink($fileItem["tmp_name"]);
|
|
}
|
|
} else {
|
|
$Ret->set_error("File non trovato");
|
|
}
|
|
}
|
|
|
|
if (count($attachments) > 0) {
|
|
$Ret->set_data($attachments);
|
|
}
|
|
|
|
return $Ret;
|
|
}
|
|
|
|
public static function save_item($activityId, $fileName, $content = null, $descrizione = null, $insert = true, $profileDb = null) {
|
|
$Ret = new Ret;
|
|
|
|
if ($insert) { // cambio nome se ne esiste un altro
|
|
$Query = new Query;
|
|
$Query->select("file_name")->from("stb_activity_file")->where("id", $activityId);
|
|
if ($profileDb) {
|
|
$Query->profileDB();
|
|
}
|
|
$Ret = $Query->firstColumn()->toRet()->execute();
|
|
if ($Ret->is_OK()) {
|
|
$arr_fileNameExisting = $Ret->get_data();
|
|
$fileName = Utility\File::check_duplicateFilename($arr_fileNameExisting, $fileName);
|
|
}
|
|
}
|
|
|
|
if ($Ret->is_OK()) {
|
|
$stbActivity = new EntityItem("stb_activity");
|
|
$stbActivity->update()->set("activityId", $activityId);
|
|
$StbActivityFile = new EntityItem("stb_activity_file");
|
|
$StbActivityFile->insert_or_update()
|
|
->set("id", $activityId)
|
|
->set("fileName", pathinfo($fileName, PATHINFO_BASENAME))
|
|
->setDatetime("lastUpd", \Utility\Date::getNow())
|
|
->set("descrizione", $descrizione, true);
|
|
|
|
if (!is_null($content)) {
|
|
$StbActivityFile->set("content", base64_encode(Utility\Compression\GZ::compress($content)))
|
|
->set("originalSize", Utility\File::getSizeFromBlob($content));
|
|
}
|
|
$stbActivity->set("stbActivityFile")->append($StbActivityFile);
|
|
|
|
if ($profileDb) {
|
|
$stbActivity->profileDB($profileDb);
|
|
}
|
|
$Ret = $stbActivity->send();
|
|
if ($Ret->is_OK()) {
|
|
$retData = $Ret->get_data();
|
|
if (is_null($fileName)) {
|
|
$retEntity = $retData[0]["entity"]["stbActivityFile"][0];
|
|
$Ret->set_data($retEntity);
|
|
$fileName = $retEntity["fileName"];
|
|
}
|
|
$Ret = self::get_item($activityId, $fileName, $profileDb);
|
|
}
|
|
}
|
|
|
|
return $Ret;
|
|
}
|
|
|
|
public static function saveFile($activityId, $file, $insert = true, $profileDb = null, $getItem = true) {
|
|
$Ret = new Ret;
|
|
$fileName = $file["name"];
|
|
if ($insert) { // cambio nome se ne esiste un altro
|
|
$Query = new Query;
|
|
$Query->select("file_name")->from("stb_activity_file")->where("id", $activityId);
|
|
if ($profileDb) {
|
|
$Query->profileDB();
|
|
}
|
|
$Ret = $Query->firstColumn()->toRet()->execute();
|
|
if ($Ret->is_OK()) {
|
|
$arr_fileNameExisting = $Ret->get_data();
|
|
$fileName = Utility\File::check_duplicateFilename($arr_fileNameExisting, $fileName);
|
|
}
|
|
}
|
|
|
|
if ($Ret->is_OK()) {
|
|
|
|
$Ret = self::uploadStbActivityFileAttachment($activityId, $file, $fileName, $profileDb);
|
|
|
|
if ($Ret->is_OK() && $getItem) {
|
|
$Ret = self::get_item($activityId, $fileName, $profileDb);
|
|
}
|
|
}
|
|
|
|
return $Ret;
|
|
}
|
|
|
|
public static function cache($activityId, $fileName, $options = array(), $profileDb = null) {
|
|
$retData = null;
|
|
$options["renew"] = isset($options["renew"]) && $options["renew"] !== false;
|
|
$options["resize"] = isset($options["resize"]) && is_array($options["resize"]) && isset($options["resize"][0]) ? $options["resize"] : false;
|
|
$options["quality"] = isset($options["quality"]) ? $options["quality"] : null;
|
|
$targetName = isset($options["targetName"]) ? $options["targetName"] : null;
|
|
if (!$profileDb && array_key_exists("profileDb", $options)) {
|
|
$profileDb = $options["profileDb"];
|
|
}
|
|
$sadImageOnError = isset($options["sadImageOnError"]) && $options["sadImageOnError"] !== false;
|
|
|
|
$Ret = self::get_item($activityId, $fileName, $profileDb);
|
|
if ($Ret->is_OK()) {
|
|
$result = $Ret->get_data();
|
|
$content = $result["content"];
|
|
$fileName = pathinfo($fileName, PATHINFO_BASENAME);
|
|
|
|
if (\Utility\File::isImageFromName($fileName) && $options["resize"] !== false) {
|
|
$Ret = \Utility\Image::getResizeImage($fileName, $options, $content);
|
|
|
|
} else {
|
|
$fileName = $targetName ?: $fileName;
|
|
$filePath = \Cache::get_filepath($fileName, true, true);
|
|
if ($options["renew"] === false && file_exists($filePath)) {
|
|
$Ret->set_string($filePath)->set_data(null);
|
|
|
|
} else {
|
|
$Ret = \Cache::writeR($fileName, $content, true, true);
|
|
}
|
|
}
|
|
|
|
//$Ret = \Cache::writeR($fileName, $content, null, true);
|
|
}
|
|
|
|
if (!$Ret->is_OK() && $sadImageOnError) {
|
|
$retData = array("sad" => true);
|
|
$filePath = "images/sad.png";
|
|
if ($options["resize"] !== false) {
|
|
$Ret = \Utility\Image::getResizeImage($filePath, $options);
|
|
} else {
|
|
$Ret->set_OK()->set_string($filePath);
|
|
}
|
|
}
|
|
|
|
return $Ret->set_data($retData);
|
|
}
|
|
|
|
public static function remove($activityId, $fileName) {
|
|
$Ret = \StbActivityFile::get_item($activityId, $fileName);
|
|
$stbActivity = new EntityItem("stb_activity");
|
|
$stbActivity->update()->set("activityId", $activityId);
|
|
if ($Ret->is_OK()) {
|
|
$itemData = $Ret->get_data();
|
|
|
|
$StbActivityFile = new EntityItem("stb_activity_file");
|
|
$StbActivityFile->delete()->set("id", $activityId)->set("fileName", $fileName);
|
|
$stbActivity->set("stbActivityFile")->append($StbActivityFile);
|
|
$Ret = $stbActivity->send();
|
|
if ($Ret->is_OK()) {
|
|
$Ret->set_data(array_pick($itemData, "file_name", "mime_type"));
|
|
}
|
|
}
|
|
|
|
return $Ret;
|
|
}
|
|
|
|
private static function downloadStbActivityFileAttachment($activityId, $fileName, $profileDb) {
|
|
$imsApi = new IMSApi();
|
|
$imsApi
|
|
->download("downloadStbActivityFileAttachment")
|
|
->profileDB($profileDb)
|
|
->queryParam("id", $activityId)
|
|
->queryParam("fileName", $fileName);
|
|
$ret = $imsApi->send();
|
|
|
|
return $ret->is_OK() ? $ret->get_data() : null;
|
|
}
|
|
|
|
private static function uploadStbActivityFileAttachment($activityId, $file, $fileName = null, $profileDb = null) {
|
|
$imsApi = new IMSApi();
|
|
$imsApi
|
|
->upload("uploadStbActivityFileAttachment")
|
|
->profileDB($profileDb)
|
|
->queryParam("activityId", $activityId)
|
|
->attach("files", $file, $fileName);
|
|
$ret = $imsApi->send();
|
|
|
|
return $ret;
|
|
}
|
|
} |