/**
* Fetch news and show them, for each users
*/
function run_opp_test_alerts($task, $args, $options)
{
    static $loaded;
    // load application context
    if (!$loaded) {
        define('SF_ROOT_DIR', sfConfig::get('sf_root_dir'));
        define('SF_APP', 'fe');
        define('SF_ENVIRONMENT', 'task');
        define('SF_DEBUG', true);
        require_once SF_ROOT_DIR . DIRECTORY_SEPARATOR . 'apps' . DIRECTORY_SEPARATOR . SF_APP . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'config.php';
        sfContext::getInstance();
        sfConfig::set('pake', true);
        error_reporting(E_ALL);
        $loaded = true;
    }
    $last_alert = null;
    if (array_key_exists('last-alert', $options)) {
        $last_alert = strftime("%Y-%m-%dT%H:%M:%SZ", strtotime($options['last-alert']));
    }
    // create a solr instance to read solr.yml config
    $solr_instance = deppOppSolr::getInstance();
    sfLoader::loadHelpers(array('Partial', 'sfSolr', 'DeppNews'));
    $start_time = microtime(true);
    echo pakeColor::colorize("Hi, there!\n", array('fg' => 'green', 'bold' => true));
    $c = new Criteria();
    $c->add(OppUserPeer::WANTS_OPP_ALERTS, 1);
    $c->add(OppUserPeer::IS_ACTIVE, 1);
    $c->add(OppUserPeer::N_ALERTS, 0, Criteria::GREATER_THAN);
    if (count($args)) {
        $c->add(OppUserPeer::ID, $args, Criteria::IN);
    }
    $users = OppUserPeer::doSelect($c);
    $n_users = count($users);
    echo pakeColor::colorize("{$n_users} users set alerts. Here are the notifications we would send them.\n", array('fg' => 'green'));
    foreach ($users as $cnt => $user) {
        $last_alert = $user->getLastAlertedAt("%Y-%m-%dT%H:%M:%SZ");
        echo "{$cnt}/{$n_users} ";
        opp_test_single_user_alerts($user, $last_alert);
    }
    $total_time = microtime(true) - $start_time;
    echo pakeColor::colorize('All done! ', array('fg' => 'green', 'bold' => true));
    echo 'Processed ';
    echo pakeColor::colorize(count($users), array('fg' => 'cyan'));
    echo ' users in ';
    echo pakeColor::colorize(sprintf("%f", $total_time), array('fg' => 'cyan'));
    echo " seconds\n";
}
/**
* Fetch news and show them, for each users
*/
function run_opp_test_newsletter($task, $args, $options)
{
    static $loaded;
    // load application context
    if (!$loaded) {
        define('SF_ROOT_DIR', sfConfig::get('sf_root_dir'));
        define('SF_APP', 'fe');
        define('SF_ENVIRONMENT', 'task');
        define('SF_DEBUG', true);
        require_once SF_ROOT_DIR . DIRECTORY_SEPARATOR . 'apps' . DIRECTORY_SEPARATOR . SF_APP . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'config.php';
        sfContext::getInstance();
        sfLoader::loadHelpers(array('Tag', 'Url', 'DeppNews'));
        sfConfig::set('pake', true);
        error_reporting(E_ALL);
        $loaded = true;
    }
    $date = null;
    if (array_key_exists('date', $options)) {
        $date = $options['date'];
    }
    $start_time = microtime(true);
    echo pakeColor::colorize("Hi, there!\n", array('fg' => 'green', 'bold' => true));
    // fetch utenti attivi che monitorano qualcosa e vogliono le news
    $c = new Criteria();
    $c->add(OppUserPeer::WANTS_OPP_NEWS, 1);
    $c->add(OppUserPeer::IS_ACTIVE, 1);
    $c->addJoin(MonitoringPeer::USER_ID, OppUserPeer::ID);
    if (count($args)) {
        $c->add(OppUserPeer::ID, $args, Criteria::IN);
    }
    $c->addGroupByColumn(OppUserPeer::ID);
    $users = OppUserPeer::doSelect($c);
    $n_users = count($users);
    echo pakeColor::colorize("{$n_users} users are monitoring. Here are the news we would send them.\n", array('fg' => 'green'));
    foreach ($users as $cnt => $user) {
        echo "{$cnt}/{$n_users} ";
        opp_test_single_newsletter($user, $date);
    }
    $total_time = microtime(true) - $start_time;
    echo pakeColor::colorize('All done! ', array('fg' => 'red', 'bold' => true));
    echo 'Processed ';
    echo pakeColor::colorize(count($users), array('fg' => 'cyan'));
    echo ' users in ';
    echo pakeColor::colorize(sprintf("%f", $total_time), array('fg' => 'cyan'));
    echo " seconds\n";
}