public function sortItems($params) { ModuleSortorderQuery::create()->filterByPageRevision($this->pageRevision)->delete(); $wrappers = $this->pageRevision->getPageModuleWrappers(); $unsortedIds = ArrayHelper::objectsToArray($wrappers, false, 'getPageModuleId'); $wrapperById = ArrayHelper::objectsToArray($wrappers, 'getPageModuleId'); // Get primary keys $items = $_POST['item']; if (!is_array($items)) { throw new Exception('Expected array POST variable `item`.'); } $sortedIds = array(); foreach ($items as $item) { $pk = json_decode($item, true); if ($pk === null) { throw new Exception('Invalid primary key for item: ' . $item); } if (!array_key_exists($pk, $wrapperById)) { throw new Exception('Module not found when sorting'); } $sortedIds[] = $pk; } if ($sortedIds !== $unsortedIds) { foreach ($wrappers as $wrapper) { $sortorder = $wrapper->getSortorder(true); $sortorder->insertAtBottom(); $sortorder->save(); } $pks = array(); foreach ($sortedIds as $id) { $pks[] = array($id, $this->pageRevision->getPageRevisionId()); } Propel::sortableReorder($pks, 'ModuleSortorder'); } $this->pageRevision->setUpdatedAt(time()); $this->pageRevision->save(); }
public function sortItems($params) { // Get primary keys $items = $_POST['item']; if (!is_array($items)) { throw new \Exception('Expected array POST variable `item`.'); } $pks = array(); foreach ($items as $item) { $pk = json_decode($item, true); if ($pk === null) { throw new \Exception('Invalid primary key for item: ' . $item); } $pks[] = $pk; } Propel::sortableReorder($pks, $this->getModelClass()); }