src/Rhyme/WMassArtsHub/Form/FormDMUploader.php line 67

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (C) 2021 Rhyme Digital - Borrowed from http://github.com/terminal42/contao-fineuploader
  4.  *
  5.  * @copyright  Copyright (c) 2008-2017, terminal42 gmbh
  6.  * @author       Blair Winans <blair@rhyme.digital>
  7.  * @author       Adam Fisher <adam@rhyme.digital>
  8.  * @author     terminal42 gmbh <info@terminal42.ch>
  9.  * @license    http://opensource.org/licenses/lgpl-3.0.html LGPL
  10.  * @link       http://github.com/terminal42/contao-fineuploader
  11.  */
  12. namespace Rhyme\WMassArtsHub\Form;
  13. use Rhyme\WMassArtsHub\Widget\DMUploaderBase;
  14. /**
  15.  * Class FormDMUploader
  16.  *
  17.  * Provide methods to handle input field "DMuploader".
  18.  */
  19. class FormDMUploader extends DMUploaderBase
  20. {
  21.     /**
  22.      * Submit user input
  23.      * @var boolean
  24.      */
  25.     protected $blnSubmitInput true;
  26.     /**
  27.      * Template
  28.      * @var string
  29.      */
  30.     protected $strTemplate 'form_dmuploader';
  31.     /**
  32.      * The CSS class prefix
  33.      *
  34.      * @var string
  35.      */
  36.     protected $strPrefix 'widget widget-dmuploader';
  37.     /**
  38.      * Multiple flag
  39.      * @var boolean
  40.      */
  41.     protected $blnIsMultiple false;
  42.     /**
  43.      * Values are already prepared
  44.      * @var boolean
  45.      */
  46.     protected $blnValuesPrepared false;
  47.     /**
  48.      * Load the database object
  49.      * @param array
  50.      */
  51.     public function __construct($arrAttributes=null)
  52.     {
  53.         parent::__construct($arrAttributes);
  54.         $this->blnIsMultiple    $this->arrConfiguration['multiple'];
  55.         $this->blnIsGallery     $this->arrConfiguration['isGallery'];
  56.         $this->blnIsDownloads   $this->arrConfiguration['isDownloads'];
  57.         if (!$this->blnIsMultiple) {
  58.             $this->arrConfiguration['uploaderLimit'] = 1;
  59.         }else{
  60.             $this->arrConfiguration['uploaderLimit'] = $this->arrConfiguration['mSize'] ? $this->arrConfiguration['mSize'] : 0;
  61.         }
  62.     }
  63.     /**
  64.      * Store the file information in the session
  65.      * @param mixed
  66.      * @return mixed
  67.      */
  68.     protected function validator($varInput)
  69.     {
  70.         $varReturn parent::validator($varInput);
  71.         $arrReturn array_filter((array) $varReturn);
  72.         $intCount = \count((array)$_SESSION['FILES']);
  73.         foreach ($arrReturn as $varFile) {
  74.             // Get the file model
  75.             if (\Validator::isUuid($varFile)) {
  76.                 $objModel = \FilesModel::findByUuid($varFile);
  77.                 if ($objModel === null) {
  78.                     continue;
  79.                 }
  80.                 $varFile $objModel->path;
  81.             }
  82.             $objFile = new \File($varFile);
  83.             $_SESSION['FILES'][$this->strName '_' $intCount++] = array
  84.             (
  85.                 'name'              => $objFile->name,
  86.                 'type'              => $objFile->mime,
  87.                 'tmp_name'          => TL_ROOT '/' $objFile->path,
  88.                 'short_tmp_name'    => $objFile->path,
  89.                 'error'             => 0,
  90.                 'size'              => $objFile->size,
  91.                 'uploaded'          => true,
  92.                 'uuid'              => ($objModel !== null) ? \StringUtil::binToUuid($objModel->uuid) : ''
  93.             );
  94.         }
  95.         return $varReturn;
  96.     }
  97.     /**
  98.      * Generate the widget and return it as string
  99.      * @param array
  100.      * @return string
  101.      */
  102.     public function parse($arrAttributes=null)
  103.     {
  104.         if (!$this->blnValuesPrepared) {
  105.             $arrSet = array();
  106.             $arrValues = array();
  107.             $arrUuids = array();
  108.             $arrTemp = array();
  109.             if (!empty($this->varValue)) { // Can be an array
  110.                 $this->varValue = (array) $this->varValue;
  111.                 foreach ($this->varValue as $varFile) {
  112.                     if (\Validator::isUuid($varFile)) {
  113.                         $arrUuids[] = $varFile;
  114.                     } else {
  115.                         $arrTemp[] = $varFile;
  116.                     }
  117.                 }
  118.                 $objFiles = \FilesModel::findMultipleByUuids($arrUuids);
  119.                 // Get the database files
  120.                 if ($objFiles !== null) {
  121.                     while ($objFiles->next()) {
  122.                         $chunk $this->generateFileItem($objFiles->path);
  123.                         if (strlen($chunk)) {
  124.                             $arrValues[$objFiles->uuid] = array
  125.                             (
  126.                                 'id' => (in_array($objFiles->uuid$arrTemp) ? $objFiles->uuid : \StringUtil::binToUuid($objFiles->uuid)),
  127.                                 'value' => $chunk
  128.                             );
  129.                             $arrSet[] = $objFiles->uuid;
  130.                         }
  131.                     }
  132.                 }
  133.                 // Get the temporary files
  134.                 foreach ($arrTemp as $varFile) {
  135.                     $chunk $this->generateFileItem($varFile);
  136.                     if (strlen($chunk)) {
  137.                         $arrValues[$varFile] = array
  138.                         (
  139.                             'id' => (in_array($varFile$arrTemp) ? $varFile : \StringUtil::binToUuid($varFile)),
  140.                             'value' => $chunk
  141.                         );
  142.                         $arrSet[] = $varFile;
  143.                     }
  144.                 }
  145.             }
  146.             // Parse the set array
  147.             foreach ($arrSet as $k=>$v) {
  148.                 if (in_array($v$arrTemp)) {
  149.                     $strSet[$k] = $v;
  150.                 } else {
  151.                     $arrSet[$k] = \StringUtil::binToUuid($v);
  152.                 }
  153.             }
  154.             $this->set implode(','$arrSet);
  155.             $this->values $arrValues;
  156.             $this->deleteTitle specialchars($GLOBALS['TL_LANG']['MSC']['delete']);
  157.             $this->extensions json_encode(trimsplit(','$this->arrConfiguration['extensions']));
  158.             $this->limit $this->arrConfiguration['uploaderLimit'] ? $this->arrConfiguration['uploaderLimit'] : 0;
  159.             $this->minSizeLimit $this->arrConfiguration['minlength'] ? $this->arrConfiguration['minlength'] : 0;
  160.             $this->sizeLimit $this->arrConfiguration['maxlength'] ? $this->arrConfiguration['maxlength'] : 0;
  161.             $this->maxConnections $this->arrConfiguration['maxConnections'] ? $this->arrConfiguration['maxConnections'] : 3;
  162.             $this->blnValuesPrepared true;
  163.         }
  164.         $this->strPrefix .= ' ' $this->strName;
  165.         return parent::parse($arrAttributes);
  166.     }
  167.     /**
  168.      * Use the parse() method instead.
  169.      *
  170.      * @throw \BadMethodCallException
  171.      */
  172.     public function generate()
  173.     {
  174.         throw new \BadMethodCallException('Please use the parse() method instead!');
  175.     }
  176. }