PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/class/html.formcompany.class.php

https://bitbucket.org/speedealing/speedealing
PHP | 375 lines | 213 code | 33 blank | 129 comment | 75 complexity | c394d2767ba131241da8413273a5e6cf MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2008-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2008-2013 Regis Houssin <regis.houssin@capnetworks.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.formcompany.class.php
  20. * \ingroup core
  21. * \brief File of class to build HTML component for third parties management
  22. */
  23. /**
  24. * Class to build HTML component for third parties management
  25. * Only common components are here.
  26. */
  27. class FormCompany
  28. {
  29. var $db;
  30. var $error;
  31. /**
  32. * Constructor
  33. *
  34. * @param DoliDB $db Database handler
  35. */
  36. function __construct($db = '')
  37. {
  38. $this->db = $db;
  39. return 1;
  40. }
  41. /**
  42. * Return list of labels (translated) of third parties type
  43. *
  44. * @param int $mode 0=Return id+label, 1=Return code+label
  45. * @param string $filter Add a SQL filter to select
  46. * @return array Array of types
  47. */
  48. function typent_array($mode=0, $filter='')
  49. {
  50. return false;
  51. /*
  52. global $langs;
  53. $effs = array();
  54. $sql = "SELECT id, code, libelle";
  55. $sql.= " FROM ".MAIN_DB_PREFIX."c_typent";
  56. $sql.= " WHERE active = 1";
  57. if ($filter) $sql.=" ".$filter;
  58. $sql.= " ORDER by id";
  59. dol_syslog(get_class($this).'::typent_array sql='.$sql,LOG_DEBUG);
  60. $resql=$this->db->query($sql);
  61. if ($resql)
  62. {
  63. $num = $this->db->num_rows($resql);
  64. $i = 0;
  65. while ($i < $num)
  66. {
  67. $objp = $this->db->fetch_object($resql);
  68. if (! $mode) $key=$objp->id;
  69. else $key=$objp->code;
  70. if ($langs->trans($objp->code) != $objp->code) $effs[$key] = $langs->trans($objp->code);
  71. else $effs[$key] = $objp->libelle;
  72. if ($effs[$key]=='-') $effs[$key]='';
  73. $i++;
  74. }
  75. $this->db->free($resql);
  76. }
  77. return $effs;
  78. */
  79. }
  80. /**
  81. * Retourne la liste deroulante des regions actives dont le pays est actif
  82. * La cle de la liste est le code (il peut y avoir plusieurs entree pour
  83. * un code donnee mais dans ce cas, le champ pays et lang differe).
  84. * Ainsi les liens avec les regions se font sur une region independemment de son nom.
  85. *
  86. * @param string $selected Preselected value
  87. * @param string $htmlname Name of HTML select field
  88. * @return void
  89. */
  90. function select_region($selected='',$htmlname='region_id')
  91. {
  92. global $conf,$langs;
  93. $langs->load("dict");
  94. $sql = "SELECT r.rowid, r.code_region as code, r.nom as libelle, r.active, p.code as country_code, p.libelle as country FROM ".MAIN_DB_PREFIX."c_regions as r, ".MAIN_DB_PREFIX."c_pays as p";
  95. $sql.= " WHERE r.fk_pays=p.rowid AND r.active = 1 and p.active = 1";
  96. $sql.= " ORDER BY p.code, p.libelle ASC";
  97. dol_syslog(get_class($this)."::select_region sql=".$sql);
  98. $resql=$this->db->query($sql);
  99. if ($resql)
  100. {
  101. print '<select class="flat" name="'.$htmlname.'">';
  102. $num = $this->db->num_rows($resql);
  103. $i = 0;
  104. if ($num)
  105. {
  106. $pays='';
  107. while ($i < $num)
  108. {
  109. $obj = $this->db->fetch_object($resql);
  110. if ($obj->code == 0) {
  111. print '<option value="0">&nbsp;</option>';
  112. }
  113. else {
  114. if ($pays == '' || $pays != $obj->country)
  115. {
  116. // Show break
  117. $key=$langs->trans("Country".strtoupper($obj->country_code));
  118. $valuetoshow=($key != "Country".strtoupper($obj->country_code))?$obj->country_code." - ".$key:$obj->country;
  119. print '<option value="-1" disabled="disabled">----- '.$valuetoshow." -----</option>\n";
  120. $pays=$obj->country;
  121. }
  122. if ($selected > 0 && $selected == $obj->code)
  123. {
  124. print '<option value="'.$obj->code.'" selected="selected">'.$obj->libelle.'</option>';
  125. }
  126. else
  127. {
  128. print '<option value="'.$obj->code.'">'.$obj->libelle.'</option>';
  129. }
  130. }
  131. $i++;
  132. }
  133. }
  134. print '</select>';
  135. }
  136. else
  137. {
  138. dol_print_error($this->db);
  139. }
  140. }
  141. /**
  142. * Return list of third parties
  143. *
  144. * @param Object $object Object we try to find contacts
  145. * @param string $var_id Name of id field
  146. * @param string $selected Pre-selected third party
  147. * @param string $htmlname Name of HTML form
  148. * @param array $limitto Disable answers that are not id in this array list
  149. * @param int $forceid This is to force antoher object id than object->id
  150. * @return void
  151. * TODO obsolete ?
  152. * cette fonction doit utiliser du javascript quoi qu'il en soit !
  153. * autant utiliser le système combobox sans rechargement de page non ?
  154. */
  155. function selectCompaniesForNewContact($object, $var_id, $selected='', $htmlname='newcompany', $limitto='', $forceid=0)
  156. {
  157. global $conf, $langs;
  158. // On recherche les societes
  159. $sql = "SELECT s.rowid, s.nom FROM";
  160. $sql.= " ".MAIN_DB_PREFIX."societe as s";
  161. $sql.= " WHERE s.entity IN (".getEntity('societe', 1).")";
  162. if ($selected && $conf->use_javascript_ajax && ! empty($conf->global->COMPANY_USE_SEARCH_TO_SELECT)) $sql.= " AND rowid = ".$selected;
  163. else
  164. {
  165. // For ajax search we limit here. For combo list, we limit later
  166. if ($conf->use_javascript_ajax && $conf->global->COMPANY_USE_SEARCH_TO_SELECT
  167. && is_array($limitto) && count($limitto))
  168. {
  169. $sql.= " AND rowid IN (".join(',',$limitto).")";
  170. }
  171. }
  172. $sql.= " ORDER BY nom ASC";
  173. $resql = $this->db->query($sql);
  174. if ($resql)
  175. {
  176. if ($conf->use_javascript_ajax && ! empty($conf->global->COMPANY_USE_SEARCH_TO_SELECT))
  177. {
  178. $minLength = (is_numeric($conf->global->COMPANY_USE_SEARCH_TO_SELECT)?$conf->global->COMPANY_USE_SEARCH_TO_SELECT:2);
  179. $socid=0;
  180. if ($selected)
  181. {
  182. $obj = $this->db->fetch_object($resql);
  183. $socid = $obj->rowid?$obj->rowid:'';
  184. }
  185. // We call a page after a small delay when a new input has been selected
  186. $javaScript = "window.location=\'".$_SERVER['PHP_SELF']."?".$var_id."=".($forceid>0?$forceid:$object->id)."&amp;".$htmlname."=\' + document.getElementById(\'".$htmlname."\').value;";
  187. $htmloption = 'onChange="ac_delay(\''.$javaScript.'\',\'500\');"'; // When we select with mouse
  188. $htmloption.= 'onKeyUp="if (event.keyCode== 13) { ac_delay(\''.$javaScript.'\',\'500\'); }"'; // When we select with keyboard
  189. print "\n".'<!-- Input text for third party with Ajax.Autocompleter (selectCompaniesForNewContact) -->'."\n";
  190. print '<table class="nobordernopadding"><tr class="nobordernopadding">';
  191. print '<td class="nobordernopadding">';
  192. if ($obj->rowid == 0)
  193. {
  194. //$langs->load("companies");
  195. //print '<input type="text" size="30" id="'.$htmlname.'_label" name="'.$htmlname.'" value="'.$langs->trans("SelectCompany").'" '.$htmloption.' />';
  196. print '<input type="text" size="30" id="search_'.$htmlname.'" name="search_'.$htmlname.'" value="" '.$htmloption.' />';
  197. }
  198. else
  199. {
  200. print '<input type="text" size="30" id="search_'.$htmlname.'" name="search_'.$htmlname.'" value="'.$obj->nom.'" '.$htmloption.' />';
  201. }
  202. print ajax_autocompleter(($socid?$socid:-1),$htmlname,DOL_URL_ROOT.'/societe/ajaxcompanies.php','',$minLength);
  203. print '</td>';
  204. print '</tr>';
  205. print '</table>';
  206. print "\n";
  207. return $socid;
  208. }
  209. else
  210. {
  211. $javaScript = "window.location='".$_SERVER['PHP_SELF']."?".$var_id."=".($forceid>0?$forceid:$object->id)."&amp;".$htmlname."=' + form.".$htmlname.".options[form.".$htmlname.".selectedIndex].value;";
  212. print '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'" onChange="'.$javaScript.'">';
  213. $num = $this->db->num_rows($resql);
  214. $i = 0;
  215. if ($num)
  216. {
  217. while ($i < $num)
  218. {
  219. $obj = $this->db->fetch_object($resql);
  220. if ($i == 0) $firstCompany = $obj->rowid;
  221. $disabled=0;
  222. if (is_array($limitto) && count($limitto) && ! in_array($obj->rowid,$limitto)) $disabled=1;
  223. if ($selected > 0 && $selected == $obj->rowid)
  224. {
  225. print '<option value="'.$obj->rowid.'"';
  226. if ($disabled) print ' disabled="disabled"';
  227. print ' selected="selected">'.dol_trunc($obj->nom,24).'</option>';
  228. $firstCompany = $obj->rowid;
  229. }
  230. else
  231. {
  232. print '<option value="'.$obj->rowid.'"';
  233. if ($disabled) print ' disabled="disabled"';
  234. print '>'.dol_trunc($obj->nom,24).'</option>';
  235. }
  236. $i ++;
  237. }
  238. }
  239. print "</select>\n";
  240. return $firstCompany;
  241. }
  242. }
  243. else
  244. {
  245. dol_print_error($this->db);
  246. }
  247. }
  248. /**
  249. * Return a select list with types of contacts
  250. *
  251. * @param Object $object Object to use to find type of contact
  252. * @param string $selected Default selected value
  253. * @param string $htmlname HTML select name
  254. * @param string $source Source ('internal' or 'external')
  255. * @param string $order Sort criteria
  256. * @param int $showempty 1=Add en empty line
  257. * @return void
  258. */
  259. function selectTypeContact($object, $selected, $htmlname = 'type', $source='internal', $order='code', $showempty=0)
  260. {
  261. $lesTypes = $object->liste_type_contact($source, $order);
  262. print '<select class="flat" name="'.$htmlname.'" id="'.$htmlname.'">';
  263. if ($showempty) print '<option value="0"></option>';
  264. foreach($lesTypes as $key=>$value)
  265. {
  266. print '<option value="'.$key.'"';
  267. if ($key == $selected)
  268. print ' selected';
  269. print '>'.$value.'</option>';
  270. }
  271. print "</select>\n";
  272. }
  273. /**
  274. * Return a select list with zip codes and their town
  275. *
  276. * @param string $selected Preselected value
  277. * @param string $htmlname HTML select name
  278. * @param string $fields Fields
  279. * @param int $fieldsize Field size
  280. * @param int $disableautocomplete 1 To disable autocomplete features
  281. * @return void
  282. */
  283. function select_ziptown($selected='', $htmlname='zipcode', $fields='', $fieldsize=0, $disableautocomplete=0)
  284. {
  285. global $conf;
  286. include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php';
  287. $size='';
  288. if (!empty($fieldsize))
  289. $size='size="'.$fieldsize.'"';
  290. $out = '<input id="'.$htmlname.'" type="text" name="'.$htmlname.'" '.$size.' value="'.$selected.'" class="input-text" >'."\n";
  291. $out.= ajax_multiautocompleter($htmlname, $fields, 'core/ajax/ziptown.php')."\n";
  292. return $out;
  293. }
  294. /**
  295. * Return HTML string to use as input of professional id into a HTML page (siren, siret, etc...)
  296. *
  297. * @param int $idprof 1,2,3,4 (Example: 1=siren,2=siret,3=naf,4=rcs/rm)
  298. * @param string $htmlname Name of HTML select
  299. * @param string $preselected Default value to show
  300. * @param string $country_code FR, IT, ...
  301. * @return string HTML string with prof id
  302. */
  303. function get_input_id_prof($idprof,$htmlname,$preselected,$country_code)
  304. {
  305. global $conf,$langs;
  306. $formlength=0;
  307. if (empty($conf->global->MAIN_DISABLEPROFIDRULES)) {
  308. if ($country_code == 'FR')
  309. {
  310. if (isset($idprof)) {
  311. if ($idprof==1) $formlength=9;
  312. else if ($idprof==2) $formlength=14;
  313. else if ($idprof==3) $formlength=5; // 4 chiffres et 1 lettre depuis janvier
  314. else if ($idprof==4) $formlength=32; // No maximum as we need to include a town name in this id
  315. }
  316. }
  317. else if ($country_code == 'ES')
  318. {
  319. if ($idprof==1) $formlength=9; //CIF/NIF/NIE 9 digits
  320. if ($idprof==2) $formlength=12; //NASS 12 digits without /
  321. if ($idprof==3) $formlength=5; //CNAE 5 digits
  322. if ($idprof==4) $formlength=32; //depend of college
  323. }
  324. }
  325. $selected=$preselected;
  326. if (! $selected && isset($idprof)) {
  327. if ($idprof==1 && ! empty($this->idprof1)) $selected=$this->idprof1;
  328. else if ($idprof==2 && ! empty($this->idprof2)) $selected=$this->idprof2;
  329. else if ($idprof==3 && ! empty($this->idprof3)) $selected=$this->idprof3;
  330. else if ($idprof==4 && ! empty($this->idprof4)) $selected=$this->idprof4;
  331. }
  332. $maxlength=$formlength;
  333. if (empty($formlength)) { $formlength=24; $maxlength=128; }
  334. $out = '<input type="text" name="'.$htmlname.'" size="'.($formlength+1).'" maxlength="'.$maxlength.'" value="'.$selected.'">';
  335. return $out;
  336. }
  337. }
  338. ?>