File: /home/assibfaf/public_html/prox-classes/Order/File.php
<?php
/**
* @package Proxim
* @author Davison Pro <davisonpro.coder@gmail.com | https://davisonpro.dev>
* @copyright 2019 Proxim
* @version 1.0.0
* @since File available since Release 1.0.0
*/
namespace Proxim\Order;
use Db;
use Proxim\Application;
use Proxim\Configuration;
use Proxim\Database\DbQuery;
use Proxim\Cache\Cache;
use Proxim\ObjectModel;
use Proxim\User\Customer;
use Proxim\User\Employee;
use Proxim\Util\ArrayUtils;
use Proxim\Util\DateUtils;
use Proxim\Validate;
use Proxim\Tools;
use Proxim\Util\StringUtils;
/**
* File
*/
class File extends ObjectModel
{
/** The default db first file id */
const START_FILE_ID = 23455;
/** File types */
const LECTURE_NOTES = 59;
const GRADIC_RUBIC = 57;
const SOURCES = 55;
const CHAPTERS = 53;
const QUESTIONS = 51;
const OTHER = 39;
const PROPOSAL = 31;
const EBOOK = 27;
const ARTICLE_TO_USE = 25;
const SAMPLE = 23;
const DRAFT = 21;
const OUTLINE = 19;
const WRITING_GUILDLINES = 17;
const ORDER_INSTRUCTIONS = 15;
const SCREENSHOT = 68;
const FINAL_PAPER = 69;
/** @var $id File ID */
public $id;
/** @var int order_id */
public $order_id;
/** @var int site_id */
public $site_id;
/** @var int uploader_id */
public $uploader_id;
/** @var int file_type_id */
public $file_type_id;
/** @var string file_description */
public $file_description;
/** @var string name */
public $name;
/** @var int size */
public $size = 0;
/** @var string source */
public $source;
/** @var string extension */
public $extension;
/** @var bool is_part */
public $is_part = 0;
/** @var bool downloaded */
public $approved = 0;
/** @var string approved_at */
public $approved_at;
/** @var bool is_final_file */
public $is_final_file = 0;
/** @var bool downloaded */
public $downloaded = 0;
/** @var string downloaded_at */
public $downloaded_at;
/** @var string date_upd */
public $date_upd;
/** @var string date_add */
public $date_add;
/**
* @see ObjectModel::$definition
*/
public static $definition = array(
'table' => 'order_file',
'primary' => 'file_id',
'fields' => array(
'order_id' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true),
'site_id' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
'uploader_id' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true),
'file_type_id' => array('type' => self::TYPE_INT, 'validate' => 'isInt', 'required' => true),
'file_description' => array('type' => self::TYPE_STRING, 'size' => 255),
'name' => array('type' => self::TYPE_STRING),
'size' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
'source' => array('type' => self::TYPE_STRING),
'extension' => array('type' => self::TYPE_STRING),
'is_part' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'is_final_file' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'approved' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'approved_at' => array('type' => self::TYPE_DATE, 'validate' => 'isDateOrNull'),
'downloaded' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'downloaded_at' => array('type' => self::TYPE_DATE, 'validate' => 'isDateOrNull'),
'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDateOrNull'),
'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
)
);
/**
* constructor.
*
* @param null $id
*/
public function __construct($id = null)
{
parent::__construct($id);
if($this->id) {
if(!$this->site_id) {
$this->site_id = PROX_ADMIN_SITE_ID;
$this->update();
}
}
}
public function add($autodate = true, $null_values = true) {
$newFileId = Db::getInstance()->getValue('SELECT MAX(`file_id`)+2 FROM `' . PROX_DB_PREFIX . 'order_file`');
if ($newFileId) {
$this->id = $newFileId;
} else {
$this->id = self::START_FILE_ID;
}
$this->site_id = (int) Configuration::get('SITE_ID');
$this->force_id = true;
return parent::add($autodate, $null_values);
}
public function getDownloadLink() {
$app = Application::getInstance();
if($app->cloudStorageEnabled()) {
$uploadsPath = Configuration::get('UPLOADS_PATH');
$downloadLink = $uploadsPath . '/' . $this->source;
} elseif( Configuration::get('INTERNAL_FILE_STORAGE', PROX_ADMIN_SITE_ID) ) {
$site_url = Configuration::get('SITE_DOMAIN', $this->site_id);
return $site_url . '/api/ifs/download/' . $this->id;
} else {
$downloadLink = $app->base_uri . '/api/v1/files/' . $this->id . '/download';
}
return $downloadLink;
}
public function present() {
$file = $this;
$order = new Order( $this->order_id );
if(!Validate::isLoadedObject($order)) {
return false;
}
$actionLabel = "";
$timeForFreeRevision = null;
$canRequestFreeRevision = $requestRefundButtonVisible = $approveButtonVisible = $canRequestPaidRevision = $downloadButtonVisible = $isFullRefundAllowed = false;
if ( $order->status_id == Order::FINISHED ) {
$timeForFreeRevision = DateUtils::date(strtotime($order->approved_at . ' + 14 days'));
$canRequestFreeRevision = DateUtils::hasPassed($timeForFreeRevision) ? false : true;
$canRequestPaidRevision = !$canRequestFreeRevision ? true : false;
}
$leaveTipButtonVisible = (bool) $order->leaveTipButtonVisible();
if( $order->status_id == Order::DELIVERED || $order->status_id == Order::FINISHED ) {
$downloadButtonVisible = true;
}
if( $order->status_id == Order::DELIVERED ) {
$approveButtonVisible = true;
$canRequestFreeRevision = true;
$isFullRefundAllowed = $requestRefundButtonVisible = (bool) Configuration::get('REQUEST_REFUND_ALLOWED');
}
if ( $this->uploader_id != $order->customer_id ) {
if ( $order->status_id == Order::FINISHED ) {
$actionLabel = "If you have any issues with your paper, be sure to contact our support team.";
} else {
$actionLabel = "Preview version awaiting to be checked before approval.";
}
}
$fileName = $this->name;
if(!StringUtils::endsWith($fileName, $this->extension)) {
$fileName .= "." . $this->extension;
}
$return = array(
"id" => (int) $this->id,
"downloadId" => (int) $this->id,
"name" => $fileName,
"fileTypeId" => (int) $this->file_type_id,
"fileType" => $order->status_id == Order::FINISHED ? "original" : "other",
"icon" => $this->extension,
"fileDescription" => $this->file_description,
"clientDescription" => $this->file_description,
"size" => (int) $this->size,
"sizeFormatted" => formatBytes($this->size),
"isUploadedByCustomer" => $this->uploader_id == $order->customer_id ? true : false,
"downloadLink" => $this->getDownloadLink(),
"isDownloaded" => $this->downloaded,
"isApprovedByCustomer" => (bool) $order->status_id == Order::FINISHED,
"isPart" => (bool) $this->is_part,
"isLast" => false,
"isNew" => !$this->downloaded ? true : false,
"createdAt" => DateUtils::convertToISOFormat($this->date_add),
"createdAtUnixTime" => DateUtils::convertToISOFormat($this->date_add),
"clientApprovedAtUnixTime" => DateUtils::convertToISOFormat($this->date_add),
"hoursAgoUploaded" => DateUtils::since($this->date_add, 2),
"actionLabel" => $actionLabel,
"isFullRefundAllowed" => $isFullRefundAllowed,
"downloadButtonVisible" => $downloadButtonVisible,
"approveButtonVisible" => $approveButtonVisible,
"leaveTipButtonVisible" => $leaveTipButtonVisible,
"requestRefundButtonVisible" => $requestRefundButtonVisible,
"requestPaidRevisionButtonVisible" => $canRequestPaidRevision,
"timeForFreeRevision" => $timeForFreeRevision ? DateUtils::timeLeft($timeForFreeRevision) : null,
"requestFreeRevisionButtonVisible" => $canRequestFreeRevision,
"requestRevisionButtonVisible" => $order->status_id == Order::DELIVERED ? true : false
);
return $return;
}
}