html.formprojet.class.php 23 KB

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