accountmodel.php 27 KB

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