public function applyDefaultPreferences($table, $preferences) { // Table-specific default values if (array_key_exists($table, self::$defaultPreferencesValuesByTable)) { $tableDefaultPreferences = self::$defaultPreferencesValuesByTable[$table]; foreach ($tableDefaultPreferences as $field => $defaultValue) { if (!isset($preferences[$field])) { $preferences[$field] = $defaultValue; } } } // Global default values $primaryKeyFieldName = TableSchema::getTablePrimaryKey($table); if ($primaryKeyFieldName) { self::$defaultPreferencesValues['sort'] = $primaryKeyFieldName; } foreach (self::$defaultPreferencesValues as $field => $defaultValue) { if (!isset($preferences[$field]) || "0" !== $preferences[$field] && empty($preferences[$field])) { if (!isset($preferences[$field])) { $preferences[$field] = $defaultValue; } } } if (isset($preferences['sort'])) { $sortColumn = $preferences['sort']; if (!TableSchema::hasTableColumn($table, $sortColumn)) { $preferences['sort'] = 'id'; } } return $preferences; }
/** * Constructor * * @param AclProvider $acl * @param string $table * @param AdapterInterface $adapter * @param Feature\AbstractFeature|Feature\FeatureSet|Feature\AbstractFeature[] $features * @param ResultSetInterface $resultSetPrototype * @param Sql $sql * @throws Exception\InvalidArgumentException */ public function __construct(Acl $acl, $table, AdapterInterface $adapter, $features = null, ResultSetInterface $resultSetPrototype = null, Sql $sql = null, $primaryKeyName = null) { $this->acl = $acl; if ($features !== null) { if ($features instanceof Feature\AbstractFeature) { $features = array($features); } if (is_array($features)) { $this->featureSet = new Feature\FeatureSet($features); } elseif ($features instanceof Feature\FeatureSet) { $this->featureSet = $features; } else { throw new Exception\InvalidArgumentException('TableGateway expects $feature to be an instance of an AbstractFeature or a FeatureSet, or an array of AbstractFeatures'); } } else { $this->featureSet = new Feature\FeatureSet(); } if ($primaryKeyName !== null) { $this->primaryKeyFieldName = $primaryKeyName; } else { $tablePrimaryKey = TableSchema::getTablePrimaryKey($table); if ($tablePrimaryKey) { $this->primaryKeyFieldName = $tablePrimaryKey; } } $rowGatewayPrototype = new AclAwareRowGateway($acl, $this->primaryKeyFieldName, $table, $adapter); $rowGatewayFeature = new RowGatewayFeature($rowGatewayPrototype); $this->featureSet->addFeature($rowGatewayFeature); $this->memcache = new MemcacheProvider(); parent::__construct($table, $adapter, $this->featureSet, $resultSetPrototype, $sql); }
/** * Fetch related, foreign rows for one record's ManyToMany relationships. * @param string $table_name * @param string $foreign_table * @param string $junction_table * @param string $junction_key_left * @param string $junction_key_right * @param string $column_equals * @return array Foreign rowset */ public function loadManyToManyRelationships($table_name, $foreign_table, $junction_table, $junction_key_left, $junction_key_right, $column_equals) { $foreign_table_pk = TableSchema::getTablePrimaryKey($foreign_table); $foreign_join_column = "{$foreign_table}.{$foreign_table_pk}"; $junction_join_column = "{$junction_table}.{$junction_key_right}"; $junction_comparison_column = "{$junction_table}.{$junction_key_left}"; $junction_table_pk = TableSchema::getTablePrimaryKey($junction_table); $junction_id_column = "{$junction_table}." . $junction_table_pk; // Less likely name collision: $junction_id_column_alias = "directus_junction_id_column_518d31856e131"; $junction_sort_column_alias = "directus_junction_sort_column_518d318e3f0f5"; $junctionSelectColumns = array($junction_id_column_alias => $junction_table_pk); $sql = new Sql($this->adapter); $select = $sql->select(); // If the Junction Table has a Sort column, do eet. // @todo is this the most efficient way? // @hint TableSchema#getUniqueColumnName $junctionColumns = TableSchema::getAllNonAliasTableColumnNames($junction_table); if (in_array('sort', $junctionColumns)) { $junctionSelectColumns[$junction_sort_column_alias] = "sort"; $select->order($junction_sort_column_alias); } $select->from($foreign_table)->join($junction_table, "{$foreign_join_column} = {$junction_join_column}", $junctionSelectColumns)->where(array($junction_comparison_column => $column_equals))->order("{$junction_id_column} ASC"); // Only select the fields not on the currently authenticated user group's read field blacklist $columns = TableSchema::getAllNonAliasTableColumnNames($foreign_table); $select->columns($columns); $ForeignTable = new RelationalTableGateway($this->acl, $foreign_table, $this->adapter); $results = $ForeignTable->selectWith($select); $results = $results->toArray(); $foreign_data = array(); $columns = TableSchema::getAllNonAliasTableColumns($foreign_table); foreach ($results as $row) { $row = $this->parseRecordValuesByMysqlType($row, $columns); $junction_table_id = (int) $row[$junction_id_column_alias]; unset($row[$junction_id_column_alias]); $entry = array($junction_table_pk => $junction_table_id); if (in_array('sort', $junctionColumns)) { // @TODO: check why is this a string instead of an integer. $entry['sort'] = (int) $row[$junction_sort_column_alias]; unset($row[$junction_sort_column_alias]); } $entry['data'] = $row; $foreign_data[] = $entry; } return array('rows' => $foreign_data); }
/** * Fetch related, foreign rows for one record's ManyToMany relationships. * * @param string $table_name * @param string $foreign_table * @param string $junction_table * @param string $junction_key_left * @param string $junction_key_right * @param string $column_equals * @param string $parentField * @param int $level * * @return array Foreign rowset */ public function loadManyToManyRelationships($table_name, $foreign_table, $junction_table, $junction_key_left, $junction_key_right, $column_equals, $parentField = null, $level = 0) { $foreign_table_pk = TableSchema::getTablePrimaryKey($foreign_table); $foreign_join_column = $foreign_table . '.' . $foreign_table_pk; $junction_join_column = $junction_table . '.' . $junction_key_right; $junction_comparison_column = $junction_table . '.' . $junction_key_left; // ============================================================================= // HOTFIX: prevent infinite circle loop // ============================================================================= if ($parentField && $this->hasToManyCallStack($parentField, $foreign_table)) { return $column_equals; } if ($parentField !== null) { $this->addToManyCallStack($level, $parentField, $foreign_table); } $junction_table_pk = TableSchema::getTablePrimaryKey($junction_table); $junction_id_column = $junction_table . '.' . $junction_table_pk; // Less likely name collision: $junction_id_column_alias = 'directus_junction_id_column_518d31856e131'; $junction_sort_column_alias = 'directus_junction_sort_column_518d318e3f0f5'; $junctionSelectColumns = [$junction_id_column_alias => $junction_table_pk]; $sql = new Sql($this->adapter); $select = $sql->select(); // If the Junction Table has a Sort column, do eet. // @todo is this the most efficient way? // @hint TableSchema#getUniqueColumnName $junctionColumns = TableSchema::getAllNonAliasTableColumnNames($junction_table); if (in_array('sort', $junctionColumns)) { $junctionSelectColumns[$junction_sort_column_alias] = 'sort'; $select->order($junction_sort_column_alias); } $select->from($foreign_table)->join($junction_table, $foreign_join_column . '=' . $junction_join_column, $junctionSelectColumns)->where([$junction_comparison_column => $column_equals])->order($junction_id_column . ' ASC'); // Only select the fields not on the currently authenticated user group's read field blacklist $columns = TableSchema::getAllNonAliasTableColumnNames($foreign_table); $select->columns($columns); $ForeignTable = new RelationalTableGateway($this->acl, $foreign_table, $this->adapter); $results = $ForeignTable->selectWith($select); $results = $results->toArray(); $foreign_data = []; $columns = TableSchema::getAllNonAliasTableColumns($foreign_table); foreach ($results as $row) { $row = $recordData = SchemaManager::parseRecordValuesByType($row, $columns); $junction_table_id = (int) $row[$junction_id_column_alias]; unset($row[$junction_id_column_alias]); $entry = [$junction_table_pk => $junction_table_id]; if (in_array('sort', $junctionColumns)) { // @TODO: check why is this a string instead of an integer. $entry['sort'] = (int) $row[$junction_sort_column_alias]; unset($row[$junction_sort_column_alias]); } $schemaArray = TableSchema::getSchemaArray($foreign_table); $alias_fields = $this->filterSchemaAliasFields($schemaArray); // (fmrly $alias_schema) $row = $this->loadToManyRelationships($row, $alias_fields, $parentField, $level + 1); $entry['data'] = $row; $foreign_data[] = $entry; } return ['rows' => $foreign_data]; }