dolibarr  20.0.0-beta
box_graph_propales_permonth.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2013 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
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 <https://www.gnu.org/licenses/>.
17  */
18 
24 include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
25 
26 
31 {
32  public $boxcode = "propalpermonth";
33  public $boximg = "object_propal";
34  public $boxlabel = "BoxProposalsPerMonth";
35  public $depends = array("propal");
36 
37  public $widgettype = 'graph';
38 
45  public function __construct($db, $param)
46  {
47  global $user;
48 
49  $this->db = $db;
50 
51  $this->hidden = !$user->hasRight('propal', 'lire');
52  }
53 
60  public function loadBox($max = 5)
61  {
62  global $conf, $user, $langs;
63 
64  $this->max = $max;
65 
66  $refreshaction = 'refresh_'.$this->boxcode;
67 
68  //include_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
69  //$propalstatic=new Propal($this->db);
70 
71  $startmonth = getDolGlobalInt('SOCIETE_FISCAL_MONTH_START', 1);
72  if (!getDolGlobalString('GRAPH_USE_FISCAL_YEAR')) {
73  $startmonth = 1;
74  }
75 
76  $langs->load("propal");
77 
78  $text = $langs->trans("BoxProposalsPerMonth", $max);
79  $this->info_box_head = array(
80  'text' => $text,
81  'limit'=> dol_strlen($text),
82  'graph'=> 1, // Set to 1 if it's a box graph
83  'sublink'=>'',
84  'subtext'=>$langs->trans("Filter"),
85  'subpicto'=>'filter.png',
86  'subclass'=>'linkobject boxfilter',
87  'target'=>'none' // Set '' to get target="_blank"
88  );
89 
90  $dir = ''; // We don't need a path because image file will not be saved into disk
91  $prefix = '';
92  $socid = 0;
93  if ($user->socid) {
94  $socid = $user->socid;
95  }
96  if (!$user->hasRight('societe', 'client', 'voir')) {
97  $prefix .= 'private-'.$user->id.'-'; // If user has no permission to see all, output dir is specific to user
98  }
99 
100  if ($user->hasRight('propal', 'lire')) {
101  $param_year = 'DOLUSERCOOKIE_box_'.$this->boxcode.'_year';
102  $param_shownb = 'DOLUSERCOOKIE_box_'.$this->boxcode.'_shownb';
103  $param_showtot = 'DOLUSERCOOKIE_box_'.$this->boxcode.'_showtot';
104 
105  include_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
106  include_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propalestats.class.php';
107  $autosetarray = preg_split("/[,;:]+/", GETPOST('DOL_AUTOSET_COOKIE'));
108  if (in_array('DOLUSERCOOKIE_box_'.$this->boxcode, $autosetarray)) {
109  $endyear = GETPOSTINT($param_year);
110  $shownb = GETPOST($param_shownb, 'alpha');
111  $showtot = GETPOST($param_showtot, 'alpha');
112  } else {
113  $tmparray = (!empty($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode]) ? json_decode($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode], true) : array());
114  $endyear = (!empty($tmparray['year']) ? $tmparray['year'] : '');
115  $shownb = (!empty($tmparray['shownb']) ? $tmparray['shownb'] : '');
116  $showtot = (!empty($tmparray['showtot']) ? $tmparray['showtot'] : '');
117  }
118  if (empty($shownb) && empty($showtot)) {
119  $shownb = 1;
120  $showtot = 1;
121  }
122  $nowarray = dol_getdate(dol_now(), true);
123  if (empty($endyear)) {
124  $endyear = $nowarray['year'];
125  }
126  $startyear = $endyear - getDolGlobalInt('MAIN_NB_OF_YEAR_IN_WIDGET_GRAPH', 3) + 1;
127 
128  $WIDTH = (($shownb && $showtot) || !empty($conf->dol_optimize_smallscreen)) ? '256' : '320';
129  $HEIGHT = '192';
130 
131  $stats = new PropaleStats($this->db, $socid, 0);
132 
133  // Build graphic number of object. $data = array(array('Lib',val1,val2,val3),...)
134  if ($shownb) {
135  $data1 = $stats->getNbByMonthWithPrevYear($endyear, $startyear, (GETPOST('action', 'aZ09') == $refreshaction ? -1 : (3600 * 24)), ($WIDTH < 300 ? 2 : 0), $startmonth);
136  $datatype1 = array_pad(array(), ($endyear - $startyear + 1), 'bars');
137 
138  $filenamenb = $dir."/".$prefix."propalsnbinyear-".$endyear.".png";
139  $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=propalstats&amp;file=propalsnbinyear-'.$endyear.'.png';
140 
141  $px1 = new DolGraph();
142  $mesg = $px1->isGraphKo();
143  if (!$mesg) {
144  $px1->SetType($datatype1);
145  $px1->SetData($data1);
146  unset($data1);
147  $i = $startyear;
148  $legend = array();
149  while ($i <= $endyear) {
150  if ($startmonth != 1) {
151  $legend[] = sprintf("%d/%d", $i - 2001, $i - 2000);
152  } else {
153  $legend[] = $i;
154  }
155  $i++;
156  }
157  $px1->SetLegend($legend);
158  $px1->SetMaxValue($px1->GetCeilMaxValue());
159  $px1->SetWidth($WIDTH);
160  $px1->SetHeight($HEIGHT);
161  $px1->SetYLabel($langs->trans("NumberOfProposals"));
162  $px1->SetShading(3);
163  $px1->SetHorizTickIncrement(1);
164  $px1->SetCssPrefix("cssboxes");
165  $px1->mode = 'depth';
166  $px1->SetTitle($langs->trans("NumberOfProposalsByMonth"));
167 
168  $px1->draw($filenamenb, $fileurlnb);
169  }
170  }
171 
172  // Build graphic number of object. $data = array(array('Lib',val1,val2,val3),...)
173  if ($showtot) {
174  $data2 = $stats->getAmountByMonthWithPrevYear($endyear, $startyear, (GETPOST('action', 'aZ09') == $refreshaction ? -1 : (3600 * 24)), ($WIDTH < 300 ? 2 : 0), $startmonth);
175  $datatype2 = array_pad(array(), ($endyear - $startyear + 1), 'bars');
176  //$datatype2 = array('lines','bars');
177 
178  $filenamenb = $dir."/".$prefix."propalsamountinyear-".$endyear.".png";
179  $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=propalstats&amp;file=propalsamountinyear-'.$endyear.'.png';
180 
181  $px2 = new DolGraph();
182  $mesg = $px2->isGraphKo();
183  if (!$mesg) {
184  $px2->SetType($datatype2);
185  $px2->SetData($data2);
186  unset($data2);
187  $i = $startyear;
188  $legend = array();
189  while ($i <= $endyear) {
190  if ($startmonth != 1) {
191  $legend[] = sprintf("%d/%d", $i - 2001, $i - 2000);
192  } else {
193  $legend[] = $i;
194  }
195  $i++;
196  }
197  $px2->SetLegend($legend);
198  $px2->SetMaxValue($px2->GetCeilMaxValue());
199  $px2->SetWidth($WIDTH);
200  $px2->SetHeight($HEIGHT);
201  $px2->SetYLabel($langs->trans("AmountOfProposalsHT"));
202  $px2->SetShading(3);
203  $px2->SetHorizTickIncrement(1);
204  $px2->SetCssPrefix("cssboxes");
205  $px2->mode = 'depth';
206  $px2->SetTitle($langs->trans("AmountOfProposalsByMonthHT"));
207 
208  $px2->draw($filenamenb, $fileurlnb);
209  }
210  }
211 
212  if (empty($conf->use_javascript_ajax)) {
213  $langs->load("errors");
214  $mesg = $langs->trans("WarningFeatureDisabledWithDisplayOptimizedForBlindNoJs");
215  }
216 
217  if (!$mesg) {
218  $stringtoshow = '';
219  $stringtoshow .= '<script nonce="'.getNonce().'" type="text/javascript">
220  jQuery(document).ready(function() {
221  jQuery("#idsubimg'.$this->boxcode.'").click(function() {
222  jQuery("#idfilter'.$this->boxcode.'").toggle();
223  });
224  });
225  </script>';
226  $stringtoshow .= '<div class="center hideobject divboxfilter" id="idfilter'.$this->boxcode.'">'; // hideobject is to start hidden
227  $stringtoshow .= '<form class="flat formboxfilter" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
228  $stringtoshow .= '<input type="hidden" name="token" value="'.newToken().'">';
229  $stringtoshow .= '<input type="hidden" name="action" value="'.$refreshaction.'">';
230  $stringtoshow .= '<input type="hidden" name="page_y" value="">';
231  $stringtoshow .= '<input type="hidden" name="DOL_AUTOSET_COOKIE" value="DOLUSERCOOKIE_box_'.$this->boxcode.':year,shownb,showtot">';
232  $stringtoshow .= '<input type="checkbox" name="'.$param_shownb.'"'.($shownb ? ' checked' : '').'> '.$langs->trans("NumberOfProposalsByMonth");
233  $stringtoshow .= ' &nbsp; ';
234  $stringtoshow .= '<input type="checkbox" name="'.$param_showtot.'"'.($showtot ? ' checked' : '').'> '.$langs->trans("AmountOfProposalsByMonthHT");
235  $stringtoshow .= '<br>';
236  $stringtoshow .= $langs->trans("Year").' <input class="flat" size="4" type="text" name="'.$param_year.'" value="'.$endyear.'">';
237  $stringtoshow .= '<input type="image" class="reposition inline-block valigntextbottom" alt="'.$langs->trans("Refresh").'" src="'.img_picto($langs->trans("Refresh"), 'refresh.png', '', '', 1).'">';
238  $stringtoshow .= '</form>';
239  $stringtoshow .= '</div>';
240  if ($shownb && $showtot) {
241  $stringtoshow .= '<div class="fichecenter">';
242  $stringtoshow .= '<div class="fichehalfleft">';
243  }
244  if ($shownb) {
245  $stringtoshow .= $px1->show();
246  }
247  if ($shownb && $showtot) {
248  $stringtoshow .= '</div>';
249  $stringtoshow .= '<div class="fichehalfright">';
250  }
251  if ($showtot) {
252  $stringtoshow .= $px2->show();
253  }
254  if ($shownb && $showtot) {
255  $stringtoshow .= '</div>';
256  $stringtoshow .= '</div>';
257  }
258  $this->info_box_contents[0][0] = array(
259  'tr'=>'class="oddeven nohover"',
260  'td' => 'class="nohover center"',
261  'textnoformat'=>$stringtoshow,
262  );
263  } else {
264  $this->info_box_contents[0][0] = array(
265  'tr'=>'class="oddeven nohover"',
266  'td' => 'class="nohover left"',
267  'maxlength' => 500,
268  'text' => $mesg,
269  );
270  }
271  } else {
272  $this->info_box_contents[0][0] = array(
273  'td' => 'class="nohover left"',
274  'text' => '<span class="opacitymedium">'.$langs->trans("ReadPermissionNotAllowed").'</span>'
275  );
276  }
277  }
278 
287  public function showBox($head = null, $contents = null, $nooutput = 0)
288  {
289  return parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
290  }
291 }
Class to build graphs.
Class ModeleBoxes.
Class to manage proposals statistics.
Class to manage the box to show proposals per month graph.
loadBox($max=5)
Load data into info_box_contents array to show array later.
showBox($head=null, $contents=null, $nooutput=0)
Method to show box.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_getdate($timestamp, $fast=false, $forcetimezone='')
Return an array with locale date info.