<?php

class catalyzRelanceTask extends sfPropelBaseTask {
    protected function configure()
    {
        $this->namespace = 'catalyz';
        $this->name = 'relance';
        $this->briefDescription = '';
        $this->detailedDescription = <<<EOF
The [catalyz:import|INFO] task does things.
Call it with:

  [php symfony catalyz:relance|INFO]
EOF;
        $this->addArgument('application', sfCommandArgument::OPTIONAL, 'The application name', 'frontend');
        // add other arguments here
        $this->addOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'af');
        $this->addOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'propel');
        // add other options here
    }

    protected function execute($arguments = array(), $options = array())
    {


        // Database initialization
        $databaseManager = new sfDatabaseManager($this->configuration);
        $connection = Propel::getConnection($options['connection'] ? $options['connection'] : '');

        $configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'af', true);
        sfContext::createInstance($configuration);
    		sfLoader::loadHelpers('Partial');

        $criteria = new Criteria();
        $lists = KataoUserPeer::doSelect($criteria);

        $destinatairesList = array();
        foreach ($lists as $list) {
             $destinatairesList[]= $list->getEmail();
        }

  //  	$destinatairesList = array('ferrere.arnaud@gmail.com','af@catalyz.fr');
    	var_dump($destinatairesList);
    	die();

        try {
            $recipients = new Swift_RecipientList();
            $recipients->addTo($destinatairesList);
    //        $recipients->addBcc('sh@catalyz.fr');

            /*
    		   * Instanciation du mailer et du message
    		*/
            $mailer = new Swift(new Swift_Connection_NativeMail());
            $message = new Swift_Message('Relance', get_partial('katao/relanceEmail'), 'text/html');

            /*
    		   * Envoi
    		*/
            $mailer->send($message, $recipients, 'no-reply@sol-violette.fr');
            $mailer->disconnect();
        }
        catch (Exception $e) {
            // Gestion des erreurs ici
            $mailer->disconnect();
            var_dump($e->getMessage());
            die();
        }
    }
}