26 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
38 public $module =
'bookcal';
43 public $element =
'availabilities';
48 public $table_element =
'bookcal_availabilities';
54 public $ismultientitymanaged = 0;
59 public $isextrafieldmanaged = 1;
64 public $picto =
'fa-file';
67 const STATUS_DRAFT = 0;
68 const STATUS_VALIDATED = 1;
69 const STATUS_CANCELED = 9;
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),
120 'end' => array(
'type'=>
'date',
'label'=>
'End Date',
'enabled'=>
'1',
'position'=>45,
'notnull'=>1,
'visible'=>1,
'searchall'=>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'),
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,),
131 public $note_private;
132 public $date_creation;
134 public $fk_user_creat;
135 public $fk_user_modif;
136 public $last_main_doc;
192 global $conf, $langs;
196 if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields[
'rowid']) && !empty($this->fields[
'ref'])) {
197 $this->fields[
'rowid'][
'visible'] = 0;
199 if (empty($conf->multicompany->enabled) && isset($this->fields[
'entity'])) {
200 $this->fields[
'entity'][
'enabled'] = 0;
210 foreach ($this->fields as $key => $val) {
211 if (isset($val[
'enabled']) && empty($val[
'enabled'])) {
212 unset($this->fields[$key]);
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);
241 return $resultcreate;
253 global $langs, $extrafields;
258 $object =
new self($this->db);
263 $result = $object->fetchCommon($fromid);
264 if ($result > 0 && !empty($object->table_element_line)) {
265 $object->fetchLines();
274 unset($object->fk_user_creat);
275 unset($object->import_key);
278 if (property_exists($object,
'ref')) {
279 $object->ref = empty($this->fields[
'ref'][
'default']) ?
"Copy_Of_".$object->ref : $this->fields[
'ref'][
'default'];
281 if (property_exists($object,
'label')) {
282 $object->label = empty($this->fields[
'label'][
'default']) ? $langs->trans(
"CopyOf").
" ".$object->label : $this->fields[
'label'][
'default'];
284 if (property_exists($object,
'status')) {
285 $object->status = self::STATUS_DRAFT;
287 if (property_exists($object,
'date_creation')) {
288 $object->date_creation =
dol_now();
290 if (property_exists($object,
'date_modification')) {
291 $object->date_modification =
null;
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])) {
302 unset($object->array_options[$key]);
308 $object->context[
'createfromclone'] =
'createfromclone';
309 $result = $object->createCommon($user);
312 $this->error = $object->error;
313 $this->errors = $object->errors;
325 if (!empty($object->socid) && property_exists($this,
'fk_soc') && $this->fk_soc == $object->socid) {
332 unset($object->context[
'createfromclone']);
339 $this->db->rollback();
351 public function fetch($id, $ref =
null)
354 if ($result > 0 && !empty($this->table_element_line)) {
367 $this->lines = array();
385 public function fetchAll($sortorder =
'', $sortfield =
'', $limit = 0, $offset = 0, array $filter = array(), $filtermode =
'AND')
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).
")";
399 $sql .=
" WHERE 1 = 1";
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)).
")";
414 $sqlwhere[] = $key.
" LIKE '%".$this->db->escape($value).
"%'";
418 if (count($sqlwhere) > 0) {
419 $sql .=
" AND (".implode(
" ".$filtermode.
" ", $sqlwhere).
")";
422 if (!empty($sortfield)) {
423 $sql .= $this->db->order($sortfield, $sortorder);
425 if (!empty($limit)) {
426 $sql .= $this->db->plimit($limit, $offset);
429 $resql = $this->db->query(
$sql);
431 $num = $this->db->num_rows($resql);
433 while ($i < ($limit ? min($limit, $num) : $num)) {
434 $obj = $this->db->fetch_object($resql);
436 $record =
new self($this->db);
437 $record->setVarsFromFetchObj($obj);
439 $records[$record->id] = $record;
443 $this->db->free($resql);
447 $this->errors[] =
'Error '.$this->db->lasterror();
448 dol_syslog(__METHOD__.
' '.join(
',', $this->errors), LOG_ERR);
473 public function delete(
User $user, $notrigger =
false)
489 if ($this->status < 0) {
490 $this->error =
'ErrorDeleteLineNotAllowedByObjectStatus';
507 global $conf, $langs;
509 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
514 if ($this->status == self::STATUS_VALIDATED) {
515 dol_syslog(get_class($this).
"::validate action abandonned: already validated", LOG_WARNING);
532 if (!$error && (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref))) {
537 $this->newref = $num;
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).
"'";
547 if (!empty($this->fields[
'fk_user_valid'])) {
548 $sql .=
", fk_user_valid = ".((int) $user->id);
550 $sql .=
" WHERE rowid = ".((int) $this->
id);
552 dol_syslog(get_class($this).
"::validate()", LOG_DEBUG);
553 $resql = $this->db->query(
$sql);
556 $this->error = $this->db->lasterror();
560 if (!$error && !$notrigger) {
562 $result = $this->
call_trigger(
'AVAILABILITIES_VALIDATE', $user);
571 $this->oldref = $this->ref;
574 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
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);
580 $error++; $this->error = $this->db->lasterror();
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);
591 if (@rename($dirsource, $dirdest)) {
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);
610 $this->status = self::STATUS_VALIDATED;
617 $this->db->rollback();
633 if ($this->status <= self::STATUS_DRAFT) {
644 return $this->
setStatusCommon($user, self::STATUS_DRAFT, $notrigger,
'AVAILABILITIES_UNVALIDATE');
654 public function cancel($user, $notrigger = 0)
657 if ($this->status != self::STATUS_VALIDATED) {
668 return $this->
setStatusCommon($user, self::STATUS_CANCELED, $notrigger,
'AVAILABILITIES_CANCEL');
678 public function reopen($user, $notrigger = 0)
681 if ($this->status != self::STATUS_CANCELED) {
692 return $this->
setStatusCommon($user, self::STATUS_VALIDATED, $notrigger,
'AVAILABILITIES_REOPEN');
705 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
707 global $conf, $langs, $hookmanager;
709 if (!empty($conf->dol_no_mouse_hover)) {
715 $label =
img_picto(
'', $this->picto).
' <u>'.$langs->trans(
"Availabilities").
'</u>';
716 if (isset($this->status)) {
717 $label .=
' '.$this->getLibStatut(5);
720 $label .=
'<b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
722 $url =
dol_buildpath(
'/bookcal/availabilities_card.php', 1).
'?id='.$this->id;
724 if ($option !=
'nolink') {
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;
730 if ($url && $add_save_lastsearch_values) {
731 $url .=
'&save_lastsearch_values=1';
736 if (empty($notooltip)) {
737 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
738 $label = $langs->trans(
"ShowAvailabilities");
739 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
741 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
742 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
744 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
747 if ($option ==
'nolink' || empty($url)) {
748 $linkstart =
'<span';
750 $linkstart =
'<a href="'.$url.
'"';
752 $linkstart .= $linkclose.
'>';
753 if ($option ==
'nolink' || empty($url)) {
754 $linkend =
'</span>';
759 $result .= $linkstart;
761 if (empty($this->showphoto_on_popup)) {
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);
767 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
769 list($class, $module) = explode(
'@', $this->picto);
772 $filename = $filearray[0][
'name'];
773 if (!empty($filename)) {
774 $pospoint = strpos($filearray[0][
'name'],
'.');
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>';
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>';
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);
790 if ($withpicto != 2) {
791 $result .= $this->ref;
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);
802 $result = $hookmanager->resPrint;
804 $result .= $hookmanager->resPrint;
818 return $this->LibStatut($this->status, $mode);
829 return $this->LibStatut($this->status, $mode);
843 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
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');
854 $statusType =
'status'.$status;
856 if ($status == self::STATUS_CANCELED) {
857 $statusType =
'status6';
860 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);
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);
877 $result = $this->db->query(
$sql);
879 if ($this->db->num_rows($result)) {
880 $obj = $this->db->fetch_object($result);
882 $this->
id = $obj->rowid;
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;
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);
896 $this->db->free($result);
914 $this->initAsSpecimenCommon();
924 $this->lines = array();
927 $result = $objectline->fetchAll(
'ASC',
'position', 0, 0, array(
'customsql'=>
'fk_availabilities = '.((
int) $this->
id)));
929 if (is_numeric($result)) {
930 $this->error = $objectline->error;
931 $this->errors = $objectline->errors;
934 $this->lines = $result;
946 global $langs, $conf;
947 $langs->load(
"agenda");
949 if (empty($conf->global->BOOKCAL_AVAILABILITIES_ADDON)) {
950 $conf->global->BOOKCAL_AVAILABILITIES_ADDON =
'mod_availabilities_standard';
953 if (!empty($conf->global->BOOKCAL_AVAILABILITIES_ADDON)) {
956 $file = $conf->global->BOOKCAL_AVAILABILITIES_ADDON.
".php";
957 $classname = $conf->global->BOOKCAL_AVAILABILITIES_ADDON;
960 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
961 foreach ($dirmodels as $reldir) {
965 $mybool |= @include_once $dir.$file;
968 if ($mybool ===
false) {
973 if (class_exists($classname)) {
974 $obj =
new $classname();
975 $numref = $obj->getNextValue($this);
977 if ($numref !=
'' && $numref !=
'-1') {
980 $this->error = $obj->error;
985 print $langs->trans(
"Error").
" ".$langs->trans(
"ClassNotFound").
' '.$classname;
989 print $langs->trans(
"ErrorNumberingModuleNotSetup", $this->element);
1005 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
1007 global $conf, $langs;
1010 $includedocgeneration = 0;
1012 $langs->load(
"agenda");
1015 $modele =
'standard_availabilities';
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;
1024 $modelpath =
"core/modules/bookcal/doc/";
1026 if ($includedocgeneration && !empty($modele)) {
1027 $result = $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
1042 global $conf, $langs;
1058 $this->db->commit();
1065 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobjectline.class.php';
1078 public $isextrafieldmanaged = 0;