html.formcontract.class.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /* Copyright (C) 2012-2018 Charlene BENKE <charlie@patas-monkey.com>
  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.formcontract.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 FormContract
  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 contracts not linked to a third party, id=contracts not linked or linked to third party id)
  50. * @param int $selected Id contract 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
  54. * @param int $showRef Show customer and supplier reference on each contract (when found)
  55. * @return int Nbr of project if OK, <0 if KO
  56. */
  57. public function select_contract($socid = -1, $selected = '', $htmlname = 'contrattid', $maxlength = 16, $showempty = 1, $showRef = 0)
  58. {
  59. // phpcs:enable
  60. global $user, $conf, $langs;
  61. $hideunselectables = false;
  62. if (!empty($conf->global->CONTRACT_HIDE_UNSELECTABLES)) {
  63. $hideunselectables = true;
  64. }
  65. // Search all contacts
  66. $sql = "SELECT c.rowid, c.ref, c.fk_soc, c.statut,";
  67. $sql .= " c.ref_customer, c.ref_supplier";
  68. $sql .= " FROM ".$this->db->prefix()."contrat as c";
  69. $sql .= " WHERE c.entity = ".$conf->entity;
  70. //if ($contratListId) $sql.= " AND c.rowid IN (".$this->db->sanitize($contratListId).")";
  71. if ($socid > 0) {
  72. // CONTRACT_ALLOW_TO_LINK_FROM_OTHER_COMPANY is 'all' or a list of ids separated by coma.
  73. if (empty($conf->global->CONTRACT_ALLOW_TO_LINK_FROM_OTHER_COMPANY)) {
  74. $sql .= " AND (c.fk_soc=".((int) $socid)." OR c.fk_soc IS NULL)";
  75. } elseif ($conf->global->CONTRACT_ALLOW_TO_LINK_FROM_OTHER_COMPANY != 'all') {
  76. $sql .= " AND (c.fk_soc IN (".$this->db->sanitize($socid.", ".$conf->global->CONTRACT_ALLOW_TO_LINK_FROM_OTHER_COMPANY).") ";
  77. $sql .= " OR c.fk_soc IS NULL)";
  78. }
  79. }
  80. if ($socid == 0) {
  81. $sql .= " AND (c.fk_soc = 0 OR c.fk_soc IS NULL)";
  82. }
  83. $sql .= " ORDER BY c.ref ";
  84. dol_syslog(get_class($this)."::select_contract", LOG_DEBUG);
  85. $resql = $this->db->query($sql);
  86. if ($resql) {
  87. print '<select class="flat" name="'.$htmlname.'">';
  88. if ($showempty) {
  89. print '<option value="0">&nbsp;</option>';
  90. }
  91. $num = $this->db->num_rows($resql);
  92. $i = 0;
  93. if ($num) {
  94. while ($i < $num) {
  95. $obj = $this->db->fetch_object($resql);
  96. // 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.
  97. if ($socid > 0 && (empty($obj->fk_soc) || $obj->fk_soc == $socid) && !$user->hasRight('societe', 'lire')) {
  98. // Do nothing
  99. } else {
  100. $labeltoshow = dol_trunc($obj->ref, 18);
  101. if ($showRef) {
  102. if ($obj->ref_customer) {
  103. $labeltoshow = $labeltoshow." - ".$obj->ref_customer;
  104. }
  105. if ($obj->ref_supplier) {
  106. $labeltoshow = $labeltoshow." - ".$obj->ref_supplier;
  107. }
  108. }
  109. //if ($obj->public) $labeltoshow.=' ('.$langs->trans("SharedProject").')';
  110. //else $labeltoshow.=' ('.$langs->trans("Private").')';
  111. if (!empty($selected) && $selected == $obj->rowid && $obj->statut > 0) {
  112. print '<option value="'.$obj->rowid.'" selected>'.$labeltoshow.'</option>';
  113. } else {
  114. $disabled = 0;
  115. if ($obj->statut == 0) {
  116. $disabled = 1;
  117. $labeltoshow .= ' ('.$langs->trans("Draft").')';
  118. }
  119. if (empty($conf->global->CONTRACT_ALLOW_TO_LINK_FROM_OTHER_COMPANY) && $socid > 0 && (!empty($obj->fk_soc) && $obj->fk_soc != $socid)) {
  120. $disabled = 1;
  121. $labeltoshow .= ' - '.$langs->trans("LinkedToAnotherCompany");
  122. }
  123. if ($hideunselectables && $disabled) {
  124. $resultat = '';
  125. } else {
  126. $resultat = '<option value="'.$obj->rowid.'"';
  127. if ($disabled) {
  128. $resultat .= ' disabled';
  129. }
  130. //if ($obj->public) $labeltoshow.=' ('.$langs->trans("Public").')';
  131. //else $labeltoshow.=' ('.$langs->trans("Private").')';
  132. $resultat .= '>'.$labeltoshow;
  133. $resultat .= '</option>';
  134. }
  135. print $resultat;
  136. }
  137. }
  138. $i++;
  139. }
  140. }
  141. print '</select>';
  142. $this->db->free($resql);
  143. if (!empty($conf->use_javascript_ajax)) {
  144. // Make select dynamic
  145. include_once DOL_DOCUMENT_ROOT.'/core/lib/ajax.lib.php';
  146. print ajax_combobox($htmlname);
  147. }
  148. return $num;
  149. } else {
  150. dol_print_error($this->db);
  151. return -1;
  152. }
  153. }
  154. /**
  155. * Show a form to select a contract
  156. *
  157. * @param int $page Page
  158. * @param int $socid Id third party (-1=all, 0=only contracts not linked to a third party, id=contracts not linked or linked to third party id)
  159. * @param int $selected Id contract preselected
  160. * @param string $htmlname Nom de la zone html
  161. * @param int $maxlength Maximum length of label
  162. * @param int $showempty Show empty line
  163. * @param int $showRef Show customer and supplier reference on each contract (when found)
  164. * @return int Nbr of project if OK, <0 if KO
  165. */
  166. public function formSelectContract($page, $socid = -1, $selected = '', $htmlname = 'contrattid', $maxlength = 16, $showempty = 1, $showRef = 0)
  167. {
  168. global $langs;
  169. print "\n";
  170. print '<form method="post" action="'.$page.'">';
  171. print '<input type="hidden" name="action" value="setcontract">';
  172. print '<input type="hidden" name="token" value="'.newToken().'">';
  173. $this->select_contract($socid, $selected, $htmlname, $maxlength, $showempty, $showRef);
  174. print '<input type="submit" class="button smallpaddingimp valignmiddle" value="'.$langs->trans("Modify").'">';
  175. print '</form>';
  176. }
  177. }