-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #269 from PrestaSafe/pr/268-fixes-optimise
Pr/268 fixes optimise
- Loading branch information
Showing
12 changed files
with
329 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { createToaster } from '@meforma/vue-toaster' | ||
const toaster = createToaster(); | ||
let numberOfEditors = 1; | ||
import { HttpClient } from '../services/HttpClient.js'; | ||
import { trans } from '../scripts/trans.js'; | ||
/** | ||
* | ||
* Checks the number of employees connected to the prettyblocks backoffice. | ||
* If there is more than one employee, an alert is displayed. | ||
* | ||
*/ | ||
export function verifyConnectedEmployees() { | ||
|
||
let formData = new FormData(); | ||
formData.append('action', 'employeeAlert'); | ||
formData.append('session_id', sessionToken); | ||
formData.append('ajax', 1); | ||
|
||
if (typeof ajaxEditingUrl == "undefined") { | ||
console.error('Url is missing.') | ||
return; | ||
} | ||
|
||
HttpClient.post(ajaxEditingUrl, Object.fromEntries(formData)) | ||
.then(data => { | ||
if (data.success && data.number_of_editors) { | ||
numberOfEditors = data.number_of_editors; | ||
let alert_message = trans('alert_message').replace('{{ number }}', numberOfEditors); | ||
if (numberOfEditors > 1) { | ||
toaster.error( | ||
alert_message, | ||
{ | ||
position: "bottom", | ||
duration: 5000, | ||
max: 1, | ||
} | ||
); | ||
} | ||
} else { | ||
console.error('Error : ', data.error); | ||
} | ||
}) | ||
.catch(e => { | ||
console.error('Error during HTTP request: ', e); | ||
}); | ||
} | ||
|
||
// Run first time | ||
verifyConnectedEmployees(); | ||
|
||
|
||
// Run every 40 seconds | ||
setInterval(verifyConnectedEmployees, 40000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
namespace PrestaSafe\PrettyBlocks\DataPersister; | ||
|
||
use Module; | ||
use PrettyBlocksMigrate; | ||
|
||
final class ConnectedEmployeeDataPersister | ||
{ | ||
/** | ||
* Delete old employee and insert new | ||
* | ||
* @param int $id_user | ||
* @param string $session_id | ||
* | ||
* @return bool | ||
*/ | ||
public static function insert(int $id_user, string $session_id): bool | ||
{ | ||
try { | ||
// Delete old employee (more than 1 min) | ||
ConnectedEmployeeDataPersister::cleanData(); | ||
|
||
return \Db::getInstance()->execute( | ||
' | ||
INSERT INTO `' . _DB_PREFIX_ . 'prettyblocks_connected_employee` (id_user, session_id, last_update) | ||
VALUES (' . | ||
(int) pSQL($id_user) . ', "' . | ||
pSQL($session_id) . '", "' . | ||
date('Y-m-d H:i:s') . | ||
'") | ||
ON DUPLICATE KEY UPDATE last_update = "' . date('Y-m-d H:i:s') . '"' | ||
); | ||
} catch (\Exception $e) { | ||
new \PrestaShopException('Error while inserting connected employee', 0, $e); | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Update the date of the connected employee | ||
* | ||
* @param string $session_id | ||
* | ||
* @return bool | ||
*/ | ||
public static function update(string $session_id): bool | ||
{ | ||
try { | ||
ConnectedEmployeeDataPersister::cleanData(); | ||
|
||
return \Db::getInstance()->update( | ||
'prettyblocks_connected_employee', | ||
['last_update' => date('Y-m-d H:i:s')], | ||
'session_id = \'' . pSQL($session_id) . '\'' | ||
); | ||
} catch (\Exception $e) { | ||
|
||
new \PrestaShopException('Error while updating connected employee', 0, $e); | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Delete old employee (more than 1 min) | ||
* | ||
* @return bool | ||
*/ | ||
public static function cleanData(): bool | ||
{ | ||
try { | ||
$database = \Db::getInstance(); | ||
$thresholdTime = date('Y-m-d H:i:s', strtotime('-1 minute')); | ||
|
||
$sql = ' | ||
DELETE FROM ' . _DB_PREFIX_ . 'prettyblocks_connected_employee | ||
WHERE last_update < "' . pSQL($thresholdTime) . '"' | ||
; | ||
|
||
return $database->execute($sql); | ||
} catch (\Exception $e) { | ||
|
||
new \PrestaShopException('Error while cleaning connected employee', 0, $e); | ||
return false; | ||
} | ||
} | ||
|
||
public static function tableExists(): void | ||
{ | ||
if (!PrettyBlocksMigrate::tableExists('prettyblocks_connected_employee')) { | ||
dump('table not exists'); | ||
Module::getInstanceByName('prettyblocks')->makeConnectedEmployee(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); | ||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); | ||
|
||
header('Cache-Control: no-store, no-cache, must-revalidate'); | ||
header('Cache-Control: post-check=0, pre-check=0', false); | ||
header('Pragma: no-cache'); | ||
|
||
header('Location: ../'); | ||
exit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace PrestaSafe\PrettyBlocks\DataProvider; | ||
|
||
final class ConnectedEmployeeDataProvider | ||
{ | ||
/** | ||
* Return the number of connected employees on the page | ||
* | ||
* @return int|null | ||
*/ | ||
public static function get(): ?int | ||
{ | ||
try { | ||
$sql = ' | ||
SELECT COUNT(*) | ||
FROM `' . _DB_PREFIX_ . 'prettyblocks_connected_employee` | ||
WHERE last_update > DATE_SUB(NOW(), INTERVAL 60 SECOND) | ||
'; | ||
|
||
return (int) \Db::getInstance()->getValue($sql); | ||
} catch (\Exception $e) { | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); | ||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); | ||
|
||
header('Cache-Control: no-store, no-cache, must-revalidate'); | ||
header('Cache-Control: post-check=0, pre-check=0', false); | ||
header('Pragma: no-cache'); | ||
|
||
header('Location: ../'); | ||
exit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.