<?php
/**
* Copyright (C) 2021 Rhyme Digital - Borrowed from http://github.com/terminal42/contao-fineuploader
*
* @copyright Copyright (c) 2008-2017, terminal42 gmbh
* @author Blair Winans <blair@rhyme.digital>
* @author Adam Fisher <adam@rhyme.digital>
* @author terminal42 gmbh <info@terminal42.ch>
* @license http://opensource.org/licenses/lgpl-3.0.html LGPL
* @link http://github.com/terminal42/contao-fineuploader
*/
namespace Rhyme\WMassArtsHub\Form;
use Rhyme\WMassArtsHub\Widget\DMUploaderBase;
/**
* Class FormDMUploader
*
* Provide methods to handle input field "DMuploader".
*/
class FormDMUploader extends DMUploaderBase
{
/**
* Submit user input
* @var boolean
*/
protected $blnSubmitInput = true;
/**
* Template
* @var string
*/
protected $strTemplate = 'form_dmuploader';
/**
* The CSS class prefix
*
* @var string
*/
protected $strPrefix = 'widget widget-dmuploader';
/**
* Multiple flag
* @var boolean
*/
protected $blnIsMultiple = false;
/**
* Values are already prepared
* @var boolean
*/
protected $blnValuesPrepared = false;
/**
* Load the database object
* @param array
*/
public function __construct($arrAttributes=null)
{
parent::__construct($arrAttributes);
$this->blnIsMultiple = $this->arrConfiguration['multiple'];
$this->blnIsGallery = $this->arrConfiguration['isGallery'];
$this->blnIsDownloads = $this->arrConfiguration['isDownloads'];
if (!$this->blnIsMultiple) {
$this->arrConfiguration['uploaderLimit'] = 1;
}else{
$this->arrConfiguration['uploaderLimit'] = $this->arrConfiguration['mSize'] ? $this->arrConfiguration['mSize'] : 0;
}
}
/**
* Store the file information in the session
* @param mixed
* @return mixed
*/
protected function validator($varInput)
{
$varReturn = parent::validator($varInput);
$arrReturn = array_filter((array) $varReturn);
$intCount = \count((array)$_SESSION['FILES']);
foreach ($arrReturn as $varFile) {
// Get the file model
if (\Validator::isUuid($varFile)) {
$objModel = \FilesModel::findByUuid($varFile);
if ($objModel === null) {
continue;
}
$varFile = $objModel->path;
}
$objFile = new \File($varFile);
$_SESSION['FILES'][$this->strName . '_' . $intCount++] = array
(
'name' => $objFile->name,
'type' => $objFile->mime,
'tmp_name' => TL_ROOT . '/' . $objFile->path,
'short_tmp_name' => $objFile->path,
'error' => 0,
'size' => $objFile->size,
'uploaded' => true,
'uuid' => ($objModel !== null) ? \StringUtil::binToUuid($objModel->uuid) : ''
);
}
return $varReturn;
}
/**
* Generate the widget and return it as string
* @param array
* @return string
*/
public function parse($arrAttributes=null)
{
if (!$this->blnValuesPrepared) {
$arrSet = array();
$arrValues = array();
$arrUuids = array();
$arrTemp = array();
if (!empty($this->varValue)) { // Can be an array
$this->varValue = (array) $this->varValue;
foreach ($this->varValue as $varFile) {
if (\Validator::isUuid($varFile)) {
$arrUuids[] = $varFile;
} else {
$arrTemp[] = $varFile;
}
}
$objFiles = \FilesModel::findMultipleByUuids($arrUuids);
// Get the database files
if ($objFiles !== null) {
while ($objFiles->next()) {
$chunk = $this->generateFileItem($objFiles->path);
if (strlen($chunk)) {
$arrValues[$objFiles->uuid] = array
(
'id' => (in_array($objFiles->uuid, $arrTemp) ? $objFiles->uuid : \StringUtil::binToUuid($objFiles->uuid)),
'value' => $chunk
);
$arrSet[] = $objFiles->uuid;
}
}
}
// Get the temporary files
foreach ($arrTemp as $varFile) {
$chunk = $this->generateFileItem($varFile);
if (strlen($chunk)) {
$arrValues[$varFile] = array
(
'id' => (in_array($varFile, $arrTemp) ? $varFile : \StringUtil::binToUuid($varFile)),
'value' => $chunk
);
$arrSet[] = $varFile;
}
}
}
// Parse the set array
foreach ($arrSet as $k=>$v) {
if (in_array($v, $arrTemp)) {
$strSet[$k] = $v;
} else {
$arrSet[$k] = \StringUtil::binToUuid($v);
}
}
$this->set = implode(',', $arrSet);
$this->values = $arrValues;
$this->deleteTitle = specialchars($GLOBALS['TL_LANG']['MSC']['delete']);
$this->extensions = json_encode(trimsplit(',', $this->arrConfiguration['extensions']));
$this->limit = $this->arrConfiguration['uploaderLimit'] ? $this->arrConfiguration['uploaderLimit'] : 0;
$this->minSizeLimit = $this->arrConfiguration['minlength'] ? $this->arrConfiguration['minlength'] : 0;
$this->sizeLimit = $this->arrConfiguration['maxlength'] ? $this->arrConfiguration['maxlength'] : 0;
$this->maxConnections = $this->arrConfiguration['maxConnections'] ? $this->arrConfiguration['maxConnections'] : 3;
$this->blnValuesPrepared = true;
}
$this->strPrefix .= ' ' . $this->strName;
return parent::parse($arrAttributes);
}
/**
* Use the parse() method instead.
*
* @throw \BadMethodCallException
*/
public function generate()
{
throw new \BadMethodCallException('Please use the parse() method instead!');
}
}