html.formprojet.class.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. <?php
  2. /* Copyright (c) 2013 Florian Henry <florian.henry@open-concept.pro>
  3. * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
  4. * Copyright (C) 2018 Charlene Benke <charlie@patas-monkey.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/core/class/html.formprojet.class.php
  21. * \ingroup core
  22. * \brief Class file for html component project
  23. */
  24. /**
  25. * Class to manage building of HTML components
  26. */
  27. class FormProjets
  28. {
  29. /**
  30. * @var DoliDB Database handler.
  31. */
  32. public $db;
  33. /**
  34. * @var string Error code (or message)
  35. */
  36. public $error = '';
  37. public $nboftasks;
  38. /**
  39. * Constructor
  40. *
  41. * @param DoliDB $db Database handler
  42. */
  43. public function __construct($db)
  44. {
  45. $this->db = $db;
  46. }
  47. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  48. /**
  49. * Output a combo list with projects qualified for a third party / user
  50. *
  51. * @param int $socid Id third party (-1=all, 0=only projects not linked to a third party, id=projects not linked or linked to third party id)
  52. * @param string|Project $selected Id of preselected project or Project (or ''). Note: If you know the ref, you can also provide it into $selected_input_value to save one request in some cases.
  53. * @param string $htmlname Name of HTML field
  54. * @param int $maxlength Maximum length of label
  55. * @param int $option_only Return only html options lines without the select tag
  56. * @param int $show_empty Add an empty line
  57. * @param int $discard_closed Discard closed projects (0=Keep, 1=hide completely, 2=Disable). Use a negative value to not show the "discarded" tooltip.
  58. * @param int $forcefocus Force focus on field (works with javascript only)
  59. * @param int $disabled Disabled
  60. * @param int $mode 0 for HTML mode and 1 for JSON mode
  61. * @param string $filterkey Key to filter
  62. * @param int $nooutput No print output. Return it only.
  63. * @param int $forceaddid Force to add project id in list, event if not qualified
  64. * @param string $morecss More css
  65. * @param int $htmlid Html id to use instead of htmlname
  66. * @return string Return html content
  67. */
  68. public function select_projects($socid = -1, $selected = '', $htmlname = 'projectid', $maxlength = 16, $option_only = 0, $show_empty = 1, $discard_closed = 0, $forcefocus = 0, $disabled = 0, $mode = 0, $filterkey = '', $nooutput = 0, $forceaddid = 0, $morecss = '', $htmlid = '')
  69. {
  70. // phpcs:enable
  71. global $langs, $conf, $form;
  72. $selected_input_value = '';
  73. if (is_object($selected)) {
  74. $selected_input_value = $selected->ref;
  75. $selected = $selected->id;
  76. }
  77. $out = '';
  78. if (!empty($conf->use_javascript_ajax) && !empty($conf->global->PROJECT_USE_SEARCH_TO_SELECT)) {
  79. $placeholder = '';
  80. if ($selected && empty($selected_input_value)) {
  81. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  82. $project = new Project($this->db);
  83. $project->fetch($selected);
  84. $selected_input_value = $project->ref;
  85. }
  86. $urloption = 'socid='.((int) $socid).'&htmlname='.urlencode($htmlname).'&discardclosed='.((int) $discard_closed);
  87. $out .= '<input type="text" class="minwidth200'.($morecss ? ' '.$morecss : '').'" name="search_'.$htmlname.'" id="search_'.$htmlname.'" value="'.$selected_input_value.'"'.$placeholder.' />';
  88. $out .= ajax_autocompleter($selected, $htmlname, DOL_URL_ROOT.'/projet/ajax/projects.php', $urloption, $conf->global->PROJECT_USE_SEARCH_TO_SELECT, 0, array(
  89. // 'update' => array(
  90. // 'projectid' => 'id'
  91. // )
  92. ));
  93. } else {
  94. $out .= $this->select_projects_list($socid, $selected, $htmlname, $maxlength, $option_only, $show_empty, abs($discard_closed), $forcefocus, $disabled, 0, $filterkey, 1, $forceaddid, $htmlid, $morecss);
  95. }
  96. if ($discard_closed > 0) {
  97. if (!empty($form)) {
  98. $out .= $form->textwithpicto('', $langs->trans("ClosedProjectsAreHidden"));
  99. }
  100. }
  101. if (empty($nooutput)) {
  102. print $out;
  103. return '';
  104. } else {
  105. return $out;
  106. }
  107. }
  108. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  109. /**
  110. * Returns an array with projects qualified for a third party
  111. *
  112. * @param int $socid Id third party (-1=all, 0=only projects not linked to a third party, id=projects not linked or linked to third party id)
  113. * @param int $selected Id project preselected
  114. * @param string $htmlname Nom de la zone html
  115. * @param int $maxlength Maximum length of label
  116. * @param int $option_only Return only html options lines without the select tag
  117. * @param int $show_empty Add an empty line
  118. * @param int $discard_closed Discard closed projects (0=Keep,1=hide completely,2=Disable)
  119. * @param int $forcefocus Force focus on field (works with javascript only)
  120. * @param int $disabled Disabled
  121. * @param int $mode 0 for HTML mode and 1 for array return (to be used by json_encode for example)
  122. * @param string $filterkey Key to filter
  123. * @param int $nooutput No print output. Return it only.
  124. * @param int $forceaddid Force to add project id in list, event if not qualified
  125. * @param int $htmlid Html id to use instead of htmlname
  126. * @param string $morecss More CSS
  127. * @return int Nb of project if OK, <0 if KO
  128. */
  129. public function select_projects_list($socid = -1, $selected = '', $htmlname = 'projectid', $maxlength = 24, $option_only = 0, $show_empty = 1, $discard_closed = 0, $forcefocus = 0, $disabled = 0, $mode = 0, $filterkey = '', $nooutput = 0, $forceaddid = 0, $htmlid = '', $morecss = 'maxwidth500')
  130. {
  131. // phpcs:enable
  132. global $user, $conf, $langs;
  133. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  134. if (empty($htmlid)) {
  135. $htmlid = $htmlname;
  136. }
  137. $out = '';
  138. $outarray = array();
  139. $hideunselectables = false;
  140. if (!empty($conf->global->PROJECT_HIDE_UNSELECTABLES)) {
  141. $hideunselectables = true;
  142. }
  143. $projectsListId = false;
  144. if (empty($user->rights->projet->all->lire)) {
  145. $projectstatic = new Project($this->db);
  146. $projectsListId = $projectstatic->getProjectsAuthorizedForUser($user, 0, 1);
  147. }
  148. // Search all projects
  149. $sql = "SELECT p.rowid, p.ref, p.title, p.fk_soc, p.fk_statut, p.public, s.nom as name, s.name_alias";
  150. $sql .= " FROM ".$this->db->prefix()."projet as p LEFT JOIN ".$this->db->prefix()."societe as s ON s.rowid = p.fk_soc";
  151. $sql .= " WHERE p.entity IN (".getEntity('project').")";
  152. if ($projectsListId !== false) {
  153. $sql .= " AND p.rowid IN (".$this->db->sanitize($projectsListId).")";
  154. }
  155. if ($socid == 0) {
  156. $sql .= " AND (p.fk_soc=0 OR p.fk_soc IS NULL)";
  157. }
  158. if ($socid > 0) {
  159. if (empty($conf->global->PROJECT_ALLOW_TO_LINK_FROM_OTHER_COMPANY)) {
  160. $sql .= " AND (p.fk_soc=".((int) $socid)." OR p.fk_soc IS NULL)";
  161. } elseif ($conf->global->PROJECT_ALLOW_TO_LINK_FROM_OTHER_COMPANY != 'all') { // PROJECT_ALLOW_TO_LINK_FROM_OTHER_COMPANY is 'all' or a list of ids separated by coma.
  162. $sql .= " AND (p.fk_soc IN (".$this->db->sanitize(((int) $socid).", ".$conf->global->PROJECT_ALLOW_TO_LINK_FROM_OTHER_COMPANY).") OR p.fk_soc IS NULL)";
  163. }
  164. }
  165. if (!empty($filterkey)) {
  166. $sql .= natural_search(array('p.title', 'p.ref'), $filterkey);
  167. }
  168. $sql .= " ORDER BY p.ref ASC";
  169. $resql = $this->db->query($sql);
  170. if ($resql) {
  171. if (!empty($conf->use_javascript_ajax)) {
  172. $morecss .= ' minwidth100';
  173. }
  174. if (empty($option_only)) {
  175. $out .= '<select class="flat'.($morecss ? ' '.$morecss : '').'"'.($disabled ? ' disabled="disabled"' : '').' id="'.$htmlid.'" name="'.$htmlname.'">';
  176. }
  177. if (!empty($show_empty)) {
  178. $out .= '<option value="0">&nbsp;</option>';
  179. }
  180. $num = $this->db->num_rows($resql);
  181. $i = 0;
  182. if ($num) {
  183. while ($i < $num) {
  184. $obj = $this->db->fetch_object($resql);
  185. // 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.
  186. if ($socid > 0 && (empty($obj->fk_soc) || $obj->fk_soc == $socid) && empty($user->rights->societe->lire)) {
  187. // Do nothing
  188. } else {
  189. if ($discard_closed == 1 && $obj->fk_statut == 2 && $obj->rowid != $selected) { // We discard closed except if selected
  190. $i++;
  191. continue;
  192. }
  193. $labeltoshow = dol_trunc($obj->ref, 18);
  194. //if ($obj->public) $labeltoshow.=' ('.$langs->trans("SharedProject").')';
  195. //else $labeltoshow.=' ('.$langs->trans("Private").')';
  196. $labeltoshow .= ', '.dol_trunc($obj->title, $maxlength);
  197. if ($obj->name) {
  198. $labeltoshow .= ' - '.$obj->name;
  199. if ($obj->name_alias) {
  200. $labeltoshow .= ' ('.$obj->name_alias.')';
  201. }
  202. }
  203. $disabled = 0;
  204. if ($obj->fk_statut == 0) {
  205. $disabled = 1;
  206. $labeltoshow .= ' - '.$langs->trans("Draft");
  207. } elseif ($obj->fk_statut == 2) {
  208. if ($discard_closed == 2) {
  209. $disabled = 1;
  210. }
  211. $labeltoshow .= ' - '.$langs->trans("Closed");
  212. } elseif (empty($conf->global->PROJECT_ALLOW_TO_LINK_FROM_OTHER_COMPANY) && $socid > 0 && (!empty($obj->fk_soc) && $obj->fk_soc != $socid)) {
  213. $disabled = 1;
  214. $labeltoshow .= ' - '.$langs->trans("LinkedToAnotherCompany");
  215. }
  216. if (!empty($selected) && $selected == $obj->rowid) {
  217. $out .= '<option value="'.$obj->rowid.'" selected';
  218. //if ($disabled) $out.=' disabled'; // with select2, field can't be preselected if disabled
  219. $out .= '>'.$labeltoshow.'</option>';
  220. } else {
  221. if ($hideunselectables && $disabled && ($selected != $obj->rowid)) {
  222. $resultat = '';
  223. } else {
  224. $resultat = '<option value="'.$obj->rowid.'"';
  225. if ($disabled) {
  226. $resultat .= ' disabled';
  227. }
  228. //if ($obj->public) $labeltoshow.=' ('.$langs->trans("Public").')';
  229. //else $labeltoshow.=' ('.$langs->trans("Private").')';
  230. $resultat .= '>';
  231. $resultat .= $labeltoshow;
  232. $resultat .= '</option>';
  233. }
  234. $out .= $resultat;
  235. $outarray[] = array(
  236. 'key' => (int) $obj->rowid,
  237. 'value' => $obj->ref,
  238. 'ref' => $obj->ref,
  239. 'labelx' => $labeltoshow,
  240. 'label' => ((bool) $disabled) ? '<span class="opacitymedium">'.$labeltoshow.'</span>' : $labeltoshow,
  241. 'disabled' => (bool) $disabled
  242. );
  243. }
  244. }
  245. $i++;
  246. }
  247. }
  248. $this->db->free($resql);
  249. if (!$mode) {
  250. if (empty($option_only)) {
  251. $out .= '</select>';
  252. }
  253. // Use select2 selector
  254. if (!empty($conf->use_javascript_ajax)) {
  255. include_once DOL_DOCUMENT_ROOT.'/core/lib/ajax.lib.php';
  256. $comboenhancement = ajax_combobox($htmlid, array(), 0, $forcefocus);
  257. $out .= $comboenhancement;
  258. $morecss .= ' minwidth100';
  259. }
  260. if (empty($nooutput)) {
  261. print $out;
  262. return '';
  263. } else {
  264. return $out;
  265. }
  266. } else {
  267. return $outarray;
  268. }
  269. } else {
  270. dol_print_error($this->db);
  271. return -1;
  272. }
  273. }
  274. /**
  275. * Output a combo list with tasks qualified for a third party
  276. *
  277. * @param int $socid Id third party (-1=all, 0=only projects not linked to a third party, id=projects not linked or linked to third party id)
  278. * @param int $selected Id task preselected
  279. * @param string $htmlname Name of HTML select
  280. * @param int $maxlength Maximum length of label
  281. * @param int $option_only Return only html options lines without the select tag
  282. * @param string $show_empty Add an empty line ('1' or string to show for empty line)
  283. * @param int $discard_closed Discard closed projects (0=Keep, 1=hide completely, 2=Disable)
  284. * @param int $forcefocus Force focus on field (works with javascript only)
  285. * @param int $disabled Disabled
  286. * @param string $morecss More css added to the select component
  287. * @param string $projectsListId ''=Automatic filter on project allowed. List of id=Filter on project ids.
  288. * @param string $showmore 'all' = Show project info, 'progress' = Show task progression, ''=Show nothing more
  289. * @param User $usertofilter User object to use for filtering
  290. * @return int Nbr of tasks if OK, <0 if KO
  291. */
  292. public function selectTasks($socid = -1, $selected = '', $htmlname = 'taskid', $maxlength = 24, $option_only = 0, $show_empty = '1', $discard_closed = 0, $forcefocus = 0, $disabled = 0, $morecss = 'maxwidth500', $projectsListId = '', $showmore = 'all', $usertofilter = null)
  293. {
  294. global $user, $conf, $langs;
  295. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  296. if (is_null($usertofilter)) {
  297. $usertofilter = $user;
  298. }
  299. $out = '';
  300. $hideunselectables = false;
  301. if (!empty($conf->global->PROJECT_HIDE_UNSELECTABLES)) {
  302. $hideunselectables = true;
  303. }
  304. if (empty($projectsListId)) {
  305. if (empty($usertofilter->rights->projet->all->lire)) {
  306. $projectstatic = new Project($this->db);
  307. $projectsListId = $projectstatic->getProjectsAuthorizedForUser($usertofilter, 0, 1);
  308. }
  309. }
  310. // Search all projects
  311. $sql = "SELECT t.rowid, t.ref as tref, t.label as tlabel, t.progress,";
  312. $sql .= " p.rowid as pid, p.ref, p.title, p.fk_soc, p.fk_statut, p.public, p.usage_task,";
  313. $sql .= " s.nom as name";
  314. $sql .= " FROM ".$this->db->prefix()."projet as p";
  315. $sql .= " LEFT JOIN ".$this->db->prefix()."societe as s ON s.rowid = p.fk_soc,";
  316. $sql .= " ".$this->db->prefix()."projet_task as t";
  317. $sql .= " WHERE p.entity IN (".getEntity('project').")";
  318. $sql .= " AND t.fk_projet = p.rowid";
  319. if ($projectsListId) {
  320. $sql .= " AND p.rowid IN (".$this->db->sanitize($projectsListId).")";
  321. }
  322. if ($socid == 0) {
  323. $sql .= " AND (p.fk_soc=0 OR p.fk_soc IS NULL)";
  324. }
  325. if ($socid > 0) {
  326. $sql .= " AND (p.fk_soc=".((int) $socid)." OR p.fk_soc IS NULL)";
  327. }
  328. $sql .= " ORDER BY p.ref, t.ref ASC";
  329. $resql = $this->db->query($sql);
  330. if ($resql) {
  331. // Use select2 selector
  332. if (empty($option_only) && !empty($conf->use_javascript_ajax)) {
  333. include_once DOL_DOCUMENT_ROOT.'/core/lib/ajax.lib.php';
  334. $comboenhancement = ajax_combobox($htmlname, '', 0, $forcefocus);
  335. $out .= $comboenhancement;
  336. $morecss = 'minwidth200 maxwidth500';
  337. }
  338. if (empty($option_only)) {
  339. $out .= '<select class="valignmiddle flat'.($morecss ? ' '.$morecss : '').'"'.($disabled ? ' disabled="disabled"' : '').' id="'.$htmlname.'" name="'.$htmlname.'">';
  340. }
  341. if (!empty($show_empty)) {
  342. $out .= '<option value="0" class="optiongrey">';
  343. if (!is_numeric($show_empty)) {
  344. //if (! empty($conf->use_javascript_ajax)) $out .= '<span class="opacitymedium">aaa';
  345. $out .= $show_empty;
  346. //if (! empty($conf->use_javascript_ajax)) $out .= '</span>';
  347. } else {
  348. $out .= '&nbsp;';
  349. }
  350. $out .= '</option>';
  351. }
  352. $num = $this->db->num_rows($resql);
  353. $i = 0;
  354. if ($num) {
  355. while ($i < $num) {
  356. $obj = $this->db->fetch_object($resql);
  357. // 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.
  358. if ($socid > 0 && (empty($obj->fk_soc) || $obj->fk_soc == $socid) && empty($usertofilter->rights->societe->lire)) {
  359. // Do nothing
  360. } else {
  361. if ($discard_closed == 1 && $obj->fk_statut == Project::STATUS_CLOSED) {
  362. $i++;
  363. continue;
  364. }
  365. $labeltoshow = '';
  366. $titletoshow = '';
  367. $disabled = 0;
  368. if ($obj->fk_statut == Project::STATUS_DRAFT) {
  369. $disabled = 1;
  370. } elseif ($obj->fk_statut == Project::STATUS_CLOSED) {
  371. if ($discard_closed == 2) {
  372. $disabled = 1;
  373. }
  374. } elseif ($socid > 0 && (!empty($obj->fk_soc) && $obj->fk_soc != $socid)) {
  375. $disabled = 1;
  376. }
  377. if (preg_match('/all/', $showmore)) {
  378. $labeltoshow .= dol_trunc($obj->ref, 18); // Project ref
  379. //if ($obj->public) $labeltoshow.=' ('.$langs->trans("SharedProject").')';
  380. //else $labeltoshow.=' ('.$langs->trans("Private").')';
  381. $labeltoshow .= ' '.dol_trunc($obj->title, $maxlength);
  382. $titletoshow = $labeltoshow;
  383. if ($obj->name) {
  384. $labeltoshow .= ' ('.$obj->name.')';
  385. $titletoshow .= ' <span class="opacitymedium">('.$obj->name.')</span>';
  386. }
  387. $disabled = 0;
  388. if ($obj->fk_statut == Project::STATUS_DRAFT) {
  389. $disabled = 1;
  390. $labeltoshow .= ' - '.$langs->trans("Draft");
  391. $titletoshow .= ' - <span class="opacitymedium">'.$langs->trans("Draft").'</span>';
  392. } elseif ($obj->fk_statut == Project::STATUS_CLOSED) {
  393. if ($discard_closed == 2) {
  394. $disabled = 1;
  395. }
  396. $labeltoshow .= ' - '.$langs->trans("Closed");
  397. $titletoshow .= ' - <span class="opacitymedium">'.$langs->trans("Closed").'</span>';
  398. } elseif ($socid > 0 && (!empty($obj->fk_soc) && $obj->fk_soc != $socid)) {
  399. $disabled = 1;
  400. $labeltoshow .= ' - '.$langs->trans("LinkedToAnotherCompany");
  401. $titletoshow .= ' - <span class="opacitymedium">'.$langs->trans("LinkedToAnotherCompany").'</span>';
  402. }
  403. $labeltoshow .= ' - ';
  404. $titletoshow .= ' - ';
  405. }
  406. // Label for task
  407. $labeltoshow .= $obj->tref.' '.dol_trunc($obj->tlabel, $maxlength);
  408. $titletoshow .= $obj->tref.' '.dol_trunc($obj->tlabel, $maxlength);
  409. if ($obj->usage_task && preg_match('/progress/', $showmore)) {
  410. $labeltoshow .= ' <span class="opacitymedium">('.$obj->progress.'%)</span>';
  411. $titletoshow .= ' <span class="opacitymedium">('.$obj->progress.'%)</span>';
  412. }
  413. if (!empty($selected) && $selected == $obj->rowid) {
  414. $out .= '<option value="'.$obj->rowid.'" selected';
  415. $out .= ' data-html="'.dol_escape_htmltag($titletoshow).'"';
  416. //if ($disabled) $out.=' disabled'; // with select2, field can't be preselected if disabled
  417. $out .= '>'.$labeltoshow.'</option>';
  418. } else {
  419. if ($hideunselectables && $disabled && ($selected != $obj->rowid)) {
  420. $resultat = '';
  421. } else {
  422. $resultat = '<option value="'.$obj->rowid.'"';
  423. if ($disabled) {
  424. $resultat .= ' disabled';
  425. }
  426. //if ($obj->public) $labeltoshow.=' ('.$langs->trans("Public").')';
  427. //else $labeltoshow.=' ('.$langs->trans("Private").')';
  428. $resultat .= ' data-html="'.dol_escape_htmltag($titletoshow).'"';
  429. $resultat .= '>';
  430. $resultat .= $labeltoshow;
  431. $resultat .= '</option>';
  432. }
  433. $out .= $resultat;
  434. }
  435. }
  436. $i++;
  437. }
  438. }
  439. if (empty($option_only)) {
  440. $out .= '</select>';
  441. }
  442. $this->nboftasks = $num;
  443. print $out;
  444. $this->db->free($resql);
  445. return $num;
  446. } else {
  447. dol_print_error($this->db);
  448. return -1;
  449. }
  450. }
  451. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  452. /**
  453. * Build a HTML select list of element of same thirdparty to suggest to link them to project
  454. *
  455. * @param string $table_element Table of the element to update
  456. * @param string $socid If of thirdparty to use as filter or 'id1,id2,...'
  457. * @param string $morecss More CSS
  458. * @param int $limitonstatus Add filters to limit length of list to opened status (for example to avoid ERR_RESPONSE_HEADERS_TOO_BIG on project/element.php page). TODO To implement
  459. * @param string $projectkey Equivalent key to fk_projet for actual table_element
  460. * @param string $placeholder Placeholder
  461. * @return int|string The HTML select list of element or '' if nothing or -1 if KO
  462. */
  463. public function select_element($table_element, $socid = 0, $morecss = '', $limitonstatus = -2, $projectkey = "fk_projet", $placeholder = '')
  464. {
  465. // phpcs:enable
  466. global $conf, $langs;
  467. if ($table_element == 'projet_task') {
  468. return ''; // Special cas of element we never link to a project (already always done)
  469. }
  470. $linkedtothirdparty = false;
  471. if (!in_array($table_element, array(
  472. 'don',
  473. 'expensereport_det',
  474. 'expensereport', 'loan',
  475. 'stock_mouvement',
  476. 'payment_salary',
  477. 'payment_various',
  478. 'salary',
  479. 'chargesociales',
  480. 'entrepot')
  481. )) {
  482. $linkedtothirdparty = true;
  483. }
  484. $sqlfilter = '';
  485. //print $table_element;
  486. switch ($table_element) {
  487. case "loan":
  488. $sql = "SELECT t.rowid, t.label as ref";
  489. break;
  490. case "facture":
  491. $sql = "SELECT t.rowid, t.ref as ref";
  492. break;
  493. case "facture_fourn":
  494. $sql = "SELECT t.rowid, t.ref, t.ref_supplier";
  495. break;
  496. case "commande_fourn":
  497. case "commande_fournisseur":
  498. $sql = "SELECT t.rowid, t.ref, t.ref_supplier";
  499. break;
  500. case "facture_rec":
  501. $sql = "SELECT t.rowid, t.titre as ref";
  502. break;
  503. case "actioncomm":
  504. $sql = "SELECT t.id as rowid, t.label as ref";
  505. $projectkey = "fk_project";
  506. break;
  507. case "expensereport":
  508. return '';
  509. case "expensereport_det":
  510. /*$sql = "SELECT rowid, '' as ref"; // table is llx_expensereport_det
  511. $projectkey="fk_projet";
  512. break;*/
  513. return '';
  514. case "commande":
  515. case "contrat":
  516. case "fichinter":
  517. $sql = "SELECT t.rowid, t.ref";
  518. break;
  519. case 'stock_mouvement':
  520. $sql = "SELECT t.rowid, t.label as ref";
  521. $projectkey = 'fk_origin';
  522. break;
  523. case "payment_salary":
  524. $sql = "SELECT t.rowid, t.num_payment as ref"; // TODO In a future fill and use real ref field
  525. break;
  526. case "payment_various":
  527. $sql = "SELECT t.rowid, t.num_payment as ref";
  528. break;
  529. case "chargesociales":
  530. default:
  531. $sql = "SELECT t.rowid, t.ref";
  532. break;
  533. }
  534. if ($linkedtothirdparty) {
  535. $sql .= ", s.nom as name";
  536. }
  537. $sql .= " FROM ".$this->db->prefix().$table_element." as t";
  538. if ($linkedtothirdparty) {
  539. $sql .= ", ".$this->db->prefix()."societe as s";
  540. }
  541. $sql .= " WHERE ".$projectkey." is null";
  542. if (!empty($socid) && $linkedtothirdparty) {
  543. if (is_numeric($socid)) {
  544. $sql .= " AND t.fk_soc = ".((int) $socid);
  545. } else {
  546. $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socid).")";
  547. }
  548. }
  549. if (!in_array($table_element, array('expensereport_det', 'stock_mouvement'))) {
  550. $sql .= ' AND t.entity IN ('.getEntity('project').')';
  551. }
  552. if ($linkedtothirdparty) {
  553. $sql .= " AND s.rowid = t.fk_soc";
  554. }
  555. if ($sqlfilter) {
  556. $sql .= " AND ".$sqlfilter;
  557. }
  558. $sql .= " ORDER BY ref DESC";
  559. dol_syslog(get_class($this).'::select_element', LOG_DEBUG);
  560. $resql = $this->db->query($sql);
  561. if ($resql) {
  562. $num = $this->db->num_rows($resql);
  563. $i = 0;
  564. if ($num > 0) {
  565. $sellist = '<select class="flat elementselect css'.$table_element.($morecss ? ' '.$morecss : '').'" name="elementselect">';
  566. $sellist .= '<option value="-1"'.($placeholder ? ' class="optiongrey"' : '').'>'.$placeholder.'</option>';
  567. while ($i < $num) {
  568. $obj = $this->db->fetch_object($resql);
  569. $ref = $obj->ref ? $obj->ref : $obj->rowid;
  570. if (!empty($obj->ref_supplier)) {
  571. $ref .= ' ('.$obj->ref_supplier.')';
  572. }
  573. if (!empty($obj->name)) {
  574. $ref .= ' - '.$obj->name;
  575. }
  576. $sellist .= '<option value="'.$obj->rowid.'">'.$ref.'</option>';
  577. $i++;
  578. }
  579. $sellist .= '</select>';
  580. }
  581. /*else
  582. {
  583. $sellist = '<select class="flat" name="elementselect">';
  584. $sellist.= '<option value="0" disabled>'.$langs->trans("None").'</option>';
  585. $sellist.= '</select>';
  586. }*/
  587. $this->db->free($resql);
  588. return $sellist;
  589. } else {
  590. dol_print_error($this->db);
  591. $this->error = $this->db->lasterror();
  592. $this->errors[] = $this->db->lasterror();
  593. dol_syslog(get_class($this)."::select_element ".$this->error, LOG_ERR);
  594. return -1;
  595. }
  596. }
  597. /**
  598. * Build a HTML select list of element of same thirdparty to suggest to link them to project
  599. *
  600. * @param string $htmlname HTML name
  601. * @param string $preselected Preselected (int or 'all' or 'none')
  602. * @param int $showempty Add an empty line
  603. * @param int $useshortlabel Use short label
  604. * @param int $showallnone Add choice "All" and "None"
  605. * @param int $showpercent Show default probability for status
  606. * @param string $morecss Add more css
  607. * @param int $noadmininfo 0=Add admin info, 1=Disable admin info
  608. * @param int $addcombojs 1=Add a js combo
  609. * @return int|string The HTML select list of element or '' if nothing or -1 if KO
  610. */
  611. public function selectOpportunityStatus($htmlname, $preselected = '-1', $showempty = 1, $useshortlabel = 0, $showallnone = 0, $showpercent = 0, $morecss = '', $noadmininfo = 0, $addcombojs = 0)
  612. {
  613. global $conf, $langs, $user;
  614. $sql = "SELECT rowid, code, label, percent";
  615. $sql .= " FROM ".$this->db->prefix().'c_lead_status';
  616. $sql .= " WHERE active = 1";
  617. $sql .= " ORDER BY position";
  618. $resql = $this->db->query($sql);
  619. if ($resql) {
  620. $num = $this->db->num_rows($resql);
  621. $i = 0;
  622. if ($num > 0) {
  623. $sellist = '<select class="flat oppstatus'.($morecss ? ' '.$morecss : '').'" id="'.$htmlname.'" name="'.$htmlname.'">';
  624. if ($showempty) {
  625. // Without &nbsp, strange move of screen when switching value
  626. $sellist .= '<option value="-1">&nbsp;</option>';
  627. }
  628. if ($showallnone) {
  629. $sellist .= '<option value="all"'.($preselected == 'all' ? ' selected="selected"' : '').'>-- '.$langs->trans("OnlyOpportunitiesShort").' --</option>';
  630. $sellist .= '<option value="openedopp"'.($preselected == 'openedopp' ? ' selected="selected"' : '').'>-- '.$langs->trans("OpenedOpportunitiesShort").' --</option>';
  631. $sellist .= '<option value="notopenedopp"'.($preselected == 'notopenedopp' ? ' selected="selected"' : '').'>-- '.$langs->trans("NotOpenedOpportunitiesShort").' --</option>';
  632. $sellist .= '<option value="none"'.($preselected == 'none' ? ' selected="selected"' : '').'>-- '.$langs->trans("NotAnOpportunityShort").' --</option>';
  633. }
  634. while ($i < $num) {
  635. $obj = $this->db->fetch_object($resql);
  636. $sellist .= '<option value="'.$obj->rowid.'" defaultpercent="'.$obj->percent.'" elemcode="'.$obj->code.'"';
  637. if ($obj->rowid == $preselected) {
  638. $sellist .= ' selected="selected"';
  639. }
  640. $sellist .= '>';
  641. if ($useshortlabel) {
  642. $finallabel = ($langs->transnoentitiesnoconv("OppStatus".$obj->code) != "OppStatus".$obj->code ? $langs->transnoentitiesnoconv("OppStatus".$obj->code) : $obj->label);
  643. } else {
  644. $finallabel = ($langs->transnoentitiesnoconv("OppStatus".$obj->code) != "OppStatus".$obj->code ? $langs->transnoentitiesnoconv("OppStatus".$obj->code) : $obj->label);
  645. if ($showpercent) {
  646. $finallabel .= ' ('.$obj->percent.'%)';
  647. }
  648. }
  649. $sellist .= $finallabel;
  650. $sellist .= '</option>';
  651. $i++;
  652. }
  653. $sellist .= '</select>';
  654. if ($user->admin && !$noadmininfo) {
  655. $sellist .= info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
  656. }
  657. if ($addcombojs) {
  658. $sellist .= ajax_combobox($htmlname);
  659. }
  660. }
  661. /*else
  662. {
  663. $sellist = '<select class="flat" name="elementselect">';
  664. $sellist.= '<option value="0" disabled>'.$langs->trans("None").'</option>';
  665. $sellist.= '</select>';
  666. }*/
  667. $this->db->free($resql);
  668. return $sellist;
  669. } else {
  670. $this->error = $this->db->lasterror();
  671. $this->errors[] = $this->db->lasterror();
  672. dol_syslog(get_class($this)."::selectOpportunityStatus ".$this->error, LOG_ERR);
  673. return -1;
  674. }
  675. }
  676. }