public function executeUpload(sfWebRequest $request)
 {
     $target_list = $this->findTargetList();
     /* @var $target_list MailingList */
     if (!$target_list) {
         return $this->notFound();
     }
     if (!$this->getGuardUser()->isTargetListMember($target_list, true)) {
         return $this->noAccess();
     }
     $form1 = new ContactUploadStep1Form();
     $form2 = new ContactUploadStep2Form(array(), array('MailingList' => $target_list));
     if ($request->isMethod('post')) {
         if ($request->hasParameter($form1->getName())) {
             $form1->bind($request->getPostParameter($form1->getName()), $request->getFiles($form1->getName()));
             if ($form1->isValid()) {
                 $filename = $form1->save();
                 $form2->setSeparator($form1->getSeparator());
                 $form2->setFile($filename, true);
                 return $this->ajax()->replaceWithPartial('#upload_form', 'upload2', array('form' => $form2, 'target_list' => $target_list))->render(true);
             } else {
                 return $this->ajax()->form($form1)->render(true);
             }
         } else {
             $form2_params = $request->getPostParameter($form2->getName());
             $bind_ok = $form2->bind($form2_params);
             if (!$bind_ok) {
                 return $this->ajax()->alert('Critical error', '', '#upload_form .form-actions', 'before')->render();
             }
             if ($form2->isValid()) {
                 if ($form2->save()) {
                     $target_list->state(Doctrine_Record::STATE_DIRTY);
                     // to invalidate cache
                     $target_list->save();
                     return $this->ajax()->replaceWith('#upload_form', '<div id="upload_form"></div>')->alert('Upload successfull', '', '#upload_form', 'append')->replaceWithComponent('#contacts', 'target', 'contacts', array('target_list' => $target_list, 'page' => 1, 'no_filter' => true))->render();
                 } else {
                     return $this->ajax()->form($form2)->alert('Upload Error', '', '#upload_form .form-actions', 'before')->render();
                 }
             } else {
                 return $this->ajax()->form($form2)->render();
             }
         }
     }
     return $this->ajax()->replaceWithPartial('#upload_form', 'upload1', array('form' => $form1, 'target_list' => $target_list))->render();
 }
 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;
 }