Пример #1
0
 public function executeMeta(sfWebRequest $request)
 {
     $route_params = $this->getRoute()->getParameters();
     if (isset($route_params['type'])) {
         // 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();
         }
         $meta = new MailingListMeta();
         $meta->setMailingList($target_list);
         switch ($route_params['type']) {
             case 'free':
                 $meta->setKind(MailingListMeta::KIND_FREE);
                 $route = 'target_meta_free';
                 break;
             case 'choice':
                 $meta->setKind(MailingListMeta::KIND_CHOICE);
                 $route = 'target_meta_choice';
                 break;
             case 'mapping':
                 $meta->setKind(MailingListMeta::KIND_MAPPING);
                 $route = 'target_meta_mapping';
                 break;
             default:
                 return $this->notFound();
         }
         $route_params = array('id' => $target_list->getId());
     } else {
         // EDIT
         $meta = MailingListMetaTable::getInstance()->find($request->getParameter('id'));
         /* @var $meta MailingListMeta */
         if (!$meta) {
             return $this->notFound();
         }
         $target_list = $meta->getMailingList();
         if (!$target_list) {
             return $this->notFound();
         }
         if (!$this->getGuardUser()->isTargetListMember($target_list, true)) {
             return $this->noAccess();
         }
         $route = 'target_meta';
         $route_params = array('id' => $meta->getId());
     }
     $form = new MailingListMetaForm($meta);
     if ($request->isMethod('post')) {
         $form->bind($request->getPostParameter($form->getName()));
         if ($form->isValid()) {
             $form->save();
             $target_list->state(Doctrine_Record::STATE_DIRTY);
             // to invalidate cacheu
             $target_list->save();
             return $this->ajax()->replaceWithPartial('#metas', 'metas', array('metas' => $target_list->getMailingListMeta()))->render();
         } else {
             return $this->ajax()->form($form)->render();
         }
     }
     if (!$meta->isNew()) {
         return $this->ajax()->remove('#meta_form_' . $meta->getId())->afterPartial('#meta_' . $meta->getId(), 'meta', array('form' => $form, 'route' => $route, 'route_params' => $route_params))->render();
     } else {
         return $this->ajax()->remove('#meta_form_')->appendPartial('#metas tbody', 'meta', array('form' => $form, 'route' => $route, 'route_params' => $route_params))->render();
     }
 }
 /**
  *
  * @param MailingList $source
  * @param Campaign $campaign
  * @param string $name
  * @return MailingList|null
  */
 public function copy(MailingList $source, Campaign $campaign = null, $name = null)
 {
     $con = $this->getConnection();
     $con->beginTransaction();
     try {
         $target = new MailingList();
         $target->setStatus(MailingListTable::STATUS_DRAFT);
         if ($name) {
             $target->setName($name);
         } else {
             $target->setName($source->getName() . ' copy ' . gmdate('Y-M-d H:i'));
         }
         if ($campaign) {
             $target->setCampaign($campaign);
         }
         $target->save();
         $meta_ids = array();
         $choice_ids = array();
         foreach ($source->getMailingListMeta() as $meta_source) {
             /* @var $meta_source MailingListMeta */
             $meta = new MailingListMeta();
             $meta->setMailingList($target);
             $meta->setKind($meta_source->getKind());
             $meta->setName($meta_source->getName());
             $meta->setSubst($meta_source->getSubst());
             $meta->save();
             $meta_ids[$meta_source->getId()] = $meta->getId();
             foreach ($meta_source->getMailingListMetaChoice() as $choice_source) {
                 /* @var $choice_source MailingListMetaChoice */
                 $choice = new MailingListMetaChoice();
                 $choice->setMailingListMeta($meta);
                 $choice->setChoice($choice_source->getChoice());
                 $choice->save();
                 $choice_ids[$choice_source->getId()] = $choice->getId();
             }
         }
         foreach ($source->getContact() as $contact_source) {
             /* @var $contact_source Contact */
             $contact = new Contact();
             $contact->setMailingList($target);
             $contact->setStatus($contact_source->getStatus());
             $contact->setEmail($contact_source->getEmail());
             $contact->setGender($contact_source->getGender());
             $contact->setFirstname($contact_source->getFirstname());
             $contact->setLastname($contact_source->getLastname());
             $contact->setCountry($contact_source->getCountry());
             $contact->save();
             foreach ($contact_source->getContactMeta() as $contact_meta_source) {
                 /* @var $contact_meta_source ContactMeta */
                 $contact_meta = new ContactMeta();
                 $contact_meta->setContact($contact);
                 $contact_meta->setValue($contact_meta_source->getValue());
                 $contact_meta->setMailingListMetaId($meta_ids[$contact_meta_source->getMailingListMetaId()]);
                 if ($contact_meta_source->getMailingListMetaChoiceId()) {
                     $contact_meta->setMailingListMetaChoiceId($choice_ids[$contact_meta_source->getMailingListMetaChoiceId()]);
                 }
                 $contact_meta->save();
             }
         }
         $con->commit();
         return $target;
     } catch (Exception $e) {
         $con->rollback();
     }
     return null;
 }