/** * * @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; }
protected function doSave($con = null) { parent::doSave($con); $this->getObject()->getMailingList()->invalidateCache(); $choices_db = $this->getObject()->getMailingListMetaChoice(); $choices = $this->getObject()->getKind() == MailingListMeta::KIND_CHOICE ? $this->getValue('choices') : array(); $choices_lower = array_map('strtolower', $choices); $done = array(); foreach ($choices_db as $choice_db) { $lower = mb_strtolower($choice_db['choice'], 'utf-8'); $choice_key = array_search($lower, $choices_lower, true); if ($choice_key === false) { $choice_db->delete($con); } else { $done[] = $lower; if ($choices[$choice_key] !== $choice_db['choice']) { $choice_db['choice'] = $choices[$choice_key]; $choice_db->save($con); } } } foreach ($choices as $choice) { if (!in_array(mb_strtolower($choice, 'utf-8'), $done, true)) { $choice_db = new MailingListMetaChoice(); $choice_db->setMailingListMetaId($this->getObject()->getId()); $choice_db->setChoice($choice); $choice_db->save(); } } }