public function configure() { parent::configure(); $this->widgetSchema['additional_info'] = new sfWidgetFormTextarea(); $this->useFields(array('name', 'additional_info')); }
public function executeUpdate(sfWebRequest $request) { $this->forward404Unless($request->isXmlHttpRequest()); $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT)); $this->forward404Unless($alternative = Doctrine_Core::getTable($this->model)->find(array($request->getParameter('id'))), sprintf('Object decision does not exist (%s).', $request->getParameter('id'))); $form = new AlternativeForm($alternative, array('user' => $this->getUser())); $alternative->setUpdatedBy(Alternative::generateUpdateAndCreatedBy($this->getUser()->getGuardUser())); $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName())); if ($form->isValid()) { $form->save(); // Create log $log = new Log(); $log->injectDataAndPersist($alternative, $this->getUser()->getGuardUser(), array('action' => 'edit')); // Process tags $tags_request = json_decode($request->getParameter('tags')); $tags = array(); foreach ($alternative->getTagAlternative() as $tag) { $tags[] = $tag->Tag->name; } foreach (array_diff($tags_request, $tags) as $result) { Tag::newTag($this->getUser()->getGuardUser(), $request->getParameter('id'), $result, 'alternative'); } foreach (array_diff($tags, $tags_request) as $result) { Tag::removeTag($this->getUser()->getGuardUser(), $request->getParameter('id'), $result, 'alternative'); } // Process links $links = array(); foreach ($alternative->getAlternativeLink() as $link) { $links[$link->id] = $link->link; } $links_request_new = array(); $links_request_old = array(); $links_request = json_decode($request->getParameter('links'), true); foreach ($links_request as $link_request) { if (is_array($link_request) && array_key_exists('id', $link_request)) { $links_request_old[$link_request['id']] = $link_request['link']; } else { $links_request_new[] = $link_request; } } foreach ($links_request_new as $link_request_new) { $link = new AlternativeLink(); $link->link = $link_request_new['link']; $link->alternative_id = $request->getParameter('id'); $link->save(); } foreach (array_diff(array_keys($links), array_keys($links_request_old)) as $result) { AlternativeLinkTable::getInstance()->find($result)->delete(); } foreach ($links_request_old as $key => $link) { if ($links_request_old[$key] !== $links[$key]) { $alternative_link = AlternativeLinkTable::getInstance()->find($key); $alternative_link->link = $links_request_old[$key]; $alternative_link->save(); } } // Process related alternatives $related_alternatives_request = json_decode($request->getParameter('related_alternatives'), true); if (!$related_alternatives_request) { $related_alternatives_request = array(); } $related_alternatives = array(); foreach ($alternative->getAlternativeRelation() as $related_alternative) { $related_alternatives[] = $related_alternative->to_id; } foreach (array_diff($related_alternatives_request, $related_alternatives) as $result) { if (AlternativeTable::getInstance()->getOneForUser($this->getUser()->getGuardUser(), $result)) { $alternative_relation = new AlternativeRelation(); $alternative_relation->setFromId($alternative->getId()); $alternative_relation->setToId($result); $alternative_relation->setCreatedBy(Alternative::generateUpdateAndCreatedBy($this->getUser()->getGuardUser())); $alternative_relation->save(); } } foreach (array_diff($related_alternatives, $related_alternatives_request) as $result) { AlternativeRelationTable::getInstance()->findByFromIdAndToId($alternative->getId(), $result)->delete(); } $alternative->refresh(true); return $this->renderText(json_encode($alternative->getRowData())); } else { return $this->renderPartial('form', array('form' => $form)); } }