Skip to content

Commit

Permalink
fix(treeview): Fix moving items within same parent
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco (Valandur) committed Jun 24, 2019
1 parent a35ebaf commit b99694a
Showing 1 changed file with 66 additions and 41 deletions.
107 changes: 66 additions & 41 deletions src/TreeView.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ public function __construct($name, $title = null, $section = null, $readonly = f
}
}

public function performReadonlyTransformation() {
public function performReadonlyTransformation()
{
return new TreeView($this->name, $this->title, $this->section, $this->readonly);
}

public function setValue($value, $data = null) {
public function setValue($value, $data = null)
{
if (!$value) {
return $this;
}
Expand All @@ -84,8 +86,7 @@ public function setValue($value, $data = null) {
* this because the default behavior would write a NULL value into the relation.
*/
public function saveInto(DataObjectInterface $record)
{
}
{ }

/**
* Recursively opens an item
Expand Down Expand Up @@ -189,8 +190,10 @@ public function move($request)
return $this->FieldHolder();
}

if (!isset($data["itemId"]) || !isset($data["parents"]) ||
!isset($data["newParent"]) || !isset($data["sort"])) {
if (
!isset($data["itemId"]) || !isset($data["parents"]) ||
!isset($data["newParent"]) || !isset($data["sort"])
) {
Controller::curr()->getResponse()->setStatusCode(
400,
"Missing required data!"
Expand All @@ -200,7 +203,6 @@ public function move($request)

$itemId = intval($data["itemId"]);
$parents = array_values(array_filter(explode(",", $data["parents"]), 'strlen'));
$path = array_merge($parents, [$itemId]);

$item = PageElement::get()->byID($itemId);

Expand All @@ -211,38 +213,54 @@ public function move($request)
} else {
$newParent = $this->section;
}

// Check if this element is allowed as a child on the new element
$allowed = in_array($item->ClassName, $newParent->getAllowedPageElements());
if (!$allowed) {
Controller::curr()->getResponse()->setStatusCode(
400,
"The type " . $item->ClassName . " is not allowed as a child of " . $newParent->ClassName
);
return $gridField->FieldHolder();
return $this->FieldHolder();
}

// Remove the element from the current parent
// Get the current parent
if (count($parents) == 0) {
$this->getItems()->removeByID($itemId);
$parent = $this->section;
} else {
$parent = PageElement::get()->byID($parents[count($parents) - 1]);
$parent->Children()->removeByID($itemId);
}

// Get requested sort order
$sort = intval($data["sort"]);
$sortBy = $this->getSortField();
$sortArr = [$sortBy => $sort];

// Add the element to the new parent
if ($newParent->ClassName == PageSection::class) {
$newParent->Elements()->Add($item, $sortArr);
// Check if we moved the element within the same parent
if ($parent->ClassName === $newParent->ClassName && $parent->ID === $newParent->ID) {
// Move the element around in the current parent
if ($newParent->ClassName == PageSection::class) {
$newParent->Elements()->Add($itemId, $sortArr);
} else {
$newParent->Children()->Add($itemId, $sortArr);
}
} else {
$newParent->Children()->Add($item, $sortArr);
}
// Remove the element from the current parent
if (count($parents) == 0) {
$parent = $this->section;
$this->getItems()->removeByID($itemId);
} else {
$parent = PageElement::get()->byID($parents[count($parents) - 1]);
$parent->Children()->removeByID($itemId);
}

// Save the parent so the relation sort order is redone
$newParent->write();
// Add the element to the new parent
if ($newParent->ClassName == PageSection::class) {
$newParent->Elements()->Add($item, $sortArr);
} else {
$newParent->Children()->Add($item, $sortArr);
}
}

return $this->FieldHolder();
}
Expand Down Expand Up @@ -277,13 +295,13 @@ public function add($request)
);
return $this->FieldHolder();
}

$this->getItems()->Add($element);
} else {
// ...otherwise add a completely new item
$itemId = isset($data["itemId"]) ? intval($data["itemId"]) : null;
$type = $data["type"];

$child = $type::create();
$child->Name = "New " . $child->singular_name();

Expand All @@ -305,11 +323,11 @@ public function add($request)
);
return $this->FieldHolder();
}

$child->write();
$item->Children()->Add($child, $sortArr);
$item->write();

// Make sure we can see the child
$this->openItem(array_merge($path, [$item->ID]));
} else {
Expand Down Expand Up @@ -433,8 +451,8 @@ public function doSearch($data, $form)
$list = $list->filter("ClassName", $allowed);
$list = new PaginatedList($list, $data);
$data = $this->customise([
'SearchForm' => $form,
'Items' => $list
'SearchForm' => $form,
'Items' => $list
]);
return $data->renderWith("FLXLabs\PageSections\TreeViewFindExistingForm");
}
Expand Down Expand Up @@ -495,7 +513,8 @@ public function DetailForm(PageElement $item, bool $loadData = true)
* @param \SilverStripe\Control\HTTPRequest $request
* @return string
*/
public function detail($request) {
public function detail($request)
{
$id = intval($request->requestVar("ID"));
if ($id) {
$request->getSession()->set("ElementID", $id);
Expand Down Expand Up @@ -524,7 +543,8 @@ public function detail($request) {
return $this->DetailForm($item, false);
}

public function handleRequest(HTTPRequest $request) {
public function handleRequest(HTTPRequest $request)
{
$this->setRequest($request);

// Forward requests to the elements in the detail form to their respective controller
Expand All @@ -544,7 +564,8 @@ public function handleRequest(HTTPRequest $request) {
* @param \SilverStripe\Control\HTTPRequest $request
* @return string
*/
public function doSave($data, $form) {
public function doSave($data, $form)
{
$id = intval($data["ID"]);
$item = PageElement::get()->byID($id);

Expand Down Expand Up @@ -700,10 +721,12 @@ private function renderTree($item, $parents, $opens, $isFirst)
$tree = "[" .
implode(
',',
array_map(function ($item) {
return $item->ID;
},
$parents)
array_map(
function ($item) {
return $item->ID;
},
$parents
)
) .
"]";

Expand All @@ -715,8 +738,8 @@ private function renderTree($item, $parents, $opens, $isFirst)
}

// Construct the array of all allowed child elements in parent slot
$parentClasses = count($parents) > 0
? $parents[count($parents) - 1]->getAllowedPageElements()
$parentClasses = count($parents) > 0
? $parents[count($parents) - 1]->getAllowedPageElements()
: $this->section->getAllowedPageElements();
$parentElems = [];
foreach ($parentClasses as $class) {
Expand All @@ -733,7 +756,7 @@ private function renderTree($item, $parents, $opens, $isFirst)
if (!$this->readonly && count($classes)) {
$addButton = TreeViewFormAction::create(
$this,
"AddAction".$item->ID,
"AddAction" . $item->ID,
null,
null,
null
Expand All @@ -751,12 +774,13 @@ private function renderTree($item, $parents, $opens, $isFirst)
// and save the allowed child classes on the button
$addAfterButton = TreeViewFormAction::create(
$this,
"AddAfterAction".$item->ID,
"AddAfterAction" . $item->ID,
null,
null,
null
);
$addAfterButton->setAttribute("data-allowed-elements",
$addAfterButton->setAttribute(
"data-allowed-elements",
json_encode($parentElems, JSON_UNESCAPED_UNICODE)
);
$addAfterButton->addExtraClass("btn add-after-button font-icon-plus");
Expand All @@ -768,7 +792,7 @@ private function renderTree($item, $parents, $opens, $isFirst)
// Create a button to delete and/or remove the element from the parent
$deleteButton = TreeViewFormAction::create(
$this,
"DeleteAction".$item->ID,
"DeleteAction" . $item->ID,
null,
null,
null
Expand All @@ -783,7 +807,7 @@ private function renderTree($item, $parents, $opens, $isFirst)
// Create a button to edit the record
$editButton = TreeViewFormAction::create(
$this,
"EditAction".$item->ID,
"EditAction" . $item->ID,
null,
null,
null
Expand All @@ -801,7 +825,7 @@ private function renderTree($item, $parents, $opens, $isFirst)
// Create the tree field
$treeButton = TreeViewFormAction::create(
$this,
"TreeNavAction".$item->ID,
"TreeNavAction" . $item->ID,
null,
"dotreenav",
["element" => $item]
Expand Down Expand Up @@ -891,4 +915,5 @@ private function closeItem($path)
}
}

class TreeView_Readonly extends TreeView {}
class TreeView_Readonly extends TreeView
{ }

0 comments on commit b99694a

Please sign in to comment.