functions.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. /* Copyright (C) 2011-2023 Regis Houssin <regis.houssin@inodbox.com>
  3. * Copyright (C) 2011 Herve Prot <herve.prot@symeos.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. /**
  20. * \file /multicompany/core/ajax/functions.php
  21. * \brief File to return ajax result
  22. */
  23. if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU', '1');
  24. if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML', '1');
  25. if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX', '1');
  26. //if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC', '1');
  27. //if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN', '1');
  28. if (! defined('NOREQUIREHOOK')) define('NOREQUIREHOOK', '1');
  29. if (! defined('CSRFCHECK_WITH_TOKEN')) define('CSRFCHECK_WITH_TOKEN', '1'); // Token is required even in GET mode
  30. if (isset($_POST['action']) && $_POST['action'] === 'getEntityLogo') {
  31. if (!defined('NOLOGIN')) {
  32. define('NOLOGIN', '1'); // This means this output page does not require to be logged.
  33. }
  34. $entity = (!empty($_POST['id']) ? (int) $_POST['id'] : 1);
  35. if (is_numeric($entity)) {
  36. define("DOLENTITY", $entity);
  37. }
  38. } else {
  39. if (!defined('NOTOKENRENEWAL')) {
  40. define('NOTOKENRENEWAL', '1'); // Disables token renewal
  41. }
  42. }
  43. $res=@include("../../../main.inc.php"); // For root directory
  44. if (empty($res) && file_exists($_SERVER['DOCUMENT_ROOT']."/main.inc.php")) {
  45. $res=@include($_SERVER['DOCUMENT_ROOT']."/main.inc.php"); // Use on dev env only
  46. }
  47. if (empty($res)) {
  48. $res=@include("../../../../main.inc.php"); // For "custom" directory
  49. }
  50. dol_include_once('/multicompany/class/actions_multicompany.class.php', 'ActionsMulticompany');
  51. require_once DOL_DOCUMENT_ROOT."/core/lib/admin.lib.php";
  52. $id = GETPOST('id', 'int'); // id of entity
  53. $action = GETPOST('action', 'alpha'); // action method
  54. $type = GETPOST('type', 'alpha'); // type of action
  55. $element = GETPOST('element', 'alpha'); // type of element
  56. $fk_element = GETPOST('fk_element', 'int'); // id of element
  57. $template = GETPOST('template', 'int');
  58. if (GETPOSTISSET('entities')) {
  59. $entities = json_decode(GETPOST('entities', 'none'), true);
  60. }
  61. /*
  62. * View
  63. */
  64. // Ajout directives pour resoudre bug IE
  65. //header('Cache-Control: Public, must-revalidate');
  66. //header('Pragma: public');
  67. //top_htmlhead("", "", 1); // Replaced with top_httphead. An ajax page does not need html header.
  68. top_httphead('application/json');
  69. if (empty($conf->multicompany->enabled)) {
  70. $db->close();
  71. http_response_code(403);
  72. }
  73. //print '<!-- Ajax page called with url '.$_SERVER["PHP_SELF"].'?'.$_SERVER["QUERY_STRING"].' -->'."\n";
  74. // Registering the location of boxes
  75. if (!empty($action) && is_numeric($id)) {
  76. if ($action == 'switchEntity' && !empty($user->login)) {
  77. dol_syslog("multicompany action=".$action." entity=".$id, LOG_DEBUG);
  78. $object = new ActionsMulticompany($db);
  79. echo $object->switchEntity($id);
  80. } elseif ($action == 'setStatusEnable' && !empty($user->admin) && empty($user->entity)) {
  81. dol_syslog("multicompany action=".$action." type=".$type." entity=".$id, LOG_DEBUG);
  82. $object = new ActionsMulticompany($db);
  83. $fieldname = ($type == 'activetemplate' ? 'active' : $type);
  84. echo $object->setStatus($id, $fieldname, 1);
  85. } elseif ($action == 'setStatusDisable' && !empty($user->admin) && empty($user->entity)) {
  86. dol_syslog("multicompany action=".$action." type=".$type." entity=".$id, LOG_DEBUG);
  87. $object = new ActionsMulticompany($db);
  88. $fieldname = ($type == 'activetemplate' ? 'active' : $type);
  89. $ret = $object->setStatus($id, $fieldname, 0);
  90. if ($ret == 1 && $type == 'active') {
  91. $ret = $object->setStatus($id, 'visible', 0);
  92. }
  93. echo $ret;
  94. } elseif ($action == 'deleteEntity' && $id != 1 && !empty($user->admin) && empty($user->entity)) {
  95. dol_syslog("multicompany action=".$action." entity=".$id, LOG_DEBUG);
  96. $object = new ActionsMulticompany($db);
  97. echo $object->deleteEntity($id);
  98. } elseif ($action == 'setColOrder' && !empty($user->admin) && empty($user->entity)) {
  99. $id = (int) $id;
  100. $direction = GETPOST('dir', 'aZ');
  101. $colOrder = array('id' => $id, 'direction' => $direction);
  102. if (dolibarr_set_const($db, 'MULTICOMPANY_COLORDER', json_encode($colOrder), 'chaine', 0, '', 0) > 0) {
  103. $ret = json_encode(array('status' => 'success'));
  104. } else {
  105. $ret = json_encode(array('status' => 'error'));
  106. }
  107. echo $ret;
  108. } elseif ($action == 'setColHidden' && !empty($user->admin) && empty($user->entity)) {
  109. $state = GETPOST('state', 'aZ');
  110. $colHidden = (!empty($conf->global->MULTICOMPANY_COLHIDDEN) ? json_decode($conf->global->MULTICOMPANY_COLHIDDEN, true) : array());
  111. if ($state == 'visible') {
  112. $colHidden = array_diff($colHidden, array(intval($id)));
  113. } else if ($state == 'hidden') {
  114. array_push($colHidden, intval($id));
  115. }
  116. sort($colHidden);
  117. if (dolibarr_set_const($db, 'MULTICOMPANY_COLHIDDEN', json_encode($colHidden), 'chaine', 0, '', 0) > 0) {
  118. $ret = json_encode(array('status' => 'success'));
  119. } else {
  120. $ret = json_encode(array('status' => 'error'));
  121. }
  122. echo $ret;
  123. } elseif ($action == 'modifyEntity' && ((!empty($user->admin) && empty($user->entity)) || !empty($user->rights->multicompany->thirdparty->write))) {
  124. if ($element == 'societe') {
  125. $object = new Societe($db);
  126. $ret = $object->fetch($fk_element);
  127. if ($ret > 0) {
  128. $object->oldcopy = clone $object;
  129. // To not set code if third party is not concerned. But if it had values, we keep them.
  130. if (empty($object->client) && empty($object->oldcopy->code_client)) $object->code_client='';
  131. if (empty($object->fournisseur) && empty($object->oldcopy->code_fournisseur)) $object->code_fournisseur='';
  132. $object->entity = $id;
  133. $ret = $object->update($object->id, $user, 0, $object->oldcopy->codeclient_modifiable(), $object->oldcopy->codefournisseur_modifiable(), 'update', 1);
  134. if ($ret > 0) {
  135. $ret = json_encode(array('status' => 'success'));
  136. } else {
  137. $ret = json_encode(array('status' => 'error', 'error' => $object->errors));
  138. }
  139. } else {
  140. $ret = json_encode(array('status' => 'error', 'error' => $object->errors));
  141. }
  142. } elseif ($element == 'contact') {
  143. require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  144. $object = new Contact($db);
  145. $ret = $object->fetch($fk_element);
  146. if ($ret > 0) {
  147. $object->entity = $id;
  148. $ret = $object->update($object->id, $user, 1, 'update', 1);
  149. if ($ret > 0) {
  150. $ret = json_encode(array('status' => 'success'));
  151. } else {
  152. $ret = json_encode(array('status' => 'error', 'error' => $object->errors));
  153. }
  154. } else {
  155. $ret = json_encode(array('status' => 'error', 'error' => $object->errors));
  156. }
  157. }
  158. else if ($element == 'project')
  159. {
  160. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  161. $object = new Project($db);
  162. $ret = $object->fetch($fk_element);
  163. if ($ret > 0) {
  164. $object->entity = $id;
  165. $ret = $object->setValueFrom('entity',$object->entity);
  166. if ($ret > 0) {
  167. $ret = json_encode(array('status' => 'success'));
  168. }
  169. else {
  170. $ret = json_encode(array('status' => 'error', 'error' => $object->errors));
  171. }
  172. }
  173. else {
  174. $ret = json_encode(array('status' => 'error', 'error' => $object->errors));
  175. }
  176. }
  177. echo $ret;
  178. } elseif ($action === 'getEntityOptions' && !empty($conf->global->MULTICOMPANY_TEMPLATE_MANAGEMENT) && !empty($user->admin) && empty($user->entity)) {
  179. $object = new ActionsMulticompany($db);
  180. $object->getInfo($id);
  181. $entities = $object->getEntitiesList(false, false, true, true);
  182. echo json_encode(
  183. array(
  184. 'status' => 'success',
  185. 'options' => $object->options,
  186. 'labels' => $entities
  187. )
  188. );
  189. } elseif ($action === 'duplicateUserGroupRights' && !empty($conf->global->MULTICOMPANY_TEMPLATE_MANAGEMENT)
  190. && !empty($user->admin) && empty($user->entity) && !empty($template) && !empty($entities)) {
  191. $multicompany = new ActionsMulticompany($db);
  192. $ret = $multicompany->duplicateUserGroupRights($id, $template, $entities);
  193. if ($ret > 0) {
  194. echo json_encode(array('status' => 'success'));
  195. } else {
  196. echo json_encode(
  197. array(
  198. 'status' => 'error',
  199. 'id' => $id,
  200. 'template' => $template,
  201. 'entities' => $entities
  202. )
  203. );
  204. }
  205. } elseif ($action === 'checkIfElementIsUsed'
  206. && !empty($conf->global->MULTICOMPANY_SHARINGS_ENABLED) && !empty($conf->global->MULTICOMPANY_SHARING_BYELEMENT_ENABLED)
  207. && !empty($user->admin) && empty($user->entity) && !empty($element) && !empty($entities)) {
  208. $multicompany = new ActionsMulticompany($db);
  209. $elementname = $element;
  210. if ($elementname == 'thirdparty') {
  211. $elementname = 'societe'; // For compatibility
  212. }
  213. require_once DOL_DOCUMENT_ROOT.'/'.$elementname.'/class/'.$elementname.'.class.php';
  214. $classname = ucfirst($elementname);
  215. $staticobject = new $classname($db);
  216. $result = array();
  217. foreach($entities as $entity) {
  218. $objectisused = $staticobject->isObjectUsed($id, $entity);
  219. if ($objectisused > 0) {
  220. $multicompany->getInfo($entity);
  221. $result[$entity] = $multicompany->label;
  222. }
  223. }
  224. if (!empty($result)) {
  225. $langs->loadLangs(array('multicompany@multicompany'));
  226. echo json_encode(
  227. array(
  228. 'status' => 'error',
  229. 'error' => $langs->transnoentities("ErrorElementIsUsedBy", implode('<br>', $result))
  230. )
  231. );
  232. } else {
  233. echo json_encode(
  234. array(
  235. 'status' => 'success',
  236. 'element' => $element
  237. )
  238. );
  239. }
  240. } else if ($action === 'getChartOfAccountsOfEntity' && !empty($user->rights->accounting->chartofaccount)) {
  241. $object = new ActionsMulticompany($db);
  242. $object->getInfo($id, true, 'CHARTOFACCOUNTS');
  243. if (!empty($object->constants['CHARTOFACCOUNTS'])) {
  244. echo json_encode(
  245. array(
  246. 'status' => 'success',
  247. 'chartofaccounts' => $object->constants['CHARTOFACCOUNTS']
  248. )
  249. );
  250. }
  251. } else if ($action === 'getEntityLogo' && empty($conf->global->MULTICOMPANY_HIDE_LOGIN_COMBOBOX)) {
  252. $urllogo = DOL_URL_ROOT.'/theme/login_logo.png';
  253. if (!empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small)) {
  254. $urllogo = DOL_URL_ROOT.'/viewimage.php?cache=1&amp;entity='.$id.'&amp;modulepart=mycompany&amp;file='.urlencode('logos/thumbs/'.$mysoc->logo_small);
  255. } elseif (!empty($mysoc->logo) && is_readable($conf->mycompany->dir_output.'/logos/'.$mysoc->logo)) {
  256. $urllogo = DOL_URL_ROOT.'/viewimage.php?cache=1&amp;entity='.$id.'&amp;modulepart=mycompany&amp;file='.urlencode('logos/'.$mysoc->logo);
  257. $width = 128;
  258. } elseif (is_readable(DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/img/dolibarr_logo.png')) {
  259. $urllogo = DOL_URL_ROOT.'/theme/'.$conf->theme.'/img/dolibarr_logo.png';
  260. } elseif (is_readable(DOL_DOCUMENT_ROOT.'/theme/dolibarr_logo.png')) {
  261. $urllogo = DOL_URL_ROOT.'/theme/dolibarr_logo.png';
  262. }
  263. $bgimg = null;
  264. $unsplashimg = null;
  265. if (!empty($conf->global->MULTICOMPANY_LOGIN_BACKGROUND_BY_ENTITY)) {
  266. if (!empty($conf->global->ADD_UNSPLASH_LOGIN_BACKGROUND)) {
  267. $unsplashimg = $conf->global->ADD_UNSPLASH_LOGIN_BACKGROUND;
  268. }
  269. if (!empty($conf->global->MAIN_LOGIN_BACKGROUND)) {
  270. $bgimg = DOL_URL_ROOT.'/viewimage.php?cache=1&noalt=1&entity='.$id.'&modulepart=mycompany&file=logos/'.urlencode($conf->global->MAIN_LOGIN_BACKGROUND);
  271. }
  272. }
  273. echo json_encode(
  274. array(
  275. 'status' => 'success',
  276. 'urllogo' => dol_html_entity_decode($urllogo, null),
  277. 'bgimg' => dol_html_entity_decode($bgimg, null),
  278. 'unsplashimg' => dol_html_entity_decode($unsplashimg, null)
  279. )
  280. );
  281. }
  282. $db->close();
  283. } else {
  284. $db->close();
  285. http_response_code(403);
  286. }