implement column edit api, except sort

This commit is contained in:
2018-08-10 12:41:08 +02:00
parent e3a8616c68
commit 1fbd3384ce
5 changed files with 150 additions and 14 deletions
+109
View File
@@ -276,6 +276,41 @@ class Changeset
return $cachedColumns = $newOrder;
}
/**
* @param string $id
* @return Column
*/
public function fetchColumn(string $id)
{
if ($this->isNewColumn($id)) {
$c = new Column($this->newColumns[$id]);
$c->markAsNew();
return $c;
} else {
$columns = collect($this->revision->columns)->keyBy('id');
return new Column($columns[$id]);
}
}
/**
* @param string $id
* @return Column
*/
public function fetchAndTransformColumn(string $id)
{
$column = $this->fetchColumn($id);
if (isset($this->columnUpdates[$column->id])) {
$column->modifyByChangeset($this->columnUpdates[$column->id]);
}
if (in_array($column->id, $this->removedColumns)) {
$column->markForRemoval();
}
return $column;
}
public function rowRemove(int $id)
{
if ($this->isNewRow($id)) {
@@ -296,6 +331,11 @@ class Changeset
return isset($this->newRows[$id]);
}
public function isNewColumn(string $id)
{
return isset($this->newColumns[$id]);
}
public function fetchAndTransformRow(int $id)
{
$r = $this->fetchRow($id);
@@ -337,6 +377,7 @@ class Changeset
* if all differences were undone.
*
* @param array|object $newVals - values of the new row
* @return object - updated column
*/
public function rowUpdate($newVals)
{
@@ -373,6 +414,56 @@ class Changeset
unset($this->rowUpdates[$_id]);
}
}
return $this->fetchAndTransformRow($_id);
}
/**
* @param $newVals
* @return Column
*/
public function columnUpdate($newVals)
{
$id = $newVals->id;
$col = $this->fetchColumn($id);
$updateObj = [];
foreach ($newVals as $field => $value) {
if (starts_with($field, '_')) continue; // internals
if ($value !== $col->$field) {
$updateObj[$field] = $value;
}
}
if ($this->isNewColumn($id)) {
$this->newColumns[$id] = array_merge($this->newColumns[$id], $updateObj);
}
else {
if (!empty($updateObj)) {
$this->columnUpdates[$id] = $updateObj;
} else {
// remove possible old update record for this row, if nothing changes now
unset($this->columnUpdates[$id]);
}
}
return $this->fetchAndTransformColumn($id);
}
public function columnRemove(string $id)
{
if ($this->isNewColumn($id)) {
unset($this->newColumns[$id]);
}
else {
$this->removedColumns[] = $id;
}
}
public function columnRestore(string $id)
{
$this->removedColumns = array_diff($this->removedColumns, [$id]);
}
/**
@@ -464,4 +555,22 @@ class Changeset
$this->newRows = array_merge($this->newRows, $rows->all());
}
public function addBlankCol()
{
$cid = (new ColumnNumerator(1))->next();
$allCols = $this->fetchAndTransformColumns();
$num = count($allCols) + 1;
$col = [
'name' => "col_{$num}",
'type' => "string",
'title' => "Column {$num}",
'id' => $cid,
];
$this->newColumns[$cid] = $col;
return $col;
}
}
+15 -6
View File
@@ -71,7 +71,7 @@ class Column implements JsonSerializable, Arrayable
foreach ((array)$columnObject as $key => $value) {
if ($value != $this->$key) {
$this->modified_attribs[] = $key;
$this->orig_attribs[] = $this->$key;
$this->orig_attribs[$key] = $this->$key;
$this->$key = $value;
}
}
@@ -123,16 +123,21 @@ class Column implements JsonSerializable, Arrayable
/**
* Create from object or array
*
* @param $obj
* @param object|array $obj
*/
public function __construct($obj)
public function __construct($obj, $allowEmptyName=false)
{
$b = objBag($obj);
if (!$b->has('name') || $b->name == '') throw new NotApplicableException('Missing name in column');
if (!$b->has('type')) throw new NotApplicableException('Missing type in column');
if (!$allowEmptyName && (!$b->has('name') || $b->name == '')) {
throw new NotApplicableException('Missing name in column');
}
if ($b->name[0] == '_') { // global row ID
if (!$b->has('type')) {
throw new NotApplicableException('Missing type in column');
}
if ($b->name[0] == '_') { // would cause problems with selects (see: _id / GRID)
throw new NotApplicableException("Column name can't start with underscore.");
}
@@ -156,6 +161,10 @@ class Column implements JsonSerializable, Arrayable
'name' => $this->name,
'title' => $this->title,
'type' => $this->type,
'_new' => $this->isNew,
'_remove' => $this->toRemove,
'_changed' => $this->modified_attribs,
'_orig' => $this->orig_attribs,
];
}