dolibarr  17.0.4
check_notifications.php
1 <?php
2 /* Copyright (C) 2016 Sergio Sanchis <sergiosanchis@hotmail.com>
3  * Copyright (C) 2017 Juanjo Menent <jmenent@2byte.es>
4  * Copyright (C) 2019 Frédéric France <frederic.france@netlogic.fr>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 if (!defined('NOTOKENRENEWAL')) {
21  define('NOTOKENRENEWAL', '1'); // Do not roll the Anti CSRF token (used if MAIN_SECURITY_CSRF_WITH_TOKEN is on)
22 }
23 if (!defined('NOREQUIREMENU')) {
24  define('NOREQUIREMENU', '1');
25 }
26 if (!defined('NOREQUIREHTML')) {
27  define('NOREQUIREHTML', '1');
28 }
29 if (!defined('NOREQUIREAJAX')) {
30  define('NOREQUIREAJAX', '1');
31 }
32 if (!defined('NOREQUIRESOC')) {
33  define('NOREQUIRESOC', '1');
34 }
35 if (!defined('NOREQUIRETRAN')) {
36  define('NOREQUIRETRAN', '1');
37 }
38 
39 // Load Dolibarr environment
40 require '../../main.inc.php';
41 
42 //$time = (int) GETPOST('time', 'int'); // Use the time parameter that is always increased by time_update, even if call is late
43 $time = dol_now();
44 $action = GETPOST('action', 'aZ09');
45 $listofreminderids = GETPOST('listofreminderids', 'aZ09');
46 
47 
48 /*
49  * Actions
50  */
51 
52 if ($action == 'stopreminder') {
53  dol_syslog("Clear notification for listofreminderids=".$listofreminderids);
54  $listofreminderid = GETPOST('listofreminderids', 'intcomma');
55 
56  // Set the reminder as done
57  $sql = 'UPDATE '.MAIN_DB_PREFIX.'actioncomm_reminder SET status = 1';
58  $sql .= ' WHERE status = 0 AND rowid IN ('.$db->sanitize($db->escape($listofreminderid)).')';
59  $sql .= ' AND fk_user = '.((int) $user->id).' AND entity = '.((int) $conf->entity);
60  $resql = $db->query($sql);
61  if (!$resql) {
62  dol_print_error($db);
63  }
64  //}
65 
66  include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
67 
68  // Clean database
69  $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'actioncomm_reminder';
70  $sql .= " WHERE dateremind < '".$db->idate(dol_time_plus_duree(dol_now(), -1, 'm'))."'";
71  $resql = $db->query($sql);
72  if (!$resql) {
73  dol_print_error($db);
74  }
75 
76  exit;
77 }
78 
79 
80 /*
81  * View
82  */
83 
84 top_httphead('application/json');
85 
86 global $user, $db, $langs, $conf;
87 
88 $eventfound = array();
89 //Uncomment this to force a test
90 //$eventfound[]=array('type'=>'agenda', 'id'=>1, 'tipo'=>'eee', 'location'=>'aaa');
91 
92 //dol_syslog('time='.$time.' $_SESSION[auto_ck_events_not_before]='.$_SESSION['auto_check_events_not_before']);
93 
94 // TODO Try to make a solution with only a javascript timer that is easier. Difficulty is to avoid notification twice when several tabs are opened.
95 // This need to extend period to be sure to not miss and save in session what we notified to avoid duplicate.
96 if (empty($_SESSION['auto_check_events_not_before']) || $time >= $_SESSION['auto_check_events_not_before'] || GETPOST('forcechecknow', 'int')) {
97  /*$time_update = (int) $conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY; // Always defined
98  if (!empty($_SESSION['auto_check_events_not_before']))
99  {
100  // We start scan from the not before so if two tabs were opend at differents seconds and we close one (so the js timer),
101  // then we are not losing periods
102  $starttime = $_SESSION['auto_check_events_not_before'];
103  // Protection to avoid too long sessions
104  if ($starttime < ($time - (int) $conf->global->MAIN_SESSION_TIMEOUT))
105  {
106  dol_syslog("We ask to check browser notification on a too large period. We fix this with current date.");
107  $starttime = $time;
108  }
109  } else {
110  $starttime = $time;
111  }
112 
113  $_SESSION['auto_check_events_not_before'] = $time + $time_update;
114  */
115 
116  // Force save of the session change we did.
117  // WARNING: Any change in sessions after that will not be saved !
118  session_write_close();
119 
120  require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
121 
122 
123  dol_syslog('NEW $_SESSION[auto_check_events_not_before]='.(empty($_SESSION['auto_check_events_not_before']) ? '' : $_SESSION['auto_check_events_not_before']));
124 
125  $sql = 'SELECT a.id as id_agenda, a.code, a.datep, a.label, a.location, ar.rowid as id_reminder, ar.dateremind, ar.fk_user as id_user_reminder';
126  $sql .= ' FROM '.MAIN_DB_PREFIX.'actioncomm as a';
127  if (!empty($user->conf->MAIN_USER_WANT_ALL_EVENTS_NOTIFICATIONS)) {
128  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'actioncomm_reminder as ar ON a.id = ar.fk_actioncomm AND ar.fk_user = '.((int) $user->id);
129  $sql .= ' WHERE a.code <> "AC_OTH_AUTO"';
130  $sql .= ' AND (';
131  $sql .= " ar.typeremind = 'browser' AND ar.dateremind < '".$db->idate(dol_now())."' AND ar.status = 0 AND ar.entity = ".$conf->entity;
132  $sql .= ' )';
133  } else {
134  $sql .= ' JOIN '.MAIN_DB_PREFIX.'actioncomm_reminder as ar ON a.id = ar.fk_actioncomm AND ar.fk_user = '.((int) $user->id);
135  $sql .= " AND ar.typeremind = 'browser' AND ar.dateremind < '".$db->idate(dol_now())."' AND ar.status = 0 AND ar.entity = ".$conf->entity;
136  }
137  $sql .= $db->order('datep', 'ASC');
138  $sql .= ' LIMIT 10'; // Avoid too many notification at once
139 
140  $resql = $db->query($sql);
141  if ($resql) {
142  while ($obj = $db->fetch_object($resql)) {
143  // Message must be formated and translated to be used with javascript directly
144  $event = array();
145  $event['type'] = 'agenda';
146  $event['id_reminder'] = $obj->id_reminder;
147  $event['id_agenda'] = $obj->id_agenda;
148  $event['id_user'] = $obj->id_user_reminder;
149  $event['code'] = $obj->code;
150  $event['label'] = $obj->label;
151  $event['location'] = $obj->location;
152  $event['reminder_date_formated_tzserver'] = dol_print_date($db->jdate($obj->dateremind), 'standard', 'tzserver');
153  $event['event_date_start_formated_tzserver'] = dol_print_date($db->jdate($obj->datep), 'standard', 'tzserver');
154  $event['reminder_date_formated'] = dol_print_date($db->jdate($obj->dateremind), 'standard', 'tzuser');
155  $event['event_date_start_formated'] = dol_print_date($db->jdate($obj->datep), 'standard', 'tzuser');
156 
157  $eventfound[$obj->id_agenda] = $event;
158  }
159  } else {
160  dol_syslog("Error sql = ".$db->lasterror(), LOG_ERR);
161  }
162 }
163 
164 print json_encode(array('pastreminders'=>$eventfound, 'nextreminder'=>''));
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
Definition: date.lib.php:121
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_now($mode='auto')
Return date for now.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
if(!defined('NOREQUIREMENU')) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
Definition: main.inc.php:1440