accountmodel.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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-2024 Alexandre Spangaro <aspangaro@easya.solutions>
  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. // List of help for fields
  106. $tabhelp = array();
  107. $tabhelp[31] = array('pcg_version'=>$langs->trans("EnterAnyCode"));
  108. // Define elementList and sourceList (used for dictionary type of contacts "llx_c_type_contact")
  109. $elementList = array();
  110. $sourceList = array();
  111. /*
  112. * Actions
  113. */
  114. if (GETPOST('button_removefilter', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter_x', 'alpha')) {
  115. $search_country_id = '';
  116. }
  117. // Actions add or modify an entry into a dictionary
  118. if (GETPOST('actionadd', 'alpha') || GETPOST('actionmodify', 'alpha')) {
  119. $listfield = explode(',', str_replace(' ', '', $tabfield[$id]));
  120. $listfieldinsert = explode(',', $tabfieldinsert[$id]);
  121. $listfieldmodify = explode(',', $tabfieldinsert[$id]);
  122. $listfieldvalue = explode(',', $tabfieldvalue[$id]);
  123. // Check that all fields are filled
  124. $ok = 1;
  125. foreach ($listfield as $f => $value) {
  126. if ($value == 'country_id' && in_array($tablib[$id], array('Pcg_version'))) {
  127. continue; // For some pages, country is not mandatory
  128. }
  129. if ((!GETPOSTISSET($value)) || GETPOST($value) == '') {
  130. $ok = 0;
  131. $fieldnamekey = $listfield[$f];
  132. // We take translate key of field
  133. if ($fieldnamekey == 'pcg_version') {
  134. $fieldnamekey = 'Pcg_version';
  135. }
  136. if ($fieldnamekey == 'label') {
  137. $fieldnamekey = 'Label';
  138. }
  139. if ($fieldnamekey == 'country') {
  140. $fieldnamekey = "Country";
  141. }
  142. setEventMessages($langs->transnoentities("ErrorFieldRequired", $langs->transnoentities($fieldnamekey)), null, 'errors');
  143. }
  144. }
  145. // Other checks
  146. if (GETPOSTISSET("pcg_version")) {
  147. if (GETPOST("pcg_version") == '0') {
  148. $ok = 0;
  149. setEventMessages($langs->transnoentities('ErrorCodeCantContainZero'), null, 'errors');
  150. }
  151. }
  152. if (GETPOSTISSET("country") && (GETPOST("country") == '0') && ($id != 2)) {
  153. $ok = 0;
  154. setEventMessages($langs->transnoentities("ErrorFieldRequired", $langs->transnoentities("Country")), null, 'errors');
  155. }
  156. // Si verif ok et action add, on ajoute la ligne
  157. if ($ok && GETPOST('actionadd', 'alpha')) {
  158. if ($tabrowid[$id]) {
  159. // Get free id for insert
  160. $newid = 0;
  161. $sql = "SELECT MAX(".$tabrowid[$id].") newid from ".$tabname[$id];
  162. $result = $db->query($sql);
  163. if ($result) {
  164. $obj = $db->fetch_object($result);
  165. $newid = ($obj->newid + 1);
  166. } else {
  167. dol_print_error($db);
  168. }
  169. }
  170. // Add new entry
  171. $sql = "INSERT INTO ".$tabname[$id]." (";
  172. // List of fields
  173. if ($tabrowid[$id] && !in_array($tabrowid[$id], $listfieldinsert)) {
  174. $sql .= $tabrowid[$id].",";
  175. }
  176. $sql .= $tabfieldinsert[$id];
  177. $sql .= ",active)";
  178. $sql .= " VALUES(";
  179. // List of values
  180. if ($tabrowid[$id] && !in_array($tabrowid[$id], $listfieldinsert)) {
  181. $sql .= $newid.",";
  182. }
  183. $i = 0;
  184. foreach ($listfieldinsert as $f => $value) {
  185. if ($value == 'price' || preg_match('/^amount/i', $value) || $value == 'taux') {
  186. $_POST[$listfieldvalue[$i]] = price2num(GETPOST($listfieldvalue[$i]), 'MU');
  187. } elseif ($value == 'entity') {
  188. $_POST[$listfieldvalue[$i]] = $conf->entity;
  189. }
  190. if ($i) {
  191. $sql .= ",";
  192. }
  193. if (GETPOST($listfieldvalue[$i]) == '') {
  194. $sql .= "null";
  195. } else {
  196. $sql .= "'".$db->escape(GETPOST($listfieldvalue[$i]))."'";
  197. }
  198. $i++;
  199. }
  200. $sql .= ",1)";
  201. dol_syslog("actionadd", LOG_DEBUG);
  202. $result = $db->query($sql);
  203. if ($result) { // Add is ok
  204. setEventMessages($langs->transnoentities("RecordSaved"), null, 'mesgs');
  205. $_POST = array('id'=>$id); // Clean $_POST array, we keep only
  206. } else {
  207. if ($db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
  208. setEventMessages($langs->transnoentities("ErrorRecordAlreadyExists"), null, 'errors');
  209. } else {
  210. dol_print_error($db);
  211. }
  212. }
  213. }
  214. // Si verif ok et action modify, on modifie la ligne
  215. if ($ok && GETPOST('actionmodify', 'alpha')) {
  216. if ($tabrowid[$id]) {
  217. $rowidcol = $tabrowid[$id];
  218. } else {
  219. $rowidcol = "rowid";
  220. }
  221. // Modify entry
  222. $sql = "UPDATE ".$tabname[$id]." SET ";
  223. // Modifie valeur des champs
  224. if ($tabrowid[$id] && !in_array($tabrowid[$id], $listfieldmodify)) {
  225. $sql .= $tabrowid[$id]."=";
  226. $sql .= "'".$db->escape($rowid)."', ";
  227. }
  228. $i = 0;
  229. foreach ($listfieldmodify as $field) {
  230. if ($field == 'price' || preg_match('/^amount/i', $field) || $field == 'taux') {
  231. $_POST[$listfieldvalue[$i]] = price2num(GETPOST($listfieldvalue[$i]), 'MU');
  232. } elseif ($field == 'entity') {
  233. $_POST[$listfieldvalue[$i]] = $conf->entity;
  234. }
  235. if ($i) {
  236. $sql .= ",";
  237. }
  238. $sql .= $field."=";
  239. if (GETPOST($listfieldvalue[$i]) == '') {
  240. $sql .= "null";
  241. } else {
  242. $sql .= "'".$db->escape(GETPOST($listfieldvalue[$i]))."'";
  243. }
  244. $i++;
  245. }
  246. $sql .= " WHERE ".$rowidcol." = ".((int) $rowid);
  247. dol_syslog("actionmodify", LOG_DEBUG);
  248. //print $sql;
  249. $resql = $db->query($sql);
  250. if (!$resql) {
  251. setEventMessages($db->error(), null, 'errors');
  252. }
  253. }
  254. }
  255. if ($action == 'confirm_delete' && $confirm == 'yes') { // delete
  256. if ($tabrowid[$id]) {
  257. $rowidcol = $tabrowid[$id];
  258. } else {
  259. $rowidcol = "rowid";
  260. }
  261. $sql = "DELETE from ".$tabname[$id]." WHERE ".$rowidcol." = ".((int) $rowid);
  262. dol_syslog("delete", LOG_DEBUG);
  263. $result = $db->query($sql);
  264. if (!$result) {
  265. if ($db->errno() == 'DB_ERROR_CHILD_EXISTS') {
  266. setEventMessages($langs->transnoentities("ErrorRecordIsUsedByChild"), null, 'errors');
  267. } else {
  268. dol_print_error($db);
  269. }
  270. }
  271. }
  272. // activate
  273. if ($action == $acts[0]) {
  274. if ($tabrowid[$id]) {
  275. $rowidcol = $tabrowid[$id];
  276. } else {
  277. $rowidcol = "rowid";
  278. }
  279. if ($rowid) {
  280. $sql = "UPDATE ".$tabname[$id]." SET active = 1 WHERE ".$rowidcol." = ".((int) $rowid);
  281. } elseif ($code) {
  282. $sql = "UPDATE ".$tabname[$id]." SET active = 1 WHERE code='".$db->escape($code)."'";
  283. }
  284. $result = $db->query($sql);
  285. if (!$result) {
  286. dol_print_error($db);
  287. }
  288. }
  289. // disable
  290. if ($action == $acts[1]) {
  291. if ($tabrowid[$id]) {
  292. $rowidcol = $tabrowid[$id];
  293. } else {
  294. $rowidcol = "rowid";
  295. }
  296. if ($rowid) {
  297. $sql = "UPDATE ".$tabname[$id]." SET active = 0 WHERE ".$rowidcol." = ".((int) $rowid);
  298. } elseif ($code) {
  299. $sql = "UPDATE ".$tabname[$id]." SET active = 0 WHERE code='".$db->escape($code)."'";
  300. }
  301. $result = $db->query($sql);
  302. if (!$result) {
  303. dol_print_error($db);
  304. }
  305. }
  306. // favorite
  307. if ($action == 'activate_favorite') {
  308. if ($tabrowid[$id]) {
  309. $rowidcol = $tabrowid[$id];
  310. } else {
  311. $rowidcol = "rowid";
  312. }
  313. if ($rowid) {
  314. $sql = "UPDATE ".$tabname[$id]." SET favorite = 1 WHERE ".$rowidcol." = ".((int) $rowid);
  315. } elseif ($code) {
  316. $sql = "UPDATE ".$tabname[$id]." SET favorite = 1 WHERE code='".$db->escape($code)."'";
  317. }
  318. $result = $db->query($sql);
  319. if (!$result) {
  320. dol_print_error($db);
  321. }
  322. }
  323. // disable favorite
  324. if ($action == 'disable_favorite') {
  325. if ($tabrowid[$id]) {
  326. $rowidcol = $tabrowid[$id];
  327. } else {
  328. $rowidcol = "rowid";
  329. }
  330. if ($rowid) {
  331. $sql = "UPDATE ".$tabname[$id]." SET favorite = 0 WHERE ".$rowidcol." = ".((int) $rowid);
  332. } elseif ($code) {
  333. $sql = "UPDATE ".$tabname[$id]." SET favorite = 0 WHERE code='".$db->escape($code)."'";
  334. }
  335. $result = $db->query($sql);
  336. if (!$result) {
  337. dol_print_error($db);
  338. }
  339. }
  340. /*
  341. * View
  342. */
  343. $form = new Form($db);
  344. $formadmin = new FormAdmin($db);
  345. $help_url = 'EN:Module_Double_Entry_Accounting#Setup|FR:Module_Comptabilit&eacute;_en_Partie_Double#Configuration';
  346. llxHeader('', $langs->trans("Pcg_version"), $help_url);
  347. $titre = $langs->trans($tablib[$id]);
  348. $linkback = '';
  349. print load_fiche_titre($titre, $linkback, 'title_accountancy');
  350. // Confirmation de la suppression de la ligne
  351. if ($action == 'delete') {
  352. 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);
  353. }
  354. //var_dump($elementList);
  355. /*
  356. * Show a dictionary
  357. */
  358. if ($id) {
  359. // Complete requete recherche valeurs avec critere de tri
  360. $sql = $tabsql[$id];
  361. if ($search_country_id > 0) {
  362. if (preg_match('/ WHERE /', $sql)) {
  363. $sql .= " AND ";
  364. } else {
  365. $sql .= " WHERE ";
  366. }
  367. $sql .= " c.rowid = ".((int) $search_country_id);
  368. }
  369. // If sort order is "country", we use country_code instead
  370. if ($sortfield == 'country') {
  371. $sortfield = 'country_code';
  372. }
  373. $sql .= $db->order($sortfield, $sortorder);
  374. $sql .= $db->plimit($listlimit + 1, $offset);
  375. //print $sql;
  376. $fieldlist = explode(',', $tabfield[$id]);
  377. print '<form action="'.$_SERVER['PHP_SELF'].'?id='.$id.'" method="POST">';
  378. print '<input type="hidden" name="token" value="'.newToken().'">';
  379. print '<div class="div-table-responsive">';
  380. print '<table class="noborder centpercent">';
  381. // Form to add a new line
  382. if ($tabname[$id]) {
  383. $fieldlist = explode(',', $tabfield[$id]);
  384. // Line for title
  385. print '<tr class="liste_titre">';
  386. foreach ($fieldlist as $field => $value) {
  387. // Determine le nom du champ par rapport aux noms possibles
  388. // dans les dictionnaires de donnees
  389. $valuetoshow = ucfirst($fieldlist[$field]); // Par defaut
  390. $valuetoshow = $langs->trans($valuetoshow); // try to translate
  391. $class = "left";
  392. if ($fieldlist[$field] == 'code') {
  393. $valuetoshow = $langs->trans("Code");
  394. }
  395. if ($fieldlist[$field] == 'label') {
  396. $valuetoshow = $langs->trans("Label");
  397. $class = 'minwidth300';
  398. }
  399. if ($fieldlist[$field] == 'country') {
  400. if (in_array('region_id', $fieldlist)) {
  401. print '<td>&nbsp;</td>';
  402. continue;
  403. } // For region page, we do not show the country input
  404. $valuetoshow = $langs->trans("Country");
  405. }
  406. if ($fieldlist[$field] == 'country_id') {
  407. $valuetoshow = '';
  408. }
  409. if ($fieldlist[$field] == 'pcg_version' || $fieldlist[$field] == 'fk_pcg_version') {
  410. $valuetoshow = $langs->trans("Pcg_version");
  411. }
  412. //var_dump($value);
  413. if ($valuetoshow != '') {
  414. print '<td class="'.$class.'">';
  415. if (!empty($tabhelp[$id][$value]) && preg_match('/^http(s*):/i', $tabhelp[$id][$value])) {
  416. print '<a href="'.$tabhelp[$id][$value].'">'.$valuetoshow.' '.img_help(1, $valuetoshow).'</a>';
  417. } elseif (!empty($tabhelp[$id][$value])) {
  418. print $form->textwithpicto($valuetoshow, $tabhelp[$id][$value]);
  419. } else {
  420. print $valuetoshow;
  421. }
  422. print '</td>';
  423. }
  424. }
  425. print '<td>';
  426. print '<input type="hidden" name="id" value="'.$id.'">';
  427. print '</td>';
  428. print '<td></td>';
  429. print '<td></td>';
  430. print '</tr>';
  431. // Line to enter new values
  432. print '<tr class="oddeven">';
  433. $obj = new stdClass();
  434. // If data was already input, we define them in obj to populate input fields.
  435. if (GETPOST('actionadd', 'alpha')) {
  436. foreach ($fieldlist as $key => $val) {
  437. if (GETPOST($val)) {
  438. $obj->$val = GETPOST($val);
  439. }
  440. }
  441. }
  442. $tmpaction = 'create';
  443. $parameters = array('fieldlist'=>$fieldlist, 'tabname'=>$tabname[$id]);
  444. $reshook = $hookmanager->executeHooks('createDictionaryFieldlist', $parameters, $obj, $tmpaction); // Note that $action and $object may have been modified by some hooks
  445. $error = $hookmanager->error;
  446. $errors = $hookmanager->errors;
  447. if (empty($reshook)) {
  448. fieldListAccountModel($fieldlist, $obj, $tabname[$id], 'add');
  449. }
  450. print '<td colspan="3" class="right">';
  451. print '<input type="submit" class="button button-add" name="actionadd" value="'.$langs->trans("Add").'">';
  452. print '</td>';
  453. print "</tr>";
  454. $colspan = count($fieldlist) + 3;
  455. print '<tr><td colspan="'.$colspan.'">&nbsp;</td></tr>'; // Keep &nbsp; to have a line with enough height
  456. }
  457. // List of available values in database
  458. dol_syslog("htdocs/admin/dict", LOG_DEBUG);
  459. $resql = $db->query($sql);
  460. if ($resql) {
  461. $num = $db->num_rows($resql);
  462. $i = 0;
  463. $param = '&id='.urlencode($id);
  464. if ($search_country_id > 0) {
  465. $param .= '&search_country_id='.urlencode($search_country_id);
  466. }
  467. $paramwithsearch = $param;
  468. if ($sortorder) {
  469. $paramwithsearch .= '&sortorder='.urlencode($sortorder);
  470. }
  471. if ($sortfield) {
  472. $paramwithsearch .= '&sortfield='.urlencode($sortfield);
  473. }
  474. // There is several pages
  475. if ($num > $listlimit) {
  476. print '<tr class="none"><td class="right" colspan="'.(3 + count($fieldlist)).'">';
  477. print_fleche_navigation($page, $_SERVER["PHP_SELF"], $paramwithsearch, ($num > $listlimit), '<li class="pagination"><span>'.$langs->trans("Page").' '.($page + 1).'</span></li>');
  478. print '</td></tr>';
  479. }
  480. // Title line with search boxes
  481. print '<tr class="liste_titre liste_titre_add">';
  482. foreach ($fieldlist as $field => $value) {
  483. $showfield = 1; // By defaut
  484. if ($fieldlist[$field] == 'region_id' || $fieldlist[$field] == 'country_id') {
  485. $showfield = 0;
  486. }
  487. if ($showfield) {
  488. if ($value == 'country') {
  489. print '<td class="liste_titre">';
  490. print $form->select_country($search_country_id, 'search_country_id', '', 28, 'maxwidth200 maxwidthonsmartphone');
  491. print '</td>';
  492. } else {
  493. print '<td class="liste_titre"></td>';
  494. }
  495. }
  496. }
  497. print '<td class="liste_titre"></td>';
  498. print '<td class="liste_titre right" colspan="2">';
  499. $searchpicto = $form->showFilterAndCheckAddButtons(0);
  500. print $searchpicto;
  501. print '</td>';
  502. print '</tr>';
  503. // Title of lines
  504. print '<tr class="liste_titre">';
  505. print getTitleFieldOfList($langs->trans("Pcg_version"), 0, $_SERVER["PHP_SELF"], "pcg_version", ($page ? 'page='.$page.'&' : ''), $param, '', $sortfield, $sortorder, '');
  506. print getTitleFieldOfList($langs->trans("Label"), 0, $_SERVER["PHP_SELF"], "label", ($page ? 'page='.$page.'&' : ''), $param, '', $sortfield, $sortorder, '');
  507. print getTitleFieldOfList($langs->trans("Country"), 0, $_SERVER["PHP_SELF"], "country_code", ($page ? 'page='.$page.'&' : ''), $param, '', $sortfield, $sortorder, '');
  508. print getTitleFieldOfList($langs->trans("Status"), 0, $_SERVER["PHP_SELF"], "active", ($page ? 'page='.$page.'&' : ''), $param, '', $sortfield, $sortorder, 'center ');
  509. print getTitleFieldOfList('');
  510. print getTitleFieldOfList('');
  511. print '</tr>';
  512. if ($num) {
  513. $i = 0;
  514. // Lines with values
  515. while ($i < $num) {
  516. $obj = $db->fetch_object($resql);
  517. //print_r($obj);
  518. print '<tr class="oddeven" id="rowid-'.$obj->rowid.'">';
  519. if ($action == 'edit' && ($rowid == (!empty($obj->rowid) ? $obj->rowid : $obj->code))) {
  520. print '<form action="'.$_SERVER['PHP_SELF'].'?id='.$id.'" method="POST">';
  521. print '<input type="hidden" name="token" value="'.newToken().'">';
  522. print '<input type="hidden" name="page" value="'.$page.'">';
  523. print '<input type="hidden" name="rowid" value="'.$rowid.'">';
  524. $tmpaction = 'edit';
  525. $parameters = array('fieldlist'=>$fieldlist, 'tabname'=>$tabname[$id]);
  526. $reshook = $hookmanager->executeHooks('editDictionaryFieldlist', $parameters, $obj, $tmpaction); // Note that $action and $object may have been modified by some hooks
  527. $error = $hookmanager->error;
  528. $errors = $hookmanager->errors;
  529. if (empty($reshook)) {
  530. fieldListAccountModel($fieldlist, $obj, $tabname[$id], 'edit');
  531. }
  532. print '<td colspan="3" class="right">';
  533. print '<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").'">';
  535. print '</td>';
  536. } else {
  537. $tmpaction = 'view';
  538. $parameters = array('fieldlist'=>$fieldlist, 'tabname'=>$tabname[$id]);
  539. $reshook = $hookmanager->executeHooks('viewDictionaryFieldlist', $parameters, $obj, $tmpaction); // Note that $action and $object may have been modified by some hooks
  540. $error = $hookmanager->error;
  541. $errors = $hookmanager->errors;
  542. if (empty($reshook)) {
  543. foreach ($fieldlist as $field => $value) {
  544. $showfield = 1;
  545. $class = "left";
  546. $tmpvar = $fieldlist[$field];
  547. $valuetoshow = $obj->$tmpvar;
  548. if ($value == 'type_template') {
  549. $valuetoshow = isset($elementList[$valuetoshow]) ? $elementList[$valuetoshow] : $valuetoshow;
  550. }
  551. if ($value == 'element') {
  552. $valuetoshow = isset($elementList[$valuetoshow]) ? $elementList[$valuetoshow] : $valuetoshow;
  553. } elseif ($value == 'source') {
  554. $valuetoshow = isset($sourceList[$valuetoshow]) ? $sourceList[$valuetoshow] : $valuetoshow;
  555. } elseif ($valuetoshow == 'all') {
  556. $valuetoshow = $langs->trans('All');
  557. } elseif ($fieldlist[$field] == 'country') {
  558. if (empty($obj->country_code)) {
  559. $valuetoshow = '-';
  560. } else {
  561. $key = $langs->trans("Country".strtoupper($obj->country_code));
  562. $valuetoshow = ($key != "Country".strtoupper($obj->country_code) ? $obj->country_code." - ".$key : $obj->country);
  563. }
  564. } elseif ($fieldlist[$field] == 'country_id') {
  565. $showfield = 0;
  566. }
  567. $class = 'tddict';
  568. if ($fieldlist[$field] == 'tracking') {
  569. $class .= ' tdoverflowauto';
  570. }
  571. // Show value for field
  572. if ($showfield) {
  573. print '<!-- '.$fieldlist[$field].' --><td class="'.$class.'">'.$valuetoshow.'</td>';
  574. }
  575. }
  576. }
  577. // Can an entry be erased or disabled ?
  578. $iserasable = 1;
  579. $canbedisabled = 1;
  580. $canbemodified = 1; // true by default
  581. $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) : '');
  582. if ($param) {
  583. $url .= '&'.$param;
  584. }
  585. $url .= '&';
  586. // Active
  587. print '<td class="center nowrap">';
  588. if ($canbedisabled) {
  589. print '<a href="'.$url.'action='.$acts[$obj->active].'">'.$actl[$obj->active].'</a>';
  590. } else {
  591. print $langs->trans("AlwaysActive");
  592. }
  593. print "</td>";
  594. // Modify link
  595. if ($canbemodified) {
  596. print '<td class="center"><a class="reposition editfielda" href="'.$url.'action=edit&token='.newToken().'">'.img_edit().'</a></td>';
  597. } else {
  598. print '<td>&nbsp;</td>';
  599. }
  600. // Delete link
  601. if ($iserasable) {
  602. print '<td class="center"><a href="'.$url.'action=delete&token='.newToken().'">'.img_delete().'</a></td>';
  603. } else {
  604. print '<td>&nbsp;</td>';
  605. }
  606. print "</tr>\n";
  607. }
  608. $i++;
  609. }
  610. } else {
  611. print '<tr><td colspan="6"><span class="opacitymedium">'.$langs->trans("NoRecordFound").'</span></td></tr>';
  612. }
  613. } else {
  614. dol_print_error($db);
  615. }
  616. print '</table>';
  617. print '</div>';
  618. print '</form>';
  619. }
  620. print '<br>';
  621. // End of page
  622. llxFooter();
  623. $db->close();
  624. /**
  625. * Show fields in insert/edit mode
  626. *
  627. * @param array $fieldlist Array of fields
  628. * @param Object $obj If we show a particular record, obj is filled with record fields
  629. * @param string $tabname Name of SQL table
  630. * @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
  631. * @return void
  632. */
  633. function fieldListAccountModel($fieldlist, $obj = null, $tabname = '', $context = '')
  634. {
  635. global $langs, $db;
  636. global $form;
  637. global $elementList, $sourceList;
  638. $formadmin = new FormAdmin($db);
  639. $formcompany = new FormCompany($db);
  640. $formaccounting = new FormAccounting($db);
  641. foreach ($fieldlist as $field => $value) {
  642. if ($fieldlist[$field] == 'country') {
  643. if (in_array('region_id', $fieldlist)) {
  644. print '<td>';
  645. //print join(',',$fieldlist);
  646. print '</td>';
  647. continue;
  648. } // For state page, we do not show the country input (we link to region, not country)
  649. print '<td>';
  650. $fieldname = 'country';
  651. print $form->select_country((!empty($obj->country_code) ? $obj->country_code : (!empty($obj->country) ? $obj->country : '')), $fieldname, '', 28, 'maxwidth200 maxwidthonsmartphone');
  652. print '</td>';
  653. } elseif ($fieldlist[$field] == 'country_id') {
  654. if (!in_array('country', $fieldlist)) { // If there is already a field country, we don't show country_id (avoid duplicate)
  655. $country_id = (!empty($obj->{$fieldlist[$field]}) ? $obj->{$fieldlist[$field]} : 0);
  656. print '<td>';
  657. print '<input type="hidden" name="'.$fieldlist[$field].'" value="'.$country_id.'">';
  658. print '</td>';
  659. }
  660. } elseif ($fieldlist[$field] == 'type_cdr') {
  661. if ($fieldlist[$field] == 'type_cdr') {
  662. print '<td class="center">';
  663. } else {
  664. print '<td>';
  665. }
  666. if ($fieldlist[$field] == 'type_cdr') {
  667. 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]} : ''));
  668. } else {
  669. print $form->selectyesno($fieldlist[$field], (!empty($obj->{$fieldlist[$field]}) ? $obj->{$fieldlist[$field]} : ''), 1);
  670. }
  671. print '</td>';
  672. } elseif ($fieldlist[$field] == 'code' && isset($obj->{$fieldlist[$field]})) {
  673. print '<td><input type="text" class="flat" value="'.(!empty($obj->{$fieldlist[$field]}) ? $obj->{$fieldlist[$field]} : '').'" size="10" name="'.$fieldlist[$field].'"></td>';
  674. } else {
  675. print '<td>';
  676. $class = '';
  677. if ($fieldlist[$field] == 'pcg_version') {
  678. $class = 'width150';
  679. }
  680. if ($fieldlist[$field] == 'label') {
  681. $class = 'width300';
  682. }
  683. print '<input type="text" class="flat'.($class ? ' '.$class : '').'" value="'.(isset($obj->{$fieldlist[$field]}) ? $obj->{$fieldlist[$field]} : '').'" name="'.$fieldlist[$field].'">';
  684. print '</td>';
  685. }
  686. }
  687. }