1 | <?php |
---|
2 | |
---|
3 | class sendAlertTask extends sfBaseTask |
---|
4 | { |
---|
5 | protected function configure() |
---|
6 | { |
---|
7 | $this->namespace = 'send'; |
---|
8 | $this->name = 'Alert'; |
---|
9 | $this->briefDescription = 'send alerts'; |
---|
10 | $this->addOption('env', null, sfCommandOption::PARAMETER_OPTIONAL, 'Changes the environment this task is run in', 'prod'); |
---|
11 | $this->addOption('app', null, sfCommandOption::PARAMETER_OPTIONAL, 'Changes the environment this task is run in', 'frontend'); |
---|
12 | } |
---|
13 | |
---|
14 | protected function execute($arguments = array(), $options = array()) |
---|
15 | { |
---|
16 | $this->configuration = sfProjectConfiguration::getApplicationConfiguration($options['app'], $options['dev'], true); |
---|
17 | $manager = new sfDatabaseManager($this->configuration); |
---|
18 | $context = sfContext::createInstance($this->configuration); |
---|
19 | $this->configuration->loadHelpers(array('Partial', 'Url')); |
---|
20 | |
---|
21 | $solr = new SolrConnector(); |
---|
22 | $query = Doctrine::getTable('Alerte')->createQuery('a')->where('next_mail < NOW()'); |
---|
23 | foreach($query->execute() as $alerte) { |
---|
24 | $date = strtotime(preg_replace('/ /', 'T', $alerte->last_mail)."Z")-3600*2; |
---|
25 | $query = $alerte->query." date:[".date('Y-m-d', $date).'T'.date('H:i:s', $date)."Z TO ".date('Y-m-d').'T'.date('H:i:s')."Z]"; |
---|
26 | $results = $solr->search($query, array('sort' => 'date desc', 'hl' => 'yes', 'hl.fragsize'=>500)); |
---|
27 | echo "$query\n"; |
---|
28 | if (! $results['response']['numFound']) |
---|
29 | continue; |
---|
30 | echo "sending mail to : ".$alerte->email."\n"; |
---|
31 | $message = $this->getMailer()->compose(array('no-reply@nosdeputes.fr' => 'Regards Citoyens (ne pas répondre)'), |
---|
32 | $alerte->email, |
---|
33 | '[NosDeputes.fr] Alerte - '.$alerte->titre); |
---|
34 | echo $alerte->titre."\n"; |
---|
35 | $text = get_partial('mail/sendAlerteTxt', array('alerte' => $alerte, 'results' => $results['response'])); |
---|
36 | $message->setBody($text, 'text/plain'); |
---|
37 | try { |
---|
38 | $this->getMailer()->send($message); |
---|
39 | $alerte->last_mail = preg_replace('/T/', ' ', preg_replace('/Z/', '', $results['response']['docs'][$results['response']['numFound'] -1]['date'])); |
---|
40 | $alerte->save(); |
---|
41 | }catch(Exception $e) { |
---|
42 | echo "ERROR: mail could not be sent ($text)\n"; |
---|
43 | } |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | } |
---|