vendor/terminal42/contao-fineuploader/form/FormFineUploader.php line 221

Open in your IDE?
  1. <?php
  2. /**
  3.  * fineuploader extension for Contao Open Source CMS
  4.  *
  5.  * @copyright  Copyright (c) 2008-2015, terminal42 gmbh
  6.  * @author     terminal42 gmbh <info@terminal42.ch>
  7.  * @license    http://opensource.org/licenses/lgpl-3.0.html LGPL
  8.  * @link       http://github.com/terminal42/contao-fineuploader
  9.  */
  10. /**
  11.  * Class FormFineUploader
  12.  *
  13.  * Provide methods to handle input field "fine uploader".
  14.  */
  15. class FormFineUploader extends FineUploaderBase
  16. {
  17.     /**
  18.      * Submit user input
  19.      * @var boolean
  20.      */
  21.     protected $blnSubmitInput true;
  22.     /**
  23.      * Template
  24.      * @var string
  25.      */
  26.     protected $strTemplate 'fineuploader_frontend';
  27.     /**
  28.      * The CSS class prefix
  29.      *
  30.      * @var string
  31.      */
  32.     protected $strPrefix 'widget widget-fineuploader';
  33.     /**
  34.      * Multiple flag
  35.      * @var boolean
  36.      */
  37.     protected $blnIsMultiple false;
  38.     /**
  39.      * Values are already prepared
  40.      * @var boolean
  41.      */
  42.     protected $blnValuesPrepared false;
  43.     /**
  44.      * Load the database object
  45.      * @param array
  46.      */
  47.     public function __construct($arrAttributes=null)
  48.     {
  49.         // Execute the AJAX actions in front end
  50.         if (\Environment::get('isAjaxRequest') && ($arrAttributes['id'] === \Input::post('name') || $arrAttributes['name'] === \Input::post('name')) && \Input::get('no_ajax') != 1) {
  51.             $this->addAttributes($arrAttributes);
  52.             $objHandler = new FineUploaderAjax();
  53.             $objHandler->executeAjaxActions($this->arrConfiguration);
  54.             return;
  55.         }
  56.         parent::__construct($arrAttributes);
  57.         $this->blnIsMultiple    $this->arrConfiguration['multiple'];
  58.         $this->blnIsGallery     $this->arrConfiguration['isGallery'];
  59.         $this->blnIsDownloads   $this->arrConfiguration['isDownloads'];
  60.         if (!$this->blnIsMultiple) {
  61.             $this->arrConfiguration['uploaderLimit'] = 1;
  62.         }else{
  63.             $this->arrConfiguration['uploaderLimit'] = $this->arrConfiguration['mSize'] ? $this->arrConfiguration['mSize'] : 0;
  64.         }
  65.         // Include the assets
  66.         $GLOBALS['TL_JAVASCRIPT']['fineuploader']         = 'system/modules/fineuploader/assets/fine-uploader/fine-uploader.min.js';
  67.         $GLOBALS['TL_JAVASCRIPT']['fineuploader_handler'] = 'system/modules/fineuploader/assets/handler.min.js';
  68.         $GLOBALS['TL_CSS']['fineuploader']                = 'system/modules/fineuploader/assets/fine-uploader/fine-uploader.min.css';
  69.     }
  70.     /**
  71.      * Validate the upload
  72.      * @return string
  73.      */
  74.     public function validateUpload()
  75.     {
  76.         $varInput parent::validateUpload();
  77.         // Check image size
  78.         if (($arrImageSize = @getimagesize(TL_ROOT '/' $varInput)) !== false) {
  79.             // Image exceeds maximum image width
  80.             if ($arrImageSize[0] > $GLOBALS['TL_CONFIG']['imageWidth']) {
  81.                 $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['filewidth'], ''$GLOBALS['TL_CONFIG']['imageWidth']));
  82.             }
  83.             // Image exceeds maximum image height
  84.             if ($arrImageSize[1] > $GLOBALS['TL_CONFIG']['imageHeight']) {
  85.                 $this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['fileheight'], ''$GLOBALS['TL_CONFIG']['imageHeight']));
  86.             }
  87.         }
  88.         return $varInput;
  89.     }
  90.     /**
  91.      * Store the file information in the session
  92.      * @param mixed
  93.      * @return mixed
  94.      */
  95.     protected function validator($varInput)
  96.     {
  97.         $varReturn parent::validator($varInput);
  98.         $arrReturn array_filter((array) $varReturn);
  99.         $intCount 0;
  100.         foreach ($arrReturn as $varFile) {
  101.             // Get the file model
  102.             if (\Validator::isUuid($varFile)) {
  103.                 $objModel = \FilesModel::findByUuid($varFile);
  104.                 if ($objModel === null) {
  105.                     continue;
  106.                 }
  107.                 $varFile $objModel->path;
  108.             }
  109.             $objFile = new \File($varFiletrue);
  110.             $_SESSION['FILES'][$this->strName '_' $intCount++] = array
  111.             (
  112.                 'name'     => $objFile->name,
  113.                 'type'     => $objFile->mime,
  114.                 'tmp_name' => TL_ROOT '/' $objFile->path,
  115.                 'error'    => 0,
  116.                 'size'     => $objFile->size,
  117.                 'uploaded' => true,
  118.                 'uuid'     => ($objModel !== null) ? \StringUtil::binToUuid($objModel->uuid) : ''
  119.             );
  120.         }
  121.         return $varReturn;
  122.     }
  123.     /**
  124.      * Generate the widget and return it as string
  125.      * @param array
  126.      * @return string
  127.      */
  128.     public function parse($arrAttributes=null)
  129.     {
  130.         if (!$this->blnValuesPrepared) {
  131.             $arrSet = array();
  132.             $arrValues = array();
  133.             $arrUuids = array();
  134.             $arrTemp = array();
  135.             if (!empty($this->varValue)) { // Can be an array
  136.                 $this->varValue = (array) $this->varValue;
  137.                 foreach ($this->varValue as $varFile) {
  138.                     if (\Validator::isUuid($varFile)) {
  139.                         $arrUuids[] = $varFile;
  140.                     } else {
  141.                         $arrTemp[] = $varFile;
  142.                     }
  143.                 }
  144.                 $objFiles = \FilesModel::findMultipleByUuids($arrUuids);
  145.                 // Get the database files
  146.                 if ($objFiles !== null) {
  147.                     while ($objFiles->next()) {
  148.                         $chunk $this->generateFileItem($objFiles->path);
  149.                         if (strlen($chunk)) {
  150.                             $arrValues[$objFiles->uuid] = array
  151.                             (
  152.                                 'id' => (in_array($objFiles->uuid$arrTemp) ? $objFiles->uuid : \StringUtil::binToUuid($objFiles->uuid)),
  153.                                 'value' => $chunk
  154.                             );
  155.                             $arrSet[] = $objFiles->uuid;
  156.                         }
  157.                     }
  158.                 }
  159.                 // Get the temporary files
  160.                 foreach ($arrTemp as $varFile) {
  161.                     $chunk $this->generateFileItem($varFile);
  162.                     if (strlen($chunk)) {
  163.                         $arrValues[$varFile] = array
  164.                         (
  165.                             'id' => (in_array($varFile$arrTemp) ? $varFile : \StringUtil::binToUuid($varFile)),
  166.                             'value' => $chunk
  167.                         );
  168.                         $arrSet[] = $varFile;
  169.                     }
  170.                 }
  171.             }
  172.             // Parse the set array
  173.             foreach ($arrSet as $k=>$v) {
  174.                 if (in_array($v$arrTemp)) {
  175.                     $strSet[$k] = $v;
  176.                 } else {
  177.                     $arrSet[$k] = \StringUtil::binToUuid($v);
  178.                 }
  179.             }
  180.             $this->set implode(','$arrSet);
  181.             $this->values $arrValues;
  182.             $this->deleteTitle specialchars($GLOBALS['TL_LANG']['MSC']['delete']);
  183.             $this->extensions json_encode(trimsplit(','$this->arrConfiguration['extensions']));
  184.             $this->limit $this->arrConfiguration['uploaderLimit'] ? $this->arrConfiguration['uploaderLimit'] : 0;
  185.             $this->minSizeLimit $this->arrConfiguration['minlength'] ? $this->arrConfiguration['minlength'] : 0;
  186.             $this->sizeLimit $this->arrConfiguration['maxlength'] ? $this->arrConfiguration['maxlength'] : 0;
  187.             $this->chunkSize $this->arrConfiguration['chunkSize'] ? $this->arrConfiguration['chunkSize'] : 0;
  188.             $this->concurrent $this->arrConfiguration['concurrent'] ? true false;
  189.             $this->maxConnections $this->arrConfiguration['maxConnections'] ? $this->arrConfiguration['maxConnections'] : 3;
  190.             $this->blnValuesPrepared true;
  191.         }
  192.         return parent::parse($arrAttributes);
  193.     }
  194.     /**
  195.      * Use the parse() method instead.
  196.      *
  197.      * @throw \BadMethodCallException
  198.      */
  199.     public function generate()
  200.     {
  201.         throw new \BadMethodCallException('Please use the parse() method instead!');
  202.     }
  203. }