public function executeOpen_actions()
 {
     // Javascript vars
     $this->tags = array('policat' => '');
     // UtilRegistry::get('twitter_tags')
     $this->widget_styles = array();
     // Open petitions
     $this->open = array();
     foreach (array(self::HOTTEST => 'Trending', self::LARGEST => 'Popular', self::RECENT => 'New') as $key => $value) {
         $data = $this->getPetitions($key);
         $this->tags[$key] = '';
         foreach ($data as &$petition) {
             $count = PetitionSigningTable::getInstance()->countByPetition($petition['id'], null, null, 60);
             $count += PetitionApiTokenTable::getInstance()->sumOffsets($petition['id'], 60);
             $count += $petition['addnum'];
             $petition['signings'] = $count;
             $text = $petition['PetitionText'][0];
             $widget = $text['DefaultWidget'];
             $style = json_decode($widget['stylings'], true);
             $tags = trim($petition['twitter_tags']);
             if ($tags) {
                 $this->tags[$key] .= ($this->tags[$key] ? ' OR ' : '') . $petition['twitter_tags'];
             }
             if (!isset($this->widget_styles[$widget['id']])) {
                 $this->widget_styles[$widget['id']] = array('width' => $style['width'], 'body_color' => '#818286', 'count' => number_format($petition['signings'], 0, '.', ',') . ' people so far', 'target' => $petition['signings'] . '-' . Petition::calcTarget($petition['signings'], $petition['target_num']), 'url' => $this->getContext()->getRouting()->generate('sign', array('id' => $widget['id'], 'hash' => Widget::calcLastHash($widget['id'], array($petition['object_version'], $widget['object_version'], $text['object_version']))), true));
             }
         }
         $this->open[$key] = array('title' => $value, 'data' => $data);
         //      $this->js['tags'][$key] .= ($this->js['tags'][$key] ? ' OR ' : '') . $this->js['tags']['policat'];
     }
 }
 public function executeData(sfWebRequest $request)
 {
     $token = PetitionApiTokenTable::getInstance()->find($request->getParameter('id'));
     /* @var $token PetitionApiToken */
     if (!$token) {
         return $this->notFound();
     }
     $petition = $token->getPetition();
     if (!$petition->isTicketManager($this->getGuardUser())) {
         return $this->noAccess();
     }
     $offsets = $token->getOffsets();
     return $this->ajax()->remove('#token_data_' . $token->getId())->afterPartial('#token_' . $token->getId(), 'data', array('token' => $token, 'offsets' => $offsets))->render();
 }
 /**
  * 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 getCount($timeToLive = 600, $refresh = false)
 {
     $count = PetitionSigningTable::getInstance()->countByPetition($this, null, null, $timeToLive, $refresh);
     $count += PetitionApiTokenTable::getInstance()->sumOffsets($this, $timeToLive, $refresh);
     $count += $this->getAddnum();
     return $count;
 }