html.formcontract.class.php 6.2 KB

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