Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
Panel notifications refactoring
  • Loading branch information
sergejey committed Mar 30, 2022
1 parent a541c7e commit 5dd5a83
Show file tree
Hide file tree
Showing 9 changed files with 660 additions and 677 deletions.
34 changes: 15 additions & 19 deletions lib/module.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -828,56 +828,52 @@ public function codeParams($in)
return $res_str;
}

public function sendnotification($str, $type = 'default')
public function sendNotification($str, $type = 'default')
{

if (!SQLTableExists('plugins_noty')) {
if (!SQLTableExists('module_notifications')) {
return json_encode(array('status'=>false));
}

if($type != 'info' && $type != 'danger' && $type != 'warning' && $type != 'success' && $type != 'default') {
$type = 'default';
}

$rec["ADD"] = time();
$rec["ADDED"] = date('Y-m-d H:i:s');
$rec["TYPE"] = $type;
$rec["PLUGINS_ID"] = SQLSelectOne("SELECT `ID` FROM `plugins` WHERE `MODULE_NAME` = '".$this->name."'")['ID'];
$rec["MODULE_NAME"] = $this->name;
$rec["MESSAGE"] = htmlspecialchars(strip_tags($str));
$rec["READ"] = 0;

if(!$rec["PLUGINS_ID"]) return json_encode(array('status' => false, 'error' => 'Only extension plugins allowed! Not system plugins!'));

if(SQLSelectOne("SELECT COUNT(*) AS TOTAL_UNREAD FROM `plugins_noty` WHERE `PLUGINS_ID` = '".$rec["PLUGINS_ID"]."' AND `READ` = '0'")['TOTAL_UNREAD'] > 10) {
if(SQLSelectOne("SELECT COUNT(*) AS TOTAL_UNREAD FROM `module_notifications` WHERE `MODULE_NAME` = '".$rec["MODULE_NAME"]."' AND `IS_READ` = '0'")['TOTAL_UNREAD'] > 10) {
return json_encode(array('status' => false, 'error' => 'More than 10 notifications in the unread status.'));
}

$ifExest = SQLSelectOne("SELECT ID FROM `plugins_noty` WHERE `MESSAGE` = '".$str."' AND `READ` = '0'");

if(!empty($ifExest) && is_array($ifExest)) {
return json_encode(array('status' => false, 'error' => 'Such a record already exists. ID - '.$ifExest['ID'], 'id' => $ifExest['ID']));
$ifExist = SQLSelectOne("SELECT ID FROM `module_notifications` WHERE `MESSAGE` = '".DBSafe($rec['MESSAGE'])."' AND TYPE='".DBSafe($rec['TYPE'])."' AND `IS_READ` = '0'");
if(!empty($ifExist) && is_array($ifExist)) {
return json_encode(array('status' => false, 'error' => 'Notification already exists. ID: '.$ifExist['ID'], 'id' => $ifExist['ID']));
}

$rec["ID"] = SQLInsert("plugins_noty", $rec);
$rec["ID"] = SQLInsert("module_notifications", $rec);

return json_encode(array('status' => true, 'id' => $rec["ID"]));

}

public function readnotification($noty_id)
public function readNotification($notification_id)
{

if (!SQLTableExists('plugins_noty')) {
if (!SQLTableExists('module_notifications')) {
return json_encode(array('status'=>false));
}

$rec["ID"] = $noty_id;
$rec["READ"] = 1;
$rec["ID"] = $notification_id;
$rec["IS_READ"] = 1;

if(empty(SQLSelectOne("SELECT ID FROM `plugins_noty` WHERE `ID` = '".$rec["ID"]."' AND `READ` = '0'")['ID'])) {
if(empty(SQLSelectOne("SELECT ID FROM `module_notifications` WHERE `ID` = '".$rec["ID"]."' AND `IS_READ` = '0'")['ID'])) {
return json_encode(array('status' => false, 'error' => 'No such noty found'));
}

SQLUpdate("plugins_noty", $rec);
SQLUpdate("module_notifications", $rec);

return json_encode(array('status' => true));

Expand Down
6 changes: 6 additions & 0 deletions modules/control_modules/control_modules.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,12 @@ function dbInstall($data)
ignore_updates: ID tinyint(3) unsigned NOT NULL auto_increment
ignore_updates: NAME varchar(50) DEFAULT '' NOT NULL
module_notifications: ID int(10) unsigned NOT NULL auto_increment
module_notifications: MODULE_NAME char(50) NOT NULL DEFAULT ''
module_notifications: MESSAGE varchar(255) NOT NULL DEFAULT ''
module_notifications: TYPE char(20) NOT NULL DEFAULT 'info'
module_notifications: IS_READ int(3) NOT NULL DEFAULT 0
module_notifications: ADDED datetime
EOD;
parent::dbInstall($data);
Expand Down
7 changes: 7 additions & 0 deletions modules/inc_panel_ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ function evalConsole($code, $print = 0) {
}
}

if ($op == 'dismiss_notification') {
$id = gr('id','int');
SQLExec("UPDATE module_notifications SET IS_READ=1 WHERE ID=".$id);
echo "OK";
exit;
}

if ($op == 'console') {
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
Expand Down
56 changes: 28 additions & 28 deletions modules/panel.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,19 @@ function run()
WHERE (`HIDDEN`='0' OR NAME='control_modules')
ORDER BY FIELD(CATEGORY, '<#LANG_SECTION_OBJECTS#>', '<#LANG_SECTION_DEVICES#>', '<#LANG_SECTION_APPLICATIONS#>',
'<#LANG_SECTION_SETTINGS#>', '<#LANG_SECTION_SYSTEM#>'),
FIELD(`NAME`,'classes','devices','settings','system_errors','xray','connect','saverestore','market') DESC,
`PRIORITY`, `TITLE`";

$modules = SQLSelect($sqlQuery);
$old_cat = 'some_never_should_be_category_name';
$modulesCnt = count($modules);

if (SQLTableExists('plugins_noty')) {
$getNOTY = SQLSelect("select pln.*, pl.MODULE_NAME from plugins_noty pln join plugins pl on pln.PLUGINS_ID=pl.id WHERE pln.READ = '0'");
} else {
$getNOTY = array();
$notifications = array();
if (SQLTableExists('module_notifications')) {
$getNotifications = SQLSelect("select * from module_notifications WHERE IS_READ = 0 ORDER BY ADDED DESC");
foreach($getNotifications as $notification) {
$notifications[$notification['MODULE_NAME']][]=$notification;
}
}

for ($i = 0; $i < $modulesCnt; $i++) {
Expand All @@ -189,30 +192,15 @@ function run()
} else {
$last_allow = $i;
}

foreach($getNOTY as $keyNoty => $notyValue) {
if($notyValue['MODULE_NAME'] == $modules[$i]["NAME"]) {

if(preg_match('|<#(.*?)#>|si', $notyValue['MODULE_NAME'], $arr)) {
$titleSearchNoty = constant($arr[1]);
} else {
$titleSearchNoty = $notyValue['MODULE_NAME'];
}

$modules[$i]['PLUGINS_NOTY_COUNT'] = $modules[$i]['PLUGINS_NOTY_COUNT']+1;
$modules[$i]['PLUGINS_NOTY_COLOR'] = $notyValue['TYPE'];
$modules[$i]['PLUGINS_ID'] = $notyValue['PLUGINS_ID'];

$getNOTY[$keyNoty]['ADD_HUMAN'] = date('d.m.Y H:i', $notyValue['ADD']);

$modules[$i]['PLUGINS_NOTY'][] = $getNOTY[$keyNoty];

unset($getNOTY[$keyNoty]);
} else {
$modules[$i]['PLUGINS_ID'] = $notyValue['PLUGINS_ID'] ?? null;
}
}


if (isset($notifications[$modules[$i]["NAME"]])) {
$modules[$i]["NOTIFICATIONS"]=$notifications[$modules[$i]["NAME"]];
$modules[$i]["NOTIFICATIONS_COUNT"]=count($modules[$i]["NOTIFICATIONS"]);
$modules[$i]["NOTIFICATIONS_TYPE"]=$modules[$i]["NOTIFICATIONS"][0]['TYPE'];
} else {
$modules[$i]["NOTIFICATIONS_COUNT"]=0;
}

if (file_exists(ROOT . 'img/modules/' . $modules[$i]['NAME'] . '.png')) {
$modules[$i]['ICON_SM'] = ROOTHTML . 'img/modules/' . $modules[$i]['NAME'] . '.png';
} else {
Expand All @@ -234,6 +222,18 @@ function run()
$out["SUB_MODULES"] = $modules;
}

if ($this->action && isset($notifications[$this->action])) {
$total = count($notifications[$this->action]);
for($i=0;$i<$total;$i++) {
$notifications[$this->action][$i]['ADDED']=date('d.m.Y H:i:s',strtotime($notifications[$this->action][$i]['ADDED']));
if ($notifications[$this->action][$i]['TYPE']=='default') {
$notifications[$this->action][$i]['TYPE']='info';
}

}
$out['MODULE_NOTIFICATIONS']=$notifications[$this->action];
}

if (is_dir(DIR_MODULES . 'app_tdwiki')) {
$out['APP_TDWIKI_INSTALLED'] = 1;
}
Expand Down
Loading

0 comments on commit 5dd5a83

Please sign in to comment.