public function run()
 {
     $this->db->query("UPDATE " . DB_PREFIX . "customer c \r\n        SET congrats = 1 \r\n        WHERE DAY(birthday) <> '" . $this->db->escape(date('d')) . "' \r\n        AND MONTH(birthday) <> '" . $this->db->escape(date('m')) . "'");
     $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer c \r\n        WHERE DAY(birthday) = '" . $this->db->escape(date('d')) . "' \r\n        AND MONTH(birthday) = '" . $this->db->escape(date('m')) . "'\r\n        AND congrats = 1");
     if ($query->num_rows && (int) $this->config->get('marketing_email_happy_birthday')) {
         $params = array('job' => 'send_birthday', 'newsletter_id' => (int) $this->config->get('marketing_email_happy_birthday'));
         $this->load->library('task');
         $task = new Task($this->registry);
         $task->object_id = (int) $this->config->get('marketing_email_happy_birthday');
         $task->object_type = 'newsletter';
         $task->task = 'happy_birthday';
         $task->type = 'send';
         $task->time_exec = date('Y-m-d') . ' 08:00:00';
         $task->params = $params;
         $task->time_interval = "";
         $task->time_last_exec = "";
         $task->run_once = true;
         $task->status = 1;
         $task->date_start_exec = date('Y-m-d') . ' 08:00:00';
         $task->date_end_exec = date('Y-m-d') . ' 23:00:00';
         foreach ($query->rows as $customer) {
             $params = array('customer_id' => $customer['customer_id'], 'fullname' => $customer['firstname'] . " " . $customer['lastname'], 'company' => $customer['company'], 'rif' => $customer['rif'], 'telephone' => $customer['telephone'], 'email' => $customer['email']);
             $queue = array("params" => $params, "status" => 1, "time_exec" => date('Y-m-d') . ' 08:00:00');
             $task->addQueue($queue);
             $this->db->query("UPDATE " . DB_PREFIX . "customer c SET congrats = 0 WHERE customer_id = '" . (int) $customer['customer_id'] . "'");
         }
         $task->createSendTask();
     }
 }
 private function link2Campaign($campaign_id)
 {
     $this->load->auto('marleting/campaign');
     $this->load->auto('task');
     $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "contact c WHERE contact_id = '" . (int) $this->request->getQuery('contact_id') . "'");
     $contact_data = $query->row;
     $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "campaign c WHERE campaign_id = '" . (int) $campaign_id . "'");
     $campaign_data = $query->row;
     if ($campaign_data['newsletter_id']) {
         $campaign_data['name'] .= ' - Campaign Triggered';
         $campaign_data['date_start'] = $this->addMinute(15);
         $campaign_data['date_end'] = $this->addDay(2);
         $campaign_data['repeat'] = "";
         $campaign_data['contacts'][] = array('contact_id' => $contact_data['contact_id'], 'name' => $contact_data['name'], 'email' => $contact_data['email']);
     }
     $mail_server_id = $this->modelCampaign->getProperty((int) $campaign_id, 'mail_server', 'mail_server_id');
     $campaign_id = $this->modelCampaign->add($campaign_data);
     $this->modelCampaign->setProperty($campaign_id, 'mail_server', 'mail_server_id', $mail_server_id);
     $params = array('job' => 'send_campaign', 'campaign_id' => $campaign_id);
     $task = new Task($this->registry);
     $task->object_id = (int) $campaign_id;
     $task->object_type = 'campaign';
     $task->task = $campaign_data['name'];
     $task->type = 'send';
     $task->time_exec = date('Y-m-d H:i:s', strtotime($campaign_data['date_start']));
     $task->params = $params;
     $task->time_interval = $campaign_data['repeat'];
     $task->time_last_exec = $row['time_last_exec'];
     $task->run_once = !(bool) $campaign_data['repeat'];
     $task->status = 1;
     $task->date_start_exec = date('Y-m-d H:i:s', strtotime($campaign_data['date_start']));
     $task->date_end_exec = date('Y-m-d H:i:s', strtotime($campaign_data['date_end']));
     foreach ($campaign_data['contacts'] as $sort_order => $contact) {
         $params = array('contact_id' => $contact['contact_id'], 'name' => $contact['name'], 'email' => $contact['email'], 'campaign_id' => $campaign_id);
         $queue = array("params" => $params, "status" => 1, "time_exec" => date('Y-m-d H:i:s', strtotime($campaign_data['date_start'])));
         $task->addQueue($queue);
     }
     $task->createSendTask();
     $this->cache->set("campaign.html.{$campaign_id}", $htmlbody);
 }
 public function send()
 {
     $htmlbody = html_entity_decode($this->cache->get("campaign.html.temp"));
     $htmlbody = str_replace('%7B', '{', $htmlbody);
     $htmlbody = str_replace('%7D', '}', $htmlbody);
     $data = unserialize($this->cache->get("campaign.data.temp"));
     $to = $data['to'];
     $campaign = $data['post'];
     $links = $data['links'];
     $campaign['contacts'] = $to;
     $campaign_id = $this->modelCampaign->add($campaign);
     $this->modelCampaign->setProperty($campaign_id, 'mail_server', 'mail_server_id', $campaign['mail_server_id']);
     $params = array('job' => 'send_campaign', 'campaign_id' => $campaign_id);
     $this->load->library('task');
     $task = new Task($this->registry);
     $task->object_id = (int) $campaign_id;
     $task->object_type = 'campaign';
     $task->task = $campaign['name'];
     $task->type = 'send';
     $task->time_exec = date('Y-m-d H:i:s', strtotime($campaign['date_start']));
     $task->params = $params;
     $task->time_interval = $campaign['repeat'];
     $task->time_last_exec = $row['time_last_exec'];
     $task->run_once = !(bool) $campaign['repeat'];
     $task->status = 1;
     $task->date_start_exec = date('Y-m-d H:i:s', strtotime($campaign['date_start']));
     $task->date_end_exec = date('Y-m-d H:i:s', strtotime($campaign['date_end']));
     foreach ($to as $sort_order => $contact) {
         foreach ($links as $link) {
             if (empty($link['url']) || empty($link['redirect'])) {
                 continue;
             }
             $link['url'] = str_replace('%7B', '{', $link['url']);
             $link['url'] = str_replace('%7D', '}', $link['url']);
             $link['url'] = str_replace('{%contact_id%}', $contact['contact_id'], $link['url']);
             $link['url'] = str_replace('{%campaign_id%}', $campaign_id, $link['url']);
             $this->modelCampaign->addLink($link, $campaign_id);
         }
         $params = array('contact_id' => $contact['contact_id'], 'name' => $contact['name'], 'email' => $contact['email'], 'campaign_id' => $campaign_id);
         $queue = array("params" => $params, "status" => 1, "time_exec" => date('Y-m-d H:i:s', strtotime($campaign['date_start'])));
         $htmlbody = str_replace('{%contact_id%}', $contact['contact_id'], $htmlbody);
         $htmlbody = str_replace('{%campaign_id%}', $campaign_id, $htmlbody);
         $this->cache->set("campaign.html.{$campaign_id}." . $contact['contact_id'], $htmlbody);
         $task->addQueue($queue);
     }
     $task->createSendTask();
     $this->cache->set("campaign.html.{$campaign_id}", $htmlbody);
     $this->session->set('success', $this->language->get('text_success'));
     $this->redirect(Url::createAdminUrl("marketing/campaign"));
 }
 public function invitefriends()
 {
     if ($this->session->has('meliAccessToken')) {
         $token = json_decode($this->session->get('meliAccessToken'));
         $url = 'https://api.mercadolibre.com/users/me?access_token=' . $token->access_token;
         $response = $this->handler->fetch($url);
         $profile = json_decode($response['body'], true);
         if ($profile['error']) {
             $this->redirect($this->oauth_url);
         }
         $list = $this->db->query("SELECT * \n            FROM " . DB_PREFIX . "contact_list \n            WHERE name = 'Amigos de " . $this->db->escape(addslashes($profile['name'])) . " en Hotmail'");
         if ($list->num_rows) {
             $list_id = $list->row['contact_list_id'];
         } else {
             $this->db->query("INSERT INTO " . DB_PREFIX . "contact_list SET \n                name        = 'Amigos de " . $this->db->escape(addslashes($profile['name'])) . " en Hotmail',\n                description = 'Amigos de " . $this->db->escape(addslashes($profile['name'])) . " en Hotmail',\n                date_added  = NOW()");
             $list_id = $this->db->getLastId();
         }
         $url = 'https://api.mercadolibre.com/users/me?access_token=' . $token->access_token . '&limit=1000';
         $response = $this->handler->fetch($url);
         if ($response['body']) {
             $xml = json_decode($response['body'], true);
             if ($xml['error']) {
                 $this->redirect($this->oauth_url);
             }
             $to = array();
             $control = array();
             foreach ($xml['data'] as $emails) {
                 $contacts[] = array($emails['name'], $emails['emails']['preferred']);
                 $name = addslashes(str_replace("'", '', $emails['name']));
                 $email = $emails['emails']['preferred'];
                 if (empty($email)) {
                     continue;
                 }
                 $customer = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE email = '" . $this->db->escape($email) . "'");
                 $contact = $this->db->query("SELECT * FROM " . DB_PREFIX . "contact WHERE email = '" . $this->db->escape($email) . "'");
                 if (!$contact->rows) {
                     $this->db->query("INSERT INTO " . DB_PREFIX . "contact SET \n                              name        = '" . $this->db->escape($name) . "',\n                              email       = '" . $this->db->escape($email) . "', \n                              customer_id = '" . (int) $customer->row['customer_id'] . "',\n                              date_added  = NOW()");
                     $contact_id = $this->db->getLastId();
                     $this->db->query("INSERT INTO " . DB_PREFIX . "contact_to_list SET \n                            contact_id     = '" . (int) $contact_id . "',\n                            contact_list_id= '" . (int) $list_id . "',\n                            date_added    = NOW()");
                 }
                 if (!in_array($email, $control) && !empty($email)) {
                     $control[] = $email;
                     $to[] = array('contact_id' => (int) $contact_id, 'name' => $name, 'email' => $email);
                 }
             }
             if ($this->config->get('marketing_email_invite_friends')) {
                 $this->load->model('marketing/newsletter');
                 $newsletter = $this->modelNewsletter->getById($this->config->get('marketing_email_invite_friends'));
                 if ($newsletter) {
                     $dom = new DOMDocument();
                     $dom->preserveWhiteSpace = false;
                     $dom->loadHTML(html_entity_decode($newsletter['htmlbody']));
                     $trace_url = Url::createUrl("marketing/campaign/trace");
                     $trackEmail = $dom->createElement('img');
                     $trackEmail->setAttribute('src', $trace_url);
                     $dom->appendChild($trackEmail);
                     $data = array('newsletter_id' => $newsletter['newsletter_id'], 'name' => 'Invitar Amigos de ' . $this->db->escape(addslashes($profile['name'])) . ' en Hotmail', 'subject' => 'Hola', 'from_name' => $this->db->escape(addslashes($profile['name'])), 'from_email' => $this->db->escape($profile['email']), 'replyto_email' => $this->db->escape($this->config->get('config_email')), 'embed_image' => 0, 'trace_email' => 1, 'trace_click' => 0, 'contacts' => $to, 'repeat' => 'no_repeat', 'date_start' => date('Y-m-d h:i:s'), 'date_end' => date('Y-m-d h:i:s'), 'date_added' => date('Y-m-d h:i:s'));
                     $this->load->model('marketing/campaign');
                     $campaign_id = $this->modelCampaign->add($data);
                     $params = array('job' => 'send_campaign', 'campaign_id' => $campaign_id);
                     $this->load->library('task');
                     $task = new Task($this->registry);
                     $task->object_id = (int) $campaign_id;
                     $task->object_type = 'campaign';
                     $task->task = $campaign['name'];
                     $task->type = 'send';
                     $task->time_exec = date('Y-m-d H:i:s');
                     $task->params = $params;
                     $task->time_interval = 'no-repeat';
                     $task->time_last_exec = date('Y-m-d H:i:s');
                     $task->run_once = true;
                     $task->status = 1;
                     $task->date_start_exec = date('Y-m-d H:i:s');
                     $task->date_end_exec = date('Y-m-d H:i:s');
                     $task->addMinute(15);
                     $control = array();
                     foreach ($to as $sort_order => $contact) {
                         if (in_array($contact['email'], $control)) {
                             continue;
                         }
                         $control[] = $contact['email'];
                         $params = array('contact_id' => $contact['contact_id'], 'name' => $contact['name'], 'email' => $contact['email'], 'campaign_id' => $campaign_id);
                         $queue = array("params" => $params, "status" => 1, "time_exec" => date('Y-m-d H:i:s'));
                         $task->addQueue($queue);
                     }
                     $task->createSendTask();
                 }
                 $this->session->set('success', $this->language->get('text_friends_invited_success'));
                 if ($this->session->has('redirect')) {
                     $this->redirect($this->session->get('rediret'));
                 } else {
                     $this->redirect(HTTP_HOME);
                 }
             }
         }
     } else {
         $this->redirect($this->oauth_url);
     }
 }
 public function run()
 {
     $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "stat s \r\n        LEFT JOIN " . DB_PREFIX . "customer c ON (s.customer_id=c.customer_id) \r\n        LEFT JOIN " . DB_PREFIX . "product p ON (s.object_id=p.product_id) \r\n        LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id=pd.product_id) \r\n        WHERE DAY(s.date_added) = '" . $this->db->escape(date('d')) . "' \r\n            AND MONTH(s.date_added) = '" . $this->db->escape(date('m')) . "'\r\n            AND YEAR(s.date_added) = '" . $this->db->escape(date('Y')) . "'\r\n            AND s.object_type = 'product'\r\n            AND s.status = 1\r\n            AND s.customer_id <> 0\r\n            AND pd.language_id = 1\r\n        GROUP BY s.customer_id\r\n        LIMIT 40");
     if ($query->num_rows && (int) $this->config->get('marketing_email_recommended_products')) {
         $this->load->library('tax');
         $this->load->library('currency');
         $this->load->library('image');
         $this->load->library('url');
         $this->tax = new Tax($this->registry);
         $this->currency = new Currency($this->registry);
         $products = array();
         foreach ($query->rows as $product) {
             $image = !empty($product['image']) ? $product['image'] : 'no_image.jpg';
             $price = $this->currency->format($this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax')), '', '', false);
             $products[] = array('product_id' => $product['product_id'], 'name' => $product['name'], 'image' => NTImage::resizeAndSave($image, 150, 150), 'model' => $product['model'], 'href' => Url::createUrl('store/product', array('product_id' => $product['product_id'])));
         }
         $params = array('job' => 'send_recommended_products', 'newsletter_id' => (int) $this->config->get('marketing_email_recommended_products'));
         $this->load->library('task');
         $task = new Task($this->registry);
         $task->object_id = (int) $this->config->get('marketing_email_recommended_products');
         $task->object_type = 'newsletter';
         $task->task = 'recommended_products';
         $task->type = 'send';
         $task->time_exec = date('Y-m-d') . ' 08:00:00';
         $task->params = $params;
         $task->time_interval = "";
         $task->time_last_exec = "";
         $task->run_once = true;
         $task->status = 1;
         $task->date_start_exec = date('Y-m-d') . ' 08:00:00';
         $task->date_end_exec = date('Y-m-d') . ' 23:00:00';
         foreach ($query->rows as $customer) {
             $params = array('customer_id' => $customer['customer_id'], 'fullname' => $customer['firstname'] . " " . $customer['lastname'], 'company' => $customer['company'], 'rif' => $customer['rif'], 'telephone' => $customer['telephone'], 'email' => $customer['email'], 'products' => $products);
             $queue = array("params" => $params, "status" => 1, "time_exec" => date('Y-m-d') . ' 08:00:00');
             $task->addQueue($queue);
             $this->db->query("UPDATE " . DB_PREFIX . "stat SET status = 0 \r\n                WHERE customer_id = '" . (int) $product['customer_id'] . "'\r\n                    AND DAY(date_added) = '" . $this->db->escape(date('d')) . "' \r\n                    AND MONTH(date_added) = '" . $this->db->escape(date('m')) . "'\r\n                    AND YEAR(date_added) = '" . $this->db->escape(date('Y')) . "'");
         }
         $task->createSendTask();
     }
 }
 public function promote()
 {
     if ($this->session->has('liveAccessToken')) {
         $token = json_decode($this->session->get('liveAccessToken'));
         $url = 'https://apis.live.net/v5.0/me?access_token=' . $token->access_token;
         $response = $this->handler->fetch($url);
         $profile = json_decode($response['body'], true);
         if ($profile['error']) {
             $this->session->clear('ltoken');
             $this->session->clear('lcode');
             $this->session->clear('liveAccessToken');
             $this->session->clear('action');
             $this->redirect($this->oauth_url);
         }
         $list = $this->db->query("SELECT * \n            FROM " . DB_PREFIX . "contact_list \n            WHERE name = \"Amigos de " . $this->db->escape(addslashes($profile['name'])) . " (" . $this->db->escape(addslashes($profile['emails']['preferred'])) . ") en Hotmail\"");
         if ($list->num_rows) {
             $list_id = $list->row['contact_list_id'];
         } else {
             $this->db->query("INSERT INTO " . DB_PREFIX . "contact_list SET \n                name        = \"Amigos de " . $this->db->escape(addslashes($profile['name'])) . " (" . $this->db->escape(addslashes($profile['emails']['preferred'])) . ") en Hotmail\",\n                description = \"Amigos de " . $this->db->escape(addslashes($profile['name'])) . " (" . $this->db->escape(addslashes($profile['emails']['preferred'])) . ") en Hotmail\",\n                date_added  = NOW()");
             $list_id = $this->db->getLastId();
         }
         $url = 'https://apis.live.net/v5.0/me/contacts?access_token=' . $token->access_token . '&limit=1000';
         $response = $this->handler->fetch($url);
         if ($response['body']) {
             $xml = json_decode($response['body'], true);
             if ($xml['error']) {
                 $this->redirect($this->oauth_url);
             }
             $to = array();
             $control = array();
             foreach ($xml['data'] as $emails) {
                 $contacts[] = array($emails['name'], $emails['emails']['preferred']);
                 $name = addslashes(str_replace("'", '', $emails['name']));
                 $email = $emails['emails']['preferred'];
                 if (empty($email)) {
                     continue;
                 }
                 $customer = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE email = '" . $this->db->escape($email) . "'");
                 $contact = $this->db->query("SELECT * FROM " . DB_PREFIX . "contact WHERE email = '" . $this->db->escape($email) . "'");
                 if (!$contact->row) {
                     $this->db->query("INSERT INTO " . DB_PREFIX . "contact SET \n                              name        = '" . $this->db->escape($name) . "',\n                              email       = '" . $this->db->escape($email) . "', \n                              customer_id = '" . (int) $customer->row['customer_id'] . "',\n                              date_added  = NOW()");
                     $contact_id = $this->db->getLastId();
                     $this->db->query("INSERT INTO " . DB_PREFIX . "contact_to_list SET \n                            contact_id     = '" . (int) $contact_id . "',\n                            contact_list_id= '" . (int) $list_id . "',\n                            date_added    = NOW()");
                 } else {
                     $contact_id = $contact->row['contact_id'];
                 }
                 if (!in_array($email, $control) && !empty($email)) {
                     $control[] = $email;
                     $to[] = array('contact_id' => (int) $contact_id, 'name' => $name, 'email' => $email);
                 }
             }
             if ($this->config->get('marketing_email_promote_product')) {
                 $this->load->model('marketing/newsletter');
                 $product = array();
                 $product_id = $this->request->hasQuery('product_id') ? $this->request->getQuery('product_id') : $this->session->get('promote_product_id');
                 $this->session->set('promote_product_id', $product_id);
                 if ($product_id) {
                     $product_id;
                     $this->load->model('store/product');
                     $product = $this->modelProduct->getProduct($product_id);
                     if ($product) {
                         $this->load->model('store/product');
                         $Url = new Url($this->registry);
                         //Libs
                         $this->load->auto('image');
                         $this->load->auto('currency');
                         $this->load->auto('tax');
                         $product['url'] = $Url::createUrl('store/product', array('product_id' => $product['product_id']));
                         $image = isset($product['image']) ? $product['image'] : 'no_image.jpg';
                         $product['image'] = NTImage::resizeAndSave($image, $this->config->get('config_image_thumb_width'), $this->config->get('config_image_thumb_height'));
                         $discount = $this->modelProduct->getProductDiscount($product['product_id']);
                         if ($discount) {
                             $product['price'] = $this->currency->format($this->tax->calculate($discount, $product['tax_class_id'], $this->config->get('config_tax')));
                         } else {
                             $product['price'] = $this->currency->format($this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax')));
                             $special = $this->modelProduct->getProductSpecial($product['product_id']);
                             if ($special) {
                                 $product['special'] = $this->currency->format($this->tax->calculate($special, $product['tax_class_id'], $this->config->get('config_tax')));
                             }
                         }
                         $discounts = $this->modelProduct->getProductDiscounts($product['product_id']);
                         foreach ($discounts as $k => $discount) {
                             $product['discounts'][$k] = array('quantity' => $discount['quantity'], 'price' => $this->currency->format($this->tax->calculate($discount['price'], $product['tax_class_id'], $this->config->get('config_tax'))));
                         }
                         $results = $this->modelProduct->getProductImages($product['product_id']);
                         foreach ($results as $k => $result) {
                             $product['images'][$k] = array('thumb' => NTImage::resizeAndSave($result['image'], $this->config->get('config_image_additional_width'), $this->config->get('config_image_additional_height')));
                         }
                         $results = $this->modelProduct->getProductTags($product['product_id']);
                         foreach ($results as $k => $result) {
                             if ($result['tag']) {
                                 $product['tags'][$k] = array('tag' => $result['tag'], 'href' => $Url::createUrl('store/search', array('q' => $result['tag'])));
                             }
                         }
                         $fullname = $profile['name'] ? addslashes($profile['name']) : $this->config->get('config_title');
                         $newsletter = $this->modelNewsletter->getById($this->config->get('marketing_email_promote_product'));
                         if ($newsletter) {
                             $data = array('newsletter_id' => $newsletter['newsletter_id'], 'name' => "Invitar Amigos de " . $this->db->escape(addslashes($profile['name'])) . " (" . $this->db->escape(addslashes($profile['emails']['preferred'])) . ") en Outlook", 'subject' => 'Hola', 'from_name' => $this->db->escape($fullname), 'from_email' => $this->db->escape($this->config->get('config_email')), 'replyto_email' => $this->db->escape($this->config->get('config_email')), 'embed_image' => 0, 'trace_email' => 1, 'trace_click' => 0, 'contacts' => $to, 'repeat' => 'no_repeat', 'date_start' => date('Y-m-d h:i:s'), 'date_end' => date('Y-m-d h:i:s'), 'date_added' => date('Y-m-d h:i:s'));
                             $this->load->model('marketing/campaign');
                             $campaign_id = $this->modelCampaign->add($data);
                             $params = array('job' => 'send_campaign', 'product_id' => $product_id, 'campaign_id' => $campaign_id);
                             $this->load->library('task');
                             $task = new Task($this->registry);
                             $task->object_id = (int) $campaign_id;
                             $task->object_type = 'campaign';
                             $task->task = $campaign['name'];
                             $task->type = 'send';
                             $task->time_exec = date('Y-m-d H:i:s');
                             $task->params = $params;
                             $task->time_interval = 'no-repeat';
                             $task->time_last_exec = date('Y-m-d H:i:s');
                             $task->run_once = true;
                             $task->status = 1;
                             $task->date_start_exec = date('Y-m-d H:i:s');
                             $task->date_end_exec = date('Y-m-d H:i:s');
                             $task->addMinute(15);
                             $control = array();
                             foreach ($to as $sort_order => $contact) {
                                 if (in_array($contact['email'], $control)) {
                                     continue;
                                 }
                                 $control[] = $contact['email'];
                                 $params = array('contact_id' => $contact['contact_id'], 'name' => $contact['name'], 'email' => $contact['email'], 'product' => $product, 'campaign_id' => $campaign_id);
                                 $queue = array("params" => $params, "status" => 1, "time_exec" => date('Y-m-d H:i:s'));
                                 $task->addQueue($queue);
                             }
                             $task->createSendTask();
                         }
                     }
                 }
                 $this->session->set('success', $this->language->get('text_promote_product_success'));
             }
             $this->redirect($Url::createUrl('store/product', array('product_id' => $product['product_id'])));
         }
     } else {
         $this->redirect($this->oauth_url);
     }
 }
 public function promote()
 {
     if ($this->google->getAccessToken()) {
         $reqUrl = 'https://www.google.com/m8/feeds/contacts/default/full?max-results=1000';
         $req = new Google_HttpRequest($reqUrl);
         $googleAuth = $this->google->getIo()->authenticatedRequest($req);
         $xml = simplexml_load_string($googleAuth->getResponseBody());
         $response = json_encode($xml);
         $reqUrl = 'https://www.googleapis.com/oauth2/v2/userinfo';
         $req = new Google_HttpRequest($reqUrl);
         $userInfo = json_decode($this->google->getIo()->authenticatedRequest($req)->getResponseBody());
         $xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
         $list = $this->db->query("SELECT * \n            FROM " . DB_PREFIX . "contact_list \n            WHERE name = \"Amigos de " . $this->db->escape(addslashes($userInfo->name)) . " (" . $this->db->escape(addslashes($userInfo->email)) . ") en Google\"");
         if ($list->num_rows) {
             $list_id = $list->row['contact_list_id'];
         } else {
             $this->db->query("INSERT INTO " . DB_PREFIX . "contact_list SET \n                name        = \"Amigos de " . $this->db->escape(addslashes($userInfo->name)) . " (" . $this->db->escape(addslashes($userInfo->email)) . ") en Google\",\n                description = \"Amigos de " . $this->db->escape(addslashes($userInfo->name)) . " (" . $this->db->escape(addslashes($userInfo->email)) . ") en Google\",\n                date_added  = NOW()");
             $list_id = $this->db->getLastId();
         }
         $to = array();
         $control = array();
         foreach ($xml->entry as $entry) {
             foreach ($entry->xpath('gd:email') as $value) {
                 $name = str_replace("'", '', (string) $entry->title);
                 $email = (string) $value->attributes()->address;
                 if (empty($email)) {
                     continue;
                 }
                 $customer = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE email = '" . $this->db->escape($email) . "'");
                 $contact = $this->db->query("SELECT * FROM " . DB_PREFIX . "contact WHERE email = '" . $this->db->escape($email) . "'");
                 if (!$contact->row) {
                     $this->db->query("INSERT INTO " . DB_PREFIX . "contact SET \n                              name        = '" . $this->db->escape($name) . "',\n                              email       = '" . $this->db->escape($email) . "', \n                              customer_id = '" . (int) $customer->row['customer_id'] . "',\n                              date_added  = NOW()");
                     $contact_id = $this->db->getLastId();
                     $this->db->query("INSERT INTO " . DB_PREFIX . "contact_to_list SET \n                            contact_id     = '" . (int) $contact_id . "',\n                            contact_list_id= '" . (int) $list_id . "',\n                            date_added    = NOW()");
                 } else {
                     $contact_id = $contact->row['contact_id'];
                 }
                 if (!in_array($email, $control) && !empty($email)) {
                     $control[] = $email;
                     $to[] = array('contact_id' => $contact_id, 'name' => $name, 'email' => $email);
                 }
             }
         }
         if ($this->config->get('marketing_email_promote_product')) {
             $this->load->model('marketing/newsletter');
             $product = array();
             $product_id = $this->request->hasQuery('product_id') ? $this->request->getQuery('product_id') : $this->session->get('promote_product_id');
             $this->session->set('promote_product_id', $product_id);
             if ($product_id) {
                 $this->load->model('store/product');
                 $product = $this->modelProduct->getProduct($product_id);
                 if ($product) {
                     $this->load->model('store/product');
                     $Url = new Url($this->registry);
                     //Libs
                     $this->load->auto('image');
                     $this->load->auto('currency');
                     $this->load->auto('tax');
                     $product['url'] = $Url::createUrl('store/product', array('product_id' => $product['product_id']));
                     $image = isset($product['image']) ? $product['image'] : 'no_image.jpg';
                     $product['image'] = NTImage::resizeAndSave($image, $this->config->get('config_image_thumb_width'), $this->config->get('config_image_thumb_height'));
                     $discount = $this->modelProduct->getProductDiscount($product['product_id']);
                     if ($discount) {
                         $product['price'] = $this->currency->format($this->tax->calculate($discount, $product['tax_class_id'], $this->config->get('config_tax')));
                     } else {
                         $product['price'] = $this->currency->format($this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax')));
                         $special = $this->modelProduct->getProductSpecial($product['product_id']);
                         if ($special) {
                             $product['special'] = $this->currency->format($this->tax->calculate($special, $product['tax_class_id'], $this->config->get('config_tax')));
                         }
                     }
                     $discounts = $this->modelProduct->getProductDiscounts($product['product_id']);
                     foreach ($discounts as $k => $discount) {
                         $product['discounts'][$k] = array('quantity' => $discount['quantity'], 'price' => $this->currency->format($this->tax->calculate($discount['price'], $product['tax_class_id'], $this->config->get('config_tax'))));
                     }
                     $results = $this->modelProduct->getProductImages($product['product_id']);
                     foreach ($results as $k => $result) {
                         $product['images'][$k] = array('thumb' => NTImage::resizeAndSave($result['image'], $this->config->get('config_image_additional_width'), $this->config->get('config_image_additional_height')));
                     }
                     $results = $this->modelProduct->getProductTags($product['product_id']);
                     foreach ($results as $k => $result) {
                         if ($result['tag']) {
                             $product['tags'][$k] = array('tag' => $result['tag'], 'href' => $Url::createUrl('store/search', array('q' => $result['tag'])));
                         }
                     }
                     $fullname = $userInfo->name ? addslashes($userInfo->name) : $this->config->get('config_title');
                     $newsletter = $this->modelNewsletter->getById($this->config->get('marketing_email_promote_product'));
                     if ($newsletter) {
                         $data = array('newsletter_id' => $newsletter['newsletter_id'], 'name' => "Invitar Amigos de " . $this->db->escape(addslashes($userInfo->name)) . " (" . $this->db->escape(addslashes($userInfo->email)) . ") en Google", 'subject' => 'Hola', 'from_name' => $this->db->escape($fullname), 'from_email' => $this->db->escape($this->config->get('config_email')), 'replyto_email' => $this->db->escape($this->config->get('config_email')), 'embed_image' => 0, 'trace_email' => 1, 'trace_click' => 0, 'contacts' => $to, 'repeat' => 'no_repeat', 'date_start' => date('Y-m-d h:i:s'), 'date_end' => date('Y-m-d h:i:s'), 'date_added' => date('Y-m-d h:i:s'));
                         $this->load->model('marketing/campaign');
                         $campaign_id = $this->modelCampaign->add($data);
                         $params = array('job' => 'send_campaign', 'product_id' => $product_id, 'campaign_id' => $campaign_id);
                         $this->load->library('task');
                         $task = new Task($this->registry);
                         $task->object_id = (int) $campaign_id;
                         $task->object_type = 'campaign';
                         $task->task = $campaign['name'];
                         $task->type = 'send';
                         $task->time_exec = date('Y-m-d H:i:s');
                         $task->params = $params;
                         $task->time_interval = 'no-repeat';
                         $task->time_last_exec = date('Y-m-d H:i:s');
                         $task->run_once = true;
                         $task->status = 1;
                         $task->date_start_exec = date('Y-m-d H:i:s');
                         $task->date_end_exec = date('Y-m-d H:i:s');
                         $task->addMinute(15);
                         $control = array();
                         foreach ($to as $sort_order => $contact) {
                             if (in_array($contact['email'], $control)) {
                                 continue;
                             }
                             $control[] = $contact['email'];
                             $params = array('contact_id' => $contact['contact_id'], 'name' => $contact['name'], 'email' => $contact['email'], 'product' => $product, 'campaign_id' => $campaign_id);
                             $queue = array("params" => $params, "status" => 1, "time_exec" => date('Y-m-d H:i:s'));
                             $task->addQueue($queue);
                         }
                         $task->createSendTask();
                     }
                 }
             }
             $this->session->set('success', $this->language->get('text_promote_product_success'));
         }
         $this->redirect($Url::createUrl('store/product', array('product_id' => $product['product_id'])));
     } else {
         $this->redirect($this->oauth_url);
     }
 }
 public function __construct()
 {
     $this->registry = new Registry();
     $this->load = new Loader($this->registry);
     $this->config = new Config();
     $this->cache = new Cache();
     $this->session = new Session();
     $this->request = new Request();
     $this->db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
     $this->registry->set('db', $this->db);
     $this->registry->set('load', $this->load);
     $this->registry->set('config', $this->config);
     $this->registry->set('cache', $this->cache);
     $this->registry->set('session', $this->session);
     $this->registry->set('request', $this->request);
     $this->load->library('customer');
     $this->customer = new Customer($this->registry);
     $this->registry->set('customer', $this->customer);
     $this->load->library('task');
     $this->load->library('email/mailer');
     $this->load->library('email/pop3');
     $this->load->library('email/smtp');
     $this->load->library('email/utf8');
     $this->mailer = new Mailer();
     $this->smtp = new SMTP();
     $this->utf8 = new utf8();
     $this->registry->set('mailer', $this->mailer);
     $this->initConfig();
     $this->initMailer();
     $this->cronSend = new CronSend($this->registry);
     $this->cronBirthday = new CronBirthday($this->registry);
     $this->cronPromoter = new CronPromoter($this->registry);
     /*
     $this->cronSale     = new CronSale($this->registry);
     $this->cronEnquiry  = new CronEnquiry($this->registry);
     $this->cronReport   = new CronReport($this->registry);
     $this->cronBackup   = new CronBackup($this->registry);
     $this->cronMaintenance = new CronMaintenance($this->registry);
     */
     $this->dt = new DateTime();
     $this->dt->setTimezone(new DateTimeZone($this->timeZone));
     $query = $this->db->query("SELECT * \r\n        FROM " . DB_PREFIX . "task \r\n        WHERE date_start_exec <= NOW()\r\n        AND status = 1\r\n        ORDER BY sort_order ASC, time_exec ASC");
     foreach ($query->rows as $key => $row) {
         $task = new Task($this->registry);
         $task->task_id = $row['task_id'];
         $task->object_id = $row['object_id'];
         $task->object_type = $row['object_type'];
         $task->task = $row['task'];
         $task->type = $row['type'];
         $task->time_exec = $row['time_exec'];
         $task->params = unserialize($row['params']);
         $task->time_interval = $row['time_interval'];
         $task->time_last_exec = $row['time_last_exec'];
         $task->run_once = $row['run_once'];
         $task->status = $row['status'];
         $task->sort_order = $row['sort_order'];
         $task->date_start_exec = $row['date_start_exec'];
         $task->date_end_exec = $row['date_end_exec'];
         $limit = "";
         $qry = $this->db->query("SELECT * \r\n            FROM " . DB_PREFIX . "task_queue t\r\n            WHERE task_id = '" . (int) $row['task_id'] . "' \r\n            AND status = 1\r\n            ORDER BY sort_order ASC, time_exec ASC\r\n            {$limit}");
         foreach ($qry->rows as $queue) {
             $task->addQueue($queue);
         }
         $this->tasks[$task->task_id] = $task;
     }
 }