예제 #1
0
 public function executeContact(sfWebRequest $request)
 {
     $route_params = $this->getRoute()->getParameters();
     if (isset($route_params['type']) && $route_params['type'] == 'new') {
         // CREATE
         $target_list = $this->findTargetList();
         /* @var $target_list MailingList */
         if (!$target_list) {
             return $this->notFound();
         }
         if (!$this->getGuardUser()->isTargetListMember($target_list, true)) {
             return $this->noAccess();
         }
         $contact = new Contact();
         $contact->setMailingList($target_list);
         $route = 'target_contact_new';
         $route_params = array('id' => $target_list->getId());
     } else {
         // EDIT
         $contact = ContactTable::getInstance()->find($request->getParameter('id'));
         /* @var $contact Contact */
         if (!$contact) {
             return $this->notFound();
         }
         $target_list = $contact->getMailingList();
         if (!$this->getGuardUser()->isTargetListMember($target_list, true)) {
             return $this->noAccess();
         }
         $route = 'target_contact';
         $route_params = array('id' => $contact->getId(), 'page' => $request->getParameter('page'));
     }
     $form = new ContactForm($contact);
     if ($request->isMethod('post')) {
         $form->bind($request->getPostParameter($form->getName()));
         if ($form->isValid()) {
             $form->save();
             $target_list->state(Doctrine_Record::STATE_DIRTY);
             // to invalidate cache
             $target_list->save();
             $page = $request->getParameter('page');
             if ($page) {
                 return $this->ajax()->replaceWithComponent('#contacts', 'target', 'contacts', array('target_list' => $target_list, 'page' => $page, 'no_filter' => true))->render();
             } else {
                 return $this->ajax()->replaceWithComponent('#contacts', 'target', 'contacts', array('target_list' => $target_list, 'last_page' => true, 'no_filter' => true))->render();
             }
         } else {
             return $this->ajax()->form($form)->render();
         }
     }
     if (!$contact->isNew()) {
         return $this->ajax()->remove('#contact_form_' . $contact->getId())->afterPartial('#contact_' . $contact->getId(), 'contact', array('form' => $form, 'route' => $route, 'route_params' => $route_params))->chosen('#contact_form_' . $contact->getId() . ' select', array('allow_single_deselect' => true))->render();
     } else {
         return $this->ajax()->remove('#contact_form_')->afterPartial('#contacts tbody', 'contact', array('form' => $form, 'route' => $route, 'route_params' => $route_params))->chosen('#contact_form_ select', array('allow_single_deselect' => true))->render();
     }
 }