@@ -18,94 +18,94 @@ |
||
| 18 | 18 | class Field implements SingletonInterface |
| 19 | 19 | { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * @var string |
|
| 23 | - */ |
|
| 24 | - static protected $currentField; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @var string |
|
| 28 | - */ |
|
| 29 | - static protected $currentTable; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var array |
|
| 33 | - */ |
|
| 34 | - protected $storage = []; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @param string $fieldName |
|
| 38 | - * @return $this |
|
| 39 | - * @throws \InvalidArgumentException |
|
| 40 | - */ |
|
| 41 | - static public function name($fieldName) |
|
| 42 | - { |
|
| 43 | - self::$currentField = $fieldName; |
|
| 44 | - self::$currentTable = ''; // reset the table name value. |
|
| 45 | - return GeneralUtility::makeInstance(self::class); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * @param string|Content $tableNameOrContentObject |
|
| 50 | - * @return $this |
|
| 51 | - */ |
|
| 52 | - public function of($tableNameOrContentObject) |
|
| 53 | - { |
|
| 54 | - // Resolve the table name. |
|
| 55 | - self::$currentTable = $tableNameOrContentObject instanceof Content ? |
|
| 56 | - $tableNameOrContentObject->getDataType() : |
|
| 57 | - $tableNameOrContentObject; |
|
| 58 | - return $this; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - public function toPropertyName() |
|
| 62 | - { |
|
| 63 | - |
|
| 64 | - $fieldName = $this->getFieldName(); |
|
| 65 | - $tableName = $this->getTableName(); |
|
| 66 | - |
|
| 67 | - if (empty($this->storage[$tableName][$fieldName])) { |
|
| 68 | - if (!array_key_exists($tableName, $this->storage)) { |
|
| 69 | - $this->storage[$tableName] = []; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - // Special case when the field name does not follow the conventions "field_name" => "fieldName". |
|
| 73 | - // Rely on mapping for those cases. |
|
| 74 | - if (!empty($GLOBALS['TCA'][$tableName]['vidi']['mappings'][$fieldName])) { |
|
| 75 | - $propertyName = $GLOBALS['TCA'][$tableName]['vidi']['mappings'][$fieldName]; |
|
| 76 | - } else { |
|
| 77 | - $propertyName = GeneralUtility::underscoredToLowerCamelCase($fieldName); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - $this->storage[$tableName][$fieldName] = $propertyName; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - return $this->storage[$tableName][$fieldName]; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * @return string |
|
| 88 | - * @throws \RuntimeException |
|
| 89 | - */ |
|
| 90 | - protected function getFieldName() |
|
| 91 | - { |
|
| 92 | - $fieldName = self::$currentField; |
|
| 93 | - if (empty($fieldName)) { |
|
| 94 | - throw new \RuntimeException('I could not find a field name value.', 1403203290); |
|
| 95 | - } |
|
| 96 | - return $fieldName; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * @return string |
|
| 101 | - */ |
|
| 102 | - protected function getTableName() |
|
| 103 | - { |
|
| 104 | - $tableName = self::$currentTable; |
|
| 105 | - if (empty($tableName)) { |
|
| 106 | - throw new \RuntimeException('I could not find a table name value.', 1403203291); |
|
| 107 | - } |
|
| 108 | - return $tableName; |
|
| 109 | - } |
|
| 21 | + /** |
|
| 22 | + * @var string |
|
| 23 | + */ |
|
| 24 | + static protected $currentField; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @var string |
|
| 28 | + */ |
|
| 29 | + static protected $currentTable; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var array |
|
| 33 | + */ |
|
| 34 | + protected $storage = []; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @param string $fieldName |
|
| 38 | + * @return $this |
|
| 39 | + * @throws \InvalidArgumentException |
|
| 40 | + */ |
|
| 41 | + static public function name($fieldName) |
|
| 42 | + { |
|
| 43 | + self::$currentField = $fieldName; |
|
| 44 | + self::$currentTable = ''; // reset the table name value. |
|
| 45 | + return GeneralUtility::makeInstance(self::class); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * @param string|Content $tableNameOrContentObject |
|
| 50 | + * @return $this |
|
| 51 | + */ |
|
| 52 | + public function of($tableNameOrContentObject) |
|
| 53 | + { |
|
| 54 | + // Resolve the table name. |
|
| 55 | + self::$currentTable = $tableNameOrContentObject instanceof Content ? |
|
| 56 | + $tableNameOrContentObject->getDataType() : |
|
| 57 | + $tableNameOrContentObject; |
|
| 58 | + return $this; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + public function toPropertyName() |
|
| 62 | + { |
|
| 63 | + |
|
| 64 | + $fieldName = $this->getFieldName(); |
|
| 65 | + $tableName = $this->getTableName(); |
|
| 66 | + |
|
| 67 | + if (empty($this->storage[$tableName][$fieldName])) { |
|
| 68 | + if (!array_key_exists($tableName, $this->storage)) { |
|
| 69 | + $this->storage[$tableName] = []; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + // Special case when the field name does not follow the conventions "field_name" => "fieldName". |
|
| 73 | + // Rely on mapping for those cases. |
|
| 74 | + if (!empty($GLOBALS['TCA'][$tableName]['vidi']['mappings'][$fieldName])) { |
|
| 75 | + $propertyName = $GLOBALS['TCA'][$tableName]['vidi']['mappings'][$fieldName]; |
|
| 76 | + } else { |
|
| 77 | + $propertyName = GeneralUtility::underscoredToLowerCamelCase($fieldName); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + $this->storage[$tableName][$fieldName] = $propertyName; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + return $this->storage[$tableName][$fieldName]; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * @return string |
|
| 88 | + * @throws \RuntimeException |
|
| 89 | + */ |
|
| 90 | + protected function getFieldName() |
|
| 91 | + { |
|
| 92 | + $fieldName = self::$currentField; |
|
| 93 | + if (empty($fieldName)) { |
|
| 94 | + throw new \RuntimeException('I could not find a field name value.', 1403203290); |
|
| 95 | + } |
|
| 96 | + return $fieldName; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * @return string |
|
| 101 | + */ |
|
| 102 | + protected function getTableName() |
|
| 103 | + { |
|
| 104 | + $tableName = self::$currentTable; |
|
| 105 | + if (empty($tableName)) { |
|
| 106 | + throw new \RuntimeException('I could not find a table name value.', 1403203291); |
|
| 107 | + } |
|
| 108 | + return $tableName; |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | 111 | } |
@@ -17,98 +17,98 @@ |
||
| 17 | 17 | class Property |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @var string |
|
| 22 | - */ |
|
| 23 | - static protected $currentProperty; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * @var string |
|
| 27 | - */ |
|
| 28 | - static protected $currentTable; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - protected $storage = []; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @param string $propertyName |
|
| 37 | - * @return $this |
|
| 38 | - * @throws \InvalidArgumentException |
|
| 39 | - */ |
|
| 40 | - static public function name($propertyName) |
|
| 41 | - { |
|
| 42 | - self::$currentProperty = $propertyName; |
|
| 43 | - self::$currentTable = ''; // reset the table name value. |
|
| 44 | - return GeneralUtility::makeInstance(self::class); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @param string|Content $tableNameOrContentObject |
|
| 49 | - * @return $this |
|
| 50 | - */ |
|
| 51 | - public function of($tableNameOrContentObject) |
|
| 52 | - { |
|
| 53 | - // Resolve the table name. |
|
| 54 | - self::$currentTable = $tableNameOrContentObject instanceof Content ? |
|
| 55 | - $tableNameOrContentObject->getDataType() : |
|
| 56 | - $tableNameOrContentObject; |
|
| 57 | - return $this; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - public function toFieldName() |
|
| 61 | - { |
|
| 62 | - $propertyName = $this->getPropertyName(); |
|
| 63 | - $tableName = $this->getTableName(); |
|
| 64 | - |
|
| 65 | - if (empty($this->storage[$tableName][$propertyName])) { |
|
| 66 | - if (!array_key_exists($tableName, $this->storage)) { |
|
| 67 | - $this->storage[$tableName] = []; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - // Default case |
|
| 71 | - $fieldName = GeneralUtility::camelCaseToLowerCaseUnderscored($propertyName); |
|
| 72 | - |
|
| 73 | - // Special case in case the field name does not follow the conventions "field_name" => "fieldName" |
|
| 74 | - // There is the chance to make some mapping |
|
| 75 | - if (!empty($GLOBALS['TCA'][$tableName]['vidi']['mappings'])) { |
|
| 76 | - $key = array_search($propertyName, $GLOBALS['TCA'][$tableName]['vidi']['mappings']); |
|
| 77 | - if ($key !== false) { |
|
| 78 | - $fieldName = $key; |
|
| 79 | - } |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - $this->storage[$tableName][$propertyName] = $fieldName; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - return $this->storage[$tableName][$propertyName]; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @return string |
|
| 90 | - * @throws \RuntimeException |
|
| 91 | - */ |
|
| 92 | - protected function getPropertyName() |
|
| 93 | - { |
|
| 94 | - $propertyName = self::$currentProperty; |
|
| 95 | - if (empty($propertyName)) { |
|
| 96 | - throw new \RuntimeException('I could not find a field name value.', 1403203290); |
|
| 97 | - } |
|
| 98 | - return $propertyName; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * @return string |
|
| 103 | - * @throws \RuntimeException |
|
| 104 | - */ |
|
| 105 | - protected function getTableName() |
|
| 106 | - { |
|
| 107 | - $tableName = self::$currentTable; |
|
| 108 | - if (empty($tableName)) { |
|
| 109 | - throw new \RuntimeException('I could not find a table name value.', 1403203291); |
|
| 110 | - } |
|
| 111 | - return $tableName; |
|
| 112 | - } |
|
| 20 | + /** |
|
| 21 | + * @var string |
|
| 22 | + */ |
|
| 23 | + static protected $currentProperty; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * @var string |
|
| 27 | + */ |
|
| 28 | + static protected $currentTable; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + protected $storage = []; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @param string $propertyName |
|
| 37 | + * @return $this |
|
| 38 | + * @throws \InvalidArgumentException |
|
| 39 | + */ |
|
| 40 | + static public function name($propertyName) |
|
| 41 | + { |
|
| 42 | + self::$currentProperty = $propertyName; |
|
| 43 | + self::$currentTable = ''; // reset the table name value. |
|
| 44 | + return GeneralUtility::makeInstance(self::class); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @param string|Content $tableNameOrContentObject |
|
| 49 | + * @return $this |
|
| 50 | + */ |
|
| 51 | + public function of($tableNameOrContentObject) |
|
| 52 | + { |
|
| 53 | + // Resolve the table name. |
|
| 54 | + self::$currentTable = $tableNameOrContentObject instanceof Content ? |
|
| 55 | + $tableNameOrContentObject->getDataType() : |
|
| 56 | + $tableNameOrContentObject; |
|
| 57 | + return $this; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + public function toFieldName() |
|
| 61 | + { |
|
| 62 | + $propertyName = $this->getPropertyName(); |
|
| 63 | + $tableName = $this->getTableName(); |
|
| 64 | + |
|
| 65 | + if (empty($this->storage[$tableName][$propertyName])) { |
|
| 66 | + if (!array_key_exists($tableName, $this->storage)) { |
|
| 67 | + $this->storage[$tableName] = []; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + // Default case |
|
| 71 | + $fieldName = GeneralUtility::camelCaseToLowerCaseUnderscored($propertyName); |
|
| 72 | + |
|
| 73 | + // Special case in case the field name does not follow the conventions "field_name" => "fieldName" |
|
| 74 | + // There is the chance to make some mapping |
|
| 75 | + if (!empty($GLOBALS['TCA'][$tableName]['vidi']['mappings'])) { |
|
| 76 | + $key = array_search($propertyName, $GLOBALS['TCA'][$tableName]['vidi']['mappings']); |
|
| 77 | + if ($key !== false) { |
|
| 78 | + $fieldName = $key; |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + $this->storage[$tableName][$propertyName] = $fieldName; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + return $this->storage[$tableName][$propertyName]; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @return string |
|
| 90 | + * @throws \RuntimeException |
|
| 91 | + */ |
|
| 92 | + protected function getPropertyName() |
|
| 93 | + { |
|
| 94 | + $propertyName = self::$currentProperty; |
|
| 95 | + if (empty($propertyName)) { |
|
| 96 | + throw new \RuntimeException('I could not find a field name value.', 1403203290); |
|
| 97 | + } |
|
| 98 | + return $propertyName; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * @return string |
|
| 103 | + * @throws \RuntimeException |
|
| 104 | + */ |
|
| 105 | + protected function getTableName() |
|
| 106 | + { |
|
| 107 | + $tableName = self::$currentTable; |
|
| 108 | + if (empty($tableName)) { |
|
| 109 | + throw new \RuntimeException('I could not find a table name value.', 1403203291); |
|
| 110 | + } |
|
| 111 | + return $tableName; |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | 114 | } |
@@ -51,1052 +51,1052 @@ |
||
| 51 | 51 | class VidiDbBackend |
| 52 | 52 | { |
| 53 | 53 | |
| 54 | - const OPERATOR_EQUAL_TO_NULL = 'operatorEqualToNull'; |
|
| 55 | - const OPERATOR_NOT_EQUAL_TO_NULL = 'operatorNotEqualToNull'; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * The TYPO3 page repository. Used for language and workspace overlay |
|
| 59 | - * |
|
| 60 | - * @var PageRepository |
|
| 61 | - */ |
|
| 62 | - protected $pageRepository; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * @var Query |
|
| 66 | - */ |
|
| 67 | - protected $query; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Store some info related to table name and its aliases. |
|
| 71 | - * |
|
| 72 | - * @var array |
|
| 73 | - */ |
|
| 74 | - protected $tableNameAliases = array( |
|
| 75 | - 'aliases' => [], |
|
| 76 | - 'aliasIncrement' => [], |
|
| 77 | - ); |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Use to store the current foreign table name alias. |
|
| 81 | - * |
|
| 82 | - * @var string |
|
| 83 | - */ |
|
| 84 | - protected $currentChildTableNameAlias = ''; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * @param Query $query |
|
| 88 | - */ |
|
| 89 | - public function __construct(Query $query) |
|
| 90 | - { |
|
| 91 | - $this->query = $query; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * @param $parameters |
|
| 96 | - * @return array |
|
| 97 | - */ |
|
| 98 | - protected static function getTypes($parameters) |
|
| 99 | - { |
|
| 100 | - $types = []; |
|
| 101 | - foreach ($parameters as $parameter) { |
|
| 102 | - if (is_array($parameter)) { |
|
| 103 | - if (MathUtility::canBeInterpretedAsInteger($parameter[0])) { |
|
| 104 | - $types[] = \Doctrine\DBAL\Connection::PARAM_INT_ARRAY; |
|
| 105 | - } else { |
|
| 106 | - $types[] = \Doctrine\DBAL\Connection::PARAM_STR_ARRAY; |
|
| 107 | - } |
|
| 108 | - } else { |
|
| 109 | - if (MathUtility::canBeInterpretedAsInteger($parameter)) { |
|
| 110 | - $types[] = ParameterType::INTEGER; |
|
| 111 | - } else { |
|
| 112 | - $types[] = ParameterType::STRING; |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - } |
|
| 116 | - return $types; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Returns the result of the query |
|
| 121 | - */ |
|
| 122 | - public function fetchResult() |
|
| 123 | - { |
|
| 124 | - $parameters = []; |
|
| 125 | - $statementParts = $this->parseQuery($parameters); |
|
| 126 | - $statementParts = $this->processStatementStructureForRecursiveMMRelation($statementParts); |
|
| 127 | - $sql = $this->buildQuery($statementParts); |
|
| 128 | - //print $sql; exit(); |
|
| 129 | - |
|
| 130 | - $rows = $this->getConnection() |
|
| 131 | - ->executeQuery($sql, $parameters, self::getTypes($parameters)) |
|
| 132 | - ->fetchAll(); |
|
| 133 | - |
|
| 134 | - return $this->getContentObjects($rows); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Returns the number of tuples matching the query. |
|
| 139 | - * |
|
| 140 | - * @return int The number of matching tuples |
|
| 141 | - */ |
|
| 142 | - public function countResult() |
|
| 143 | - { |
|
| 144 | - $parameters = []; |
|
| 145 | - $statementParts = $this->parseQuery($parameters); |
|
| 146 | - $statementParts = $this->processStatementStructureForRecursiveMMRelation($statementParts); |
|
| 147 | - $types = self::getTypes($parameters); |
|
| 148 | - |
|
| 149 | - // if limit is set, we need to count the rows "manually" as COUNT(*) ignores LIMIT constraints |
|
| 150 | - if (!empty($statementParts['limit'])) { |
|
| 151 | - $sql = $this->buildQuery($statementParts); |
|
| 152 | - |
|
| 153 | - $count = $this |
|
| 154 | - ->getConnection() |
|
| 155 | - ->executeQuery($sql, $parameters, $types) |
|
| 156 | - ->rowCount(); |
|
| 157 | - } else { |
|
| 158 | - $statementParts['fields'] = array('COUNT(*)'); |
|
| 159 | - // having orderings without grouping is not compatible with non-MySQL DBMS |
|
| 160 | - $statementParts['orderings'] = []; |
|
| 161 | - if (isset($statementParts['keywords']['distinct'])) { |
|
| 162 | - unset($statementParts['keywords']['distinct']); |
|
| 163 | - $distinctField = $this->query->getDistinct() ? $this->query->getDistinct() : 'uid'; |
|
| 164 | - $statementParts['fields'] = array('COUNT(DISTINCT ' . $statementParts['mainTable'] . '.' . $distinctField . ')'); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - $sql = $this->buildQuery($statementParts); |
|
| 168 | - $count = $this |
|
| 169 | - ->getConnection() |
|
| 170 | - ->executeQuery($sql, $parameters, $types) |
|
| 171 | - ->fetchColumn(0); |
|
| 172 | - } |
|
| 173 | - return (int)$count; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * Parses the query and returns the SQL statement parts. |
|
| 178 | - * |
|
| 179 | - * @param array &$parameters |
|
| 180 | - * @return array |
|
| 181 | - */ |
|
| 182 | - public function parseQuery(array &$parameters) |
|
| 183 | - { |
|
| 184 | - $statementParts = []; |
|
| 185 | - $statementParts['keywords'] = []; |
|
| 186 | - $statementParts['tables'] = []; |
|
| 187 | - $statementParts['unions'] = []; |
|
| 188 | - $statementParts['fields'] = []; |
|
| 189 | - $statementParts['where'] = []; |
|
| 190 | - $statementParts['additionalWhereClause'] = []; |
|
| 191 | - $statementParts['orderings'] = []; |
|
| 192 | - $statementParts['limit'] = []; |
|
| 193 | - $query = $this->query; |
|
| 194 | - $source = $query->getSource(); |
|
| 195 | - $this->parseSource($source, $statementParts); |
|
| 196 | - $this->parseConstraint($query->getConstraint(), $source, $statementParts, $parameters); |
|
| 197 | - $this->parseOrderings($query->getOrderings(), $source, $statementParts); |
|
| 198 | - $this->parseLimitAndOffset($query->getLimit(), $query->getOffset(), $statementParts); |
|
| 199 | - $tableNames = array_unique(array_keys($statementParts['tables'] + $statementParts['unions'])); |
|
| 200 | - foreach ($tableNames as $tableNameOrAlias) { |
|
| 201 | - if (is_string($tableNameOrAlias) && strlen($tableNameOrAlias) > 0) { |
|
| 202 | - $this->addAdditionalWhereClause($query->getTypo3QuerySettings(), $tableNameOrAlias, $statementParts); |
|
| 203 | - } |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - return $statementParts; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Fiddle with the statement structure to handle recursive MM relations. |
|
| 211 | - * For the recursive MM query to work, we must invert some values. |
|
| 212 | - * Let see if that is the best way of doing that... |
|
| 213 | - * |
|
| 214 | - * @param array $statementParts |
|
| 215 | - * @return array |
|
| 216 | - */ |
|
| 217 | - public function processStatementStructureForRecursiveMMRelation(array $statementParts) |
|
| 218 | - { |
|
| 219 | - |
|
| 220 | - if ($this->hasRecursiveMMRelation()) { |
|
| 221 | - $tableName = $this->query->getType(); |
|
| 222 | - |
|
| 223 | - // In order the MM query to work for a recursive MM query, we must invert some values. |
|
| 224 | - // tx_domain_model_foo0 (the alias) <--> tx_domain_model_foo (the origin table name) |
|
| 225 | - $values = []; |
|
| 226 | - foreach ($statementParts['fields'] as $key => $value) { |
|
| 227 | - $values[$key] = str_replace($tableName, $tableName . '0', $value); |
|
| 228 | - } |
|
| 229 | - $statementParts['fields'] = $values; |
|
| 230 | - |
|
| 231 | - // Same comment as above. |
|
| 232 | - $values = []; |
|
| 233 | - foreach ($statementParts['where'] as $key => $value) { |
|
| 234 | - $values[$key] = str_replace($tableName . '0', $tableName, $value); |
|
| 235 | - } |
|
| 236 | - $statementParts['where'] = $values; |
|
| 237 | - |
|
| 238 | - // We must be more restrictive by transforming the "left" union by "inner" |
|
| 239 | - $values = []; |
|
| 240 | - foreach ($statementParts['unions'] as $key => $value) { |
|
| 241 | - $values[$key] = str_replace('LEFT JOIN', 'INNER JOIN', $value); |
|
| 242 | - } |
|
| 243 | - $statementParts['unions'] = $values; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - return $statementParts; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * Tell whether there is a recursive MM relation. |
|
| 251 | - * |
|
| 252 | - * @return bool |
|
| 253 | - */ |
|
| 254 | - public function hasRecursiveMMRelation() |
|
| 255 | - { |
|
| 256 | - return isset($this->tableNameAliases['aliasIncrement'][$this->query->getType()]); |
|
| 257 | - |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * Returns the statement, ready to be executed. |
|
| 262 | - * |
|
| 263 | - * @param array $statementParts The SQL statement parts |
|
| 264 | - * @return string The SQL statement |
|
| 265 | - */ |
|
| 266 | - public function buildQuery(array $statementParts) |
|
| 267 | - { |
|
| 268 | - |
|
| 269 | - // Add more statement to the UNION part. |
|
| 270 | - if (!empty($statementParts['unions'])) { |
|
| 271 | - foreach ($statementParts['unions'] as $tableName => $unionPart) { |
|
| 272 | - if (!empty($statementParts['additionalWhereClause'][$tableName])) { |
|
| 273 | - $statementParts['unions'][$tableName] .= ' AND ' . implode(' AND ', $statementParts['additionalWhereClause'][$tableName]); |
|
| 274 | - } |
|
| 275 | - } |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - $statement = 'SELECT ' . implode(' ', $statementParts['keywords']) . ' ' . implode(',', $statementParts['fields']) . ' FROM ' . implode(' ', $statementParts['tables']) . ' ' . implode(' ', $statementParts['unions']); |
|
| 279 | - if (!empty($statementParts['where'])) { |
|
| 280 | - $statement .= ' WHERE ' . implode('', $statementParts['where']); |
|
| 281 | - if (!empty($statementParts['additionalWhereClause'][$this->query->getType()])) { |
|
| 282 | - $statement .= ' AND ' . implode(' AND ', $statementParts['additionalWhereClause'][$this->query->getType()]); |
|
| 283 | - } |
|
| 284 | - } elseif (!empty($statementParts['additionalWhereClause'])) { |
|
| 285 | - $statement .= ' WHERE ' . implode(' AND ', $statementParts['additionalWhereClause'][$this->query->getType()]); |
|
| 286 | - } |
|
| 287 | - if (!empty($statementParts['orderings'])) { |
|
| 288 | - $statement .= ' ORDER BY ' . implode(', ', $statementParts['orderings']); |
|
| 289 | - } |
|
| 290 | - if (!empty($statementParts['limit'])) { |
|
| 291 | - $statement .= ' LIMIT ' . $statementParts['limit']; |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - return $statement; |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - /** |
|
| 298 | - * Transforms a Query Source into SQL and parameter arrays |
|
| 299 | - * |
|
| 300 | - * @param SourceInterface $source The source |
|
| 301 | - * @param array &$sql |
|
| 302 | - * @return void |
|
| 303 | - */ |
|
| 304 | - protected function parseSource(SourceInterface $source, array &$sql) |
|
| 305 | - { |
|
| 306 | - $tableName = $this->getTableName(); |
|
| 307 | - $sql['fields'][$tableName] = $tableName . '.*'; |
|
| 308 | - if ($this->query->getDistinct()) { |
|
| 309 | - $sql['fields'][$tableName] = $tableName . '.' . $this->query->getDistinct(); |
|
| 310 | - $sql['keywords']['distinct'] = 'DISTINCT'; |
|
| 311 | - } |
|
| 312 | - $sql['tables'][$tableName] = $tableName; |
|
| 313 | - $sql['mainTable'] = $tableName; |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - /** |
|
| 317 | - * Transforms a constraint into SQL and parameter arrays |
|
| 318 | - * |
|
| 319 | - * @param ConstraintInterface $constraint The constraint |
|
| 320 | - * @param SourceInterface $source The source |
|
| 321 | - * @param array &$statementParts The query parts |
|
| 322 | - * @param array &$parameters The parameters that will replace the markers |
|
| 323 | - * @return void |
|
| 324 | - */ |
|
| 325 | - protected function parseConstraint(ConstraintInterface $constraint = null, SourceInterface $source, array &$statementParts, array &$parameters) |
|
| 326 | - { |
|
| 327 | - if ($constraint instanceof AndInterface) { |
|
| 328 | - $statementParts['where'][] = '('; |
|
| 329 | - $this->parseConstraint($constraint->getConstraint1(), $source, $statementParts, $parameters); |
|
| 330 | - $statementParts['where'][] = ' AND '; |
|
| 331 | - $this->parseConstraint($constraint->getConstraint2(), $source, $statementParts, $parameters); |
|
| 332 | - $statementParts['where'][] = ')'; |
|
| 333 | - } elseif ($constraint instanceof OrInterface) { |
|
| 334 | - $statementParts['where'][] = '('; |
|
| 335 | - $this->parseConstraint($constraint->getConstraint1(), $source, $statementParts, $parameters); |
|
| 336 | - $statementParts['where'][] = ' OR '; |
|
| 337 | - $this->parseConstraint($constraint->getConstraint2(), $source, $statementParts, $parameters); |
|
| 338 | - $statementParts['where'][] = ')'; |
|
| 339 | - } elseif ($constraint instanceof NotInterface) { |
|
| 340 | - $statementParts['where'][] = 'NOT ('; |
|
| 341 | - $this->parseConstraint($constraint->getConstraint(), $source, $statementParts, $parameters); |
|
| 342 | - $statementParts['where'][] = ')'; |
|
| 343 | - } elseif ($constraint instanceof ComparisonInterface) { |
|
| 344 | - $this->parseComparison($constraint, $source, $statementParts, $parameters); |
|
| 345 | - } |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * Parse a Comparison into SQL and parameter arrays. |
|
| 350 | - * |
|
| 351 | - * @param ComparisonInterface $comparison The comparison to parse |
|
| 352 | - * @param SourceInterface $source The source |
|
| 353 | - * @param array &$statementParts SQL query parts to add to |
|
| 354 | - * @param array &$parameters Parameters to bind to the SQL |
|
| 355 | - * @return void |
|
| 356 | - * @throws Exception\RepositoryException |
|
| 357 | - */ |
|
| 358 | - protected function parseComparison(ComparisonInterface $comparison, SourceInterface $source, array &$statementParts, array &$parameters) |
|
| 359 | - { |
|
| 360 | - $operand1 = $comparison->getOperand1(); |
|
| 361 | - $operator = $comparison->getOperator(); |
|
| 362 | - $operand2 = $comparison->getOperand2(); |
|
| 363 | - if ($operator === QueryInterface::OPERATOR_IN) { |
|
| 364 | - $items = []; |
|
| 365 | - $hasValue = false; |
|
| 366 | - foreach ($operand2 as $value) { |
|
| 367 | - $value = $this->getPlainValue($value); |
|
| 368 | - if ($value !== null) { |
|
| 369 | - $items[] = $value; |
|
| 370 | - $hasValue = true; |
|
| 371 | - } |
|
| 372 | - } |
|
| 373 | - if ($hasValue === false) { |
|
| 374 | - $statementParts['where'][] = '1<>1'; |
|
| 375 | - } else { |
|
| 376 | - $this->parseDynamicOperand($operand1, $operator, $source, $statementParts, $parameters, null); |
|
| 377 | - $parameters[] = $items; |
|
| 378 | - } |
|
| 379 | - } elseif ($operator === QueryInterface::OPERATOR_CONTAINS) { |
|
| 380 | - if ($operand2 === null) { |
|
| 381 | - $statementParts['where'][] = '1<>1'; |
|
| 382 | - } else { |
|
| 383 | - throw new \Exception('Not implemented! Contact extension author.', 1412931227); |
|
| 384 | - # @todo re-implement me if necessary. |
|
| 385 | - #$tableName = $this->query->getType(); |
|
| 386 | - #$propertyName = $operand1->getPropertyName(); |
|
| 387 | - #while (strpos($propertyName, '.') !== false) { |
|
| 388 | - # $this->addUnionStatement($tableName, $propertyName, $statementParts); |
|
| 389 | - #} |
|
| 390 | - #$columnName = $propertyName; |
|
| 391 | - #$columnMap = $propertyName; |
|
| 392 | - #$typeOfRelation = $columnMap instanceof ColumnMap ? $columnMap->getTypeOfRelation() : null; |
|
| 393 | - #if ($typeOfRelation === ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY) { |
|
| 394 | - # $relationTableName = $columnMap->getRelationTableName(); |
|
| 395 | - # $statementParts['where'][] = $tableName . '.uid IN (SELECT ' . $columnMap->getParentKeyFieldName() . ' FROM ' . $relationTableName . ' WHERE ' . $columnMap->getChildKeyFieldName() . '=?)'; |
|
| 396 | - # $parameters[] = intval($this->getPlainValue($operand2)); |
|
| 397 | - #} elseif ($typeOfRelation === ColumnMap::RELATION_HAS_MANY) { |
|
| 398 | - # $parentKeyFieldName = $columnMap->getParentKeyFieldName(); |
|
| 399 | - # if (isset($parentKeyFieldName)) { |
|
| 400 | - # $childTableName = $columnMap->getChildTableName(); |
|
| 401 | - # $statementParts['where'][] = $tableName . '.uid=(SELECT ' . $childTableName . '.' . $parentKeyFieldName . ' FROM ' . $childTableName . ' WHERE ' . $childTableName . '.uid=?)'; |
|
| 402 | - # $parameters[] = intval($this->getPlainValue($operand2)); |
|
| 403 | - # } else { |
|
| 404 | - # $statementParts['where'][] = 'FIND_IN_SET(?,' . $tableName . '.' . $columnName . ')'; |
|
| 405 | - # $parameters[] = intval($this->getPlainValue($operand2)); |
|
| 406 | - # } |
|
| 407 | - #} else { |
|
| 408 | - # throw new Exception\RepositoryException('Unsupported or non-existing property name "' . $propertyName . '" used in relation matching.', 1327065745); |
|
| 409 | - #} |
|
| 410 | - } |
|
| 411 | - } else { |
|
| 412 | - if ($operand2 === null) { |
|
| 413 | - if ($operator === QueryInterface::OPERATOR_EQUAL_TO) { |
|
| 414 | - $operator = self::OPERATOR_EQUAL_TO_NULL; |
|
| 415 | - } elseif ($operator === QueryInterface::OPERATOR_NOT_EQUAL_TO) { |
|
| 416 | - $operator = self::OPERATOR_NOT_EQUAL_TO_NULL; |
|
| 417 | - } |
|
| 418 | - } |
|
| 419 | - $this->parseDynamicOperand($operand1, $operator, $source, $statementParts, $parameters); |
|
| 420 | - $parameters[] = $this->getPlainValue($operand2); |
|
| 421 | - } |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - /** |
|
| 425 | - * Returns a plain value, i.e. objects are flattened if possible. |
|
| 426 | - * |
|
| 427 | - * @param mixed $input |
|
| 428 | - * @return mixed |
|
| 429 | - * @throws UnexpectedTypeException |
|
| 430 | - */ |
|
| 431 | - protected function getPlainValue($input) |
|
| 432 | - { |
|
| 433 | - if (is_array($input)) { |
|
| 434 | - throw new UnexpectedTypeException('An array could not be converted to a plain value.', 1274799932); |
|
| 435 | - } |
|
| 436 | - if ($input instanceof \DateTime) { |
|
| 437 | - return $input->format('U'); |
|
| 438 | - } elseif (is_object($input)) { |
|
| 439 | - if ($input instanceof LazyLoadingProxy) { |
|
| 440 | - $realInput = $input->_loadRealInstance(); |
|
| 441 | - } else { |
|
| 442 | - $realInput = $input; |
|
| 443 | - } |
|
| 444 | - if ($realInput instanceof DomainObjectInterface) { |
|
| 445 | - return $realInput->getUid(); |
|
| 446 | - } else { |
|
| 447 | - throw new UnexpectedTypeException('An object of class "' . get_class($realInput) . '" could not be converted to a plain value.', 1274799934); |
|
| 448 | - } |
|
| 449 | - } elseif (is_bool($input)) { |
|
| 450 | - return $input === true ? 1 : 0; |
|
| 451 | - } else { |
|
| 452 | - return $input; |
|
| 453 | - } |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - /** |
|
| 457 | - * Parse a DynamicOperand into SQL and parameter arrays. |
|
| 458 | - * |
|
| 459 | - * @param DynamicOperandInterface $operand |
|
| 460 | - * @param string $operator One of the JCR_OPERATOR_* constants |
|
| 461 | - * @param SourceInterface $source The source |
|
| 462 | - * @param array &$statementParts The query parts |
|
| 463 | - * @param array &$parameters The parameters that will replace the markers |
|
| 464 | - * @param string $valueFunction an optional SQL function to apply to the operand value |
|
| 465 | - * @return void |
|
| 466 | - */ |
|
| 467 | - protected function parseDynamicOperand(DynamicOperandInterface $operand, $operator, SourceInterface $source, array &$statementParts, array &$parameters, $valueFunction = null) |
|
| 468 | - { |
|
| 469 | - if ($operand instanceof LowerCaseInterface) { |
|
| 470 | - $this->parseDynamicOperand($operand->getOperand(), $operator, $source, $statementParts, $parameters, 'LOWER'); |
|
| 471 | - } elseif ($operand instanceof UpperCaseInterface) { |
|
| 472 | - $this->parseDynamicOperand($operand->getOperand(), $operator, $source, $statementParts, $parameters, 'UPPER'); |
|
| 473 | - } elseif ($operand instanceof PropertyValueInterface) { |
|
| 474 | - $propertyName = $operand->getPropertyName(); |
|
| 475 | - |
|
| 476 | - // Reset value. |
|
| 477 | - $this->currentChildTableNameAlias = ''; |
|
| 478 | - |
|
| 479 | - if ($source instanceof SelectorInterface) { |
|
| 480 | - $tableName = $this->query->getType(); |
|
| 481 | - while (strpos($propertyName, '.') !== false) { |
|
| 482 | - $this->addUnionStatement($tableName, $propertyName, $statementParts); |
|
| 483 | - } |
|
| 484 | - } elseif ($source instanceof JoinInterface) { |
|
| 485 | - $tableName = $source->getJoinCondition()->getSelector1Name(); |
|
| 486 | - } |
|
| 487 | - |
|
| 488 | - $columnName = $propertyName; |
|
| 489 | - $resolvedOperator = $this->resolveOperator($operator); |
|
| 490 | - $constraintSQL = ''; |
|
| 491 | - |
|
| 492 | - $marker = $operator === QueryInterface::OPERATOR_IN |
|
| 493 | - ? '(?)' |
|
| 494 | - : '?'; |
|
| 495 | - |
|
| 496 | - if ($valueFunction === null) { |
|
| 497 | - $constraintSQL .= (!empty($tableName) ? $tableName . '.' : '') . $columnName . ' ' . $resolvedOperator . ' ' . $marker; |
|
| 498 | - } else { |
|
| 499 | - $constraintSQL .= $valueFunction . '(' . (!empty($tableName) ? $tableName . '.' : '') . $columnName . ') ' . $resolvedOperator . ' ' . $marker; |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - if (isset($tableName) && !empty($this->currentChildTableNameAlias)) { |
|
| 503 | - $constraintSQL = $this->replaceTableNameByAlias($tableName, $this->currentChildTableNameAlias, $constraintSQL); |
|
| 504 | - } |
|
| 505 | - $statementParts['where'][] = $constraintSQL; |
|
| 506 | - } |
|
| 507 | - } |
|
| 508 | - |
|
| 509 | - /** |
|
| 510 | - * @param string &$tableName |
|
| 511 | - * @param string &$propertyPath |
|
| 512 | - * @param array &$statementParts |
|
| 513 | - */ |
|
| 514 | - protected function addUnionStatement(&$tableName, &$propertyPath, array &$statementParts) |
|
| 515 | - { |
|
| 516 | - |
|
| 517 | - $table = Tca::table($tableName); |
|
| 518 | - |
|
| 519 | - $explodedPropertyPath = explode('.', $propertyPath, 2); |
|
| 520 | - $fieldName = $explodedPropertyPath[0]; |
|
| 521 | - |
|
| 522 | - // Field of type "group" are special because property path must contain the table name |
|
| 523 | - // to determine the relation type. Example for sys_category, property path will look like "items.sys_file" |
|
| 524 | - $parts = explode('.', $propertyPath, 3); |
|
| 525 | - if ($table->field($fieldName)->isGroup() && count($parts) > 2) { |
|
| 526 | - $explodedPropertyPath[0] = $parts[0] . '.' . $parts[1]; |
|
| 527 | - $explodedPropertyPath[1] = $parts[2]; |
|
| 528 | - $fieldName = $explodedPropertyPath[0]; |
|
| 529 | - } |
|
| 530 | - |
|
| 531 | - $parentKeyFieldName = $table->field($fieldName)->getForeignField(); |
|
| 532 | - $childTableName = $table->field($fieldName)->getForeignTable(); |
|
| 533 | - |
|
| 534 | - if ($childTableName === null) { |
|
| 535 | - throw new InvalidRelationConfigurationException('The relation information for property "' . $fieldName . '" of class "' . $tableName . '" is missing.', 1353170925); |
|
| 536 | - } |
|
| 537 | - |
|
| 538 | - if ($table->field($fieldName)->hasOne()) { // includes relation "one-to-one" and "many-to-one" |
|
| 539 | - // sometimes the opposite relation is not defined. We don't want to force this config for backward compatibility reasons. |
|
| 540 | - // $parentKeyFieldName === null does the trick somehow. Before condition was if (isset($parentKeyFieldName)) |
|
| 541 | - if ($table->field($fieldName)->hasRelationManyToOne() || $parentKeyFieldName === null) { |
|
| 542 | - $statementParts['unions'][$childTableName] = 'LEFT JOIN ' . $childTableName . ' ON ' . $tableName . '.' . $fieldName . '=' . $childTableName . '.uid'; |
|
| 543 | - } else { |
|
| 544 | - $statementParts['unions'][$childTableName] = 'LEFT JOIN ' . $childTableName . ' ON ' . $tableName . '.uid=' . $childTableName . '.' . $parentKeyFieldName; |
|
| 545 | - } |
|
| 546 | - } elseif ($table->field($fieldName)->hasRelationManyToMany()) { |
|
| 547 | - $relationTableName = $table->field($fieldName)->getManyToManyTable(); |
|
| 548 | - |
|
| 549 | - $parentKeyFieldName = $table->field($fieldName)->isOppositeRelation() ? 'uid_foreign' : 'uid_local'; |
|
| 550 | - $childKeyFieldName = !$table->field($fieldName)->isOppositeRelation() ? 'uid_foreign' : 'uid_local'; |
|
| 551 | - |
|
| 552 | - // MM table e.g sys_category_record_mm |
|
| 553 | - $relationTableNameAlias = $this->generateAlias($relationTableName); |
|
| 554 | - $join = sprintf( |
|
| 555 | - 'LEFT JOIN %s AS %s ON %s.uid=%s.%s', $relationTableName, |
|
| 556 | - $relationTableNameAlias, |
|
| 557 | - $tableName, |
|
| 558 | - $relationTableNameAlias, |
|
| 559 | - $parentKeyFieldName |
|
| 560 | - ); |
|
| 561 | - $statementParts['unions'][$relationTableNameAlias] = $join; |
|
| 562 | - |
|
| 563 | - // Foreign table e.g sys_category |
|
| 564 | - $childTableNameAlias = $this->generateAlias($childTableName); |
|
| 565 | - $this->currentChildTableNameAlias = $childTableNameAlias; |
|
| 566 | - $join = sprintf( |
|
| 567 | - 'LEFT JOIN %s AS %s ON %s.%s=%s.uid', |
|
| 568 | - $childTableName, |
|
| 569 | - $childTableNameAlias, |
|
| 570 | - $relationTableNameAlias, |
|
| 571 | - $childKeyFieldName, |
|
| 572 | - $childTableNameAlias |
|
| 573 | - ); |
|
| 574 | - $statementParts['unions'][$childTableNameAlias] = $join; |
|
| 575 | - |
|
| 576 | - // Find a possible table name for a MM condition. |
|
| 577 | - $tableNameCondition = $table->field($fieldName)->getAdditionalTableNameCondition(); |
|
| 578 | - if ($tableNameCondition) { |
|
| 579 | - |
|
| 580 | - // If we can find a source file name, we can then retrieve more MM conditions from the TCA such as a field name. |
|
| 581 | - $sourceFileName = $this->query->getSourceFieldName(); |
|
| 582 | - if (empty($sourceFileName)) { |
|
| 583 | - $additionalMMConditions = array( |
|
| 584 | - 'tablenames' => $tableNameCondition, |
|
| 585 | - ); |
|
| 586 | - } else { |
|
| 587 | - $additionalMMConditions = Tca::table($tableNameCondition)->field($sourceFileName)->getAdditionalMMCondition(); |
|
| 588 | - } |
|
| 589 | - |
|
| 590 | - foreach ($additionalMMConditions as $additionalFieldName => $additionalMMCondition) { |
|
| 591 | - $additionalJoin = sprintf(' AND %s.%s = "%s"', $relationTableNameAlias, $additionalFieldName, $additionalMMCondition); |
|
| 592 | - $statementParts['unions'][$relationTableNameAlias] .= $additionalJoin; |
|
| 593 | - |
|
| 594 | - $additionalJoin = sprintf(' AND %s.%s = "%s"', $relationTableNameAlias, $additionalFieldName, $additionalMMCondition); |
|
| 595 | - $statementParts['unions'][$childTableNameAlias] .= $additionalJoin; |
|
| 596 | - } |
|
| 597 | - } |
|
| 598 | - |
|
| 599 | - } elseif ($table->field($fieldName)->hasMany()) { // includes relations "many-to-one" and "csv" relations |
|
| 600 | - $childTableNameAlias = $this->generateAlias($childTableName); |
|
| 601 | - $this->currentChildTableNameAlias = $childTableNameAlias; |
|
| 602 | - |
|
| 603 | - if (isset($parentKeyFieldName)) { |
|
| 604 | - $join = sprintf( |
|
| 605 | - 'LEFT JOIN %s AS %s ON %s.uid=%s.%s', |
|
| 606 | - $childTableName, |
|
| 607 | - $childTableNameAlias, |
|
| 608 | - $tableName, |
|
| 609 | - $childTableNameAlias, |
|
| 610 | - $parentKeyFieldName |
|
| 611 | - ); |
|
| 612 | - $statementParts['unions'][$childTableNameAlias] = $join; |
|
| 613 | - } else { |
|
| 614 | - $join = sprintf( |
|
| 615 | - 'LEFT JOIN %s AS %s ON (FIND_IN_SET(%s.uid, %s.%s))', |
|
| 616 | - $childTableName, |
|
| 617 | - $childTableNameAlias, |
|
| 618 | - $childTableNameAlias, |
|
| 619 | - $tableName, |
|
| 620 | - $fieldName |
|
| 621 | - ); |
|
| 622 | - $statementParts['unions'][$childTableNameAlias] = $join; |
|
| 623 | - } |
|
| 624 | - } else { |
|
| 625 | - throw new Exception('Could not determine type of relation.', 1252502725); |
|
| 626 | - } |
|
| 627 | - |
|
| 628 | - $statementParts['keywords']['distinct'] = 'DISTINCT'; |
|
| 629 | - $propertyPath = $explodedPropertyPath[1]; |
|
| 630 | - $tableName = $childTableName; |
|
| 631 | - } |
|
| 632 | - |
|
| 633 | - /** |
|
| 634 | - * Returns the SQL operator for the given JCR operator type. |
|
| 635 | - * |
|
| 636 | - * @param string $operator One of the JCR_OPERATOR_* constants |
|
| 637 | - * @return string an SQL operator |
|
| 638 | - * @throws Exception |
|
| 639 | - */ |
|
| 640 | - protected function resolveOperator($operator) |
|
| 641 | - { |
|
| 642 | - switch ($operator) { |
|
| 643 | - case self::OPERATOR_EQUAL_TO_NULL: |
|
| 644 | - $operator = 'IS'; |
|
| 645 | - break; |
|
| 646 | - case self::OPERATOR_NOT_EQUAL_TO_NULL: |
|
| 647 | - $operator = 'IS NOT'; |
|
| 648 | - break; |
|
| 649 | - case QueryInterface::OPERATOR_IN: |
|
| 650 | - $operator = 'IN'; |
|
| 651 | - break; |
|
| 652 | - case QueryInterface::OPERATOR_EQUAL_TO: |
|
| 653 | - $operator = '='; |
|
| 654 | - break; |
|
| 655 | - case QueryInterface::OPERATOR_NOT_EQUAL_TO: |
|
| 656 | - $operator = '!='; |
|
| 657 | - break; |
|
| 658 | - case QueryInterface::OPERATOR_LESS_THAN: |
|
| 659 | - $operator = '<'; |
|
| 660 | - break; |
|
| 661 | - case QueryInterface::OPERATOR_LESS_THAN_OR_EQUAL_TO: |
|
| 662 | - $operator = '<='; |
|
| 663 | - break; |
|
| 664 | - case QueryInterface::OPERATOR_GREATER_THAN: |
|
| 665 | - $operator = '>'; |
|
| 666 | - break; |
|
| 667 | - case QueryInterface::OPERATOR_GREATER_THAN_OR_EQUAL_TO: |
|
| 668 | - $operator = '>='; |
|
| 669 | - break; |
|
| 670 | - case QueryInterface::OPERATOR_LIKE: |
|
| 671 | - $operator = 'LIKE'; |
|
| 672 | - break; |
|
| 673 | - default: |
|
| 674 | - throw new Exception('Unsupported operator encountered.', 1242816073); |
|
| 675 | - } |
|
| 676 | - return $operator; |
|
| 677 | - } |
|
| 678 | - |
|
| 679 | - /** |
|
| 680 | - * Adds additional WHERE statements according to the query settings. |
|
| 681 | - * |
|
| 682 | - * @param QuerySettingsInterface $querySettings The TYPO3 CMS specific query settings |
|
| 683 | - * @param string $tableNameOrAlias The table name to add the additional where clause for |
|
| 684 | - * @param array &$statementParts |
|
| 685 | - * @return void |
|
| 686 | - */ |
|
| 687 | - protected function addAdditionalWhereClause(QuerySettingsInterface $querySettings, $tableNameOrAlias, &$statementParts) |
|
| 688 | - { |
|
| 689 | - $this->addVisibilityConstraintStatement($querySettings, $tableNameOrAlias, $statementParts); |
|
| 690 | - if ($querySettings->getRespectSysLanguage()) { |
|
| 691 | - $this->addSysLanguageStatement($tableNameOrAlias, $statementParts, $querySettings); |
|
| 692 | - } |
|
| 693 | - } |
|
| 694 | - |
|
| 695 | - /** |
|
| 696 | - * Adds enableFields and deletedClause to the query if necessary |
|
| 697 | - * |
|
| 698 | - * @param QuerySettingsInterface $querySettings |
|
| 699 | - * @param string $tableNameOrAlias The database table name |
|
| 700 | - * @param array &$statementParts The query parts |
|
| 701 | - * @return void |
|
| 702 | - */ |
|
| 703 | - protected function addVisibilityConstraintStatement(QuerySettingsInterface $querySettings, $tableNameOrAlias, array &$statementParts) |
|
| 704 | - { |
|
| 705 | - $statement = ''; |
|
| 706 | - $tableName = $this->resolveTableNameAlias($tableNameOrAlias); |
|
| 707 | - if (is_array($GLOBALS['TCA'][$tableName]['ctrl'])) { |
|
| 708 | - $ignoreEnableFields = $querySettings->getIgnoreEnableFields(); |
|
| 709 | - $enableFieldsToBeIgnored = $querySettings->getEnableFieldsToBeIgnored(); |
|
| 710 | - $includeDeleted = $querySettings->getIncludeDeleted(); |
|
| 711 | - if (ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isFrontend()) { |
|
| 712 | - $statement .= $this->getFrontendConstraintStatement($tableNameOrAlias, $ignoreEnableFields, $enableFieldsToBeIgnored, $includeDeleted); |
|
| 713 | - } else { |
|
| 714 | - // 'BE' case |
|
| 715 | - $statement .= $this->getBackendConstraintStatement($tableNameOrAlias, $ignoreEnableFields, $includeDeleted); |
|
| 716 | - } |
|
| 717 | - |
|
| 718 | - // Remove the prefixing "AND" if any. |
|
| 719 | - if (!empty($statement)) { |
|
| 720 | - $statement = strtolower(substr($statement, 1, 3)) === 'and' ? substr($statement, 5) : $statement; |
|
| 721 | - $statementParts['additionalWhereClause'][$tableNameOrAlias][] = $statement; |
|
| 722 | - } |
|
| 723 | - } |
|
| 724 | - } |
|
| 725 | - |
|
| 726 | - /** |
|
| 727 | - * Returns constraint statement for frontend context |
|
| 728 | - * |
|
| 729 | - * @param string $tableNameOrAlias |
|
| 730 | - * @param boolean $ignoreEnableFields A flag indicating whether the enable fields should be ignored |
|
| 731 | - * @param array $enableFieldsToBeIgnored If $ignoreEnableFields is true, this array specifies enable fields to be ignored. If it is null or an empty array (default) all enable fields are ignored. |
|
| 732 | - * @param boolean $includeDeleted A flag indicating whether deleted records should be included |
|
| 733 | - * @return string |
|
| 734 | - * @throws Exception\InconsistentQuerySettingsException |
|
| 735 | - */ |
|
| 736 | - protected function getFrontendConstraintStatement($tableNameOrAlias, $ignoreEnableFields, $enableFieldsToBeIgnored, $includeDeleted) |
|
| 737 | - { |
|
| 738 | - $statement = ''; |
|
| 739 | - $tableName = $this->resolveTableNameAlias($tableNameOrAlias); |
|
| 740 | - if ($ignoreEnableFields && !$includeDeleted) { |
|
| 741 | - if (count($enableFieldsToBeIgnored)) { |
|
| 742 | - // array_combine() is necessary because of the way \TYPO3\CMS\Frontend\Page\PageRepository::enableFields() is implemented |
|
| 743 | - $statement .= $this->getPageRepository()->enableFields($tableName, -1, array_combine($enableFieldsToBeIgnored, $enableFieldsToBeIgnored)); |
|
| 744 | - } else { |
|
| 745 | - $statement .= $this->getPageRepository()->deleteClause($tableName); |
|
| 746 | - } |
|
| 747 | - } elseif (!$ignoreEnableFields && !$includeDeleted) { |
|
| 748 | - $statement .= $this->getPageRepository()->enableFields($tableName); |
|
| 749 | - } elseif (!$ignoreEnableFields && $includeDeleted) { |
|
| 750 | - throw new InconsistentQuerySettingsException('Query setting "ignoreEnableFields=false" can not be used together with "includeDeleted=true" in frontend context.', 1327678173); |
|
| 751 | - } |
|
| 752 | - return $this->replaceTableNameByAlias($tableName, $tableNameOrAlias, $statement); |
|
| 753 | - } |
|
| 754 | - |
|
| 755 | - /** |
|
| 756 | - * Returns constraint statement for backend context |
|
| 757 | - * |
|
| 758 | - * @param string $tableNameOrAlias |
|
| 759 | - * @param boolean $ignoreEnableFields A flag indicating whether the enable fields should be ignored |
|
| 760 | - * @param boolean $includeDeleted A flag indicating whether deleted records should be included |
|
| 761 | - * @return string |
|
| 762 | - */ |
|
| 763 | - protected function getBackendConstraintStatement($tableNameOrAlias, $ignoreEnableFields, $includeDeleted) |
|
| 764 | - { |
|
| 765 | - $tableName = $this->resolveTableNameAlias($tableNameOrAlias); |
|
| 766 | - $statement = ''; |
|
| 767 | - if (!$ignoreEnableFields) { |
|
| 768 | - $statement .= BackendUtility::BEenableFields($tableName); |
|
| 769 | - } |
|
| 770 | - |
|
| 771 | - // If the table is found to have "workspace" support, add the corresponding fields in the statement. |
|
| 772 | - if (Tca::table($tableName)->hasWorkspaceSupport()) { |
|
| 773 | - if ($this->getBackendUser()->workspace === 0) { |
|
| 774 | - $statement .= ' AND ' . $tableName . '.t3ver_state<=' . new VersionState(VersionState::DEFAULT_STATE); |
|
| 775 | - } else { |
|
| 776 | - // Show only records of live and of the current workspace |
|
| 777 | - // In case we are in a Versioning preview |
|
| 778 | - $statement .= ' AND (' . |
|
| 779 | - $tableName . '.t3ver_wsid=0 OR ' . |
|
| 780 | - $tableName . '.t3ver_wsid=' . (int)$this->getBackendUser()->workspace . |
|
| 781 | - ')'; |
|
| 782 | - } |
|
| 783 | - |
|
| 784 | - // Check if this segment make sense here or whether it should be in the "if" part when we have workspace = 0 |
|
| 785 | - $statement .= ' AND ' . $tableName . '.pid<>-1'; |
|
| 786 | - } |
|
| 787 | - |
|
| 788 | - if (!$includeDeleted) { |
|
| 789 | - $statement .= BackendUtility::deleteClause($tableName); |
|
| 790 | - } |
|
| 791 | - |
|
| 792 | - return $this->replaceTableNameByAlias($tableName, $tableNameOrAlias, $statement); |
|
| 793 | - } |
|
| 794 | - |
|
| 795 | - /** |
|
| 796 | - * Builds the language field statement |
|
| 797 | - * |
|
| 798 | - * @param string $tableNameOrAlias The database table name |
|
| 799 | - * @param array &$statementParts The query parts |
|
| 800 | - * @param QuerySettingsInterface $querySettings The TYPO3 CMS specific query settings |
|
| 801 | - * @return void |
|
| 802 | - * @throws Exception |
|
| 803 | - */ |
|
| 804 | - protected function addSysLanguageStatement($tableNameOrAlias, array &$statementParts, $querySettings) |
|
| 805 | - { |
|
| 806 | - $tableName = $this->resolveTableNameAlias($tableNameOrAlias); |
|
| 807 | - if (is_array($GLOBALS['TCA'][$tableName]['ctrl'])) { |
|
| 808 | - if (!empty($GLOBALS['TCA'][$tableName]['ctrl']['languageField'])) { |
|
| 809 | - // Select all entries for the current language |
|
| 810 | - $additionalWhereClause = $tableNameOrAlias . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['languageField'] . ' IN (' . intval($querySettings->getLanguageUid()) . ',-1)'; |
|
| 811 | - // If any language is set -> get those entries which are not translated yet |
|
| 812 | - // They will be removed by t3lib_page::getRecordOverlay if not matching overlay mode |
|
| 813 | - if (isset($GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField']) |
|
| 814 | - && $querySettings->getLanguageUid() > 0 |
|
| 815 | - ) { |
|
| 816 | - $additionalWhereClause .= ' OR (' . $tableNameOrAlias . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['languageField'] . '=0' . |
|
| 817 | - ' AND ' . $tableNameOrAlias . '.uid NOT IN (SELECT ' . $tableName . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'] . |
|
| 818 | - ' FROM ' . $tableName . |
|
| 819 | - ' WHERE ' . $tableName . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'] . '>0' . |
|
| 820 | - ' AND ' . $tableName . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['languageField'] . '>0'; |
|
| 821 | - |
|
| 822 | - // Add delete clause to ensure all entries are loaded |
|
| 823 | - if (isset($GLOBALS['TCA'][$tableName]['ctrl']['delete'])) { |
|
| 824 | - $additionalWhereClause .= ' AND ' . $tableNameOrAlias . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['delete'] . '=0'; |
|
| 825 | - } |
|
| 826 | - $additionalWhereClause .= '))'; |
|
| 827 | - } |
|
| 828 | - $statementParts['additionalWhereClause'][$tableNameOrAlias][] = '(' . $additionalWhereClause . ')'; |
|
| 829 | - } |
|
| 830 | - } |
|
| 831 | - } |
|
| 832 | - |
|
| 833 | - /** |
|
| 834 | - * Transforms orderings into SQL. |
|
| 835 | - * |
|
| 836 | - * @param array $orderings An array of orderings (Tx_Extbase_Persistence_QOM_Ordering) |
|
| 837 | - * @param SourceInterface $source The source |
|
| 838 | - * @param array &$statementParts The query parts |
|
| 839 | - * @return void |
|
| 840 | - * @throws Exception\UnsupportedOrderException |
|
| 841 | - */ |
|
| 842 | - protected function parseOrderings(array $orderings, SourceInterface $source, array &$statementParts) |
|
| 843 | - { |
|
| 844 | - foreach ($orderings as $fieldNameAndPath => $order) { |
|
| 845 | - switch ($order) { |
|
| 846 | - case QueryInterface::ORDER_ASCENDING: |
|
| 847 | - $order = 'ASC'; |
|
| 848 | - break; |
|
| 849 | - case QueryInterface::ORDER_DESCENDING: |
|
| 850 | - $order = 'DESC'; |
|
| 851 | - break; |
|
| 852 | - default: |
|
| 853 | - throw new UnsupportedOrderException('Unsupported order encountered.', 1456845126); |
|
| 854 | - } |
|
| 855 | - |
|
| 856 | - $tableName = $this->getFieldPathResolver()->getDataType($fieldNameAndPath, $this->query->getType()); |
|
| 857 | - $fieldName = $this->getFieldPathResolver()->stripFieldPath($fieldNameAndPath, $tableName); |
|
| 858 | - $statementParts['orderings'][] = sprintf('%s.%s %s', $tableName, $fieldName, $order); |
|
| 859 | - } |
|
| 860 | - } |
|
| 861 | - |
|
| 862 | - /** |
|
| 863 | - * Transforms limit and offset into SQL |
|
| 864 | - * |
|
| 865 | - * @param int $limit |
|
| 866 | - * @param int $offset |
|
| 867 | - * @param array &$statementParts |
|
| 868 | - * @return void |
|
| 869 | - */ |
|
| 870 | - protected function parseLimitAndOffset($limit, $offset, array &$statementParts) |
|
| 871 | - { |
|
| 872 | - if ($limit !== null && $offset !== null) { |
|
| 873 | - $statementParts['limit'] = intval($offset) . ', ' . intval($limit); |
|
| 874 | - } elseif ($limit !== null) { |
|
| 875 | - $statementParts['limit'] = intval($limit); |
|
| 876 | - } |
|
| 877 | - } |
|
| 878 | - |
|
| 879 | - /** |
|
| 880 | - * @param array $rows |
|
| 881 | - * @return array |
|
| 882 | - */ |
|
| 883 | - protected function getContentObjects(array $rows): array |
|
| 884 | - { |
|
| 885 | - $contentObjects = []; |
|
| 886 | - foreach ($rows as $row) { |
|
| 887 | - |
|
| 888 | - // Get language uid from querySettings. |
|
| 889 | - // Ensure the backend handling is not broken (fallback to Get parameter 'L' if needed) |
|
| 890 | - $overlaidRow = $this->doLanguageAndWorkspaceOverlay( |
|
| 891 | - $row, |
|
| 892 | - $this->query->getTypo3QuerySettings() |
|
| 893 | - ); |
|
| 894 | - |
|
| 895 | - $contentObjects[] = GeneralUtility::makeInstance( |
|
| 896 | - Content::class, |
|
| 897 | - $this->query->getType(), |
|
| 898 | - $overlaidRow |
|
| 899 | - ); |
|
| 900 | - } |
|
| 901 | - |
|
| 902 | - return $contentObjects; |
|
| 903 | - } |
|
| 904 | - |
|
| 905 | - /** |
|
| 906 | - * Performs workspace and language overlay on the given row array. The language and workspace id is automatically |
|
| 907 | - * detected (depending on FE or BE context). You can also explicitly set the language/workspace id. |
|
| 908 | - * |
|
| 909 | - * @param array $row |
|
| 910 | - * @param QuerySettingsInterface $querySettings The TYPO3 CMS specific query settings |
|
| 911 | - * @return array |
|
| 912 | - */ |
|
| 913 | - protected function doLanguageAndWorkspaceOverlay(array $row, $querySettings) |
|
| 914 | - { |
|
| 915 | - $tableName = $this->getTableName(); |
|
| 916 | - |
|
| 917 | - $pageRepository = $this->getPageRepository(); |
|
| 918 | - if (is_object($GLOBALS['TSFE'])) { |
|
| 919 | - $languageMode = $GLOBALS['TSFE']->sys_language_mode; |
|
| 920 | - #if ($this->isBackendUserLogged() && $this->getBackendUser()->workspace !== 0) { |
|
| 921 | - # $pageRepository->versioningWorkspaceId = $this->getBackendUser()->workspace; |
|
| 922 | - #} |
|
| 923 | - } else { |
|
| 924 | - $languageMode = ''; |
|
| 925 | - $workspaceUid = $this->getBackendUser()->workspace; |
|
| 926 | - #$pageRepository->versioningWorkspaceId = $workspaceUid; |
|
| 927 | - #if ($this->getBackendUser()->workspace !== 0) { |
|
| 928 | - # $pageRepository->versioningPreview = 1; |
|
| 929 | - #} |
|
| 930 | - } |
|
| 931 | - |
|
| 932 | - // If current row is a translation select its parent |
|
| 933 | - if (isset($GLOBALS['TCA'][$tableName]['ctrl']['languageField']) |
|
| 934 | - && isset($GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField']) |
|
| 935 | - ) { |
|
| 936 | - if (isset($row[$GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField']]) |
|
| 937 | - && $row[$GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField']] > 0 |
|
| 938 | - ) { |
|
| 939 | - $queryBuilder = $this->getQueryBuilder(); |
|
| 940 | - $row = $queryBuilder |
|
| 941 | - ->select($tableName . '.*') |
|
| 942 | - ->from($tableName) |
|
| 943 | - ->andWhere( |
|
| 944 | - $tableName . '.uid=' . (int)$row[$GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField']], |
|
| 945 | - $tableName . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['languageField'] . ' = 0' |
|
| 946 | - ) |
|
| 947 | - ->execute() |
|
| 948 | - ->fetch(); |
|
| 949 | - } |
|
| 950 | - } |
|
| 951 | - |
|
| 952 | - // Retrieve the original uid; Used for Workspaces! |
|
| 953 | - if (ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isBackend()) { |
|
| 954 | - $pageRepository->versionOL($tableName, $row, true, true); |
|
| 955 | - } else { |
|
| 956 | - \TYPO3\CMS\Backend\Utility\BackendUtility::workspaceOL($tableName, $row); |
|
| 957 | - } |
|
| 958 | - if (isset($row['_ORIG_uid'])) { |
|
| 959 | - $row['uid'] = $row['_ORIG_uid']; |
|
| 960 | - } |
|
| 961 | - |
|
| 962 | - // Special case for table "pages" |
|
| 963 | - if ($tableName == 'pages') { |
|
| 964 | - $row = $pageRepository->getPageOverlay($row, $querySettings->getLanguageUid()); |
|
| 965 | - } elseif (isset($GLOBALS['TCA'][$tableName]['ctrl']['languageField']) |
|
| 966 | - && $GLOBALS['TCA'][$tableName]['ctrl']['languageField'] !== '' |
|
| 967 | - ) { |
|
| 968 | - if (in_array($row[$GLOBALS['TCA'][$tableName]['ctrl']['languageField']], array(-1, 0))) { |
|
| 969 | - $overlayMode = $languageMode === 'strict' ? 'hideNonTranslated' : ''; |
|
| 970 | - $row = $pageRepository->getRecordOverlay($tableName, $row, $querySettings->getLanguageUid(), $overlayMode); |
|
| 971 | - } |
|
| 972 | - } |
|
| 973 | - |
|
| 974 | - return $row; |
|
| 975 | - } |
|
| 976 | - |
|
| 977 | - /** |
|
| 978 | - * Return a resolved table name given a possible table name alias. |
|
| 979 | - * |
|
| 980 | - * @param string $tableNameOrAlias |
|
| 981 | - * @return string |
|
| 982 | - */ |
|
| 983 | - protected function resolveTableNameAlias($tableNameOrAlias) |
|
| 984 | - { |
|
| 985 | - $resolvedTableName = $tableNameOrAlias; |
|
| 986 | - if (!empty($this->tableNameAliases['aliases'][$tableNameOrAlias])) { |
|
| 987 | - $resolvedTableName = $this->tableNameAliases['aliases'][$tableNameOrAlias]; |
|
| 988 | - } |
|
| 989 | - return $resolvedTableName; |
|
| 990 | - } |
|
| 991 | - |
|
| 992 | - /** |
|
| 993 | - * Generate a unique table name alias for the given table name. |
|
| 994 | - * |
|
| 995 | - * @param string $tableName |
|
| 996 | - * @return string |
|
| 997 | - */ |
|
| 998 | - protected function generateAlias($tableName) |
|
| 999 | - { |
|
| 1000 | - |
|
| 1001 | - if (!isset($this->tableNameAliases['aliasIncrement'][$tableName])) { |
|
| 1002 | - $this->tableNameAliases['aliasIncrement'][$tableName] = 0; |
|
| 1003 | - } |
|
| 1004 | - |
|
| 1005 | - $numberOfAliases = $this->tableNameAliases['aliasIncrement'][$tableName]; |
|
| 1006 | - $tableNameAlias = $tableName . $numberOfAliases; |
|
| 1007 | - |
|
| 1008 | - $this->tableNameAliases['aliasIncrement'][$tableName]++; |
|
| 1009 | - $this->tableNameAliases['aliases'][$tableNameAlias] = $tableName; |
|
| 1010 | - |
|
| 1011 | - return $tableNameAlias; |
|
| 1012 | - } |
|
| 1013 | - |
|
| 1014 | - /** |
|
| 1015 | - * Replace the table names by its table name alias within the given statement. |
|
| 1016 | - * |
|
| 1017 | - * @param string $tableName |
|
| 1018 | - * @param string $tableNameAlias |
|
| 1019 | - * @param string $statement |
|
| 1020 | - * @return string |
|
| 1021 | - */ |
|
| 1022 | - protected function replaceTableNameByAlias($tableName, $tableNameAlias, $statement) |
|
| 1023 | - { |
|
| 1024 | - if ($statement && $tableName !== $tableNameAlias) { |
|
| 1025 | - $statement = str_replace($tableName, $tableNameAlias, $statement); |
|
| 1026 | - } |
|
| 1027 | - return $statement; |
|
| 1028 | - } |
|
| 1029 | - |
|
| 1030 | - /** |
|
| 1031 | - * Returns an instance of the current Backend User. |
|
| 1032 | - * |
|
| 1033 | - * @return BackendUserAuthentication |
|
| 1034 | - */ |
|
| 1035 | - protected function getBackendUser() |
|
| 1036 | - { |
|
| 1037 | - return $GLOBALS['BE_USER']; |
|
| 1038 | - } |
|
| 1039 | - |
|
| 1040 | - /** |
|
| 1041 | - * Tell whether a Backend User is logged in. |
|
| 1042 | - * |
|
| 1043 | - * @return bool |
|
| 1044 | - */ |
|
| 1045 | - protected function isBackendUserLogged() |
|
| 1046 | - { |
|
| 1047 | - return is_object($GLOBALS['BE_USER']); |
|
| 1048 | - } |
|
| 1049 | - |
|
| 1050 | - /** |
|
| 1051 | - * @return PageRepository|object |
|
| 1052 | - */ |
|
| 1053 | - protected function getPageRepository() |
|
| 1054 | - { |
|
| 1055 | - if (!$this->pageRepository instanceof PageRepository) { |
|
| 1056 | - if (ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isFrontend() && is_object($GLOBALS['TSFE'])) { |
|
| 1057 | - $this->pageRepository = $GLOBALS['TSFE']->sys_page; |
|
| 1058 | - } else { |
|
| 1059 | - $this->pageRepository = GeneralUtility::makeInstance(PageRepository::class); |
|
| 1060 | - } |
|
| 1061 | - } |
|
| 1062 | - |
|
| 1063 | - return $this->pageRepository; |
|
| 1064 | - } |
|
| 1065 | - |
|
| 1066 | - /** |
|
| 1067 | - * @return FieldPathResolver|object |
|
| 1068 | - */ |
|
| 1069 | - protected function getFieldPathResolver() |
|
| 1070 | - { |
|
| 1071 | - return GeneralUtility::makeInstance(FieldPathResolver::class); |
|
| 1072 | - } |
|
| 1073 | - |
|
| 1074 | - /** |
|
| 1075 | - * @return object|Connection |
|
| 1076 | - */ |
|
| 1077 | - protected function getConnection(): Connection |
|
| 1078 | - { |
|
| 1079 | - /** @var ConnectionPool $connectionPool */ |
|
| 1080 | - return GeneralUtility::makeInstance(ConnectionPool::class) |
|
| 1081 | - ->getConnectionForTable($this->getTableName()); |
|
| 1082 | - } |
|
| 1083 | - |
|
| 1084 | - /** |
|
| 1085 | - * @return object|QueryBuilder |
|
| 1086 | - */ |
|
| 1087 | - protected function getQueryBuilder(): QueryBuilder |
|
| 1088 | - { |
|
| 1089 | - /** @var ConnectionPool $connectionPool */ |
|
| 1090 | - $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class); |
|
| 1091 | - return $connectionPool->getQueryBuilderForTable($this->getTableName()); |
|
| 1092 | - } |
|
| 1093 | - |
|
| 1094 | - /** |
|
| 1095 | - * @return string |
|
| 1096 | - */ |
|
| 1097 | - public function getTableName(): string |
|
| 1098 | - { |
|
| 1099 | - return $this->query->getSource()->getNodeTypeName(); // getSelectorName() |
|
| 1100 | - } |
|
| 54 | + const OPERATOR_EQUAL_TO_NULL = 'operatorEqualToNull'; |
|
| 55 | + const OPERATOR_NOT_EQUAL_TO_NULL = 'operatorNotEqualToNull'; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * The TYPO3 page repository. Used for language and workspace overlay |
|
| 59 | + * |
|
| 60 | + * @var PageRepository |
|
| 61 | + */ |
|
| 62 | + protected $pageRepository; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * @var Query |
|
| 66 | + */ |
|
| 67 | + protected $query; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Store some info related to table name and its aliases. |
|
| 71 | + * |
|
| 72 | + * @var array |
|
| 73 | + */ |
|
| 74 | + protected $tableNameAliases = array( |
|
| 75 | + 'aliases' => [], |
|
| 76 | + 'aliasIncrement' => [], |
|
| 77 | + ); |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Use to store the current foreign table name alias. |
|
| 81 | + * |
|
| 82 | + * @var string |
|
| 83 | + */ |
|
| 84 | + protected $currentChildTableNameAlias = ''; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * @param Query $query |
|
| 88 | + */ |
|
| 89 | + public function __construct(Query $query) |
|
| 90 | + { |
|
| 91 | + $this->query = $query; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * @param $parameters |
|
| 96 | + * @return array |
|
| 97 | + */ |
|
| 98 | + protected static function getTypes($parameters) |
|
| 99 | + { |
|
| 100 | + $types = []; |
|
| 101 | + foreach ($parameters as $parameter) { |
|
| 102 | + if (is_array($parameter)) { |
|
| 103 | + if (MathUtility::canBeInterpretedAsInteger($parameter[0])) { |
|
| 104 | + $types[] = \Doctrine\DBAL\Connection::PARAM_INT_ARRAY; |
|
| 105 | + } else { |
|
| 106 | + $types[] = \Doctrine\DBAL\Connection::PARAM_STR_ARRAY; |
|
| 107 | + } |
|
| 108 | + } else { |
|
| 109 | + if (MathUtility::canBeInterpretedAsInteger($parameter)) { |
|
| 110 | + $types[] = ParameterType::INTEGER; |
|
| 111 | + } else { |
|
| 112 | + $types[] = ParameterType::STRING; |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + } |
|
| 116 | + return $types; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Returns the result of the query |
|
| 121 | + */ |
|
| 122 | + public function fetchResult() |
|
| 123 | + { |
|
| 124 | + $parameters = []; |
|
| 125 | + $statementParts = $this->parseQuery($parameters); |
|
| 126 | + $statementParts = $this->processStatementStructureForRecursiveMMRelation($statementParts); |
|
| 127 | + $sql = $this->buildQuery($statementParts); |
|
| 128 | + //print $sql; exit(); |
|
| 129 | + |
|
| 130 | + $rows = $this->getConnection() |
|
| 131 | + ->executeQuery($sql, $parameters, self::getTypes($parameters)) |
|
| 132 | + ->fetchAll(); |
|
| 133 | + |
|
| 134 | + return $this->getContentObjects($rows); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Returns the number of tuples matching the query. |
|
| 139 | + * |
|
| 140 | + * @return int The number of matching tuples |
|
| 141 | + */ |
|
| 142 | + public function countResult() |
|
| 143 | + { |
|
| 144 | + $parameters = []; |
|
| 145 | + $statementParts = $this->parseQuery($parameters); |
|
| 146 | + $statementParts = $this->processStatementStructureForRecursiveMMRelation($statementParts); |
|
| 147 | + $types = self::getTypes($parameters); |
|
| 148 | + |
|
| 149 | + // if limit is set, we need to count the rows "manually" as COUNT(*) ignores LIMIT constraints |
|
| 150 | + if (!empty($statementParts['limit'])) { |
|
| 151 | + $sql = $this->buildQuery($statementParts); |
|
| 152 | + |
|
| 153 | + $count = $this |
|
| 154 | + ->getConnection() |
|
| 155 | + ->executeQuery($sql, $parameters, $types) |
|
| 156 | + ->rowCount(); |
|
| 157 | + } else { |
|
| 158 | + $statementParts['fields'] = array('COUNT(*)'); |
|
| 159 | + // having orderings without grouping is not compatible with non-MySQL DBMS |
|
| 160 | + $statementParts['orderings'] = []; |
|
| 161 | + if (isset($statementParts['keywords']['distinct'])) { |
|
| 162 | + unset($statementParts['keywords']['distinct']); |
|
| 163 | + $distinctField = $this->query->getDistinct() ? $this->query->getDistinct() : 'uid'; |
|
| 164 | + $statementParts['fields'] = array('COUNT(DISTINCT ' . $statementParts['mainTable'] . '.' . $distinctField . ')'); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + $sql = $this->buildQuery($statementParts); |
|
| 168 | + $count = $this |
|
| 169 | + ->getConnection() |
|
| 170 | + ->executeQuery($sql, $parameters, $types) |
|
| 171 | + ->fetchColumn(0); |
|
| 172 | + } |
|
| 173 | + return (int)$count; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * Parses the query and returns the SQL statement parts. |
|
| 178 | + * |
|
| 179 | + * @param array &$parameters |
|
| 180 | + * @return array |
|
| 181 | + */ |
|
| 182 | + public function parseQuery(array &$parameters) |
|
| 183 | + { |
|
| 184 | + $statementParts = []; |
|
| 185 | + $statementParts['keywords'] = []; |
|
| 186 | + $statementParts['tables'] = []; |
|
| 187 | + $statementParts['unions'] = []; |
|
| 188 | + $statementParts['fields'] = []; |
|
| 189 | + $statementParts['where'] = []; |
|
| 190 | + $statementParts['additionalWhereClause'] = []; |
|
| 191 | + $statementParts['orderings'] = []; |
|
| 192 | + $statementParts['limit'] = []; |
|
| 193 | + $query = $this->query; |
|
| 194 | + $source = $query->getSource(); |
|
| 195 | + $this->parseSource($source, $statementParts); |
|
| 196 | + $this->parseConstraint($query->getConstraint(), $source, $statementParts, $parameters); |
|
| 197 | + $this->parseOrderings($query->getOrderings(), $source, $statementParts); |
|
| 198 | + $this->parseLimitAndOffset($query->getLimit(), $query->getOffset(), $statementParts); |
|
| 199 | + $tableNames = array_unique(array_keys($statementParts['tables'] + $statementParts['unions'])); |
|
| 200 | + foreach ($tableNames as $tableNameOrAlias) { |
|
| 201 | + if (is_string($tableNameOrAlias) && strlen($tableNameOrAlias) > 0) { |
|
| 202 | + $this->addAdditionalWhereClause($query->getTypo3QuerySettings(), $tableNameOrAlias, $statementParts); |
|
| 203 | + } |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + return $statementParts; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Fiddle with the statement structure to handle recursive MM relations. |
|
| 211 | + * For the recursive MM query to work, we must invert some values. |
|
| 212 | + * Let see if that is the best way of doing that... |
|
| 213 | + * |
|
| 214 | + * @param array $statementParts |
|
| 215 | + * @return array |
|
| 216 | + */ |
|
| 217 | + public function processStatementStructureForRecursiveMMRelation(array $statementParts) |
|
| 218 | + { |
|
| 219 | + |
|
| 220 | + if ($this->hasRecursiveMMRelation()) { |
|
| 221 | + $tableName = $this->query->getType(); |
|
| 222 | + |
|
| 223 | + // In order the MM query to work for a recursive MM query, we must invert some values. |
|
| 224 | + // tx_domain_model_foo0 (the alias) <--> tx_domain_model_foo (the origin table name) |
|
| 225 | + $values = []; |
|
| 226 | + foreach ($statementParts['fields'] as $key => $value) { |
|
| 227 | + $values[$key] = str_replace($tableName, $tableName . '0', $value); |
|
| 228 | + } |
|
| 229 | + $statementParts['fields'] = $values; |
|
| 230 | + |
|
| 231 | + // Same comment as above. |
|
| 232 | + $values = []; |
|
| 233 | + foreach ($statementParts['where'] as $key => $value) { |
|
| 234 | + $values[$key] = str_replace($tableName . '0', $tableName, $value); |
|
| 235 | + } |
|
| 236 | + $statementParts['where'] = $values; |
|
| 237 | + |
|
| 238 | + // We must be more restrictive by transforming the "left" union by "inner" |
|
| 239 | + $values = []; |
|
| 240 | + foreach ($statementParts['unions'] as $key => $value) { |
|
| 241 | + $values[$key] = str_replace('LEFT JOIN', 'INNER JOIN', $value); |
|
| 242 | + } |
|
| 243 | + $statementParts['unions'] = $values; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + return $statementParts; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * Tell whether there is a recursive MM relation. |
|
| 251 | + * |
|
| 252 | + * @return bool |
|
| 253 | + */ |
|
| 254 | + public function hasRecursiveMMRelation() |
|
| 255 | + { |
|
| 256 | + return isset($this->tableNameAliases['aliasIncrement'][$this->query->getType()]); |
|
| 257 | + |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * Returns the statement, ready to be executed. |
|
| 262 | + * |
|
| 263 | + * @param array $statementParts The SQL statement parts |
|
| 264 | + * @return string The SQL statement |
|
| 265 | + */ |
|
| 266 | + public function buildQuery(array $statementParts) |
|
| 267 | + { |
|
| 268 | + |
|
| 269 | + // Add more statement to the UNION part. |
|
| 270 | + if (!empty($statementParts['unions'])) { |
|
| 271 | + foreach ($statementParts['unions'] as $tableName => $unionPart) { |
|
| 272 | + if (!empty($statementParts['additionalWhereClause'][$tableName])) { |
|
| 273 | + $statementParts['unions'][$tableName] .= ' AND ' . implode(' AND ', $statementParts['additionalWhereClause'][$tableName]); |
|
| 274 | + } |
|
| 275 | + } |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + $statement = 'SELECT ' . implode(' ', $statementParts['keywords']) . ' ' . implode(',', $statementParts['fields']) . ' FROM ' . implode(' ', $statementParts['tables']) . ' ' . implode(' ', $statementParts['unions']); |
|
| 279 | + if (!empty($statementParts['where'])) { |
|
| 280 | + $statement .= ' WHERE ' . implode('', $statementParts['where']); |
|
| 281 | + if (!empty($statementParts['additionalWhereClause'][$this->query->getType()])) { |
|
| 282 | + $statement .= ' AND ' . implode(' AND ', $statementParts['additionalWhereClause'][$this->query->getType()]); |
|
| 283 | + } |
|
| 284 | + } elseif (!empty($statementParts['additionalWhereClause'])) { |
|
| 285 | + $statement .= ' WHERE ' . implode(' AND ', $statementParts['additionalWhereClause'][$this->query->getType()]); |
|
| 286 | + } |
|
| 287 | + if (!empty($statementParts['orderings'])) { |
|
| 288 | + $statement .= ' ORDER BY ' . implode(', ', $statementParts['orderings']); |
|
| 289 | + } |
|
| 290 | + if (!empty($statementParts['limit'])) { |
|
| 291 | + $statement .= ' LIMIT ' . $statementParts['limit']; |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + return $statement; |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + /** |
|
| 298 | + * Transforms a Query Source into SQL and parameter arrays |
|
| 299 | + * |
|
| 300 | + * @param SourceInterface $source The source |
|
| 301 | + * @param array &$sql |
|
| 302 | + * @return void |
|
| 303 | + */ |
|
| 304 | + protected function parseSource(SourceInterface $source, array &$sql) |
|
| 305 | + { |
|
| 306 | + $tableName = $this->getTableName(); |
|
| 307 | + $sql['fields'][$tableName] = $tableName . '.*'; |
|
| 308 | + if ($this->query->getDistinct()) { |
|
| 309 | + $sql['fields'][$tableName] = $tableName . '.' . $this->query->getDistinct(); |
|
| 310 | + $sql['keywords']['distinct'] = 'DISTINCT'; |
|
| 311 | + } |
|
| 312 | + $sql['tables'][$tableName] = $tableName; |
|
| 313 | + $sql['mainTable'] = $tableName; |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + /** |
|
| 317 | + * Transforms a constraint into SQL and parameter arrays |
|
| 318 | + * |
|
| 319 | + * @param ConstraintInterface $constraint The constraint |
|
| 320 | + * @param SourceInterface $source The source |
|
| 321 | + * @param array &$statementParts The query parts |
|
| 322 | + * @param array &$parameters The parameters that will replace the markers |
|
| 323 | + * @return void |
|
| 324 | + */ |
|
| 325 | + protected function parseConstraint(ConstraintInterface $constraint = null, SourceInterface $source, array &$statementParts, array &$parameters) |
|
| 326 | + { |
|
| 327 | + if ($constraint instanceof AndInterface) { |
|
| 328 | + $statementParts['where'][] = '('; |
|
| 329 | + $this->parseConstraint($constraint->getConstraint1(), $source, $statementParts, $parameters); |
|
| 330 | + $statementParts['where'][] = ' AND '; |
|
| 331 | + $this->parseConstraint($constraint->getConstraint2(), $source, $statementParts, $parameters); |
|
| 332 | + $statementParts['where'][] = ')'; |
|
| 333 | + } elseif ($constraint instanceof OrInterface) { |
|
| 334 | + $statementParts['where'][] = '('; |
|
| 335 | + $this->parseConstraint($constraint->getConstraint1(), $source, $statementParts, $parameters); |
|
| 336 | + $statementParts['where'][] = ' OR '; |
|
| 337 | + $this->parseConstraint($constraint->getConstraint2(), $source, $statementParts, $parameters); |
|
| 338 | + $statementParts['where'][] = ')'; |
|
| 339 | + } elseif ($constraint instanceof NotInterface) { |
|
| 340 | + $statementParts['where'][] = 'NOT ('; |
|
| 341 | + $this->parseConstraint($constraint->getConstraint(), $source, $statementParts, $parameters); |
|
| 342 | + $statementParts['where'][] = ')'; |
|
| 343 | + } elseif ($constraint instanceof ComparisonInterface) { |
|
| 344 | + $this->parseComparison($constraint, $source, $statementParts, $parameters); |
|
| 345 | + } |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * Parse a Comparison into SQL and parameter arrays. |
|
| 350 | + * |
|
| 351 | + * @param ComparisonInterface $comparison The comparison to parse |
|
| 352 | + * @param SourceInterface $source The source |
|
| 353 | + * @param array &$statementParts SQL query parts to add to |
|
| 354 | + * @param array &$parameters Parameters to bind to the SQL |
|
| 355 | + * @return void |
|
| 356 | + * @throws Exception\RepositoryException |
|
| 357 | + */ |
|
| 358 | + protected function parseComparison(ComparisonInterface $comparison, SourceInterface $source, array &$statementParts, array &$parameters) |
|
| 359 | + { |
|
| 360 | + $operand1 = $comparison->getOperand1(); |
|
| 361 | + $operator = $comparison->getOperator(); |
|
| 362 | + $operand2 = $comparison->getOperand2(); |
|
| 363 | + if ($operator === QueryInterface::OPERATOR_IN) { |
|
| 364 | + $items = []; |
|
| 365 | + $hasValue = false; |
|
| 366 | + foreach ($operand2 as $value) { |
|
| 367 | + $value = $this->getPlainValue($value); |
|
| 368 | + if ($value !== null) { |
|
| 369 | + $items[] = $value; |
|
| 370 | + $hasValue = true; |
|
| 371 | + } |
|
| 372 | + } |
|
| 373 | + if ($hasValue === false) { |
|
| 374 | + $statementParts['where'][] = '1<>1'; |
|
| 375 | + } else { |
|
| 376 | + $this->parseDynamicOperand($operand1, $operator, $source, $statementParts, $parameters, null); |
|
| 377 | + $parameters[] = $items; |
|
| 378 | + } |
|
| 379 | + } elseif ($operator === QueryInterface::OPERATOR_CONTAINS) { |
|
| 380 | + if ($operand2 === null) { |
|
| 381 | + $statementParts['where'][] = '1<>1'; |
|
| 382 | + } else { |
|
| 383 | + throw new \Exception('Not implemented! Contact extension author.', 1412931227); |
|
| 384 | + # @todo re-implement me if necessary. |
|
| 385 | + #$tableName = $this->query->getType(); |
|
| 386 | + #$propertyName = $operand1->getPropertyName(); |
|
| 387 | + #while (strpos($propertyName, '.') !== false) { |
|
| 388 | + # $this->addUnionStatement($tableName, $propertyName, $statementParts); |
|
| 389 | + #} |
|
| 390 | + #$columnName = $propertyName; |
|
| 391 | + #$columnMap = $propertyName; |
|
| 392 | + #$typeOfRelation = $columnMap instanceof ColumnMap ? $columnMap->getTypeOfRelation() : null; |
|
| 393 | + #if ($typeOfRelation === ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY) { |
|
| 394 | + # $relationTableName = $columnMap->getRelationTableName(); |
|
| 395 | + # $statementParts['where'][] = $tableName . '.uid IN (SELECT ' . $columnMap->getParentKeyFieldName() . ' FROM ' . $relationTableName . ' WHERE ' . $columnMap->getChildKeyFieldName() . '=?)'; |
|
| 396 | + # $parameters[] = intval($this->getPlainValue($operand2)); |
|
| 397 | + #} elseif ($typeOfRelation === ColumnMap::RELATION_HAS_MANY) { |
|
| 398 | + # $parentKeyFieldName = $columnMap->getParentKeyFieldName(); |
|
| 399 | + # if (isset($parentKeyFieldName)) { |
|
| 400 | + # $childTableName = $columnMap->getChildTableName(); |
|
| 401 | + # $statementParts['where'][] = $tableName . '.uid=(SELECT ' . $childTableName . '.' . $parentKeyFieldName . ' FROM ' . $childTableName . ' WHERE ' . $childTableName . '.uid=?)'; |
|
| 402 | + # $parameters[] = intval($this->getPlainValue($operand2)); |
|
| 403 | + # } else { |
|
| 404 | + # $statementParts['where'][] = 'FIND_IN_SET(?,' . $tableName . '.' . $columnName . ')'; |
|
| 405 | + # $parameters[] = intval($this->getPlainValue($operand2)); |
|
| 406 | + # } |
|
| 407 | + #} else { |
|
| 408 | + # throw new Exception\RepositoryException('Unsupported or non-existing property name "' . $propertyName . '" used in relation matching.', 1327065745); |
|
| 409 | + #} |
|
| 410 | + } |
|
| 411 | + } else { |
|
| 412 | + if ($operand2 === null) { |
|
| 413 | + if ($operator === QueryInterface::OPERATOR_EQUAL_TO) { |
|
| 414 | + $operator = self::OPERATOR_EQUAL_TO_NULL; |
|
| 415 | + } elseif ($operator === QueryInterface::OPERATOR_NOT_EQUAL_TO) { |
|
| 416 | + $operator = self::OPERATOR_NOT_EQUAL_TO_NULL; |
|
| 417 | + } |
|
| 418 | + } |
|
| 419 | + $this->parseDynamicOperand($operand1, $operator, $source, $statementParts, $parameters); |
|
| 420 | + $parameters[] = $this->getPlainValue($operand2); |
|
| 421 | + } |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + /** |
|
| 425 | + * Returns a plain value, i.e. objects are flattened if possible. |
|
| 426 | + * |
|
| 427 | + * @param mixed $input |
|
| 428 | + * @return mixed |
|
| 429 | + * @throws UnexpectedTypeException |
|
| 430 | + */ |
|
| 431 | + protected function getPlainValue($input) |
|
| 432 | + { |
|
| 433 | + if (is_array($input)) { |
|
| 434 | + throw new UnexpectedTypeException('An array could not be converted to a plain value.', 1274799932); |
|
| 435 | + } |
|
| 436 | + if ($input instanceof \DateTime) { |
|
| 437 | + return $input->format('U'); |
|
| 438 | + } elseif (is_object($input)) { |
|
| 439 | + if ($input instanceof LazyLoadingProxy) { |
|
| 440 | + $realInput = $input->_loadRealInstance(); |
|
| 441 | + } else { |
|
| 442 | + $realInput = $input; |
|
| 443 | + } |
|
| 444 | + if ($realInput instanceof DomainObjectInterface) { |
|
| 445 | + return $realInput->getUid(); |
|
| 446 | + } else { |
|
| 447 | + throw new UnexpectedTypeException('An object of class "' . get_class($realInput) . '" could not be converted to a plain value.', 1274799934); |
|
| 448 | + } |
|
| 449 | + } elseif (is_bool($input)) { |
|
| 450 | + return $input === true ? 1 : 0; |
|
| 451 | + } else { |
|
| 452 | + return $input; |
|
| 453 | + } |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + /** |
|
| 457 | + * Parse a DynamicOperand into SQL and parameter arrays. |
|
| 458 | + * |
|
| 459 | + * @param DynamicOperandInterface $operand |
|
| 460 | + * @param string $operator One of the JCR_OPERATOR_* constants |
|
| 461 | + * @param SourceInterface $source The source |
|
| 462 | + * @param array &$statementParts The query parts |
|
| 463 | + * @param array &$parameters The parameters that will replace the markers |
|
| 464 | + * @param string $valueFunction an optional SQL function to apply to the operand value |
|
| 465 | + * @return void |
|
| 466 | + */ |
|
| 467 | + protected function parseDynamicOperand(DynamicOperandInterface $operand, $operator, SourceInterface $source, array &$statementParts, array &$parameters, $valueFunction = null) |
|
| 468 | + { |
|
| 469 | + if ($operand instanceof LowerCaseInterface) { |
|
| 470 | + $this->parseDynamicOperand($operand->getOperand(), $operator, $source, $statementParts, $parameters, 'LOWER'); |
|
| 471 | + } elseif ($operand instanceof UpperCaseInterface) { |
|
| 472 | + $this->parseDynamicOperand($operand->getOperand(), $operator, $source, $statementParts, $parameters, 'UPPER'); |
|
| 473 | + } elseif ($operand instanceof PropertyValueInterface) { |
|
| 474 | + $propertyName = $operand->getPropertyName(); |
|
| 475 | + |
|
| 476 | + // Reset value. |
|
| 477 | + $this->currentChildTableNameAlias = ''; |
|
| 478 | + |
|
| 479 | + if ($source instanceof SelectorInterface) { |
|
| 480 | + $tableName = $this->query->getType(); |
|
| 481 | + while (strpos($propertyName, '.') !== false) { |
|
| 482 | + $this->addUnionStatement($tableName, $propertyName, $statementParts); |
|
| 483 | + } |
|
| 484 | + } elseif ($source instanceof JoinInterface) { |
|
| 485 | + $tableName = $source->getJoinCondition()->getSelector1Name(); |
|
| 486 | + } |
|
| 487 | + |
|
| 488 | + $columnName = $propertyName; |
|
| 489 | + $resolvedOperator = $this->resolveOperator($operator); |
|
| 490 | + $constraintSQL = ''; |
|
| 491 | + |
|
| 492 | + $marker = $operator === QueryInterface::OPERATOR_IN |
|
| 493 | + ? '(?)' |
|
| 494 | + : '?'; |
|
| 495 | + |
|
| 496 | + if ($valueFunction === null) { |
|
| 497 | + $constraintSQL .= (!empty($tableName) ? $tableName . '.' : '') . $columnName . ' ' . $resolvedOperator . ' ' . $marker; |
|
| 498 | + } else { |
|
| 499 | + $constraintSQL .= $valueFunction . '(' . (!empty($tableName) ? $tableName . '.' : '') . $columnName . ') ' . $resolvedOperator . ' ' . $marker; |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + if (isset($tableName) && !empty($this->currentChildTableNameAlias)) { |
|
| 503 | + $constraintSQL = $this->replaceTableNameByAlias($tableName, $this->currentChildTableNameAlias, $constraintSQL); |
|
| 504 | + } |
|
| 505 | + $statementParts['where'][] = $constraintSQL; |
|
| 506 | + } |
|
| 507 | + } |
|
| 508 | + |
|
| 509 | + /** |
|
| 510 | + * @param string &$tableName |
|
| 511 | + * @param string &$propertyPath |
|
| 512 | + * @param array &$statementParts |
|
| 513 | + */ |
|
| 514 | + protected function addUnionStatement(&$tableName, &$propertyPath, array &$statementParts) |
|
| 515 | + { |
|
| 516 | + |
|
| 517 | + $table = Tca::table($tableName); |
|
| 518 | + |
|
| 519 | + $explodedPropertyPath = explode('.', $propertyPath, 2); |
|
| 520 | + $fieldName = $explodedPropertyPath[0]; |
|
| 521 | + |
|
| 522 | + // Field of type "group" are special because property path must contain the table name |
|
| 523 | + // to determine the relation type. Example for sys_category, property path will look like "items.sys_file" |
|
| 524 | + $parts = explode('.', $propertyPath, 3); |
|
| 525 | + if ($table->field($fieldName)->isGroup() && count($parts) > 2) { |
|
| 526 | + $explodedPropertyPath[0] = $parts[0] . '.' . $parts[1]; |
|
| 527 | + $explodedPropertyPath[1] = $parts[2]; |
|
| 528 | + $fieldName = $explodedPropertyPath[0]; |
|
| 529 | + } |
|
| 530 | + |
|
| 531 | + $parentKeyFieldName = $table->field($fieldName)->getForeignField(); |
|
| 532 | + $childTableName = $table->field($fieldName)->getForeignTable(); |
|
| 533 | + |
|
| 534 | + if ($childTableName === null) { |
|
| 535 | + throw new InvalidRelationConfigurationException('The relation information for property "' . $fieldName . '" of class "' . $tableName . '" is missing.', 1353170925); |
|
| 536 | + } |
|
| 537 | + |
|
| 538 | + if ($table->field($fieldName)->hasOne()) { // includes relation "one-to-one" and "many-to-one" |
|
| 539 | + // sometimes the opposite relation is not defined. We don't want to force this config for backward compatibility reasons. |
|
| 540 | + // $parentKeyFieldName === null does the trick somehow. Before condition was if (isset($parentKeyFieldName)) |
|
| 541 | + if ($table->field($fieldName)->hasRelationManyToOne() || $parentKeyFieldName === null) { |
|
| 542 | + $statementParts['unions'][$childTableName] = 'LEFT JOIN ' . $childTableName . ' ON ' . $tableName . '.' . $fieldName . '=' . $childTableName . '.uid'; |
|
| 543 | + } else { |
|
| 544 | + $statementParts['unions'][$childTableName] = 'LEFT JOIN ' . $childTableName . ' ON ' . $tableName . '.uid=' . $childTableName . '.' . $parentKeyFieldName; |
|
| 545 | + } |
|
| 546 | + } elseif ($table->field($fieldName)->hasRelationManyToMany()) { |
|
| 547 | + $relationTableName = $table->field($fieldName)->getManyToManyTable(); |
|
| 548 | + |
|
| 549 | + $parentKeyFieldName = $table->field($fieldName)->isOppositeRelation() ? 'uid_foreign' : 'uid_local'; |
|
| 550 | + $childKeyFieldName = !$table->field($fieldName)->isOppositeRelation() ? 'uid_foreign' : 'uid_local'; |
|
| 551 | + |
|
| 552 | + // MM table e.g sys_category_record_mm |
|
| 553 | + $relationTableNameAlias = $this->generateAlias($relationTableName); |
|
| 554 | + $join = sprintf( |
|
| 555 | + 'LEFT JOIN %s AS %s ON %s.uid=%s.%s', $relationTableName, |
|
| 556 | + $relationTableNameAlias, |
|
| 557 | + $tableName, |
|
| 558 | + $relationTableNameAlias, |
|
| 559 | + $parentKeyFieldName |
|
| 560 | + ); |
|
| 561 | + $statementParts['unions'][$relationTableNameAlias] = $join; |
|
| 562 | + |
|
| 563 | + // Foreign table e.g sys_category |
|
| 564 | + $childTableNameAlias = $this->generateAlias($childTableName); |
|
| 565 | + $this->currentChildTableNameAlias = $childTableNameAlias; |
|
| 566 | + $join = sprintf( |
|
| 567 | + 'LEFT JOIN %s AS %s ON %s.%s=%s.uid', |
|
| 568 | + $childTableName, |
|
| 569 | + $childTableNameAlias, |
|
| 570 | + $relationTableNameAlias, |
|
| 571 | + $childKeyFieldName, |
|
| 572 | + $childTableNameAlias |
|
| 573 | + ); |
|
| 574 | + $statementParts['unions'][$childTableNameAlias] = $join; |
|
| 575 | + |
|
| 576 | + // Find a possible table name for a MM condition. |
|
| 577 | + $tableNameCondition = $table->field($fieldName)->getAdditionalTableNameCondition(); |
|
| 578 | + if ($tableNameCondition) { |
|
| 579 | + |
|
| 580 | + // If we can find a source file name, we can then retrieve more MM conditions from the TCA such as a field name. |
|
| 581 | + $sourceFileName = $this->query->getSourceFieldName(); |
|
| 582 | + if (empty($sourceFileName)) { |
|
| 583 | + $additionalMMConditions = array( |
|
| 584 | + 'tablenames' => $tableNameCondition, |
|
| 585 | + ); |
|
| 586 | + } else { |
|
| 587 | + $additionalMMConditions = Tca::table($tableNameCondition)->field($sourceFileName)->getAdditionalMMCondition(); |
|
| 588 | + } |
|
| 589 | + |
|
| 590 | + foreach ($additionalMMConditions as $additionalFieldName => $additionalMMCondition) { |
|
| 591 | + $additionalJoin = sprintf(' AND %s.%s = "%s"', $relationTableNameAlias, $additionalFieldName, $additionalMMCondition); |
|
| 592 | + $statementParts['unions'][$relationTableNameAlias] .= $additionalJoin; |
|
| 593 | + |
|
| 594 | + $additionalJoin = sprintf(' AND %s.%s = "%s"', $relationTableNameAlias, $additionalFieldName, $additionalMMCondition); |
|
| 595 | + $statementParts['unions'][$childTableNameAlias] .= $additionalJoin; |
|
| 596 | + } |
|
| 597 | + } |
|
| 598 | + |
|
| 599 | + } elseif ($table->field($fieldName)->hasMany()) { // includes relations "many-to-one" and "csv" relations |
|
| 600 | + $childTableNameAlias = $this->generateAlias($childTableName); |
|
| 601 | + $this->currentChildTableNameAlias = $childTableNameAlias; |
|
| 602 | + |
|
| 603 | + if (isset($parentKeyFieldName)) { |
|
| 604 | + $join = sprintf( |
|
| 605 | + 'LEFT JOIN %s AS %s ON %s.uid=%s.%s', |
|
| 606 | + $childTableName, |
|
| 607 | + $childTableNameAlias, |
|
| 608 | + $tableName, |
|
| 609 | + $childTableNameAlias, |
|
| 610 | + $parentKeyFieldName |
|
| 611 | + ); |
|
| 612 | + $statementParts['unions'][$childTableNameAlias] = $join; |
|
| 613 | + } else { |
|
| 614 | + $join = sprintf( |
|
| 615 | + 'LEFT JOIN %s AS %s ON (FIND_IN_SET(%s.uid, %s.%s))', |
|
| 616 | + $childTableName, |
|
| 617 | + $childTableNameAlias, |
|
| 618 | + $childTableNameAlias, |
|
| 619 | + $tableName, |
|
| 620 | + $fieldName |
|
| 621 | + ); |
|
| 622 | + $statementParts['unions'][$childTableNameAlias] = $join; |
|
| 623 | + } |
|
| 624 | + } else { |
|
| 625 | + throw new Exception('Could not determine type of relation.', 1252502725); |
|
| 626 | + } |
|
| 627 | + |
|
| 628 | + $statementParts['keywords']['distinct'] = 'DISTINCT'; |
|
| 629 | + $propertyPath = $explodedPropertyPath[1]; |
|
| 630 | + $tableName = $childTableName; |
|
| 631 | + } |
|
| 632 | + |
|
| 633 | + /** |
|
| 634 | + * Returns the SQL operator for the given JCR operator type. |
|
| 635 | + * |
|
| 636 | + * @param string $operator One of the JCR_OPERATOR_* constants |
|
| 637 | + * @return string an SQL operator |
|
| 638 | + * @throws Exception |
|
| 639 | + */ |
|
| 640 | + protected function resolveOperator($operator) |
|
| 641 | + { |
|
| 642 | + switch ($operator) { |
|
| 643 | + case self::OPERATOR_EQUAL_TO_NULL: |
|
| 644 | + $operator = 'IS'; |
|
| 645 | + break; |
|
| 646 | + case self::OPERATOR_NOT_EQUAL_TO_NULL: |
|
| 647 | + $operator = 'IS NOT'; |
|
| 648 | + break; |
|
| 649 | + case QueryInterface::OPERATOR_IN: |
|
| 650 | + $operator = 'IN'; |
|
| 651 | + break; |
|
| 652 | + case QueryInterface::OPERATOR_EQUAL_TO: |
|
| 653 | + $operator = '='; |
|
| 654 | + break; |
|
| 655 | + case QueryInterface::OPERATOR_NOT_EQUAL_TO: |
|
| 656 | + $operator = '!='; |
|
| 657 | + break; |
|
| 658 | + case QueryInterface::OPERATOR_LESS_THAN: |
|
| 659 | + $operator = '<'; |
|
| 660 | + break; |
|
| 661 | + case QueryInterface::OPERATOR_LESS_THAN_OR_EQUAL_TO: |
|
| 662 | + $operator = '<='; |
|
| 663 | + break; |
|
| 664 | + case QueryInterface::OPERATOR_GREATER_THAN: |
|
| 665 | + $operator = '>'; |
|
| 666 | + break; |
|
| 667 | + case QueryInterface::OPERATOR_GREATER_THAN_OR_EQUAL_TO: |
|
| 668 | + $operator = '>='; |
|
| 669 | + break; |
|
| 670 | + case QueryInterface::OPERATOR_LIKE: |
|
| 671 | + $operator = 'LIKE'; |
|
| 672 | + break; |
|
| 673 | + default: |
|
| 674 | + throw new Exception('Unsupported operator encountered.', 1242816073); |
|
| 675 | + } |
|
| 676 | + return $operator; |
|
| 677 | + } |
|
| 678 | + |
|
| 679 | + /** |
|
| 680 | + * Adds additional WHERE statements according to the query settings. |
|
| 681 | + * |
|
| 682 | + * @param QuerySettingsInterface $querySettings The TYPO3 CMS specific query settings |
|
| 683 | + * @param string $tableNameOrAlias The table name to add the additional where clause for |
|
| 684 | + * @param array &$statementParts |
|
| 685 | + * @return void |
|
| 686 | + */ |
|
| 687 | + protected function addAdditionalWhereClause(QuerySettingsInterface $querySettings, $tableNameOrAlias, &$statementParts) |
|
| 688 | + { |
|
| 689 | + $this->addVisibilityConstraintStatement($querySettings, $tableNameOrAlias, $statementParts); |
|
| 690 | + if ($querySettings->getRespectSysLanguage()) { |
|
| 691 | + $this->addSysLanguageStatement($tableNameOrAlias, $statementParts, $querySettings); |
|
| 692 | + } |
|
| 693 | + } |
|
| 694 | + |
|
| 695 | + /** |
|
| 696 | + * Adds enableFields and deletedClause to the query if necessary |
|
| 697 | + * |
|
| 698 | + * @param QuerySettingsInterface $querySettings |
|
| 699 | + * @param string $tableNameOrAlias The database table name |
|
| 700 | + * @param array &$statementParts The query parts |
|
| 701 | + * @return void |
|
| 702 | + */ |
|
| 703 | + protected function addVisibilityConstraintStatement(QuerySettingsInterface $querySettings, $tableNameOrAlias, array &$statementParts) |
|
| 704 | + { |
|
| 705 | + $statement = ''; |
|
| 706 | + $tableName = $this->resolveTableNameAlias($tableNameOrAlias); |
|
| 707 | + if (is_array($GLOBALS['TCA'][$tableName]['ctrl'])) { |
|
| 708 | + $ignoreEnableFields = $querySettings->getIgnoreEnableFields(); |
|
| 709 | + $enableFieldsToBeIgnored = $querySettings->getEnableFieldsToBeIgnored(); |
|
| 710 | + $includeDeleted = $querySettings->getIncludeDeleted(); |
|
| 711 | + if (ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isFrontend()) { |
|
| 712 | + $statement .= $this->getFrontendConstraintStatement($tableNameOrAlias, $ignoreEnableFields, $enableFieldsToBeIgnored, $includeDeleted); |
|
| 713 | + } else { |
|
| 714 | + // 'BE' case |
|
| 715 | + $statement .= $this->getBackendConstraintStatement($tableNameOrAlias, $ignoreEnableFields, $includeDeleted); |
|
| 716 | + } |
|
| 717 | + |
|
| 718 | + // Remove the prefixing "AND" if any. |
|
| 719 | + if (!empty($statement)) { |
|
| 720 | + $statement = strtolower(substr($statement, 1, 3)) === 'and' ? substr($statement, 5) : $statement; |
|
| 721 | + $statementParts['additionalWhereClause'][$tableNameOrAlias][] = $statement; |
|
| 722 | + } |
|
| 723 | + } |
|
| 724 | + } |
|
| 725 | + |
|
| 726 | + /** |
|
| 727 | + * Returns constraint statement for frontend context |
|
| 728 | + * |
|
| 729 | + * @param string $tableNameOrAlias |
|
| 730 | + * @param boolean $ignoreEnableFields A flag indicating whether the enable fields should be ignored |
|
| 731 | + * @param array $enableFieldsToBeIgnored If $ignoreEnableFields is true, this array specifies enable fields to be ignored. If it is null or an empty array (default) all enable fields are ignored. |
|
| 732 | + * @param boolean $includeDeleted A flag indicating whether deleted records should be included |
|
| 733 | + * @return string |
|
| 734 | + * @throws Exception\InconsistentQuerySettingsException |
|
| 735 | + */ |
|
| 736 | + protected function getFrontendConstraintStatement($tableNameOrAlias, $ignoreEnableFields, $enableFieldsToBeIgnored, $includeDeleted) |
|
| 737 | + { |
|
| 738 | + $statement = ''; |
|
| 739 | + $tableName = $this->resolveTableNameAlias($tableNameOrAlias); |
|
| 740 | + if ($ignoreEnableFields && !$includeDeleted) { |
|
| 741 | + if (count($enableFieldsToBeIgnored)) { |
|
| 742 | + // array_combine() is necessary because of the way \TYPO3\CMS\Frontend\Page\PageRepository::enableFields() is implemented |
|
| 743 | + $statement .= $this->getPageRepository()->enableFields($tableName, -1, array_combine($enableFieldsToBeIgnored, $enableFieldsToBeIgnored)); |
|
| 744 | + } else { |
|
| 745 | + $statement .= $this->getPageRepository()->deleteClause($tableName); |
|
| 746 | + } |
|
| 747 | + } elseif (!$ignoreEnableFields && !$includeDeleted) { |
|
| 748 | + $statement .= $this->getPageRepository()->enableFields($tableName); |
|
| 749 | + } elseif (!$ignoreEnableFields && $includeDeleted) { |
|
| 750 | + throw new InconsistentQuerySettingsException('Query setting "ignoreEnableFields=false" can not be used together with "includeDeleted=true" in frontend context.', 1327678173); |
|
| 751 | + } |
|
| 752 | + return $this->replaceTableNameByAlias($tableName, $tableNameOrAlias, $statement); |
|
| 753 | + } |
|
| 754 | + |
|
| 755 | + /** |
|
| 756 | + * Returns constraint statement for backend context |
|
| 757 | + * |
|
| 758 | + * @param string $tableNameOrAlias |
|
| 759 | + * @param boolean $ignoreEnableFields A flag indicating whether the enable fields should be ignored |
|
| 760 | + * @param boolean $includeDeleted A flag indicating whether deleted records should be included |
|
| 761 | + * @return string |
|
| 762 | + */ |
|
| 763 | + protected function getBackendConstraintStatement($tableNameOrAlias, $ignoreEnableFields, $includeDeleted) |
|
| 764 | + { |
|
| 765 | + $tableName = $this->resolveTableNameAlias($tableNameOrAlias); |
|
| 766 | + $statement = ''; |
|
| 767 | + if (!$ignoreEnableFields) { |
|
| 768 | + $statement .= BackendUtility::BEenableFields($tableName); |
|
| 769 | + } |
|
| 770 | + |
|
| 771 | + // If the table is found to have "workspace" support, add the corresponding fields in the statement. |
|
| 772 | + if (Tca::table($tableName)->hasWorkspaceSupport()) { |
|
| 773 | + if ($this->getBackendUser()->workspace === 0) { |
|
| 774 | + $statement .= ' AND ' . $tableName . '.t3ver_state<=' . new VersionState(VersionState::DEFAULT_STATE); |
|
| 775 | + } else { |
|
| 776 | + // Show only records of live and of the current workspace |
|
| 777 | + // In case we are in a Versioning preview |
|
| 778 | + $statement .= ' AND (' . |
|
| 779 | + $tableName . '.t3ver_wsid=0 OR ' . |
|
| 780 | + $tableName . '.t3ver_wsid=' . (int)$this->getBackendUser()->workspace . |
|
| 781 | + ')'; |
|
| 782 | + } |
|
| 783 | + |
|
| 784 | + // Check if this segment make sense here or whether it should be in the "if" part when we have workspace = 0 |
|
| 785 | + $statement .= ' AND ' . $tableName . '.pid<>-1'; |
|
| 786 | + } |
|
| 787 | + |
|
| 788 | + if (!$includeDeleted) { |
|
| 789 | + $statement .= BackendUtility::deleteClause($tableName); |
|
| 790 | + } |
|
| 791 | + |
|
| 792 | + return $this->replaceTableNameByAlias($tableName, $tableNameOrAlias, $statement); |
|
| 793 | + } |
|
| 794 | + |
|
| 795 | + /** |
|
| 796 | + * Builds the language field statement |
|
| 797 | + * |
|
| 798 | + * @param string $tableNameOrAlias The database table name |
|
| 799 | + * @param array &$statementParts The query parts |
|
| 800 | + * @param QuerySettingsInterface $querySettings The TYPO3 CMS specific query settings |
|
| 801 | + * @return void |
|
| 802 | + * @throws Exception |
|
| 803 | + */ |
|
| 804 | + protected function addSysLanguageStatement($tableNameOrAlias, array &$statementParts, $querySettings) |
|
| 805 | + { |
|
| 806 | + $tableName = $this->resolveTableNameAlias($tableNameOrAlias); |
|
| 807 | + if (is_array($GLOBALS['TCA'][$tableName]['ctrl'])) { |
|
| 808 | + if (!empty($GLOBALS['TCA'][$tableName]['ctrl']['languageField'])) { |
|
| 809 | + // Select all entries for the current language |
|
| 810 | + $additionalWhereClause = $tableNameOrAlias . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['languageField'] . ' IN (' . intval($querySettings->getLanguageUid()) . ',-1)'; |
|
| 811 | + // If any language is set -> get those entries which are not translated yet |
|
| 812 | + // They will be removed by t3lib_page::getRecordOverlay if not matching overlay mode |
|
| 813 | + if (isset($GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField']) |
|
| 814 | + && $querySettings->getLanguageUid() > 0 |
|
| 815 | + ) { |
|
| 816 | + $additionalWhereClause .= ' OR (' . $tableNameOrAlias . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['languageField'] . '=0' . |
|
| 817 | + ' AND ' . $tableNameOrAlias . '.uid NOT IN (SELECT ' . $tableName . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'] . |
|
| 818 | + ' FROM ' . $tableName . |
|
| 819 | + ' WHERE ' . $tableName . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'] . '>0' . |
|
| 820 | + ' AND ' . $tableName . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['languageField'] . '>0'; |
|
| 821 | + |
|
| 822 | + // Add delete clause to ensure all entries are loaded |
|
| 823 | + if (isset($GLOBALS['TCA'][$tableName]['ctrl']['delete'])) { |
|
| 824 | + $additionalWhereClause .= ' AND ' . $tableNameOrAlias . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['delete'] . '=0'; |
|
| 825 | + } |
|
| 826 | + $additionalWhereClause .= '))'; |
|
| 827 | + } |
|
| 828 | + $statementParts['additionalWhereClause'][$tableNameOrAlias][] = '(' . $additionalWhereClause . ')'; |
|
| 829 | + } |
|
| 830 | + } |
|
| 831 | + } |
|
| 832 | + |
|
| 833 | + /** |
|
| 834 | + * Transforms orderings into SQL. |
|
| 835 | + * |
|
| 836 | + * @param array $orderings An array of orderings (Tx_Extbase_Persistence_QOM_Ordering) |
|
| 837 | + * @param SourceInterface $source The source |
|
| 838 | + * @param array &$statementParts The query parts |
|
| 839 | + * @return void |
|
| 840 | + * @throws Exception\UnsupportedOrderException |
|
| 841 | + */ |
|
| 842 | + protected function parseOrderings(array $orderings, SourceInterface $source, array &$statementParts) |
|
| 843 | + { |
|
| 844 | + foreach ($orderings as $fieldNameAndPath => $order) { |
|
| 845 | + switch ($order) { |
|
| 846 | + case QueryInterface::ORDER_ASCENDING: |
|
| 847 | + $order = 'ASC'; |
|
| 848 | + break; |
|
| 849 | + case QueryInterface::ORDER_DESCENDING: |
|
| 850 | + $order = 'DESC'; |
|
| 851 | + break; |
|
| 852 | + default: |
|
| 853 | + throw new UnsupportedOrderException('Unsupported order encountered.', 1456845126); |
|
| 854 | + } |
|
| 855 | + |
|
| 856 | + $tableName = $this->getFieldPathResolver()->getDataType($fieldNameAndPath, $this->query->getType()); |
|
| 857 | + $fieldName = $this->getFieldPathResolver()->stripFieldPath($fieldNameAndPath, $tableName); |
|
| 858 | + $statementParts['orderings'][] = sprintf('%s.%s %s', $tableName, $fieldName, $order); |
|
| 859 | + } |
|
| 860 | + } |
|
| 861 | + |
|
| 862 | + /** |
|
| 863 | + * Transforms limit and offset into SQL |
|
| 864 | + * |
|
| 865 | + * @param int $limit |
|
| 866 | + * @param int $offset |
|
| 867 | + * @param array &$statementParts |
|
| 868 | + * @return void |
|
| 869 | + */ |
|
| 870 | + protected function parseLimitAndOffset($limit, $offset, array &$statementParts) |
|
| 871 | + { |
|
| 872 | + if ($limit !== null && $offset !== null) { |
|
| 873 | + $statementParts['limit'] = intval($offset) . ', ' . intval($limit); |
|
| 874 | + } elseif ($limit !== null) { |
|
| 875 | + $statementParts['limit'] = intval($limit); |
|
| 876 | + } |
|
| 877 | + } |
|
| 878 | + |
|
| 879 | + /** |
|
| 880 | + * @param array $rows |
|
| 881 | + * @return array |
|
| 882 | + */ |
|
| 883 | + protected function getContentObjects(array $rows): array |
|
| 884 | + { |
|
| 885 | + $contentObjects = []; |
|
| 886 | + foreach ($rows as $row) { |
|
| 887 | + |
|
| 888 | + // Get language uid from querySettings. |
|
| 889 | + // Ensure the backend handling is not broken (fallback to Get parameter 'L' if needed) |
|
| 890 | + $overlaidRow = $this->doLanguageAndWorkspaceOverlay( |
|
| 891 | + $row, |
|
| 892 | + $this->query->getTypo3QuerySettings() |
|
| 893 | + ); |
|
| 894 | + |
|
| 895 | + $contentObjects[] = GeneralUtility::makeInstance( |
|
| 896 | + Content::class, |
|
| 897 | + $this->query->getType(), |
|
| 898 | + $overlaidRow |
|
| 899 | + ); |
|
| 900 | + } |
|
| 901 | + |
|
| 902 | + return $contentObjects; |
|
| 903 | + } |
|
| 904 | + |
|
| 905 | + /** |
|
| 906 | + * Performs workspace and language overlay on the given row array. The language and workspace id is automatically |
|
| 907 | + * detected (depending on FE or BE context). You can also explicitly set the language/workspace id. |
|
| 908 | + * |
|
| 909 | + * @param array $row |
|
| 910 | + * @param QuerySettingsInterface $querySettings The TYPO3 CMS specific query settings |
|
| 911 | + * @return array |
|
| 912 | + */ |
|
| 913 | + protected function doLanguageAndWorkspaceOverlay(array $row, $querySettings) |
|
| 914 | + { |
|
| 915 | + $tableName = $this->getTableName(); |
|
| 916 | + |
|
| 917 | + $pageRepository = $this->getPageRepository(); |
|
| 918 | + if (is_object($GLOBALS['TSFE'])) { |
|
| 919 | + $languageMode = $GLOBALS['TSFE']->sys_language_mode; |
|
| 920 | + #if ($this->isBackendUserLogged() && $this->getBackendUser()->workspace !== 0) { |
|
| 921 | + # $pageRepository->versioningWorkspaceId = $this->getBackendUser()->workspace; |
|
| 922 | + #} |
|
| 923 | + } else { |
|
| 924 | + $languageMode = ''; |
|
| 925 | + $workspaceUid = $this->getBackendUser()->workspace; |
|
| 926 | + #$pageRepository->versioningWorkspaceId = $workspaceUid; |
|
| 927 | + #if ($this->getBackendUser()->workspace !== 0) { |
|
| 928 | + # $pageRepository->versioningPreview = 1; |
|
| 929 | + #} |
|
| 930 | + } |
|
| 931 | + |
|
| 932 | + // If current row is a translation select its parent |
|
| 933 | + if (isset($GLOBALS['TCA'][$tableName]['ctrl']['languageField']) |
|
| 934 | + && isset($GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField']) |
|
| 935 | + ) { |
|
| 936 | + if (isset($row[$GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField']]) |
|
| 937 | + && $row[$GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField']] > 0 |
|
| 938 | + ) { |
|
| 939 | + $queryBuilder = $this->getQueryBuilder(); |
|
| 940 | + $row = $queryBuilder |
|
| 941 | + ->select($tableName . '.*') |
|
| 942 | + ->from($tableName) |
|
| 943 | + ->andWhere( |
|
| 944 | + $tableName . '.uid=' . (int)$row[$GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField']], |
|
| 945 | + $tableName . '.' . $GLOBALS['TCA'][$tableName]['ctrl']['languageField'] . ' = 0' |
|
| 946 | + ) |
|
| 947 | + ->execute() |
|
| 948 | + ->fetch(); |
|
| 949 | + } |
|
| 950 | + } |
|
| 951 | + |
|
| 952 | + // Retrieve the original uid; Used for Workspaces! |
|
| 953 | + if (ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isBackend()) { |
|
| 954 | + $pageRepository->versionOL($tableName, $row, true, true); |
|
| 955 | + } else { |
|
| 956 | + \TYPO3\CMS\Backend\Utility\BackendUtility::workspaceOL($tableName, $row); |
|
| 957 | + } |
|
| 958 | + if (isset($row['_ORIG_uid'])) { |
|
| 959 | + $row['uid'] = $row['_ORIG_uid']; |
|
| 960 | + } |
|
| 961 | + |
|
| 962 | + // Special case for table "pages" |
|
| 963 | + if ($tableName == 'pages') { |
|
| 964 | + $row = $pageRepository->getPageOverlay($row, $querySettings->getLanguageUid()); |
|
| 965 | + } elseif (isset($GLOBALS['TCA'][$tableName]['ctrl']['languageField']) |
|
| 966 | + && $GLOBALS['TCA'][$tableName]['ctrl']['languageField'] !== '' |
|
| 967 | + ) { |
|
| 968 | + if (in_array($row[$GLOBALS['TCA'][$tableName]['ctrl']['languageField']], array(-1, 0))) { |
|
| 969 | + $overlayMode = $languageMode === 'strict' ? 'hideNonTranslated' : ''; |
|
| 970 | + $row = $pageRepository->getRecordOverlay($tableName, $row, $querySettings->getLanguageUid(), $overlayMode); |
|
| 971 | + } |
|
| 972 | + } |
|
| 973 | + |
|
| 974 | + return $row; |
|
| 975 | + } |
|
| 976 | + |
|
| 977 | + /** |
|
| 978 | + * Return a resolved table name given a possible table name alias. |
|
| 979 | + * |
|
| 980 | + * @param string $tableNameOrAlias |
|
| 981 | + * @return string |
|
| 982 | + */ |
|
| 983 | + protected function resolveTableNameAlias($tableNameOrAlias) |
|
| 984 | + { |
|
| 985 | + $resolvedTableName = $tableNameOrAlias; |
|
| 986 | + if (!empty($this->tableNameAliases['aliases'][$tableNameOrAlias])) { |
|
| 987 | + $resolvedTableName = $this->tableNameAliases['aliases'][$tableNameOrAlias]; |
|
| 988 | + } |
|
| 989 | + return $resolvedTableName; |
|
| 990 | + } |
|
| 991 | + |
|
| 992 | + /** |
|
| 993 | + * Generate a unique table name alias for the given table name. |
|
| 994 | + * |
|
| 995 | + * @param string $tableName |
|
| 996 | + * @return string |
|
| 997 | + */ |
|
| 998 | + protected function generateAlias($tableName) |
|
| 999 | + { |
|
| 1000 | + |
|
| 1001 | + if (!isset($this->tableNameAliases['aliasIncrement'][$tableName])) { |
|
| 1002 | + $this->tableNameAliases['aliasIncrement'][$tableName] = 0; |
|
| 1003 | + } |
|
| 1004 | + |
|
| 1005 | + $numberOfAliases = $this->tableNameAliases['aliasIncrement'][$tableName]; |
|
| 1006 | + $tableNameAlias = $tableName . $numberOfAliases; |
|
| 1007 | + |
|
| 1008 | + $this->tableNameAliases['aliasIncrement'][$tableName]++; |
|
| 1009 | + $this->tableNameAliases['aliases'][$tableNameAlias] = $tableName; |
|
| 1010 | + |
|
| 1011 | + return $tableNameAlias; |
|
| 1012 | + } |
|
| 1013 | + |
|
| 1014 | + /** |
|
| 1015 | + * Replace the table names by its table name alias within the given statement. |
|
| 1016 | + * |
|
| 1017 | + * @param string $tableName |
|
| 1018 | + * @param string $tableNameAlias |
|
| 1019 | + * @param string $statement |
|
| 1020 | + * @return string |
|
| 1021 | + */ |
|
| 1022 | + protected function replaceTableNameByAlias($tableName, $tableNameAlias, $statement) |
|
| 1023 | + { |
|
| 1024 | + if ($statement && $tableName !== $tableNameAlias) { |
|
| 1025 | + $statement = str_replace($tableName, $tableNameAlias, $statement); |
|
| 1026 | + } |
|
| 1027 | + return $statement; |
|
| 1028 | + } |
|
| 1029 | + |
|
| 1030 | + /** |
|
| 1031 | + * Returns an instance of the current Backend User. |
|
| 1032 | + * |
|
| 1033 | + * @return BackendUserAuthentication |
|
| 1034 | + */ |
|
| 1035 | + protected function getBackendUser() |
|
| 1036 | + { |
|
| 1037 | + return $GLOBALS['BE_USER']; |
|
| 1038 | + } |
|
| 1039 | + |
|
| 1040 | + /** |
|
| 1041 | + * Tell whether a Backend User is logged in. |
|
| 1042 | + * |
|
| 1043 | + * @return bool |
|
| 1044 | + */ |
|
| 1045 | + protected function isBackendUserLogged() |
|
| 1046 | + { |
|
| 1047 | + return is_object($GLOBALS['BE_USER']); |
|
| 1048 | + } |
|
| 1049 | + |
|
| 1050 | + /** |
|
| 1051 | + * @return PageRepository|object |
|
| 1052 | + */ |
|
| 1053 | + protected function getPageRepository() |
|
| 1054 | + { |
|
| 1055 | + if (!$this->pageRepository instanceof PageRepository) { |
|
| 1056 | + if (ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isFrontend() && is_object($GLOBALS['TSFE'])) { |
|
| 1057 | + $this->pageRepository = $GLOBALS['TSFE']->sys_page; |
|
| 1058 | + } else { |
|
| 1059 | + $this->pageRepository = GeneralUtility::makeInstance(PageRepository::class); |
|
| 1060 | + } |
|
| 1061 | + } |
|
| 1062 | + |
|
| 1063 | + return $this->pageRepository; |
|
| 1064 | + } |
|
| 1065 | + |
|
| 1066 | + /** |
|
| 1067 | + * @return FieldPathResolver|object |
|
| 1068 | + */ |
|
| 1069 | + protected function getFieldPathResolver() |
|
| 1070 | + { |
|
| 1071 | + return GeneralUtility::makeInstance(FieldPathResolver::class); |
|
| 1072 | + } |
|
| 1073 | + |
|
| 1074 | + /** |
|
| 1075 | + * @return object|Connection |
|
| 1076 | + */ |
|
| 1077 | + protected function getConnection(): Connection |
|
| 1078 | + { |
|
| 1079 | + /** @var ConnectionPool $connectionPool */ |
|
| 1080 | + return GeneralUtility::makeInstance(ConnectionPool::class) |
|
| 1081 | + ->getConnectionForTable($this->getTableName()); |
|
| 1082 | + } |
|
| 1083 | + |
|
| 1084 | + /** |
|
| 1085 | + * @return object|QueryBuilder |
|
| 1086 | + */ |
|
| 1087 | + protected function getQueryBuilder(): QueryBuilder |
|
| 1088 | + { |
|
| 1089 | + /** @var ConnectionPool $connectionPool */ |
|
| 1090 | + $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class); |
|
| 1091 | + return $connectionPool->getQueryBuilderForTable($this->getTableName()); |
|
| 1092 | + } |
|
| 1093 | + |
|
| 1094 | + /** |
|
| 1095 | + * @return string |
|
| 1096 | + */ |
|
| 1097 | + public function getTableName(): string |
|
| 1098 | + { |
|
| 1099 | + return $this->query->getSource()->getNodeTypeName(); // getSelectorName() |
|
| 1100 | + } |
|
| 1101 | 1101 | |
| 1102 | 1102 | } |