Exemplo n.º 1
0
 protected function prepareEditData($categID)
 {
     $form = new ListingForm($this->entityManager);
     return ['sort' => 1, 'category' => $categID, 'content' => [['title' => 'Some new title en', 'link' => 'New link en', 'alias' => 'new alias in en', 'text' => 'New text in en'], ['title' => 'New title in Es', 'link' => 'New link es', 'alias' => 'new alias in es', 'text' => 'New text in es']], 'listing_csrf' => $form->get('listing_csrf')->getValue()];
 }
Exemplo n.º 2
0
 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'));
 }