vendor/terminal42/contao-fineuploader/src/ConfigGenerator.php line 49

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Terminal42\FineUploaderBundle;
  4. use Contao\Config;
  5. use Contao\FilesModel;
  6. use Contao\FrontendUser;
  7. use Contao\Validator;
  8. class ConfigGenerator
  9. {
  10.     /**
  11.      * @var bool
  12.      */
  13.     private $debug;
  14.     /**
  15.      * ConfigGenerator constructor.
  16.      *
  17.      * @param bool $debug
  18.      */
  19.     public function __construct($debug)
  20.     {
  21.         $this->debug $debug;
  22.     }
  23.     /**
  24.      * Generate the configuration from widget attributes.
  25.      *
  26.      * @return UploaderConfig
  27.      */
  28.     public function generateFromWidgetAttributes(array $attributes)
  29.     {
  30.         $config = new UploaderConfig();
  31.         $config->setLabels($this->generateLabels());
  32.         // Set the config from attributes
  33.         $this->setConfigFromAttributes($config$attributes);
  34.         // Enable the debug
  35.         if ($this->debug) {
  36.             $config->enableDebug();
  37.         }
  38.         // Set the upload folder to the default one if not set yet
  39.         if (!$config->getUploadFolder()) {
  40.             $this->setUploadFolder($configConfig::get('uploadPath'));
  41.         }
  42.         return $config;
  43.     }
  44.     /**
  45.      * Generate the configuration array ready to use for JavaScript uploader setup.
  46.      *
  47.      * @return array
  48.      */
  49.     public function generateJavaScriptConfig(UploaderConfig $config)
  50.     {
  51.         $return = [
  52.             'debug' => $config->isDebugEnabled(),
  53.             'extensions' => $config->getExtensions(),
  54.             'maxConnections' => $config->getMaxConnections(),
  55.             'limit' => $config->getLimit(),
  56.             'minSizeLimit' => $config->getMinSizeLimit(),
  57.             'sizeLimit' => $config->getMaxSizeLimit(),
  58.             'uploadButtonTitle' => $config->getUploadButtonTitle(),
  59.             'messages' => $config->getLabels()['messages'] ?? [],
  60.         ];
  61.         // Enable the chunking
  62.         if ($config->isChunkingEnabled()) {
  63.             $return['chunking'] = true;
  64.             $return['chunkSize'] = $config->getChunkSize();
  65.             $return['concurrent'] = $config->isConcurrentEnabled();
  66.         }
  67.         return $return;
  68.     }
  69.     /**
  70.      * Set the config from attributes.
  71.      */
  72.     private function setConfigFromAttributes(UploaderConfig $config, array $attributes): void
  73.     {
  74.         foreach ($attributes as $k => $v) {
  75.             switch ($k) {
  76.                 case 'uploadFolder':
  77.                     $this->setUploadFolder($config$v);
  78.                     break;
  79.                 case 'useHomeDir':
  80.                     if ($v && FE_USER_LOGGED_IN) {
  81.                         $user FrontendUser::getInstance();
  82.                         if ($user->assignDir && $user->homeDir) {
  83.                             $this->setUploadFolder($config$user->homeDir);
  84.                         }
  85.                     }
  86.                     break;
  87.                 case 'extensions':
  88.                     $config->setExtensions(trimsplit(','$v));
  89.                     break;
  90.                 case 'mSize':
  91.                     if (isset($attributes['multiple']) && $attributes['multiple']) {
  92.                         $config->setLimit($v);
  93.                     }
  94.                     break;
  95.                 case 'uploaderLimit':
  96.                     $config->setLimit($v);
  97.                     break;
  98.                 case 'minlength':
  99.                     $config->setMinSizeLimit($v);
  100.                     break;
  101.                 case 'maxlength':
  102.                     $config->setMaxSizeLimit($v);
  103.                     break;
  104.                 case 'maxWidth':
  105.                     $config->setMaxImageWidth($v);
  106.                     break;
  107.                 case 'maxHeight':
  108.                     $config->setMaxImageHeight($v);
  109.                     break;
  110.                 case 'uploadButtonTitle':
  111.                     $config->setUploadButtonTitle($v);
  112.                     break;
  113.                 case 'maxConnections':
  114.                     $config->setMaxConnections($v);
  115.                     break;
  116.                 case 'chunking':
  117.                     $v $config->enableChunking() : $config->disableChunking();
  118.                     break;
  119.                 case 'chunkSize':
  120.                     $config->setChunkSize($v);
  121.                     break;
  122.                 case 'concurrent':
  123.                     $v $config->enableConcurrent() : $config->disableConcurrent();
  124.                     break;
  125.                 case 'directUpload':
  126.                     $v $config->enableDirectUpload() : $config->disableDirectUpload();
  127.                     break;
  128.                 case 'storeFile':
  129.                     $v $config->enableStoreFile() : $config->disableStoreFile();
  130.                     break;
  131.                 case 'doNotOverwrite':
  132.                     $v $config->enableDoNotOverwrite() : $config->disableDoNotOverwrite();
  133.                     break;
  134.                 case 'addToDbafs':
  135.                     $v $config->enableAddToDbafs() : $config->disableAddToDbafs();
  136.                     break;
  137.                 case 'debug':
  138.                     $v $config->enableDebug() : $config->disableDebug();
  139.                     break;
  140.             }
  141.         }
  142.     }
  143.     /**
  144.      * Set the upload folder.
  145.      *
  146.      * @param string $folder Can be a regular path or UUID
  147.      */
  148.     private function setUploadFolder(UploaderConfig $config$folder): void
  149.     {
  150.         if (Validator::isUuid($folder)) {
  151.             $model FilesModel::findByUuid($folder);
  152.             // Set the path from model
  153.             if (null !== $model) {
  154.                 $config->setUploadFolder($model->path);
  155.             }
  156.         } else {
  157.             $config->setUploadFolder($folder);
  158.         }
  159.     }
  160.     /**
  161.      * Generate the labels.
  162.      *
  163.      * @return array
  164.      */
  165.     private function generateLabels()
  166.     {
  167.         $properties = [
  168.             'text' => [
  169.                 'formatProgress',
  170.                 'failUpload',
  171.                 'waitingForResponse',
  172.                 'paused',
  173.             ],
  174.             'messages' => [
  175.                 'typeError',
  176.                 'sizeError',
  177.                 'minSizeError',
  178.                 'emptyError',
  179.                 'noFilesError',
  180.                 'tooManyItemsError',
  181.                 'maxHeightImageError',
  182.                 'maxWidthImageError',
  183.                 'minHeightImageError',
  184.                 'minWidthImageError',
  185.                 'retryFailTooManyItems',
  186.                 'onLeave',
  187.                 'unsupportedBrowserIos8Safari',
  188.             ],
  189.             'retry' => [
  190.                 'autoRetryNote',
  191.             ],
  192.             'deleteFile' => [
  193.                 'confirmMessage',
  194.                 'deletingStatusText',
  195.                 'deletingFailedText',
  196.             ],
  197.             'paste' => [
  198.                 'namePromptMessage',
  199.             ],
  200.         ];
  201.         $labels = [];
  202.         foreach ($properties as $category => $messages) {
  203.             foreach ($messages as $message) {
  204.                 // Use label only if available, otherwise fall back to default message
  205.                 // defined in FineUploader JS script (EN)
  206.                 if (isset($GLOBALS['TL_LANG']['MSC']['fineuploader.trans.'.$category][$message])) {
  207.                     $labels[$category][$message] = $GLOBALS['TL_LANG']['MSC']['fineuploader.trans.'.$category][$message];
  208.                 }
  209.             }
  210.         }
  211.         return $labels;
  212.     }
  213. }