private function getModelJson(PredefinedEvent $model) { $customfieldservice = $this->getServiceLocator()->get('CustomFieldService'); $json = array('name' => $model->getName(), 'category_root' => $model->getCategory()->getParent() ? $model->getCategory()->getParent()->getName() : $model->getCategory()->getName(), 'category' => $model->getCategory()->getName(), 'duration' => $model->getDuration()); $fields = array(); foreach ($model->getCustomFieldsValues() as $value) { $fields[$value->getCustomField()->getName()] = $customfieldservice->getFormattedValue($value->getCustomField(), $value->getValue()); } $json['fields'] = $fields; return $json; }
public function saveAction() { $objectManager = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager'); $messages = array(); if ($this->getRequest()->isPost()) { $post = $this->getRequest()->getPost(); $id = $post['id']; $catid = $this->params()->fromQuery('catid', null); if (isset($post['custom_fields']) && isset($post['custom_fields']['category_id'])) { $catid = $post['custom_fields']['category_id']; } $datas = $this->getForm($id, null, $catid, $post['organisation']); $form = $datas['form']; $pevent = $datas['pevent']; $form->setData($post); $form->setPreferFormInputFilter(true); // remove required inputfilter on custom fields (default to true for select elements...) foreach ($form->getInputFilter()->getInputs() as $input) { if ($input instanceof \Zend\InputFilter\InputFilter) { foreach ($input->getInputs() as $i) { $i->setRequired(false); } } } if ($form->isValid()) { // category, may be disable if ($post['category']) { $category = $post['category']; $pevent->setCategory($objectManager->getRepository('Application\\Entity\\Category')->find($post['category'])); } else { if ($pevent->getCategory()) { $category = $pevent->getCategory()->getId(); } else { // last chance cat id passed by query $category = $catid; $pevent->setCategory($objectManager->getRepository('Application\\Entity\\Category')->find($catid)); } } if (!$id) { // if modification : link to parent and calculate position // link to parent if (isset($post['parent'])) { $pevent->setParent($objectManager->getRepository('Application\\Entity\\PredefinedEvent')->find($post['parent'])); // calculate order (order by parent) $qb = $objectManager->createQueryBuilder(); $qb->select('MAX(f.place)')->from('Application\\Entity\\PredefinedEvent', 'f')->where('f.parent = ' . $post['parent']); $result = $qb->getQuery()->getResult(); if ($result[0][1]) { $pevent->setPlace($result[0][1] + 1); } else { $pevent->setPlace(1); } } else { // no parent => model => order by category $qb = $objectManager->createQueryBuilder(); $qb->select('MAX(f.place)')->from('Application\\Entity\\PredefinedEvent', 'f')->where('f.category = ' . $category); $result = $qb->getQuery()->getResult(); if ($result[0][1]) { $pevent->setPlace($result[0][1] + 1); } else { $pevent->setPlace(1); } } } $pevent->setImpact($objectManager->getRepository('Application\\Entity\\Impact')->find($post['impact'])); // alarms if (isset($post['alarm']) && is_array($post['alarm'])) { foreach ($post['alarm'] as $key => $alarmpost) { $alarm = new PredefinedEvent(); $alarm->setCategory($objectManager->getRepository('Application\\Entity\\AlarmCategory')->findAll()[0]); $alarm->setOrganisation($pevent->getOrganisation()); $alarm->setParent($pevent); $alarm->setListable(false); $alarm->setSearchable(false); $alarm->setPunctual(true); $alarm->setImpact($objectManager->getRepository('Application\\Entity\\Impact')->find(5)); $name = new CustomFieldValue(); $name->setCustomField($alarm->getCategory()->getNamefield()); $name->setValue($alarmpost['name']); $name->setEvent($alarm); $alarm->addCustomFieldValue($name); $comment = new CustomFieldValue(); $comment->setCustomField($alarm->getCategory()->getTextfield()); $comment->setValue($alarmpost['comment']); $comment->setEvent($alarm); $alarm->addCustomFieldValue($comment); $deltabegin = new CustomFieldValue(); $deltabegin->setCustomField($alarm->getCategory()->getDeltaBeginField()); $deltabegin->setValue(preg_replace('/\\s+/', '', $alarmpost['deltabegin'])); $deltabegin->setEvent($alarm); $alarm->addCustomFieldValue($deltabegin); $deltaend = new CustomFieldValue(); $deltaend->setCustomField($alarm->getCategory()->getDeltaEndField()); $deltaend->setValue(preg_replace('/\\s+/', '', $alarmpost['deltaend'])); $deltaend->setEvent($alarm); $alarm->addCustomFieldValue($deltaend); $objectManager->persist($name); $objectManager->persist($comment); $objectManager->persist($deltabegin); $objectManager->persist($deltaend); $objectManager->persist($alarm); } } // fichiers if (isset($post['fichiers']) && is_array($post['fichiers'])) { foreach ($post['fichiers'] as $key => $value) { $file = $objectManager->getRepository("Application\\Entity\\File")->find($key); if ($file) { $file->addEvent($pevent); $objectManager->persist($file); } } } $objectManager->persist($pevent); // predefined custom field values if (isset($post['custom_fields'])) { foreach ($post['custom_fields'] as $key => $value) { $customfield = $objectManager->getRepository('Application\\Entity\\CustomField')->findOneBy(array('id' => $key)); if ($customfield) { $customfieldvalue = $objectManager->getRepository('Application\\Entity\\CustomFieldValue')->findOneBy(array('customfield' => $customfield->getId(), 'event' => $id)); if (!$customfieldvalue) { $customfieldvalue = new CustomFieldValue(); $customfieldvalue->setEvent($pevent); $customfieldvalue->setCustomField($customfield); $pevent->addCustomFieldValue($customfieldvalue); } if (is_array($value)) { // transformation des champs multiples $temp = ""; foreach ($value as $v) { $temp .= (string) $v . "\r"; } $value = trim($temp); } $customfieldvalue->setValue($value); $objectManager->persist($customfieldvalue); } } } try { $start = microtime(true); $objectManager->flush(); error_log(microtime(true) - $start); } catch (\Exception $e) { error_log($e->getMessage()); } $this->flashMessenger()->addSuccessMessage("Modèle " . $pevent->getName() . " enregistré."); $this->processFormMessages($form->getMessages()); } else { // traitement des erreurs de validation $pevent = null; $this->processFormMessages($form->getMessages(), $messages); } } $json = array(); if ($pevent) { $json['id'] = $pevent->getId(); $json['name'] = $this->getServiceLocator()->get('EventService')->getName($pevent); $json['impactstyle'] = $pevent->getImpact()->getStyle(); $json['impactname'] = $pevent->getImpact()->getName(); if ($pevent->getParent()) { $json['parentid'] = $pevent->getParent()->getId(); } } $json['messages'] = $messages; return new JsonModel($json); }
public function createFromPredefinedEvent(\Application\Entity\PredefinedEvent $predefined) { $this->setCategory($predefined->getCategory()); $this->setImpact($predefined->getImpact()); $this->setPunctual($predefined->isPunctual()); }