protected function setItemFields() { $form = $this->_form; $item = $this->_item; $em = $this->em(); $item->setName($form->get('name')); $item->setOrganization($em->find('\\ru\\nazarov\\crm\\entities\\Organization', $form->get('org'))); $item->setPosition($form->get('pos')); $item->setComment($form->get('comment')); $ids = $form->get(AddPersonAction::CONTACT_ID_KEY); $types = $form->get(AddPersonAction::CONTACT_TYPES_KEY); $vals = $form->get(AddPersonAction::CONTACT_VALUES_KEY); if ($ids == null) { foreach ($item->getContacts()->toArray() as $c) { $this->checkContactRmPossibility($c); $em->remove($c); } } else { foreach ($item->getContacts()->toArray() as $c) { if (($idx = array_search($c->getId(), $ids)) === false) { $this->checkContactRmPossibility($c); $em->remove($c); } else { $c->setType($em->find('\\ru\\nazarov\\crm\\entities\\ContactType', $types[$idx])); $c->setValue($vals[$idx]); array_splice($ids, $idx, 1); array_splice($types, $idx, 1); array_splice($vals, $idx, 1); } } foreach ($ids as $i => $id) { if (!empty($id)) { continue; } $c = new \ru\nazarov\crm\entities\Contact(); $c->setPerson($item); $c->setType($em->find('\\ru\\nazarov\\crm\\entities\\ContactType', $types[$i])); $c->setValue($vals[$i]); $em->persist($c); } } }
public function execute() { $em = $this->em(); $form = $this->prepareForm(new \ru\nazarov\crm\forms\PersonForm('person-form', 'Add person', '/?action=add_person', \ru\nazarov\crm\forms\Form::METHOD_POST)); if (!$form->isEmpty() && $form->validate()) { $p = new \ru\nazarov\crm\entities\Person(); $p->setName($form->get('name')); $p->setOrganization($em->find('\\ru\\nazarov\\crm\\entities\\Organization', $form->get('org'))); $p->setPosition($form->get('pos')); $p->setComment($form->get('comment')); $p->setLegalEntity($_SESSION['le']); $em->persist($p); $contacts = $form->get(self::CONTACT_VALUES_KEY); if ($contacts != null) { foreach ($form->get(self::CONTACT_TYPES_KEY) as $i => $type) { $c = new \ru\nazarov\crm\entities\Contact(); $c->setPerson($p); $c->setType($em->find('\\ru\\nazarov\\crm\\entities\\ContactType', $type)); $c->setValue($contacts[$i]); $em->persist($c); } } $em->flush(); $form->clean(); } $view = $this->view(); $view->set('form', $form)->set('contact_types_key', self::CONTACT_TYPES_KEY)->set('contact_ids_key', self::CONTACT_ID_KEY)->set('contact_values_key', self::CONTACT_VALUES_KEY); $form->setFieldVals('org', \ru\nazarov\sitebase\Facade::getPersonFormOrgsSelectDp()); if ($form->get('org') == null) { $form->set('org', 'undef'); } if (count($types = $form->get(self::CONTACT_TYPES_KEY)) > 0) { $view->set('contacts', array_map(function ($type, $val) { return (object) array('type' => $type, 'val' => $val); }, $types, $form->get(self::CONTACT_VALUES_KEY))); } parent::execute(); }