@@ -16,13 +16,13 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class ProcessAction extends Enumeration |
| 18 | 18 | { |
| 19 | - public const REMOVE = 'remove'; |
|
| 19 | + public const REMOVE = 'remove'; |
|
| 20 | 20 | |
| 21 | - public const UPDATE = 'update'; |
|
| 21 | + public const UPDATE = 'update'; |
|
| 22 | 22 | |
| 23 | - public const COPY = 'copy'; |
|
| 23 | + public const COPY = 'copy'; |
|
| 24 | 24 | |
| 25 | - public const MOVE = 'move'; |
|
| 25 | + public const MOVE = 'move'; |
|
| 26 | 26 | |
| 27 | - public const LOCALIZE = 'localize'; |
|
| 27 | + public const LOCALIZE = 'localize'; |
|
| 28 | 28 | } |
@@ -18,135 +18,135 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class CoreDataHandler extends AbstractDataHandler |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * @var array |
|
| 23 | - */ |
|
| 24 | - protected $dataHandler; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Process Content with action "update". |
|
| 28 | - * |
|
| 29 | - * @param Content $content |
|
| 30 | - * @throws \Exception |
|
| 31 | - * @return bool |
|
| 32 | - */ |
|
| 33 | - public function processUpdate(Content $content) |
|
| 34 | - { |
|
| 35 | - $values = []; |
|
| 36 | - |
|
| 37 | - // Check the field to be updated exists |
|
| 38 | - foreach ($content->toArray() as $fieldName => $value) { |
|
| 39 | - if (!Tca::table($content->getDataType())->hasField($fieldName)) { |
|
| 40 | - $message = sprintf('It looks field "%s" does not exist for data type "%s"', $fieldName, $content->getDataType()); |
|
| 41 | - throw new \Exception($message, 1390668497); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - // Flatten value if array given which is required for the DataHandler. |
|
| 45 | - if (is_array($value)) { |
|
| 46 | - $value = implode(',', $value); |
|
| 47 | - } |
|
| 48 | - $values[$fieldName] = $value; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - $data[$content->getDataType()][$content->getUid()] = $values; |
|
| 52 | - |
|
| 53 | - $dataHandler = $this->getDataHandler(); |
|
| 54 | - $dataHandler->start($data, array()); |
|
| 55 | - $dataHandler->process_datamap(); |
|
| 56 | - $this->errorMessages = $dataHandler->errorLog; |
|
| 57 | - |
|
| 58 | - // Returns true is log does not contain errors. |
|
| 59 | - return empty($dataHandler->errorLog); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Process Content with action "remove". |
|
| 64 | - * |
|
| 65 | - * @param Content $content |
|
| 66 | - * @return bool |
|
| 67 | - */ |
|
| 68 | - public function processRemove(Content $content) |
|
| 69 | - { |
|
| 70 | - // Build command |
|
| 71 | - $cmd[$content->getDataType()][$content->getUid()]['delete'] = 1; |
|
| 72 | - |
|
| 73 | - /** @var $dataHandler \TYPO3\CMS\Core\DataHandling\DataHandler */ |
|
| 74 | - $dataHandler = $this->getDataHandler(); |
|
| 75 | - $dataHandler->start([], $cmd); |
|
| 76 | - $dataHandler->process_datamap(); |
|
| 77 | - $dataHandler->process_cmdmap(); |
|
| 78 | - $this->errorMessages = $dataHandler->errorLog; |
|
| 79 | - |
|
| 80 | - // Returns true is log does not contain errors. |
|
| 81 | - return empty($dataHandler->errorLog); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Process Content with action "copy". |
|
| 86 | - * |
|
| 87 | - * @param Content $content |
|
| 88 | - * @param string $target |
|
| 89 | - * @return bool |
|
| 90 | - */ |
|
| 91 | - public function processCopy(Content $content, $target) |
|
| 92 | - { |
|
| 93 | - // TODO: Implement processCopy() method. |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Process Content with action "move". |
|
| 98 | - * The $target corresponds to the pid to move the records to. |
|
| 99 | - * It can also be a negative value in case of sorting. The negative value would be the uid of its predecessor. |
|
| 100 | - * |
|
| 101 | - * @param Content $content |
|
| 102 | - * @param int $target corresponds |
|
| 103 | - * @return bool |
|
| 104 | - */ |
|
| 105 | - public function processMove(Content $content, $target) |
|
| 106 | - { |
|
| 107 | - // Build command |
|
| 108 | - $cmd[$content->getDataType()][$content->getUid()]['move'] = $target; |
|
| 109 | - |
|
| 110 | - /** @var $dataHandler \TYPO3\CMS\Core\DataHandling\DataHandler */ |
|
| 111 | - $dataHandler = $this->getDataHandler(); |
|
| 112 | - $dataHandler->start([], $cmd); |
|
| 113 | - $dataHandler->process_datamap(); |
|
| 114 | - $dataHandler->process_cmdmap(); |
|
| 115 | - $this->errorMessages = $dataHandler->errorLog; |
|
| 116 | - |
|
| 117 | - // Returns true is log does not contain errors. |
|
| 118 | - return empty($dataHandler->errorLog); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Process Content with action "localize". |
|
| 123 | - * |
|
| 124 | - * @param Content $content |
|
| 125 | - * @param int $language |
|
| 126 | - * @return bool |
|
| 127 | - */ |
|
| 128 | - public function processLocalize(Content $content, $language) |
|
| 129 | - { |
|
| 130 | - $command[$content->getDataType()][$content->getUid()]['localize'] = $language; |
|
| 131 | - |
|
| 132 | - $dataHandler = $this->getDataHandler(); |
|
| 133 | - $dataHandler->start([], $command); |
|
| 134 | - $dataHandler->process_datamap(); |
|
| 135 | - $dataHandler->process_cmdmap(); |
|
| 136 | - $this->errorMessages = $dataHandler->errorLog; |
|
| 137 | - |
|
| 138 | - // Returns true is log does not contain errors. |
|
| 139 | - return empty($dataHandler->errorLog); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * @return DataHandler |
|
| 144 | - */ |
|
| 145 | - protected function getDataHandler() |
|
| 146 | - { |
|
| 147 | - if (!$this->dataHandler) { |
|
| 148 | - $this->dataHandler = GeneralUtility::makeInstance(DataHandler::class); |
|
| 149 | - } |
|
| 150 | - return $this->dataHandler; |
|
| 151 | - } |
|
| 21 | + /** |
|
| 22 | + * @var array |
|
| 23 | + */ |
|
| 24 | + protected $dataHandler; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Process Content with action "update". |
|
| 28 | + * |
|
| 29 | + * @param Content $content |
|
| 30 | + * @throws \Exception |
|
| 31 | + * @return bool |
|
| 32 | + */ |
|
| 33 | + public function processUpdate(Content $content) |
|
| 34 | + { |
|
| 35 | + $values = []; |
|
| 36 | + |
|
| 37 | + // Check the field to be updated exists |
|
| 38 | + foreach ($content->toArray() as $fieldName => $value) { |
|
| 39 | + if (!Tca::table($content->getDataType())->hasField($fieldName)) { |
|
| 40 | + $message = sprintf('It looks field "%s" does not exist for data type "%s"', $fieldName, $content->getDataType()); |
|
| 41 | + throw new \Exception($message, 1390668497); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + // Flatten value if array given which is required for the DataHandler. |
|
| 45 | + if (is_array($value)) { |
|
| 46 | + $value = implode(',', $value); |
|
| 47 | + } |
|
| 48 | + $values[$fieldName] = $value; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + $data[$content->getDataType()][$content->getUid()] = $values; |
|
| 52 | + |
|
| 53 | + $dataHandler = $this->getDataHandler(); |
|
| 54 | + $dataHandler->start($data, array()); |
|
| 55 | + $dataHandler->process_datamap(); |
|
| 56 | + $this->errorMessages = $dataHandler->errorLog; |
|
| 57 | + |
|
| 58 | + // Returns true is log does not contain errors. |
|
| 59 | + return empty($dataHandler->errorLog); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Process Content with action "remove". |
|
| 64 | + * |
|
| 65 | + * @param Content $content |
|
| 66 | + * @return bool |
|
| 67 | + */ |
|
| 68 | + public function processRemove(Content $content) |
|
| 69 | + { |
|
| 70 | + // Build command |
|
| 71 | + $cmd[$content->getDataType()][$content->getUid()]['delete'] = 1; |
|
| 72 | + |
|
| 73 | + /** @var $dataHandler \TYPO3\CMS\Core\DataHandling\DataHandler */ |
|
| 74 | + $dataHandler = $this->getDataHandler(); |
|
| 75 | + $dataHandler->start([], $cmd); |
|
| 76 | + $dataHandler->process_datamap(); |
|
| 77 | + $dataHandler->process_cmdmap(); |
|
| 78 | + $this->errorMessages = $dataHandler->errorLog; |
|
| 79 | + |
|
| 80 | + // Returns true is log does not contain errors. |
|
| 81 | + return empty($dataHandler->errorLog); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Process Content with action "copy". |
|
| 86 | + * |
|
| 87 | + * @param Content $content |
|
| 88 | + * @param string $target |
|
| 89 | + * @return bool |
|
| 90 | + */ |
|
| 91 | + public function processCopy(Content $content, $target) |
|
| 92 | + { |
|
| 93 | + // TODO: Implement processCopy() method. |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Process Content with action "move". |
|
| 98 | + * The $target corresponds to the pid to move the records to. |
|
| 99 | + * It can also be a negative value in case of sorting. The negative value would be the uid of its predecessor. |
|
| 100 | + * |
|
| 101 | + * @param Content $content |
|
| 102 | + * @param int $target corresponds |
|
| 103 | + * @return bool |
|
| 104 | + */ |
|
| 105 | + public function processMove(Content $content, $target) |
|
| 106 | + { |
|
| 107 | + // Build command |
|
| 108 | + $cmd[$content->getDataType()][$content->getUid()]['move'] = $target; |
|
| 109 | + |
|
| 110 | + /** @var $dataHandler \TYPO3\CMS\Core\DataHandling\DataHandler */ |
|
| 111 | + $dataHandler = $this->getDataHandler(); |
|
| 112 | + $dataHandler->start([], $cmd); |
|
| 113 | + $dataHandler->process_datamap(); |
|
| 114 | + $dataHandler->process_cmdmap(); |
|
| 115 | + $this->errorMessages = $dataHandler->errorLog; |
|
| 116 | + |
|
| 117 | + // Returns true is log does not contain errors. |
|
| 118 | + return empty($dataHandler->errorLog); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Process Content with action "localize". |
|
| 123 | + * |
|
| 124 | + * @param Content $content |
|
| 125 | + * @param int $language |
|
| 126 | + * @return bool |
|
| 127 | + */ |
|
| 128 | + public function processLocalize(Content $content, $language) |
|
| 129 | + { |
|
| 130 | + $command[$content->getDataType()][$content->getUid()]['localize'] = $language; |
|
| 131 | + |
|
| 132 | + $dataHandler = $this->getDataHandler(); |
|
| 133 | + $dataHandler->start([], $command); |
|
| 134 | + $dataHandler->process_datamap(); |
|
| 135 | + $dataHandler->process_cmdmap(); |
|
| 136 | + $this->errorMessages = $dataHandler->errorLog; |
|
| 137 | + |
|
| 138 | + // Returns true is log does not contain errors. |
|
| 139 | + return empty($dataHandler->errorLog); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * @return DataHandler |
|
| 144 | + */ |
|
| 145 | + protected function getDataHandler() |
|
| 146 | + { |
|
| 147 | + if (!$this->dataHandler) { |
|
| 148 | + $this->dataHandler = GeneralUtility::makeInstance(DataHandler::class); |
|
| 149 | + } |
|
| 150 | + return $this->dataHandler; |
|
| 151 | + } |
|
| 152 | 152 | } |
@@ -16,53 +16,53 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class BackendUserPreferenceService |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * Returns a class instance |
|
| 21 | - * |
|
| 22 | - * @return \Fab\Vidi\Service\BackendUserPreferenceService|object |
|
| 23 | - */ |
|
| 24 | - public static function getInstance() |
|
| 25 | - { |
|
| 26 | - return GeneralUtility::makeInstance(\Fab\Vidi\Service\BackendUserPreferenceService::class); |
|
| 27 | - } |
|
| 19 | + /** |
|
| 20 | + * Returns a class instance |
|
| 21 | + * |
|
| 22 | + * @return \Fab\Vidi\Service\BackendUserPreferenceService|object |
|
| 23 | + */ |
|
| 24 | + public static function getInstance() |
|
| 25 | + { |
|
| 26 | + return GeneralUtility::makeInstance(\Fab\Vidi\Service\BackendUserPreferenceService::class); |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Returns a configuration key for the current BE User. |
|
| 31 | - * |
|
| 32 | - * @param string $key |
|
| 33 | - * @return mixed |
|
| 34 | - */ |
|
| 35 | - public function get($key) |
|
| 36 | - { |
|
| 37 | - $result = ''; |
|
| 38 | - if ($this->getBackendUser() && !empty($this->getBackendUser()->uc[$key])) { |
|
| 39 | - $result = $this->getBackendUser()->uc[$key]; |
|
| 40 | - } |
|
| 41 | - return $result; |
|
| 42 | - } |
|
| 29 | + /** |
|
| 30 | + * Returns a configuration key for the current BE User. |
|
| 31 | + * |
|
| 32 | + * @param string $key |
|
| 33 | + * @return mixed |
|
| 34 | + */ |
|
| 35 | + public function get($key) |
|
| 36 | + { |
|
| 37 | + $result = ''; |
|
| 38 | + if ($this->getBackendUser() && !empty($this->getBackendUser()->uc[$key])) { |
|
| 39 | + $result = $this->getBackendUser()->uc[$key]; |
|
| 40 | + } |
|
| 41 | + return $result; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Set a configuration for the current BE User. |
|
| 46 | - * |
|
| 47 | - * @param string $key |
|
| 48 | - * @param mixed $value |
|
| 49 | - * @return void |
|
| 50 | - */ |
|
| 51 | - public function set($key, $value) |
|
| 52 | - { |
|
| 53 | - if ($this->getBackendUser()) { |
|
| 54 | - $this->getBackendUser()->uc[$key] = $value; |
|
| 55 | - $this->getBackendUser()->writeUC(); |
|
| 56 | - } |
|
| 57 | - } |
|
| 44 | + /** |
|
| 45 | + * Set a configuration for the current BE User. |
|
| 46 | + * |
|
| 47 | + * @param string $key |
|
| 48 | + * @param mixed $value |
|
| 49 | + * @return void |
|
| 50 | + */ |
|
| 51 | + public function set($key, $value) |
|
| 52 | + { |
|
| 53 | + if ($this->getBackendUser()) { |
|
| 54 | + $this->getBackendUser()->uc[$key] = $value; |
|
| 55 | + $this->getBackendUser()->writeUC(); |
|
| 56 | + } |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Returns an instance of the current Backend User. |
|
| 61 | - * |
|
| 62 | - * @return BackendUserAuthentication |
|
| 63 | - */ |
|
| 64 | - protected function getBackendUser() |
|
| 65 | - { |
|
| 66 | - return $GLOBALS['BE_USER']; |
|
| 67 | - } |
|
| 59 | + /** |
|
| 60 | + * Returns an instance of the current Backend User. |
|
| 61 | + * |
|
| 62 | + * @return BackendUserAuthentication |
|
| 63 | + */ |
|
| 64 | + protected function getBackendUser() |
|
| 65 | + { |
|
| 66 | + return $GLOBALS['BE_USER']; |
|
| 67 | + } |
|
| 68 | 68 | } |
@@ -22,82 +22,82 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | class FileReferenceService implements SingletonInterface |
| 24 | 24 | { |
| 25 | - /** |
|
| 26 | - * @var array |
|
| 27 | - */ |
|
| 28 | - protected static $instances = []; |
|
| 25 | + /** |
|
| 26 | + * @var array |
|
| 27 | + */ |
|
| 28 | + protected static $instances = []; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Returns a class instance |
|
| 32 | - * |
|
| 33 | - * @return \Fab\Vidi\Service\FileReferenceService|object |
|
| 34 | - */ |
|
| 35 | - public static function getInstance() |
|
| 36 | - { |
|
| 37 | - return GeneralUtility::makeInstance(\Fab\Vidi\Service\FileReferenceService::class); |
|
| 38 | - } |
|
| 30 | + /** |
|
| 31 | + * Returns a class instance |
|
| 32 | + * |
|
| 33 | + * @return \Fab\Vidi\Service\FileReferenceService|object |
|
| 34 | + */ |
|
| 35 | + public static function getInstance() |
|
| 36 | + { |
|
| 37 | + return GeneralUtility::makeInstance(\Fab\Vidi\Service\FileReferenceService::class); |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * @param Content $object |
|
| 42 | - * @param string $propertyName |
|
| 43 | - * @return File[] |
|
| 44 | - */ |
|
| 45 | - public function findReferencedBy($propertyName, Content $object) |
|
| 46 | - { |
|
| 47 | - if (!isset(self::$instances[$object->getUid()][$propertyName])) { |
|
| 48 | - // Initialize instances value |
|
| 49 | - if (!isset(self::$instances[$object->getUid()])) { |
|
| 50 | - self::$instances[$object->getUid()] = []; |
|
| 51 | - } |
|
| 40 | + /** |
|
| 41 | + * @param Content $object |
|
| 42 | + * @param string $propertyName |
|
| 43 | + * @return File[] |
|
| 44 | + */ |
|
| 45 | + public function findReferencedBy($propertyName, Content $object) |
|
| 46 | + { |
|
| 47 | + if (!isset(self::$instances[$object->getUid()][$propertyName])) { |
|
| 48 | + // Initialize instances value |
|
| 49 | + if (!isset(self::$instances[$object->getUid()])) { |
|
| 50 | + self::$instances[$object->getUid()] = []; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - $fieldName = GeneralUtility::camelCaseToLowerCaseUnderscored($propertyName); |
|
| 54 | - $field = Tca::table($object->getDataType())->field($fieldName); |
|
| 55 | - if ($field->getForeignTable() === 'sys_file_reference') { |
|
| 56 | - $files = $this->findByFileReference($propertyName, $object); |
|
| 57 | - self::$instances[$object->getUid()][$propertyName] = $files; |
|
| 58 | - } else { |
|
| 59 | - // @todo the standard way of handling file references is by "sys_file_reference". Let see if there is other use cases... |
|
| 60 | - } |
|
| 61 | - } |
|
| 53 | + $fieldName = GeneralUtility::camelCaseToLowerCaseUnderscored($propertyName); |
|
| 54 | + $field = Tca::table($object->getDataType())->field($fieldName); |
|
| 55 | + if ($field->getForeignTable() === 'sys_file_reference') { |
|
| 56 | + $files = $this->findByFileReference($propertyName, $object); |
|
| 57 | + self::$instances[$object->getUid()][$propertyName] = $files; |
|
| 58 | + } else { |
|
| 59 | + // @todo the standard way of handling file references is by "sys_file_reference". Let see if there is other use cases... |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - return self::$instances[$object->getUid()][$propertyName]; |
|
| 64 | - } |
|
| 63 | + return self::$instances[$object->getUid()][$propertyName]; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * Fetch the files given an object assuming |
|
| 68 | - * |
|
| 69 | - * @param $propertyName |
|
| 70 | - * @param Content $object |
|
| 71 | - * @return File[] |
|
| 72 | - */ |
|
| 73 | - protected function findByFileReference($propertyName, Content $object) |
|
| 74 | - { |
|
| 75 | - $fileField = 'uid_local'; |
|
| 76 | - $tableName = 'sys_file_reference'; |
|
| 66 | + /** |
|
| 67 | + * Fetch the files given an object assuming |
|
| 68 | + * |
|
| 69 | + * @param $propertyName |
|
| 70 | + * @param Content $object |
|
| 71 | + * @return File[] |
|
| 72 | + */ |
|
| 73 | + protected function findByFileReference($propertyName, Content $object) |
|
| 74 | + { |
|
| 75 | + $fileField = 'uid_local'; |
|
| 76 | + $tableName = 'sys_file_reference'; |
|
| 77 | 77 | |
| 78 | - $rows = $this->getDataService()->getRecords( |
|
| 79 | - $tableName, |
|
| 80 | - [ |
|
| 81 | - 'tablenames' => $object->getDataType(), |
|
| 82 | - 'fieldname'=> GeneralUtility::camelCaseToLowerCaseUnderscored($propertyName), |
|
| 83 | - 'uid_foreign'=> $object->getUid(), |
|
| 84 | - ] |
|
| 85 | - ); |
|
| 78 | + $rows = $this->getDataService()->getRecords( |
|
| 79 | + $tableName, |
|
| 80 | + [ |
|
| 81 | + 'tablenames' => $object->getDataType(), |
|
| 82 | + 'fieldname'=> GeneralUtility::camelCaseToLowerCaseUnderscored($propertyName), |
|
| 83 | + 'uid_foreign'=> $object->getUid(), |
|
| 84 | + ] |
|
| 85 | + ); |
|
| 86 | 86 | |
| 87 | - // Build array of Files |
|
| 88 | - $files = []; |
|
| 89 | - foreach ($rows as $row) { |
|
| 90 | - $files[] = GeneralUtility::makeInstance(ResourceFactory::class)->getFileObject($row[$fileField]); |
|
| 91 | - } |
|
| 87 | + // Build array of Files |
|
| 88 | + $files = []; |
|
| 89 | + foreach ($rows as $row) { |
|
| 90 | + $files[] = GeneralUtility::makeInstance(ResourceFactory::class)->getFileObject($row[$fileField]); |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - return $files; |
|
| 94 | - } |
|
| 93 | + return $files; |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - /** |
|
| 97 | - * @return object|DataService |
|
| 98 | - */ |
|
| 99 | - protected function getDataService(): DataService |
|
| 100 | - { |
|
| 101 | - return GeneralUtility::makeInstance(DataService::class); |
|
| 102 | - } |
|
| 96 | + /** |
|
| 97 | + * @return object|DataService |
|
| 98 | + */ |
|
| 99 | + protected function getDataService(): DataService |
|
| 100 | + { |
|
| 101 | + return GeneralUtility::makeInstance(DataService::class); |
|
| 102 | + } |
|
| 103 | 103 | } |
@@ -22,121 +22,121 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | class ContentService |
| 24 | 24 | { |
| 25 | - /** |
|
| 26 | - * @var string |
|
| 27 | - */ |
|
| 28 | - protected $dataType; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @var Content[] |
|
| 32 | - */ |
|
| 33 | - protected $objects = []; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @var int |
|
| 37 | - */ |
|
| 38 | - protected $numberOfObjects = 0; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Constructor |
|
| 42 | - * |
|
| 43 | - * @param string $dataType |
|
| 44 | - */ |
|
| 45 | - public function __construct($dataType = '') |
|
| 46 | - { |
|
| 47 | - if (empty($dataType)) { |
|
| 48 | - $dataType = $this->getModuleLoader()->getDataType(); |
|
| 49 | - } |
|
| 50 | - $this->dataType = $dataType; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Fetch the files given an object assuming |
|
| 55 | - * |
|
| 56 | - * @param Matcher $matcher |
|
| 57 | - * @param Order $order The order |
|
| 58 | - * @param int $limit |
|
| 59 | - * @param int $offset |
|
| 60 | - * @return $this |
|
| 61 | - */ |
|
| 62 | - public function findBy(Matcher $matcher, Order $order = null, $limit = null, $offset = null) |
|
| 63 | - { |
|
| 64 | - // Query the repository. |
|
| 65 | - $objects = ContentRepositoryFactory::getInstance($this->dataType)->findBy($matcher, $order, $limit, $offset); |
|
| 66 | - $signalResult = $this->emitAfterFindContentObjectsSignal($objects, $matcher, $order, $limit, $offset); |
|
| 67 | - |
|
| 68 | - // Reset objects variable after possible signal / slot processing. |
|
| 69 | - $this->objects = $signalResult->getContentObjects(); |
|
| 70 | - |
|
| 71 | - // Count number of content objects. |
|
| 72 | - if ($signalResult->getHasBeenProcessed()) { |
|
| 73 | - $this->numberOfObjects = $signalResult->getNumberOfObjects(); |
|
| 74 | - } else { |
|
| 75 | - $this->numberOfObjects = ContentRepositoryFactory::getInstance($this->dataType)->countBy($matcher); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - return $this; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Signal that is called after the content objects have been found. |
|
| 83 | - * |
|
| 84 | - * @param array $contentObjects |
|
| 85 | - * @param Matcher $matcher |
|
| 86 | - * @param Order $order |
|
| 87 | - * @param int $limit |
|
| 88 | - * @param int $offset |
|
| 89 | - * @return AfterFindContentObjectsSignalArguments |
|
| 90 | - */ |
|
| 91 | - protected function emitAfterFindContentObjectsSignal($contentObjects, Matcher $matcher, Order $order = null, $limit = 0, $offset = 0) |
|
| 92 | - { |
|
| 93 | - /** @var AfterFindContentObjectsSignalArguments $signalArguments */ |
|
| 94 | - $signalArguments = GeneralUtility::makeInstance(AfterFindContentObjectsSignalArguments::class); |
|
| 95 | - $signalArguments->setDataType($this->dataType) |
|
| 96 | - ->setContentObjects($contentObjects) |
|
| 97 | - ->setMatcher($matcher) |
|
| 98 | - ->setOrder($order) |
|
| 99 | - ->setLimit($limit) |
|
| 100 | - ->setOffset($offset) |
|
| 101 | - ->setHasBeenProcessed(false); |
|
| 102 | - |
|
| 103 | - $signalResult = $this->getSignalSlotDispatcher()->dispatch(ContentService::class, 'afterFindContentObjects', array($signalArguments)); |
|
| 104 | - return $signalResult[0]; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Get the Vidi Module Loader. |
|
| 109 | - * |
|
| 110 | - * @return ModuleLoader|object |
|
| 111 | - */ |
|
| 112 | - protected function getModuleLoader() |
|
| 113 | - { |
|
| 114 | - return GeneralUtility::makeInstance(ModuleLoader::class); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Get the SignalSlot dispatcher. |
|
| 119 | - * |
|
| 120 | - * @return Dispatcher|object |
|
| 121 | - */ |
|
| 122 | - protected function getSignalSlotDispatcher() |
|
| 123 | - { |
|
| 124 | - return GeneralUtility::makeInstance(Dispatcher::class); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * @return Content[] |
|
| 129 | - */ |
|
| 130 | - public function getObjects() |
|
| 131 | - { |
|
| 132 | - return $this->objects; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * @return int |
|
| 137 | - */ |
|
| 138 | - public function getNumberOfObjects() |
|
| 139 | - { |
|
| 140 | - return $this->numberOfObjects; |
|
| 141 | - } |
|
| 25 | + /** |
|
| 26 | + * @var string |
|
| 27 | + */ |
|
| 28 | + protected $dataType; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @var Content[] |
|
| 32 | + */ |
|
| 33 | + protected $objects = []; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @var int |
|
| 37 | + */ |
|
| 38 | + protected $numberOfObjects = 0; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Constructor |
|
| 42 | + * |
|
| 43 | + * @param string $dataType |
|
| 44 | + */ |
|
| 45 | + public function __construct($dataType = '') |
|
| 46 | + { |
|
| 47 | + if (empty($dataType)) { |
|
| 48 | + $dataType = $this->getModuleLoader()->getDataType(); |
|
| 49 | + } |
|
| 50 | + $this->dataType = $dataType; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Fetch the files given an object assuming |
|
| 55 | + * |
|
| 56 | + * @param Matcher $matcher |
|
| 57 | + * @param Order $order The order |
|
| 58 | + * @param int $limit |
|
| 59 | + * @param int $offset |
|
| 60 | + * @return $this |
|
| 61 | + */ |
|
| 62 | + public function findBy(Matcher $matcher, Order $order = null, $limit = null, $offset = null) |
|
| 63 | + { |
|
| 64 | + // Query the repository. |
|
| 65 | + $objects = ContentRepositoryFactory::getInstance($this->dataType)->findBy($matcher, $order, $limit, $offset); |
|
| 66 | + $signalResult = $this->emitAfterFindContentObjectsSignal($objects, $matcher, $order, $limit, $offset); |
|
| 67 | + |
|
| 68 | + // Reset objects variable after possible signal / slot processing. |
|
| 69 | + $this->objects = $signalResult->getContentObjects(); |
|
| 70 | + |
|
| 71 | + // Count number of content objects. |
|
| 72 | + if ($signalResult->getHasBeenProcessed()) { |
|
| 73 | + $this->numberOfObjects = $signalResult->getNumberOfObjects(); |
|
| 74 | + } else { |
|
| 75 | + $this->numberOfObjects = ContentRepositoryFactory::getInstance($this->dataType)->countBy($matcher); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + return $this; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Signal that is called after the content objects have been found. |
|
| 83 | + * |
|
| 84 | + * @param array $contentObjects |
|
| 85 | + * @param Matcher $matcher |
|
| 86 | + * @param Order $order |
|
| 87 | + * @param int $limit |
|
| 88 | + * @param int $offset |
|
| 89 | + * @return AfterFindContentObjectsSignalArguments |
|
| 90 | + */ |
|
| 91 | + protected function emitAfterFindContentObjectsSignal($contentObjects, Matcher $matcher, Order $order = null, $limit = 0, $offset = 0) |
|
| 92 | + { |
|
| 93 | + /** @var AfterFindContentObjectsSignalArguments $signalArguments */ |
|
| 94 | + $signalArguments = GeneralUtility::makeInstance(AfterFindContentObjectsSignalArguments::class); |
|
| 95 | + $signalArguments->setDataType($this->dataType) |
|
| 96 | + ->setContentObjects($contentObjects) |
|
| 97 | + ->setMatcher($matcher) |
|
| 98 | + ->setOrder($order) |
|
| 99 | + ->setLimit($limit) |
|
| 100 | + ->setOffset($offset) |
|
| 101 | + ->setHasBeenProcessed(false); |
|
| 102 | + |
|
| 103 | + $signalResult = $this->getSignalSlotDispatcher()->dispatch(ContentService::class, 'afterFindContentObjects', array($signalArguments)); |
|
| 104 | + return $signalResult[0]; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Get the Vidi Module Loader. |
|
| 109 | + * |
|
| 110 | + * @return ModuleLoader|object |
|
| 111 | + */ |
|
| 112 | + protected function getModuleLoader() |
|
| 113 | + { |
|
| 114 | + return GeneralUtility::makeInstance(ModuleLoader::class); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Get the SignalSlot dispatcher. |
|
| 119 | + * |
|
| 120 | + * @return Dispatcher|object |
|
| 121 | + */ |
|
| 122 | + protected function getSignalSlotDispatcher() |
|
| 123 | + { |
|
| 124 | + return GeneralUtility::makeInstance(Dispatcher::class); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * @return Content[] |
|
| 129 | + */ |
|
| 130 | + public function getObjects() |
|
| 131 | + { |
|
| 132 | + return $this->objects; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * @return int |
|
| 137 | + */ |
|
| 138 | + public function getNumberOfObjects() |
|
| 139 | + { |
|
| 140 | + return $this->numberOfObjects; |
|
| 141 | + } |
|
| 142 | 142 | } |
@@ -21,145 +21,145 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class SpreadSheetService |
| 23 | 23 | { |
| 24 | - public const XmlHeader = "<?xml version=\"1.0\" encoding=\"%s\"?\>\n<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\">"; |
|
| 25 | - public const XmlFooter = "</Workbook>"; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * @var string |
|
| 29 | - */ |
|
| 30 | - protected $encoding = 'UTF-8'; // encoding type to specify in file. |
|
| 31 | - // Note that you're on your own for making sure your data is actually encoded to this encoding |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @var string |
|
| 35 | - */ |
|
| 36 | - protected $title = 'Sheet1'; // title for Worksheet |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * @var string |
|
| 40 | - */ |
|
| 41 | - protected $buffer = ''; |
|
| 42 | - |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Constructor |
|
| 46 | - */ |
|
| 47 | - public function __construct() |
|
| 48 | - { |
|
| 49 | - $this->addToBuffer($this->generateHeader()); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @param array $row |
|
| 54 | - */ |
|
| 55 | - public function addRow($row) |
|
| 56 | - { |
|
| 57 | - $this->addToBuffer($this->generateRow($row)); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @return mixed |
|
| 62 | - */ |
|
| 63 | - public function toString() |
|
| 64 | - { |
|
| 65 | - $this->addToBuffer($this->generateFooter()); |
|
| 66 | - return $this->buffer; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @param $data |
|
| 71 | - */ |
|
| 72 | - protected function addToBuffer($data) |
|
| 73 | - { |
|
| 74 | - $this->buffer .= $data; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * @return string |
|
| 79 | - */ |
|
| 80 | - protected function generateHeader() |
|
| 81 | - { |
|
| 82 | - // workbook header |
|
| 83 | - $output = stripslashes(sprintf(self::XmlHeader, $this->encoding)) . "\n"; |
|
| 84 | - |
|
| 85 | - // Set up styles |
|
| 86 | - $output .= "<Styles>\n"; |
|
| 87 | - $output .= "<Style ss:ID=\"sDT\"><NumberFormat ss:Format=\"Short Date\"/></Style>\n"; |
|
| 88 | - $output .= "</Styles>\n"; |
|
| 89 | - |
|
| 90 | - // worksheet header |
|
| 91 | - $output .= sprintf("<Worksheet ss:Name=\"%s\">\n <Table>\n", htmlentities($this->title)); |
|
| 92 | - |
|
| 93 | - return $output; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - protected function generateFooter() |
|
| 97 | - { |
|
| 98 | - $output = ''; |
|
| 99 | - |
|
| 100 | - // worksheet footer |
|
| 101 | - $output .= " </Table>\n</Worksheet>\n"; |
|
| 102 | - |
|
| 103 | - // workbook footer |
|
| 104 | - $output .= self::XmlFooter; |
|
| 105 | - |
|
| 106 | - return $output; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @param $row |
|
| 111 | - * @return string |
|
| 112 | - */ |
|
| 113 | - protected function generateRow($row) |
|
| 114 | - { |
|
| 115 | - $output = ''; |
|
| 116 | - $output .= " <Row>\n"; |
|
| 117 | - foreach ($row as $v) { |
|
| 118 | - $output .= $this->generateCell($v); |
|
| 119 | - } |
|
| 120 | - $output .= " </Row>\n"; |
|
| 121 | - return $output; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * @param $item |
|
| 126 | - * @return string |
|
| 127 | - */ |
|
| 128 | - protected function generateCell($item) |
|
| 129 | - { |
|
| 130 | - $output = ''; |
|
| 131 | - $style = ''; |
|
| 132 | - |
|
| 133 | - // Tell Excel to treat as a number. Note that Excel only stores roughly 15 digits, so keep |
|
| 134 | - // as text if number is longer than that. |
|
| 135 | - if (preg_match("/^-?\d+(?:[.,]\d+)?$/", $item) && (strlen($item) < 15)) { |
|
| 136 | - $type = 'Number'; |
|
| 137 | - } |
|
| 138 | - // Sniff for valid dates; should look something like 2010-07-14 or 7/14/2010 etc. Can |
|
| 139 | - // also have an optional time after the date. |
|
| 140 | - // |
|
| 141 | - // Note we want to be very strict in what we consider a date. There is the possibility |
|
| 142 | - // of really screwing up the data if we try to reformat a string that was not actually |
|
| 143 | - // intended to represent a date. |
|
| 144 | - elseif (preg_match("/^(\d{1,2}|\d{4})[\/\-]\d{1,2}[\/\-](\d{1,2}|\d{4})([^\d].+)?$/", $item) && |
|
| 145 | - ($timestamp = strtotime($item)) && |
|
| 146 | - ($timestamp > 0) && |
|
| 147 | - ($timestamp < strtotime('+500 years')) |
|
| 148 | - ) { |
|
| 149 | - $type = 'DateTime'; |
|
| 150 | - $item = strftime("%Y-%m-%dT%H:%M:%S", $timestamp); |
|
| 151 | - $style = 'sDT'; // defined in header; tells excel to format date for display |
|
| 152 | - } else { |
|
| 153 | - $type = 'String'; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - $item = str_replace(''', ''', htmlspecialchars($item, ENT_QUOTES)); |
|
| 157 | - $item = str_replace("\n", ' ', $item); |
|
| 158 | - $output .= " "; |
|
| 159 | - $output .= $style ? "<Cell ss:StyleID=\"$style\">" : "<Cell>"; |
|
| 160 | - $output .= sprintf("<Data ss:Type=\"%s\">%s</Data>", $type, $item); |
|
| 161 | - $output .= "</Cell>\n"; |
|
| 162 | - |
|
| 163 | - return $output; |
|
| 164 | - } |
|
| 24 | + public const XmlHeader = "<?xml version=\"1.0\" encoding=\"%s\"?\>\n<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\">"; |
|
| 25 | + public const XmlFooter = "</Workbook>"; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * @var string |
|
| 29 | + */ |
|
| 30 | + protected $encoding = 'UTF-8'; // encoding type to specify in file. |
|
| 31 | + // Note that you're on your own for making sure your data is actually encoded to this encoding |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @var string |
|
| 35 | + */ |
|
| 36 | + protected $title = 'Sheet1'; // title for Worksheet |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * @var string |
|
| 40 | + */ |
|
| 41 | + protected $buffer = ''; |
|
| 42 | + |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Constructor |
|
| 46 | + */ |
|
| 47 | + public function __construct() |
|
| 48 | + { |
|
| 49 | + $this->addToBuffer($this->generateHeader()); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @param array $row |
|
| 54 | + */ |
|
| 55 | + public function addRow($row) |
|
| 56 | + { |
|
| 57 | + $this->addToBuffer($this->generateRow($row)); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @return mixed |
|
| 62 | + */ |
|
| 63 | + public function toString() |
|
| 64 | + { |
|
| 65 | + $this->addToBuffer($this->generateFooter()); |
|
| 66 | + return $this->buffer; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @param $data |
|
| 71 | + */ |
|
| 72 | + protected function addToBuffer($data) |
|
| 73 | + { |
|
| 74 | + $this->buffer .= $data; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * @return string |
|
| 79 | + */ |
|
| 80 | + protected function generateHeader() |
|
| 81 | + { |
|
| 82 | + // workbook header |
|
| 83 | + $output = stripslashes(sprintf(self::XmlHeader, $this->encoding)) . "\n"; |
|
| 84 | + |
|
| 85 | + // Set up styles |
|
| 86 | + $output .= "<Styles>\n"; |
|
| 87 | + $output .= "<Style ss:ID=\"sDT\"><NumberFormat ss:Format=\"Short Date\"/></Style>\n"; |
|
| 88 | + $output .= "</Styles>\n"; |
|
| 89 | + |
|
| 90 | + // worksheet header |
|
| 91 | + $output .= sprintf("<Worksheet ss:Name=\"%s\">\n <Table>\n", htmlentities($this->title)); |
|
| 92 | + |
|
| 93 | + return $output; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + protected function generateFooter() |
|
| 97 | + { |
|
| 98 | + $output = ''; |
|
| 99 | + |
|
| 100 | + // worksheet footer |
|
| 101 | + $output .= " </Table>\n</Worksheet>\n"; |
|
| 102 | + |
|
| 103 | + // workbook footer |
|
| 104 | + $output .= self::XmlFooter; |
|
| 105 | + |
|
| 106 | + return $output; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @param $row |
|
| 111 | + * @return string |
|
| 112 | + */ |
|
| 113 | + protected function generateRow($row) |
|
| 114 | + { |
|
| 115 | + $output = ''; |
|
| 116 | + $output .= " <Row>\n"; |
|
| 117 | + foreach ($row as $v) { |
|
| 118 | + $output .= $this->generateCell($v); |
|
| 119 | + } |
|
| 120 | + $output .= " </Row>\n"; |
|
| 121 | + return $output; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * @param $item |
|
| 126 | + * @return string |
|
| 127 | + */ |
|
| 128 | + protected function generateCell($item) |
|
| 129 | + { |
|
| 130 | + $output = ''; |
|
| 131 | + $style = ''; |
|
| 132 | + |
|
| 133 | + // Tell Excel to treat as a number. Note that Excel only stores roughly 15 digits, so keep |
|
| 134 | + // as text if number is longer than that. |
|
| 135 | + if (preg_match("/^-?\d+(?:[.,]\d+)?$/", $item) && (strlen($item) < 15)) { |
|
| 136 | + $type = 'Number'; |
|
| 137 | + } |
|
| 138 | + // Sniff for valid dates; should look something like 2010-07-14 or 7/14/2010 etc. Can |
|
| 139 | + // also have an optional time after the date. |
|
| 140 | + // |
|
| 141 | + // Note we want to be very strict in what we consider a date. There is the possibility |
|
| 142 | + // of really screwing up the data if we try to reformat a string that was not actually |
|
| 143 | + // intended to represent a date. |
|
| 144 | + elseif (preg_match("/^(\d{1,2}|\d{4})[\/\-]\d{1,2}[\/\-](\d{1,2}|\d{4})([^\d].+)?$/", $item) && |
|
| 145 | + ($timestamp = strtotime($item)) && |
|
| 146 | + ($timestamp > 0) && |
|
| 147 | + ($timestamp < strtotime('+500 years')) |
|
| 148 | + ) { |
|
| 149 | + $type = 'DateTime'; |
|
| 150 | + $item = strftime("%Y-%m-%dT%H:%M:%S", $timestamp); |
|
| 151 | + $style = 'sDT'; // defined in header; tells excel to format date for display |
|
| 152 | + } else { |
|
| 153 | + $type = 'String'; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + $item = str_replace(''', ''', htmlspecialchars($item, ENT_QUOTES)); |
|
| 157 | + $item = str_replace("\n", ' ', $item); |
|
| 158 | + $output .= " "; |
|
| 159 | + $output .= $style ? "<Cell ss:StyleID=\"$style\">" : "<Cell>"; |
|
| 160 | + $output .= sprintf("<Data ss:Type=\"%s\">%s</Data>", $type, $item); |
|
| 161 | + $output .= "</Cell>\n"; |
|
| 162 | + |
|
| 163 | + return $output; |
|
| 164 | + } |
|
| 165 | 165 | } |
@@ -80,7 +80,7 @@ |
||
| 80 | 80 | protected function generateHeader() |
| 81 | 81 | { |
| 82 | 82 | // workbook header |
| 83 | - $output = stripslashes(sprintf(self::XmlHeader, $this->encoding)) . "\n"; |
|
| 83 | + $output = stripslashes(sprintf(self::XmlHeader, $this->encoding))."\n"; |
|
| 84 | 84 | |
| 85 | 85 | // Set up styles |
| 86 | 86 | $output .= "<Styles>\n"; |
@@ -17,125 +17,125 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class DataService implements SingletonInterface |
| 19 | 19 | { |
| 20 | - public function getRecord(string $tableName, array $demand = []): array |
|
| 21 | - { |
|
| 22 | - $queryBuilder = $this->getQueryBuilder($tableName); |
|
| 23 | - $queryBuilder |
|
| 24 | - ->select('*') |
|
| 25 | - ->from($tableName); |
|
| 26 | - |
|
| 27 | - $this->addDemandConstraints($demand, $queryBuilder); |
|
| 28 | - $record = $queryBuilder->execute()->fetchAssociative(); |
|
| 29 | - return is_array($record) |
|
| 30 | - ? $record |
|
| 31 | - : []; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - public function getRecords(string $tableName, array $demand = [], int $maxResult = 0, int $firstResult = 0): array |
|
| 35 | - { |
|
| 36 | - $queryBuilder = $this->getQueryBuilder($tableName); |
|
| 37 | - $queryBuilder |
|
| 38 | - ->select('*') |
|
| 39 | - ->from($tableName); |
|
| 40 | - |
|
| 41 | - $this->addDemandConstraints($demand, $queryBuilder); |
|
| 42 | - |
|
| 43 | - if ($maxResult) { |
|
| 44 | - $queryBuilder->setMaxResults($maxResult); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - if ($firstResult) { |
|
| 48 | - $queryBuilder->setFirstResult($firstResult); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - return $queryBuilder->execute()->fetchAllAssociative(); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - public function count(string $tableName, array $demand = [], int $maxResult = 0, int $firstResult = 0): int |
|
| 55 | - { |
|
| 56 | - $queryBuilder = $this->getQueryBuilder($tableName); |
|
| 57 | - |
|
| 58 | - // We have to count "manually" if we have a limit or offset. |
|
| 59 | - if ($maxResult || $firstResult) { |
|
| 60 | - $records = $this->getRecords($tableName, $demand, $maxResult, $firstResult); |
|
| 61 | - $count = count($records); |
|
| 62 | - } else { |
|
| 63 | - $queryBuilder |
|
| 64 | - ->count('*') |
|
| 65 | - ->from($tableName); |
|
| 66 | - |
|
| 67 | - $this->addDemandConstraints($demand, $queryBuilder); |
|
| 68 | - $count = (int)$queryBuilder->execute()->fetchOne(); |
|
| 69 | - } |
|
| 70 | - return $count; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - public function insert(string $tableName, array $values): int |
|
| 74 | - { |
|
| 75 | - $connection = $this->getConnection($tableName); |
|
| 76 | - $connection->insert( |
|
| 77 | - $tableName, |
|
| 78 | - $values |
|
| 79 | - ); |
|
| 80 | - return (int)$connection->lastInsertId(); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - public function update(string $tableName, array $values, array $identifiers): void |
|
| 84 | - { |
|
| 85 | - $connection = $this->getConnection($tableName); |
|
| 86 | - $connection->update( |
|
| 87 | - $tableName, |
|
| 88 | - $values, |
|
| 89 | - $identifiers |
|
| 90 | - ); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - public function delete(string $tableName, array $identifiers): void |
|
| 94 | - { |
|
| 95 | - $connection = $this->getConnection($tableName); |
|
| 96 | - $connection->delete( |
|
| 97 | - $tableName, |
|
| 98 | - $identifiers |
|
| 99 | - ); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - protected function addDemandConstraints(array $demand, $queryBuilder): void |
|
| 103 | - { |
|
| 104 | - $expressions = []; |
|
| 105 | - foreach ($demand as $fieldName => $value) { |
|
| 106 | - if (is_numeric($value)) { |
|
| 107 | - $expressions[] = $queryBuilder->expr()->eq( |
|
| 108 | - $fieldName, |
|
| 109 | - $value |
|
| 110 | - ); |
|
| 111 | - } elseif (is_string($value)) { |
|
| 112 | - $expressions[] = $queryBuilder->expr()->eq( |
|
| 113 | - $fieldName, |
|
| 114 | - $queryBuilder->expr()->literal($value) |
|
| 115 | - ); |
|
| 116 | - } elseif (is_array($value)) { |
|
| 117 | - $expressions[] = $queryBuilder->expr()->in( |
|
| 118 | - $fieldName, |
|
| 119 | - $value |
|
| 120 | - ); |
|
| 121 | - } |
|
| 122 | - } |
|
| 123 | - foreach ($expressions as $expression) { |
|
| 124 | - $queryBuilder->andWhere($expression); |
|
| 125 | - } |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - protected function getConnection(string $tableName): Connection |
|
| 129 | - { |
|
| 130 | - /** @var ConnectionPool $connectionPool */ |
|
| 131 | - $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class); |
|
| 132 | - return $connectionPool->getConnectionForTable($tableName); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - protected function getQueryBuilder(string $tableName): QueryBuilder |
|
| 136 | - { |
|
| 137 | - /** @var ConnectionPool $connectionPool */ |
|
| 138 | - $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class); |
|
| 139 | - return $connectionPool->getQueryBuilderForTable($tableName); |
|
| 140 | - } |
|
| 20 | + public function getRecord(string $tableName, array $demand = []): array |
|
| 21 | + { |
|
| 22 | + $queryBuilder = $this->getQueryBuilder($tableName); |
|
| 23 | + $queryBuilder |
|
| 24 | + ->select('*') |
|
| 25 | + ->from($tableName); |
|
| 26 | + |
|
| 27 | + $this->addDemandConstraints($demand, $queryBuilder); |
|
| 28 | + $record = $queryBuilder->execute()->fetchAssociative(); |
|
| 29 | + return is_array($record) |
|
| 30 | + ? $record |
|
| 31 | + : []; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + public function getRecords(string $tableName, array $demand = [], int $maxResult = 0, int $firstResult = 0): array |
|
| 35 | + { |
|
| 36 | + $queryBuilder = $this->getQueryBuilder($tableName); |
|
| 37 | + $queryBuilder |
|
| 38 | + ->select('*') |
|
| 39 | + ->from($tableName); |
|
| 40 | + |
|
| 41 | + $this->addDemandConstraints($demand, $queryBuilder); |
|
| 42 | + |
|
| 43 | + if ($maxResult) { |
|
| 44 | + $queryBuilder->setMaxResults($maxResult); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + if ($firstResult) { |
|
| 48 | + $queryBuilder->setFirstResult($firstResult); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + return $queryBuilder->execute()->fetchAllAssociative(); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + public function count(string $tableName, array $demand = [], int $maxResult = 0, int $firstResult = 0): int |
|
| 55 | + { |
|
| 56 | + $queryBuilder = $this->getQueryBuilder($tableName); |
|
| 57 | + |
|
| 58 | + // We have to count "manually" if we have a limit or offset. |
|
| 59 | + if ($maxResult || $firstResult) { |
|
| 60 | + $records = $this->getRecords($tableName, $demand, $maxResult, $firstResult); |
|
| 61 | + $count = count($records); |
|
| 62 | + } else { |
|
| 63 | + $queryBuilder |
|
| 64 | + ->count('*') |
|
| 65 | + ->from($tableName); |
|
| 66 | + |
|
| 67 | + $this->addDemandConstraints($demand, $queryBuilder); |
|
| 68 | + $count = (int)$queryBuilder->execute()->fetchOne(); |
|
| 69 | + } |
|
| 70 | + return $count; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + public function insert(string $tableName, array $values): int |
|
| 74 | + { |
|
| 75 | + $connection = $this->getConnection($tableName); |
|
| 76 | + $connection->insert( |
|
| 77 | + $tableName, |
|
| 78 | + $values |
|
| 79 | + ); |
|
| 80 | + return (int)$connection->lastInsertId(); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + public function update(string $tableName, array $values, array $identifiers): void |
|
| 84 | + { |
|
| 85 | + $connection = $this->getConnection($tableName); |
|
| 86 | + $connection->update( |
|
| 87 | + $tableName, |
|
| 88 | + $values, |
|
| 89 | + $identifiers |
|
| 90 | + ); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + public function delete(string $tableName, array $identifiers): void |
|
| 94 | + { |
|
| 95 | + $connection = $this->getConnection($tableName); |
|
| 96 | + $connection->delete( |
|
| 97 | + $tableName, |
|
| 98 | + $identifiers |
|
| 99 | + ); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + protected function addDemandConstraints(array $demand, $queryBuilder): void |
|
| 103 | + { |
|
| 104 | + $expressions = []; |
|
| 105 | + foreach ($demand as $fieldName => $value) { |
|
| 106 | + if (is_numeric($value)) { |
|
| 107 | + $expressions[] = $queryBuilder->expr()->eq( |
|
| 108 | + $fieldName, |
|
| 109 | + $value |
|
| 110 | + ); |
|
| 111 | + } elseif (is_string($value)) { |
|
| 112 | + $expressions[] = $queryBuilder->expr()->eq( |
|
| 113 | + $fieldName, |
|
| 114 | + $queryBuilder->expr()->literal($value) |
|
| 115 | + ); |
|
| 116 | + } elseif (is_array($value)) { |
|
| 117 | + $expressions[] = $queryBuilder->expr()->in( |
|
| 118 | + $fieldName, |
|
| 119 | + $value |
|
| 120 | + ); |
|
| 121 | + } |
|
| 122 | + } |
|
| 123 | + foreach ($expressions as $expression) { |
|
| 124 | + $queryBuilder->andWhere($expression); |
|
| 125 | + } |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + protected function getConnection(string $tableName): Connection |
|
| 129 | + { |
|
| 130 | + /** @var ConnectionPool $connectionPool */ |
|
| 131 | + $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class); |
|
| 132 | + return $connectionPool->getConnectionForTable($tableName); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + protected function getQueryBuilder(string $tableName): QueryBuilder |
|
| 136 | + { |
|
| 137 | + /** @var ConnectionPool $connectionPool */ |
|
| 138 | + $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class); |
|
| 139 | + return $connectionPool->getQueryBuilderForTable($tableName); |
|
| 140 | + } |
|
| 141 | 141 | } |
@@ -18,42 +18,42 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class OrderObjectFactory implements SingletonInterface |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * Gets a singleton instance of this class. |
|
| 23 | - * |
|
| 24 | - * @return \Fab\Vidi\Persistence\OrderObjectFactory|object |
|
| 25 | - */ |
|
| 26 | - public static function getInstance() |
|
| 27 | - { |
|
| 28 | - return GeneralUtility::makeInstance(\Fab\Vidi\Persistence\OrderObjectFactory::class); |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Returns an order object. |
|
| 33 | - * |
|
| 34 | - * @param string $dataType |
|
| 35 | - * @return Order|object |
|
| 36 | - */ |
|
| 37 | - public function getOrder($dataType = '') |
|
| 38 | - { |
|
| 39 | - // Default ordering |
|
| 40 | - $order = Tca::table($dataType)->getDefaultOrderings(); |
|
| 41 | - |
|
| 42 | - // Retrieve a possible id of the column from the request |
|
| 43 | - $orderings = GeneralUtility::_GP('order'); |
|
| 44 | - |
|
| 45 | - if (is_array($orderings) && isset($orderings[0])) { |
|
| 46 | - $columnPosition = $orderings[0]['column']; |
|
| 47 | - $direction = $orderings[0]['dir']; |
|
| 48 | - |
|
| 49 | - if ($columnPosition > 0) { |
|
| 50 | - $field = Tca::grid()->getFieldNameByPosition($columnPosition); |
|
| 51 | - |
|
| 52 | - $order = array( |
|
| 53 | - $field => strtoupper($direction) |
|
| 54 | - ); |
|
| 55 | - } |
|
| 56 | - } |
|
| 57 | - return GeneralUtility::makeInstance(Order::class, $order); |
|
| 58 | - } |
|
| 21 | + /** |
|
| 22 | + * Gets a singleton instance of this class. |
|
| 23 | + * |
|
| 24 | + * @return \Fab\Vidi\Persistence\OrderObjectFactory|object |
|
| 25 | + */ |
|
| 26 | + public static function getInstance() |
|
| 27 | + { |
|
| 28 | + return GeneralUtility::makeInstance(\Fab\Vidi\Persistence\OrderObjectFactory::class); |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Returns an order object. |
|
| 33 | + * |
|
| 34 | + * @param string $dataType |
|
| 35 | + * @return Order|object |
|
| 36 | + */ |
|
| 37 | + public function getOrder($dataType = '') |
|
| 38 | + { |
|
| 39 | + // Default ordering |
|
| 40 | + $order = Tca::table($dataType)->getDefaultOrderings(); |
|
| 41 | + |
|
| 42 | + // Retrieve a possible id of the column from the request |
|
| 43 | + $orderings = GeneralUtility::_GP('order'); |
|
| 44 | + |
|
| 45 | + if (is_array($orderings) && isset($orderings[0])) { |
|
| 46 | + $columnPosition = $orderings[0]['column']; |
|
| 47 | + $direction = $orderings[0]['dir']; |
|
| 48 | + |
|
| 49 | + if ($columnPosition > 0) { |
|
| 50 | + $field = Tca::grid()->getFieldNameByPosition($columnPosition); |
|
| 51 | + |
|
| 52 | + $order = array( |
|
| 53 | + $field => strtoupper($direction) |
|
| 54 | + ); |
|
| 55 | + } |
|
| 56 | + } |
|
| 57 | + return GeneralUtility::makeInstance(Order::class, $order); |
|
| 58 | + } |
|
| 59 | 59 | } |
@@ -24,259 +24,259 @@ |
||
| 24 | 24 | */ |
| 25 | 25 | class MatcherObjectFactory implements SingletonInterface |
| 26 | 26 | { |
| 27 | - /** |
|
| 28 | - * Gets a singleton instance of this class. |
|
| 29 | - * |
|
| 30 | - * @return $this |
|
| 31 | - */ |
|
| 32 | - public static function getInstance(): self |
|
| 33 | - { |
|
| 34 | - return GeneralUtility::makeInstance(self::class); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Returns a matcher object. |
|
| 39 | - * |
|
| 40 | - * @param array $matches |
|
| 41 | - * @param string $dataType |
|
| 42 | - * @return Matcher |
|
| 43 | - */ |
|
| 44 | - public function getMatcher(array $matches = [], $dataType = ''): Matcher |
|
| 45 | - { |
|
| 46 | - if ($dataType === '') { |
|
| 47 | - $dataType = $this->getModuleLoader()->getDataType(); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** @var $matcher Matcher */ |
|
| 51 | - $matcher = GeneralUtility::makeInstance(Matcher::class, [], $dataType); |
|
| 52 | - |
|
| 53 | - $matcher = $this->applyCriteriaFromDataTables($matcher); |
|
| 54 | - $matcher = $this->applyCriteriaFromMatchesArgument($matcher, $matches); |
|
| 55 | - |
|
| 56 | - if ($this->isBackendMode()) { |
|
| 57 | - $matcher = $this->applyCriteriaFromUrl($matcher); |
|
| 58 | - $matcher = $this->applyCriteriaFromTSConfig($matcher); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - // Trigger signal for post processing Matcher Object. |
|
| 62 | - $this->emitPostProcessMatcherObjectSignal($matcher); |
|
| 63 | - |
|
| 64 | - return $matcher; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Get a possible id from the URL and apply as filter criteria. |
|
| 69 | - * Except if the main module belongs to the File. The id would be a combined identifier |
|
| 70 | - * including the storage and a mount point. |
|
| 71 | - * |
|
| 72 | - * @param Matcher $matcher |
|
| 73 | - * @return Matcher $matcher |
|
| 74 | - */ |
|
| 75 | - protected function applyCriteriaFromUrl(Matcher $matcher): Matcher |
|
| 76 | - { |
|
| 77 | - if (GeneralUtility::_GP('id') |
|
| 78 | - && !$this->getModuleLoader()->isPidIgnored() |
|
| 79 | - && $this->getModuleLoader()->getMainModule() !== ModuleName::FILE) { |
|
| 80 | - $matcher->equals('pid', GeneralUtility::_GP('id')); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - return $matcher; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * @param Matcher $matcher |
|
| 88 | - * @return Matcher $matcher |
|
| 89 | - */ |
|
| 90 | - protected function applyCriteriaFromTSConfig(Matcher $matcher): Matcher |
|
| 91 | - { |
|
| 92 | - $dataType = $matcher->getDataType(); |
|
| 93 | - $tsConfigPath = sprintf('tx_vidi.dataType.%s.constraints', $dataType); |
|
| 94 | - $tsConfig = $this->getBackendUser()->getTSConfig($tsConfigPath); |
|
| 95 | - |
|
| 96 | - if (is_array($tsConfig['properties']) && !empty($tsConfig['properties'])) { |
|
| 97 | - foreach ($tsConfig['properties'] as $constraint) { |
|
| 98 | - if (preg_match('/(.+) (>=|>|<|<=|=|like) (.+)/is', $constraint, $matches) && count($matches) === 4) { |
|
| 99 | - $operator = $matcher->getSupportedOperators()[strtolower(trim($matches[2]))]; |
|
| 100 | - $operand = trim($matches[1]); |
|
| 101 | - $value = trim($matches[3]); |
|
| 102 | - |
|
| 103 | - $matcher->$operator($operand, $value); |
|
| 104 | - } elseif (preg_match('/(.+) (in) (.+)/is', $constraint, $matches) && count($matches) === 4) { |
|
| 105 | - $operator = $matcher->getSupportedOperators()[trim($matches[2])]; |
|
| 106 | - $operand = trim($matches[1]); |
|
| 107 | - $value = trim($matches[3]); |
|
| 108 | - $matcher->$operator($operand, GeneralUtility::trimExplode(',', $value, true)); |
|
| 109 | - } |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - return $matcher; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * @param Matcher $matcher |
|
| 118 | - * @param array $matches |
|
| 119 | - * @return Matcher $matcher |
|
| 120 | - */ |
|
| 121 | - protected function applyCriteriaFromMatchesArgument(Matcher $matcher, $matches): Matcher |
|
| 122 | - { |
|
| 123 | - foreach ($matches as $fieldNameAndPath => $value) { |
|
| 124 | - // CSV values should be considered as "in" operator in the query, otherwise "equals". |
|
| 125 | - $explodedValues = GeneralUtility::trimExplode(',', $value, true); |
|
| 126 | - if (count($explodedValues) > 1) { |
|
| 127 | - $matcher->in($fieldNameAndPath, $explodedValues); |
|
| 128 | - } else { |
|
| 129 | - $matcher->equals($fieldNameAndPath, $explodedValues[0]); |
|
| 130 | - } |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - return $matcher; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Apply criteria specific to jQuery plugin DataTable. |
|
| 138 | - * |
|
| 139 | - * @param Matcher $matcher |
|
| 140 | - * @return Matcher $matcher |
|
| 141 | - */ |
|
| 142 | - protected function applyCriteriaFromDataTables(Matcher $matcher): Matcher |
|
| 143 | - { |
|
| 144 | - // Special case for Grid in the BE using jQuery DataTables plugin. |
|
| 145 | - // Retrieve a possible search term from GP. |
|
| 146 | - $query = GeneralUtility::_GP('search'); |
|
| 147 | - if (is_array($query)) { |
|
| 148 | - if (!empty($query['value'])) { |
|
| 149 | - $query = $query['value']; |
|
| 150 | - } else { |
|
| 151 | - $query = ''; |
|
| 152 | - } |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - if (strlen($query) > 0) { |
|
| 156 | - // Parse the json query coming from the Visual Search. |
|
| 157 | - $query = rawurldecode($query); |
|
| 158 | - $queryParts = json_decode($query, true); |
|
| 159 | - |
|
| 160 | - if (is_array($queryParts)) { |
|
| 161 | - $matcher = $this->parseQuery($queryParts, $matcher); |
|
| 162 | - } else { |
|
| 163 | - $matcher->setSearchTerm($query); |
|
| 164 | - } |
|
| 165 | - } |
|
| 166 | - return $matcher; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * @param array $queryParts |
|
| 171 | - * @param Matcher $matcher |
|
| 172 | - * @return Matcher $matcher |
|
| 173 | - */ |
|
| 174 | - protected function parseQuery(array $queryParts, Matcher $matcher): Matcher |
|
| 175 | - { |
|
| 176 | - $dataType = $matcher->getDataType(); |
|
| 177 | - foreach ($queryParts as $term) { |
|
| 178 | - $fieldNameAndPath = key($term); |
|
| 179 | - |
|
| 180 | - $resolvedDataType = $this->getFieldPathResolver()->getDataType($fieldNameAndPath, $dataType); |
|
| 181 | - $fieldName = $this->getFieldPathResolver()->stripFieldPath($fieldNameAndPath, $dataType); |
|
| 182 | - |
|
| 183 | - // Retrieve the value. |
|
| 184 | - $value = current($term); |
|
| 185 | - |
|
| 186 | - if (Tca::grid($resolvedDataType)->hasFacet($fieldName) && Tca::grid($resolvedDataType)->facet($fieldName)->canModifyMatcher()) { |
|
| 187 | - $matcher = Tca::grid($resolvedDataType)->facet($fieldName)->modifyMatcher($matcher, $value); |
|
| 188 | - } elseif (Tca::table($resolvedDataType)->hasField($fieldName)) { |
|
| 189 | - // Check whether the field exists and set it as "equal" or "like". |
|
| 190 | - if ($this->isOperatorEquals($fieldNameAndPath, $dataType, $value)) { |
|
| 191 | - $matcher->equals($fieldNameAndPath, $value); |
|
| 192 | - } else { |
|
| 193 | - $matcher->like($fieldNameAndPath, $value); |
|
| 194 | - } |
|
| 195 | - } elseif ($fieldNameAndPath === 'text') { |
|
| 196 | - // Special case if field is "text" which is a pseudo field in this case. |
|
| 197 | - // Set the search term which means Vidi will |
|
| 198 | - // search in various fields with operator "like". The fields come from key "searchFields" in the TCA. |
|
| 199 | - $matcher->setSearchTerm($value); |
|
| 200 | - } |
|
| 201 | - } |
|
| 202 | - return $matcher; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * Tell whether the operator should be equals instead of like for a search, e.g. if the value is numerical. |
|
| 207 | - * |
|
| 208 | - * @param string $fieldName |
|
| 209 | - * @param string $dataType |
|
| 210 | - * @param string $value |
|
| 211 | - * @return bool |
|
| 212 | - */ |
|
| 213 | - protected function isOperatorEquals($fieldName, $dataType, $value): bool |
|
| 214 | - { |
|
| 215 | - return (Tca::table($dataType)->field($fieldName)->hasRelation() && MathUtility::canBeInterpretedAsInteger($value)) |
|
| 216 | - || Tca::table($dataType)->field($fieldName)->isNumerical(); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * Signal that is called for post-processing a matcher object. |
|
| 221 | - * |
|
| 222 | - * @param Matcher $matcher |
|
| 223 | - */ |
|
| 224 | - protected function emitPostProcessMatcherObjectSignal(Matcher $matcher): void |
|
| 225 | - { |
|
| 226 | - if (strlen($matcher->getDataType()) <= 0) { |
|
| 227 | - /** @var ModuleLoader $moduleLoader */ |
|
| 228 | - $moduleLoader = GeneralUtility::makeInstance(ModuleLoader::class); |
|
| 229 | - $matcher->setDataType($moduleLoader->getDataType()); |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - $this->getSignalSlotDispatcher()->dispatch('Fab\Vidi\Controller\Backend\ContentController', 'postProcessMatcherObject', array($matcher, $matcher->getDataType())); |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * Get the SignalSlot dispatcher |
|
| 237 | - * |
|
| 238 | - * @return Dispatcher|object |
|
| 239 | - */ |
|
| 240 | - protected function getSignalSlotDispatcher() |
|
| 241 | - { |
|
| 242 | - return GeneralUtility::makeInstance(Dispatcher::class); |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * Get the Vidi Module Loader. |
|
| 247 | - * |
|
| 248 | - * @return ModuleLoader|object |
|
| 249 | - */ |
|
| 250 | - protected function getModuleLoader() |
|
| 251 | - { |
|
| 252 | - return GeneralUtility::makeInstance(ModuleLoader::class); |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * @return FieldPathResolver|object |
|
| 257 | - */ |
|
| 258 | - protected function getFieldPathResolver() |
|
| 259 | - { |
|
| 260 | - return GeneralUtility::makeInstance(FieldPathResolver::class); |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * Returns an instance of the current Backend User. |
|
| 265 | - * |
|
| 266 | - * @return BackendUserAuthentication |
|
| 267 | - */ |
|
| 268 | - protected function getBackendUser(): BackendUserAuthentication |
|
| 269 | - { |
|
| 270 | - return $GLOBALS['BE_USER']; |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * Returns whether the current mode is Backend |
|
| 275 | - * |
|
| 276 | - * @return bool |
|
| 277 | - */ |
|
| 278 | - protected function isBackendMode(): bool |
|
| 279 | - { |
|
| 280 | - return ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isBackend(); |
|
| 281 | - } |
|
| 27 | + /** |
|
| 28 | + * Gets a singleton instance of this class. |
|
| 29 | + * |
|
| 30 | + * @return $this |
|
| 31 | + */ |
|
| 32 | + public static function getInstance(): self |
|
| 33 | + { |
|
| 34 | + return GeneralUtility::makeInstance(self::class); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Returns a matcher object. |
|
| 39 | + * |
|
| 40 | + * @param array $matches |
|
| 41 | + * @param string $dataType |
|
| 42 | + * @return Matcher |
|
| 43 | + */ |
|
| 44 | + public function getMatcher(array $matches = [], $dataType = ''): Matcher |
|
| 45 | + { |
|
| 46 | + if ($dataType === '') { |
|
| 47 | + $dataType = $this->getModuleLoader()->getDataType(); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** @var $matcher Matcher */ |
|
| 51 | + $matcher = GeneralUtility::makeInstance(Matcher::class, [], $dataType); |
|
| 52 | + |
|
| 53 | + $matcher = $this->applyCriteriaFromDataTables($matcher); |
|
| 54 | + $matcher = $this->applyCriteriaFromMatchesArgument($matcher, $matches); |
|
| 55 | + |
|
| 56 | + if ($this->isBackendMode()) { |
|
| 57 | + $matcher = $this->applyCriteriaFromUrl($matcher); |
|
| 58 | + $matcher = $this->applyCriteriaFromTSConfig($matcher); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + // Trigger signal for post processing Matcher Object. |
|
| 62 | + $this->emitPostProcessMatcherObjectSignal($matcher); |
|
| 63 | + |
|
| 64 | + return $matcher; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Get a possible id from the URL and apply as filter criteria. |
|
| 69 | + * Except if the main module belongs to the File. The id would be a combined identifier |
|
| 70 | + * including the storage and a mount point. |
|
| 71 | + * |
|
| 72 | + * @param Matcher $matcher |
|
| 73 | + * @return Matcher $matcher |
|
| 74 | + */ |
|
| 75 | + protected function applyCriteriaFromUrl(Matcher $matcher): Matcher |
|
| 76 | + { |
|
| 77 | + if (GeneralUtility::_GP('id') |
|
| 78 | + && !$this->getModuleLoader()->isPidIgnored() |
|
| 79 | + && $this->getModuleLoader()->getMainModule() !== ModuleName::FILE) { |
|
| 80 | + $matcher->equals('pid', GeneralUtility::_GP('id')); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + return $matcher; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * @param Matcher $matcher |
|
| 88 | + * @return Matcher $matcher |
|
| 89 | + */ |
|
| 90 | + protected function applyCriteriaFromTSConfig(Matcher $matcher): Matcher |
|
| 91 | + { |
|
| 92 | + $dataType = $matcher->getDataType(); |
|
| 93 | + $tsConfigPath = sprintf('tx_vidi.dataType.%s.constraints', $dataType); |
|
| 94 | + $tsConfig = $this->getBackendUser()->getTSConfig($tsConfigPath); |
|
| 95 | + |
|
| 96 | + if (is_array($tsConfig['properties']) && !empty($tsConfig['properties'])) { |
|
| 97 | + foreach ($tsConfig['properties'] as $constraint) { |
|
| 98 | + if (preg_match('/(.+) (>=|>|<|<=|=|like) (.+)/is', $constraint, $matches) && count($matches) === 4) { |
|
| 99 | + $operator = $matcher->getSupportedOperators()[strtolower(trim($matches[2]))]; |
|
| 100 | + $operand = trim($matches[1]); |
|
| 101 | + $value = trim($matches[3]); |
|
| 102 | + |
|
| 103 | + $matcher->$operator($operand, $value); |
|
| 104 | + } elseif (preg_match('/(.+) (in) (.+)/is', $constraint, $matches) && count($matches) === 4) { |
|
| 105 | + $operator = $matcher->getSupportedOperators()[trim($matches[2])]; |
|
| 106 | + $operand = trim($matches[1]); |
|
| 107 | + $value = trim($matches[3]); |
|
| 108 | + $matcher->$operator($operand, GeneralUtility::trimExplode(',', $value, true)); |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + return $matcher; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * @param Matcher $matcher |
|
| 118 | + * @param array $matches |
|
| 119 | + * @return Matcher $matcher |
|
| 120 | + */ |
|
| 121 | + protected function applyCriteriaFromMatchesArgument(Matcher $matcher, $matches): Matcher |
|
| 122 | + { |
|
| 123 | + foreach ($matches as $fieldNameAndPath => $value) { |
|
| 124 | + // CSV values should be considered as "in" operator in the query, otherwise "equals". |
|
| 125 | + $explodedValues = GeneralUtility::trimExplode(',', $value, true); |
|
| 126 | + if (count($explodedValues) > 1) { |
|
| 127 | + $matcher->in($fieldNameAndPath, $explodedValues); |
|
| 128 | + } else { |
|
| 129 | + $matcher->equals($fieldNameAndPath, $explodedValues[0]); |
|
| 130 | + } |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + return $matcher; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Apply criteria specific to jQuery plugin DataTable. |
|
| 138 | + * |
|
| 139 | + * @param Matcher $matcher |
|
| 140 | + * @return Matcher $matcher |
|
| 141 | + */ |
|
| 142 | + protected function applyCriteriaFromDataTables(Matcher $matcher): Matcher |
|
| 143 | + { |
|
| 144 | + // Special case for Grid in the BE using jQuery DataTables plugin. |
|
| 145 | + // Retrieve a possible search term from GP. |
|
| 146 | + $query = GeneralUtility::_GP('search'); |
|
| 147 | + if (is_array($query)) { |
|
| 148 | + if (!empty($query['value'])) { |
|
| 149 | + $query = $query['value']; |
|
| 150 | + } else { |
|
| 151 | + $query = ''; |
|
| 152 | + } |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + if (strlen($query) > 0) { |
|
| 156 | + // Parse the json query coming from the Visual Search. |
|
| 157 | + $query = rawurldecode($query); |
|
| 158 | + $queryParts = json_decode($query, true); |
|
| 159 | + |
|
| 160 | + if (is_array($queryParts)) { |
|
| 161 | + $matcher = $this->parseQuery($queryParts, $matcher); |
|
| 162 | + } else { |
|
| 163 | + $matcher->setSearchTerm($query); |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | + return $matcher; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * @param array $queryParts |
|
| 171 | + * @param Matcher $matcher |
|
| 172 | + * @return Matcher $matcher |
|
| 173 | + */ |
|
| 174 | + protected function parseQuery(array $queryParts, Matcher $matcher): Matcher |
|
| 175 | + { |
|
| 176 | + $dataType = $matcher->getDataType(); |
|
| 177 | + foreach ($queryParts as $term) { |
|
| 178 | + $fieldNameAndPath = key($term); |
|
| 179 | + |
|
| 180 | + $resolvedDataType = $this->getFieldPathResolver()->getDataType($fieldNameAndPath, $dataType); |
|
| 181 | + $fieldName = $this->getFieldPathResolver()->stripFieldPath($fieldNameAndPath, $dataType); |
|
| 182 | + |
|
| 183 | + // Retrieve the value. |
|
| 184 | + $value = current($term); |
|
| 185 | + |
|
| 186 | + if (Tca::grid($resolvedDataType)->hasFacet($fieldName) && Tca::grid($resolvedDataType)->facet($fieldName)->canModifyMatcher()) { |
|
| 187 | + $matcher = Tca::grid($resolvedDataType)->facet($fieldName)->modifyMatcher($matcher, $value); |
|
| 188 | + } elseif (Tca::table($resolvedDataType)->hasField($fieldName)) { |
|
| 189 | + // Check whether the field exists and set it as "equal" or "like". |
|
| 190 | + if ($this->isOperatorEquals($fieldNameAndPath, $dataType, $value)) { |
|
| 191 | + $matcher->equals($fieldNameAndPath, $value); |
|
| 192 | + } else { |
|
| 193 | + $matcher->like($fieldNameAndPath, $value); |
|
| 194 | + } |
|
| 195 | + } elseif ($fieldNameAndPath === 'text') { |
|
| 196 | + // Special case if field is "text" which is a pseudo field in this case. |
|
| 197 | + // Set the search term which means Vidi will |
|
| 198 | + // search in various fields with operator "like". The fields come from key "searchFields" in the TCA. |
|
| 199 | + $matcher->setSearchTerm($value); |
|
| 200 | + } |
|
| 201 | + } |
|
| 202 | + return $matcher; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * Tell whether the operator should be equals instead of like for a search, e.g. if the value is numerical. |
|
| 207 | + * |
|
| 208 | + * @param string $fieldName |
|
| 209 | + * @param string $dataType |
|
| 210 | + * @param string $value |
|
| 211 | + * @return bool |
|
| 212 | + */ |
|
| 213 | + protected function isOperatorEquals($fieldName, $dataType, $value): bool |
|
| 214 | + { |
|
| 215 | + return (Tca::table($dataType)->field($fieldName)->hasRelation() && MathUtility::canBeInterpretedAsInteger($value)) |
|
| 216 | + || Tca::table($dataType)->field($fieldName)->isNumerical(); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * Signal that is called for post-processing a matcher object. |
|
| 221 | + * |
|
| 222 | + * @param Matcher $matcher |
|
| 223 | + */ |
|
| 224 | + protected function emitPostProcessMatcherObjectSignal(Matcher $matcher): void |
|
| 225 | + { |
|
| 226 | + if (strlen($matcher->getDataType()) <= 0) { |
|
| 227 | + /** @var ModuleLoader $moduleLoader */ |
|
| 228 | + $moduleLoader = GeneralUtility::makeInstance(ModuleLoader::class); |
|
| 229 | + $matcher->setDataType($moduleLoader->getDataType()); |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + $this->getSignalSlotDispatcher()->dispatch('Fab\Vidi\Controller\Backend\ContentController', 'postProcessMatcherObject', array($matcher, $matcher->getDataType())); |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * Get the SignalSlot dispatcher |
|
| 237 | + * |
|
| 238 | + * @return Dispatcher|object |
|
| 239 | + */ |
|
| 240 | + protected function getSignalSlotDispatcher() |
|
| 241 | + { |
|
| 242 | + return GeneralUtility::makeInstance(Dispatcher::class); |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * Get the Vidi Module Loader. |
|
| 247 | + * |
|
| 248 | + * @return ModuleLoader|object |
|
| 249 | + */ |
|
| 250 | + protected function getModuleLoader() |
|
| 251 | + { |
|
| 252 | + return GeneralUtility::makeInstance(ModuleLoader::class); |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * @return FieldPathResolver|object |
|
| 257 | + */ |
|
| 258 | + protected function getFieldPathResolver() |
|
| 259 | + { |
|
| 260 | + return GeneralUtility::makeInstance(FieldPathResolver::class); |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * Returns an instance of the current Backend User. |
|
| 265 | + * |
|
| 266 | + * @return BackendUserAuthentication |
|
| 267 | + */ |
|
| 268 | + protected function getBackendUser(): BackendUserAuthentication |
|
| 269 | + { |
|
| 270 | + return $GLOBALS['BE_USER']; |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * Returns whether the current mode is Backend |
|
| 275 | + * |
|
| 276 | + * @return bool |
|
| 277 | + */ |
|
| 278 | + protected function isBackendMode(): bool |
|
| 279 | + { |
|
| 280 | + return ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isBackend(); |
|
| 281 | + } |
|
| 282 | 282 | } |