/** * * @param int $petition_id * @return PetitionContact */ public function getPetitionContactByPetition($petition_id) { return PetitionContactTable::getInstance()->findOneByPetitionIdAndContactId($petition_id, $this->getId()); }
public function executeDownload(sfWebRequest $request) { $petition = PetitionTable::getInstance()->findById($request->getParameter('id'), $this->userIsAdmin()); /* @var $petition Petition */ if (!$petition) { return $this->notFound(); } if (!$petition->isEditableBy($this->getGuardUser())) { return $this->noAccess(); } if ($petition->getKind() != Petition::KIND_PLEDGE) { return $this->noAccess(); } $target_list = $petition->getMailingList(); if (!$target_list) { return $this->notFound(); } $contact_data = ContactTable::getInstance()->queryFullData($target_list, $petition)->execute(array(), Doctrine_Core::HYDRATE_ARRAY); $pledge_data = ContactTable::getInstance()->queryByMailingList($target_list, $petition)->execute(array(), Doctrine_Core::HYDRATE_ARRAY); $pledge_data_contacts = PetitionContactTable::getInstance()->queryByPetition($petition->getId())->execute(array(), Doctrine_Core::HYDRATE_ARRAY); $mailing_list_meta = $target_list->getMailingListMeta(); $pledge_items = $petition->getPledgeItems(); $head = array('email', 'gender', 'first name', 'last name', 'country', 'language'); foreach ($mailing_list_meta as $mlm) { /* @var $mlm MailingListMeta */ if ($mlm->getKind() != MailingListMeta::KIND_MAPPING) { $head[] = $mlm->getName(); } } foreach ($pledge_items as $pledge_item) { /* @var $pledge_item PledgeItem */ $head[] = $pledge_item->getName(); $head[] = 'pledge date'; } $head[] = 'comment'; $out = fopen('php://temp/export', 'a+'); foreach ($contact_data as $id => $contact) { $pledges = array_key_exists($id, $pledge_data) ? $pledge_data[$id]['Pledges'] : array(); $data = array($contact['email'], array_key_exists($contact['gender'], Contact::$GENDER_SHOW) ? Contact::$GENDER_SHOW[$contact['gender']] : null, $contact['firstname'], $contact['lastname'], $contact['country'], $contact['language_id']); $contact_metas = array(); foreach ($contact['ContactMeta'] as $cm) { $cm_id = $cm['mailing_list_meta_id']; if (!array_key_exists($cm_id, $contact_metas)) { $contact_metas[$cm_id] = array(); } if ($cm['MailingListMetaChoice']) { $contact_metas[$cm_id][] = $cm['MailingListMetaChoice']['choice']; } else { $contact_metas[$cm_id][] = $cm['value']; } } foreach ($mailing_list_meta as $mlm) { /* @var $mlm MailingListMeta */ if ($mlm->getKind() != MailingListMeta::KIND_MAPPING) { $data[] = array_key_exists($mlm->getId(), $contact_metas) ? implode('|', $contact_metas[$mlm->getId()]) : ''; } } foreach ($pledge_items as $pledge_item) { /* @var $pledge_item PledgeItem */ $pledge_status = array_key_exists($pledge_item->getId(), $pledges) ? $pledges[$pledge_item->getId()]['status'] : null; $data[] = array_key_exists($pledge_status, PledgeTable::$STATUS_SHOW) ? PledgeTable::$STATUS_SHOW[$pledge_status] : null; $data[] = array_key_exists($pledge_item->getId(), $pledges) ? $pledges[$pledge_item->getId()]['status_at'] : null; } $data[] = array_key_exists($id, $pledge_data_contacts) ? $pledge_data_contacts[$id]['comment'] : null; fputcsv($out, $data, ';'); } header('Content-Description: File Transfer'); header('Content-Type: application/csv'); header('Content-Type: text/plain'); header('Content-Disposition: attachment; filename=' . '"pledges.csv"'); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); ob_clean(); flush(); $stdout = fopen('php://output', 'w'); fwrite($stdout, ""); fputcsv($stdout, $head, ';'); fclose($stdout); rewind($out); fpassthru($out); fclose($out); exit; }
protected function execute($arguments = array(), $options = array()) { $context = sfContext::createInstance($this->configuration); $i18n = $context->getI18N(); // initialize the database connection $databaseManager = new sfDatabaseManager($this->configuration); $connection = $databaseManager->getDatabase($options['connection'])->getConnection(); $petition_text_by_lang = array(); $table = PetitionSigningTable::getInstance(); $con = $table->getConnection(); $con->beginTransaction(); try { $petition_signing = $table->createQuery('ps')->addFrom('ps.PetitionSigningWave psw')->where('ps.wave_cron > 0')->andWhere('DATE_SUB(NOW(),INTERVAL 2 MINUTE) > ps.updated_at')->andWhere('psw.status = ?', PetitionSigning::STATUS_VERIFIED)->orderBy('ps.created_at DESC')->fetchOne(); if (empty($petition_signing)) { echo "nothing pending. (stop)"; $connection->rollback(); return; } /* @var $petition_signing PetitionSigning */ $petition = $petition_signing->getPetition(); /* @var $petition Petition */ $is_pledge = $petition->getKind() == Petition::KIND_PLEDGE; $waves_verified = array(); foreach ($petition_signing->getPetitionSigningWave() as $wave) { if ($wave->getStatus() == PetitionSigning::STATUS_VERIFIED) { $waves_verified[] = $wave->getWave(); $wave->setStatus(PetitionSigning::STATUS_SENT); } } $contacts = ContactTable::getInstance()->createQuery('c')->addFrom('c.PetitionSigningContact psc')->where('psc.petition_signing_id = ?', $petition_signing->getId())->andWhereIn('psc.wave', $waves_verified)->addFrom('c.ContactMeta cm')->addFrom('cm.MailingListMetaChoice mlmc')->fetchArray(); $subst_fields = $petition_signing->getPetition()->getGeoSubstFields(); $i = 0; foreach ($contacts as $contact) { $subst = $petition_signing->getSubst(); foreach ($subst_fields as $pattern => $subst_field) { switch ($subst_field['type']) { case 'fix': $subst[$pattern] = $contact[$subst_field['id']]; break; case 'free': $subst[$pattern] = ''; foreach ($contact['ContactMeta'] as $cm) { if ($cm['mailing_list_meta_id'] == $subst_field['id']) { $subst[$pattern] = $cm['value']; } } break; case 'choice': $subst[$pattern] = ''; foreach ($contact['ContactMeta'] as $cm) { if ($cm['mailing_list_meta_id'] == $subst_field['id']) { $subst[$pattern] = $cm['MailingListMetaChoice']['choice']; } } break; } } $wave = $petition_signing->getWave($contact['PetitionSigningContact'][0]['wave']); $wave_lang_id = $wave->getLanguageId(); if ($wave_lang_id) { $i18n->setCulture($wave_lang_id); } if ($contact['gender'] == Contact::GENDER_FEMALE) { $personal_salutation = $i18n->__('Dear Madam %F %L,', array('%F' => $contact['firstname'], '%L' => $contact['lastname'])); } elseif ($contact['gender'] == Contact::GENDER_MALE) { $personal_salutation = $i18n->__('Dear Sir %F %L,', array('%F' => $contact['firstname'], '%L' => $contact['lastname'])); } else { $personal_salutation = $i18n->__('Dear Sir/Madam %F %L,', array('%F' => $contact['firstname'], '%L' => $contact['lastname'])); } $personal_salutation .= "\n\n"; $subst[PetitionTable::KEYWORD_PERSONAL_SALUTATION] = $personal_salutation; if ($wave) { if ($is_pledge) { $petition_contact = PetitionContactTable::getInstance()->findOneByPetitionIdAndContactId($petition->getId(), $contact['id']); if (!$petition_contact) { $petition_contact = new PetitionContact(); $petition_contact->setPetitionId($petition->getId()); $petition_contact->setContactId($contact['id']); $new_secret = ''; while (strlen($new_secret) < 15) { $new_secret .= strtoupper(strtr(base_convert(mt_rand(), 10, 36), array('0' => '', 'o' => ''))); } $petition_contact->setSecret(substr($new_secret, 0, 15)); $petition_contact->save(); } $secret = $petition_contact->getSecret(); $subst['#PLEDGE-URL#'] = $this->getRouting()->generate('pledge_contact', array('petition_id' => $petition->getId(), 'contact_id' => $contact['id'], 'secret' => $secret), true); } if (array_key_exists($contact['language_id'], $petition_text_by_lang)) { $petition_text = $petition_text_by_lang[$contact['language_id']]; } else { $petition_text = PetitionTextTable::getInstance()->fetchByPetitionAndPrefLang($petition, $contact['language_id'], Doctrine_Core::HYDRATE_ARRAY); $petition_text_by_lang[$contact['language_id']] = $petition_text; } if ($petition->getKind() == Petition::KIND_PLEDGE) { if ($petition_text) { $subject = $petition_text['email_subject']; $body = $petition_text['email_body']; } else { $subject = $body = ''; } } else { $subject = $wave->getField(Petition::FIELD_EMAIL_SUBJECT); $body = $wave->getField(Petition::FIELD_EMAIL_BODY); } if ($petition_text) { UtilMail::appendMissingKeywords($body, $petition_text['email_body'], PetitionSigningTable::$KEYWORDS); } $i++; try { /* Email to target */ UtilMail::send(null, $wave->getEmailContact($petition->getFromEmail(), true), array($contact['email'] => $contact['firstname'] . ' ' . $contact['lastname']), $subject, $body, null, $subst, null, $wave->getEmailContact()); /* email problem */ } catch (Swift_RfcComplianceException $e) { // ignore invalid emails } } } $id = $petition_signing->getId(); $waves_sent = implode(',', $waves_verified); $petition_signing->setWaveSent($petition_signing->getWaveCron()); $petition_signing->setWaveCron(0); $petition_signing->save(); echo "{$i} mails sent. [{$id}:{$waves_sent}] (continue)"; $con->commit(); } catch (Exception $e) { $con->rollback(); print $e; echo 'exception in transaction. (stop)'; } }
public function executeIndex(sfWebRequest $request) { $petiion_id = $request->getParameter('petition_id'); $contact_id = $request->getParameter('contact_id'); $this->show_thankyou = false; if ($contact_id) { $petition_contact = PetitionContactTable::getInstance()->findOneByPetitionIdAndContactId($petiion_id, $contact_id); if (!$petition_contact) { return $this->notFound(); } if ($petition_contact->getSecret() != $request->getParameter('secret')) { return $this->notFound(); } $contact = $petition_contact->getContact(); $petition = $petition_contact->getPetition(); /* @var $petition Petition */ } else { $petition = PetitionTable::getInstance()->find($petiion_id); if (!$petition) { return $this->notFound(); } $contact = new Contact(); $contact->setFirstname('John'); $contact->setLastname('Doe'); $contact->setGender(Contact::GENDER_MALE); $petition_contact = new PetitionContact(); $petition_contact->setPetition($petition); $petition_contact->setContact($contact); $this->show_thankyou = true; } $languages = LanguageTable::getInstance()->queryByActivePetitionTexts($petition)->execute(); $this->languages = $languages; $language_ids = array(); foreach ($languages as $language) { $language_ids[] = $language->getId(); } $contact_lang = $contact->getLanguageId() ?: 'en'; if (!in_array($contact_lang, $language_ids)) { $contact_lang = in_array('en', $language_ids) ? 'en' : reset($language_ids); } $lang = $request->getGetParameter('lang'); if ($lang && in_array($lang, $language_ids)) { $contact_lang = $lang; } $contact->setLanguageId($contact_lang); $petition_text = $contact->getPetitionTextForPetition($petition); $this->getUser()->setCulture($contact_lang); if (!$petition_text) { return $this->notFound(); } /* @var $petition_text PetitionText */ $i18n = $this->getContext()->getI18N(); $i18n->setCulture($petition_text->getLanguageId()); $salutation = $contact->generateSalutation($i18n); $this->salutation = $salutation; $this->petition_text = $petition_text; $this->petition = $petition; $this->petition_contact = $petition_contact; $this->ask_password = false; $this->wrong_password = false; $this->session = null; $this->password_no_match = false; $this->password_too_short = false; if ($petition_contact->getPassword()) { $session = $request->getPostParameter('session'); if ($session && is_string($session) && $session == crypt($petition_contact->getPassword(), $session)) { $this->session = $session; } else { if ($request->isMethod('post')) { $password = trim($request->getPostParameter('password')); if ($password) { if ($petition_contact->checkPassword($password)) { $this->session = crypt($petition_contact->getPassword(), '$6$' . PetitionContactTable::salt()); } else { $this->wrong_password = true; $this->ask_password = true; return; } } else { $this->ask_password = true; return; } } else { $this->ask_password = true; return; } } } $pledge_table = PledgeTable::getInstance(); $pledge_items = $petition->getPledgeItems(); $pledges = array(); foreach ($pledge_items as $pledge_item) { /* @var $pledge_item PledgeItem */ if ($pledge_item->getStatus() == PledgeItemTable::STATUS_ACTIVE) { $pledge = $pledge_table->findOneByPledgeItemAndContact($pledge_item, $contact); if (!$pledge) { $pledge = new Pledge(); $pledge->setPledgeItem($pledge_item); $pledge->setContact($contact); if (!$contact->isNew()) { $pledge->save(); } } else { $pledge->setPledgeItem($pledge_item); } $pledges[] = $pledge; } } if ($request->isMethod('post')) { $this->show_thankyou = true; $pledge_changed = false; foreach ($pledges as $pledge) { $status = $request->getPostParameter('status_' . $pledge->getPledgeItem()->getId()); if (in_array($status, array(PledgeTable::STATUS_YES, PledgeTable::STATUS_NO, PledgeTable::STATUS_NO_COMMENT))) { $pledge_changed = $pledge_changed || $pledge->getStatus() != $status; if ($pledge->getStatus() != PledgeTable::STATUS_YES) { if ($pledge->getStatus() != $status) { $pledge->setStatusAt(gmdate('Y-m-d H:i:s')); } $pledge->setStatus($status); } } $pledge->save(); } if ($petition->getPledgeWithComments()) { $comment = $request->getPostParameter('comment'); if (is_string($comment)) { $petition_contact->setComment(trim($comment)); $petition_contact->save(); } } if ($pledge_changed) { $petition->state(Doctrine_Record::STATE_DIRTY); // trigger widget update $petition->save(); } $password1 = trim($request->getPostParameter('new_password1')); $password2 = trim($request->getPostParameter('new_password2')); if ($password1) { if ($password1 !== $password2) { $this->password_no_match = true; $this->show_thankyou = false; } else { if (strlen($password1) < 8) { $this->password_too_short = true; $this->show_thankyou = false; } else { $petition_contact->setHashPassword($password1); $petition_contact->save(); $this->session = crypt($petition_contact->getPassword(), '$6$' . PetitionContactTable::salt()); } } } } $this->pledges = $pledges; }