/** * * @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; }
public function executeEdit(sfWebRequest $request) { $route_params = $this->getRoute()->getParameters(); $this->csrf_token = UtilCSRF::gen('target_activate'); if (isset($route_params['type']) && $route_params['type'] == 'new') { // CREATE $campaign = CampaignTable::getInstance()->findById($request->getParameter('id'), $this->userIsAdmin()); /* @var $campaign Campaign */ if (!$campaign) { return $this->notFound(); } if (!$this->getGuardUser()->isCampaignMember($campaign)) { return $this->noAccess(); } $target_list = new MailingList(); $target_list->setCampaign($campaign); $target_list->setStatus(MailingListTable::STATUS_DRAFT); } else { $target_list = $this->findTargetList(); /* @var $target_list MailingList */ if (!$target_list) { return $this->notFound(); } if (!$this->getGuardUser()->isTargetListMember($target_list, true)) { return $this->noAccess(); } $this->metas = $target_list->getMailingListMeta(); } if ($target_list->getCampaignId()) { $this->campaign = $target_list->getCampaign(); if (!$this->userIsAdmin() && $this->campaign->getStatus() == CampaignTable::STATUS_DELETED) { return $this->notFound(); } } $this->target_list = $target_list; $this->form = new MailingListForm($target_list); if ($request->isMethod('post')) { $form_data = $request->getPostParameter($this->form->getName()); if ($form_data) { $this->form->bind($form_data); if ($this->form->isValid()) { $was_new = $target_list->isNew(); $this->form->save(); $target_list->refresh(); if ($was_new) { if (!$this->getGuardUser()->isCampaignAdmin($target_list->getCampaign())) { $tr = new TargetListRights(); $tr->setUser($this->getGuardUser()); $tr->setMailingList($target_list); $tr->setActive(1); $tr->save(); } return $this->ajax()->redirectRotue('target_edit', array('id' => $target_list->getId()))->render(); } else { return $this->ajax()->replaceWithPartial('#form', 'form', array('form' => new MailingListForm($target_list)))->alert('Name updated', '', '#form', 'after')->render(); } } else { return $this->ajax()->form($this->form)->render(); } } } $this->includeIframeTransport(); $this->includeChosen(); }
public function processValues($values) { $values = parent::processValues($values); if (isset($values['mailing_list_id'])) { $ml_id = $values['mailing_list_id']; if ($ml_id) { if (strpos($ml_id, 'c') === 0) { $ml_id = substr($ml_id, 1); $source_target_list = MailingListTable::getInstance()->findById($ml_id, true); $copy_target_list = MailingListTable::getInstance()->copy($source_target_list, $this->getObject()->getCampaign(), $source_target_list->getName() . ' copy ' . gmdate('Y-m-d H:i')); if ($copy_target_list) { $user = $this->getOption(self::USER, null); /* @var $user sfGuardUser */ $tr = new TargetListRights(); $tr->setMailingListId($copy_target_list->getId()); $tr->setUserId($user->getId()); $tr->setActive(1); $tr->save(); $this->getObject()->setMailingList($copy_target_list); $values['mailing_list_id'] = $copy_target_list->getId(); } else { throw new Exception('target list copy error'); } } else { $this->getObject()->setMailingList(MailingListTable::getInstance()->findById($ml_id, true)); } } else { $ml = new MailingList(); $ml->setCampaignId($this->getObject()->getCampaignId()); $ml->setStatus(MailingListTable::STATUS_DRAFT); $ml->setName($this->getObject()->getName() . ' Target-List ' . gmdate('Y-m-d H:i')); $this->getObject()->setMailingList($ml); } } if ($this->getObject()->isGeoKind()) { $target_choices = $target_choices = $this->getObject()->getMailingList()->getTargetChoices(); $set = array(); foreach (array($this->getValue('target_selector_1'), $this->getValue('target_selector_2')) as $ts) { if ($ts && array_key_exists($ts, $target_choices)) { $set[] = $ts; } } $values['email_targets'] = json_encode($set); } return $values; }