protected function prepareAddPostData($categoryID) { $form = new ListingForm($this->entityManager); $postParams = ['sort' => 0, 'category' => $categoryID, 'listing_csrf' => $form->get('listing_csrf')->getValue()]; $allLangs = $this->entityManager->getRepository(get_class(new Lang()))->findAll(); foreach ($allLangs as $lang) { $title = '<exotico>"Title in ' . $lang->getIsoCode(); $postParams['content'][] = ['link' => '<exotico>"Link in ' . $lang->getIsoCode(), 'title' => $title, 'alias' => Strings::alias($title), 'text' => 'Text in ' . $lang->getIsoCode()]; } return $postParams; }
public function create($data) { $langs = $this->getServiceLocator()->get('entity-manager')->getRepository(get_class(new Lang()))->countLanguages(); if (!$langs) { return new JsonModel(['message' => ['type' => 'error', 'text' => $this->translator->translate('You must insert at least one language in order to add categories')], 'parent' => $data['parent']]); } $category = new Category(); $this->setParents($category, $data['parent']); $this->prepareFormAndLanguage($category, $form, true); foreach ($data['content'] as &$content) { $content['alias'] = Strings::alias($content['title']); } $form->setData($data); if ($form->isValid()) { $entityManager = $this->getServiceLocator()->get('entity-manager'); $entityManager->persist($category); $entityManager->flush(); $this->getResponse()->setStatusCode(201); return new JsonModel(['message' => ['type' => 'success', 'text' => $this->translator->translate('The new category was added successfully')], 'parent' => (int) $data['parent']]); } return $this->renderCategData($category, $form, $data['parent']); }
public function handleCreateUpdate($data, $id = null) { $action = !$id ? 'add' : 'edit'; $this->dependencyProvider($entityManager, $listingEntity, $categoryTree, $listingRepository); if (!$id) { //check if there is at least one category available $categoryEntity = new Entity\Category(); $categoryNumber = $entityManager->getRepository(get_class($categoryEntity))->countAll(); if (!$categoryNumber) { return $this->redirToList('You must create at least one category in order to add pages', 'error'); } } if ($action == 'edit') { $listing = $listingRepository->findOneBy(['id' => $id]); if (!$listing) { return $this->redirWrongParameter(); } } else { $listing = new Entity\Listing(); } if (!empty($data['content'])) { foreach ($data['content'] as &$content) { if (empty($content['alias'])) { $content['alias'] = Strings::alias($content['title']); } else { $content['alias'] = Strings::alias($content['alias']); } } } $imgDir = $this->getServiceLocator()->get('config')['listing']['img-core-dir']; $languagesService = $this->getServiceLocator()->get('language'); $languages = $languagesService->getActiveLanguages(); //add empty language content to the collection, so that input fields are created $this->addEmptyContent($listing); $listingContent = $action == 'edit' ? $listing->getContent() : null; $form = new ListingForm($this->getServiceLocator()->get('entity-manager'), $listingContent); $form->bind($listing); $form->get('category')->setValueOptions($categoryTree->getSelectOptions()); $returnError = function ($message) use($form, $listing, $action, $languages) { $message = ['type' => 'error', 'text' => $message, 'no_redir' => 1]; return $this->renderData($form, $listing, $action, $languages, $message); }; $hasImage = !empty($data['listing_image']['base64']) && !empty($data['listing_image']['name']); //validate image if ($hasImage) { $messages = []; $result = $form->validateBase64Image($data['listing_image']['name'], $data['listing_image']['base64'], $messages); if (!$result) { return $returnError($this->translator->translate(implode('<br />', $messages))); } //v_todo - translate } $form->setData($data); if ($form->isValid()) { $category = $entityManager->find(get_class($this->getServiceLocator()->get('category-entity')), ['id' => $form->getInputFilter()->getValue('category')]); $listing->setOnlyCategory($category); $entityManager->persist($listing); //is the image scheduled for removal if ($action == 'edit') { if (!empty($data['image_remove']) && $listing->getListingImage()) { $this->removeListingImage($listing->getListingImage(), $imgDir, $listing->getId()); } } //upload new image $upload = false; if ($hasImage) { if ($action == 'edit') { if (empty($data['image_remove']) && $listing->getListingImage()) { $this->removeListingImage($listing->getListingImage(), $imgDir, $listing->getId()); } } $listingImage = new ListingImage($listing); $listingImage->setImageName($data['listing_image']['name']); $upload = true; } $entityManager->flush(); if ($upload) { $uploadDir = $imgDir . $listing->getId(); if (!file_exists($uploadDir) && !is_dir($uploadDir)) { mkdir($uploadDir); } \file_put_contents($uploadDir . '/' . $data['listing_image']['name'], base64_decode($data['listing_image']['base64'])); } if ($this->getRequest()->isPost()) { $this->getResponse()->setStatusCode(201); } return new JsonModel(['message' => ['type' => 'success', 'text' => $this->translator->translate(sprintf($this->translator->translate('The page has been %s successfully'), $this->translator->translate($action . 'ed')))]]); } return $returnError($this->translator->translate('Please check the form for errors')); }