public function addAction() { $formCategory = new CategoryForm(); // On récupère l'objet Request $request = $this->getRequest(); // On vérifie si le formulaire a été posté if ($request->isPost()) { // On instancie notre modèle Category $category = new Category(); // Et on passe l'InputFilter de Category au formulaire $formCategory->setInputFilter($category->getInputFilter()); $formCategory->setData($request->getPost()); // Si le formulaire est valide if ($formCategory->isValid()) { // On prend les données du formulaire qui sont converti pour correspondre à notre modèle Category $category->exchangeArray($formCategory->getData()); // On enregistre ces données dans la table Category $this->getServiceLocator()->get('Application\\Service\\CategoryService')->saveCategory($category); $this->getServiceLocator()->get('Zend\\Log')->info("La catégorie '{$category->name}' a été ajoutée"); $this->flashMessenger()->addMessage(array('success' => "La catégorie '{$category->name}' a été ajoutée")); // Puis on redirige sur la page d'accueil. return $this->redirect()->toRoute('zfcadmin/category'); } else { // Si le formulaire n'est pas valide, on reste sur la page et les erreurs apparaissent foreach ($formCategory->getMessages() as $messageId => $message) { $this->getServiceLocator()->get('Zend\\Log')->err("Validation failure '{$messageId}': {$message}"); $this->flashMessenger()->addMessage(array('error' => "Validation failure '{$messageId}': {$message}")); } } } return new ViewModel(array('form' => $formCategory, 'flashMessages' => $this->flashMessenger()->getMessages())); }
public function load(ObjectManager $manager) { $category = new Category(); $category->setName("TestCat"); $category->setShortName("Test"); $category->setColor("#FF0000"); $category->setCompactMode(false); $category->setTimeline(true); $category->setTimelineConfirmed(false); $manager->persist($category); $manager->flush(); $this->addReference("category", $category); }
public function getArrayCopy() { $object_vars = array_merge(get_object_vars($this), parent::getArrayCopy()); return $object_vars; }
public function formAction() { $request = $this->getRequest(); $objectManager = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager'); $viewmodel = new ViewModel(); // disable layout if request by Ajax $viewmodel->setTerminal($request->isXmlHttpRequest()); $id = $this->params()->fromQuery('id', null); $category = new Category(); $builder = new AnnotationBuilder(); $form = $builder->createForm($category); $form->setHydrator(new DoctrineObject($objectManager))->setObject($category); $form->get('parent')->setValueOptions($objectManager->getRepository('Application\\Entity\\Category')->getRootsAsArray($id)); $form->get('readroles')->setValueOptions($objectManager->getRepository('Core\\Entity\\Role')->getAllAsArray()); $type = new Select('type'); $type->setValueOptions(Category::getTypeValueOptions()); $type->setLabel('Type : '); $form->add($type); if ($id) { // bind to the category $category = $objectManager->getRepository('Application\\Entity\\Category')->find($id); if ($category) { if ($category instanceof RadarCategory) { $form->get('type')->setValue('radar'); } elseif ($category instanceof AntennaCategory) { $form->get('type')->setValue('antenna'); } elseif ($category instanceof FrequencyCategory) { $form->get('type')->setValue('frequency'); } elseif ($category instanceof BrouillageCategory) { $form->get('type')->setValue('brouillage'); } elseif ($category instanceof MilCategory) { $form->get('type')->setValue('mil'); } $form->get('type')->setAttribute('disabled', true); // select parent if ($category->getParent()) { $form->get('parent')->setAttribute('value', $category->getParent()->getId()); } // fill title fields available $customfields = array(); foreach ($category->getCustomfields() as $field) { $customfields[$field->getId()] = $field->getName(); } $form->get('fieldname')->setValueOptions($customfields); $form->bind($category); $form->setData($category->getArrayCopy()); } } $form->add(array('name' => 'submit', 'attributes' => array('type' => 'submit', 'value' => 'Enregistrer', 'class' => 'btn btn-primary'))); $viewmodel->setVariables(array('form' => $form, 'system' => $category ? $category->isSystem() : false)); return $viewmodel; }