/** * * @return array */ public function updateAction() { if ($this->getRequest()->isPost()) { $params = $this->getRequest()->getParams(); $countries = \Application\Query\CountryQuery::create()->find()->toCombo(); $portCustom = PortCustomQuery::create()->findByPKOrThrow($params["id_port_custom"], $this->i18n->_("It does not exist the PortCustom with id {$params["id_port_custom"]}")); try { $this->getCatalog("PortCustomCatalog")->beginTransaction(); PortCustomFactory::populate($portCustom, $params); if ($params["air"] != PortCustom::$Air["Active"]) { $portCustom->setAir(PortCustom::$Air["Inactive"]); } if ($params["sea"] != PortCustom::$Sea["Active"]) { $portCustom->setSea(PortCustom::$Sea["Inactive"]); } if ($params["ground"] != PortCustom::$Ground["Active"]) { $portCustom->setGround(PortCustom::$Ground["Inactive"]); } // Set if is a departure port or a destination port corresponding to port type if ($params["is"] == 1) { $portCustom->setIsDeparture(1)->setIsDestination(2); } else { if ($params["is"] == 2) { $portCustom->setIsDeparture(2)->setIsDestination(1); } else { if ($params["is"] == 3) { $portCustom->setIsDeparture(1)->setIsDestination(1); } } } //die("<pre>".print_r($portCustom, true)."</pre>"); $this->getCatalog("PortCustomCatalog")->update($portCustom); //Foreach to process courtyards foreach ($params["courtyards"] as $courtyardParams) { // Assign the country name for any case (insert or update) $courtyardParams["country"] = $countries[$portCustom->getCode()]; if ($courtyardParams["id_forwarder"] == "null") { $courtyardParams["id_forwarder"] = null; } if ($courtyardParams["id_contact"] == "null") { $courtyardParams["id_contact"] = null; } // If courtyard have id then update courtyard, if it have id then create courtyard if ($courtyardParams["id_courtyard"] > 0) { // Find database courtyard $courtyard = CourtyardQuery::create()->findByPKOrThrow($courtyardParams["id_courtyard"], $this->i18n->_("It does not exist the Courtyard with id {$courtyardParams["id_courtyard"]}")); CourtyardFactory::populate($courtyard, $courtyardParams); $this->getCatalog("CourtyardCatalog")->update($courtyard); } else { // Add extra data required to courtyard params $courtyardParams["id_port_custom"] = $portCustom->getIdPortCustom(); $courtyardParams["status"] = Courtyard::$Status["Active"]; $courtyard = CourtyardFactory::createFromArray($courtyardParams); // save the record and create a link between Courtyard and Forwarder $this->getCatalog("CourtyardCatalog")->create($courtyard); } } $this->getCatalog("PortCustomCatalog")->commit(); $this->setFlash('ok', $this->i18n->_("Se actualizo correctamente el PortCustom")); } catch (Exception $e) { $this->getCatalog("PortCustomCatalog")->rollBack(); $this->setFlash('error', $this->i18n->_($e->getMessage())); } } $this->_redirect('port-custom/list'); }
public function updateAction() { if ($this->getRequest()->isPost()) { $params = $this->getRequest()->getParams(); $params["contacts"] = $this->getRequest()->getParam("contacts", array()); $id = $this->getRequest()->getParam('id'); //$idCourtyard = $this->getRequest()->getParam('id_courtyard'); $courtyardParams = $this->getRequest()->getParam('courtyards'); //die(print_r($courtyardParams)); $forwarder = ForwarderQuery::create()->findByPKOrThrow($id, $this->i18n->_("It does not exist the Forwarder with id {$id}")); //$courtyard = CourtyardQuery::create()->findOneOrThrow($idCourtyard, $this->i18n->_("It does not exist the Courtyard with id {$idCourtyard}")); try { $this->getForwarderCatalog()->beginTransaction(); ForwarderFactory::populate($forwarder, $params); $this->getForwarderCatalog()->update($forwarder); //$this->getForwarderCatalog()->unlinkAllPortCustom($forwarder->getIdForwarder()); foreach ($params['contacts'] as $idContact) { $contact = ContactQuery::create()->findByPK($idContact); $contact->setIdCompany($forwarder->getIdCompany()); $this->getContactCatalog()->update($contact); } foreach ($courtyardParams as $i => $courtyardParam) { $idCourtyard = $courtyardParam['id_courtyard']; $idAddress = $courtyardParam['id_address']; if ($idCourtyard > 0) { $courtyard = CourtyardQuery::create()->findByPKOrThrow($idCourtyard, $this->i18n->_("It does not exist the Courtyard with id {$idCourtyard}")); CourtyardFactory::populate($courtyard, $courtyardParam); $this->getCourtyardCatalog()->update($courtyard); $this->getForwarderCatalog()->linkToPortCustom($forwarder->getIdForwarder(), $courtyard->getIdPortCustom()); $address = AddressQuery::create()->findByPKOrThrow($idAddress, $this->i18n->_("It does not exist the address with id {$idAddress}")); AddressFactory::populate($address, $courtyardParam); $this->getAddressCatalog()->update($address); } else { $newCourtyard = $courtyardParam; $courtyard = CourtyardFactory::createFromArray($courtyardParam); $courtyard->setIdForwarder($forwarder->getIdForwarder()); $this->getCourtyardCatalog()->create($courtyard); $this->getForwarderCatalog()->linkToPortCustom($forwarder->getIdForwarder(), $courtyard->getIdPortCustom()); $arrayPhones = $newContacts['id_phone_number']; $address = AddressFactory::createFromArray($newCourtyard); $this->getAddressCatalog()->create($address); //$this->getContactCatalog()->linkToAddress($contact->getIdPerson(), $address->getIdAddress(), $type); } } } catch (Exception $e) { $this->getForwarderCatalog()->rollBack(); $this->setFlash('error', $this->i18n->_($e->getMessage())); } } $this->setFlash('ok', $this->i18n->_("The Forwarder with id {$id} was updated")); $this->getForwarderCatalog()->commit(); $this->_redirect('forwarder/list'); }
/** * * @return array */ public function updateAction() { $form = $this->getForm(); if ($this->getRequest()->isPost()) { $params = $this->getRequest()->getParams(); if (!$form->isValid($params)) { $this->view->setTpl("New"); $this->view->form = $form; return; } $id = $this->getRequest()->getParam('id'); $courtyard = CourtyardQuery::create()->findByPKOrThrow($id, $this->i18n->_("It does not exist the Courtyard with id {$id}")); try { $this->getCourtyardCatalog()->beginTransaction(); CourtyardFactory::populate($courtyard, $form->getValues()); $this->getCourtyardCatalog()->update($courtyard); $this->getCourtyardCatalog()->commit(); $this->setFlash('ok', $this->i18n->_("Se actualizo correctamente el Courtyard")); } catch (Exception $e) { $this->getCourtyardCatalog()->rollBack(); $this->setFlash('error', $this->i18n->_($e->getMessage())); } } $this->_redirect('courtyard/list'); }