function osc_runAlert($type = null, $last_exec = null) { if (!in_array($type, array('HOURLY', 'DAILY', 'WEEKLY', 'INSTANT'))) { return; } if ($last_exec == null) { $cron = Cron::newInstance()->getCronByType($type); if (is_array($cron)) { $last_exec = $cron['d_last_exec']; } else { $last_exec = '0000-00-00 00:00:00'; } } $internal_name = 'alert_email_hourly'; switch ($type) { case 'HOURLY': $internal_name = 'alert_email_hourly'; break; case 'DAILY': $internal_name = 'alert_email_daily'; break; case 'WEEKLY': $internal_name = 'alert_email_weekly'; break; case 'INSTANT': $internal_name = 'alert_email_instant'; break; } $active = TRUE; $searches = Alerts::newInstance()->findByTypeGroup($type, $active); foreach ($searches as $s_search) { // Get if there're new ads on this search $json = $s_search['s_search']; $array_conditions = (array) json_decode($json); $new_search = Search::newInstance(); $new_search->setJsonAlert($array_conditions); $new_search->addConditions(sprintf(" %st_item.dt_pub_date > '%s' ", DB_TABLE_PREFIX, $last_exec)); $items = $new_search->doSearch(); $totalItems = $new_search->count(); if (count($items) > 0) { // If we have new items from last check // Catch the user subscribed to this search $users = Alerts::newInstance()->findUsersBySearchAndType($s_search['s_search'], $type, $active); if (count($users) > 0) { $ads = ''; foreach ($items as $item) { $ads .= '<a href="' . osc_item_url_ns($item['pk_i_id']) . '">' . $item['s_title'] . '</a><br/>'; } foreach ($users as $user) { osc_run_hook('hook_' . $internal_name, $user, $ads, $s_search, $items, $totalItems); AlertsStats::newInstance()->increase(date('Y-m-d')); } } } } }
$next += 24 * 3600; } $next_text = date('Y-m-d H:i:s', $next); Cron::newInstance()->update(array('d_last_exec' => $now_text, 'd_next_exec' => $next_text), array('e_type' => 'DAILY')); } else { //too early for crons } } // WEEKLY CRONS $crons = Cron::newInstance()->getCronByType('WEEKLY'); if (isset($crons[0])) { $cron = $crons[0]; unset($crons); $now_text = date('Y-m-d H:i:s'); $now = strtotime($now_text); $next = strtotime($cron['d_next_exec']); if ($now - $next >= -10) { //(7*24*3600-10)) { // executing HOURLY crons include_once "cron.weekly.php"; // update the database while ($next <= $now) { $next += 7 * 24 * 3600; } $next_text = date('Y-m-d H:i:s', $next); Cron::newInstance()->update(array('d_last_exec' => $now_text, 'd_next_exec' => $next_text), array('e_type' => 'WEEKLY')); } else { //too early for crons } } osc_run_hook('cron');
function osc_runAlert($type = null) { if ($type == null) { return; } $internal_name = 'alert_email_hourly'; switch ($type) { case 'HOURLY': $internal_name = 'alert_email_hourly'; break; case 'DAILY': $internal_name = 'alert_email_daily'; break; case 'WEEKLY': $internal_name = 'alert_email_weekly'; break; case 'INSTANT': $internal_name = 'alert_email_instant'; break; } $searches = Alerts::newInstance()->getAlertsByTypeGroup($type); foreach ($searches as $s_search) { $a_search = Search::newInstance(); // Get if there're new ads on this search $a_search = osc_unserialize(base64_decode($s_search['s_search'])); $crons = Cron::newInstance()->getCronByType($type); if (isset($crons[0])) { $last_exec = $crons[0]['d_last_exec']; } else { $last_exec = '0000-00-00 00:00:00'; } $a_search->addConditions(sprintf(" %st_item.dt_pub_date > '%s' ", DB_TABLE_PREFIX, $last_exec)); $totalItems = $a_search->count(); $items = $a_search->doSearch(); if (count($items) > 0) { //If we have new items from last check //Catch the user subscribed to this search $users = Alerts::newInstance()->getUsersBySearchAndType($s_search['s_search'], $type); if (count($users) > 0) { $prefLocale = osc_language(); $page = Page::newInstance()->findByInternalName($internal_name); $page = Page::newInstance()->findByInternalName($internal_name); $page_description = $page['locale']; $_title = $page_description[$prefLocale]['s_title']; $_body = $page_description[$prefLocale]['s_text']; $ads = ""; foreach ($items as $item) { $ads .= '<a href="' . osc_item_url_ns($item['pk_i_id']) . '">' . $item['s_title'] . '</a><br/>'; } foreach ($users as $user) { if ($user['fk_i_user_id'] != 0) { $user = User::newInstance()->findByPrimaryKey($user['fk_i_user_id']); } else { $user['s_name'] = $user['s_email']; } $unsub_link = osc_user_unsubscribe_alert_url($user['s_email'], $s_search['s_search']); //osc_create_url(array('file' => 'user', 'action' => 'unsub_alert', 'email' => $user['s_email'], 'alert' => $s_search['s_search'])) ; $unsub_link = '<a href="' . $unsub_link . '">unsubscribe alert</a>'; $words = array(); $words[] = array('{USER_NAME}', '{USER_EMAIL}', '{ADS}', '{UNSUB_LINK}'); $words[] = array($user['s_name'], $user['s_email'], $ads, $unsub_link); $title = osc_mailBeauty($_title, $words); $body = osc_mailBeauty($_body, $words); $params = array('subject' => $title, 'to' => $user['s_email'], 'to_name' => $user['s_name'], 'body' => $body, 'alt_body' => $body); osc_sendMail($params); } } } } }