Files
prestashop-session/src/PageViewsTable.php
info@myprestarocks 0feba3038f Add .gitignore and .htaccess for security
🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-31 19:40:28 +01:00

190 lines
5.0 KiB
PHP

<?php
/**
* Page Views Table - Detailed page view tracking
*
* Handles installation and management of the mpr_page_views table.
* This is an optional table for detailed funnel analysis.
*
* Usage:
* use MyPrestaRocks\Session\PageViewsTable;
*
* // In module install():
* PageViewsTable::install();
*
* // In module uninstall():
* PageViewsTable::uninstall('yourmodulename');
*
* @author mypresta.rocks <info@mypresta.rocks>
* @copyright Copyright (c) mypresta.rocks
* @license MIT
*/
namespace MyPrestaRocks\Session;
if (!defined('_PS_VERSION_')) {
exit;
}
class PageViewsTable
{
/**
* Table name without prefix
*/
private const TABLE_NAME = 'mpr_page_views';
/**
* Get full table name with prefix
*
* @return string
*/
public static function getTableName()
{
return _DB_PREFIX_ . self::TABLE_NAME;
}
/**
* Check if the table exists
*
* @return bool
*/
public static function tableExists()
{
$sql = "SHOW TABLES LIKE '" . self::getTableName() . "'";
return (bool) \Db::getInstance()->getValue($sql);
}
/**
* Install the page views table
*
* @return bool
*/
public static function install()
{
$engine = _MYSQL_ENGINE_;
$charset = 'utf8mb4';
$table = self::getTableName();
$sql = "CREATE TABLE IF NOT EXISTS `{$table}` (
`id_page_view` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`id_session` INT(10) UNSIGNED NOT NULL,
`page_type` TINYINT(2) UNSIGNED NOT NULL,
`page_id` INT(10) UNSIGNED DEFAULT NULL,
`controller` VARCHAR(64) DEFAULT NULL,
`date_add` DATETIME NOT NULL,
PRIMARY KEY (`id_page_view`),
KEY `id_session` (`id_session`),
KEY `page_type` (`page_type`),
KEY `date_add` (`date_add`),
KEY `session_date` (`id_session`, `date_add`)
) ENGINE={$engine} DEFAULT CHARSET={$charset};";
return \Db::getInstance()->execute($sql);
}
/**
* Uninstall the page views table
*
* @param string $currentModule The module being uninstalled
* @return bool
*/
public static function uninstall($currentModule)
{
foreach (SessionTable::getModulesUsingSession() as $moduleName) {
if ($moduleName === $currentModule) {
continue;
}
if (\Module::isInstalled($moduleName)) {
return true;
}
}
return \Db::getInstance()->execute("DROP TABLE IF EXISTS `" . self::getTableName() . "`");
}
/**
* Track detailed page view
*
* @param int $idSession Session ID
* @param int $pageType Page type constant
* @param int|null $pageId Page ID
* @param string|null $controller Controller name
* @return bool
*/
public static function trackPageView($idSession, $pageType, $pageId = null, $controller = null)
{
if (!$idSession) {
return false;
}
$table = self::getTableName();
$sql = "INSERT INTO `{$table}` (id_session, page_type, page_id, controller, date_add)
VALUES (" . (int) $idSession . ", " . (int) $pageType . ", " .
($pageId !== null ? (int) $pageId : 'NULL') . ", " .
($controller ? "'" . pSQL(substr($controller, 0, 64)) . "'" : 'NULL') . ", NOW())";
return \Db::getInstance()->execute($sql);
}
/**
* Get page views for a session
*
* @param int $idSession Session ID
* @param int $limit Max records to return
* @return array
*/
public static function getPageViewsForSession($idSession, $limit = 100)
{
$table = self::getTableName();
$sql = "SELECT * FROM `{$table}`
WHERE id_session = " . (int) $idSession . "
ORDER BY date_add ASC
LIMIT " . (int) $limit;
return \Db::getInstance()->executeS($sql) ?: [];
}
/**
* Get page view statistics for a session
*
* @param int $idSession Session ID
* @return array
*/
public static function getPageViewStats($idSession)
{
$table = self::getTableName();
$sql = "SELECT page_type, COUNT(*) as count
FROM `{$table}`
WHERE id_session = " . (int) $idSession . "
GROUP BY page_type";
$byType = \Db::getInstance()->executeS($sql) ?: [];
$stats = ['by_type' => []];
foreach ($byType as $row) {
$stats['by_type'][$row['page_type']] = (int) $row['count'];
}
return $stats;
}
/**
* Clean old page view records
*
* @param int $hours Hours to keep
* @return bool
*/
public static function cleanOldPageViews($hours = 24)
{
$table = self::getTableName();
return \Db::getInstance()->execute(
"DELETE FROM `{$table}`
WHERE date_add < DATE_SUB(NOW(), INTERVAL " . (int) $hours . " HOUR)"
);
}
}