/** * Updates a collection of widgets * * @param array $input Raw widget data which comes from a form * @param string $webPageId * @param string $name Item's name * @return boolean */ public function update(array $input, $webPageId, $name) { if (isset($input['widget'], $input['attached'])) { $attachedIds = json_decode($input['attached']); $items = $this->normalizeInput($input['widget']); // We might have removed items $removedIds = $this->getRemovedIds($items, $attachedIds); // So if we had them, now remove them in back-end's side if (!empty($removedIds)) { foreach ($removedIds as $removedId) { // Otherwise that item was cancelled, and therefore needs to be removed $this->itemMapper->deleteById($removedId) && $this->itemMapper->deleteAllByParentId($removedId); } } // And finally it's time to insert or update a widget foreach ($items as $item) { $item = $this->prepareItem($item, $webPageId, $name); // When id is empty, it means that a new item was added if (empty($item['id'])) { $this->itemMapper->insert($item); } else { // Otherwise it was existing item $this->itemMapper->update($item); } } } else { // All menus were removed, therefore simply remove all items associated items with target web page id $this->itemMapper->deleteAllByWebPageId($webPageId); } return true; }
/** * Deletes an item by its associated id * * @param string $id Item's id * @return boolean */ public function deleteById($id) { $name = Filter::escape($this->itemMapper->fetchNameById($id)); $this->track('The "%s" item has been removed', $name); return $this->itemMapper->deleteById($id) && $this->itemMapper->deleteAllByParentId($id); }