|
29 | 29 | namespace MetaModels\AttributeTranslatedFileBundle\Attribute; |
30 | 30 |
|
31 | 31 | use Contao\Config; |
| 32 | +use Contao\CoreBundle\Framework\Adapter; |
32 | 33 | use Contao\FilesModel; |
33 | 34 | use Contao\StringUtil; |
| 35 | +use Contao\System; |
34 | 36 | use Contao\Validator; |
| 37 | +use Doctrine\DBAL\Connection; |
35 | 38 | use MetaModels\Attribute\TranslatedReference; |
36 | 39 | use MetaModels\Helper\ToolboxFile; |
| 40 | +use MetaModels\IMetaModel; |
37 | 41 | use MetaModels\Render\Template; |
38 | 42 |
|
39 | 43 | /** |
40 | 44 | * This is the MetaModelAttribute class for handling translated file fields. |
41 | 45 | */ |
42 | 46 | class TranslatedFile extends TranslatedReference |
43 | 47 | { |
| 48 | + /** |
| 49 | + * The toolbox for file. |
| 50 | + * |
| 51 | + * @var ToolboxFile|null |
| 52 | + */ |
| 53 | + private $toolboxFile; |
| 54 | + |
| 55 | + /** |
| 56 | + * The string util. |
| 57 | + * |
| 58 | + * @var Adapter|StringUtil|null |
| 59 | + */ |
| 60 | + private $stringUtil; |
| 61 | + |
| 62 | + /** |
| 63 | + * The validator. |
| 64 | + * |
| 65 | + * @var Adapter|Validator|null |
| 66 | + */ |
| 67 | + private $validator; |
| 68 | + |
| 69 | + /** |
| 70 | + * The repository for files. |
| 71 | + * |
| 72 | + * @var Adapter|FilesModel|null |
| 73 | + */ |
| 74 | + private $fileRepository; |
| 75 | + |
| 76 | + /** |
| 77 | + * The contao configurations. |
| 78 | + * |
| 79 | + * @var Adapter|Config|null |
| 80 | + */ |
| 81 | + private $config; |
| 82 | + |
| 83 | + /** |
| 84 | + * {@inheritDoc} |
| 85 | + * |
| 86 | + * @param ToolboxFile|null $toolboxFile The toolbox for file. |
| 87 | + * @param Adapter|StringUtil|null $stringUtil The string util. |
| 88 | + * @param Adapter|Validator|null $validator The validator. |
| 89 | + * @param Adapter|FilesModel|null $fileRepository The repository for files. |
| 90 | + * @param Adapter|Config|null $config The contao configurations. |
| 91 | + */ |
| 92 | + public function __construct( |
| 93 | + IMetaModel $objMetaModel, |
| 94 | + $arrData = [], |
| 95 | + Connection $connection = null, |
| 96 | + ToolboxFile $toolboxFile = null, |
| 97 | + Adapter $stringUtil = null, |
| 98 | + Adapter $validator = null, |
| 99 | + Adapter $fileRepository = null, |
| 100 | + Adapter $config = null |
| 101 | + ) { |
| 102 | + parent::__construct($objMetaModel, $arrData, $connection); |
| 103 | + |
| 104 | + if (null === $toolboxFile) { |
| 105 | + // @codingStandardsIgnoreStart |
| 106 | + @\trigger_error( |
| 107 | + 'Toolbox file is missing. It has to be passed in the constructor. Fallback will be dropped.', |
| 108 | + E_USER_DEPRECATED |
| 109 | + ); |
| 110 | + // @codingStandardsIgnoreEnd |
| 111 | + $toolboxFile = System::getContainer()->get('metamodels.attribute_file.toolbox.file'); |
| 112 | + } |
| 113 | + |
| 114 | + if (null === $stringUtil) { |
| 115 | + // @codingStandardsIgnoreStart |
| 116 | + @\trigger_error( |
| 117 | + 'String util file is missing. It has to be passed in the constructor. Fallback will be dropped.', |
| 118 | + E_USER_DEPRECATED |
| 119 | + ); |
| 120 | + // @codingStandardsIgnoreEnd |
| 121 | + |
| 122 | + $stringUtil = System::getContainer()->get('contao.framework')->getAdapter(StringUtil::class); |
| 123 | + } |
| 124 | + |
| 125 | + if (null === $validator) { |
| 126 | + // @codingStandardsIgnoreStart |
| 127 | + @\trigger_error( |
| 128 | + 'Validator is missing. It has to be passed in the constructor. Fallback will be dropped.', |
| 129 | + E_USER_DEPRECATED |
| 130 | + ); |
| 131 | + // @codingStandardsIgnoreEnd |
| 132 | + |
| 133 | + $validator = System::getContainer()->get('contao.framework')->getAdapter(Validator::class); |
| 134 | + } |
| 135 | + |
| 136 | + if (null === $fileRepository) { |
| 137 | + // @codingStandardsIgnoreStart |
| 138 | + @\trigger_error( |
| 139 | + 'File repository is missing. It has to be passed in the constructor. Fallback will be dropped.', |
| 140 | + E_USER_DEPRECATED |
| 141 | + ); |
| 142 | + // @codingStandardsIgnoreEnd |
| 143 | + |
| 144 | + $fileRepository = System::getContainer()->get('contao.framework')->getAdapter(FilesModel::class); |
| 145 | + } |
| 146 | + |
| 147 | + if (null === $config) { |
| 148 | + // @codingStandardsIgnoreStart |
| 149 | + @\trigger_error( |
| 150 | + 'Config is missing. It has to be passed in the constructor. Fallback will be dropped.', |
| 151 | + E_USER_DEPRECATED |
| 152 | + ); |
| 153 | + // @codingStandardsIgnoreEnd |
| 154 | + |
| 155 | + $config = System::getContainer()->get('contao.framework')->getAdapter(Config::class); |
| 156 | + } |
| 157 | + |
| 158 | + $this->toolboxFile = $toolboxFile; |
| 159 | + $this->stringUtil = $stringUtil; |
| 160 | + $this->validator = $validator; |
| 161 | + $this->fileRepository = $fileRepository; |
| 162 | + $this->config = $config; |
| 163 | + } |
| 164 | + |
44 | 165 | /** |
45 | 166 | * {@inheritdoc} |
46 | 167 | */ |
@@ -98,7 +219,7 @@ protected function prepareTemplate(Template $objTemplate, $arrRowData, $objSetti |
98 | 219 | { |
99 | 220 | parent::prepareTemplate($objTemplate, $arrRowData, $objSettings); |
100 | 221 |
|
101 | | - $toolbox = new ToolboxFile(); |
| 222 | + $toolbox = clone $this->toolboxFile; |
102 | 223 | $toolbox |
103 | 224 | ->setBaseLanguage($this->getMetaModel()->getActiveLanguage()) |
104 | 225 | ->setFallbackLanguage($this->getMetaModel()->getFallbackLanguage()) |
@@ -177,10 +298,10 @@ private function handleCustomFileTree(&$fieldDefinition) |
177 | 298 | // Set root path of file chooser depending on contao version. |
178 | 299 | $file = null; |
179 | 300 |
|
180 | | - if (Validator::isStringUuid($this->get('file_uploadFolder')) |
181 | | - || Validator::isBinaryUuid($this->get('file_uploadFolder')) |
| 301 | + if ($this->validator->isStringUuid($this->get('file_uploadFolder')) |
| 302 | + || $this->validator->isBinaryUuid($this->get('file_uploadFolder')) |
182 | 303 | ) { |
183 | | - $file = FilesModel::findByUuid($this->get('file_uploadFolder')); |
| 304 | + $file = $this->fileRepository->findByUuid($this->get('file_uploadFolder')); |
184 | 305 | } |
185 | 306 |
|
186 | 307 | // Check if we have a file. |
@@ -210,7 +331,7 @@ public function getFieldDefinition($arrOverrides = array()) |
210 | 331 |
|
211 | 332 | $fieldDefinition['inputType'] = 'fileTree'; |
212 | 333 | $fieldDefinition['eval']['files'] = true; |
213 | | - $fieldDefinition['eval']['extensions'] = Config::get('allowedDownload'); |
| 334 | + $fieldDefinition['eval']['extensions'] = $this->config->get('allowedDownload'); |
214 | 335 | $fieldDefinition['eval']['multiple'] = (bool) $this->get('file_multiple'); |
215 | 336 |
|
216 | 337 | $widgetMode = $this->getOverrideValue('file_widgetMode', $arrOverrides); |
@@ -345,7 +466,7 @@ public function getTranslatedDataFor($arrIds, $strLangCode) |
345 | 466 | $values = parent::getTranslatedDataFor($arrIds, $strLangCode); |
346 | 467 | foreach ($values as $valueId => $value) { |
347 | 468 | $values[$valueId]['value'] = ToolboxFile::convertUuidsOrPathsToMetaModels( |
348 | | - StringUtil::deserialize($value['value'], true) |
| 469 | + $this->stringUtil->deserialize($value['value'], true) |
349 | 470 | ); |
350 | 471 | } |
351 | 472 |
|
|
0 commit comments