dolibarr  7.0.0-beta
doc_generic_project_odt.modules.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2010-2012 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2012 Juanjo Menent <jmenent@2byte.es>
4  * Copyright (C) 2013 Florian Henry <florian.henry@ope-concept.pro>
5  * Copyright (C) 2016 Charlie Benke <charlie@patas-monkey.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  * or see http://www.gnu.org/
20  */
21 
28 require_once DOL_DOCUMENT_ROOT.'/core/modules/project/modules_project.php';
29 require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
30 require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
31 require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
32 require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
33 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
34 require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
35 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
36 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
37 require_once DOL_DOCUMENT_ROOT.'/core/lib/doc.lib.php';
38 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
39 if (! empty($conf->propal->enabled)) require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
40 if (! empty($conf->facture->enabled)) require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
41 if (! empty($conf->facture->enabled)) require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture-rec.class.php';
42 if (! empty($conf->commande->enabled)) require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
43 if (! empty($conf->fournisseur->enabled)) require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
44 if (! empty($conf->fournisseur->enabled)) require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
45 if (! empty($conf->contrat->enabled)) require_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
46 if (! empty($conf->ficheinter->enabled)) require_once DOL_DOCUMENT_ROOT.'/fichinter/class/fichinter.class.php';
47 if (! empty($conf->deplacement->enabled)) require_once DOL_DOCUMENT_ROOT.'/compta/deplacement/class/deplacement.class.php';
48 if (! empty($conf->agenda->enabled)) require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
49 
50 
55 {
56  var $emetteur; // Objet societe qui emet
57 
58  var $phpmin = array(5,2,0); // Minimum version of PHP required by module
59  var $version = 'dolibarr';
60 
61 
67  function __construct($db)
68  {
69  global $conf,$langs,$mysoc;
70 
71  $langs->load("main");
72  $langs->load("companies");
73 
74  $this->db = $db;
75  $this->name = "ODT templates";
76  $this->description = $langs->trans("DocumentModelOdt");
77  $this->scandir = 'PROJECT_ADDON_PDF_ODT_PATH'; // Name of constant that is used to save list of directories to scan
78 
79  // Dimension page pour format A4
80  $this->type = 'odt';
81  $this->page_largeur = 0;
82  $this->page_hauteur = 0;
83  $this->format = array($this->page_largeur,$this->page_hauteur);
84  $this->marge_gauche=0;
85  $this->marge_droite=0;
86  $this->marge_haute=0;
87  $this->marge_basse=0;
88 
89  $this->option_logo = 1; // Affiche logo
90  $this->option_tva = 0; // Gere option tva COMMANDE_TVAOPTION
91  $this->option_modereg = 0; // Affiche mode reglement
92  $this->option_condreg = 0; // Affiche conditions reglement
93  $this->option_codeproduitservice = 0; // Affiche code produit-service
94  $this->option_multilang = 1; // Dispo en plusieurs langues
95  $this->option_escompte = 0; // Affiche si il y a eu escompte
96  $this->option_credit_note = 0; // Support credit notes
97  $this->option_freetext = 1; // Support add of a personalised text
98  $this->option_draft_watermark = 0; // Support add of a watermark on drafts
99 
100  // Recupere emetteur
101  $this->emetteur=$mysoc;
102  if (! $this->emetteur->pays_code) $this->emetteur->pays_code=substr($langs->defaultlang,-2); // Par defaut, si n'etait pas defini
103  }
104 
105 
114  function get_substitutionarray_object($object,$outputlangs,$array_key='object')
115  {
116  global $conf;
117 
118  $resarray=array(
119  $array_key.'_id'=>$object->id,
120  $array_key.'_ref'=>$object->ref,
121  $array_key.'_title'=>$object->title,
122  $array_key.'_description'=>$object->description,
123  $array_key.'_date_creation'=>dol_print_date($object->date_c,'day'),
124  $array_key.'_date_modification'=>dol_print_date($object->date_m,'day'),
125  $array_key.'_date_start'=>dol_print_date($object->date_start,'day'),
126  $array_key.'_date_end'=>dol_print_date($object->date_end,'day'),
127  $array_key.'_note_private'=>$object->note_private,
128  $array_key.'_note_public'=>$object->note_public,
129  $array_key.'_public'=>$object->public,
130  $array_key.'_statut'=>$object->getLibStatut()
131  );
132 
133  // Retrieve extrafields
134  $extrafieldkey=$object->element;
135 
136  require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
137  $extrafields = new ExtraFields($this->db);
138  $extralabels = $extrafields->fetch_name_optionals_label($extrafieldkey,true);
139  $object->fetch_optionals($object->id,$extralabels);
140 
141  $resarray = $this->fill_substitutionarray_with_extrafields($object,$resarray,$extrafields,$array_key,$outputlangs);
142 
143  return $resarray;
144  }
145 
153  function get_substitutionarray_tasks($task,$outputlangs)
154  {
155  global $conf;
156 
157  return array(
158  'task_ref'=>$task->ref,
159  'task_fk_project'=>$task->fk_project,
160  'task_projectref'=>$task->projectref,
161  'task_projectlabel'=>$task->projectlabel,
162  'task_label'=>$task->label,
163  'task_description'=>$task->description,
164  'task_fk_parent'=>$task->fk_parent,
165  'task_duration'=>$task->duration,
166  'task_progress'=>$task->progress,
167  'task_public'=>$task->public,
168  'task_date_start'=>dol_print_date($task->date_start,'day'),
169  'task_date_end'=>dol_print_date($task->date_end,'day'),
170  'task_note_private'=>$task->note_private,
171  'task_note_public'=>$task->note_public
172  );
173  }
174 
182  function get_substitutionarray_project_contacts($contact,$outputlangs)
183  {
184  global $conf;
185  $pc='projcontacts_'; // prefix to avoid typos
186 
187  $ret = array(
188  $pc.'id'=>$contact['id'],
189  $pc.'rowid'=>$contact['rowid'],
190  $pc.'role'=>$contact['libelle'],
191  $pc.'lastname'=>$contact['lastname'],
192  $pc.'firstname'=>$contact['firstname'],
193  $pc.'civility'=>$contact['civility'],
194  $pc.'fullcivname'=>$contact['fullname'],
195  $pc.'socname'=>$contact['socname'],
196  $pc.'email'=>$contact['email']
197  );
198 
199  if ($contact['source']=='external') {
200  $ret[$pc.'isInternal'] = ''; // not internal
201 
202  $ct = new Contact($this->db);
203  $ct->fetch($contact['id']);
204  $ret[$pc.'phone_pro'] = $ct->phone_pro;
205  $ret[$pc.'phone_perso'] = $ct->phone_perso;
206  $ret[$pc.'phone_mobile'] = $ct->phone_mobile;
207 
208  // fetch external user extrafields
209  require_once(DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php');
210  $extrafields=new ExtraFields($this->db);
211  $extralabels=$extrafields->fetch_name_optionals_label($ct->table_element, true);
212  $extrafields_num = $ct->fetch_optionals($ct->id, $extralabels);
213  //dol_syslog(get_class($this)."::get_substitutionarray_project_contacts: ===== Number of Extrafields found: ".$extrafields_num, LOG_DEBUG);
214  foreach($ct->array_options as $efkey => $efval) {
215  dol_syslog(get_class($this)."::get_substitutionarray_project_contacts: +++++ Extrafield ".$efkey." => ".$efval, LOG_DEBUG);
216  $ret[$pc.$efkey] = $efval; // add nothing else because it already comes as 'options_XX'
217  }
218  } elseif ($contact['source']=='internal') {
219  $ret[$pc.'isInternal'] = '1'; // this is an internal user
220 
221  $ct = new User($this->db);
222  $ct->fetch($contact['id']);
223  $ret[$pc.'phone_pro'] = $ct->office_phone;
224  $ret[$pc.'phone_perso'] = '';
225  $ret[$pc.'phone_mobile'] = $ct->user_mobile;
226  // do internal users have extrafields ?
227  }
228  return $ret;
229  }
230 
238  function get_substitutionarray_project_file($file,$outputlangs)
239  {
240  global $conf;
241 
242  return array(
243  'projfile_name'=>$file['name'],
244  'projfile_date'=>dol_print_date($file['date'],'day'),
245  'projfile_size'=>$file['size']
246  );
247  }
248 
256  function get_substitutionarray_project_reference($refdetail,$outputlangs)
257  {
258  global $conf;
259 
260  return array(
261  'projref_type'=>$refdetail['type'],
262  'projref_ref'=>$refdetail['ref'],
263  'projref_date'=>dol_print_date($refdetail['date'],'day'),
264  'projref_socname'=>$refdetail['socname'],
265  'projref_amountht'=>price($refdetail['amountht'],0,$outputlangs),
266  'projref_amountttc'=>price($refdetail['amountttc'],0,$outputlangs),
267  'projref_status'=>$refdetail['status']
268  );
269  }
270 
278  function get_substitutionarray_tasksressource($taskressource,$outputlangs)
279  {
280  global $conf;
281  //dol_syslog(get_class($this).'::get_substitutionarray_tasksressource taskressource='.var_export($taskressource,true),LOG_DEBUG);
282  return array(
283  'taskressource_rowid'=>$taskressource['rowid'],
284  'taskressource_role'=>$taskressource['libelle'],
285  'taskressource_lastname'=>$taskressource['lastname'],
286  'taskressource_firstname'=>$taskressource['firstname'],
287  'taskressource_fullcivname'=>$taskressource['fullname'],
288  'taskressource_socname'=>$taskressource['socname'],
289  'taskressource_email'=>$taskressource['email']
290  );
291  }
292 
300  function get_substitutionarray_taskstime($tasktime,$outputlangs)
301  {
302  global $conf;
303 
304  return array(
305  'tasktime_rowid'=>$tasktime['rowid'],
306  'tasktime_task_date'=>dol_print_date($tasktime['task_date'],'day'),
307  'tasktime_task_duration_sec'=>$tasktime['task_duration'],
308  'tasktime_task_duration'=>convertSecondToTime($tasktime['task_duration'],'all'),
309  'tasktime_note'=>$tasktime['note'],
310  'tasktime_fk_user'=>$tasktime['fk_user'],
311  'tasktime_user_name'=>$tasktime['name'],
312  'tasktime_user_first'=>$tasktime['firstname'],
313  'tasktime_fullcivname'=>$tasktime['fullcivname']
314  );
315  }
316 
324  function get_substitutionarray_task_file($file,$outputlangs)
325  {
326  global $conf;
327 
328  return array(
329  'tasksfile_name'=>$file['name'],
330  'tasksfile_date'=>dol_print_date($file['date'],'day'),
331  'tasksfile_size'=>$file['size']
332  );
333  }
334 
335 
342  function info($langs)
343  {
344  global $conf,$langs;
345 
346  $langs->load("companies");
347  $langs->load("errors");
348 
349  $form = new Form($this->db);
350 
351  $texte = $this->description.".<br>\n";
352  $texte.= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
353  $texte.= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
354  $texte.= '<input type="hidden" name="action" value="setModuleOptions">';
355  $texte.= '<input type="hidden" name="param1" value="PROJECT_ADDON_PDF_ODT_PATH">';
356  $texte.= '<table class="nobordernopadding" width="100%">';
357 
358  // List of directories area
359  $texte.= '<tr><td>';
360  $texttitle=$langs->trans("ListOfDirectories");
361  $listofdir=explode(',',preg_replace('/[\r\n]+/',',',trim($conf->global->PROJECT_ADDON_PDF_ODT_PATH)));
362  $listoffiles=array();
363  foreach($listofdir as $key=>$tmpdir)
364  {
365  $tmpdir=trim($tmpdir);
366  $tmpdir=preg_replace('/DOL_DATA_ROOT/',DOL_DATA_ROOT,$tmpdir);
367  if (! $tmpdir) {
368  unset($listofdir[$key]); continue;
369  }
370  if (! is_dir($tmpdir)) $texttitle.=img_warning($langs->trans("ErrorDirNotFound",$tmpdir),0);
371  else
372  {
373  $tmpfiles=dol_dir_list($tmpdir,'files',0,'\.(ods|odt)');
374  if (count($tmpfiles)) $listoffiles=array_merge($listoffiles,$tmpfiles);
375  }
376  }
377  $texthelp=$langs->trans("ListOfDirectoriesForModelGenODT");
378  // Add list of substitution keys
379  $texthelp.='<br>'.$langs->trans("FollowingSubstitutionKeysCanBeUsed").'<br>';
380  $texthelp.=$langs->transnoentitiesnoconv("FullListOnOnlineDocumentation"); // This contains an url, we don't modify it
381 
382  $texte.= $form->textwithpicto($texttitle,$texthelp,1,'help','',1);
383  $texte.= '<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">';
384  $texte.= '<textarea class="flat" cols="60" name="value1">';
385  $texte.=$conf->global->PROJECT_ADDON_PDF_ODT_PATH;
386  $texte.= '</textarea>';
387  $texte.= '</div><div style="display: inline-block; vertical-align: middle;">';
388  $texte.= '<input type="submit" class="button" value="'.$langs->trans("Modify").'" name="Button">';
389  $texte.= '<br></div></div>';
390 
391  // Scan directories
392  $nbofiles=count($listoffiles);
393  if (! empty($conf->global->PROJECT_ADDON_PDF_ODT_PATH))
394  {
395  $texte.=$langs->trans("NumberOfModelFilesFound").': <b>';
396  //$texte.=$nbofiles?'<a id="a_'.get_class($this).'" href="#">':'';
397  $texte.=$nbofiles;
398  //$texte.=$nbofiles?'</a>':'';
399  $texte.='</b>';
400  }
401 
402  if ($nbofiles)
403  {
404  $texte.='<div id="div_'.get_class($this).'" class="hidden">';
405  foreach($listoffiles as $file)
406  {
407  $texte.=$file['name'].'<br>';
408  }
409  $texte.='<div id="div_'.get_class($this).'">';
410  }
411 
412  $texte.= '</td>';
413 
414  $texte.= '<td valign="top" rowspan="2" class="hideonsmartphone">';
415  $texte.= $langs->trans("ExampleOfDirectoriesForModelGen");
416  $texte.= '</td>';
417  $texte.= '</tr>';
418 
419  $texte.= '</table>';
420  $texte.= '</form>';
421 
422  return $texte;
423  }
424 
433  function write_file($object,$outputlangs,$srctemplatepath)
434  {
435  global $user,$langs,$conf,$mysoc,$hookmanager;
436 
437  if (empty($srctemplatepath))
438  {
439  dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
440  return -1;
441  }
442 
443  // Add odtgeneration hook
444  if (! is_object($hookmanager))
445  {
446  include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
447  $hookmanager=new HookManager($this->db);
448  }
449  $hookmanager->initHooks(array('odtgeneration'));
450  global $action;
451 
452  if (! is_object($outputlangs)) $outputlangs=$langs;
453  $sav_charset_output=$outputlangs->charset_output;
454  $outputlangs->charset_output='UTF-8';
455 
456  $outputlangs->load("main");
457  $outputlangs->load("dict");
458  $outputlangs->load("companies");
459  $outputlangs->load("projects");
460 
461  if ($conf->projet->dir_output)
462  {
463  // If $object is id instead of object
464  if (! is_object($object))
465  {
466  $id = $object;
467  $object = new Project($this->db);
468  $result=$object->fetch($id);
469  if ($result < 0)
470  {
471  dol_print_error($this->db,$object->error);
472  return -1;
473  }
474  }
475 
476  $dir = $conf->projet->dir_output;
477  $objectref = dol_sanitizeFileName($object->ref);
478  if (! preg_match('/specimen/i',$objectref)) $dir.= "/" . $objectref;
479  $file = $dir . "/" . $objectref . ".odt";
480 
481  if (! file_exists($dir))
482  {
483  if (dol_mkdir($dir) < 0)
484  {
485  $this->error=$langs->transnoentities("ErrorCanNotCreateDir",$dir);
486  return -1;
487  }
488  }
489 
490  if (file_exists($dir))
491  {
492  //print "srctemplatepath=".$srctemplatepath; // Src filename
493  $newfile=basename($srctemplatepath);
494  $newfiletmp=preg_replace('/\.od(t|s)/i','',$newfile);
495  $newfiletmp=preg_replace('/template_/i','',$newfiletmp);
496  $newfiletmp=preg_replace('/modele_/i','',$newfiletmp);
497  $newfiletmp=$objectref.'_'.$newfiletmp;
498  //$file=$dir.'/'.$newfiletmp.'.'.dol_print_date(dol_now(),'%Y%m%d%H%M%S').'.odt';
499  // Get extension (ods or odt)
500  $newfileformat=substr($newfile, strrpos($newfile, '.')+1);
501  if ( ! empty($conf->global->MAIN_DOC_USE_TIMING))
502  {
503  $format=$conf->global->MAIN_DOC_USE_TIMING;
504  if ($format == '1') $format='%Y%m%d%H%M%S';
505  $filename=$newfiletmp.'-'.dol_print_date(dol_now(),$format).'.'.$newfileformat;
506  }
507  else
508  {
509  $filename=$newfiletmp.'.'.$newfileformat;
510  }
511  $file=$dir.'/'.$filename;
512  //print "newdir=".$dir;
513  //print "newfile=".$newfile;
514  //print "file=".$file;
515  //print "conf->societe->dir_temp=".$conf->societe->dir_temp;
516 
517  dol_mkdir($conf->projet->dir_temp);
518 
519  // If PROJECTLEADER contact defined on project, we use it
520  $usecontact=false;
521  $arrayidcontact=$object->getIdContact('external','PROJECTLEADER');
522  if (count($arrayidcontact) > 0)
523  {
524  $usecontact=true;
525  $result=$object->fetch_contact($arrayidcontact[0]);
526  }
527 
528  // Recipient name
529  if (! empty($usecontact))
530  {
531  // if we have a PROJECTLEADER contact and we dont use it as recipient we store the contact object for later use
532  $contactobject = $object->contact;
533  }
534 
535  $socobject=$object->thirdparty;
536 
537  // Make substitution
538  $substitutionarray=array(
539  '__FROM_NAME__' => $this->emetteur->name,
540  '__FROM_EMAIL__' => $this->emetteur->email,
541  );
542  complete_substitutions_array($substitutionarray, $langs, $object);
543  // Call the ODTSubstitution hook
544  $parameters=array('file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs,'substitutionarray'=>&$tmparray);
545  $reshook=$hookmanager->executeHooks('ODTSubstitution',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
546 
547  // Open and load template
548  require_once ODTPHP_PATH.'odf.php';
549  try {
550  $odfHandler = new odf(
551  $srctemplatepath,
552  array(
553  'PATH_TO_TMP' => $conf->projet->dir_temp,
554  'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy.
555  'DELIMITER_LEFT' => '{',
556  'DELIMITER_RIGHT' => '}'
557  )
558  );
559  }
560  catch(Exception $e)
561  {
562  $this->error=$e->getMessage();
563  return -1;
564  }
565  // After construction $odfHandler->contentXml contains content and
566  // [!-- BEGIN row.lines --]*[!-- END row.lines --] has been replaced by
567  // [!-- BEGIN lines --]*[!-- END lines --]
568  //print html_entity_decode($odfHandler->__toString());
569  //print exit;
570 
571 
572 
573 
574  // Make substitutions into odt of user info
575  $array_user=$this->get_substitutionarray_user($user,$outputlangs);
576  $array_soc=$this->get_substitutionarray_mysoc($mysoc,$outputlangs);
577  $array_thirdparty=$this->get_substitutionarray_thirdparty($socobject,$outputlangs);
578  $array_objet=$this->get_substitutionarray_object($object,$outputlangs);
579  $array_other=$this->get_substitutionarray_other($outputlangs);
580  // retrieve contact information for use in project as contact_xxx tags
581  $array_project_contact = array();
582  if ($usecontact)
583  $array_project_contact=$this->get_substitutionarray_contact($contactobject,$outputlangs,'contact');
584 
585  $tmparray = array_merge($array_user,$array_soc,$array_thirdparty,$array_objet,$array_other,$array_project_contact);
586  complete_substitutions_array($tmparray, $outputlangs, $object);
587  // Call the ODTSubstitution hook
588  $parameters=array('odfHandler'=>&$odfHandler,'file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs,'substitutionarray'=>&$tmparray);
589  $reshook=$hookmanager->executeHooks('ODTSubstitution',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
590  foreach($tmparray as $key=>$value)
591  {
592  try {
593  if (preg_match('/logo$/',$key)) // Image
594  {
595  if (file_exists($value)) $odfHandler->setImage($key, $value);
596  else $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
597  }
598  else // Text
599  {
600  $odfHandler->setVars($key, $value, true, 'UTF-8');
601  }
602  }
603  catch(OdfException $e)
604  {
605  }
606  }
607 
608  // Replace tags of lines for tasks
609  try
610  {
611  $listlines = $odfHandler->setSegment('tasks');
612 
613  $taskstatic = new Task($this->db);
614 
615  // Security check
616  $socid=0;
617  if (!empty($object->fk_soc)) $socid = $object->fk_soc;
618 
619  $tasksarray=$taskstatic->getTasksArray(0, 0, $object->id, $socid, 0);
620 
621 
622  foreach ($tasksarray as $task)
623  {
624  $tmparray=$this->get_substitutionarray_tasks($task,$outputlangs);
625  //complete_substitutions_array($tmparray, $outputlangs, $object, $task, "completesubstitutionarray_lines");
626  foreach($tmparray as $key => $val)
627  {
628  try
629  {
630  $listlines->setVars($key, $val, true, 'UTF-8');
631  }
632  catch(OdfException $e)
633  {
634  }
635  catch(SegmentException $e)
636  {
637  }
638  }
639 
640  $taskobj=new Task($this->db);
641  $taskobj->fetch($task->id);
642 
643  // Replace tags of lines for contacts task
644  $sourcearray=array('internal','external');
645  $contact_arrray=array();
646  foreach ($sourcearray as $source) {
647  $contact_temp=$taskobj->liste_contact(-1,$source);
648  if ((is_array($contact_temp) && count($contact_temp) > 0))
649  {
650  $contact_arrray=array_merge($contact_arrray,$contact_temp);
651  }
652  }
653  if ((is_array($contact_arrray) && count($contact_arrray) > 0))
654  {
655  $listlinestaskres = $listlines->__get('tasksressources');
656 
657  foreach ($contact_arrray as $contact)
658  {
659  if ($contact['source']=='internal') {
660  $objectdetail=new User($this->db);
661  $objectdetail->fetch($contact['id']);
662  $contact['socname']=$mysoc->name;
663  } elseif ($contact['source']=='external') {
664  $objectdetail=new Contact($this->db);
665  $objectdetail->fetch($contact['id']);
666 
667  $soc=new Societe($this->db);
668  $soc->fetch($contact['socid']);
669  $contact['socname']=$soc->name;
670  }
671  $contact['fullname']=$objectdetail->getFullName($outputlangs,1);
672 
673  $tmparray=$this->get_substitutionarray_tasksressource($contact,$outputlangs);
674 
675  foreach($tmparray as $key => $val)
676  {
677  try
678  {
679  $listlinestaskres->setVars($key, $val, true, 'UTF-8');
680  }
681  catch(OdfException $e)
682  {
683  }
684  catch(SegmentException $e)
685  {
686  }
687  }
688  $listlinestaskres->merge();
689  }
690  }
691 
692  //Time ressources
693  $sql = "SELECT t.rowid, t.task_date, t.task_duration, t.fk_user, t.note";
694  $sql.= ", u.lastname, u.firstname";
695  $sql .= " FROM ".MAIN_DB_PREFIX."projet_task_time as t";
696  $sql .= " , ".MAIN_DB_PREFIX."user as u";
697  $sql .= " WHERE t.fk_task =".$task->id;
698  $sql .= " AND t.fk_user = u.rowid";
699  $sql .= " ORDER BY t.task_date DESC";
700 
701  $resql = $this->db->query($sql);
702  if ($resql)
703  {
704  $num = $this->db->num_rows($resql);
705  $i = 0;
706  $tasks = array();
707  $listlinestasktime = $listlines->__get('taskstimes');
708  while ($i < $num)
709  {
710  $row = $this->db->fetch_array($resql);
711  if (!empty($row['fk_user'])) {
712  $objectdetail=new User($this->db);
713  $objectdetail->fetch($row['fk_user']);
714  $row['fullcivname']=$objectdetail->getFullName($outputlangs,1);
715  } else {
716  $row['fullcivname']='';
717  }
718 
719  $tmparray=$this->get_substitutionarray_taskstime($row,$outputlangs);
720 
721  foreach($tmparray as $key => $val)
722  {
723  try
724  {
725  $listlinestasktime->setVars($key, $val, true, 'UTF-8');
726  }
727  catch(OdfException $e)
728  {
729  }
730  catch(SegmentException $e)
731  {
732  }
733  }
734  $listlinestasktime->merge();
735  $i++;
736  }
737  $this->db->free($resql);
738  }
739 
740 
741  // Replace tags of project files
742  $listtasksfiles = $listlines->__get('tasksfiles');
743 
744  $upload_dir = $conf->projet->dir_output.'/'.dol_sanitizeFileName($object->ref).'/'.dol_sanitizeFileName($task->ref);
745  $filearray=dol_dir_list($upload_dir,"files",0,'','(\.meta|_preview.*\.png)$','name',SORT_ASC,1);
746 
747 
748  foreach ($filearray as $filedetail)
749  {
750  $tmparray=$this->get_substitutionarray_task_file($filedetail,$outputlangs);
751  //dol_syslog(get_class($this).'::main $tmparray'.var_export($tmparray,true));
752  foreach($tmparray as $key => $val)
753  {
754  try
755  {
756  $listtasksfiles->setVars($key, $val, true, 'UTF-8');
757  }
758  catch(OdfException $e)
759  {
760  }
761  catch(SegmentException $e)
762  {
763  }
764  }
765  $listtasksfiles->merge();
766  }
767  $listlines->merge();
768  }
769  $odfHandler->mergeSegment($listlines);
770  }
771  catch(OdfException $e)
772  {
773  $ExceptionTrace=$e->getTrace();
774  // no segment defined on ODT is not an error
775  if($ExceptionTrace[0]['function'] != 'setSegment')
776  {
777  $this->error=$e->getMessage();
778  dol_syslog($this->error, LOG_WARNING);
779  return -1;
780  }
781  }
782 
783  // Replace tags of project files
784  try
785  {
786  $listlines = $odfHandler->setSegment('projectfiles');
787 
788  $upload_dir = $conf->projet->dir_output.'/'.dol_sanitizeFileName($object->ref);
789  $filearray=dol_dir_list($upload_dir,"files",0,'','(\.meta|_preview.*\.png)$','name',SORT_ASC,1);
790 
791  foreach ($filearray as $filedetail)
792  {
793  //dol_syslog(get_class($this).'::main $filedetail'.var_export($filedetail,true));
794  $tmparray=$this->get_substitutionarray_project_file($filedetail,$outputlangs);
795 
796  foreach($tmparray as $key => $val)
797  {
798  try
799  {
800  $listlines->setVars($key, $val, true, 'UTF-8');
801  }
802  catch(OdfException $e)
803  {
804  }
805  catch(SegmentException $e)
806  {
807  }
808  }
809  $listlines->merge();
810  }
811  $odfHandler->mergeSegment($listlines);
812  }
813  catch(OdfException $e)
814  {
815  $this->error=$e->getMessage();
816  dol_syslog($this->error, LOG_WARNING);
817  return -1;
818  }
819 
820  // Replace tags of lines for contacts
821  $sourcearray=array('internal','external');
822  $contact_arrray=array();
823  foreach ($sourcearray as $source) {
824  $contact_temp=$object->liste_contact(-1,$source);
825  if ((is_array($contact_temp) && count($contact_temp) > 0))
826  {
827  $contact_arrray=array_merge($contact_arrray,$contact_temp);
828  }
829  }
830  if ((is_array($contact_arrray) && count($contact_arrray) > 0))
831  {
832  try
833  {
834  $listlines = $odfHandler->setSegment('projectcontacts');
835 
836  foreach ($contact_arrray as $contact)
837  {
838  if ($contact['source']=='internal') {
839  $objectdetail=new User($this->db);
840  $objectdetail->fetch($contact['id']);
841  $contact['socname']=$mysoc->name;
842  } elseif ($contact['source']=='external') {
843  $objectdetail=new Contact($this->db);
844  $objectdetail->fetch($contact['id']);
845 
846  $soc=new Societe($this->db);
847  $soc->fetch($contact['socid']);
848  $contact['socname']=$soc->name;
849  }
850  $contact['fullname']=$objectdetail->getFullName($outputlangs,1);
851 
852  $tmparray=$this->get_substitutionarray_project_contacts($contact,$outputlangs);
853  foreach($tmparray as $key => $val)
854  {
855  try
856  {
857  $listlines->setVars($key, $val, true, 'UTF-8');
858  }
859  catch(OdfException $e)
860  {
861  }
862  catch(SegmentException $e)
863  {
864  }
865  }
866  $listlines->merge();
867  }
868  $odfHandler->mergeSegment($listlines);
869  }
870  catch(OdfException $e)
871  {
872  $this->error=$e->getMessage();
873  dol_syslog($this->error, LOG_WARNING);
874  return -1;
875  }
876  }
877 
878  //List of referent
879 
880  $listofreferent=array(
881  'propal'=>array(
882  'title'=>"ListProposalsAssociatedProject",
883  'class'=>'Propal',
884  'table'=>'propal',
885  'test'=>$conf->propal->enabled && $user->rights->propale->lire),
886  'order'=>array(
887  'title'=>"ListOrdersAssociatedProject",
888  'class'=>'Commande',
889  'table'=>'commande',
890  'test'=>$conf->commande->enabled && $user->rights->commande->lire),
891  'invoice'=>array(
892  'title'=>"ListInvoicesAssociatedProject",
893  'class'=>'Facture',
894  'table'=>'facture',
895  'test'=>$conf->facture->enabled && $user->rights->facture->lire),
896  'invoice_predefined'=>array(
897  'title'=>"ListPredefinedInvoicesAssociatedProject",
898  'class'=>'FactureRec',
899  'table'=>'facture_rec',
900  'test'=>$conf->facture->enabled && $user->rights->facture->lire),
901  'order_supplier'=>array(
902  'title'=>"ListSupplierOrdersAssociatedProject",
903  'table'=>'commande_fournisseur',
904  'class'=>'CommandeFournisseur',
905  'test'=>$conf->fournisseur->enabled && $user->rights->fournisseur->commande->lire),
906  'invoice_supplier'=>array(
907  'title'=>"ListSupplierInvoicesAssociatedProject",
908  'table'=>'facture_fourn',
909  'class'=>'FactureFournisseur',
910  'test'=>$conf->fournisseur->enabled && $user->rights->fournisseur->facture->lire),
911  'contract'=>array(
912  'title'=>"ListContractAssociatedProject",
913  'class'=>'Contrat',
914  'table'=>'contrat',
915  'test'=>$conf->contrat->enabled && $user->rights->contrat->lire),
916  'intervention'=>array(
917  'title'=>"ListFichinterAssociatedProject",
918  'class'=>'Fichinter',
919  'table'=>'fichinter',
920  'disableamount'=>1,
921  'test'=>$conf->ficheinter->enabled && $user->rights->ficheinter->lire),
922  'trip'=>array(
923  'title'=>"ListTripAssociatedProject",
924  'class'=>'Deplacement',
925  'table'=>'deplacement',
926  'disableamount'=>1,
927  'test'=>$conf->deplacement->enabled && $user->rights->deplacement->lire),
928  'agenda'=>array(
929  'title'=>"ListActionsAssociatedProject",
930  'class'=>'ActionComm',
931  'table'=>'actioncomm',
932  'disableamount'=>1,
933  'test'=>$conf->agenda->enabled && $user->rights->agenda->allactions->lire)
934  );
935 
936  //Insert reference
937  try
938  {
939  $listlines = $odfHandler->setSegment('projectrefs');
940 
941  foreach ($listofreferent as $keyref => $valueref)
942  {
943  $title=$valueref['title'];
944  $tablename=$valueref['table'];
945  $classname=$valueref['class'];
946  $qualified=$valueref['test'];
947  if ($qualified)
948  {
949  $elementarray = $object->get_element_list($keyref, $tablename);
950  if (count($elementarray)>0 && is_array($elementarray))
951  {
952  $var=true;
953  $total_ht = 0;
954  $total_ttc = 0;
955  $num=count($elementarray);
956  for ($i = 0; $i < $num; $i++)
957  {
958  $ref_array=array();
959  $ref_array['type']=$langs->trans($classname);
960 
961  $element = new $classname($this->db);
962  $element->fetch($elementarray[$i]);
963  $element->fetch_thirdparty();
964 
965  //Ref object
966  $ref_array['ref']=$element->ref;
967 
968  //Date object
969  $dateref=$element->date;
970  if (empty($dateref)) $dateref=$element->datep;
971  if (empty($dateref)) $dateref=$element->date_contrat;
972  $ref_array['date']=$dateref;
973 
974  //Soc object
975  if (is_object($element->thirdparty)) {
976  $ref_array['socname']=$element->thirdparty->name;
977  } else {
978  $ref_array['socname']='';
979  }
980 
981  //Amount object
982  if (empty($valueref['disableamount'])) {
983  if (!empty($element->total_ht)) {
984  $ref_array['amountht']=$element->total_ht;
985  $ref_array['amountttc']=$element->total_ttc;
986  }else {
987  $ref_array['amountht']=0;
988  $ref_array['amountttc']=0;
989  }
990  }else {
991  $ref_array['amountht']='';
992  $ref_array['amountttc']='';
993  }
994 
995  $ref_array['status']=$element->getLibStatut(0);
996 
997  $tmparray=$this->get_substitutionarray_project_reference($ref_array,$outputlangs);
998 
999  foreach($tmparray as $key => $val)
1000  {
1001  try
1002  {
1003  $listlines->setVars($key, $val, true, 'UTF-8');
1004  }
1005  catch(OdfException $e)
1006  {
1007  }
1008  catch(SegmentException $e)
1009  {
1010  }
1011  }
1012  $listlines->merge();
1013  }
1014 
1015  }
1016  }
1017  $odfHandler->mergeSegment($listlines);
1018  }
1019  }
1020  catch(OdfException $e)
1021  {
1022  $this->error=$e->getMessage();
1023  dol_syslog($this->error, LOG_WARNING);
1024  return -1;
1025  }
1026 
1027  // Replace labels translated
1028  $tmparray=$outputlangs->get_translations_for_substitutions();
1029  foreach($tmparray as $key=>$value)
1030  {
1031  try {
1032  $odfHandler->setVars($key, $value, true, 'UTF-8');
1033  }
1034  catch(OdfException $e)
1035  {
1036  }
1037  }
1038 
1039  // Call the beforeODTSave hook
1040  $parameters=array('odfHandler'=>&$odfHandler,'file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs,'substitutionarray'=>&$tmparray);
1041  $reshook=$hookmanager->executeHooks('beforeODTSave',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
1042 
1043 
1044  // Write new file
1045  if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
1046  try {
1047  $odfHandler->exportAsAttachedPDF($file);
1048  }catch (Exception $e){
1049  $this->error=$e->getMessage();
1050  return -1;
1051  }
1052  }
1053  else {
1054  try {
1055  $odfHandler->saveToDisk($file);
1056  }catch (Exception $e){
1057  $this->error=$e->getMessage();
1058  return -1;
1059  }
1060  }
1061  $parameters=array('odfHandler'=>&$odfHandler,'file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs,'substitutionarray'=>&$tmparray);
1062  $reshook=$hookmanager->executeHooks('afterODTCreation',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
1063 
1064  if (! empty($conf->global->MAIN_UMASK))
1065  @chmod($file, octdec($conf->global->MAIN_UMASK));
1066 
1067  $odfHandler=null; // Destroy object
1068 
1069  $this->result = array('fullpath'=>$file);
1070 
1071  return 1; // Success
1072  }
1073  else
1074  {
1075  $this->error=$langs->transnoentities("ErrorCanNotCreateDir",$dir);
1076  return -1;
1077  }
1078  }
1079 
1080  return -1;
1081  }
1082 
1083 }
convertSecondToTime($iSecond, $format='all', $lengthOfDay=86400, $lengthOfWeek=7)
Return, in clear text, value of a number of seconds in days, hours and minutes.
Definition: date.lib.php:171
Class to manage contact/addresses.
</td >< tdclass="liste_titre"align="right"></td ></tr >< trclass="liste_titre">< inputtype="checkbox"onClick="toggle(this)"/> Ref p ref Label p label Duration p duration warehouseinternal SELECT description FROM product_lang WHERE qty< br > qty qty qty StockTooLow img yes disabled img no img no< trclass="oddeven">< td >< inputtype="checkbox"class="check"name="'.$i.'"'.$disabled.'></td >< td >< inputtype="checkbox"class="check"name="choose'.$i.'"></td >< tdclass="nowrap"></td >< td >< inputtype="hidden"name="desc'.$i.'"value="'.dol_escape_htmltag($objp-> description
Only used if Module[ID]Desc translation string is not found.
Definition: replenish.php:554
get_substitutionarray_project_reference($refdetail, $outputlangs)
Define array with couple substitution key => substitution value.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
get_substitutionarray_tasks($task, $outputlangs)
Define array with couple substitution key => substitution value.
Class to build documents using ODF templates generator.
Class to manage Dolibarr users.
Definition: user.class.php:39
get_substitutionarray_user($user, $outputlangs)
Define array with couple subtitution key => subtitution value.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
if(empty($reshook)) $form
View.
Definition: perms.php:103
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="")
Scan a directory and return a list of files/directories.
Definition: files.lib.php:58
info($langs)
Return description of a module.
get_substitutionarray_project_contacts($contact, $outputlangs)
Define array with couple substitution key => substitution value.
Class to manage standard extra fields.
Class to manage hooks.
get_substitutionarray_contact($object, $outputlangs, $array_key= 'object')
Define array with couple subtitution key => subtitution value.
Class to manage generation of HTML components Only common components must be here.
get_substitutionarray_taskstime($tasktime, $outputlangs)
Define array with couple substitution key => substitution value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
Class to manage third parties objects (customers, suppliers, prospects...)
img_warning($titlealt= 'default', $moreatt= '')
Show warning logo.
Parent class for projects models.
Class to manage projects.
write_file($object, $outputlangs, $srctemplatepath)
Function to build a document on disk using the generic odt module.
get_substitutionarray_tasksressource($taskressource, $outputlangs)
Define array with couple substitution key => substitution value.
if($_POST["cancel"]==$langs->trans("Cancel")&&!$id) if($action== 'setdatev'&&$user->rights->tax->charges->creer) if($action== 'add'&&$_POST["cancel"]<> $langs->trans("Cancel")) if($action== 'delete') $title
Actions.
Definition: card.php:183
get_substitutionarray_mysoc($mysoc, $outputlangs)
Define array with couple subtitution key => subtitution value.
get_substitutionarray_object($object, $outputlangs, $array_key='object')
Define array with couple substitution key => substitution value.
dol_now($mode='gmt')
Return date for now.
get_substitutionarray_project_file($file, $outputlangs)
Define array with couple substitution key => substitution value.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:104
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages...
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Class to manage tasks.
Definition: task.class.php:32
get_substitutionarray_thirdparty($object, $outputlangs)
Define array with couple subtitution key => subtitution value.
dol_mkdir($dir, $dataroot='', $newmask=null)
Creation of a directory (this can create recursive subdir)
fill_substitutionarray_with_extrafields($object, $array_to_fill, $extrafields, $array_key, $outputlangs)
Fill array with couple extrafield key => extrafield value.
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
get_substitutionarray_other($outputlangs)
Define array with couple subtitution key => subtitution value.
get_substitutionarray_task_file($file, $outputlangs)
Define array with couple substitution key => substitution value.
type
Definition: viewcat.php:283
complete_substitutions_array(&$substitutionarray, $outputlangs, $object=null, $parameters=null, $callfunc="completesubstitutionarray")
Complete the $substitutionarray with more entries coming from external module that had set the "subst...