Skip to content

Commit

Permalink
WIP: System interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
wellingguzman committed Apr 26, 2017
1 parent fae66e6 commit 2d5dfc8
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 37 deletions.
30 changes: 24 additions & 6 deletions api/core/Directus/Database/Object/Table.php
Expand Up @@ -11,6 +11,7 @@
namespace Directus\Database\Object;

use Directus\Collection\Arrayable;
use Directus\Database\SchemaManager;
use Directus\Util\Traits\ArrayPropertyAccess;
use Directus\Util\Traits\ArrayPropertyToArray;
use Directus\Util\Traits\ArraySetter;
Expand Down Expand Up @@ -76,6 +77,16 @@ class Table implements \ArrayAccess, Arrayable, \JsonSerializable
*/
protected $single = false;

/**
* @var string
*/
protected $sortColumn = null;

/**
* @var string
*/
protected $statusColumn = null;

/**
* The table record default status
*
Expand Down Expand Up @@ -300,6 +311,10 @@ public function setColumns(array $columns)
// to always set the primary column to the first primary key column
if (!$this->getPrimaryColumn() && $column->isPrimary()) {
$this->setPrimaryColumn($column->getName());
} else if (!$this->getSortColumn() && $column->getUI() === 'sort') {
$this->setSortColumn($column->getName());
} else if (!$this->getStatusColumn() && $column->getUI() === 'status') {
$this->setStatusColumn($column->getName());
}

$this->columns[] = $column;
Expand Down Expand Up @@ -335,7 +350,10 @@ public function getColumns(array $names = [])
*/
public function getColumn($name)
{
return array_shift($this->getColumns([$name]));
$columns = $this->getColumns([$name]);

// get the first matched result
return array_shift($columns);
}

/**
Expand Down Expand Up @@ -481,7 +499,7 @@ public function getFirstNonSystemColumn()
*/
public function hasStatusColumn()
{
return $this->hasColumn(STATUS_COLUMN_NAME);
return $this->getStatusColumn() ? true : false;
}

/**
Expand Down Expand Up @@ -543,7 +561,7 @@ public function getPrimaryColumn()
*/
public function setStatusColumn($name)
{
$this->status_column = $name;
$this->statusColumn = $name;

return $this;
}
Expand All @@ -555,7 +573,7 @@ public function setStatusColumn($name)
*/
public function getStatusColumn()
{
return $this->status_column;
return $this->statusColumn;
}

/**
Expand All @@ -567,7 +585,7 @@ public function getStatusColumn()
*/
public function setSortColumn($name)
{
$this->sort_column = $name;
$this->sortColumn = $name;

return $this;
}
Expand All @@ -579,7 +597,7 @@ public function setSortColumn($name)
*/
public function getSortColumn()
{
return $this->sort_column;
return $this->sortColumn;
}

/**
Expand Down
26 changes: 16 additions & 10 deletions api/core/Directus/Database/SchemaManager.php
Expand Up @@ -27,6 +27,12 @@
*/
class SchemaManager
{
// Names of the system interfaces
// former system columns
const INTERFACE_PRIMARY_KEY = 'primary_key';
const INTERFACE_STATUS = 'status';
const INTERFACE_SORT = 'sort';

/**
* Schema source instance
*
Expand Down Expand Up @@ -526,11 +532,11 @@ public function createColumnObjectFromArray($column)
$options = json_decode(ArrayUtils::get($column, 'options', ''), true);
$column['options'] = $options ? $options : [];

// $isSystemColumn = static::isSystemColumn($column['id']);
// $column['system'] = (bool) $isSystemColumn;
// if ($isSystemColumn) {
// $column['hidden'] = true;
// }
$isSystemColumn = $this->isSystemColumn($column['ui']);
$column['system'] = $isSystemColumn;
if ($isSystemColumn) {
$column['hidden_input'] = $column['hidden_list'] = true;
}

// NOTE: Alias column must are nullable
if (ArrayUtils::get($column, 'type') === 'ALIAS') {
Expand All @@ -552,17 +558,17 @@ public function createColumnObjectFromArray($column)
}

/**
* Checks whether the column name is a system column name
* Checks whether the column UI is a system column
*
* @param $columnName
* @param $columnUI
*
* @return bool
*/
public function isSystemColumn($columnName)
public function isSystemColumn($columnUI)
{
$systemFields = ['id', 'sort', STATUS_COLUMN_NAME];
$systemFields = [static::INTERFACE_PRIMARY_KEY, static::INTERFACE_SORT, static::INTERFACE_STATUS];

return in_array($columnName, $systemFields);
return in_array($columnUI, $systemFields);
}

protected function addTable($name, $schema)
Expand Down
6 changes: 3 additions & 3 deletions api/core/Directus/Database/TableSchema.php
Expand Up @@ -293,13 +293,13 @@ public static function getColumnRelationship($tableName, $columnName)
/**
* Whether or not the column name is the name of a system column.
*
* @param $columnName
* @param $interfaceName
*
* @return bool
*/
public static function isSystemColumn($columnName)
public static function isSystemColumn($interfaceName)
{
static::getSchemaManagerInstance()->isSystemColumn($columnName);
return static::getSchemaManagerInstance()->isSystemColumn($interfaceName);
}

/**
Expand Down
60 changes: 42 additions & 18 deletions api/core/Directus/Services/ColumnsService.php
Expand Up @@ -11,9 +11,12 @@
namespace Directus\Services;

use Directus\Database\Ddl\Column\Custom;
use Directus\Database\Object\Table;
use Directus\Database\SchemaManager;
use Directus\Database\TableGateway\RelationalTableGateway as TableGateway;
use Directus\Database\TableSchema;
use Directus\Database\Object\Column;
use Directus\Exception\Exception;
use Directus\Util\ArrayUtils;
use Zend\Db\Sql\Ddl\AlterTable;
use Zend\Db\Sql\Ddl\Column\Integer;
Expand All @@ -28,6 +31,7 @@
class ColumnsService extends AbstractService
{
const PRIMARY_KEY = 'primary_key';
const SORT = 'sort';

/**
* @var TableGateway
Expand Down Expand Up @@ -90,15 +94,9 @@ public function update($tableName, $columnName, $data, $patch = false)

$columnData = $columnData->toArray();

if (ArrayUtils::get($data, 'ui') === static::PRIMARY_KEY) {
$tableObject = $this->getSchemaManager()->getTableSchema($tableName);
if ($primaryColumn = $tableObject->getPrimaryColumn()) {
$this->removePrimaryKey($tableName, $primaryColumn);
$this->setDefaultInterface($tableName, $primaryColumn);
}

$this->setPrimaryColumn($tableName, $columnName);
}
// if the column is a system column
// set the current column to the default interface
$this->updateCurrentSystemInterfaces($tableName, ArrayUtils::get($data, 'ui'));

$this->ddlUpdate($tableName, $columnName, $data);
$data = ArrayUtils::pick($data, [
Expand Down Expand Up @@ -216,24 +214,20 @@ public function addColumn($table, $data)
throw new \Exception(sprintf('table [%s] not found', $table));
}

$isPrimaryInterface = ArrayUtils::get($data, 'ui') === static::PRIMARY_KEY;
if ($isPrimaryInterface && $primaryColumn = $tableObject->getPrimaryColumn()) {
$this->removePrimaryKey($table, $primaryColumn);
$this->setDefaultInterface($table, $primaryColumn);
}
// if the column is a system column
// set the current column to the default interface
$interfaceName = ArrayUtils::get($data, 'ui');
$this->updateCurrentSystemInterfaces($table, $interfaceName);

$result = $this->createColumn($table, $data);

// Primary column
if ($isPrimaryInterface) {
if ($interfaceName === SchemaManager::INTERFACE_PRIMARY_KEY) {
if (!$this->setPrimaryColumn($table, $columnName)) {
throw new \Exception('error creating the new column');
}
}

// $tableGateway = $this->createTableGateway($table);
// $tableGateway->addVirtualColumn($table, $data);

return $result;
}

Expand Down Expand Up @@ -300,4 +294,34 @@ public function setDefaultInterface($table, $column)
'column_name' => $column
]);
}

/**
* Sets the column (data) to default interface
*
* @param $table
* @param $newInterfaceName
*/
protected function updateCurrentSystemInterfaces($table, $newInterfaceName)
{
/** @var Table $tableObject */
$tableObject = $this->getSchemaManager()->getTableSchema($table);

if (!TableSchema::isSystemColumn($newInterfaceName)) {
return;
}

$callback = function ($column) use ($table, $newInterfaceName) {
if ($newInterfaceName === SchemaManager::INTERFACE_PRIMARY_KEY) {
$this->removePrimaryKey($table, $column);
}

$this->setDefaultInterface($table, $column);
};

foreach ($tableObject->getColumns() as $columnObject) {
if ($columnObject->getUI() === $newInterfaceName) {
$callback($columnObject->getName());
}
}
}
}
1 change: 1 addition & 0 deletions api/routes/A1/Table.php
Expand Up @@ -216,6 +216,7 @@ public function postColumn($table, $column)
}

$newRecord = $TableGateway->manageRecordUpdate('directus_columns', $data, TableGateway::ACTIVITY_ENTRY_MODE_DISABLED);

return $this->app->response($newRecord);
}

Expand Down

0 comments on commit 2d5dfc8

Please sign in to comment.