dolibarr  17.0.3
availabilities.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2022 Alice Adminson <aadminson@example.com>
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 
25 // Put here all includes required by your class file
26 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
27 //require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
28 //require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
29 
34 {
38  public $module = 'bookcal';
39 
43  public $element = 'availabilities';
44 
48  public $table_element = 'bookcal_availabilities';
49 
54  public $ismultientitymanaged = 0;
55 
59  public $isextrafieldmanaged = 1;
60 
64  public $picto = 'fa-file';
65 
66 
67  const STATUS_DRAFT = 0;
68  const STATUS_VALIDATED = 1;
69  const STATUS_CANCELED = 9;
70 
71 
100  // BEGIN MODULEBUILDER PROPERTIES
104  public $fields=array(
105  'rowid' => array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>'1', 'position'=>1, 'notnull'=>1, 'visible'=>0, 'noteditable'=>'1', 'index'=>1, 'css'=>'left', 'comment'=>"Id"),
106  'ref' => array('type'=>'varchar(128)', 'label'=>'Ref', 'enabled'=>'1', 'position'=>20, 'notnull'=>1, 'visible'=>1, 'index'=>1, 'searchall'=>1, 'showoncombobox'=>'1', 'validate'=>'1', 'comment'=>"Reference of object"),
107  'label' => array('type'=>'varchar(255)', 'label'=>'Label', 'enabled'=>'1', 'position'=>30, 'notnull'=>0, 'visible'=>1, 'searchall'=>1, 'css'=>'minwidth300', 'cssview'=>'wordbreak', 'help'=>"Help text", 'showoncombobox'=>'2', 'validate'=>'1',),
108  'description' => array('type'=>'text', 'label'=>'Description', 'enabled'=>'1', 'position'=>60, 'notnull'=>0, 'visible'=>3, 'validate'=>'1',),
109  'note_public' => array('type'=>'html', 'label'=>'NotePublic', 'enabled'=>'1', 'position'=>61, 'notnull'=>0, 'visible'=>0, 'cssview'=>'wordbreak', 'validate'=>'1',),
110  'note_private' => array('type'=>'html', 'label'=>'NotePrivate', 'enabled'=>'1', 'position'=>62, 'notnull'=>0, 'visible'=>0, 'cssview'=>'wordbreak', 'validate'=>'1',),
111  'date_creation' => array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>'1', 'position'=>500, 'notnull'=>1, 'visible'=>-2,),
112  'tms' => array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>'1', 'position'=>501, 'notnull'=>0, 'visible'=>-2,),
113  'fk_user_creat' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserAuthor', 'picto'=>'user', 'enabled'=>'1', 'position'=>510, 'notnull'=>1, 'visible'=>-2, 'foreignkey'=>'user.rowid',),
114  'fk_user_modif' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'picto'=>'user', 'enabled'=>'1', 'position'=>511, 'notnull'=>-1, 'visible'=>-2,),
115  'last_main_doc' => array('type'=>'varchar(255)', 'label'=>'LastMainDoc', 'enabled'=>'1', 'position'=>600, 'notnull'=>0, 'visible'=>0,),
116  'import_key' => array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>'1', 'position'=>1000, 'notnull'=>-1, 'visible'=>-2,),
117  'model_pdf' => array('type'=>'varchar(255)', 'label'=>'Model pdf', 'enabled'=>'1', 'position'=>1010, 'notnull'=>-1, 'visible'=>0,),
118  'status' => array('type'=>'integer', 'label'=>'Status', 'enabled'=>'1', 'position'=>2000, 'notnull'=>1, 'visible'=>1, 'index'=>1, 'arrayofkeyval'=>array('0'=>'Draft', '1'=>'Validated', '9'=>'Canceled'), 'validate'=>'1',),
119  'start' => array('type'=>'date', 'label'=>'Start Date', 'enabled'=>'1', 'position'=>40, 'notnull'=>1, 'visible'=>1, 'searchall'=>1, 'isameasure'=>'1',),
120  'end' => array('type'=>'date', 'label'=>'End Date', 'enabled'=>'1', 'position'=>45, 'notnull'=>1, 'visible'=>1, 'searchall'=>1, 'isameasure'=>'1',),
121  'type' => array('type'=>'integer', 'label'=>'Type', 'enabled'=>'1', 'position'=>49, 'notnull'=>1, 'visible'=>1, 'index'=>1, 'arrayofkeyval'=>array('0'=>'Customer', '1'=>'Supplier', '2'=>'Else'),),
122  'duration' => array('type'=>'integer', 'label'=>'Duration', 'enabled'=>'1', 'position'=>47, 'notnull'=>1, 'visible'=>1, 'default'=>'30', 'isameasure'=>'1',),
123  'startHour' => array('type'=>'integer', 'label'=>'Start Hour', 'enabled'=>'1', 'position'=>46, 'notnull'=>1, 'visible'=>-1,),
124  'endHour' => array('type'=>'integer', 'label'=>'End Hour', 'enabled'=>'1', 'position'=>46.5, 'notnull'=>1, 'visible'=>-1,),
125  );
126  public $rowid;
127  public $ref;
128  public $label;
129  public $description;
130  public $note_public;
131  public $note_private;
132  public $date_creation;
133  public $tms;
134  public $fk_user_creat;
135  public $fk_user_modif;
136  public $last_main_doc;
137  public $import_key;
138  public $model_pdf;
139  public $status;
140  public $start;
141  public $end;
142  public $type;
143  public $duration;
144  public $startHour;
145  public $endHour;
146  // END MODULEBUILDER PROPERTIES
147 
148 
149  // If this object has a subtable with lines
150 
151  // /**
152  // * @var string Name of subtable line
153  // */
154  // public $table_element_line = 'bookcal_availabilitiesline';
155 
156  // /**
157  // * @var string Field with ID of parent key if this object has a parent
158  // */
159  // public $fk_element = 'fk_availabilities';
160 
161  // /**
162  // * @var string Name of subtable class that manage subtable lines
163  // */
164  // public $class_element_line = 'Availabilitiesline';
165 
166  // /**
167  // * @var array List of child tables. To test if we can delete object.
168  // */
169  // protected $childtables = array();
170 
171  // /**
172  // * @var array List of child tables. To know object to delete on cascade.
173  // * If name matches '@ClassNAme:FilePathClass;ParentFkFieldName' it will
174  // * call method deleteByParentField(parentId, ParentFkFieldName) to fetch and delete child object
175  // */
176  // protected $childtablesoncascade = array('bookcal_availabilitiesdet');
177 
178  // /**
179  // * @var AvailabilitiesLine[] Array of subtable lines
180  // */
181  // public $lines = array();
182 
183 
184 
190  public function __construct(DoliDB $db)
191  {
192  global $conf, $langs;
193 
194  $this->db = $db;
195 
196  if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields['rowid']) && !empty($this->fields['ref'])) {
197  $this->fields['rowid']['visible'] = 0;
198  }
199  if (empty($conf->multicompany->enabled) && isset($this->fields['entity'])) {
200  $this->fields['entity']['enabled'] = 0;
201  }
202 
203  // Example to show how to set values of fields definition dynamically
204  /*if ($user->rights->bookcal->availabilities->read) {
205  $this->fields['myfield']['visible'] = 1;
206  $this->fields['myfield']['noteditable'] = 0;
207  }*/
208 
209  // Unset fields that are disabled
210  foreach ($this->fields as $key => $val) {
211  if (isset($val['enabled']) && empty($val['enabled'])) {
212  unset($this->fields[$key]);
213  }
214  }
215 
216  // Translate some data of arrayofkeyval
217  if (is_object($langs)) {
218  foreach ($this->fields as $key => $val) {
219  if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval'])) {
220  foreach ($val['arrayofkeyval'] as $key2 => $val2) {
221  $this->fields[$key]['arrayofkeyval'][$key2] = $langs->trans($val2);
222  }
223  }
224  }
225  }
226  }
227 
235  public function create(User $user, $notrigger = false)
236  {
237  $resultcreate = $this->createCommon($user, $notrigger);
238 
239  //$resultvalidate = $this->validate($user, $notrigger);
240 
241  return $resultcreate;
242  }
243 
251  public function createFromClone(User $user, $fromid)
252  {
253  global $langs, $extrafields;
254  $error = 0;
255 
256  dol_syslog(__METHOD__, LOG_DEBUG);
257 
258  $object = new self($this->db);
259 
260  $this->db->begin();
261 
262  // Load source object
263  $result = $object->fetchCommon($fromid);
264  if ($result > 0 && !empty($object->table_element_line)) {
265  $object->fetchLines();
266  }
267 
268  // get lines so they will be clone
269  //foreach($this->lines as $line)
270  // $line->fetch_optionals();
271 
272  // Reset some properties
273  unset($object->id);
274  unset($object->fk_user_creat);
275  unset($object->import_key);
276 
277  // Clear fields
278  if (property_exists($object, 'ref')) {
279  $object->ref = empty($this->fields['ref']['default']) ? "Copy_Of_".$object->ref : $this->fields['ref']['default'];
280  }
281  if (property_exists($object, 'label')) {
282  $object->label = empty($this->fields['label']['default']) ? $langs->trans("CopyOf")." ".$object->label : $this->fields['label']['default'];
283  }
284  if (property_exists($object, 'status')) {
285  $object->status = self::STATUS_DRAFT;
286  }
287  if (property_exists($object, 'date_creation')) {
288  $object->date_creation = dol_now();
289  }
290  if (property_exists($object, 'date_modification')) {
291  $object->date_modification = null;
292  }
293  // ...
294  // Clear extrafields that are unique
295  if (is_array($object->array_options) && count($object->array_options) > 0) {
296  $extrafields->fetch_name_optionals_label($this->table_element);
297  foreach ($object->array_options as $key => $option) {
298  $shortkey = preg_replace('/options_/', '', $key);
299  if (!empty($extrafields->attributes[$this->table_element]['unique'][$shortkey])) {
300  //var_dump($key);
301  //var_dump($clonedObj->array_options[$key]); exit;
302  unset($object->array_options[$key]);
303  }
304  }
305  }
306 
307  // Create clone
308  $object->context['createfromclone'] = 'createfromclone';
309  $result = $object->createCommon($user);
310  if ($result < 0) {
311  $error++;
312  $this->error = $object->error;
313  $this->errors = $object->errors;
314  }
315 
316  if (!$error) {
317  // copy internal contacts
318  if ($this->copy_linked_contact($object, 'internal') < 0) {
319  $error++;
320  }
321  }
322 
323  if (!$error) {
324  // copy external contacts if same company
325  if (!empty($object->socid) && property_exists($this, 'fk_soc') && $this->fk_soc == $object->socid) {
326  if ($this->copy_linked_contact($object, 'external') < 0) {
327  $error++;
328  }
329  }
330  }
331 
332  unset($object->context['createfromclone']);
333 
334  // End
335  if (!$error) {
336  $this->db->commit();
337  return $object;
338  } else {
339  $this->db->rollback();
340  return -1;
341  }
342  }
343 
351  public function fetch($id, $ref = null)
352  {
353  $result = $this->fetchCommon($id, $ref);
354  if ($result > 0 && !empty($this->table_element_line)) {
355  $this->fetchLines();
356  }
357  return $result;
358  }
359 
365  public function fetchLines()
366  {
367  $this->lines = array();
368 
369  $result = $this->fetchLinesCommon();
370  return $result;
371  }
372 
373 
385  public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
386  {
387  global $conf;
388 
389  dol_syslog(__METHOD__, LOG_DEBUG);
390 
391  $records = array();
392 
393  $sql = "SELECT ";
394  $sql .= $this->getFieldList('t');
395  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
396  if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1) {
397  $sql .= " WHERE t.entity IN (".getEntity($this->element).")";
398  } else {
399  $sql .= " WHERE 1 = 1";
400  }
401  // Manage filter
402  $sqlwhere = array();
403  if (count($filter) > 0) {
404  foreach ($filter as $key => $value) {
405  if ($key == 't.rowid') {
406  $sqlwhere[] = $key." = ".((int) $value);
407  } elseif (in_array($this->fields[$key]['type'], array('date', 'datetime', 'timestamp'))) {
408  $sqlwhere[] = $key." = '".$this->db->idate($value)."'";
409  } elseif ($key == 'customsql') {
410  $sqlwhere[] = $value;
411  } elseif (strpos($value, '%') === false) {
412  $sqlwhere[] = $key." IN (".$this->db->sanitize($this->db->escape($value)).")";
413  } else {
414  $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
415  }
416  }
417  }
418  if (count($sqlwhere) > 0) {
419  $sql .= " AND (".implode(" ".$filtermode." ", $sqlwhere).")";
420  }
421 
422  if (!empty($sortfield)) {
423  $sql .= $this->db->order($sortfield, $sortorder);
424  }
425  if (!empty($limit)) {
426  $sql .= $this->db->plimit($limit, $offset);
427  }
428 
429  $resql = $this->db->query($sql);
430  if ($resql) {
431  $num = $this->db->num_rows($resql);
432  $i = 0;
433  while ($i < ($limit ? min($limit, $num) : $num)) {
434  $obj = $this->db->fetch_object($resql);
435 
436  $record = new self($this->db);
437  $record->setVarsFromFetchObj($obj);
438 
439  $records[$record->id] = $record;
440 
441  $i++;
442  }
443  $this->db->free($resql);
444 
445  return $records;
446  } else {
447  $this->errors[] = 'Error '.$this->db->lasterror();
448  dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
449 
450  return -1;
451  }
452  }
453 
461  public function update(User $user, $notrigger = false)
462  {
463  return $this->updateCommon($user, $notrigger);
464  }
465 
473  public function delete(User $user, $notrigger = false)
474  {
475  return $this->deleteCommon($user, $notrigger);
476  //return $this->deleteCommon($user, $notrigger, 1);
477  }
478 
487  public function deleteLine(User $user, $idline, $notrigger = false)
488  {
489  if ($this->status < 0) {
490  $this->error = 'ErrorDeleteLineNotAllowedByObjectStatus';
491  return -2;
492  }
493 
494  return $this->deleteLineCommon($user, $idline, $notrigger);
495  }
496 
497 
505  public function validate($user, $notrigger = 0)
506  {
507  global $conf, $langs;
508 
509  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
510 
511  $error = 0;
512 
513  // Protection
514  if ($this->status == self::STATUS_VALIDATED) {
515  dol_syslog(get_class($this)."::validate action abandonned: already validated", LOG_WARNING);
516  return 0;
517  }
518 
519  /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bookcal->availabilities->write))
520  || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bookcal->availabilities->availabilities_advance->validate))))
521  {
522  $this->error='NotEnoughPermissions';
523  dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR);
524  return -1;
525  }*/
526 
527  $now = dol_now();
528 
529  $this->db->begin();
530 
531  // Define new ref
532  if (!$error && (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref))) { // empty should not happened, but when it occurs, the test save life
533  $num = $this->getNextNumRef();
534  } else {
535  $num = $this->ref;
536  }
537  $this->newref = $num;
538 
539  if (!empty($num)) {
540  // Validate
541  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
542  $sql .= " SET ref = '".$this->db->escape($num)."',";
543  $sql .= " status = ".self::STATUS_VALIDATED;
544  if (!empty($this->fields['date_validation'])) {
545  $sql .= ", date_validation = '".$this->db->idate($now)."'";
546  }
547  if (!empty($this->fields['fk_user_valid'])) {
548  $sql .= ", fk_user_valid = ".((int) $user->id);
549  }
550  $sql .= " WHERE rowid = ".((int) $this->id);
551 
552  dol_syslog(get_class($this)."::validate()", LOG_DEBUG);
553  $resql = $this->db->query($sql);
554  if (!$resql) {
555  dol_print_error($this->db);
556  $this->error = $this->db->lasterror();
557  $error++;
558  }
559 
560  if (!$error && !$notrigger) {
561  // Call trigger
562  $result = $this->call_trigger('AVAILABILITIES_VALIDATE', $user);
563  if ($result < 0) {
564  $error++;
565  }
566  // End call triggers
567  }
568  }
569 
570  if (!$error) {
571  $this->oldref = $this->ref;
572 
573  // Rename directory if dir was a temporary ref
574  if (preg_match('/^[\(]?PROV/i', $this->ref)) {
575  // Now we rename also files into index
576  $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filename = CONCAT('".$this->db->escape($this->newref)."', SUBSTR(filename, ".(strlen($this->ref) + 1).")), filepath = 'availabilities/".$this->db->escape($this->newref)."'";
577  $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'availabilities/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
578  $resql = $this->db->query($sql);
579  if (!$resql) {
580  $error++; $this->error = $this->db->lasterror();
581  }
582 
583  // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
584  $oldref = dol_sanitizeFileName($this->ref);
585  $newref = dol_sanitizeFileName($num);
586  $dirsource = $conf->bookcal->dir_output.'/availabilities/'.$oldref;
587  $dirdest = $conf->bookcal->dir_output.'/availabilities/'.$newref;
588  if (!$error && file_exists($dirsource)) {
589  dol_syslog(get_class($this)."::validate() rename dir ".$dirsource." into ".$dirdest);
590 
591  if (@rename($dirsource, $dirdest)) {
592  dol_syslog("Rename ok");
593  // Rename docs starting with $oldref with $newref
594  $listoffiles = dol_dir_list($conf->bookcal->dir_output.'/availabilities/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/'));
595  foreach ($listoffiles as $fileentry) {
596  $dirsource = $fileentry['name'];
597  $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
598  $dirsource = $fileentry['path'].'/'.$dirsource;
599  $dirdest = $fileentry['path'].'/'.$dirdest;
600  @rename($dirsource, $dirdest);
601  }
602  }
603  }
604  }
605  }
606 
607  // Set new ref and current status
608  if (!$error) {
609  $this->ref = $num;
610  $this->status = self::STATUS_VALIDATED;
611  }
612 
613  if (!$error) {
614  $this->db->commit();
615  return 1;
616  } else {
617  $this->db->rollback();
618  return -1;
619  }
620  }
621 
622 
630  public function setDraft($user, $notrigger = 0)
631  {
632  // Protection
633  if ($this->status <= self::STATUS_DRAFT) {
634  return 0;
635  }
636 
637  /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bookcal->write))
638  || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bookcal->bookcal_advance->validate))))
639  {
640  $this->error='Permission denied';
641  return -1;
642  }*/
643 
644  return $this->setStatusCommon($user, self::STATUS_DRAFT, $notrigger, 'AVAILABILITIES_UNVALIDATE');
645  }
646 
654  public function cancel($user, $notrigger = 0)
655  {
656  // Protection
657  if ($this->status != self::STATUS_VALIDATED) {
658  return 0;
659  }
660 
661  /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bookcal->write))
662  || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bookcal->bookcal_advance->validate))))
663  {
664  $this->error='Permission denied';
665  return -1;
666  }*/
667 
668  return $this->setStatusCommon($user, self::STATUS_CANCELED, $notrigger, 'AVAILABILITIES_CANCEL');
669  }
670 
678  public function reopen($user, $notrigger = 0)
679  {
680  // Protection
681  if ($this->status != self::STATUS_CANCELED) {
682  return 0;
683  }
684 
685  /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bookcal->write))
686  || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bookcal->bookcal_advance->validate))))
687  {
688  $this->error='Permission denied';
689  return -1;
690  }*/
691 
692  return $this->setStatusCommon($user, self::STATUS_VALIDATED, $notrigger, 'AVAILABILITIES_REOPEN');
693  }
694 
705  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
706  {
707  global $conf, $langs, $hookmanager;
708 
709  if (!empty($conf->dol_no_mouse_hover)) {
710  $notooltip = 1; // Force disable tooltips
711  }
712 
713  $result = '';
714 
715  $label = img_picto('', $this->picto).' <u>'.$langs->trans("Availabilities").'</u>';
716  if (isset($this->status)) {
717  $label .= ' '.$this->getLibStatut(5);
718  }
719  $label .= '<br>';
720  $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
721 
722  $url = dol_buildpath('/bookcal/availabilities_card.php', 1).'?id='.$this->id;
723 
724  if ($option != 'nolink') {
725  // Add param to save lastsearch_values or not
726  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
727  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
728  $add_save_lastsearch_values = 1;
729  }
730  if ($url && $add_save_lastsearch_values) {
731  $url .= '&save_lastsearch_values=1';
732  }
733  }
734 
735  $linkclose = '';
736  if (empty($notooltip)) {
737  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
738  $label = $langs->trans("ShowAvailabilities");
739  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
740  }
741  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
742  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
743  } else {
744  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
745  }
746 
747  if ($option == 'nolink' || empty($url)) {
748  $linkstart = '<span';
749  } else {
750  $linkstart = '<a href="'.$url.'"';
751  }
752  $linkstart .= $linkclose.'>';
753  if ($option == 'nolink' || empty($url)) {
754  $linkend = '</span>';
755  } else {
756  $linkend = '</a>';
757  }
758 
759  $result .= $linkstart;
760 
761  if (empty($this->showphoto_on_popup)) {
762  if ($withpicto) {
763  $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
764  }
765  } else {
766  if ($withpicto) {
767  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
768 
769  list($class, $module) = explode('@', $this->picto);
770  $upload_dir = $conf->$module->multidir_output[$conf->entity]."/$class/".dol_sanitizeFileName($this->ref);
771  $filearray = dol_dir_list($upload_dir, "files");
772  $filename = $filearray[0]['name'];
773  if (!empty($filename)) {
774  $pospoint = strpos($filearray[0]['name'], '.');
775 
776  $pathtophoto = $class.'/'.$this->ref.'/thumbs/'.substr($filename, 0, $pospoint).'_mini'.substr($filename, $pospoint);
777  if (empty($conf->global->{strtoupper($module.'_'.$class).'_FORMATLISTPHOTOSASUSERS'})) {
778  $result .= '<div class="floatleft inline-block valignmiddle divphotoref"><div class="photoref"><img class="photo'.$module.'" alt="No photo" border="0" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart='.$module.'&entity='.$conf->entity.'&file='.urlencode($pathtophoto).'"></div></div>';
779  } else {
780  $result .= '<div class="floatleft inline-block valignmiddle divphotoref"><img class="photouserphoto userphoto" alt="No photo" border="0" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart='.$module.'&entity='.$conf->entity.'&file='.urlencode($pathtophoto).'"></div>';
781  }
782 
783  $result .= '</div>';
784  } else {
785  $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
786  }
787  }
788  }
789 
790  if ($withpicto != 2) {
791  $result .= $this->ref;
792  }
793 
794  $result .= $linkend;
795  //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
796 
797  global $action, $hookmanager;
798  $hookmanager->initHooks(array('availabilitiesdao'));
799  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
800  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
801  if ($reshook > 0) {
802  $result = $hookmanager->resPrint;
803  } else {
804  $result .= $hookmanager->resPrint;
805  }
806 
807  return $result;
808  }
809 
816  public function getLabelStatus($mode = 0)
817  {
818  return $this->LibStatut($this->status, $mode);
819  }
820 
827  public function getLibStatut($mode = 0)
828  {
829  return $this->LibStatut($this->status, $mode);
830  }
831 
832  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
840  public function LibStatut($status, $mode = 0)
841  {
842  // phpcs:enable
843  if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
844  global $langs;
845  //$langs->load("bookcal@bookcal");
846  $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
847  $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Enabled');
848  $this->labelStatus[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv('Disabled');
849  $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
850  $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Enabled');
851  $this->labelStatusShort[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv('Disabled');
852  }
853 
854  $statusType = 'status'.$status;
855  //if ($status == self::STATUS_VALIDATED) $statusType = 'status1';
856  if ($status == self::STATUS_CANCELED) {
857  $statusType = 'status6';
858  }
859 
860  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
861  }
862 
869  public function info($id)
870  {
871  $sql = "SELECT rowid,";
872  $sql .= " date_creation as datec, tms as datem,";
873  $sql .= " fk_user_creat, fk_user_modif";
874  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
875  $sql .= " WHERE t.rowid = ".((int) $id);
876 
877  $result = $this->db->query($sql);
878  if ($result) {
879  if ($this->db->num_rows($result)) {
880  $obj = $this->db->fetch_object($result);
881 
882  $this->id = $obj->rowid;
883 
884  $this->user_creation_id = $obj->fk_user_creat;
885  $this->user_modification_id = $obj->fk_user_modif;
886  if (!empty($obj->fk_user_valid)) {
887  $this->user_validation_id = $obj->fk_user_valid;
888  }
889  $this->date_creation = $this->db->jdate($obj->datec);
890  $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
891  if (!empty($obj->datev)) {
892  $this->date_validation = empty($obj->datev) ? '' : $this->db->jdate($obj->datev);
893  }
894  }
895 
896  $this->db->free($result);
897  } else {
898  dol_print_error($this->db);
899  }
900  }
901 
908  public function initAsSpecimen()
909  {
910  // Set here init that are not commonf fields
911  // $this->property1 = ...
912  // $this->property2 = ...
913 
914  $this->initAsSpecimenCommon();
915  }
916 
922  public function getLinesArray()
923  {
924  $this->lines = array();
925 
926  $objectline = new AvailabilitiesLine($this->db);
927  $result = $objectline->fetchAll('ASC', 'position', 0, 0, array('customsql'=>'fk_availabilities = '.((int) $this->id)));
928 
929  if (is_numeric($result)) {
930  $this->error = $objectline->error;
931  $this->errors = $objectline->errors;
932  return $result;
933  } else {
934  $this->lines = $result;
935  return $this->lines;
936  }
937  }
938 
944  public function getNextNumRef()
945  {
946  global $langs, $conf;
947  $langs->load("bookcal@bookcal");
948 
949  if (empty($conf->global->BOOKCAL_AVAILABILITIES_ADDON)) {
950  $conf->global->BOOKCAL_AVAILABILITIES_ADDON = 'mod_availabilities_standard';
951  }
952 
953  if (!empty($conf->global->BOOKCAL_AVAILABILITIES_ADDON)) {
954  $mybool = false;
955 
956  $file = $conf->global->BOOKCAL_AVAILABILITIES_ADDON.".php";
957  $classname = $conf->global->BOOKCAL_AVAILABILITIES_ADDON;
958 
959  // Include file with class
960  $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
961  foreach ($dirmodels as $reldir) {
962  $dir = dol_buildpath($reldir."core/modules/bookcal/");
963 
964  // Load file with numbering class (if found)
965  $mybool |= @include_once $dir.$file;
966  }
967 
968  if ($mybool === false) {
969  dol_print_error('', "Failed to include file ".$file);
970  return '';
971  }
972 
973  if (class_exists($classname)) {
974  $obj = new $classname();
975  $numref = $obj->getNextValue($this);
976 
977  if ($numref != '' && $numref != '-1') {
978  return $numref;
979  } else {
980  $this->error = $obj->error;
981  //dol_print_error($this->db,get_class($this)."::getNextNumRef ".$obj->error);
982  return "";
983  }
984  } else {
985  print $langs->trans("Error")." ".$langs->trans("ClassNotFound").' '.$classname;
986  return "";
987  }
988  } else {
989  print $langs->trans("ErrorNumberingModuleNotSetup", $this->element);
990  return "";
991  }
992  }
993 
1005  public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
1006  {
1007  global $conf, $langs;
1008 
1009  $result = 0;
1010  $includedocgeneration = 0;
1011 
1012  $langs->load("bookcal@bookcal");
1013 
1014  if (!dol_strlen($modele)) {
1015  $modele = 'standard_availabilities';
1016 
1017  if (!empty($this->model_pdf)) {
1018  $modele = $this->model_pdf;
1019  } elseif (!empty($conf->global->AVAILABILITIES_ADDON_PDF)) {
1020  $modele = $conf->global->AVAILABILITIES_ADDON_PDF;
1021  }
1022  }
1023 
1024  $modelpath = "core/modules/bookcal/doc/";
1025 
1026  if ($includedocgeneration && !empty($modele)) {
1027  $result = $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
1028  }
1029 
1030  return $result;
1031  }
1032 
1040  public function doScheduledJob()
1041  {
1042  global $conf, $langs;
1043 
1044  //$conf->global->SYSLOG_FILE = 'DOL_DATA_ROOT/dolibarr_mydedicatedlofile.log';
1045 
1046  $error = 0;
1047  $this->output = '';
1048  $this->error = '';
1049 
1050  dol_syslog(__METHOD__, LOG_DEBUG);
1051 
1052  $now = dol_now();
1053 
1054  $this->db->begin();
1055 
1056  // ...
1057 
1058  $this->db->commit();
1059 
1060  return $error;
1061  }
1062 }
1063 
1064 
1065 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
1066 
1071 {
1072  // To complete with content of an object AvailabilitiesLine
1073  // We should have a field rowid, fk_availabilities and position
1074 
1078  public $isextrafieldmanaged = 0;
1079 
1085  public function __construct(DoliDB $db)
1086  {
1087  $this->db = $db;
1088  }
1089 }
CommonObject\deleteCommon
deleteCommon(User $user, $notrigger=false, $forcechilddeletion=0)
Delete object in database.
Definition: commonobject.class.php:9573
Availabilities
Class for Availabilities.
Definition: availabilities.class.php:33
CommonObject\setStatusCommon
setStatusCommon($user, $status, $notrigger=0, $triggercode='')
Set to a status.
Definition: commonobject.class.php:9851
db
$conf db
API class for accounts.
Definition: inc.php:41
Availabilities\fetchAll
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, array $filter=array(), $filtermode='AND')
Load list of objects in memory from the database.
Definition: availabilities.class.php:385
CommonObject\fetchCommon
fetchCommon($id, $ref=null, $morewhere='')
Load object in memory from the database.
Definition: commonobject.class.php:9363
dol_sanitizeFileName
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
Definition: functions.lib.php:1225
CommonObject\copy_linked_contact
copy_linked_contact($objFrom, $source='internal')
Copy contact from one element to current.
Definition: commonobject.class.php:1236
Availabilities\info
info($id)
Load the info information in the object.
Definition: availabilities.class.php:869
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
Availabilities\update
update(User $user, $notrigger=false)
Update object into database.
Definition: availabilities.class.php:461
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4993
Availabilities\generateDocument
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $moreparams=null)
Create a document onto disk according to template module.
Definition: availabilities.class.php:1005
AvailabilitiesLine
Class AvailabilitiesLine.
Definition: availabilities.class.php:1070
Availabilities\cancel
cancel($user, $notrigger=0)
Set cancel status.
Definition: availabilities.class.php:654
ref
$object ref
Definition: info.php:78
Availabilities\getNextNumRef
getNextNumRef()
Returns the reference to the following non used object depending on the active numbering module.
Definition: availabilities.class.php:944
dol_buildpath
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
Definition: functions.lib.php:1061
dol_dir_list
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition: files.lib.php:61
CommonObject\fetchLinesCommon
fetchLinesCommon($morewhere='')
Load object in memory from the database.
Definition: commonobject.class.php:9419
Availabilities\__construct
__construct(DoliDB $db)
Constructor.
Definition: availabilities.class.php:190
CommonObjectLine
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
Definition: commonobjectline.class.php:32
CommonObject
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Definition: commonobject.class.php:44
Availabilities\doScheduledJob
doScheduledJob()
Action executed by scheduler CAN BE A CRON TASK.
Definition: availabilities.class.php:1040
Availabilities\createFromClone
createFromClone(User $user, $fromid)
Clone an object into another one.
Definition: availabilities.class.php:251
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:4024
Availabilities\setDraft
setDraft($user, $notrigger=0)
Set draft status.
Definition: availabilities.class.php:630
CommonObject\createCommon
createCommon(User $user, $notrigger=false)
Create object into database.
Definition: commonobject.class.php:9191
Availabilities\initAsSpecimen
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
Definition: availabilities.class.php:908
Availabilities\getLibStatut
getLibStatut($mode=0)
Return the label of the status.
Definition: availabilities.class.php:827
$resql
if(isModEnabled('facture') &&!empty($user->rights->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') &&!empty($user->rights->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)) $resql
Social contributions to pay.
Definition: index.php:745
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1628
Availabilities\fetch
fetch($id, $ref=null)
Load object in memory from the database.
Definition: availabilities.class.php:351
CommonObject\updateCommon
updateCommon(User $user, $notrigger=false)
Update object into database.
Definition: commonobject.class.php:9469
Availabilities\getLinesArray
getLinesArray()
Create an array of lines.
Definition: availabilities.class.php:922
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3887
Availabilities\validate
validate($user, $notrigger=0)
Validate object.
Definition: availabilities.class.php:505
User
Class to manage Dolibarr users.
Definition: user.class.php:46
Availabilities\reopen
reopen($user, $notrigger=0)
Set back to validated status.
Definition: availabilities.class.php:678
AvailabilitiesLine\__construct
__construct(DoliDB $db)
Constructor.
Definition: availabilities.class.php:1085
dolGetStatus
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
Definition: functions.lib.php:10745
img_object
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Definition: functions.lib.php:4360
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2951
Availabilities\fetchLines
fetchLines()
Load object lines in memory from the database.
Definition: availabilities.class.php:365
CommonObject\deleteLineCommon
deleteLineCommon(User $user, $idline, $notrigger=false)
Delete a line of object in database.
Definition: commonobject.class.php:9789
Availabilities\deleteLine
deleteLine(User $user, $idline, $notrigger=false)
Delete a line of object in database.
Definition: availabilities.class.php:487
CommonObject\call_trigger
call_trigger($triggerName, $user)
Call trigger based on this instance.
Definition: commonobject.class.php:5818
CommonObject\getFieldList
getFieldList($alias='')
Function to concat keys of fields.
Definition: commonobject.class.php:9143
Availabilities\LibStatut
LibStatut($status, $mode=0)
Return the status.
Definition: availabilities.class.php:840
Availabilities\create
create(User $user, $notrigger=false)
Create object into database.
Definition: availabilities.class.php:235
Availabilities\getNomUrl
getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Return a link to the object card (with optionaly the picto)
Definition: availabilities.class.php:705
Availabilities\getLabelStatus
getLabelStatus($mode=0)
Return the label of the status.
Definition: availabilities.class.php:816