actions_addupdatedelete.inc.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. <?php
  2. /* Copyright (C) 2017-2019 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. * or see https://www.gnu.org/
  17. */
  18. /**
  19. * \file htdocs/core/actions_addupdatedelete.inc.php
  20. * \brief Code for common actions cancel / add / update / update_extras / delete / deleteline / validate / cancel / reopen / clone
  21. */
  22. // $action or $cancel must be defined
  23. // $object must be defined
  24. // $permissiontoadd must be defined
  25. // $permissiontodelete must be defined
  26. // $backurlforlist must be defined
  27. // $backtopage may be defined
  28. // $noback may be defined
  29. // $triggermodname may be defined
  30. $hidedetails = isset($hidedetails) ? $hidedetails : '';
  31. $hidedesc = isset($hidedesc) ? $hidedesc : '';
  32. $hideref = isset($hideref) ? $hideref : '';
  33. if (!empty($permissionedit) && empty($permissiontoadd)) {
  34. $permissiontoadd = $permissionedit; // For backward compatibility
  35. }
  36. if ($cancel) {
  37. /*var_dump($cancel);var_dump($backtopage);var_dump($backtopageforcancel);exit;*/
  38. if (!empty($backtopageforcancel)) {
  39. header("Location: " . $backtopageforcancel);
  40. exit;
  41. } elseif (!empty($backtopage)) {
  42. header("Location: " . $backtopage);
  43. exit;
  44. }
  45. $action = '';
  46. } /* */
  47. if ($action == 'add' && !empty($permissiontoadd)) {
  48. foreach ($object->fields as $key => $val) {
  49. if ($object->fields[$key]['type'] == 'duration') {
  50. if (GETPOST($key . 'hour') == '' && GETPOST($key . 'min') == '') {
  51. continue; // The field was not submited to be saved
  52. }
  53. } else {
  54. if (!GETPOSTISSET($key)) {
  55. continue; // The field was not submited to be saved
  56. }
  57. }
  58. // Ignore special fields
  59. if (in_array($key, array('rowid', 'entity', 'import_key'))) {
  60. continue;
  61. }
  62. if (in_array($key, array('date_creation', 'tms', 'fk_user_creat', 'fk_user_modif'))) {
  63. if (!in_array(abs($val['visible']), array(1, 3))) {
  64. continue; // Only 1 and 3 that are case to create
  65. }
  66. }
  67. // Set value to insert
  68. if (in_array($object->fields[$key]['type'], array('text', 'html'))) {
  69. $value = GETPOST($key, 'restricthtml');
  70. } elseif ($object->fields[$key]['type'] == 'date') {
  71. $value = dol_mktime(12, 0, 0, GETPOST($key . 'month', 'int'), GETPOST($key . 'day', 'int'), GETPOST($key . 'year', 'int')); // for date without hour, we use gmt
  72. } elseif ($object->fields[$key]['type'] == 'datetime') {
  73. $value = dol_mktime(GETPOST($key . 'hour', 'int'), GETPOST($key . 'min', 'int'), GETPOST($key . 'sec', 'int'), GETPOST($key . 'month', 'int'), GETPOST($key . 'day', 'int'), GETPOST($key . 'year', 'int'), 'tzuserrel');
  74. } elseif ($object->fields[$key]['type'] == 'duration') {
  75. $value = 60 * 60 * GETPOST($key . 'hour', 'int') + 60 * GETPOST($key . 'min', 'int');
  76. } elseif (preg_match('/^(integer|price|real|double)/', $object->fields[$key]['type'])) {
  77. $value = price2num(GETPOST($key, 'alphanohtml')); // To fix decimal separator according to lang setup
  78. } elseif ($object->fields[$key]['type'] == 'boolean') {
  79. $value = ((GETPOST($key) == '1' || GETPOST($key) == 'on') ? 1 : 0);
  80. } elseif ($object->fields[$key]['type'] == 'reference') {
  81. $tmparraykey = array_keys($object->param_list);
  82. $value = $tmparraykey[GETPOST($key)] . ',' . GETPOST($key . '2');
  83. } else {
  84. if ($key == 'lang') {
  85. $value = GETPOST($key, 'aZ09') ? GETPOST($key, 'aZ09') : "";
  86. } else {
  87. $value = GETPOST($key, 'alphanohtml');
  88. }
  89. }
  90. if (preg_match('/^integer:/i', $object->fields[$key]['type']) && $value == '-1') {
  91. $value = ''; // This is an implicit foreign key field
  92. }
  93. if (!empty($object->fields[$key]['foreignkey']) && $value == '-1') {
  94. $value = ''; // This is an explicit foreign key field
  95. }
  96. //var_dump($key.' '.$value.' '.$object->fields[$key]['type']);
  97. $object->$key = $value;
  98. if (!empty($val['notnull']) && $val['notnull'] > 0 && $object->$key == '' && isset($val['default']) && $val['default'] == '(PROV)') {
  99. $object->$key = '(PROV)';
  100. }
  101. if (!empty($val['notnull']) && $val['notnull'] > 0 && $object->$key == '' && !isset($val['default'])) {
  102. $error++;
  103. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv($val['label'])), null, 'errors');
  104. }
  105. // Validation of fields values
  106. if (getDolGlobalInt('MAIN_FEATURES_LEVEL') >= 2 || !empty($conf->global->MAIN_ACTIVATE_VALIDATION_RESULT)) {
  107. if (!$error && !empty($val['validate']) && is_callable(array($object, 'validateField'))) {
  108. if (!$object->validateField($object->fields, $key, $value)) {
  109. $error++;
  110. }
  111. }
  112. }
  113. }
  114. // Fill array 'array_options' with data from add form
  115. if (!$error) {
  116. $ret = $extrafields->setOptionalsFromPost(null, $object, '', 1);
  117. if ($ret < 0) {
  118. $error++;
  119. }
  120. }
  121. if (!$error) {
  122. $result = $object->create($user);
  123. if ($result > 0) {
  124. // Creation OK
  125. if (isModEnabled('categorie') && method_exists($object, 'setCategories')) {
  126. $categories = GETPOST('categories', 'array:int');
  127. $object->setCategories($categories);
  128. }
  129. $urltogo = $backtopage ? str_replace('__ID__', $result, $backtopage) : $backurlforlist;
  130. $urltogo = preg_replace('/--IDFORBACKTOPAGE--/', $object->id, $urltogo); // New method to autoselect project after a New on another form object creation
  131. if (empty($noback)) {
  132. header("Location: " . $urltogo);
  133. exit;
  134. }
  135. } else {
  136. $error++;
  137. // Creation KO
  138. if (!empty($object->errors)) {
  139. setEventMessages(null, $object->errors, 'errors');
  140. } else {
  141. setEventMessages($object->error, null, 'errors');
  142. }
  143. $action = 'create';
  144. }
  145. } else {
  146. $action = 'create';
  147. }
  148. }
  149. if ($action == 'update' && !empty($permissiontoadd)) {
  150. foreach ($object->fields as $key => $val) {
  151. // Check if field was submited to be edited
  152. if ($object->fields[$key]['type'] == 'duration') {
  153. if (!GETPOSTISSET($key . 'hour') || !GETPOSTISSET($key . 'min')) {
  154. continue; // The field was not submited to be saved
  155. }
  156. } elseif ($object->fields[$key]['type'] == 'boolean') {
  157. if (!GETPOSTISSET($key)) {
  158. $object->$key = 0; // use 0 instead null if the field is defined as not null
  159. continue;
  160. }
  161. } else {
  162. if (!GETPOSTISSET($key)) {
  163. continue; // The field was not submited to be saved
  164. }
  165. }
  166. // Ignore special fields
  167. if (in_array($key, array('rowid', 'entity', 'import_key'))) {
  168. continue;
  169. }
  170. if (in_array($key, array('date_creation', 'tms', 'fk_user_creat', 'fk_user_modif'))) {
  171. if (!in_array(abs($val['visible']), array(1, 3, 4))) {
  172. continue; // Only 1 and 3 and 4, that are cases to update
  173. }
  174. }
  175. // Set value to update
  176. if (preg_match('/^(text|html)/', $object->fields[$key]['type'])) {
  177. $tmparray = explode(':', $object->fields[$key]['type']);
  178. if (!empty($tmparray[1])) {
  179. $value = GETPOST($key, $tmparray[1]);
  180. } else {
  181. $value = GETPOST($key, 'restricthtml');
  182. }
  183. } elseif ($object->fields[$key]['type'] == 'date') {
  184. $value = dol_mktime(12, 0, 0, GETPOST($key . 'month', 'int'), GETPOST($key . 'day', 'int'), GETPOST($key . 'year', 'int')); // for date without hour, we use gmt
  185. } elseif ($object->fields[$key]['type'] == 'datetime') {
  186. $value = dol_mktime(GETPOST($key . 'hour', 'int'), GETPOST($key . 'min', 'int'), GETPOST($key . 'sec', 'int'), GETPOST($key . 'month', 'int'), GETPOST($key . 'day', 'int'), GETPOST($key . 'year', 'int'), 'tzuserrel');
  187. } elseif ($object->fields[$key]['type'] == 'duration') {
  188. if (GETPOST($key . 'hour', 'int') != '' || GETPOST($key . 'min', 'int') != '') {
  189. $value = 60 * 60 * GETPOST($key . 'hour', 'int') + 60 * GETPOST($key . 'min', 'int');
  190. } else {
  191. $value = '';
  192. }
  193. } elseif (preg_match('/^(integer|price|real|double)/', $object->fields[$key]['type'])) {
  194. $value = price2num(GETPOST($key, 'alphanohtml')); // To fix decimal separator according to lang setup
  195. } elseif ($object->fields[$key]['type'] == 'boolean') {
  196. $value = ((GETPOST($key, 'aZ09') == 'on' || GETPOST($key, 'aZ09') == '1') ? 1 : 0);
  197. } elseif ($object->fields[$key]['type'] == 'reference') {
  198. $value = array_keys($object->param_list)[GETPOST($key)] . ',' . GETPOST($key . '2');
  199. } else {
  200. if ($key == 'lang') {
  201. $value = GETPOST($key, 'aZ09');
  202. } else {
  203. $value = GETPOST($key, 'alphanohtml');
  204. }
  205. }
  206. if (preg_match('/^integer:/i', $object->fields[$key]['type']) && $value == '-1') {
  207. $value = ''; // This is an implicit foreign key field
  208. }
  209. if (!empty($object->fields[$key]['foreignkey']) && $value == '-1') {
  210. $value = ''; // This is an explicit foreign key field
  211. }
  212. $object->$key = $value;
  213. if ($val['notnull'] > 0 && $object->$key == '' && is_null($val['default'])) {
  214. $error++;
  215. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv($val['label'])), null, 'errors');
  216. }
  217. // Validation of fields values
  218. if (getDolGlobalInt('MAIN_FEATURES_LEVEL') >= 2 || !empty($conf->global->MAIN_ACTIVATE_VALIDATION_RESULT)) {
  219. if (!$error && !empty($val['validate']) && is_callable(array($object, 'validateField'))) {
  220. if (!$object->validateField($object->fields, $key, $value)) {
  221. $error++;
  222. }
  223. }
  224. }
  225. if (isModEnabled('categorie')) {
  226. $categories = GETPOST('categories', 'array');
  227. if (method_exists($object, 'setCategories')) {
  228. $object->setCategories($categories);
  229. }
  230. }
  231. }
  232. // Fill array 'array_options' with data from add form
  233. if (!$error) {
  234. $ret = $extrafields->setOptionalsFromPost(null, $object, '@GETPOSTISSET');
  235. if ($ret < 0) {
  236. $error++;
  237. }
  238. }
  239. if (!$error) {
  240. $result = $object->update($user);
  241. if ($result > 0) {
  242. $action = 'view';
  243. $urltogo = $backtopage ? str_replace('__ID__', $result, $backtopage) : $backurlforlist;
  244. $urltogo = preg_replace('/--IDFORBACKTOPAGE--/', $object->id, $urltogo); // New method to autoselect project after a New on another form object creation
  245. if ($urltogo && !$noback) {
  246. header("Location: " . $urltogo);
  247. exit;
  248. }
  249. } else {
  250. $error++;
  251. // Creation KO
  252. setEventMessages($object->error, $object->errors, 'errors');
  253. $action = 'edit';
  254. }
  255. } else {
  256. $action = 'edit';
  257. }
  258. }
  259. if ($action == 'confirm_users_delete') {
  260. $toselectConfirm = explode(',', $toselectString);
  261. $groupusers = new GroupUsers($db);
  262. foreach ($toselectConfirm as $userId) {
  263. $moredata = explode('_', $userId);
  264. $groupusers->deleteLine($user, $moredata[0]);
  265. $data = $helper->createDataArray($id, $massaction, $moredata[1]);
  266. $userNaplo->createUserNaplo($user, $data);
  267. dol_syslog("A {$userId} szamu felhasznalo torlesre kerult.", LOG_DEBUG | LOG_INFO | LOG_WARNING | LOG_ERR);
  268. }
  269. unset($_SESSION['usersArray']);
  270. $helper->returnWindowLocation($_SERVER["PHP_SELF"], $id, $idmenu);
  271. }
  272. // Action to update one extrafield
  273. if ($action == "update_extras" && !empty($permissiontoadd)) {
  274. $object->fetch(GETPOST('id', 'int'));
  275. $attributekey = GETPOST('attribute', 'alpha');
  276. $attributekeylong = 'options_' . $attributekey;
  277. if (GETPOSTISSET($attributekeylong . 'day') && GETPOSTISSET($attributekeylong . 'month') && GETPOSTISSET($attributekeylong . 'year')) {
  278. // This is properties of a date
  279. $object->array_options['options_' . $attributekey] = dol_mktime(GETPOST($attributekeylong . 'hour', 'int'), GETPOST($attributekeylong . 'min', 'int'), GETPOST($attributekeylong . 'sec', 'int'), GETPOST($attributekeylong . 'month', 'int'), GETPOST($attributekeylong . 'day', 'int'), GETPOST($attributekeylong . 'year', 'int'));
  280. //var_dump(dol_print_date($object->array_options['options_'.$attributekey]));exit;
  281. } else {
  282. $object->array_options['options_' . $attributekey] = GETPOST($attributekeylong, 'alpha');
  283. }
  284. $result = $object->insertExtraFields(empty($triggermodname) ? '' : $triggermodname, $user);
  285. if ($result > 0) {
  286. setEventMessages($langs->trans('RecordSaved'), null, 'mesgs');
  287. $action = 'view';
  288. } else {
  289. $error++;
  290. setEventMessages($object->error, $object->errors, 'errors');
  291. $action = 'edit_extras';
  292. }
  293. }
  294. if ($action == 'confirm_delete' && !empty($permissiontodelete)) {
  295. if (!($object->id > 0)) {
  296. dol_print_error('', 'Error, object must be fetched before being deleted');
  297. exit;
  298. }
  299. $result = $object->delete($user);
  300. if ($result > 0) {
  301. // Delete OK
  302. setEventMessages("RecordDeleted", null, 'mesgs');
  303. if (empty($noback)) {
  304. header("Location: " . $backurlforlist);
  305. exit;
  306. }
  307. } else {
  308. $error++;
  309. if (!empty($object->errors)) {
  310. setEventMessages(null, $object->errors, 'errors');
  311. } else {
  312. setEventMessages($object->error, null, 'errors');
  313. }
  314. }
  315. $action = '';
  316. }
  317. // Remove a line
  318. if ($action == 'confirm_deleteline' && $confirm == 'yes' && !empty($permissiontoadd)) {
  319. if (method_exists($object, 'deleteline')) {
  320. $result = $object->deleteline($user, $lineid); // For backward compatibility
  321. } else {
  322. $result = $object->deleteLine($user, $lineid);
  323. }
  324. if ($result > 0) {
  325. // Define output language
  326. $outputlangs = $langs;
  327. $newlang = '';
  328. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id', 'aZ09')) {
  329. $newlang = GETPOST('lang_id', 'aZ09');
  330. }
  331. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && is_object($object->thirdparty)) {
  332. $newlang = $object->thirdparty->default_lang;
  333. }
  334. if (!empty($newlang)) {
  335. $outputlangs = new Translate("", $conf);
  336. $outputlangs->setDefaultLang($newlang);
  337. }
  338. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
  339. if (method_exists($object, 'generateDocument')) {
  340. $ret = $object->fetch($object->id); // Reload to get new records
  341. $object->generateDocument($object->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  342. }
  343. }
  344. setEventMessages($langs->trans('RecordDeleted'), null, 'mesgs');
  345. if (empty($noback)) {
  346. header('Location: ' . $_SERVER["PHP_SELF"] . '?id=' . $object->id);
  347. exit;
  348. }
  349. } else {
  350. $error++;
  351. setEventMessages($object->error, $object->errors, 'errors');
  352. }
  353. $action = '';
  354. }
  355. // Confirmation to clear all users from groups
  356. if ($action == 'userclearall' && $permissiontoclearall) {
  357. $formconfirm = '';
  358. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?idmenu=' . $idmenu, $langs->trans('ClearAllUsersFromGroups'), $langs->trans('ConfirmClearAllUsersFromGroupsObject'), 'confirm_userclearall', '', 0, 1);
  359. print $formconfirm;
  360. }
  361. if ($action == 'confirm_userclearall' && $permissiontoclearall) {
  362. $data = $helper->getListOfUsersFromTheGroupTable();
  363. if (empty($data)) {
  364. dol_syslog("No groupusers found.", LOG_DEBUG | LOG_INFO | LOG_WARNING | LOG_ERR);
  365. setEventMessage($langs->trans('noUsersInGroups'), 'errors');
  366. } else {
  367. $db->begin();
  368. foreach ($data as $record) {
  369. $record = (object) $record;
  370. $insertResult = $helper->createUserNaplo($record);
  371. $object = new GroupUsers($db);
  372. $result = $helper->deleteLine($object, $record->rowid);
  373. if ($insertResult && $result) {
  374. $db->commit();
  375. } else {
  376. $db->rollback();
  377. setEventMessage($langs->trans('unsuccessfulUsersDelete'), 'errors');
  378. return -1;
  379. }
  380. }
  381. }
  382. $helper->returnWindowLocation($_SERVER["PHP_SELF"], $id, $idmenu);
  383. }
  384. if ($action == 'toolclearall' && $permissiontoclearall) {
  385. $formconfirm = '';
  386. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"], $langs->trans('ClearAllToolsFromGroups'), $langs->trans('ConfirmClearAllToolsFromGroupsObject'), 'confirm_toolclearall', '', 0, 1);
  387. print $formconfirm;
  388. }
  389. if ($action == 'confirm_toolclearall' && $permissiontoclearall) {
  390. $data = $helper->getListOfToolsFromTheGroups();
  391. if (!$data) {
  392. dol_syslog("No groupusers found.", LOG_DEBUG | LOG_INFO | LOG_WARNING | LOG_ERR);
  393. setEventMessage($langs->trans('noUsersInGroups'), 'errors');
  394. } else {
  395. $db->begin();
  396. foreach ($data as $record) {
  397. $insertResult = $helper->createToolNaplo($record);
  398. $object = new PackageTool($db);
  399. $result = $helper->deleteLine($object, $record->id);
  400. if ($insertResult && $result) {
  401. $db->commit();
  402. } else {
  403. $db->rollback();
  404. setEventMessage($langs->trans('unsuccessfulToolsDelete'), 'errors');
  405. return -1;
  406. }
  407. }
  408. }
  409. $helper->returnWindowLocation($_SERVER["PHP_SELF"], $id, $idmenu);
  410. }
  411. if ($action == 'packageclearall' && $permissiontoclearall) {
  412. $formconfirm = '';
  413. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $id, $langs->trans('ClearAllDevicesFromPackage'), $langs->trans('ConfirmClearAllDevicesFromPackagesObject'), 'confirm_packageclearall', '', 0, 1);
  414. print $formconfirm;
  415. }
  416. if ($action == 'confirm_packageclearall' && $permissiontoclearall) {
  417. $data = $helper->getListOfPackagesFromTheGroups();
  418. if (!$data) {
  419. dol_syslog("No grouppackage found.", LOG_DEBUG | LOG_INFO | LOG_WARNING | LOG_ERR);
  420. setEventMessage($langs->trans('noToolsInPackage'), 'errors');
  421. } else {
  422. $db->begin();
  423. foreach ($data as $record) {
  424. $insertResult = $helper->createPackageNaplo($record);
  425. $object = new GroupToolsPackages($db);
  426. $result = $helper->deleteLine($object, $record->id);
  427. if ($insertResult && $result) {
  428. $db->commit();
  429. } else {
  430. $db->rollback();
  431. setEventMessage($langs->trans('unsuccessfulToolDelete'), 'errors');
  432. return -1;
  433. }
  434. }
  435. }
  436. $helper->returnWindowLocation($_SERVER["PHP_SELF"], $id, $idmenu);
  437. }
  438. if ($action == 'confirm_logout_record') {
  439. global $db, $user;
  440. $errorString = '';
  441. $group_user_id = $helper->getGroupUserIdByUserId($id);
  442. if (!$group_user_id) {
  443. $error++;
  444. }
  445. $userNaploObj = new UserNaplo($db);
  446. $userNaploObj->user_id = $id;
  447. $userNaploObj->group_user_id = $group_user_id;
  448. $userNaploObj->status = 0;
  449. $resultUserNaplo = $userNaploObj->create($user);
  450. if ($resultUserNaplo <= 0) {
  451. $resultUserNaplo = $userNaploObj->create($user);
  452. $error++;
  453. }
  454. $userLoginNaploObj = new UserLoginNaplo($db);
  455. $userLoginNaploObj->user_id = $id;
  456. $userLoginNaploObj->login_logout_status = 1;
  457. $resultUserLoginNaplo = $userLoginNaploObj->create($user);
  458. if ($resultUserLoginNaplo <= 0) {
  459. $error++;
  460. }
  461. $now = date("Y-m-d H:i:s", dol_now());
  462. $userLogoutChangeNaploObj = new LogoutChangeNaplo($db);
  463. $userLogoutChangeNaploObj->user_id = $id;
  464. $userLogoutChangeNaploObj->fk_logout = $resultUserLoginNaplo;
  465. $userLogoutChangeNaploObj->comment = 'Admin Logout from LogoutHandler.';
  466. $userLogoutChangeNaploObj->original_date = $now;
  467. $userLogoutChangeNaploObj->corrected_date = $now;
  468. $resultLogoutChangeNaplos = $userLogoutChangeNaploObj->create($user);
  469. if ($resultLogoutChangeNaplos <= 0) {
  470. $error++;
  471. }
  472. if ($error) {
  473. $db->rollback();
  474. setEventMessage($langs->trans('ConfirmLogoutError'), 'errors');
  475. }
  476. $db->commit();
  477. $helper->returnDailyClosingWindowLocation($_SERVER["PHP_SELF"], $id, $idmenu);
  478. }
  479. if ($action == 'confirm_users_settlement') {
  480. global $db, $user;
  481. $usersOfGroups = $helper->getUsersById($db, $groupuser_id);
  482. $usersArray = [];
  483. foreach ($usersOfGroups as $userObj) {
  484. $usersArray[$userObj['rowid']] = $userObj['fk_user'];
  485. }
  486. $db->begin();
  487. $error = 0;
  488. $groupusers = new GroupUsers($db);
  489. $groupObj = new Group($db);
  490. $userNaplo = new UserNaplo($db);
  491. $groupResult = $groupObj->fetch($groupuser_id);
  492. $groupEntity = $groupObj->fk_entity;
  493. foreach ($usersArray as $userkey => $userval) {
  494. $deleteResult = $groupusers->deleteLine($user, $userkey);
  495. if ($deleteResult < 0) {
  496. $error++;
  497. }
  498. $createResult = $groupusers->createGroupUsers($groupuser_id, $userval);
  499. if ($createResult < 0) {
  500. $error++;
  501. }
  502. $dataAdd = $helper->createDataArray($groupuser_id, 'add', $userval);
  503. $naploResult = $userNaplo->createUserNaplo($user, $dataAdd);
  504. if ($naploResult < 0) {
  505. $error++;
  506. }
  507. $userUpdateResult = $helper->updateUserEntityInUser($userval, $groupEntity);
  508. if ($userUpdateResult < 0) {
  509. $error++;
  510. }
  511. }
  512. if ($error) {
  513. $db->rollback();
  514. unset($toselect);
  515. setEventMessage($langs->trans("PackageUserFailed", 'error'));
  516. } else {
  517. $confirmbutton = null;
  518. $db->commit();
  519. $helper->returnWindowLocation($_SERVER["PHP_SELF"], $id, $idmenu);
  520. }
  521. }