コード例 #1
0
 protected function doSave($con = null)
 {
     if (null === $con) {
         $con = $this->getConnection();
     }
     parent::doSave($con);
     $petition_contact = $this->getObject();
     $petition = $petition_contact->getPetition();
     $contact = $petition_contact->getContact();
     $pledge_items = PledgeItemTable::getInstance()->fetchByIds($petition->getActivePledgeItemIds());
     $pledge_table = PledgeTable::getInstance();
     foreach ($pledge_items as $pledge_item) {
         /* @var $pledge_item PledgeItem */
         $pledge = $pledge_table->findOneByPledgeItemAndContact($pledge_item, $contact);
         if (!$pledge) {
             $pledge = new Pledge();
             $pledge->setPledgeItem($pledge_item);
             $pledge->setContact($contact);
         }
         $status = $this->getValue('pledge_' . $pledge_item->getId());
         if ($status != $pledge->getStatus()) {
             $pledge->setStatus($status);
             $pledge->setStatusAt(gmdate('Y-m-d H:i:s'));
             $pledge->save($con);
         }
     }
 }
コード例 #2
0
 public function getChoices(Petition $petition, $first, $active_pledge_item_ids = false)
 {
     $ts = $petition->getTargetSelectors();
     $choices = array();
     $query = $this->createQuery('ml')->where('ml.id = ?', $petition->getMailingListId());
     $fix_field = false;
     $direct_contact = false;
     if (count($ts) > 1) {
         $second = $ts[1]['id'];
         if (is_numeric($second)) {
             $query->leftJoin('ml.MailingListMeta mlm2')->andWhere('mlm2.id = ?', $second)->leftJoin('mlm2.MailingListMetaChoice mlmc2')->leftJoin('mlmc2.ContactMeta cm2')->leftJoin('cm2.Contact c')->select('DISTINCT ml.id, mlm2.id, mlmc2.*');
         } else {
             $fix_field = true;
             $col = $ts[1]['id'];
             $query->leftJoin('ml.Contact c')->groupBy("c.{$col}")->select("DISTINCT ml.id, c.id, c.{$col}");
         }
     } else {
         $direct_contact = true;
         $query->leftJoin('ml.Contact c')->select('DISTINCT ml.id, c.firstname, c.lastname, c.country');
     }
     if (is_numeric($ts[0]['id'])) {
         $query->leftJoin('c.ContactMeta cm1');
         if (array_key_exists('kind', $ts[0]) && $ts[0]['kind'] == MailingListMeta::KIND_MAPPING) {
             $query->andWhere('cm1.mailing_list_meta_choice_id IN (SELECT mlmc1.id FROM MailingListMetaChoice mlmc1 WHERE mlmc1.mailing_list_meta_id = ? AND mlmc1.choice = ?)', array($ts[0]['meta_id'], $first));
         } else {
             $query->andWhere('cm1.mailing_list_meta_choice_id = ?', $first);
         }
     } else {
         $col = $ts[0]['id'];
         $query->andWhere("c.{$col} = ?", $first);
         // should be secure
     }
     $list = $query->execute();
     $pledges = array();
     $infos = array();
     $pledge_table = PledgeTable::getInstance();
     $pledge_info_columns = $petition->getPledgeInfoColumnsArray();
     if ($list) {
         if ($direct_contact) {
             foreach ($list[0]['Contact'] as $choice) {
                 /* @var $choice Contact */
                 $choices[$choice['id']] = $choice['firstname'] . ' ' . $choice['lastname'];
             }
             $pledges = $pledge_table->getPledgesForContacts($list[0]['Contact'], $active_pledge_item_ids);
             $infos = ContactTable::getInstance()->getPledgeInfoColumns($list[0]['Contact'], $pledge_info_columns);
         } else {
             if ($fix_field) {
                 $col = $ts[1]['id'];
                 foreach ($list[0]['Contact'] as $choice) {
                     $choices[$choice[$col]] = $choice[$col];
                 }
             } else {
                 foreach ($list[0]['MailingListMeta'][0]['MailingListMetaChoice'] as $choice) {
                     $choices[$choice['id']] = $choice['choice'];
                 }
             }
         }
     }
     return array('choices' => $choices, 'pledges' => $direct_contact && $active_pledge_item_ids !== false ? $pledges : false, 'infos' => $infos);
 }
コード例 #3
0
 public function executeContactEdit(sfWebRequest $request)
 {
     $petition = PetitionTable::getInstance()->findById($request->getParameter('petition_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();
     }
     $petition_contact = PetitionContactTable::getInstance()->findOneByPetitionIdAndContactId($petition->getId(), $request->getParameter('id'));
     if (!$petition_contact) {
         $contact = ContactTable::getInstance()->find($request->getParameter('id'));
         /* @var $contact Contact */
         if (!$contact) {
             return $this->notFound();
         }
         if ($contact->getMailingListId() != $petition->getMailingListId()) {
             return $this->notFound();
         }
         $petition_contact = new PetitionContact();
         $petition_contact->setPetition($petition);
         $petition_contact->setContact($contact);
     } else {
         $contact = $petition_contact->getContact();
     }
     $form = new PetitionContactForm($petition_contact);
     if ($request->isMethod('post')) {
         $form->bind($request->getPostParameter($form->getName()));
         if ($form->isValid()) {
             $form->save();
             $active_pledge_item_ids = $petition->getActivePledgeItemIds();
             $pledges = PledgeTable::getInstance()->getPledgesForContacts(array($contact), $active_pledge_item_ids);
             $pledge_items = PledgeItemTable::getInstance()->fetchByIds($active_pledge_item_ids);
             return $this->ajax()->remove('#contact_edit_row_' . $contact->getId())->replaceWithPartial('#contact_' . $contact->getId(), 'contact', array('contact' => $contact, 'active_pledge_item_ids' => $active_pledge_item_ids, 'pledges' => $pledges, 'pledge_items' => $pledge_items, 'petition_id' => $petition->getId()))->tooltip('#contact_' . $contact->getId() . ' .add_tooltip')->render();
         } else {
             return $this->ajax()->form($form)->render();
         }
     }
     return $this->ajax()->remove('#contact_edit_row_' . $contact->getId())->afterPartial('#contact_' . $contact->getId(), 'contactEdit', array('form' => $form))->render();
 }
コード例 #4
0
 public function getTargetSelectorChoices2($first, $second)
 {
     $tagging_cache = sfCacheTaggingToolkit::getTaggingCache();
     $cache_key = 'Petition_TS2_' . $this->getId() . '_' . (is_scalar($first) && is_scalar($second) ? $first . '__' . $second : md5(json_encode(array($first, $second))));
     $cached_ret = $tagging_cache->get($cache_key, null);
     if ($cached_ret !== null) {
         return $cached_ret;
     }
     $contacts = ContactTable::getInstance()->queryByTargetSelector($this, $first, $second)->execute();
     $pledge_table = PledgeTable::getInstance();
     $choices = array();
     $active_pledge_item_ids = $this->getActivePledgeItemIds();
     $pledge_info_columns = $this->getPledgeInfoColumnsArray();
     $pledges = $pledge_table->getPledgesForContacts($contacts, $active_pledge_item_ids);
     $infos = ContactTable::getInstance()->getPledgeInfoColumns($contacts, $pledge_info_columns);
     foreach ($contacts as $contact) {
         /* @var $contact Contact */
         $choices[$contact['id']] = $contact['firstname'] . ' ' . $contact['lastname'];
     }
     $ret = array('choices' => $choices, 'pledges' => $pledges, 'infos' => $infos);
     $tags = $this->getCacheTags();
     if ($this->getMailingListId()) {
         $tags = array_merge($tags, $this->getMailingList()->getCacheTags());
     }
     $tagging_cache->set($cache_key, $ret, 24 * 3600, $tags);
     return $ret;
 }
コード例 #5
0
 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;
 }