Skip to content

Commit 5dd5a83

Browse files
committed
Refactoring
Panel notifications refactoring
1 parent a541c7e commit 5dd5a83

9 files changed

Lines changed: 660 additions & 677 deletions

File tree

lib/module.class.php

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -828,56 +828,52 @@ public function codeParams($in)
828828
return $res_str;
829829
}
830830

831-
public function sendnotification($str, $type = 'default')
831+
public function sendNotification($str, $type = 'default')
832832
{
833833

834-
if (!SQLTableExists('plugins_noty')) {
834+
if (!SQLTableExists('module_notifications')) {
835835
return json_encode(array('status'=>false));
836836
}
837837

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

842-
$rec["ADD"] = time();
842+
$rec["ADDED"] = date('Y-m-d H:i:s');
843843
$rec["TYPE"] = $type;
844-
$rec["PLUGINS_ID"] = SQLSelectOne("SELECT `ID` FROM `plugins` WHERE `MODULE_NAME` = '".$this->name."'")['ID'];
844+
$rec["MODULE_NAME"] = $this->name;
845845
$rec["MESSAGE"] = htmlspecialchars(strip_tags($str));
846-
$rec["READ"] = 0;
847-
848-
if(!$rec["PLUGINS_ID"]) return json_encode(array('status' => false, 'error' => 'Only extension plugins allowed! Not system plugins!'));
849846

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

854-
$ifExest = SQLSelectOne("SELECT ID FROM `plugins_noty` WHERE `MESSAGE` = '".$str."' AND `READ` = '0'");
855-
856-
if(!empty($ifExest) && is_array($ifExest)) {
857-
return json_encode(array('status' => false, 'error' => 'Such a record already exists. ID - '.$ifExest['ID'], 'id' => $ifExest['ID']));
851+
$ifExist = SQLSelectOne("SELECT ID FROM `module_notifications` WHERE `MESSAGE` = '".DBSafe($rec['MESSAGE'])."' AND TYPE='".DBSafe($rec['TYPE'])."' AND `IS_READ` = '0'");
852+
if(!empty($ifExist) && is_array($ifExist)) {
853+
return json_encode(array('status' => false, 'error' => 'Notification already exists. ID: '.$ifExist['ID'], 'id' => $ifExist['ID']));
858854
}
859855

860-
$rec["ID"] = SQLInsert("plugins_noty", $rec);
856+
$rec["ID"] = SQLInsert("module_notifications", $rec);
861857

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

864860
}
865861

866-
public function readnotification($noty_id)
862+
public function readNotification($notification_id)
867863
{
868864

869-
if (!SQLTableExists('plugins_noty')) {
865+
if (!SQLTableExists('module_notifications')) {
870866
return json_encode(array('status'=>false));
871867
}
872868

873-
$rec["ID"] = $noty_id;
874-
$rec["READ"] = 1;
869+
$rec["ID"] = $notification_id;
870+
$rec["IS_READ"] = 1;
875871

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

880-
SQLUpdate("plugins_noty", $rec);
876+
SQLUpdate("module_notifications", $rec);
881877

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

modules/control_modules/control_modules.class.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,12 @@ function dbInstall($data)
300300
ignore_updates: ID tinyint(3) unsigned NOT NULL auto_increment
301301
ignore_updates: NAME varchar(50) DEFAULT '' NOT NULL
302302
303+
module_notifications: ID int(10) unsigned NOT NULL auto_increment
304+
module_notifications: MODULE_NAME char(50) NOT NULL DEFAULT ''
305+
module_notifications: MESSAGE varchar(255) NOT NULL DEFAULT ''
306+
module_notifications: TYPE char(20) NOT NULL DEFAULT 'info'
307+
module_notifications: IS_READ int(3) NOT NULL DEFAULT 0
308+
module_notifications: ADDED datetime
303309
304310
EOD;
305311
parent::dbInstall($data);

modules/inc_panel_ajax.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ function evalConsole($code, $print = 0) {
1818
}
1919
}
2020

21+
if ($op == 'dismiss_notification') {
22+
$id = gr('id','int');
23+
SQLExec("UPDATE module_notifications SET IS_READ=1 WHERE ID=".$id);
24+
echo "OK";
25+
exit;
26+
}
27+
2128
if ($op == 'console') {
2229
ini_set('display_errors', 0);
2330
ini_set('display_startup_errors', 0);

modules/panel.class.php

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,19 @@ function run()
158158
WHERE (`HIDDEN`='0' OR NAME='control_modules')
159159
ORDER BY FIELD(CATEGORY, '<#LANG_SECTION_OBJECTS#>', '<#LANG_SECTION_DEVICES#>', '<#LANG_SECTION_APPLICATIONS#>',
160160
'<#LANG_SECTION_SETTINGS#>', '<#LANG_SECTION_SYSTEM#>'),
161+
FIELD(`NAME`,'classes','devices','settings','system_errors','xray','connect','saverestore','market') DESC,
161162
`PRIORITY`, `TITLE`";
162163

163164
$modules = SQLSelect($sqlQuery);
164165
$old_cat = 'some_never_should_be_category_name';
165166
$modulesCnt = count($modules);
166167

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

173176
for ($i = 0; $i < $modulesCnt; $i++) {
@@ -189,30 +192,15 @@ function run()
189192
} else {
190193
$last_allow = $i;
191194
}
192-
193-
foreach($getNOTY as $keyNoty => $notyValue) {
194-
if($notyValue['MODULE_NAME'] == $modules[$i]["NAME"]) {
195-
196-
if(preg_match('|<#(.*?)#>|si', $notyValue['MODULE_NAME'], $arr)) {
197-
$titleSearchNoty = constant($arr[1]);
198-
} else {
199-
$titleSearchNoty = $notyValue['MODULE_NAME'];
200-
}
201-
202-
$modules[$i]['PLUGINS_NOTY_COUNT'] = $modules[$i]['PLUGINS_NOTY_COUNT']+1;
203-
$modules[$i]['PLUGINS_NOTY_COLOR'] = $notyValue['TYPE'];
204-
$modules[$i]['PLUGINS_ID'] = $notyValue['PLUGINS_ID'];
205-
206-
$getNOTY[$keyNoty]['ADD_HUMAN'] = date('d.m.Y H:i', $notyValue['ADD']);
207-
208-
$modules[$i]['PLUGINS_NOTY'][] = $getNOTY[$keyNoty];
209-
210-
unset($getNOTY[$keyNoty]);
211-
} else {
212-
$modules[$i]['PLUGINS_ID'] = $notyValue['PLUGINS_ID'] ?? null;
213-
}
214-
}
215-
195+
196+
if (isset($notifications[$modules[$i]["NAME"]])) {
197+
$modules[$i]["NOTIFICATIONS"]=$notifications[$modules[$i]["NAME"]];
198+
$modules[$i]["NOTIFICATIONS_COUNT"]=count($modules[$i]["NOTIFICATIONS"]);
199+
$modules[$i]["NOTIFICATIONS_TYPE"]=$modules[$i]["NOTIFICATIONS"][0]['TYPE'];
200+
} else {
201+
$modules[$i]["NOTIFICATIONS_COUNT"]=0;
202+
}
203+
216204
if (file_exists(ROOT . 'img/modules/' . $modules[$i]['NAME'] . '.png')) {
217205
$modules[$i]['ICON_SM'] = ROOTHTML . 'img/modules/' . $modules[$i]['NAME'] . '.png';
218206
} else {
@@ -234,6 +222,18 @@ function run()
234222
$out["SUB_MODULES"] = $modules;
235223
}
236224

225+
if ($this->action && isset($notifications[$this->action])) {
226+
$total = count($notifications[$this->action]);
227+
for($i=0;$i<$total;$i++) {
228+
$notifications[$this->action][$i]['ADDED']=date('d.m.Y H:i:s',strtotime($notifications[$this->action][$i]['ADDED']));
229+
if ($notifications[$this->action][$i]['TYPE']=='default') {
230+
$notifications[$this->action][$i]['TYPE']='info';
231+
}
232+
233+
}
234+
$out['MODULE_NOTIFICATIONS']=$notifications[$this->action];
235+
}
236+
237237
if (is_dir(DIR_MODULES . 'app_tdwiki')) {
238238
$out['APP_TDWIKI_INSTALLED'] = 1;
239239
}

0 commit comments

Comments
 (0)