/** * Persist changes to a navigation item * @return \Zend\View\Model\JsonModel */ public function saveAction() { $locator = $this->getServiceLocator(); $modelPage = $locator->get('DotsPages\\Db\\Model\\Page'); $modelBlock = $locator->get('DotsBlock\\Db\\Model\\Block'); $modelNavBlock = $locator->get('DotsNavBlock\\Db\\Model\\NavigationBlock'); $POST = $this->getRequest()->getPost()->toArray(); $form = $this->getEditItemForm(); $form->setData($POST); if ($form->isValid()) { $data = $form->getInputFilter()->getValues(); $data = $data['navigation']; // $data = $form->getSubForm('navigation')->getValues(true); $page = $modelPage->getByAlias($POST['alias']); if (array_key_exists('block_id', $POST) && $POST['block_id']) { $block = $modelBlock->getById($POST['block_id']); } else { $block = new Block(); $block->page_id = $page->id; $block->section = $POST['section']; $block->type = self::TYPE; $block->position = $POST['position']; $block->save(); } if (array_key_exists('id', $data) && $data['id']) { $navBlock = $modelNavBlock->getById($data['id']); } else { $navBlock = new NavigationBlock(); $navBlock->block_id = $block->id; } if ($navBlock->type == 'file' && $data['type'] != 'file' && $navBlock->href) { unset($navBlock->href); } $navBlock->type = $data['type']; $navBlock->title = $data['title']; $navBlock->position = $data['position']; switch ($navBlock->type) { case 'link': $navBlock->href = $data['link']; break; case 'page': $selectedPage = $modelPage->getById($data['entity_id']); $navBlock->entity_id = $data['entity_id']; $navBlock->href = $selectedPage->alias; break; } if (!is_numeric($data['position'])) { $navBlock->position = 1; } $navBlock->save(); return $this->jsonResponse(array('success' => true, 'data' => $navBlock->toArray())); } return $this->jsonResponse(array('success' => false, 'errors' => $form->getMessages())); }
public function saveAction() { $locator = $this->getServiceLocator(); $appConfig = $locator->get('ApplicationConfig'); $modelPage = $locator->get('DotsPages\\Db\\Model\\Page'); $modelBlock = $locator->get('DotsBlock\\Db\\Model\\Block'); $modelLinkBlock = $locator->get('DotsLinkBlock\\Db\\Model\\LinkBlock'); $POST = $this->getRequest()->getPost()->toArray(); $FILES = $this->getRequest()->getFiles()->toArray(); $form = new LinkContentForm(); if (!empty($FILES)) { $POST['file'] = $FILES['file']; } $form->setData($POST); if ($form->isValid()) { $data = $form->getInputFilter()->getValues(); $page = $modelPage->getByAlias($POST['alias']); if ($POST['block_id']) { $block = $modelBlock->getById($POST['block_id']); } else { $block = new Block(); $block->page_id = $page->id; $block->section = $POST['section']; $block->type = self::TYPE; $block->position = $POST['position']; $block->save(); } if ($data['id']) { $linkBlock = $modelLinkBlock->getById($data['id']); } else { $linkBlock = new LinkBlock(); $linkBlock->block_id = $block->id; } if ($linkBlock->type == 'file' && $data['type'] != 'file' && $linkBlock->href) { unset($linkBlock->href); } $linkBlock->type = $data['type']; $linkBlock->title = $data['title']; $linkBlock->position = $data['position']; switch ($linkBlock->type) { case 'link': $linkBlock->href = $data['link']; break; case 'file': $upload = new Upload(array('path' => 'data/uploads/', 'destination' => $appConfig['public_path'] . '/')); $path = $upload->process(array('file' => $POST['file'])); $data['file'] = $path['file']; if ($data['file']) { if ($linkBlock->href) { unset($linkBlock->href); } $linkBlock->href = $data['file']; } break; case 'page': $selectedPage = $modelPage->getById($data['entity_id']); $linkBlock->entity_id = $data['entity_id']; $linkBlock->href = $selectedPage->alias; break; } if (!is_numeric($data['position'])) { $linkBlock->position = 1; } $linkBlock->save(); return $this->jsonResponse(array('success' => true, 'data' => $linkBlock->toArray())); } return $this->jsonResponse(array('success' => false, 'errors' => $form->getMessages())); }