dolibarr  7.0.0-beta
check_notifications.php
1 <?php
2 /* Copyright (C) 2016 Sergio Sanchis <sergiosanchis@hotmail.com>
3  * Copyright (C) 2017 Juanjo Menent <jmenent@2byte.es>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1'); // Disables token renewal
20 if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1');
21 if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1');
22 if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1');
23 if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1');
24 
25 require '../../main.inc.php';
26 
27 
28 /*
29  * View
30  */
31 
32 top_httphead('text/html'); // TODO Use a json mime type
33 
34 global $user, $db, $langs, $conf;
35 
36 $time = (int) GETPOST('time','int'); // Use the time parameter that is always increased by time_update, even if call is late
37 //$time=dol_now();
38 
39 
40 $eventfound = array();
41 //Uncomment this to force a test
42 //$eventfound[]=array('type'=>'agenda', 'id'=>1, 'tipo'=>'eee', 'location'=>'aaa');
43 
44 //dol_syslog('time='.$time.' $_SESSION[auto_ck_events_not_before]='.$_SESSION['auto_check_events_not_before']);
45 
46 // 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.
47 if ($time >= $_SESSION['auto_check_events_not_before'])
48 {
49  $time_update = (int) $conf->global->MAIN_BROWSER_NOTIFICATION_FREQUENCY; // Always defined
50  if (! empty($_SESSION['auto_check_events_not_before']))
51  {
52  // We start scan from the not before so if two tabs were opend at differents seconds and we close one (so the js timer),
53  // then we are not losing periods
54  $starttime = $_SESSION['auto_check_events_not_before'];
55  // Protection to avoid too long sessions
56  if ($starttime < ($time - (int) $conf->global->MAIN_SESSION_TIMEOUT))
57  {
58  dol_syslog("We ask to check browser notification on a too large period. We fix this with current date.");
59  $starttime = $time;
60  }
61  }
62  else
63  {
64  $starttime = $time;
65  }
66 
67  $_SESSION['auto_check_events_not_before'] = $time + $time_update;
68 
69  // Force save of session change we did.
70  // WARNING: Any change in sessions after that will not be saved !
71  session_write_close();
72 
73  require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
74 
75 
76  dol_syslog('NEW $_SESSION[auto_check_events_not_before]='.$_SESSION['auto_check_events_not_before']);
77 
78  $sql = 'SELECT id';
79  $sql .= ' FROM ' . MAIN_DB_PREFIX . 'actioncomm a, ' . MAIN_DB_PREFIX . 'actioncomm_resources ar';
80  $sql .= ' WHERE a.id = ar.fk_actioncomm';
81  // 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.
82  // This need to extend period to be sure to not miss and save in session what we notified to avoid duplicate (save is not done yet).
83  $sql .= " AND datep BETWEEN '" . $db->idate($starttime) . "' AND '" . $db->idate($time + $time_update - 1) . "'";
84  $sql .= ' AND a.code <> "AC_OTH_AUTO"';
85  $sql .= ' AND ar.element_type = "user"';
86  $sql .= ' AND ar.fk_element = ' . $user->id;
87  $sql .= ' LIMIT 10'; // Avoid too many notification at once
88 
89  $resql = $db->query($sql);
90  if ($resql) {
91 
92  $actionmod = new ActionComm($db);
93 
94  while ($obj = $db->fetch_object($resql))
95  {
96  $langs->load("agenda");
97  $langs->load("commercial");
98 
99  $actionmod->fetch($obj->id);
100 
101  // Message must be formated and translated to be used with javascript directly
102  $event = array();
103  $event['type'] = 'agenda';
104  $event['id'] = $actionmod->id;
105  $event['tipo'] = $langs->transnoentities('Action' . $actionmod->code);
106  $event['titulo'] = $actionmod->label;
107  $event['location'] = $langs->transnoentities('Location').': '.$actionmod->location;
108 
109  $eventfound[] = $event;
110  }
111  }
112  else
113  {
114  dol_syslog("Error sql = ".$db->lasterror(), LOG_ERR);
115  }
116 
117 }
118 
119 print json_encode($eventfound);
120 
Class to manage agenda events (actions)
if(GETPOST('cancel','alpha')) if(!GETPOST('confirmmassaction','alpha')&&$massaction!= 'presend'&&$massaction!= 'confirm_presend')
Draft customers invoices.
Definition: list.php:147
GETPOST($paramname, $check='none', $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='')
Write log message into outputs.
print
Draft customers invoices.
Definition: index.php:91
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->societe->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1013
if(!defined('NOREQUIREMENU')) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html')
Show HTTP header.
Definition: main.inc.php:1052