dolibarr  19.0.0-dev
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 // Security check
48 // No permission check at top, but action later are all done with a test on $user->id.
49 
50 
51 /*
52  * Actions
53  */
54 
55 if ($action == 'stopreminder') {
56  dol_syslog("Clear notification for listofreminderids=".$listofreminderids);
57  $listofreminderid = GETPOST('listofreminderids', 'intcomma');
58 
59  // Set the reminder as done
60  $sql = 'UPDATE '.MAIN_DB_PREFIX.'actioncomm_reminder SET status = 1';
61  $sql .= ' WHERE status = 0 AND rowid IN ('.$db->sanitize($db->escape($listofreminderid)).')';
62  $sql .= ' AND fk_user = '.((int) $user->id).' AND entity = '.((int) $conf->entity);
63  $resql = $db->query($sql);
64  if (!$resql) {
65  dol_print_error($db);
66  }
67  //}
68 
69  include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
70 
71  // Clean database
72  $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'actioncomm_reminder';
73  $sql .= " WHERE dateremind < '".$db->idate(dol_time_plus_duree(dol_now(), -1, 'm'))."'";
74  $sql .= " AND fk_user = ".((int) $user->id).' AND entity = '.((int) $conf->entity);
75  $resql = $db->query($sql);
76  if (!$resql) {
77  dol_print_error($db);
78  }
79 
80  exit;
81 }
82 
83 
84 /*
85  * View
86  */
87 
88 top_httphead('application/json');
89 
90 global $user, $db, $langs, $conf;
91 
92 $eventfound = array();
93 //Uncomment this to force a test
94 //$eventfound[]=array('type'=>'agenda', 'id'=>1, 'tipo'=>'eee', 'location'=>'aaa');
95 
96 //dol_syslog('time='.$time.' $_SESSION[auto_ck_events_not_before]='.$_SESSION['auto_check_events_not_before']);
97 
98 // 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.
99 // This need to extend period to be sure to not miss and save in session what we notified to avoid duplicate.
100 if (empty($_SESSION['auto_check_events_not_before']) || $time >= $_SESSION['auto_check_events_not_before'] || GETPOST('forcechecknow', 'int')) {
101  /*$time_update = (int) $conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY; // Always defined
102  if (!empty($_SESSION['auto_check_events_not_before']))
103  {
104  // We start scan from the not before so if two tabs were opend at differents seconds and we close one (so the js timer),
105  // then we are not losing periods
106  $starttime = $_SESSION['auto_check_events_not_before'];
107  // Protection to avoid too long sessions
108  if ($starttime < ($time - (int) $conf->global->MAIN_SESSION_TIMEOUT))
109  {
110  dol_syslog("We ask to check browser notification on a too large period. We fix this with current date.");
111  $starttime = $time;
112  }
113  } else {
114  $starttime = $time;
115  }
116 
117  $_SESSION['auto_check_events_not_before'] = $time + $time_update;
118  */
119 
120  // Force save of the session change we did.
121  // WARNING: Any change in sessions after that will not be saved !
122  session_write_close();
123 
124  require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
125 
126 
127  dol_syslog('NEW $_SESSION[auto_check_events_not_before]='.(empty($_SESSION['auto_check_events_not_before']) ? '' : $_SESSION['auto_check_events_not_before']));
128 
129  $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';
130  $sql .= ' FROM '.MAIN_DB_PREFIX.'actioncomm as a';
131  $sql .= ' INNER JOIN '.MAIN_DB_PREFIX.'actioncomm_reminder as ar ON a.id = ar.fk_actioncomm AND ar.fk_user = '.((int) $user->id);
132  $sql .= " AND ar.typeremind = 'browser' AND ar.dateremind < '".$db->idate(dol_now())."' AND ar.status = 0 AND ar.entity = ".((int) $conf->entity); // No sharing of entity for alerts
133  $sql .= $db->order('datep', 'ASC');
134  $sql .= $db->plimit(10); // Avoid too many notification at once
135 
136  $resql = $db->query($sql);
137  if ($resql) {
138  while ($obj = $db->fetch_object($resql)) {
139  // Message must be formated and translated to be used with javascript directly
140  $event = array();
141  $event['type'] = 'agenda';
142  $event['id_reminder'] = $obj->id_reminder;
143  $event['id_agenda'] = $obj->id_agenda;
144  $event['id_user'] = $obj->id_user_reminder;
145  $event['code'] = $obj->code;
146  $event['label'] = $obj->label;
147  $event['location'] = $obj->location;
148  $event['reminder_date_formated_tzserver'] = dol_print_date($db->jdate($obj->dateremind), 'standard', 'tzserver');
149  $event['event_date_start_formated_tzserver'] = dol_print_date($db->jdate($obj->datep), 'standard', 'tzserver');
150  $event['reminder_date_formated'] = dol_print_date($db->jdate($obj->dateremind), 'standard', 'tzuser');
151  $event['event_date_start_formated'] = dol_print_date($db->jdate($obj->datep), 'standard', 'tzuser');
152 
153  $eventfound[$obj->id_agenda] = $event;
154  }
155  } else {
156  dol_syslog("Error sql = ".$db->lasterror(), LOG_ERR);
157  }
158 }
159 
160 print json_encode(array('pastreminders'=>$eventfound, 'nextreminder'=>''));
if(isModEnabled('facture') && $user->hasRight('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') && $user->hasRight('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)) $sql
Social contributions to pay.
Definition: index.php:746
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
Definition: date.lib.php:122
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(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
Definition: main.inc.php:1494