Example #1
0
 public function index()
 {
     $this->load->model('tool/ka_tasks');
     $this->model_tool_ka_tasks->logMessage("Task Scheduler: started");
     if (!$this->user->isLogged() && (empty($this->request->get['key']) || $this->request->get['key'] != $this->config->get('ka_ts_run_scheduler_key'))) {
         echo "Task Scheduler: wrong key.";
         $this->model_tool_ka_tasks->logMessage("WARNING: key is not valid or not found");
         return;
     }
     $sql_now = $this->model_tool_ka_tasks->getSqlNow();
     $this->last_scheduler_run = strtotime($this->model_tool_ka_tasks->getConfigHidden('ka_ts_last_scheduler_run'));
     $this->model_tool_ka_tasks->setConfigHidden('ka_ts_last_scheduler_run', $sql_now);
     //
     // STAGE 1: find a task to run
     //
     $task_id = isset($this->request->get['task_id']) ? $this->request->get['task_id'] : 0;
     $this->current_task = $current_task = $this->fetchTask($task_id);
     // scheduler does not have any active tasks or they do not require start now
     //
     if (empty($current_task)) {
         $this->model_tool_ka_tasks->logMessage($msg = "Task Scheduler: " . $this->last_error);
         echo $msg;
         return false;
     }
     $this->model_tool_ka_tasks->logMessage("Task Scheduler: fetched task: taskid = {$current_task['task_id']}, task module {$current_task['module']}");
     //
     // STAGE 2: run the found task
     //
     // mark the task as started
     //
     if ($current_task['run_status'] == 'not_started') {
         $this->db->query("UPDATE " . DB_PREFIX . "ka_tasks SET \n\t\t\t\tfirst_run = NOW(), run_count = 0 \n\t\t\t\tWHERE task_id = '{$current_task['task_id']}'");
         $this->model_tool_ka_tasks->setConfigHidden('ka_ts_last_task_id', $current_task['task_id']);
     }
     // check if the task can realy start
     //
     if (!$this->model_tool_ka_tasks->isSchedulerModel($current_task['module'])) {
         $this->model_tool_ka_tasks->changeTaskStatus($current_task['task_id'], 'not_started');
         $this->model_tool_ka_tasks->logMessage($msg = "WARNING: module {$current_task['module']} cannot be uploaded, it is not compatible with Task Scheduler API");
         $this->log->write($msg);
         return false;
     }
     $this->old_session_data = $this->session->data;
     $session = new Session();
     $this->registry->set('session', $session);
     if (empty($current_task['run_status']) || $current_task['run_status'] == 'not_started') {
         $stat = array();
         $session->data['token'] = md5(mt_rand());
     } elseif ($current_task['run_status'] == 'not_finished') {
         $stat = $current_task['stat'];
         $this->session->data = unserialize($current_task['session_data']);
     } else {
         $this->model_tool_ka_tasks->changeTaskStatus($current_task['task_id'], 'not_started');
         $this->model_tool_ka_tasks->logMessage($e = "ERROR: Unknown run_status ({$current_task['run_status']}). Reset to not_started.");
         trigger_error($e);
         return false;
     }
     $this->load->model($current_task['module']);
     $name = 'model_' . str_replace('/', '_', $current_task['module']);
     if (!is_object($this->{$name})) {
         $this->model_tool_ka_tasks->logMessage($msg = "WARNING: model {$name} cannot be uploaded for unkown reason. Model object does not exist.");
         $this->log->write($msg);
         $this->model_tool_ka_tasks->changeTaskStatus($current_task['task_id'], 'not_started');
         return false;
     }
     // switch the task to 'working' status
     //
     $this->model_tool_ka_tasks->changeTaskStatus($current_task['task_id'], 'working');
     /*
     	supported results:
     		array(
     			'result'       - not_finished/finished;
     			'next_task_id' - optional
     		)
     		
     		string             - not_finished/finished
     		
     	$stat - should be received by reference and statistics results will be returned
     			as a hash array.
     */
     $next_task_id = 0;
     $res = '';
     $run_result = $this->{$name}->runSchedulerOperation($current_task['operation'], $current_task['params'], $stat);
     if (is_array($run_result)) {
         $res = $run_result['result'];
         if (isset($run_result['next_task_id'])) {
             $next_task_id = $run_result['next_task_id'];
         }
     } else {
         $res = $run_result;
     }
     //
     // STAGE 3: save results
     //
     $stat = serialize($stat);
     $complete_count_sql = '';
     if ($res == 'finished') {
         $session_data = '';
         $run_status = 'not_started';
         $complete_count_sql = "complete_count = complete_count + 1,";
     } elseif ($res == 'not_finished') {
         $session_data = serialize($this->session->data);
         $run_status = 'not_finished';
     } else {
         $this->model_tool_ka_tasks->logMessage($e = "ERROR: Unknown function result ({$res}). Stopped.");
         trigger_error($e, E_USER_ERROR);
     }
     $this->session->data = $this->old_session_data;
     $this->db->query("UPDATE " . DB_PREFIX . "ka_tasks\n\t\t\tSET\n\t\t\t\tstat = '" . $this->db->escape($stat) . "',\n\t\t\t\tsession_data = '" . $this->db->escape($session_data) . "',\n\t\t\t\trun_status = '{$run_status}',\n\t\t\t\trun_count = run_count + 1,\n\t\t\t\t{$complete_count_sql}\n\t\t\t\tlast_run = now()\n\t\t\tWHERE\n\t\t\t\ttask_id = '{$current_task['task_id']}'\n\t\t");
     //
     // STAGE 4: redict the page to the next run if required
     //
     $url = "key=" . $this->config->get('ka_ts_run_scheduler_key');
     if (!empty($this->session->data['token'])) {
         $url = $url . '&token=' . $this->session->data['token'];
     }
     if ($res == 'not_finished') {
         $location = $this->url->link('catalog/ka_run_scheduler', $url, 'SSL');
         $run_count = $this->current_task['run_count'] + 1;
         $this->model_tool_ka_tasks->logMessage("Task Scheduler: task is not finished, (run_count: {$run_count}) redirecting...");
         $this->redirect_page($location);
     } elseif ($res == 'finished') {
         $task = $this->model_tool_ka_tasks->getTask($current_task['task_id'], true);
         if ($this->config->get('ka_ts_send_email_on_completion') == 'Y') {
             $ka_mail = new KaMail($this->registry);
             $ka_mail->data['task'] = $task;
             $ka_mail->send($this->config->get('config_email'), $this->config->get('config_email'), $this->language->get('Task is complete'), 'ka_task_complete.tpl');
         }
         $this->model_tool_ka_tasks->logMessage($msg = "Task Scheduler: task (id: {$current_task['task_id']}) is finished.");
         echo $msg . "<br />";
         if (!empty($next_task_id)) {
             $location = $this->url->link('catalog/ka_run_scheduler', $url . '&task_id=' . $next_task_id, 'SSL');
             $this->model_tool_ka_tasks->logMessage("Task Scheduler: Next task called (next_task_id: {$next_task_id}) redirecting...");
             $this->redirect_page($location);
         }
     }
     $this->model_tool_ka_tasks->logMessage($msg = "Task Scheduler: script ends.");
     echo $msg;
 }
Example #2
0
 public function sendReminder($abandoned_cart_id, $reminder_email_id)
 {
     $this->lastError = '';
     $this->load->model('tool/image');
     $abandoned_cart = $this->getAbandonedCart($abandoned_cart_id);
     if (empty($abandoned_cart)) {
         $this->lastError = "sendReminder: Abandoned cart ({$abandoned_cart_id}) not found";
         return false;
     }
     $customer = $abandoned_cart['customer'];
     if (empty($customer)) {
         $this->lastError = "sendReminder: Customer record is empty, abandoned_cart_id ({$abandoned_cart_id})";
         return false;
     }
     // change language if required
     //
     $old_language = null;
     $old_language_id = 0;
     $language_id = $abandoned_cart['language_id'];
     if ($language_id && $language_id != $this->config->get('config_language_id')) {
         $language = $this->loadTempLanguage($language_id);
         if (!empty($language)) {
             $old_language = $this->registry->get('language');
             $old_language_id = $this->config->get('config_language_id');
             $this->registry->set('language', $language);
             $this->config->set('config_language_id', $language_id);
         }
     }
     $this->language->load('ka_extensions/ka_acr');
     $ka_mail = new KaMail($this->registry, $abandoned_cart['store_id']);
     // do not remind about empty cart
     //
     if (empty($abandoned_cart['products'])) {
         return false;
     }
     $subtotal = 0;
     foreach ($abandoned_cart['products'] as $product) {
         $option_data = array();
         foreach ($product['option'] as $option) {
             if ($option['type'] != 'file') {
                 $value = $option['option_value'];
             } else {
                 $filename = $this->encryption->decrypt($option['option_value']);
                 $value = utf8_substr($filename, 0, utf8_strrpos($filename, '.'));
             }
             $option_data[] = array('name' => $option['name'], 'value' => utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value, 'type' => $option['type']);
         }
         $product['image'] = $this->getImagePath($product['image']);
         $cid = 'product_' . $product['product_id'] . '_image';
         $ka_mail->images[$cid] = $product['image'];
         $ka_mail->data['products'][] = array('key' => $product['key'], 'name' => $product['name'], 'image_cid' => $cid, 'model' => $product['model'], 'option' => $option_data, 'quantity' => $product['quantity'], 'price' => $this->currency->format($product['price']), 'total' => $this->currency->format($product['total']), 'href' => $this->url->link('product/product', 'product_id=' . $product['product_id']));
         $subtotal += $product['total'];
     }
     $ka_mail->data['subtotal'] = $this->currency->format($subtotal);
     $ka_mail->data['customer'] = $customer;
     $store = $this->getStore($abandoned_cart['store_id']);
     $token = self::$token_prefix . $this->generateKaToken($abandoned_cart['abandoned_cart_id']);
     $ka_mail->data['cart_url'] = $store['http_catalog'] . 'index.php?route=account/login&token=' . $token;
     $token = $this->generateAcrToken($abandoned_cart['abandoned_cart_id']);
     $ka_mail->data['unsubscribe_url'] = $store['http_catalog'] . 'index.php?route=account/ka_unsubscribe&acr_token=' . $token;
     $extra = array();
     $extra['images_in_emails'] = $this->config->get('ka_acr_images_in_emails');
     $is_sent = false;
     $this->load->model('localisation/ka_reminder_emails');
     $reminder_email = $this->model_localisation_ka_reminder_emails->getReminderEmail($reminder_email_id);
     if (!empty($reminder_email)) {
         if (empty($reminder_email['subject'])) {
             $reminder_email['subject'] = $this->language->get('You have some items in the cart');
         }
         $ka_mail->data['description'] = $reminder_email['description'];
         $ka_mail->send('', $customer['email'], $reminder_email['subject'], 'ka_cart_reminder.tpl', $extra);
         $this->model_localisation_ka_reminder_emails->updateLastSubmitted($reminder_email['reminder_email_id']);
         $is_sent = true;
     }
     //restore language
     //
     if (!empty($old_language)) {
         $this->config->set('config_language_id', $old_language_id);
         $this->registry->set('language', $old_language);
     }
     return $is_sent;
 }