private function _countNAlertsForUser($con = null) { $c = new Criteria($con); $c->add(OppAlertUserPeer::USER_ID, $this->getUserId()); return OppAlertUserPeer::doCount($c); }
/** * fetch and return user alerts data structure * * @param string $user * @param string $max_results * @param string $force_last_alert - date to test as if it was last alerted on... * @return array of hashes ('term' => $term, 'results' => $results) * @author Guglielmo Celata */ public static function getUserAlerts($user, $max_results = 10, $last_alert = null) { // arguments validation if (!$user instanceof OppUser) { throw new Exception("first argument must be of type user"); } // if last alert is not passed, then fetch it, as not to break compatibility if (is_null($last_alert)) { $last_alert = $user->getLastAlertedAt("%Y-%m-%dT%H:%M:%SZ"); } // loop to build static data-structure (self::$user_alerts) $user_alerts = array(); $alert = OppAlertUserPeer::fetchUserAlerts($user); foreach ($alert as $alert) { $alert_term = $alert->getOppAlertTerm()->getTerm(); if ($last_alert) { $fields_constraints = 'created_at_dt:' . sprintf("[%s TO NOW]", $last_alert); } else { $fields_constraints = 'created_at_dt:' . sprintf("[NOW-%dDAYS/SECOND TO NOW]", sfConfig::get('app_alert_default_days_back', 9)); } $type_filters_s = OppAlertTermPeer::get_filters_labels($alert->getTypeFilters()); if (!is_null($type_filters_s) && $type_filters_s != '') { $type_filters = explode("|", $alert->getTypeFilters()); } else { $type_filters = array(); } if (count($type_filters)) { $type_constraints = ""; foreach ($type_filters as $cnt => $type_filter) { $type_constraint = ""; switch ($type_filter) { case 'politici': $type_constraint = "(+sfl_model:OppPolitico)"; break; case 'argomenti': $type_constraint = "(+sfl_model:Tag)"; break; case 'emendamenti': $type_constraint = "(+sfl_model:OppEmendamento)"; break; case 'votazioni': $type_constraint = "(+sfl_model:OppVotazione)"; break; case 'resoconti': $type_constraint = "(+sfl_model:OppResoconto)"; break; case 'disegni': case 'decreti': case 'decrleg': case 'mozioni': case 'interpellanze': case 'interrogazioni': case 'risoluzioni': case 'odg': case 'comunicazionigoverno': case 'audizioni': $type_constraint = "(+sfl_model:(OppAtto OppDocumento) +tipo_atto_s:{$type_filter})"; break; } $type_constraints .= ($cnt > 0 ? ' OR ' : '') . $type_constraint; } if ($type_constraints != "") { $fields_constraints .= $fields_constraints != '' ? " AND ({$type_constraints})" : $type_constraints; } } $alert_results = deppOppSolr::getSfResults($alert_term, 0, $max_results, $fields_constraints, true); $user_alert = array('term' => $alert_term, 'type_filters' => $type_filters_s, 'results' => $alert_results); $user_alerts[] = $user_alert; } return $user_alerts; }
/** * fetch today's alert regarding terms monitored by the user * * @param string $user - OppUser object * @return void * @author Guglielmo Celata */ function opp_test_single_user_alerts($user, $last_alert) { $start_time = microtime(true); $df = new sfDateFormat('it_IT'); echo pakeColor::colorize(sprintf("Processo l'utente %s (%s)...\n", $user, $user->getToken()), array('bold' => true)); echo pakeColor::colorize(sprintf("Ultimo alert: %s\n", $last_alert)); $user_alerts = oppAlertingTools::getUserAlerts($user, sfConfig::get('app_alert_max_results', 50), $last_alert); $n_alerts = OppAlertUserPeer::countUserAlerts($user); $n_total_notifications = oppAlertingTools::countTotalAlertsNotifications($user_alerts); if ($n_total_notifications > 0) { $user_alerts_expanded = join(", ", array_map('extractTerm', array_slice($user_alerts, 0, 5))) . ($n_alerts > 5 ? ',...' : ''); echo pakeColor::colorize(sprintf("%d %s per %s (%s)\n\n", $n_total_notifications, $n_total_notifications == 1 ? 'avviso' : 'avvisi', $n_alerts == 1 ? 'un termine' : $n_alerts . ' termini', $user_alerts_expanded)); echo pakeTaskUserAlerts($user_alerts); } else { echo pakeColor::colorize(sprintf(" (nessun avviso)\n\n")); } $execution_time = microtime(true) - $start_time; }
public function executeSendAlerts() { $user_id = $this->getRequestParameter('user_id'); $last_alert = $this->getRequestParameter('last_alert'); $this->user = OppUserPeer::retrieveByPK($user_id); $this->sf_site_url = sfConfig::get('sf_site_url', 'openparlamento'); $this->user_token = $this->user->getToken(); $this->user_alerts = oppAlertingTools::getUserAlerts($this->user, sfConfig::get('app_alert_max_results', 50), $last_alert); $this->n_alerts = OppAlertUserPeer::countUserAlerts($this->user); $this->n_total_notifications = oppAlertingTools::countTotalAlertsNotifications($this->user_alerts); // do not send email if no news if ($this->n_alerts == 0) { return sfView::NONE; } // mail class initialization $mail = new sfMail(); $mail->setCharset('utf-8'); $mail->setContentType('text/html'); // definition of the required parameters if ($this->user->isAdhoc()) { $mail->setSender(sfConfig::get('app_newsletter_pd_sender_address', '*****@*****.**'), sfConfig::get('app_newsletter_pd_from_tag', 'political desk')); $mail->setFrom(sfConfig::get('app_newsletter_pd_from_address', '*****@*****.**'), sfConfig::get('app_newsletter_pd_from_tag', 'political desk')); } else { $mail->setSender(sfConfig::get('app_newsletter_sender_address', '*****@*****.**'), sfConfig::get('app_newsletter_from_tag', 'openparlamento')); $mail->setFrom(sfConfig::get('app_newsletter_from_address', '*****@*****.**'), sfConfig::get('app_newsletter_from_tag', 'openparlamento')); } $mail->addAddress($this->user->getEmail()); // invia tutte le mail in BCC a un indirizzo di servizio if ($this->user->isAdhoc()) { $mail->addBcc('*****@*****.**'); } // alert utente espansi per il subject della mail $user_alerts_expanded = join(", ", array_map('extractTerm', array_slice($this->user_alerts, 0, 3))) . ($this->n_alerts > 3 ? ',...' : ''); $subj = sprintf("%d %s per %s che stai monitorando (%s) ", $this->n_total_notifications, $this->n_total_notifications == 1 ? 'avviso' : 'avvisi', $this->n_alerts == 1 ? 'il termine' : 'i ' . $this->n_alerts . ' termini', $user_alerts_expanded); $mail->setSubject($subj); $this->mail = $mail; }
<?php if ($sf_flash->has('notice')) { ?> <div class="flash-messages" style="background-color: #afa"> <?php echo $sf_flash->get('notice'); ?> </div> <?php } ?> <?php if ($sf_user->isAuthenticated() && ($sf_user->hasCredential('amministratore') || $sf_user->hasCredential('adhoc')) && !OppAlertUserPeer::hasAlert($query, OppUserPeer::retrieveByPK($sf_user->getId()), $type_filters)) { ?> <h4 style="margin-left: 0.5em"> <?php echo link_to("avvisami quando questa espressione viene usata alla Camera o al Senato" . ($type_filters != '' ? ", con questi filtri: {$type_filters_label}" : ""), 'monitoring/addAlert?term=' . str_replace("/", "|", $query) . "&type_filters={$type_filters}"); ?> </h4> <?php }