Ejemplo n.º 1
0
 protected function getDirectusTablesInfo()
 {
     $config = Bootstrap::get('config');
     $blacklist = [];
     if (array_key_exists('tableBlacklist', $config)) {
         $blacklist = $config['tableBlacklist'];
     }
     $select = new Select();
     $select->columns(['table_name', 'hidden' => new Expression('IFNULL(hidden, 0)'), 'single' => new Expression('IFNULL(single, 0)'), 'user_create_column', 'user_update_column', 'date_create_column', 'date_update_column', 'footer', 'list_view', 'column_groupings', 'filter_column_blacklist', 'primary_column']);
     $select->from('directus_tables');
     $skipTables = array_merge(SchemaManager::getDirectusTables(), (array) $blacklist);
     $select->where([new NotIn('table_name', $skipTables)]);
     $sql = new Sql($this->adapter);
     $statement = $sql->prepareStatementForSqlObject($select);
     $result = $statement->execute();
     return iterator_to_array($result);
 }
Ejemplo n.º 2
0
 /**
  * @inheritDoc
  */
 public function getTables()
 {
     $zendDb = Bootstrap::get('zendDb');
     $config = Bootstrap::get('config');
     $blacklist = [];
     if (array_key_exists('tableBlacklist', $config)) {
         $blacklist = $config['tableBlacklist'];
     }
     $select = new Select();
     $select->columns(['id' => 'TABLE_NAME', 'table_name' => 'TABLE_NAME', 'date_created' => 'CREATE_TIME', 'comment' => 'TABLE_COMMENT', 'count' => 'TABLE_ROWS']);
     $select->from(['ST' => new TableIdentifier('TABLES', 'INFORMATION_SCHEMA')]);
     $select->join(['DT' => 'directus_tables'], 'DT.table_name = ST.TABLE_NAME', ['hidden' => new Expression('IFNULL(hidden, 0)'), 'single' => new Expression('IFNULL(single, 0)'), 'user_create_column', 'user_update_column', 'date_create_column', 'date_update_column', 'footer', 'list_view', 'column_groupings', 'filter_column_blacklist', 'primary_column'], $select::JOIN_LEFT);
     $ignoredTables = SchemaManager::getDirectusTables(DirectusPreferencesTableGateway::$IGNORED_TABLES);
     $select->where(['ST.TABLE_SCHEMA' => $zendDb->getCurrentSchema(), 'ST.TABLE_TYPE' => 'BASE TABLE', new NotIn('ST.TABLE_NAME', array_merge($ignoredTables, (array) $blacklist))]);
     $sql = new Sql($zendDb);
     $statement = $sql->prepareStatementForSqlObject($select);
     $result = $statement->execute();
     return iterator_to_array($result);
 }
Ejemplo n.º 3
0
 /**
  * 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];
 }
Ejemplo n.º 4
0
 public function testDirectusTables()
 {
     $this->assertTrue(Schema::isDirectusTable('directus_files'));
     $this->assertFalse(Schema::isDirectusTable('directus_storage'));
 }
Ejemplo n.º 5
0
 private static function formatColumnRow($row)
 {
     $columnName = $row['column_name'];
     foreach ($row as $key => $value) {
         if (is_null($value)) {
             unset($row[$key]);
         }
     }
     unset($row['table_name']);
     $row['id'] = $columnName;
     $row['options'] = [];
     // Many-to-Many type it actually can be null,
     // it's based on a junction table, not a real column.
     // Issue #612 https://github.com/RNGR/directus6/issues/612
     if (array_key_exists('type', $row) && $row['type'] == 'ALIAS') {
         $row['is_nullable'] = 'YES';
     }
     $hasDefaultValue = isset($row['default_value']);
     $anAlias = static::isColumnTypeAnAlias($row['type']);
     if ($row['is_nullable'] === 'NO' && !$hasDefaultValue && !$anAlias) {
         $row['required'] = true;
     }
     // Basic type casting. Should eventually be done with the schema
     if ($hasDefaultValue) {
         $row['default_value'] = SchemaManager::parseType($row['default_value'], $row['type']);
     }
     $row['required'] = (bool) $row['required'];
     $row['system'] = (bool) static::isSystemColumn($row['id']);
     $row['hidden_list'] = (bool) $row['hidden_list'];
     $row['hidden_input'] = (bool) $row['hidden_input'];
     //$row['is_writable'] = !in_array($row['id'], $writeFieldBlacklist);
     if (array_key_exists('sort', $row)) {
         $row['sort'] = (int) $row['sort'];
     }
     // Default UI types.
     if (!isset($row['ui'])) {
         $row['ui'] = self::columnTypeToUIType($row['type']);
     }
     // Defualts as system columns
     if (static::isSystemColumn($row['id'])) {
         $row['system'] = true;
         $row['hidden'] = true;
     }
     if (array_key_exists('related_table', $row)) {
         $row['relationship'] = [];
         $row['relationship']['type'] = ArrayUtils::get($row, 'relationship_type');
         $row['relationship']['related_table'] = $row['related_table'];
         unset($row['relationship_type']);
         unset($row['related_table']);
         if (array_key_exists('junction_key_left', $row)) {
             $row['relationship']['junction_key_left'] = $row['junction_key_left'];
             unset($row['junction_key_left']);
         }
         if (array_key_exists('junction_key_right', $row)) {
             $row['relationship']['junction_key_right'] = $row['junction_key_right'];
             unset($row['junction_key_right']);
         }
         if (array_key_exists('junction_table', $row)) {
             $row['relationship']['junction_table'] = $row['junction_table'];
             unset($row['junction_table']);
         }
     }
     return $row;
 }
Ejemplo n.º 6
0
            $outputData = $get_new;
    }
    JsonView::render($outputData);
})->via('GET', 'POST');
$app->get("/{$v}/groups/:id/?", function ($id = null) use($ZendDb, $acl) {
    // @TODO need POST and PUT
    // Hardcoding ID temporarily
    is_null($id) ? $id = 1 : null;
    $tableName = 'directus_groups';
    $Groups = new TableGateway($acl, $tableName, $ZendDb);
    $response = $Groups->find($id);
    if (!$response) {
        $response = ['message' => __t('unable_to_find_group_with_id_x', ['id' => $id]), 'success' => false];
    }
    $columns = TableSchema::getAllNonAliasTableColumns($tableName);
    $response = SchemaManager::parseRecordValuesByType($response, $columns);
    JsonView::render($response);
});
/**
 * FILES COLLECTION
 */
$app->map("/{$v}/files(/:id)/?", function ($id = null) use($app, $ZendDb, $acl, $params, $requestPayload) {
    if (!is_null($id)) {
        $params['id'] = $id;
    }
    $table = 'directus_files';
    $currentUser = Auth::getUserInfo();
    $TableGateway = new TableGateway($acl, $table, $ZendDb);
    $activityLoggingEnabled = !(isset($_GET['skip_activity_log']) && 1 == $_GET['skip_activity_log']);
    $activityMode = $activityLoggingEnabled ? TableGateway::ACTIVITY_ENTRY_MODE_PARENT : TableGateway::ACTIVITY_ENTRY_MODE_DISABLED;
    switch ($app->request()->getMethod()) {
Ejemplo n.º 7
0
 protected function parseRecordValuesByType(array $records, $tableName = null)
 {
     $tableName = $tableName === null ? $this->table : $tableName;
     $columns = TableSchema::getAllNonAliasTableColumns($tableName);
     return SchemaManager::parseRecordValuesByType($records, $columns);
 }
 public function fetchAllByUser($user_id, $assoc = false)
 {
     $select = new Select($this->table);
     $select->columns(['id', 'user', 'table_name', 'columns_visible', 'sort', 'sort_order', 'status', 'title', 'search_string']);
     $select->where->equalTo('user', $user_id)->isNull('title');
     $coreTables = SchemaManager::getDirectusTables(static::$IGNORED_TABLES);
     $select->where->addPredicate(new NotIn('table_name', $coreTables));
     $metadata = new \Zend\Db\Metadata\Metadata($this->getAdapter());
     $tables = $metadata->getTableNames();
     $tables = array_diff($tables, $coreTables);
     $rows = $this->selectWith($select)->toArray();
     $preferences = [];
     $tablePrefs = [];
     foreach ($rows as $row) {
         $tablePrefs[$row['table_name']] = $row;
     }
     //Get Default Preferences
     foreach ($tables as $key => $table) {
         // Honor ACL. Skip the tables that the user doesn't have access too
         if (!TableSchema::canGroupViewTable($table)) {
             continue;
         }
         $tableName = $table;
         if (!isset($tablePrefs[$table])) {
             $table = null;
         } else {
             $table = $tablePrefs[$table];
         }
         if (!isset($table['user'])) {
             $table = null;
         }
         $table = $this->constructPreferences($user_id, $tableName, $table);
         $preferences[$tableName] = $table;
     }
     return $preferences;
 }