PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/trackingfront/trackingfront.php

https://gitlab.com/staging06/myproject
PHP | 237 lines | 188 code | 25 blank | 24 comment | 33 complexity | 35a44515d39a52682e1247161864d4cb MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2015 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Academic Free License (AFL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/afl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2015 PrestaShop SA
  23. * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. if (!defined('_PS_VERSION_'))
  27. exit;
  28. class TrackingFront extends Module
  29. {
  30. public function __construct()
  31. {
  32. $this->name = 'trackingfront';
  33. $this->tab = 'shipping_logistics';
  34. $this->version = '1.8.3';
  35. $this->author = 'PrestaShop';
  36. $this->need_instance = 0;
  37. parent::__construct();
  38. $this->displayName = $this->l('Tracking - Front office');
  39. $this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
  40. $this->description = $this->l('Enables your affiliates to access their own statistics. See Stats/Referrers.');
  41. }
  42. public function postProcess()
  43. {
  44. if (Tools::isSubmit('ajaxProductFilter'))
  45. {
  46. $fake_employee = new Employee();
  47. $fake_employee->id = 424242;
  48. $fake_employee->stats_date_from = $this->context->cookie->stats_date_from;
  49. $fake_employee->stats_date_to = $this->context->cookie->stats_date_to;
  50. if (empty($fake_employee->stats_date_from) || empty($fake_employee->stats_date_to) || $fake_employee->stats_date_from == '0000-00-00' || $fake_employee->stats_date_to == '0000-00-00')
  51. {
  52. if (empty($fake_employee->stats_date_from) || $fake_employee->stats_date_from == '0000-00-00')
  53. $fake_employee->stats_date_from = date('Y').'-01-01';
  54. if (empty($fake_employee->stats_date_to) || $fake_employee->stats_date_to == '0000-00-00')
  55. $fake_employee->stats_date_to = date('Y').'-12-31';
  56. }
  57. $result = Db::getInstance()->getRow('
  58. SELECT `id_referrer`
  59. FROM `'._DB_PREFIX_.'referrer`
  60. WHERE `id_referrer` = '.(int)Tools::getValue('id_referrer').' AND `passwd` = \''.pSQL(Tools::getValue('token')).'\'');
  61. if (isset($result['id_referrer']) && (int)$result['id_referrer'] > 0)
  62. Referrer::getAjaxProduct((int)$result['id_referrer'], (int)Tools::getValue('id_product'), $fake_employee);
  63. }
  64. elseif (Tools::isSubmit('logout_tracking'))
  65. {
  66. unset($this->context->cookie->tracking_id);
  67. unset($this->context->cookie->tracking_passwd);
  68. Tools::redirect(Tools::getShopDomain(true, false).__PS_BASE_URI__.'modules/trackingfront/stats.php');
  69. }
  70. elseif (Tools::isSubmit('submitLoginTracking'))
  71. {
  72. $errors = array();
  73. $login = trim(Tools::getValue('login'));
  74. $passwd = trim(Tools::getValue('passwd'));
  75. if (empty($login))
  76. $errors[] = $this->l('login is required');
  77. elseif (!Validate::isGenericName($login))
  78. $errors[] = $this->l('invalid login');
  79. elseif (empty($passwd))
  80. $errors[] = $this->l('password is required');
  81. elseif (!Validate::isPasswd($passwd, 1))
  82. $errors[] = $this->l('invalid password');
  83. else
  84. {
  85. $passwd = Tools::encrypt($passwd);
  86. $result = Db::getInstance()->getRow('
  87. SELECT `id_referrer`
  88. FROM `'._DB_PREFIX_.'referrer`
  89. WHERE `name` = \''.pSQL($login).'\' AND `passwd` = \''.pSQL($passwd).'\'');
  90. if (!isset($result['id_referrer']) || !($tracking_id = (int)$result['id_referrer']))
  91. $errors[] = $this->l('authentication failed');
  92. else
  93. {
  94. $this->context->cookie->tracking_id = $tracking_id;
  95. $this->context->cookie->tracking_passwd = $passwd;
  96. Tools::redirect(Tools::getShopDomain(true, false).__PS_BASE_URI__.'modules/trackingfront/stats.php');
  97. }
  98. }
  99. $this->smarty->assign('errors', $errors);
  100. }
  101. $from = date('Y-m-d');
  102. $to = date('Y-m-d');
  103. if (Tools::isSubmit('submitDatePicker'))
  104. {
  105. $from = Tools::getValue('datepickerFrom');
  106. $to = Tools::getValue('datepickerTo');
  107. }
  108. if (Tools::isSubmit('submitDateDay'))
  109. {
  110. $from = date('Y-m-d');
  111. $to = date('Y-m-d');
  112. }
  113. if (Tools::isSubmit('submitDateDayPrev'))
  114. {
  115. $yesterday = time() - 60 * 60 * 24;
  116. $from = date('Y-m-d', $yesterday);
  117. $to = date('Y-m-d', $yesterday);
  118. }
  119. if (Tools::isSubmit('submitDateMonth'))
  120. {
  121. $from = date('Y-m-01');
  122. $to = date('Y-m-t');
  123. }
  124. if (Tools::isSubmit('submitDateMonthPrev'))
  125. {
  126. $m = (date('m') == 1 ? 12 : date('m') - 1);
  127. $y = ($m == 12 ? date('Y') - 1 : date('Y'));
  128. $from = $y.'-'.$m.'-01';
  129. $to = $y.'-'.$m.date('-t', mktime(12, 0, 0, $m, 15, $y));
  130. }
  131. if (Tools::isSubmit('submitDateYear'))
  132. {
  133. $from = date('Y-01-01');
  134. $to = date('Y-12-31');
  135. }
  136. if (Tools::isSubmit('submitDateYearPrev'))
  137. {
  138. $from = (date('Y') - 1).date('-01-01');
  139. $to = (date('Y') - 1).date('-12-31');
  140. }
  141. $this->context->cookie->stats_date_from = $from;
  142. $this->context->cookie->stats_date_to = $to;
  143. }
  144. public function isLogged()
  145. {
  146. if (!$this->context->cookie->tracking_id || !$this->context->cookie->tracking_passwd)
  147. return false;
  148. $result = Db::getInstance()->getRow('
  149. SELECT `id_referrer`
  150. FROM `'._DB_PREFIX_.'referrer`
  151. WHERE `id_referrer` = '.(int)$this->context->cookie->tracking_id.' AND `passwd` = \''.pSQL($this->context->cookie->tracking_passwd).'\'');
  152. return isset($result['id_referrer']) ? $result['id_referrer'] : false;
  153. }
  154. public function displayLogin()
  155. {
  156. return $this->display(__FILE__, 'login.tpl');
  157. }
  158. public function displayAccount()
  159. {
  160. if (!isset($this->context->cookie->stats_date_from))
  161. $this->context->cookie->stats_date_from = date('Y-m-01');
  162. if (!isset($this->context->cookie->stats_date_to))
  163. $this->context->cookie->stats_date_to = date('Y-m-t');
  164. Referrer::refreshCache(array(array('id_referrer' => (int)$this->context->cookie->tracking_id)));
  165. $referrer = new Referrer((int)$this->context->cookie->tracking_id);
  166. $this->smarty->assign('referrer', $referrer);
  167. $this->smarty->assign('datepickerFrom', $this->context->cookie->stats_date_from);
  168. $this->smarty->assign('datepickerTo', $this->context->cookie->stats_date_to);
  169. $display_tab = array(
  170. 'uniqs' => $this->l('Unique visitors'),
  171. 'visitors' => $this->l('Visitors'),
  172. 'visits' => $this->l('Visits'),
  173. 'pages' => $this->l('Pages viewed'),
  174. 'registrations' => $this->l('Registrations'),
  175. 'orders' => $this->l('Orders'),
  176. 'base_fee' => $this->l('Base fee'),
  177. 'percent_fee' => $this->l('Percent fee'),
  178. 'click_fee' => $this->l('Click fee'),
  179. 'sales' => $this->l('Sales'),
  180. 'cart' => $this->l('Average cart'),
  181. 'reg_rate' => $this->l('Registration rate'),
  182. 'order_rate' => $this->l('Order rate')
  183. );
  184. $this->smarty->assign('displayTab', $display_tab);
  185. $products = Product::getSimpleProducts($this->context->language->id);
  186. $products_array = array();
  187. foreach ($products as $product)
  188. $products_array[] = $product['id_product'];
  189. $js_files = array();
  190. $jquery_files = Media::getJqueryPath();
  191. if (is_array($jquery_files))
  192. $js_files = array_merge($js_files, $jquery_files);
  193. else
  194. $js_files[] = $jquery_files;
  195. $jquery_ui_files = Media::getJqueryUIPath('ui.datepicker', 'base', true);
  196. $js_files = array_merge($js_files, $jquery_ui_files['js']);
  197. $css_files = $jquery_ui_files['css'];
  198. $js_files[] = $this->_path.'js/trackingfront.js';
  199. $js_tpl_var = array(
  200. 'product_ids' => implode(', ', $products_array),
  201. 'referrer_id' => $referrer->id,
  202. 'token' => $this->context->cookie->tracking_passwd,
  203. 'display_tab' => implode('", "', array_keys($display_tab))
  204. );
  205. $this->smarty->assign(array(
  206. 'js' => $js_files,
  207. 'css' => $css_files,
  208. 'js_tpl_var' => $js_tpl_var
  209. ));
  210. return $this->display(__FILE__, 'views/templates/front/account.tpl');
  211. }
  212. }