public function save() { $separator = $this->getValue('separator'); $separator2 = $this->getValue('separator2'); $ml = $this->getOption('MailingList'); if ($ml instanceof MailingList) { $con = $ml->getTable()->getConnection(); $con->beginTransaction(); try { $ml->invalidateCache(); $substs = $ml->getSubstFields(); ksort($substs); $meta_choices_db = Doctrine_Core::getTable('MailingListMeta')->createQuery('mlm')->where('mlm.mailing_list_id = ?', $ml->getId())->andWhere('mlm.kind = ?', MailingListMeta::KIND_CHOICE)->addFrom('mlm.MailingListMetaChoice mlmc')->fetchArray(); $meta_choices = array(); foreach ($meta_choices_db as $meta_choice_db) { $choices = array(); foreach ($meta_choice_db['MailingListMetaChoice'] as $choice_db) { $choices[mb_strtolower(trim($choice_db['choice']), 'utf-8')] = $choice_db['id']; } $meta_choices[$meta_choice_db['id']] = $choices; } if (($handle = @fopen(ContactUploadStep1Form::getDir($this->getValue('file')), 'r')) !== false) { $skip = true; $female = $this->getValue('female'); $male = $this->getValue('male'); $countries = array_keys(sfCultureInfo::getInstance()->getCountries()); $language_col = $this->getValue('language'); if (strlen($language_col)) { $language_ids = LanguageTable::getInstance()->fetchLanguageIds(); } setlocale(LC_ALL, 'en_US.UTF-8'); // fixes missing Umlauts while (($data = fgetcsv($handle, 0, $separator)) !== false) { if ($skip) { $skip = false; continue; } $contact = new Contact(); $contact->setMailingListId($ml->getId()); foreach ($substs as $subst) { $value = ''; $col = (int) $this->getValue('field_' . $subst['id']); if (array_key_exists($col, $data)) { $value = trim($data[$col]); } switch ($subst['type']) { case 'fix': switch ($subst['id']) { case 'gender': $contact['gender'] = $value == $female ? Contact::GENDER_FEMALE : ($value == $male ? Contact::GENDER_MALE : Contact::GENDER_NEUTRAL); break; case 'country': $country = strtoupper($value); if (in_array($country, $countries)) { $contact['country'] = $country; } break; default: $contact[$subst['id']] = $value; } break; case 'free': $meta = new ContactMeta(); $meta->setMailingListMetaId($subst['id']); $meta->setValue($value); $contact->ContactMeta[] = $meta; break; case 'choice': if (array_key_exists($subst['id'], $meta_choices)) { $choices = $meta_choices[$subst['id']]; if ($subst['multi']) { foreach (explode($separator2, mb_strtolower($value, 'utf-8')) as $value_i) { $lower = trim($value_i); if (array_key_exists($lower, $choices)) { $meta = new ContactMeta(); $meta->setMailingListMetaId($subst['id']); $meta->setMailingListMetaChoiceId($choices[$lower]); $contact->ContactMeta[] = $meta; } } } else { $lower = mb_strtolower($value, 'utf-8'); if (array_key_exists($lower, $choices)) { $meta = new ContactMeta(); $meta->setMailingListMetaId($subst['id']); $meta->setMailingListMetaChoiceId($choices[$lower]); $contact->ContactMeta[] = $meta; } } } break; } } if (strlen($language_col)) { if (array_key_exists((int) $language_col, $data)) { $language_id = trim($data[(int) $language_col]); if (in_array($language_id, $language_ids)) { $contact->setLanguageId($language_id); } } } $contact->save(); } @fclose($handle); } $con->commit(); } catch (Exception $e) { $con->rollback(); return false; } } return true; }
/** * * @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(); $metas = $this->getObject()->getMailingList()->getMailingListMeta(); // TODO: optimize $contact_metas = $this->getObject()->getContactMeta(); foreach ($metas as $meta) { /* @var $meta MailingListMeta */ $value = $this->getValue('meta_' . $meta['id']); if ($meta['kind'] == MailingListMeta::KIND_CHOICE && $meta->getMulti()) { if (!is_array($value)) { $value = array(); } $keep_multis = array(); foreach ($contact_metas as $cc) { if ($cc['mailing_list_meta_id'] === $meta['id']) { /* @var $cc ContactMeta */ if (in_array($cc->getMailingListMetaChoiceId(), $value)) { $keep_multis[] = $cc->getMailingListMetaChoiceId(); // keep } else { $cc->delete($con); // delete } } } foreach ($value as $value_i) { if (!in_array($value_i, $keep_multis)) { $contact_meta = new ContactMeta(); // create $contact_meta->setMailingListMetaId($meta['id']); $contact_meta->setContactId($this->getObject()->getId()); $contact_meta->setMailingListMetaChoiceId($value_i); $contact_meta->save($con); } } } else { $contact_meta = null; foreach ($contact_metas as $cc) { if ($cc['mailing_list_meta_id'] === $meta['id']) { $contact_meta = $cc; break; } } if ($contact_meta === null) { $contact_meta = new ContactMeta(); $contact_meta->setMailingListMetaId($meta['id']); $contact_meta->setContactId($this->getObject()->getId()); } switch ($meta['kind']) { case MailingListMeta::KIND_CHOICE: $contact_meta->setMailingListMetaChoiceId($value); break; case MailingListMeta::KIND_FREE: $contact_meta->setValue($value); break; } $contact_meta->save($con); } } }