コード例 #1
0
 /**
  *
  * @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;
 }