protected function execute($arguments = array(), $options = array())
 {
     $context = sfContext::createInstance($this->configuration);
     // initialize the database connection
     $databaseManager = new sfDatabaseManager($this->configuration);
     $connection = $databaseManager->getDatabase($options['connection'])->getConnection();
     $table = PetitionTable::getInstance();
     $con = $table->getConnection();
     $silent = $options['silent'];
     $utc_hour = $options['utc-hour'];
     if ($utc_hour !== '') {
         if (gmdate('H') !== $utc_hour) {
             if (!$silent) {
                 echo "exiting, it is not the right hour.\n";
             }
             return;
         }
     }
     $petitions = $table->fetchScheduleNeed();
     foreach ($petitions as $petition) {
         /* @var $petition Petition */
         if (!$silent) {
             echo $petition->getId() . "\t" . $petition->getName() . "\n";
         }
         //      $petition->state(Doctrine_Record::STATE_DIRTY); // with cachetagging this does not help, we have to change something
         $kind = $petition->getKind();
         $petition->setKind(0);
         $petition->setKind($kind);
         $petition->save();
     }
     if (!$silent) {
         echo "done.\n";
     }
 }
Ejemplo n.º 2
0
 public function executeNew(sfWebRequest $request)
 {
     $petition = PetitionTable::getInstance()->findById($request->getParameter('id'), $this->userIsAdmin());
     /* @var $petition Petition */
     if (!$petition) {
         return $this->notFound();
     }
     if (!$petition->isTicketManager($this->getGuardUser())) {
         return $this->noAccess();
     }
     $token = new PetitionApiToken();
     $token->setPetition($petition);
     $code = '';
     while (strlen($code) < 30) {
         $code .= substr(base_convert(mt_rand(), 10, 36), 1, 5);
     }
     $code = substr($code, 0, 30);
     $token->setToken($code);
     $form = new PetitionApiTokenForm($token);
     if ($request->isMethod('post')) {
         $form->bind($request->getPostParameter($form->getName()));
         if ($form->isValid()) {
             $form->save();
             return $this->ajax()->redirectRotue('petition_tokens', array('id' => $petition->getId()))->render();
         }
         return $this->ajax()->form($form)->render();
     }
     $this->petition = $petition;
     $this->form = $form;
 }
Ejemplo n.º 3
0
 public function countActionsDeleted()
 {
     if ($this->count_actions_deleted_cache !== null) {
         return $this->count_actions_deleted_cache;
     }
     return $this->count_actions_deleted_cache = PetitionTable::getInstance()->countActionsDeletedByTargetList($this);
 }
Ejemplo n.º 4
0
 /**
  * @param int $id
  * @return Petition
  */
 private function getPetition($id)
 {
     $petition = PetitionTable::getInstance()->findById($id, $this->userIsAdmin());
     /* @var $petition Petition */
     $right = $this->rightPetition($petition);
     if ($right) {
         return $petition;
     } else {
         return $right === false ? $this->notFound() : $this->noAccess();
     }
 }
Ejemplo n.º 5
0
 public function executeTrending()
 {
     $user = $this->getGuardUser();
     $petition_table = PetitionTable::getInstance();
     $petition_query = $petition_table->queryByUserCampaigns($user, false, false, true);
     $petition_query = $petition_table->filter($petition_query, new poilcatFilterArray(array(PetitionTable::FILTER_ORDER => PetitionTable::ORDER_TRENDING)));
     $petition_query->limit(5);
     $this->petitions = $petition_query->execute();
     $widget_table = WidgetTable::getInstance();
     $widget_query = $widget_table->queryByUser($user);
     $widget_table->filter($widget_query, new poilcatFilterArray(array(WidgetTable::FILTER_ORDER => WidgetTable::ORDER_TRENDING)));
     $widget_query->limit(5);
     $this->widgets = $widget_query->execute();
 }
 public function configure()
 {
     $query = $this->getOption(self::OPTION_QUERY);
     /* @var $query Doctrine_Query */
     $lang_sub_query = $query->copy()->select('DISTINCT pt.language_id');
     $lang_query = Doctrine_Core::getTable('Language')->createQuery('l')->where('l.id IN (' . $lang_sub_query->getDql() . ')', $lang_sub_query);
     $lang_query->setParams($lang_sub_query->getParams());
     if ($this->isDataOwner()) {
         $this->setWidget('t', new sfWidgetFormChoice(array('choices' => self::$TYPE_SHOW, 'label' => false), array('class' => 'span2')));
     }
     //    if (!$this->isDataOwner())
     //      $this->getWidget('t')->setAttribute('disabled', 'disabled');
     if ($this->isDataOwner()) {
         $this->setValidator('t', new sfValidatorChoice(array('choices' => array_keys(self::$TYPE_SHOW), 'required' => false)));
     } else {
         $this->setValidator('t', new sfValidatorChoice(array('choices' => array(''), 'required' => false)));
     }
     $this->setWidget('l', new sfWidgetFormDoctrineChoice(array('model' => 'Language', 'add_empty' => ' Language', 'query' => $lang_query, 'label' => false), array('class' => 'span2')));
     $this->setValidator('l', new sfValidatorDoctrineChoice(array('required' => false, 'model' => 'Language', 'query' => $lang_query)));
     $countries = $query->copy()->select('DISTINCT ps.country')->execute(array(), Doctrine_Core::HYDRATE_SINGLE_SCALAR);
     if (is_string($countries)) {
         $countries = array($countries);
     }
     $countries = array_filter($countries);
     $this->setWidget('c', new sfWidgetFormI18nChoiceCountry(array('countries' => $countries, 'add_empty' => ' Country', 'label' => false), array('class' => 'span2')));
     $this->setValidator('c', new sfValidatorChoice(array('choices' => $countries, 'required' => false)));
     $campaign = $this->getOption(PetitionSigningTable::CAMPAIGN);
     if ($campaign && !$this->getOption(PetitionSigningTable::PETITION)) {
         $petitions = PetitionTable::getInstance()->queryByCampaign($campaign)->execute();
         $petition_choices = array('' => '');
         foreach ($petitions as $petition_i) {
             /* @var $petition_i Petition */
             $petition_choices[$petition_i->getId()] = $petition_i->getName();
         }
         $this->setWidget('p', new sfWidgetFormChoice(array('choices' => $petition_choices, 'label' => false), array('class' => 'span2', 'data-placeholder' => 'Action')));
         $this->setValidator('p', new sfValidatorChoice(array('choices' => array_keys($petition_choices), 'required' => false)));
     }
     $petition = $this->getOption(PetitionSigningTable::PETITION);
     if ($petition) {
         $widget_filter = PetitionSigningTable::getInstance()->getWidgetFilter($petition);
         $this->setWidget('w', new sfWidgetFormChoice(array('choices' => $widget_filter, 'label' => false), array('class' => 'span2', 'data-placeholder' => 'Widget')));
         $this->setValidator('w', new sfValidatorChoice(array('required' => false, 'choices' => array_merge(array_keys($widget_filter['Users']), array_keys($widget_filter['Organisations']), array_keys($widget_filter['Widgets'])))));
     }
     $this->setWidget('s', new sfWidgetFormInputText(array('label' => false), array('type' => 'search', 'placeholder' => 'Search', 'class' => 'span2', 'style' => 'vertical-align:top', 'title' => 'Enter a part of, or the full name, email-address, or other data. If you don\'t get a search result, check different spellings and accents')));
     $this->setValidator('s', new sfValidatorString(array('required' => false)));
 }
Ejemplo n.º 7
0
 public function executeList()
 {
     $page = isset($this->page) ? $this->page : 1;
     $table = PetitionTable::getInstance();
     if (isset($this->campaign)) {
         $this->form = new FilterPetitionForm(array(), array(FilterPetitionForm::USER => $this->getGuardUser(), FilterPetitionForm::DELETED_TOO => $this->userIsAdmin()));
         $this->form->bindSelf('c' . $this->campaign->getId());
         $query = $table->queryByCampaign($this->campaign, $this->userIsAdmin());
         $this->petitions = new policatPager($query, $page, 'petition_pager', array('id' => $this->campaign->getId()), true, 20, $this->form);
     } else {
         $this->form = new FilterPetitionForm(array(), array(FilterPetitionForm::WITH_CAMPAIGN => true, FilterPetitionForm::USER => $this->getGuardUser(), FilterPetitionForm::DELETED_TOO => $this->userIsAdmin()));
         $this->form->bindSelf('all');
         $query = $table->queryByUserCampaigns($this->getGuardUser(), false, false);
         $this->petitions = new policatPager($query, $page, 'petition_pager_all', array(), true, 20, $this->form);
     }
     $this->csrf_token_leave = UtilCSRF::gen('action_leave');
     $this->csrf_token_join = UtilCSRF::gen('action_join');
 }
Ejemplo n.º 8
0
 public function executePetition(sfWebRequest $request)
 {
     $petition = PetitionTable::getInstance()->findById($request->getParameter('id'), $this->userIsAdmin());
     /* @var $petition Petition */
     if (!$petition) {
         return $this->notFound();
     }
     if (!$petition->isGeoKind()) {
         return $this->notFound();
     }
     if (!$petition->isEditableBy($this->getGuardUser())) {
         return $this->noAccess();
     }
     $target_list_id = $petition->getMailingListId();
     if ($target_list_id) {
         $target_list = $petition->getMailingList();
         if ($this->getGuardUser()->isTargetListMember($target_list, true)) {
             $this->csrf_token = UtilCSRF::gen('target_activate');
             $this->metas = $target_list->getMailingListMeta();
             if ($target_list->getCampaignId()) {
                 $this->campaign = $target_list->getCampaign();
                 if (!$this->userIsAdmin() && $this->campaign->getStatus() == CampaignTable::STATUS_DELETED) {
                     return $this->notFound();
                 }
                 $this->target_list = $target_list;
                 $this->form = new MailingListForm($target_list);
             }
         }
     }
     $this->petition = $petition;
     $this->target_form = new EditPetitionTargetForm($petition, array(EditPetitionTargetForm::USER => $this->getGuardUser()));
     $this->open_edit = $request->getGetParameter('e') ? true : false;
     $this->setTemplate('edit');
     $this->includeIframeTransport();
     $this->includeChosen();
 }
Ejemplo n.º 9
0
 public function executeData(sfWebRequest $request)
 {
     $petition = PetitionTable::getInstance()->findById($request->getParameter('id'), $this->userIsAdmin());
     /* @var $petition Petition */
     if (!$petition) {
         return $this->notFound();
     }
     if (!$this->getGuardUser()->isPetitionMember($petition, true)) {
         return $this->noAccess();
     }
     $this->petition = $petition;
     $this->includeChosen();
 }
Ejemplo n.º 10
0
 /**
  * Executes actionSignings action
  *
  * @param sfRequest $request A request object
  */
 public function executeActionSignings(sfWebRequest $request)
 {
     $this->setLayout(false);
     $response = $this->getResponse();
     // does the client want JSONP?
     $callback = trim(preg_replace('/[^a-z_.]/i', '', $request->getParameter('callback', null)));
     // determine the requested action (petition)
     $action_id = $request->getParameter('action_id');
     if (!is_numeric($action_id) || $action_id < 0) {
         $response->setStatusCode(400);
         return $this->renderJson(array('status' => 'error', 'message' => 'bad action ID given'), $callback);
     }
     $petition = PetitionTable::getInstance()->findByIdCachedActive($action_id);
     if (!$petition) {
         $response->setStatusCode(404);
         return $this->renderJson(array('status' => 'error', 'message' => 'action could not be found'), $callback);
     }
     $signings_table = PetitionSigningTable::getInstance();
     $token_table = PetitionApiTokenTable::getInstance();
     $timeToLive = 60;
     $refresh = false;
     $token_code = $request->getPostParameter('token');
     if ($token_code) {
         $token = $token_table->fetchByPetitionAndToken($petition, $token_code, PetitionApiTokenTable::STATUS_ACTIVE);
         if (!$token) {
             return $this->renderJson(array('status' => 'error', 'message' => 'token wrong'), $callback);
         }
         $foreign_singings = $request->getPostParameter('signings', null);
         if ($foreign_singings) {
             if (is_array($foreign_singings)) {
                 $offsets = ApiTokenOffsetTable::getInstance()->fetchOffsetsByToken($token);
                 $change = false;
                 foreach ($foreign_singings as $country_code => $country_addnum) {
                     if (preg_match('/^[a-z]{2}$/i', $country_code) && preg_match('/^-?[0-9]+$/i', $country_addnum)) {
                         $country_code = strtoupper($country_code);
                         if ($offsets->contains($country_code)) {
                             $offset = $offsets->get($country_code);
                             /* @var $offset ApiTokenOffset */
                             if ($offset->getAddnum() != $country_addnum) {
                                 $offset->setAddnum($country_addnum);
                                 $change = true;
                             }
                         } else {
                             $offset = new ApiTokenOffset();
                             $offset->setApiToken($token);
                             $offset->setCountry($country_code);
                             $offset->setAddnum($country_addnum);
                             $change = true;
                             $offset->save();
                         }
                     }
                 }
                 if ($change) {
                     $offsets->save();
                     $refresh = true;
                 }
             } else {
                 return $this->renderJson(array('status' => 'error', 'message' => 'wrong format'), $callback);
             }
         }
     }
     // widget filter: collect desired widget ids (comma separated list)
     $widget_ids = $request->getParameter('widgets', '');
     if ($widget_ids) {
         if ($widget_ids === true || $widget_ids === 'true' || $widget_ids === 'TRUE') {
             $widget_ids = true;
         } else {
             $widget_ids = array_filter(array_unique(array_map('intval', explode(',', $widget_ids))));
         }
     } else {
         $widget_ids = null;
     }
     // prepare date range filter
     $min_date = $request->getParameter('from_via_policat', null);
     $max_date = $request->getParameter('to_via_policat', null);
     $with_date = $min_date !== null || $max_date !== null;
     if ($min_date !== null && $max_date !== null && $min_date > $max_date) {
         $t = $min_date;
         $min_date = $max_date;
         $max_date = $t;
     }
     $min_date = (int) $min_date;
     $max_date = (int) $max_date;
     $segregate = $request->getParameter('segregate');
     $by_countries = $segregate === 'countries';
     $data = array('action_id' => (int) $action_id);
     //    // as per customer requests, we only add the addnum stuff when we are NOT filtering by widget(s)
     //    if (empty($widgets) && !$countryFilter && !$min_date && !$max_date) {
     //      $data['signings'] += $add_num;
     //    }
     if ($widget_ids) {
         foreach (WidgetTable::getInstance()->fetchIdsByPetition($petition) as $widget_id) {
             if ($widget_ids === true || is_array($widget_ids) && in_array($widget_id, $widget_ids)) {
                 if (!array_key_exists('widgets', $data)) {
                     $data['widgets'] = array();
                     $data['widget_first_signing'] = array();
                     $data['widget_last_signing'] = array();
                 }
                 if ($by_countries) {
                     $widget_data = $signings_table->countByWidgetCountries($widget_id, $min_date, $max_date, $timeToLive, $refresh);
                 } else {
                     $widget_data = $signings_table->countByWidget($widget_id, $min_date, $max_date, $timeToLive, $refresh);
                 }
                 $data['widgets'][(int) $widget_id] = $widget_data;
                 if ($widget_data) {
                     $widget_min_max = $signings_table->fetchSigningDateRange(array_keys($data['widgets']), $min_date, $max_date, $timeToLive, $refresh);
                     if ($widget_min_max) {
                         $data['widget_first_signing'][(int) $widget_id] = strtotime($widget_min_max['min_created']);
                         $data['widget_last_signing'][(int) $widget_id] = strtotime($widget_min_max['max_created']);
                     }
                 }
                 $data['widgets_first_signing'] = min($data['widget_first_signing']);
                 $data['widgets_last_signing'] = max($data['widget_last_signing']);
             }
         }
     }
     if ($by_countries) {
         $data['signings_via_policat'] = $signings_table->countByPetitionCountries($action_id, $min_date, $max_date, $timeToLive, $refresh);
     } else {
         $data['signings_via_policat'] = $signings_table->countByPetition($action_id, $min_date, $max_date, $timeToLive, $refresh);
     }
     if ($data['signings_via_policat']) {
         $action_min_max = $signings_table->fetchSigningDateRangeByPetition($action_id, $min_date, $max_date, $timeToLive, $refresh);
         if ($action_min_max) {
             $data['policat_first_signing'] = strtotime($action_min_max['min_created']);
             $data['policat_last_signing'] = strtotime($action_min_max['max_created']);
         }
     }
     if ($by_countries) {
         $data['signings_via_api'] = $token_table->sumOffsetsCountry($action_id, $timeToLive, $refresh);
     } else {
         $data['signings_via_api'] = $token_table->sumOffsets($action_id, $timeToLive, $refresh);
     }
     $data['manual_counter_tweak'] = (int) $petition->getAddNum();
     if (!$with_date) {
         if ($by_countries) {
             $total = array();
             foreach ($data['signings_via_policat'] as $country => $num) {
                 $total[$country] = $num;
             }
             if (array_key_exists('unknown', $total)) {
                 $total['unknown'] = 0;
             }
             $total['unknown'] += $data['manual_counter_tweak'];
             foreach ($data['signings_via_api'] as $country => $num) {
                 if (array_key_exists($country, $total)) {
                     $total[$country] += $num;
                 } else {
                     $total[$country] = $num;
                 }
             }
         } else {
             $total = $data['signings_via_api'] + $data['signings_via_policat'] + $data['manual_counter_tweak'];
         }
         $data['signings_total'] = $total;
     }
     $response->addCacheControlHttpHeader('public');
     $response->addCacheControlHttpHeader('max-age', 60);
     return $this->renderJson($data, $callback);
 }
Ejemplo n.º 11
0
 private function getPetitions()
 {
     return PetitionTable::getInstance()->createQuery('p')->where('p.status = ?', Petition::STATUS_ACTIVE)->andWhere('p.homepage = 1')->leftJoin('p.PetitionText pt')->andWhere('pt.status = ?', PetitionText::STATUS_ACTIVE)->andWhere('p.language_id = pt.language_id')->leftJoin('pt.DefaultWidget w')->andWhere('w.status = ?', Widget::STATUS_ACTIVE)->select('p.name, p.object_version, p.kind, p.language_id, p.read_more_url, pt.id, pt.object_version, pt.title, pt.target, pt.body, pt.footer, pt.email_subject, pt.email_body, w.id, w.object_version, w.title, w.target, w.intro, w.footer, w.email_subject, w.email_body')->limit(5)->orderBy('p.created_at DESC, p.id ASC')->execute(array(), Doctrine_Core::HYDRATE_ARRAY);
 }
Ejemplo n.º 12
0
 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;
 }
Ejemplo n.º 13
0
 public function executeFeature()
 {
     $title = trim(StoreTable::value(StoreTable::PORTAL_HOME_TITLE, ''));
     $this->title = $title ? $title : 'Featured action';
     $markup = trim(StoreTable::value(StoreTable::PORTAL_HOME_MARKUP, ''));
     if ($markup) {
         $markup = preg_replace_callback('/#KEYVISUAL-(\\d+)#/', array($this, 'getKeyvisualUrl'), $markup);
         $markup = preg_replace('/#WIDGET-(\\d+)#/', 'PasjhkX\\1KmsownedS', $markup);
         // prevent markdown messing up widget
         $markup = UtilMarkdown::transform($markup, true, true);
         $this->markup = preg_replace_callback('/PasjhkX(\\d+)KmsownedS/', array('UtilWidget', 'renderWidget'), $markup);
         return;
     }
     $id = PetitionTable::getInstance()->createQuery('p')->where('p.status = ?', Petition::STATUS_ACTIVE)->andWhere('p.homepage = 1')->leftJoin('p.Campaign c')->andWhere('c.status = ?', CampaignTable::STATUS_ACTIVE)->leftJoin('p.PetitionText pt')->andWhere('pt.status = ?', PetitionText::STATUS_ACTIVE)->andWhere('pt.language_id = ?', 'en')->leftJoin('pt.DefaultWidget w')->andWhere('w.status = ?', Widget::STATUS_ACTIVE)->select('p.id')->addSelect('(SELECT count(z.id) FROM PetitionSigning z WHERE DATE_SUB(NOW(),INTERVAL 1 DAY) <= z.created_at  and z.petition_id = p.id and z.status = ' . PetitionSigning::STATUS_VERIFIED . ') as signings24')->limit(1)->orderBy('signings24 DESC, p.id ASC')->fetchArray();
     if ($id) {
         $id = $id[0]['id'];
         $petition = PetitionTable::getInstance()->createQuery('p')->where('p.id = ?', $id)->andWhere('p.status = ?', Petition::STATUS_ACTIVE)->andWhere('p.homepage = 1')->leftJoin('p.Campaign c')->andWhere('c.status = ?', CampaignTable::STATUS_ACTIVE)->leftJoin('p.PetitionText pt')->andWhere('pt.status = ?', PetitionText::STATUS_ACTIVE)->andWhere('pt.language_id = ?', 'en')->leftJoin('pt.DefaultWidget w')->andWhere('w.status = ?', Widget::STATUS_ACTIVE)->select('p.name, p.object_version, p.kind, p.language_id, p.twitter_tags, p.key_visual, p.read_more_url, pt.id, pt.object_version, pt.title, pt.target, pt.body, pt.footer, pt.email_subject, pt.email_body, w.id, w.object_version, w.title, w.target, w.intro, w.footer, w.email_subject, w.email_body, w.stylings')->fetchOne();
         if ($petition) {
             /* @var $petition Petition */
             $text = $petition['PetitionText'][0];
             $widget = $text['DefaultWidget'];
             $url = $this->generateUrl('sign_hp', array('id' => $widget['id'], 'hash' => $widget->getLastHash(true)), true);
             $this->count = $petition->getCount(60);
             $this->target = $this->count . '-' . Petition::calcTarget($this->count, $petition->getTargetNum());
             $this->widget_id = $widget['id'];
             $this->stylings = json_decode($widget->getStylings(), true);
             $this->stylings['type'] = 'embed';
             $this->stylings['url'] = $url;
             $this->stylings['width'] = 'auto';
         }
     }
 }
 protected function execute($arguments = array(), $options = array())
 {
     // initialize the database connection
     $databaseManager = new sfDatabaseManager($this->configuration);
     $connection = $databaseManager->getDatabase($options['connection'])->getConnection();
     $filename = $arguments['filename'];
     $petition_id = $arguments['petition_id'];
     $search_table = PetitionSigningSearchTable::getInstance();
     $petition = PetitionTable::getInstance()->findById($petition_id, true);
     if (empty($petition)) {
         echo "Petition not found.\n";
         return;
     }
     $campaign = $petition->getCampaign();
     $formfields = $petition->getFormfields();
     $formfields[] = Petition::FIELD_REF;
     $table = Doctrine_Core::getTable('PetitionSigning');
     printf("Campaign: %s\nPetition: %s\n", $campaign['name'], $petition['name']);
     $first_id = false;
     $first_line = null;
     if (($handle = @fopen($filename, "r")) !== false) {
         $con = $table->getConnection();
         $con->beginTransaction();
         echo "Begin transaction\n";
         try {
             $emails = array();
             $i = 0;
             while (($data = fgetcsv($handle, 1000, ",")) !== false) {
                 $i++;
                 if ($i % 100 == 0) {
                     echo "{$i}\n";
                 }
                 //if ($i > 300) break;
                 if (!is_array($first_line)) {
                     $first_line = $data;
                 } else {
                     $line = array_combine($first_line, $data);
                     $signing = new PetitionSigning();
                     $signing->setPetitionId($petition_id);
                     $signing->setStatus(PetitionSigning::STATUS_VERIFIED);
                     foreach ($formfields as $formfield) {
                         switch ($formfield) {
                             //                case 'created_at':
                             //                  $signing->setCreatedAt($line[$formfield]);
                             //                  break;
                             //                case 'updated_at':
                             //                  $signing->setUpdatedAt($line[$formfield]);
                             //                  break;
                             case Petition::FIELD_FULLNAME:
                                 $fullname = array();
                                 if (isset($line[Petition::FIELD_FIRSTNAME])) {
                                     $fullname[] = trim($line[Petition::FIELD_FIRSTNAME]);
                                 }
                                 if (isset($line[Petition::FIELD_LASTNAME])) {
                                     $fullname[] = trim($line[Petition::FIELD_LASTNAME]);
                                 }
                                 if (isset($line[Petition::FIELD_FULLNAME])) {
                                     $fullname[] = trim($line[Petition::FIELD_FULLNAME]);
                                 }
                                 $fullname = join(' ', $fullname);
                                 $signing->setField(Petition::FIELD_FULLNAME, $fullname);
                                 break;
                             case Petition::FIELD_SUBSCRIBE:
                                 if (isset($line[Petition::FIELD_SUBSCRIBE]) && $line[Petition::FIELD_SUBSCRIBE] == '1') {
                                     $signing->setField(Petition::FIELD_SUBSCRIBE, array('yes'));
                                 } else {
                                     $signing->setField(Petition::FIELD_SUBSCRIBE, array());
                                 }
                                 break;
                             default:
                                 if (isset($line[$formfield])) {
                                     $signing->setField($formfield, $line[$formfield]);
                                 }
                         }
                     }
                     $signing->save();
                     $search_table->savePetitionSigning($signing, false);
                     if ($first_id === false) {
                         $first_id = $signing->getId();
                     }
                     $email = $signing->getField(Petition::FIELD_EMAIL);
                     if (is_string($email)) {
                         $emails[] = trim($email);
                     }
                     $signing->free();
                 }
             }
             echo "checking duplicates\n";
             while (count($emails) > 0) {
                 $i = 0;
                 $where_param = array();
                 $where = array();
                 while ($i++ < 100) {
                     $email = array_shift($emails);
                     if (empty($email)) {
                         $break;
                     }
                     $where_param[] = $email;
                     $where[] = '(LOWER(ps.email) = LOWER(?))';
                 }
                 echo count($emails) . "\n";
                 $duplicates = $table->createQuery('ps')->where('ps.petition_id = ?', $petition_id)->andWhere('ps.id < ?', $first_id)->andWhere(join(' OR ', $where), $where_param)->execute();
                 foreach ($duplicates as $duplicate) {
                     $duplicate->setStatus(PetitionSigning::STATUS_DUPLICATE);
                 }
                 $duplicates->save();
                 $duplicates->free();
             }
             echo "Commit transaction";
             $con->commit();
             echo ".\n";
         } catch (Exception $e) {
             $con->rollback();
             echo "DB error. (rollback)\n";
         }
         fclose($handle);
     } else {
         echo "File error.\n";
         return;
     }
 }
 function filter(Doctrine_Query $query)
 {
     return PetitionTable::getInstance()->filter($query, $this);
 }
Ejemplo n.º 16
0
 public function executeEdit(sfWebRequest $request)
 {
     $route_params = $this->getRoute()->getParameters();
     if (isset($route_params['new'])) {
         // CREATE FORM OR REDIRECT TO CREATE FORM
         if (!$request->isMethod('post')) {
             return $this->redirect($this->generateUrl('dashboard'));
         }
         $petition = PetitionTable::getInstance()->findById($request->getParameter('id', $this->userIsAdmin()));
         /* @var $petition Petition */
         if (!$petition) {
             return $this->notFound();
         }
         if (!$this->getGuardUser()->isPetitionMember($petition, true)) {
             return $this->noAccess();
         }
         $widget = new Widget();
         $widget->setPetition($petition);
         $widget->setCampaignId($petition->getCampaignId());
         $widget->setUser($this->getGuardUser());
         $this->ajax()->setAlertTarget('#new_widget', 'after');
         $lang = $request->getPostParameter('lang');
         if (!$lang || !is_numeric($lang)) {
             return $this->ajax()->alert('Please select a language.')->render();
         }
         $pt = PetitionTextTable::getInstance()->find($lang);
         /* @var $pt PetitionText */
         if (!$pt || $pt->getPetitionId() != $petition->getId()) {
             return $this->notFound();
         }
         if ($pt->getStatus() != PetitionText::STATUS_ACTIVE) {
             return $this->ajax()->alert('Translation not active', 'Error')->render();
         }
         $widget->setPetitionText($pt);
         $this->form = new EditWidgetForm($widget);
         $this->lang = $pt->getId();
         if (!$request->getPostParameter($this->form->getName()) && !$request->getPostParameter('page')) {
             return $this->ajax()->redirectPostRoute('widget_create', array('id' => $petition->getId()), array('page' => 1, 'lang' => $pt->getId()))->render();
         }
     } else {
         // EDIT FORM
         $widget = WidgetTable::getInstance()->find($request->getParameter('id'));
         /* @var $widget Widget */
         if (!$widget || !($this->getGuardUser()->isPetitionMember($widget->getPetition(), true) || $widget->getUserId() == $this->getGuardUser()->getId())) {
             return $this->noAccess();
         }
         if (!$this->userIsAdmin() && ($widget->getPetition()->getStatus() == Petition::STATUS_DELETED || $widget->getCampaign()->getStatus() == CampaignTable::STATUS_DELETED)) {
             return $this->notFound();
         }
         $this->form = new EditWidgetForm($widget);
     }
     $this->petition = $widget->getPetition();
     if ($request->isMethod('post') && !$request->getPostParameter('page')) {
         $this->form->bind($request->getPostParameter($this->form->getName()));
         if ($this->form->isValid()) {
             $this->form->save();
             if ($widget->getStatus() == Widget::STATUS_ACTIVE && !$widget->getPetitionText()->getWidgetId()) {
                 $widget->getPetitionText()->setDefaultWidget($widget);
                 $widget->getPetitionText()->save();
             }
             if ($this->getGuardUser()->isPetitionMember($widget->getPetition(), true)) {
                 return $this->ajax()->redirectRotue('petition_widgets', array('id' => $this->petition->getId()))->render();
             } else {
                 return $this->ajax()->redirectRotue('widget_index')->render();
             }
         } else {
             return $this->ajax()->form($this->form)->render();
         }
     }
     $this->includeMarkdown();
     $this->includeHighlight();
 }
Ejemplo n.º 17
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;
 }