* @copyright 2007-2011 PrestaShop SA * @version Release: $Revision: 7307 $ * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA */ if (!defined('_PS_VERSION_')) exit; class StatsSearch extends ModuleGraph { private $_html = ''; private $_query = ''; private $_query2 = ''; function __construct() { $this->name = 'statssearch'; $this->tab = 'analytics_stats'; $this->version = 1.0; $this->author = 'PrestaShop'; $this->need_instance = 0; parent::__construct(); $this->_query = 'SELECT `keywords`, COUNT(TRIM(`keywords`)) as occurences, MAX(results) as total FROM `'._DB_PREFIX_.'statssearch` WHERE 1 '.$this->sqlShopRestriction().' AND `date_add` BETWEEN '; $this->_query2 = 'GROUP BY `keywords` HAVING occurences > 1 ORDER BY occurences DESC'; $this->displayName = $this->l('Shop search'); $this->description = $this->l('Display which keywords have been searched by your visitors.'); } function install() { if (!parent::install() OR !$this->registerHook('search') OR !$this->registerHook('AdminStatsModules')) return false; return Db::getInstance()->execute(' CREATE TABLE `'._DB_PREFIX_.'statssearch` ( id_statssearch INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, id_shop INTEGER UNSIGNED NOT NULL DEFAULT \'1\', id_group_shop INTEGER UNSIGNED NOT NULL DEFAULT \'1\', keywords VARCHAR(255) NOT NULL, results INT(6) NOT NULL DEFAULT 0, date_add DATETIME NOT NULL, PRIMARY KEY(id_statssearch) ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8'); } function uninstall() { if (!parent::uninstall()) return false; return (Db::getInstance()->execute('DROP TABLE `'._DB_PREFIX_.'statssearch`')); } /** * Insert keywords in statssearch table when a search is launched on FO */ function hookSearch($params) { $sql = 'INSERT INTO `'._DB_PREFIX_.'statssearch` (`id_shop`, `id_group_shop`, `keywords`, `results`, `date_add`) VALUES ('.$this->context->shop->getID(true).', '.$this->context->shop->getGroupID().', \''.pSQL($params['expr']).'\', '.(int)$params['total'].', NOW())'; Db::getInstance()->execute($sql); } function hookAdminStatsModules() { if (Tools::getValue('export')) $this->csvExport(array('type' => 'pie')); $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($this->_query.ModuleGraph::getDateBetween().$this->_query2); $this->_html = '
'; return $this->_html; } protected function getData($layers) { $this->_titles['main'] = $this->l('First 10 keywords'); $totalResult = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($this->_query.$this->getDate().$this->_query2); $total = 0; $total2 = 0; foreach ($totalResult as $totalRow) $total += $totalRow['occurences']; $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($this->_query.$this->getDate().$this->_query2.' LIMIT 9'); foreach ($result as $row) { if (!$row['occurences']) continue; $this->_legend[] = $row['keywords']; $this->_values[] = $row['occurences']; $total2 += $row['occurences']; } if ($total > $total2) { $this->_legend[] = $this->l('Others'); $this->_values[] = $total - $total2; } } }