accountmodel.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. <?php
  2. /* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2019 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
  5. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  6. * Copyright (C) 2010-2016 Juanjo Menent <jmenent@2byte.es>
  7. * Copyright (C) 2011-2019 Philippe Grand <philippe.grand@atoo-net.com>
  8. * Copyright (C) 2011 Remy Younes <ryounes@gmail.com>
  9. * Copyright (C) 2012-2015 Marcos García <marcosgdf@gmail.com>
  10. * Copyright (C) 2012 Christophe Battarel <christophe.battarel@ltairis.fr>
  11. * Copyright (C) 2011-2016 Alexandre Spangaro <aspangaro@open-dsi.fr>
  12. * Copyright (C) 2015 Ferran Marcet <fmarcet@2byte.es>
  13. * Copyright (C) 2016 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 3 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  27. */
  28. /**
  29. * \file htdocs/accountancy/admin/accountmodel.php
  30. * \ingroup Accountancy (Double entries)
  31. * \brief Page to administer model of chart of accounts
  32. */
  33. require '../../main.inc.php';
  34. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
  35. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
  36. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  37. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  38. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  39. require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
  40. if (! empty($conf->accounting->enabled)) require_once DOL_DOCUMENT_ROOT . '/core/class/html.formaccounting.class.php';
  41. // Load translation files required by the page
  42. $langs->loadLangs(array("errors","admin","companies","resource","holiday","compta","accountancy","hrm"));
  43. $action=GETPOST('action', 'aZ09')?GETPOST('action', 'aZ09'):'view';
  44. $confirm=GETPOST('confirm', 'alpha');
  45. $id=31;
  46. $rowid=GETPOST('rowid', 'alpha');
  47. $code=GETPOST('code', 'alpha');
  48. $acts[0] = "activate";
  49. $acts[1] = "disable";
  50. $actl[0] = img_picto($langs->trans("Disabled"), 'switch_off');
  51. $actl[1] = img_picto($langs->trans("Activated"), 'switch_on');
  52. $listoffset=GETPOST('listoffset', 'alpha');
  53. $listlimit=GETPOST('listlimit', 'int')>0?GETPOST('listlimit', 'int'):1000;
  54. $active = 1;
  55. $sortfield = GETPOST("sortfield", 'aZ09comma');
  56. $sortorder = GETPOST("sortorder", 'aZ09comma');
  57. $page = GETPOST("page", 'int');
  58. if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
  59. $offset = $listlimit * $page ;
  60. $pageprev = $page - 1;
  61. $pagenext = $page + 1;
  62. $search_country_id = GETPOST('search_country_id', 'int');
  63. // Security check
  64. if ($user->societe_id > 0) accessforbidden();
  65. if (! $user->rights->accounting->chartofaccount) accessforbidden();
  66. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  67. $hookmanager->initHooks(array('admin'));
  68. // This page is a generic page to edit dictionaries
  69. // Put here declaration of dictionaries properties
  70. // Name of SQL tables of dictionaries
  71. $tabname=array();
  72. $tabname[31]= MAIN_DB_PREFIX."accounting_system";
  73. // Dictionary labels
  74. $tablib=array();
  75. $tablib[31]= "Pcg_version";
  76. // Requests to extract data
  77. $tabsql=array();
  78. $tabsql[31]= "SELECT s.rowid as rowid, pcg_version, s.label, s.fk_country as country_id, c.code as country_code, c.label as country, s.active FROM ".MAIN_DB_PREFIX."accounting_system as s, ".MAIN_DB_PREFIX."c_country as c WHERE s.fk_country=c.rowid and c.active=1";
  79. // Criteria to sort dictionaries
  80. $tabsqlsort=array();
  81. $tabsqlsort[31]="pcg_version ASC";
  82. // Nom des champs en resultat de select pour affichage du dictionnaire
  83. $tabfield=array();
  84. $tabfield[31]= "pcg_version,label,country_id,country";
  85. // Nom des champs d'edition pour modification d'un enregistrement
  86. $tabfieldvalue=array();
  87. $tabfieldvalue[31]= "pcg_version,label,country";
  88. // Nom des champs dans la table pour insertion d'un enregistrement
  89. $tabfieldinsert=array();
  90. $tabfieldinsert[31]= "pcg_version,label,fk_country";
  91. // Nom du rowid si le champ n'est pas de type autoincrement
  92. // Example: "" if id field is "rowid" and has autoincrement on
  93. // "nameoffield" if id field is not "rowid" or has not autoincrement on
  94. $tabrowid=array();
  95. $tabrowid[31]= "";
  96. // Condition to show dictionary in setup page
  97. $tabcond=array();
  98. $tabcond[31]= ! empty($conf->accounting->enabled);
  99. // List of help for fields
  100. $tabhelp=array();
  101. $tabhelp[31] = array('pcg_version'=>$langs->trans("EnterAnyCode"));
  102. // List of check for fields (NOT USED YET)
  103. $tabfieldcheck=array();
  104. $tabfieldcheck[31] = array();
  105. // Define elementList and sourceList (used for dictionary type of contacts "llx_c_type_contact")
  106. $elementList = array();
  107. $sourceList=array();
  108. /*
  109. * Actions
  110. */
  111. if (GETPOST('button_removefilter', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter_x', 'alpha'))
  112. {
  113. $search_country_id = '';
  114. }
  115. // Actions add or modify an entry into a dictionary
  116. if (GETPOST('actionadd', 'alpha') || GETPOST('actionmodify', 'alpha'))
  117. {
  118. $listfield=explode(',', str_replace(' ', '', $tabfield[$id]));
  119. $listfieldinsert=explode(',', $tabfieldinsert[$id]);
  120. $listfieldmodify=explode(',', $tabfieldinsert[$id]);
  121. $listfieldvalue=explode(',', $tabfieldvalue[$id]);
  122. // Check that all fields are filled
  123. $ok=1;
  124. foreach ($listfield as $f => $value)
  125. {
  126. if ($value == 'country_id' && in_array($tablib[$id], array('Pcg_version'))) continue; // For some pages, country is not mandatory
  127. if ((! isset($_POST[$value]) || $_POST[$value]==''))
  128. {
  129. $ok=0;
  130. $fieldnamekey=$listfield[$f];
  131. // We take translate key of field
  132. if ($fieldnamekey == 'pcg_version') $fieldnamekey='Pcg_version';
  133. if ($fieldnamekey == 'libelle' || ($fieldnamekey == 'label')) $fieldnamekey='Label';
  134. setEventMessages($langs->transnoentities("ErrorFieldRequired", $langs->transnoentities($fieldnamekey)), null, 'errors');
  135. }
  136. }
  137. // Other checks
  138. if ($tabname[$id] == MAIN_DB_PREFIX."c_actioncomm" && isset($_POST["type"]) && in_array($_POST["type"], array('system','systemauto'))) {
  139. $ok=0;
  140. setEventMessages($langs->transnoentities('ErrorReservedTypeSystemSystemAuto'), null, 'errors');
  141. }
  142. if (isset($_POST["pcg_version"]))
  143. {
  144. if ($_POST["pcg_version"]=='0')
  145. {
  146. $ok=0;
  147. setEventMessages($langs->transnoentities('ErrorCodeCantContainZero'), null, 'errors');
  148. }
  149. /*if (!is_numeric($_POST['code'])) // disabled, code may not be in numeric base
  150. {
  151. $ok = 0;
  152. $msg .= $langs->transnoentities('ErrorFieldFormat', $langs->transnoentities('Code')).'<br>';
  153. }*/
  154. }
  155. if (isset($_POST["country"]) && ($_POST["country"]=='0') && ($id != 2))
  156. {
  157. $ok=0;
  158. setEventMessages($langs->transnoentities("ErrorFieldRequired", $langs->transnoentities("Country")), null, 'errors');
  159. }
  160. // Si verif ok et action add, on ajoute la ligne
  161. if ($ok && GETPOST('actionadd', 'alpha'))
  162. {
  163. if ($tabrowid[$id])
  164. {
  165. // Recupere id libre pour insertion
  166. $newid=0;
  167. $sql = "SELECT max(".$tabrowid[$id].") newid from ".$tabname[$id];
  168. $result = $db->query($sql);
  169. if ($result)
  170. {
  171. $obj = $db->fetch_object($result);
  172. $newid=($obj->newid + 1);
  173. } else {
  174. dol_print_error($db);
  175. }
  176. }
  177. // Add new entry
  178. $sql = "INSERT INTO ".$tabname[$id]." (";
  179. // List of fields
  180. if ($tabrowid[$id] && ! in_array($tabrowid[$id], $listfieldinsert))
  181. $sql.= $tabrowid[$id].",";
  182. $sql.= $tabfieldinsert[$id];
  183. $sql.=",active)";
  184. $sql.= " VALUES(";
  185. // List of values
  186. if ($tabrowid[$id] && ! in_array($tabrowid[$id], $listfieldinsert))
  187. $sql.= $newid.",";
  188. $i=0;
  189. foreach ($listfieldinsert as $f => $value)
  190. {
  191. if ($value == 'price' || preg_match('/^amount/i', $value) || $value == 'taux') {
  192. $_POST[$listfieldvalue[$i]] = price2num($_POST[$listfieldvalue[$i]], 'MU');
  193. }
  194. elseif ($value == 'entity') {
  195. $_POST[$listfieldvalue[$i]] = $conf->entity;
  196. }
  197. if ($i) $sql.=",";
  198. if ($_POST[$listfieldvalue[$i]] == '') $sql.="null";
  199. else $sql.="'".$db->escape($_POST[$listfieldvalue[$i]])."'";
  200. $i++;
  201. }
  202. $sql.=",1)";
  203. dol_syslog("actionadd", LOG_DEBUG);
  204. $result = $db->query($sql);
  205. if ($result) // Add is ok
  206. {
  207. setEventMessages($langs->transnoentities("RecordSaved"), null, 'mesgs');
  208. $_POST=array('id'=>$id); // Clean $_POST array, we keep only
  209. }
  210. else
  211. {
  212. if ($db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
  213. setEventMessages($langs->transnoentities("ErrorRecordAlreadyExists"), null, 'errors');
  214. }
  215. else {
  216. dol_print_error($db);
  217. }
  218. }
  219. }
  220. // Si verif ok et action modify, on modifie la ligne
  221. if ($ok && GETPOST('actionmodify', 'alpha'))
  222. {
  223. if ($tabrowid[$id]) { $rowidcol=$tabrowid[$id]; }
  224. else { $rowidcol="rowid"; }
  225. // Modify entry
  226. $sql = "UPDATE ".$tabname[$id]." SET ";
  227. // Modifie valeur des champs
  228. if ($tabrowid[$id] && ! in_array($tabrowid[$id], $listfieldmodify))
  229. {
  230. $sql.= $tabrowid[$id]."=";
  231. $sql.= "'".$db->escape($rowid)."', ";
  232. }
  233. $i = 0;
  234. foreach ($listfieldmodify as $field)
  235. {
  236. if ($field == 'price' || preg_match('/^amount/i', $field) || $field == 'taux') {
  237. $_POST[$listfieldvalue[$i]] = price2num($_POST[$listfieldvalue[$i]], 'MU');
  238. }
  239. elseif ($field == 'entity') {
  240. $_POST[$listfieldvalue[$i]] = $conf->entity;
  241. }
  242. if ($i) $sql.=",";
  243. $sql.= $field."=";
  244. if ($_POST[$listfieldvalue[$i]] == '') $sql.="null";
  245. else $sql.="'".$db->escape($_POST[$listfieldvalue[$i]])."'";
  246. $i++;
  247. }
  248. $sql.= " WHERE ".$rowidcol." = '".$rowid."'";
  249. dol_syslog("actionmodify", LOG_DEBUG);
  250. //print $sql;
  251. $resql = $db->query($sql);
  252. if (! $resql)
  253. {
  254. setEventMessages($db->error(), null, 'errors');
  255. }
  256. }
  257. //$_GET["id"]=GETPOST('id', 'int'); // Force affichage dictionnaire en cours d'edition
  258. }
  259. if (GETPOST('actioncancel', 'alpha'))
  260. {
  261. //$_GET["id"]=GETPOST('id', 'int'); // Force affichage dictionnaire en cours d'edition
  262. }
  263. if ($action == 'confirm_delete' && $confirm == 'yes') // delete
  264. {
  265. if ($tabrowid[$id]) { $rowidcol=$tabrowid[$id]; }
  266. else { $rowidcol="rowid"; }
  267. $sql = "DELETE from ".$tabname[$id]." WHERE ".$rowidcol."='".$rowid."'";
  268. dol_syslog("delete", LOG_DEBUG);
  269. $result = $db->query($sql);
  270. if (! $result)
  271. {
  272. if ($db->errno() == 'DB_ERROR_CHILD_EXISTS')
  273. {
  274. setEventMessages($langs->transnoentities("ErrorRecordIsUsedByChild"), null, 'errors');
  275. }
  276. else
  277. {
  278. dol_print_error($db);
  279. }
  280. }
  281. }
  282. // activate
  283. if ($action == $acts[0])
  284. {
  285. if ($tabrowid[$id]) { $rowidcol=$tabrowid[$id]; }
  286. else { $rowidcol="rowid"; }
  287. if ($rowid) {
  288. $sql = "UPDATE ".$tabname[$id]." SET active = 1 WHERE ".$rowidcol."='".$rowid."'";
  289. }
  290. elseif ($code) {
  291. $sql = "UPDATE ".$tabname[$id]." SET active = 1 WHERE code='".$code."'";
  292. }
  293. $result = $db->query($sql);
  294. if (!$result)
  295. {
  296. dol_print_error($db);
  297. }
  298. }
  299. // disable
  300. if ($action == $acts[1])
  301. {
  302. if ($tabrowid[$id]) { $rowidcol=$tabrowid[$id]; }
  303. else { $rowidcol="rowid"; }
  304. if ($rowid) {
  305. $sql = "UPDATE ".$tabname[$id]." SET active = 0 WHERE ".$rowidcol."='".$rowid."'";
  306. }
  307. elseif ($code) {
  308. $sql = "UPDATE ".$tabname[$id]." SET active = 0 WHERE code='".$code."'";
  309. }
  310. $result = $db->query($sql);
  311. if (!$result)
  312. {
  313. dol_print_error($db);
  314. }
  315. }
  316. // favorite
  317. if ($action == 'activate_favorite')
  318. {
  319. if ($tabrowid[$id]) { $rowidcol=$tabrowid[$id]; }
  320. else { $rowidcol="rowid"; }
  321. if ($rowid) {
  322. $sql = "UPDATE ".$tabname[$id]." SET favorite = 1 WHERE ".$rowidcol."='".$rowid."'";
  323. }
  324. elseif ($code) {
  325. $sql = "UPDATE ".$tabname[$id]." SET favorite = 1 WHERE code='".$code."'";
  326. }
  327. $result = $db->query($sql);
  328. if (!$result)
  329. {
  330. dol_print_error($db);
  331. }
  332. }
  333. // disable favorite
  334. if ($action == 'disable_favorite')
  335. {
  336. if ($tabrowid[$id]) { $rowidcol=$tabrowid[$id]; }
  337. else { $rowidcol="rowid"; }
  338. if ($rowid) {
  339. $sql = "UPDATE ".$tabname[$id]." SET favorite = 0 WHERE ".$rowidcol."='".$rowid."'";
  340. }
  341. elseif ($code) {
  342. $sql = "UPDATE ".$tabname[$id]." SET favorite = 0 WHERE code='".$code."'";
  343. }
  344. $result = $db->query($sql);
  345. if (!$result)
  346. {
  347. dol_print_error($db);
  348. }
  349. }
  350. /*
  351. * View
  352. */
  353. $form = new Form($db);
  354. $formadmin=new FormAdmin($db);
  355. llxHeader();
  356. $titre=$langs->trans($tablib[$id]);
  357. $linkback='';
  358. print load_fiche_titre($titre, $linkback, 'title_accountancy');
  359. // Confirmation de la suppression de la ligne
  360. if ($action == 'delete')
  361. {
  362. print $form->formconfirm($_SERVER["PHP_SELF"].'?'.($page?'page='.urlencode($page).'&':'').'sortfield='.urlencode($sortfield).'&sortorder='.urlencode($sortorder).'&rowid='.urlencode($rowid).'&code='.urlencode($code).'&id='.urlencode($id), $langs->trans('DeleteLine'), $langs->trans('ConfirmDeleteLine'), 'confirm_delete', '', 0, 1);
  363. }
  364. //var_dump($elementList);
  365. /*
  366. * Show a dictionary
  367. */
  368. if ($id)
  369. {
  370. // Complete requete recherche valeurs avec critere de tri
  371. $sql=$tabsql[$id];
  372. if ($search_country_id > 0)
  373. {
  374. if (preg_match('/ WHERE /', $sql)) $sql.= " AND ";
  375. else $sql.=" WHERE ";
  376. $sql.= " c.rowid = ".$search_country_id;
  377. }
  378. // If sort order is "country", we use country_code instead
  379. if ($sortfield == 'country') $sortfield='country_code';
  380. $sql.=$db->order($sortfield, $sortorder);
  381. $sql.=$db->plimit($listlimit+1, $offset);
  382. //print $sql;
  383. $fieldlist=explode(',', $tabfield[$id]);
  384. print '<form action="'.$_SERVER['PHP_SELF'].'?id='.$id.'" method="POST">';
  385. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  386. print '<div class="div-table-responsive">';
  387. print '<table class="noborder" width="100%">';
  388. // Form to add a new line
  389. if ($tabname[$id])
  390. {
  391. $alabelisused=0;
  392. $var=false;
  393. $fieldlist=explode(',', $tabfield[$id]);
  394. // Line for title
  395. print '<tr class="liste_titre">';
  396. foreach ($fieldlist as $field => $value)
  397. {
  398. // Determine le nom du champ par rapport aux noms possibles
  399. // dans les dictionnaires de donnees
  400. $valuetoshow=ucfirst($fieldlist[$field]); // Par defaut
  401. $valuetoshow=$langs->trans($valuetoshow); // try to translate
  402. $class="left";
  403. if ($fieldlist[$field]=='code') { $valuetoshow=$langs->trans("Code"); }
  404. if ($fieldlist[$field]=='libelle' || $fieldlist[$field]=='label')
  405. {
  406. $valuetoshow=$langs->trans("Label");
  407. }
  408. if ($fieldlist[$field]=='country') {
  409. if (in_array('region_id', $fieldlist)) { print '<td>&nbsp;</td>'; continue; } // For region page, we do not show the country input
  410. $valuetoshow=$langs->trans("Country");
  411. }
  412. if ($fieldlist[$field]=='country_id') { $valuetoshow=''; }
  413. if ($fieldlist[$field]=='pcg_version' || $fieldlist[$field]=='fk_pcg_version') { $valuetoshow=$langs->trans("Pcg_version"); }
  414. if ($valuetoshow != '') {
  415. print '<td class="'.$class.'">';
  416. if (! empty($tabhelp[$id][$value]) && preg_match('/^http(s*):/i', $tabhelp[$id][$value])) {
  417. print '<a href="'.$tabhelp[$id][$value].'" target="_blank">'.$valuetoshow.' '.img_help(1, $valuetoshow).'</a>';
  418. } elseif (! empty($tabhelp[$id][$value])) {
  419. print $form->textwithpicto($valuetoshow, $tabhelp[$id][$value]);
  420. } else {
  421. print $valuetoshow;
  422. }
  423. print '</td>';
  424. }
  425. if ($fieldlist[$field]=='libelle' || $fieldlist[$field]=='label') $alabelisused=1;
  426. }
  427. print '<td>';
  428. print '<input type="hidden" name="id" value="'.$id.'">';
  429. print '</td>';
  430. print '<td style="min-width: 26px;"></td>';
  431. print '<td style="min-width: 26px;"></td>';
  432. print '</tr>';
  433. // Line to enter new values
  434. print "<tr ".$bcnd[$var].">";
  435. $obj = new stdClass();
  436. // If data was already input, we define them in obj to populate input fields.
  437. if (GETPOST('actionadd', 'alpha'))
  438. {
  439. foreach ($fieldlist as $key=>$val)
  440. {
  441. if (GETPOST($val))
  442. $obj->$val=GETPOST($val);
  443. }
  444. }
  445. $tmpaction = 'create';
  446. $parameters=array('fieldlist'=>$fieldlist, 'tabname'=>$tabname[$id]);
  447. $reshook=$hookmanager->executeHooks('createDictionaryFieldlist', $parameters, $obj, $tmpaction); // Note that $action and $object may have been modified by some hooks
  448. $error=$hookmanager->error; $errors=$hookmanager->errors;
  449. if (empty($reshook))
  450. {
  451. fieldListAccountModel($fieldlist, $obj, $tabname[$id], 'add');
  452. }
  453. print '<td colspan="3" class="right">';
  454. print '<input type="submit" class="button" name="actionadd" value="'.$langs->trans("Add").'">';
  455. print '</td>';
  456. print "</tr>";
  457. $colspan=count($fieldlist)+3;
  458. print '<tr><td colspan="'.$colspan.'">&nbsp;</td></tr>'; // Keep &nbsp; to have a line with enough height
  459. }
  460. // List of available values in database
  461. dol_syslog("htdocs/admin/dict", LOG_DEBUG);
  462. $resql=$db->query($sql);
  463. if ($resql)
  464. {
  465. $num = $db->num_rows($resql);
  466. $i = 0;
  467. $param = '&id='.$id;
  468. if ($search_country_id > 0) $param.= '&search_country_id='.$search_country_id;
  469. $paramwithsearch = $param;
  470. if ($sortorder) $paramwithsearch.= '&sortorder='.$sortorder;
  471. if ($sortfield) $paramwithsearch.= '&sortfield='.$sortfield;
  472. // There is several pages
  473. if ($num > $listlimit)
  474. {
  475. print '<tr class="none"><td class="right" colspan="'.(3+count($fieldlist)).'">';
  476. print_fleche_navigation($page, $_SERVER["PHP_SELF"], $paramwithsearch, ($num > $listlimit), '<li class="pagination"><span>'.$langs->trans("Page").' '.($page+1).'</span></li>');
  477. print '</td></tr>';
  478. }
  479. // Title line with search boxes
  480. print '<tr class="liste_titre liste_titre_add">';
  481. foreach ($fieldlist as $field => $value)
  482. {
  483. $showfield=1; // By defaut
  484. if ($fieldlist[$field]=='region_id' || $fieldlist[$field]=='country_id') { $showfield=0; }
  485. if ($showfield)
  486. {
  487. if ($value == 'country')
  488. {
  489. print '<td class="liste_titre">';
  490. print $form->select_country($search_country_id, 'search_country_id', '', 28, 'maxwidth200 maxwidthonsmartphone');
  491. print '</td>';
  492. }
  493. else
  494. {
  495. print '<td class="liste_titre"></td>';
  496. }
  497. }
  498. }
  499. print '<td class="liste_titre"></td>';
  500. print '<td class="liste_titre right" colspan="2">';
  501. $searchpicto=$form->showFilterAndCheckAddButtons(0);
  502. print $searchpicto;
  503. print '</td>';
  504. print '</tr>';
  505. // Title of lines
  506. print '<tr class="liste_titre">';
  507. foreach ($fieldlist as $field => $value)
  508. {
  509. // Determine le nom du champ par rapport aux noms possibles
  510. // dans les dictionnaires de donnees
  511. $showfield=1; // By defaut
  512. $class="left";
  513. $sortable=1;
  514. $valuetoshow='';
  515. /*
  516. $tmparray=getLabelOfField($fieldlist[$field]);
  517. $showfield=$tmp['showfield'];
  518. $valuetoshow=$tmp['valuetoshow'];
  519. $align=$tmp['align'];
  520. $sortable=$tmp['sortable'];
  521. */
  522. $valuetoshow=ucfirst($fieldlist[$field]); // By defaut
  523. $valuetoshow=$langs->trans($valuetoshow); // try to translate
  524. if ($fieldlist[$field]=='code') {
  525. $valuetoshow=$langs->trans("Code");
  526. }
  527. if ($fieldlist[$field]=='libelle' || $fieldlist[$field]=='label') {
  528. $valuetoshow=$langs->trans("Label");
  529. }
  530. if ($fieldlist[$field]=='country') {
  531. $valuetoshow=$langs->trans("Country");
  532. }
  533. if ($fieldlist[$field]=='country_id') {
  534. $showfield=0;
  535. }
  536. if ($fieldlist[$field]=='fk_pcg_version') {
  537. $valuetoshow=$langs->trans("Pcg_version");
  538. }
  539. // Affiche nom du champ
  540. if ($showfield) {
  541. print getTitleFieldOfList($valuetoshow, 0, $_SERVER["PHP_SELF"], ($sortable?$fieldlist[$field]:''), ($page?'page='.$page.'&':''), $param, "", $sortfield, $sortorder, $class.' ');
  542. }
  543. }
  544. print getTitleFieldOfList($langs->trans("Status"), 0, $_SERVER["PHP_SELF"], "active", ($page?'page='.$page.'&':''), $param, '', $sortfield, $sortorder, 'center ');
  545. print getTitleFieldOfList('');
  546. print getTitleFieldOfList('');
  547. print '</tr>';
  548. if ($num)
  549. {
  550. // Lines with values
  551. while ($i < $num)
  552. {
  553. $obj = $db->fetch_object($resql);
  554. //print_r($obj);
  555. print '<tr class="oddeven" id="rowid-'.$obj->rowid.'">';
  556. if ($action == 'edit' && ($rowid == (! empty($obj->rowid)?$obj->rowid:$obj->code)))
  557. {
  558. print '<form action="'.$_SERVER['PHP_SELF'].'?id='.$id.'" method="POST">';
  559. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  560. print '<input type="hidden" name="page" value="'.$page.'">';
  561. print '<input type="hidden" name="rowid" value="'.$rowid.'">';
  562. $tmpaction='edit';
  563. $parameters=array('fieldlist'=>$fieldlist, 'tabname'=>$tabname[$id]);
  564. $reshook=$hookmanager->executeHooks('editDictionaryFieldlist', $parameters, $obj, $tmpaction); // Note that $action and $object may have been modified by some hooks
  565. $error=$hookmanager->error; $errors=$hookmanager->errors;
  566. if (empty($reshook)) fieldListAccountModel($fieldlist, $obj, $tabname[$id], 'edit');
  567. print '<td colspan="3" class="right"><a name="'.(! empty($obj->rowid)?$obj->rowid:$obj->code).'">&nbsp;</a><input type="submit" class="button" name="actionmodify" value="'.$langs->trans("Modify").'">';
  568. print '&nbsp;<input type="submit" class="button" name="actioncancel" value="'.$langs->trans("Cancel").'"></td>';
  569. }
  570. else
  571. {
  572. $tmpaction = 'view';
  573. $parameters=array('var'=>$var, 'fieldlist'=>$fieldlist, 'tabname'=>$tabname[$id]);
  574. $reshook=$hookmanager->executeHooks('viewDictionaryFieldlist', $parameters, $obj, $tmpaction); // Note that $action and $object may have been modified by some hooks
  575. $error=$hookmanager->error; $errors=$hookmanager->errors;
  576. if (empty($reshook))
  577. {
  578. foreach ($fieldlist as $field => $value)
  579. {
  580. $showfield=1;
  581. $class="left";
  582. $valuetoshow=$obj->{$fieldlist[$field]};
  583. if ($value == 'type_template')
  584. {
  585. $valuetoshow = isset($elementList[$valuetoshow])?$elementList[$valuetoshow]:$valuetoshow;
  586. }
  587. if ($value == 'element')
  588. {
  589. $valuetoshow = isset($elementList[$valuetoshow])?$elementList[$valuetoshow]:$valuetoshow;
  590. }
  591. elseif ($value == 'source')
  592. {
  593. $valuetoshow = isset($sourceList[$valuetoshow])?$sourceList[$valuetoshow]:$valuetoshow;
  594. }
  595. elseif ($valuetoshow=='all') {
  596. $valuetoshow=$langs->trans('All');
  597. }
  598. elseif ($fieldlist[$field]=='country') {
  599. if (empty($obj->country_code))
  600. {
  601. $valuetoshow='-';
  602. }
  603. else
  604. {
  605. $key=$langs->trans("Country".strtoupper($obj->country_code));
  606. $valuetoshow=($key != "Country".strtoupper($obj->country_code)?$obj->country_code." - ".$key:$obj->country);
  607. }
  608. }
  609. elseif ($fieldlist[$field]=='country_id') {
  610. $showfield=0;
  611. }
  612. $class='tddict';
  613. if ($fieldlist[$field] == 'tracking') $class.=' tdoverflowauto';
  614. // Show value for field
  615. if ($showfield) print '<!-- '.$fieldlist[$field].' --><td class="'.$class.'">'.$valuetoshow.'</td>';
  616. }
  617. }
  618. // Can an entry be erased or disabled ?
  619. $iserasable=1;$canbedisabled=1;$canbemodified=1; // true by default
  620. $url = $_SERVER["PHP_SELF"].'?'.($page?'page='.$page.'&':'').'sortfield='.$sortfield.'&sortorder='.$sortorder.'&rowid='.(! empty($obj->rowid)?$obj->rowid:(! empty($obj->code)?$obj->code:'')).'&code='.(! empty($obj->code)?urlencode($obj->code):'');
  621. if ($param) $url .= '&'.$param;
  622. $url.='&';
  623. // Active
  624. print '<td class="center nowrap">';
  625. if ($canbedisabled) print '<a href="'.$url.'action='.$acts[$obj->active].'">'.$actl[$obj->active].'</a>';
  626. else print $langs->trans("AlwaysActive");
  627. print "</td>";
  628. // Modify link
  629. if ($canbemodified) print '<td class="center"><a class="reposition" href="'.$url.'action=edit">'.img_edit().'</a></td>';
  630. else print '<td>&nbsp;</td>';
  631. // Delete link
  632. if ($iserasable) print '<td class="center"><a href="'.$url.'action=delete">'.img_delete().'</a></td>';
  633. else print '<td>&nbsp;</td>';
  634. print "</tr>\n";
  635. }
  636. $i++;
  637. }
  638. }
  639. }
  640. else {
  641. dol_print_error($db);
  642. }
  643. print '</table>';
  644. print '</div>';
  645. print '</form>';
  646. }
  647. print '<br>';
  648. // End of page
  649. llxFooter();
  650. $db->close();
  651. /**
  652. * Show fields in insert/edit mode
  653. *
  654. * @param array $fieldlist Array of fields
  655. * @param Object $obj If we show a particular record, obj is filled with record fields
  656. * @param string $tabname Name of SQL table
  657. * @param string $context 'add'=Output field for the "add form", 'edit'=Output field for the "edit form", 'hide'=Output field for the "add form" but we dont want it to be rendered
  658. * @return void
  659. */
  660. function fieldListAccountModel($fieldlist, $obj = '', $tabname = '', $context = '')
  661. {
  662. global $conf,$langs,$db;
  663. global $form;
  664. global $region_id;
  665. global $elementList,$sourceList;
  666. $formadmin = new FormAdmin($db);
  667. $formcompany = new FormCompany($db);
  668. $formaccounting = new FormAccounting($db);
  669. foreach ($fieldlist as $field => $value)
  670. {
  671. if ($fieldlist[$field] == 'country')
  672. {
  673. if (in_array('region_id', $fieldlist))
  674. {
  675. print '<td>';
  676. //print join(',',$fieldlist);
  677. print '</td>';
  678. continue;
  679. } // For state page, we do not show the country input (we link to region, not country)
  680. print '<td>';
  681. $fieldname='country';
  682. print $form->select_country((! empty($obj->country_code)?$obj->country_code:(! empty($obj->country)?$obj->country:'')), $fieldname, '', 28, 'maxwidth200 maxwidthonsmartphone');
  683. print '</td>';
  684. }
  685. elseif ($fieldlist[$field] == 'country_id')
  686. {
  687. if (! in_array('country', $fieldlist)) // If there is already a field country, we don't show country_id (avoid duplicate)
  688. {
  689. $country_id = (! empty($obj->{$fieldlist[$field]}) ? $obj->{$fieldlist[$field]} : 0);
  690. print '<td>';
  691. print '<input type="hidden" name="'.$fieldlist[$field].'" value="'.$country_id.'">';
  692. print '</td>';
  693. }
  694. }
  695. elseif ($fieldlist[$field] == 'type_cdr') {
  696. if ($fieldlist[$field] == 'type_cdr') print '<td class="center">';
  697. else print '<td>';
  698. if ($fieldlist[$field] == 'type_cdr') {
  699. print $form->selectarray($fieldlist[$field], array(0=>$langs->trans('None'), 1=>$langs->trans('AtEndOfMonth'), 2=>$langs->trans('CurrentNext')), (! empty($obj->{$fieldlist[$field]})?$obj->{$fieldlist[$field]}:''));
  700. } else {
  701. print $form->selectyesno($fieldlist[$field], (! empty($obj->{$fieldlist[$field]})?$obj->{$fieldlist[$field]}:''), 1);
  702. }
  703. print '</td>';
  704. }
  705. elseif ($fieldlist[$field] == 'code' && isset($obj->{$fieldlist[$field]})) {
  706. print '<td><input type="text" class="flat" value="'.(! empty($obj->{$fieldlist[$field]})?$obj->{$fieldlist[$field]}:'').'" size="10" name="'.$fieldlist[$field].'"></td>';
  707. }
  708. else
  709. {
  710. print '<td>';
  711. $size=''; $class='';
  712. if ($fieldlist[$field]=='code') $size='size="8" ';
  713. if ($fieldlist[$field]=='position') $size='size="4" ';
  714. if ($fieldlist[$field]=='libelle') $size='centpercent';
  715. if ($fieldlist[$field]=='sortorder' || $fieldlist[$field]=='sens' || $fieldlist[$field]=='category_type') $size='size="2" ';
  716. print '<input type="text" '.$size.' class="flat'.($class?' '.$class:'').'" value="'.(isset($obj->{$fieldlist[$field]})?$obj->{$fieldlist[$field]}:'').'" name="'.$fieldlist[$field].'">';
  717. print '</td>';
  718. }
  719. }
  720. }