/** * @param string $task_name * @param array $data * @return array|bool */ public function createTask($task_name, $data = array()) { if (!$task_name) { $this->errors[] = 'Can not to create task. Empty task name has been given.'; } //first of all needs to define recipient count $this->load->model('sale/customer'); $this->load->model('setting/store'); $store_info = $this->model_setting_store->getStore((int) $this->session->data['current_store_id']); if ($store_info) { $store_name = $store_info['store_name']; } else { $store_name = $this->config->get('store_name'); } //get URIs of recipients if ($data['protocol'] == 'email') { list($uris, $subscribers) = $this->_get_email_list($data); $task_controller = 'task/sale/contact/sendEmail'; //if message does not contains html-tags replace breaklines to <br> $decoded = html_entity_decode($data['message'], ENT_QUOTES, 'UTF-8'); if ($decoded == strip_tags($decoded)) { $data['message'] = nl2br($data['message']); } } elseif ($data['protocol'] == 'sms') { list($uris, $subscribers) = $this->_get_phone_list($data); $task_controller = 'task/sale/contact/sendSms'; } if (!$uris) { $this->errors[] = 'No recipients!'; return false; } //numbers of emails per task step $divider = 10; //timeout in seconds for one email send $time_per_send = 4; $steps_count = ceil(sizeof($uris) / $divider); $tm = new ATaskManager(); //create new task $task_id = $tm->addTask(array('name' => $task_name, 'starter' => 1, 'created_by' => $this->user->getId(), 'status' => 1, 'start_time' => date('Y-m-d H:i:s', mktime(0, 0, 0, date('m'), date('d') + 1, date('Y'))), 'last_time_run' => '0000-00-00 00:00:00', 'progress' => '0', 'last_result' => '1', 'run_interval' => '0', 'max_execution_time' => sizeof($uris) * $time_per_send * 2)); if (!$task_id) { $this->errors = array_merge($this->errors, $tm->errors); return false; } $tm->updateTaskDetails($task_id, array('created_by' => $this->user->getId(), 'settings' => array('recipients_count' => sizeof($uris), 'sent' => 0))); //create steps for sending $k = 0; $sort_order = 1; while ($steps_count > 0) { $uri_list = array_slice($uris, $k, $divider); $step_id = $tm->addStep(array('task_id' => $task_id, 'sort_order' => $sort_order, 'status' => 1, 'last_time_run' => '0000-00-00 00:00:00', 'last_result' => '0', 'max_execution_time' => $time_per_send * $divider * 2, 'controller' => $task_controller, 'settings' => array('to' => $uri_list, 'subject' => $data['subject'], 'message' => $data['message'], 'store_name' => $store_name, 'subscribers' => $subscribers))); if (!$step_id) { $this->errors = array_merge($this->errors, $tm->errors); return false; } else { // get eta in seconds $this->eta[$step_id] = $time_per_send * $divider; } $steps_count--; $k = $k + $divider; $sort_order++; } $task_details = $tm->getTaskById($task_id); if ($task_details) { foreach ($this->eta as $step_id => $eta) { $task_details['steps'][$step_id]['eta'] = $eta; //remove settings from output json array. We will take it from database on execution. $task_details['steps'][$step_id]['settings'] = array(); } return $task_details; } else { $this->errors[] = 'Can not to get task details for execution'; $this->errors = array_merge($this->errors, $tm->errors); return false; } }
private function _send() { $this->loadLanguage('sale/contact'); $task_id = (int) $this->request->get['task_id']; $step_id = (int) $this->request->get['step_id']; if (!$task_id || !$step_id) { $error_text = 'Cannot run task step. Task_id (or step_id) has not been set.'; $this->_return_error($error_text); } $tm = new ATaskManager(); $task_info = $tm->getTaskById($task_id); $sent = (int) $task_info['settings']['sent']; $task_steps = $tm->getTaskSteps($task_id); $step_info = array(); foreach ($task_steps as $task_step) { if ($task_step['step_id'] == $step_id) { $step_info = $task_step; if ($task_step['sort_order'] == 1) { $tm->updateTask($task_id, array('last_time_run' => date('Y-m-d H:i:s'))); } break; } } if (!$step_info) { $error_text = 'Cannot run task step. Looks like task #' . $task_id . ' does not contain step #' . $step_id; $this->_return_error($error_text); } $tm->updateStep($step_id, array('last_time_run' => date('Y-m-d H:i:s'))); if (!$step_info['settings'] || !$step_info['settings']['to']) { $error_text = 'Cannot run task step #' . $step_id . '. Unknown settings for it.'; $this->_return_error($error_text); } $this->loadModel('sale/customer'); $this->loadModel('setting/store'); $store_info = $this->model_setting_store->getStore((int) $this->session->data['current_store_id']); $from = ''; if ($store_info) { $from = $store_info['store_main_email']; } if (!$from) { $from = $this->config->get('store_main_email'); } $send_data = array('subject' => $step_info['settings']['subject'], 'message' => $step_info['settings']['message'], 'sender' => $step_info['settings']['store_name'], 'from' => $from); //send emails in loop and update task's step info for restarting if step or task failed $step_settings = $step_info['settings']; $cnt = 0; $step_result = true; foreach ($step_info['settings']['to'] as $to) { $send_data['subscriber'] = in_array($to, $step_info['settings']['subscribers']) ? true : false; if ($this->protocol == 'email') { $result = $this->_send_email($to, $send_data); } elseif ($this->protocol == 'sms') { $result = $this->_send_sms($to, $send_data); } else { $result = false; } if ($result) { //remove sent address from step $k = array_search($to, $step_settings['to']); unset($step_settings['to'][$k]); $tm->updateStep($step_id, array('settings' => serialize($step_settings))); //update task details to show them at the end $sent++; $tm->updateTaskDetails($task_id, array('created_by' => $this->user->getId(), 'settings' => array('recipients_count' => $task_info['settings']['recipients_count'], 'sent' => $sent))); } else { $step_result = false; } $cnt++; } $tm->updateStep($step_id, array('last_result' => $step_result)); if (!$step_result) { $this->_return_error('Some errors during step run. See log for details.'); } return $step_result; }