vendor/terminal42/contao-fineuploader/src/Widget/BaseWidget.php line 117

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Terminal42\FineUploaderBundle\Widget;
  4. use Contao\System;
  5. use Contao\Template;
  6. use Contao\Widget;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. use Terminal42\FineUploaderBundle\AssetsManager;
  9. use Terminal42\FineUploaderBundle\ConfigGenerator;
  10. use Terminal42\FineUploaderBundle\UploaderConfig;
  11. use Terminal42\FineUploaderBundle\WidgetHelper;
  12. abstract class BaseWidget extends Widget
  13. {
  14.     /**
  15.      * Submit user input.
  16.      *
  17.      * @var bool
  18.      */
  19.     protected $blnSubmitInput true;
  20.     /**
  21.      * @var ContainerInterface
  22.      */
  23.     protected $container;
  24.     /**
  25.      * @var UploaderConfig
  26.      */
  27.     protected $uploaderConfig;
  28.     /**
  29.      * Initialize the object.
  30.      *
  31.      * @param array $attributes
  32.      */
  33.     public function __construct($attributes null)
  34.     {
  35.         parent::__construct($attributes);
  36.         $this->container System::getContainer();
  37.         $request $this->container->get('request_stack')->getCurrentRequest();
  38.         // Set the default attributes
  39.         $this->setDefaultAttributes();
  40.         // Clean the chunks session when the widget is initialized in a non-ajax request
  41.         if (!$request->isXmlHttpRequest()) {
  42.             $this->container->get('terminal42_fineuploader.chunk_uploader')->clearSession($this);
  43.         }
  44.     }
  45.     /**
  46.      * Set the widget property.
  47.      *
  48.      * @param string $key
  49.      *
  50.      * @throws \InvalidArgumentException
  51.      */
  52.     public function __set($key$value): void
  53.     {
  54.         switch ($key) {
  55.             case 'imageSize':
  56.             case 'uploaderConfig':
  57.                 if (!\is_array($value)) {
  58.                     throw new \InvalidArgumentException(sprintf('The "%s" must be an array'$key));
  59.                 }
  60.                 $this->arrConfiguration[$key] = $value;
  61.                 break;
  62.             case 'isGallery':
  63.             case 'isDownloads':
  64.                 $this->arrConfiguration[$key] = $value true false;
  65.                 break;
  66.             case 'multiple':
  67.                 $this->arrConfiguration[$key] = $value true false;
  68.                 // Set the uploader limit to 1 if it's not multiple
  69.                 if (!$value) {
  70.                     $this->uploaderLimit 1;
  71.                 }
  72.                 break;
  73.             /** @noinspection PhpMissingBreakStatementInspection */
  74.             case 'mandatory':
  75.                 if ($value) {
  76.                     $this->arrAttributes['required'] = 'required';
  77.                 } else {
  78.                     unset($this->arrAttributes['required']);
  79.                 }
  80.             // DO NOT BREAK HERE
  81.             // no break
  82.             default:
  83.                 parent::__set($key$value);
  84.         }
  85.     }
  86.     /**
  87.      * Parse the template file and return it as string.
  88.      *
  89.      * @param array $attributes An optional attributes array
  90.      *
  91.      * @return string The template markup
  92.      */
  93.     public function parse($attributes null)
  94.     {
  95.         if (!$this->jsConfig) {
  96.             $this->jsConfig $this->getConfigGenerator()->generateJavaScriptConfig($this->getUploaderConfig());
  97.         }
  98.         return parent::parse($attributes);
  99.     }
  100.     /**
  101.      * Get the uploader config.
  102.      *
  103.      * @return UploaderConfig
  104.      */
  105.     public function getUploaderConfig()
  106.     {
  107.         if (null === $this->uploaderConfig) {
  108.             $this->uploaderConfig $this->getConfigGenerator()->generateFromWidgetAttributes($this->arrConfiguration);
  109.         }
  110.         return $this->uploaderConfig;
  111.     }
  112.     /**
  113.      * Parse the values and return them as HTML string.
  114.      *
  115.      * @return string
  116.      */
  117.     public function parseValues()
  118.     {
  119.         return $this->getWidgetHelper()->generateValuesTemplate($this)->parse();
  120.     }
  121.     /**
  122.      * Get the widget configuration.
  123.      *
  124.      * @return array
  125.      */
  126.     public function getConfiguration()
  127.     {
  128.         return $this->arrConfiguration;
  129.     }
  130.     /**
  131.      * Required by \Contao\Widget class. Use the parse() method instead.
  132.      *
  133.      * @throws \BadMethodCallException
  134.      */
  135.     public function generate(): void
  136.     {
  137.         throw new \BadMethodCallException('Use the parse() method instead');
  138.     }
  139.     /**
  140.      * Get the item template.
  141.      *
  142.      * @return Template
  143.      */
  144.     abstract public function getItemTemplate();
  145.     /**
  146.      * Get the values template.
  147.      *
  148.      * @return Template
  149.      */
  150.     abstract public function getValuesTemplate();
  151.     /**
  152.      * Set the default attributes.
  153.      */
  154.     protected function setDefaultAttributes(): void
  155.     {
  156.         $this->decodeEntities true;
  157.         // Set the default image size
  158.         if (!$this->imageSize) {
  159.             $this->imageSize = [8060'center_center'];
  160.         }
  161.         // Set the uploader limit to 1 if it's not multiple
  162.         if (!$this->multiple) {
  163.             $this->uploaderLimit 1;
  164.         }
  165.         // Set the default upload button title
  166.         if (!$this->uploadButtonTitle) {
  167.             $this->uploadButtonTitle $GLOBALS['TL_LANG']['MSC']['fineuploader.upload'];
  168.         }
  169.     }
  170.     /**
  171.      * Return an array if the "multiple" attribute is set.
  172.      *
  173.      * @param string $input
  174.      *
  175.      * @return array|string
  176.      */
  177.     protected function validator($input)
  178.     {
  179.         return $this->container->get('terminal42_fineuploader.validator')->validateInput($this$input);
  180.     }
  181.     /**
  182.      * Get the config generator.
  183.      *
  184.      * @return ConfigGenerator
  185.      */
  186.     protected function getConfigGenerator()
  187.     {
  188.         return $this->container->get('terminal42_fineuploader.config_generator');
  189.     }
  190.     /**
  191.      * Get the widget helper.
  192.      *
  193.      * @return WidgetHelper
  194.      */
  195.     protected function getWidgetHelper()
  196.     {
  197.         return $this->container->get('terminal42_fineuploader.widget_helper');
  198.     }
  199.     /**
  200.      * Get the assets manager.
  201.      *
  202.      * @return AssetsManager
  203.      */
  204.     protected function getAssetsManager()
  205.     {
  206.         return $this->container->get('terminal42_fineuploader.assets_manager');
  207.     }
  208. }