PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/producttooltip/producttooltip.php

https://gitlab.com/sutrix.hoa.tran/Research-Prestashop
PHP | 283 lines | 225 code | 28 blank | 30 comment | 27 complexity | bd29017d0aabe5628c66401050f80fb8 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 ProductToolTip extends Module
  29. {
  30. public function __construct()
  31. {
  32. $this->name = 'producttooltip';
  33. $this->tab = 'front_office_features';
  34. $this->version = '1.4.0';
  35. $this->author = 'PrestaShop';
  36. $this->need_instance = 0;
  37. $this->bootstrap = true;
  38. parent::__construct();
  39. $this->displayName = $this->l('Product tooltips');
  40. $this->description = $this->l('Shows information on a product page: how many people are viewing it, the last time it was sold and the last time it was added to a cart.');
  41. }
  42. public function install()
  43. {
  44. if (!parent::install())
  45. return false;
  46. /* Default configuration values */
  47. Configuration::updateValue('PS_PTOOLTIP_PEOPLE', 1);
  48. Configuration::updateValue('PS_PTOOLTIP_DATE_CART', 1);
  49. Configuration::updateValue('PS_PTOOLTIP_DATE_ORDER', 1);
  50. Configuration::updateValue('PS_PTOOLTIP_DAYS', 3);
  51. Configuration::updateValue('PS_PTOOLTIP_LIFETIME', 30);
  52. return $this->registerHook('header') && $this->registerHook('productfooter');
  53. }
  54. public function uninstall()
  55. {
  56. if (!Configuration::deleteByName('PS_PTOOLTIP_PEOPLE')
  57. || !Configuration::deleteByName('PS_PTOOLTIP_DATE_CART')
  58. || !Configuration::deleteByName('PS_PTOOLTIP_DATE_ORDER')
  59. || !Configuration::deleteByName('PS_PTOOLTIP_DAYS')
  60. || !Configuration::deleteByName('PS_PTOOLTIP_LIFETIME')
  61. || !parent::uninstall()
  62. )
  63. return false;
  64. return true;
  65. }
  66. public function getContent()
  67. {
  68. $html = '';
  69. /* Update values in DB */
  70. if (Tools::isSubmit('SubmitToolTip'))
  71. {
  72. Configuration::updateValue('PS_PTOOLTIP_PEOPLE', (int)Tools::getValue('PS_PTOOLTIP_PEOPLE'));
  73. Configuration::updateValue('PS_PTOOLTIP_DATE_CART', (int)Tools::getValue('PS_PTOOLTIP_DATE_CART'));
  74. Configuration::updateValue('PS_PTOOLTIP_DATE_ORDER', (int)Tools::getValue('PS_PTOOLTIP_DATE_ORDER'));
  75. Configuration::updateValue('PS_PTOOLTIP_DAYS', ((int)(Tools::getValue('PS_PTOOLTIP_DAYS') < 0 ? 0 : (int)Tools::getValue('PS_PTOOLTIP_DAYS'))));
  76. Configuration::updateValue('PS_PTOOLTIP_LIFETIME', ((int)(Tools::getValue('PS_PTOOLTIP_LIFETIME') < 0 ? 0 : (int)Tools::getValue('PS_PTOOLTIP_LIFETIME'))));
  77. $html .= $this->displayConfirmation($this->l('Settings updated'));
  78. }
  79. /* Configuration form */
  80. return $html.$this->renderForm();
  81. }
  82. public function hookHeader($params)
  83. {
  84. $this->context->controller->addJQueryPlugin('growl');
  85. }
  86. public function hookProductFooter($params)
  87. {
  88. $id_product = (int)$params['product']->id;
  89. /* First we try to display the number of people who are currently watching this product page */
  90. if (Configuration::get('PS_PTOOLTIP_PEOPLE'))
  91. {
  92. $date = strftime('%Y-%m-%d %H:%M:%S', time() - (int)(Configuration::get('PS_PTOOLTIP_LIFETIME') * 60));
  93. $nb_people = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
  94. SELECT COUNT(DISTINCT(id_connections)) nb
  95. FROM '._DB_PREFIX_.'page p
  96. LEFT JOIN '._DB_PREFIX_.'connections_page cp ON (p.id_page = cp.id_page)
  97. WHERE p.id_page_type = 1 AND p.id_object = '.(int)$id_product.' AND cp.time_start > \''.pSQL($date).'\'');
  98. if (isset($nb_people['nb']) && $nb_people['nb'] > 0)
  99. $this->smarty->assign('nb_people', (int)$nb_people['nb']);
  100. }
  101. /* Then, we try to display last sale */
  102. if (Configuration::get('PS_PTOOLTIP_DATE_ORDER'))
  103. {
  104. $days = (int)Configuration::get('PS_PTOOLTIP_DAYS');
  105. $date = strftime('%Y-%m-%d', strtotime('-'.(int)$days.' day'));
  106. $order = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
  107. SELECT o.date_add
  108. FROM '._DB_PREFIX_.'order_detail od
  109. LEFT JOIN '._DB_PREFIX_.'orders o ON (od.id_order = o.id_order)
  110. WHERE od.product_id = '.(int)$id_product.' AND o.date_add >= \''.pSQL($date).'\'
  111. ORDER BY o.date_add DESC');
  112. if (isset($order['date_add']) && Validate::isDateFormat($order['date_add']) && $order['date_add'] != '0000-00-00 00:00:00')
  113. $this->smarty->assign('date_last_order', $order['date_add']);
  114. else
  115. {
  116. /* No sale? display last cart add instead */
  117. if (Configuration::get('PS_PTOOLTIP_DATE_CART'))
  118. {
  119. $cart = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
  120. SELECT cp.date_add
  121. FROM '._DB_PREFIX_.'cart_product cp
  122. WHERE cp.id_product = '.(int)$id_product);
  123. if (isset($cart['date_add']) && Validate::isDateFormat($cart['date_add']) && $cart['date_add'] != '0000-00-00 00:00:00')
  124. $this->smarty->assign('date_last_cart', $cart['date_add']);
  125. }
  126. }
  127. }
  128. if ((isset($nb_people['nb']) && $nb_people['nb'] > 0) || isset($order['date_add']) || isset($cart['date_add']))
  129. return $this->display(__FILE__, 'producttooltip.tpl');
  130. }
  131. public function renderForm()
  132. {
  133. $fields_form = array(
  134. 'form' => array(
  135. 'legend' => array(
  136. 'title' => $this->l('Settings'),
  137. 'icon' => 'icon-cogs'
  138. ),
  139. 'input' => array(
  140. array(
  141. 'type' => 'switch',
  142. 'label' => $this->l('Number of visitors'),
  143. 'desc' => $this->l('Display the number of visitors who are currently watching this product.').'<br>'.
  144. $this->l('If you activate the option above, you must activate the first option ("Save page views for each customer") of the "Data mining for statistics" (StatsData) module.'),
  145. 'name' => 'PS_PTOOLTIP_PEOPLE',
  146. 'values' => array(
  147. array(
  148. 'id' => 'active_on',
  149. 'value' => 1,
  150. 'label' => $this->l('Enabled')
  151. ),
  152. array(
  153. 'id' => 'active_off',
  154. 'value' => 0,
  155. 'label' => $this->l('Disabled')
  156. )
  157. ),
  158. ),
  159. array(
  160. 'type' => 'text',
  161. 'label' => $this->l('Period length'),
  162. 'desc' => $this->l('Set the reference period length.').'<br>'.
  163. $this->l('For instance, if set to 30 minutes, the module will display the number of visitors in the last 30 minutes.'),
  164. 'name' => 'PS_PTOOLTIP_LIFETIME',
  165. 'suffix' => $this->l('minutes'),
  166. 'values' => array(
  167. array(
  168. 'id' => 'active_on',
  169. 'value' => 1,
  170. 'label' => $this->l('Enabled')
  171. ),
  172. array(
  173. 'id' => 'active_off',
  174. 'value' => 0,
  175. 'label' => $this->l('Disabled')
  176. )
  177. ),
  178. ),
  179. array(
  180. 'type' => 'switch',
  181. 'label' => $this->l('Last order date'),
  182. 'desc' => $this->l('Display the last time the product has been ordered.'),
  183. 'name' => 'PS_PTOOLTIP_DATE_ORDER',
  184. 'values' => array(
  185. array(
  186. 'id' => 'active_on',
  187. 'value' => 1,
  188. 'label' => $this->l('Enabled')
  189. ),
  190. array(
  191. 'id' => 'active_off',
  192. 'value' => 0,
  193. 'label' => $this->l('Disabled')
  194. )
  195. ),
  196. ),
  197. array(
  198. 'type' => 'switch',
  199. 'label' => $this->l('Added to a cart'),
  200. 'desc' => $this->l('If the product has not been ordered yet, display the last time it was added to a cart.'),
  201. 'name' => 'PS_PTOOLTIP_DATE_CART',
  202. 'values' => array(
  203. array(
  204. 'id' => 'active_on',
  205. 'value' => 1,
  206. 'label' => $this->l('Enabled')
  207. ),
  208. array(
  209. 'id' => 'active_off',
  210. 'value' => 0,
  211. 'label' => $this->l('Disabled')
  212. )
  213. ),
  214. ),
  215. array(
  216. 'type' => 'text',
  217. 'label' => $this->l('Do not display events older than'),
  218. 'name' => 'PS_PTOOLTIP_DAYS',
  219. 'suffix' => $this->l('days')
  220. ),
  221. ),
  222. 'submit' => array(
  223. 'title' => $this->l('Save'),
  224. )
  225. ),
  226. );
  227. $helper = new HelperForm();
  228. $helper->show_toolbar = false;
  229. $helper->table = $this->table;
  230. $lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
  231. $helper->default_form_language = $lang->id;
  232. $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
  233. $this->fields_form = array();
  234. $helper->identifier = $this->identifier;
  235. $helper->submit_action = 'SubmitToolTip';
  236. $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
  237. $helper->token = Tools::getAdminTokenLite('AdminModules');
  238. $helper->tpl_vars = array(
  239. 'fields_value' => $this->getConfigFieldsValues(),
  240. 'languages' => $this->context->controller->getLanguages(),
  241. 'id_language' => $this->context->language->id
  242. );
  243. return $helper->generateForm(array($fields_form));
  244. }
  245. public function getConfigFieldsValues()
  246. {
  247. return array(
  248. 'PS_PTOOLTIP_PEOPLE' => Tools::getValue('PS_PTOOLTIP_PEOPLE', Configuration::get('PS_PTOOLTIP_PEOPLE')),
  249. 'PS_PTOOLTIP_LIFETIME' => Tools::getValue('PS_PTOOLTIP_LIFETIME', Configuration::get('PS_PTOOLTIP_LIFETIME')),
  250. 'PS_PTOOLTIP_DATE_ORDER' => Tools::getValue('PS_PTOOLTIP_DATE_ORDER', Configuration::get('PS_PTOOLTIP_DATE_ORDER')),
  251. 'PS_PTOOLTIP_DATE_CART' => Tools::getValue('PS_PTOOLTIP_DATE_CART', Configuration::get('PS_PTOOLTIP_DATE_CART')),
  252. 'PS_PTOOLTIP_DAYS' => Tools::getValue('PS_PTOOLTIP_DAYS', Configuration::get('PS_PTOOLTIP_DAYS')),
  253. );
  254. }
  255. }