html.formintervention.class.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /* Copyright (C) 2012-2013 Charles-Fr BENKE <charles.fr@benke.fr>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. * or see https://www.gnu.org/
  17. */
  18. /**
  19. * \file htdocs/core/class/html.formintervention.class.php
  20. * \ingroup core
  21. * \brief File of class with all html predefined components
  22. */
  23. /**
  24. * Class to manage generation of HTML components for contract module
  25. */
  26. class FormIntervention
  27. {
  28. /**
  29. * @var DoliDB Database handler.
  30. */
  31. public $db;
  32. /**
  33. * @var string Error code (or message)
  34. */
  35. public $error = '';
  36. /**
  37. * Constructor
  38. *
  39. * @param DoliDB $db Database handler
  40. */
  41. public function __construct($db)
  42. {
  43. $this->db = $db;
  44. }
  45. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  46. /**
  47. * Show a combo list with contracts qualified for a third party
  48. *
  49. * @param int $socid Id third party (-1=all, 0=only interventions not linked to a third party, id=intervention not linked or linked to third party id)
  50. * @param int $selected Id intervention preselected
  51. * @param string $htmlname Nom de la zone html
  52. * @param int $maxlength Maximum length of label
  53. * @param int $showempty Show empty line ('1' or string to show for empty line)
  54. * @param int $draftonly Show only drafts intervention
  55. * @return int Nbre of project if OK, <0 if KO
  56. */
  57. public function select_interventions($socid = -1, $selected = '', $htmlname = 'interventionid', $maxlength = 16, $showempty = 1, $draftonly = false)
  58. {
  59. // phpcs:enable
  60. global $user, $conf, $langs;
  61. $out = '';
  62. $hideunselectables = false;
  63. // Search all contacts
  64. $sql = "SELECT f.rowid, f.ref, f.fk_soc, f.fk_statut";
  65. $sql .= " FROM ".$this->db->prefix()."fichinter as f";
  66. $sql .= " WHERE f.entity = ".$conf->entity;
  67. if ($socid != '') {
  68. if ($socid == '0') {
  69. $sql .= " AND (f.fk_soc = 0 OR f.fk_soc IS NULL)";
  70. } else {
  71. $sql .= " AND f.fk_soc = ".((int) $socid);
  72. }
  73. }
  74. if ($draftonly) $sql .= " AND f.fk_statut = 0";
  75. dol_syslog(get_class($this)."::select_intervention", LOG_DEBUG);
  76. $resql = $this->db->query($sql);
  77. if ($resql) {
  78. $out .= '<select id="interventionid" class="flat" name="'.dol_escape_htmltag($htmlname).'">';
  79. if ($showempty) {
  80. $out .= '<option value="0">';
  81. if (!is_numeric($showempty)) $out .= $showempty;
  82. else $out .= '&nbsp;';
  83. $out .= '</option>';
  84. }
  85. $num = $this->db->num_rows($resql);
  86. $i = 0;
  87. if ($num) {
  88. while ($i < $num) {
  89. $obj = $this->db->fetch_object($resql);
  90. // If we ask to filter on a company and user has no permission to see all companies and project is linked to another company, we hide project.
  91. if ($socid > 0 && (empty($obj->fk_soc) || $obj->fk_soc == $socid) && !$user->hasRight('societe', 'lire')) {
  92. // Do nothing
  93. } else {
  94. $labeltoshow = dol_trunc($obj->ref, 18);
  95. if (!empty($selected) && $selected == $obj->rowid && $obj->statut > 0) {
  96. $out .= '<option value="'.$obj->rowid.'" selected>'.$labeltoshow.'</option>';
  97. } else {
  98. $disabled = 0;
  99. if (!$obj->fk_statut > 0 && ! $draftonly) {
  100. $disabled = 1;
  101. $labeltoshow .= ' ('.$langs->trans("Draft").')';
  102. }
  103. if ($socid > 0 && (!empty($obj->fk_soc) && $obj->fk_soc != $socid)) {
  104. $disabled = 1;
  105. $labeltoshow .= ' - '.$langs->trans("LinkedToAnotherCompany");
  106. }
  107. if ($hideunselectables && $disabled) {
  108. $resultat = '';
  109. } else {
  110. $resultat = '<option value="'.$obj->rowid.'"';
  111. if ($disabled) {
  112. $resultat .= ' disabled';
  113. }
  114. $resultat .= '>'.$labeltoshow;
  115. $resultat .= '</option>';
  116. }
  117. $out .= $resultat;
  118. }
  119. }
  120. $i++;
  121. }
  122. }
  123. $out .= '</select>';
  124. $this->db->free($resql);
  125. return $out;
  126. } else {
  127. dol_print_error($this->db);
  128. return '';
  129. }
  130. }
  131. }