Пример #1
0
 public function getWidgetStatus($widget_id)
 {
     if (!is_numeric($widget_id)) {
         return null;
     }
     $cache = $this->getContext()->getViewCacheManager()->getCache();
     if ($cache instanceof sfTagCache) {
         $cache = $cache->getCache();
     }
     $key = 'api_widget_status_' . (int) $widget_id;
     $data = $cache->get($key, null);
     if ($data !== null) {
         $data['timeout'] = $cache->getTimeout($key);
         return $data;
     }
     $data = WidgetTable::getInstance()->fetchStatus($widget_id);
     if ($data && is_array($data)) {
         $data = reset($data);
     }
     if (!is_array($data)) {
         $data = array();
     }
     $cache->set($key, $data, 600);
     $data['timeout'] = $cache->getTimeout($key);
     return $data;
 }
Пример #2
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();
 }
Пример #3
0
 public function executeList()
 {
     $page = isset($this->page) ? $this->page : 1;
     if (isset($this->petition)) {
         $this->form = new FilterWidgetForm();
         $this->form->bindSelf('p' . $this->petition->getId());
         $this->widgets = new policatPager(WidgetTable::getInstance()->queryByPetition($this->petition), $page, 'widget_pager_petition', array('id' => $this->petition->getId()), true, 20, $this->form);
     } else {
         $this->form = new FilterWidgetForm(array(), array(FilterWidgetForm::WITH_CAMPAIGN => true, FilterWidgetForm::USER => $this->getGuardUser()));
         $this->form->bindSelf('all');
         $this->widgets = new policatPager(WidgetTable::getInstance()->queryByUser($this->getGuardUser()), $page, 'widget_pager', array(), true, 20, $this->form);
     }
     $this->csrf_token = UtilCSRF::gen('widget_data_owner');
     $this->csrf_token_revoke = UtilCSRF::gen('widget_revoke_data_owner');
 }
Пример #4
0
 public static function renderWidget($params)
 {
     $widget = WidgetTable::getInstance()->fetch($params[1]);
     if (!$widget || $widget->getStatus() != Widget::STATUS_ACTIVE) {
         return '';
     }
     $context = sfContext::getInstance();
     $petition = $widget->getPetition();
     $url = $context->getRouting()->generate('sign_hp', array('id' => $widget['id'], 'hash' => $widget->getLastHash(true)), true);
     $count = $petition->getCount(60);
     $target = $count . '-' . Petition::calcTarget($count, $petition->getTargetNum());
     $widget_id = $widget['id'];
     $stylings = json_decode($widget->getStylings(), true);
     $stylings['type'] = 'embed';
     $stylings['url'] = $url;
     $stylings['width'] = 'auto';
     $stylings['count'] = number_format($count, 0, '.', ',') . ' ' . 'people so far';
     $stylings['target'] = $target;
     return '<script type="text/javascript">' . self::getInitJS() . self::getAddStyleJS($widget_id, $stylings) . self::getWidgetHereJs($widget_id, false) . '</script>';
 }
Пример #5
0
 protected function fetchWidget()
 {
     $id = $this->getRequest()->getParameter('id');
     if (!is_numeric($id)) {
         return $this->showError('Invalid ID');
     }
     $this->widget = WidgetTable::getInstance()->fetch($id);
     $this->widget->getPetition()->state(Doctrine_Record::STATE_CLEAN);
     // petition can not have changed yet, stupid doctrine
     if (empty($this->widget)) {
         return $this->forward404('No widget found');
     }
     $this->setContentTags($this->widget);
     $this->addContentTags($this->widget->getCampaign());
     $this->addContentTags($this->widget->getPetition());
     $this->addContentTags($this->widget->getPetitionText());
     $donations_paypal_strore = StoreTable::getInstance()->findByKeyCached(StoreTable::DONATIONS_PAYPAL);
     if ($donations_paypal_strore) {
         $this->addContentTags($donations_paypal_strore);
     }
 }
Пример #6
0
 public function executeRevokeData(sfWebRequest $request)
 {
     $this->ajax()->setAlertTarget('#widget_list table', 'after');
     if ($request->getPostParameter('csrf_token') !== UtilCSRF::gen('widget_revoke_data_owner')) {
         return $this->ajax()->alert('CSRF Attack detected, please relogin.', 'Error')->render();
     }
     $id = $request->getPostParameter('id');
     if (!is_numeric($id)) {
         return $this->ajax()->alert('invalid data', 'Error')->render();
     }
     $widget = WidgetTable::getInstance()->find($id);
     /* @var $widget Widget */
     if (!$widget) {
         return $this->ajax()->alert('Widget not found', 'Error')->render();
     }
     if (!$this->userIsAdmin() && ($widget->getCampaign()->getStatus() == CampaignTable::STATUS_DELETED || $widget->getPetition()->getStatus() == Petition::STATUS_DELETED)) {
         return $this->ajax()->alert('Widget not found', 'Error')->render();
     }
     if (!$this->getGuardUser()->isDataOwnerOfCampaign($widget->getPetition()->getCampaign())) {
         return $this->ajax()->alert('You are not Data manager', '')->render();
     }
     if ($widget->getDataOwner() != WidgetTable::DATA_OWNER_YES) {
         return $this->ajax()->alert('This user is not data-owner.', '')->render();
     }
     $widget->setDataOwner(WidgetTable::DATA_OWNER_NO);
     $widget->save();
     return $this->ajax()->alert('Data-owner revoked', '')->render();
 }
Пример #7
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);
 }
 public function getWidgetFilter(Petition $petition)
 {
     $query_orga = WidgetTable::getInstance()->queryByPetition($petition)->andWhere('w.organisation != ""')->andWhere('w.id IN (SELECT DISTINCT ps.widget_id FROM PetitionSigning ps WHERE ps.status = ? AND ps.widget_id = w.id)', PetitionSigning::STATUS_VERIFIED);
     $query_orga->select('DISTINCT ' . $query_orga->getRootAlias() . '.organisation');
     $result_orga = $query_orga->fetchArray();
     $organisations = array();
     foreach ($result_orga as $orga) {
         $organisations[$orga['organisation']] = $orga['organisation'];
     }
     $query_user = WidgetTable::getInstance()->queryByPetition($petition)->andWhere('w.user_id IS NOT NULL')->andWhere('w.id IN (SELECT DISTINCT ps.widget_id FROM PetitionSigning ps WHERE ps.status = ? AND ps.widget_id = w.id)', PetitionSigning::STATUS_VERIFIED);
     $query_user->select('DISTINCT ' . $query_user->getRootAlias() . '.user_id');
     $result_user = $query_user->fetchArray();
     $user_ids = array();
     foreach ($result_user as $user_id) {
         $user_ids[] = $user_id['user_id'];
     }
     $users = array();
     if ($user_ids) {
         $user_objs = sfGuardUserTable::getInstance()->queryAll()->andWhereIn('u.id', $user_ids)->execute();
         foreach ($user_objs as $user) {
             /* @var $user sfGuardUser */
             $users['u' . $user->getId()] = $user->getName();
         }
     }
     $widgets = array();
     $widget_ids = WidgetTable::getInstance()->queryByPetition($petition)->andWhere('w.id IN (SELECT DISTINCT ps.widget_id FROM PetitionSigning ps WHERE ps.status = ? AND ps.widget_id = w.id)', PetitionSigning::STATUS_VERIFIED)->select('w.id')->fetchArray();
     foreach ($widget_ids as $widget) {
         $widgets['w' . $widget['id']] = 'Widget #' . $widget['id'];
     }
     return array('' => array('' => ''), 'Organisations' => $organisations, 'Users' => $users, 'Widgets' => $widgets);
 }
 function filter(Doctrine_Query $query)
 {
     return WidgetTable::getInstance()->filter($query, $this);
 }
Пример #10
0
 /**
  * @param $id
  * @return Widget
  */
 private function getWidget($id)
 {
     $widget = WidgetTable::getInstance()->find($id);
     /* @var $widget Widget */
     $right = $this->rightWidget($widget);
     if ($right) {
         return $widget;
     } else {
         return $right === false ? $this->notFound() : $this->noAccess('You are not Data-owner of this widget.');
     }
 }
Пример #11
0
 public function countWidgets()
 {
     return WidgetTable::getInstance()->countByPetition($this);
 }