dolibarr 23.0.3
pdf_standard_actions.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
6 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2024 Nick Fragoulis
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 * or see https://www.gnu.org/
22 */
23
30require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
31require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
32require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
33require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
34require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
35
40{
44 public $db;
45
49 public $error;
50
54 public $errors;
55
59 public $warnings;
60
64 public $description;
65
69 public $date_edition;
70
74 public $year;
75
79 public $month;
80
84 public $title;
85
89 public $subject;
90
94 public $marge_gauche;
95
99 public $marge_droite;
100
104 public $marge_haute;
105
109 public $marge_basse;
110
114 public $format;
115
119 public $type;
120
124 public $page_hauteur;
125
129 public $page_largeur;
130
134 public $corner_radius;
135
139 public $result;
140
148 public function __construct($db, $month, $year)
149 {
150 global $langs;
151
152 // Load translation files required by the page
153 $langs->loadLangs(array("commercial", "projects"));
154
155 $this->db = $db;
156 $this->description = "";
157 $this->date_edition = time();
158 $this->month = $month;
159 $this->year = $year;
160 $this->corner_radius = getDolGlobalInt('MAIN_PDF_FRAME_CORNER_RADIUS', 0);
161 // Page size for A4 format
162 $this->type = 'pdf';
163 $formatarray = pdf_getFormat();
164 $this->page_largeur = $formatarray['width'];
165 $this->page_hauteur = $formatarray['height'];
166 $this->format = array($this->page_largeur, $this->page_hauteur);
167 $this->marge_gauche = getDolGlobalInt('MAIN_PDF_MARGIN_LEFT', 10);
168 $this->marge_droite = getDolGlobalInt('MAIN_PDF_MARGIN_RIGHT', 10);
169 $this->marge_haute = getDolGlobalInt('MAIN_PDF_MARGIN_TOP', 10);
170 $this->marge_basse = getDolGlobalInt('MAIN_PDF_MARGIN_BOTTOM', 10);
171
172 $this->title = $langs->transnoentitiesnoconv("ActionsReport").' '.$this->year."-".$this->month;
173 $this->subject = $langs->transnoentitiesnoconv("ActionsReport").' '.$this->year."-".$this->month;
174 }
175
176 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
184 public function write_file($object, $outputlangs)
185 {
186 // phpcs:enable
187 global $user, $conf, $langs, $hookmanager;
188
189 if (!is_object($outputlangs)) {
190 $outputlangs = $langs;
191 }
192 // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
193 if (getDolGlobalString('MAIN_USE_FPDF')) {
194 $outputlangs->charset_output = 'ISO-8859-1';
195 }
196
197 // Load traductions files required by page
198 $outputlangs->loadLangs(array("main", "dict", "companies", "bills", "products"));
199
200 $dir = $conf->agenda->dir_temp."/";
201 $file = $dir."actions-".sprintf("%04d", $this->year)."-".sprintf("%02d", $this->month).".pdf";
202
203 if (!file_exists($dir)) {
204 if (dol_mkdir($dir) < 0) {
205 $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
206 return 0;
207 }
208 }
209
210 if (file_exists($dir)) {
211 // Add pdfgeneration hook
212 if (!is_object($hookmanager)) {
213 include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
214 $hookmanager = new HookManager($this->db);
215 }
216 $hookmanager->initHooks(array('pdfgeneration'));
217
218 global $action;
219 $object = new stdClass();
220
221 $parameters = array('file' => $file, 'outputlangs' => $outputlangs);
222 $reshook = $hookmanager->executeHooks('beforePDFCreation', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
223
224 $pdf = pdf_getInstance($this->format);
225 $heightforinfotot = 50; // Height reserved to output the info and total part
226 $heightforfreetext = getDolGlobalInt('MAIN_PDF_FREETEXT_HEIGHT', 5); // Height reserved to output the free text on last page
227 $heightforfooter = $this->marge_basse + 8; // Height reserved to output the footer (value include bottom margin)
228 $pdf->setAutoPageBreak(true, 0);
229
230 if (class_exists('TCPDF')) {
231 $pdf->setPrintHeader(false);
232 $pdf->setPrintFooter(false);
233 }
234 $pdf->SetFont(pdf_getPDFFont($outputlangs));
235
236 $pdf->Open();
237 $pagenb = 0;
238 $pdf->SetDrawColor(128, 128, 128);
239 $pdf->SetFillColor(220, 220, 220);
240
241 $pdf->SetTitle($outputlangs->convToOutputCharset($this->title));
242 $pdf->SetSubject($outputlangs->convToOutputCharset($this->subject));
243 $pdf->SetCreator("Dolibarr ".DOL_VERSION);
244 $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
245 $pdf->SetKeywords($outputlangs->convToOutputCharset($this->title." ".$this->subject));
246
247 // @phan-suppress-next-line PhanPluginSuspiciousParamOrder
248 $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite); // Left, Top, Right
249
250 $nbpage = $this->_pages($pdf, $outputlangs); // Write content
251
252 if (method_exists($pdf, 'AliasNbPages')) {
253 $pdf->AliasNbPages(); // @phan-suppress-current-line PhanUndeclaredMethod
254 }
255 $pdf->Close();
256
257 $pdf->Output($file, 'F');
258
259 // Add pdfgeneration hook
260 if (!is_object($hookmanager)) {
261 include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
262 $hookmanager = new HookManager($this->db);
263 }
264 $hookmanager->initHooks(array('pdfgeneration'));
265 $parameters = array('file' => $file, 'object' => $object, 'outputlangs' => $outputlangs);
266 global $action;
267 $reshook = $hookmanager->executeHooks('afterPDFCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
268 $this->warnings = $hookmanager->warnings;
269 if ($reshook < 0) {
270 $this->error = $hookmanager->error;
271 $this->errors = $hookmanager->errors;
272 dolChmod($file);
273 return -1;
274 }
275
276 dolChmod($file);
277
278 $this->result = array('fullpath' => $file);
279
280 return 1;
281 }
282 return 0;
283 }
284
292 private function _pages(&$pdf, $outputlangs)
293 {
294 global $conf;
295
296 $height = 3; // height for text separation
297 $pagenb = 1;
298
299 $y = $this->_pagehead($pdf, $outputlangs, $pagenb);
300 $y++;
301 $pdf->SetFont('', '', 8);
302
303 $sql = "SELECT s.nom as thirdparty, s.rowid as socid, s.client,";
304 $sql .= " a.id, a.datep as dp, a.datep2 as dp2,";
305 $sql .= " a.fk_contact, a.note, a.percent as percent, a.fulldayevent, a.label, a.fk_project,";
306 $sql .= " c.code, c.libelle as label_type,";
307 $sql .= " u.login";
308 $sql .= " FROM ".MAIN_DB_PREFIX."c_actioncomm as c, ".MAIN_DB_PREFIX."user as u, ".MAIN_DB_PREFIX."actioncomm as a";
309 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON a.fk_soc = s.rowid";
310 $sql .= " WHERE c.id=a.fk_action AND a.fk_user_author = u.rowid";
311 $sql .= " AND a.datep BETWEEN '".$this->db->idate(dol_get_first_day($this->year, $this->month, false))."'";
312 $sql .= " AND '".$this->db->idate(dol_get_last_day($this->year, $this->month, false))."'";
313 $sql .= " AND a.entity = ".$conf->entity;
314 $sql .= " ORDER BY a.datep DESC";
315
316 $eventstatic = new ActionComm($this->db);
317 $projectstatic = new Project($this->db);
318
319 dol_syslog(get_class($this)."::_page", LOG_DEBUG);
320 $resql = $this->db->query($sql);
321 if ($resql) {
322 $num = $this->db->num_rows($resql);
323 $i = 0;
324 $y0 = $y1 = $y2 = $y3 = 0;
325
326 while ($i < $num) {
327 $obj = $this->db->fetch_object($resql);
328
329 $eventstatic->id = $obj->id;
330 $eventstatic->percentage = $obj->percent;
331 $eventstatic->fulldayevent = $obj->fulldayevent;
332
333 $y = max($y, $pdf->GetY(), $y0, $y1, $y2, $y3);
334
335 // Calculate height of text
336 $text = '';
337 if (!preg_match('/^'.preg_quote($obj->label, '/').'/', $obj->note)) {
338 $text = $obj->label."\n";
339 }
340 $text .= dolGetFirstLineOfText(dol_string_nohtmltag($obj->note), 1);
341 // Add status to text
342 $text .= "\n";
343 $status = $outputlangs->trans("Status").': '.dol_htmlentitiesbr_decode($eventstatic->getLibStatut(1, 1));
344 $text .= $status;
345 if ($obj->fk_project > 0) {
346 $projectstatic->fetch($obj->fk_project);
347 if ($projectstatic->ref) {
348 $text .= ($status ? ' - ' : '').$outputlangs->transnoentitiesnoconv("Project").": ".dol_htmlentitiesbr_decode($projectstatic->ref);
349 }
350 }
351
352 //print 'd'.$text; exit;
353 $nboflines = dol_nboflines($text);
354
355 $heightlinemax = max(2 * $height, $nboflines * $height);
356 // Check if there is enough space to print record
357 if ((1 + $y + $heightlinemax) >= ($this->page_hauteur - $this->marge_haute)) {
358 // We need to break page
359 $pagenb++;
360 $y = $this->_pagehead($pdf, $outputlangs, $pagenb);
361 $y++;
362 $pdf->SetFont('', '', 8);
363 }
364 $y++;
365
366 // Date
367 $pdf->SetXY($this->marge_gauche, $y);
368 $textdate = dol_print_date($this->db->jdate($obj->dp), "day")."\n".dol_print_date($this->db->jdate($obj->dp), "hour");
369 if ($obj->dp2) {
370 if (dol_print_date($this->db->jdate($obj->dp), "day") != dol_print_date($this->db->jdate($obj->dp2), "day")) {
371 $textdate .= " -> ".dol_print_date($this->db->jdate($obj->dp2), "day")." - ".dol_print_date($this->db->jdate($obj->dp2), "hour");
372 } else {
373 $textdate .= " -> ".dol_print_date($this->db->jdate($obj->dp2), "hour");
374 }
375 }
376 $textdate = $outputlangs->trans("ID").' '.$obj->id.' - '.$textdate;
377 $pdf->MultiCell(45 - $this->marge_gauche, $height, $textdate, 0, 'L', false);
378 $y0 = $pdf->GetY();
379
380 // Third party
381 $pdf->SetXY(45, $y);
382 $pdf->MultiCell(28, $height, dol_trunc($outputlangs->convToOutputCharset($obj->thirdparty), 28), 0, 'L', false);
383 $y1 = $pdf->GetY();
384
385 // Action code
386 $code = $obj->code;
387 if (!getDolGlobalString('AGENDA_USE_EVENT_TYPE')) {
388 if ($code == 'AC_OTH') {
389 $code = 'AC_MANUAL';
390 }
391 if ($code == 'AC_OTH_AUTO') {
392 $code = 'AC_AUTO';
393 }
394 }
395 $pdf->SetXY(73, $y);
396 $labelactiontype = $outputlangs->transnoentitiesnoconv("Action".$code);
397 $labelactiontypeshort = $outputlangs->transnoentitiesnoconv("Action".$code.'Short');
398 $pdf->MultiCell(32, $height, dol_trunc($outputlangs->convToOutputCharset($labelactiontypeshort == "Action".$code.'Short' ? $labelactiontype : $labelactiontypeshort), 32), 0, 'L', false);
399 $y2 = $pdf->GetY();
400
401 // Description of event
402 $pdf->SetXY(106, $y);
403 $pdf->MultiCell(94, $height, $outputlangs->convToOutputCharset(dol_trunc(dol_string_nohtmltag($text, 0), 250, 'right', 'UTF-8', 0)), 0, 'L', false);
404 $y3 = $pdf->GetY();
405
406 $i++;
407 }
408 }
409
410 return 1;
411 }
412
421 private function _pagehead(&$pdf, $outputlangs, $pagenb)
422 {
423 // Do not add the BACKGROUND as this is a report
424 //pdf_pagehead($pdf,$outputlangs,$this->page_hauteur);
425
426 // New page
427 $pdf->AddPage();
428
429 // Show title
430 $pdf->SetFont('', 'B', 10);
431 $pdf->SetXY($this->marge_gauche, $this->marge_haute);
432 $pdf->MultiCell(120, 1, $outputlangs->convToOutputCharset($this->title), 0, 'L', false);
433 // Show page nb only on iso languages (so default Helvetica font)
434 if (pdf_getPDFFont($outputlangs) == 'Helvetica') {
435 $pdf->SetXY($this->page_largeur - $this->marge_droite - 40, $this->marge_haute);
436 $pdf->MultiCell(40, 1, $pagenb.'/'.$pdf->getAliasNbPages(), 0, 'R', false);
437 }
438
439 $y = $pdf->GetY() + 2;
440
441 $pdf->RoundedRect($this->marge_gauche, $y, ($this->page_largeur - $this->marge_gauche - $this->marge_droite), ($this->page_hauteur - $this->marge_haute - $this->marge_basse), $this->corner_radius, '1234', 'D');
442 $y = $pdf->GetY() + 1;
443
444 return $y;
445 }
446}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class to manage agenda events (actions)
Class to manage hooks.
Class to manage projects.
Class to generate event report.
_pagehead(&$pdf, $outputlangs, $pagenb)
Show top header of page.
__construct($db, $month, $year)
Constructor.
write_file($object, $outputlangs)
Write the object to document file to disk.
_pages(&$pdf, $outputlangs)
Write content of pages.
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition date.lib.php:603
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:622
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_htmlentitiesbr_decode($stringtodecode, $pagecodeto='UTF-8')
This function is called to decode a HTML string (it decodes entities and br tags)
dolGetFirstLineOfText($text, $nboflines=1, $charset='UTF-8')
Return first line of text.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
dolChmod($filepath, $newmask='')
Change mod of a file.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
dol_nboflines($s, $maxchar=0)
Return nb of lines of a clear text.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
pdf_getFormat($outputlangs=null, $mode='setup')
Return array with format properties of default PDF format.
Definition pdf.lib.php:87
pdf_getPDFFont($outputlangs)
Return font name to use for PDF generation.
Definition pdf.lib.php:268
pdf_getInstance($format='', $metric='mm', $pagetype='P')
Return a PDF instance object.
Definition pdf.lib.php:129
if(preg_match('/(crypted|dolcrypt):/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
Definition repair.php:125