workstation.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. <?php
  2. /* Copyright (C) 2004-2017 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2020 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
  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, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/admin/workstation.php
  20. * \ingroup workstation
  21. * \brief Workstation setup page.
  22. */
  23. // Load Dolibarr environment
  24. require "../main.inc.php";
  25. // Libraries
  26. require_once DOL_DOCUMENT_ROOT . "/core/lib/admin.lib.php";
  27. require_once DOL_DOCUMENT_ROOT . '/workstation/lib/workstation.lib.php';
  28. //require_once "../class/myclass.class.php";
  29. // Translations
  30. $langs->loadLangs(array("admin", "workstation"));
  31. // Parameters
  32. $action = GETPOST('action', 'aZ09');
  33. $backtopage = GETPOST('backtopage', 'alpha');
  34. $value = GETPOST('value', 'alpha');
  35. /*$arrayofparameters = array(
  36. 'WORKSTATION_MYPARAM1'=>array('css'=>'minwidth200', 'enabled'=>1),
  37. 'WORKSTATION_MYPARAM2'=>array('css'=>'minwidth500', 'enabled'=>1)
  38. );*/
  39. $error = 0;
  40. $setupnotempty = 0;
  41. $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
  42. // Access control
  43. if (!$user->admin) {
  44. accessforbidden();
  45. }
  46. /*
  47. * Actions
  48. */
  49. include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php';
  50. if ($action == 'updateMask') {
  51. $maskconstorder = GETPOST('maskconstWorkstation', 'alpha');
  52. $maskorder = GETPOST('maskWorkstation', 'alpha');
  53. if ($maskconstorder) {
  54. $res = dolibarr_set_const($db, $maskconstorder, $maskorder, 'chaine', 0, '', $conf->entity);
  55. }
  56. if (!($res > 0)) {
  57. $error++;
  58. }
  59. if (!$error) {
  60. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  61. } else {
  62. setEventMessages($langs->trans("Error"), null, 'errors');
  63. }
  64. } elseif ($action == 'specimen') {
  65. $modele = GETPOST('module', 'alpha');
  66. $tmpobjectkey = GETPOST('object');
  67. $tmpobject = new $tmpobjectkey($db);
  68. $tmpobject->initAsSpecimen();
  69. // Search template files
  70. $file = ''; $classname = ''; $filefound = 0;
  71. $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
  72. foreach ($dirmodels as $reldir) {
  73. $file = dol_buildpath($reldir."core/modules/workstation/doc/pdf_".$modele."_".strtolower($tmpobjectkey).".modules.php", 0);
  74. if (file_exists($file)) {
  75. $filefound = 1;
  76. $classname = "pdf_".$modele;
  77. break;
  78. }
  79. }
  80. if ($filefound) {
  81. require_once $file;
  82. $module = new $classname($db);
  83. if ($module->write_file($tmpobject, $langs) > 0) {
  84. header("Location: ".DOL_URL_ROOT."/document.php?modulepart=".strtolower($tmpobjectkey)."&file=SPECIMEN.pdf");
  85. return;
  86. } else {
  87. setEventMessages($module->error, null, 'errors');
  88. dol_syslog($module->error, LOG_ERR);
  89. }
  90. } else {
  91. setEventMessages($langs->trans("ErrorModuleNotFound"), null, 'errors');
  92. dol_syslog($langs->trans("ErrorModuleNotFound"), LOG_ERR);
  93. }
  94. } elseif ($action == 'set') {
  95. // Activate a model
  96. $ret = addDocumentModel($value, $type, $label, $scandir);
  97. } elseif ($action == 'del') {
  98. $tmpobjectkey = GETPOST('object');
  99. $ret = delDocumentModel($value, $type);
  100. if ($ret > 0) {
  101. $constforval = strtoupper($tmpobjectkey).'_ADDON_PDF';
  102. if ($conf->global->$constforval == "$value") {
  103. dolibarr_del_const($db, $constforval, $conf->entity);
  104. }
  105. }
  106. } elseif ($action == 'setdoc') {
  107. // Set default model
  108. $tmpobjectkey = GETPOST('object');
  109. $constforval = strtoupper($tmpobjectkey).'_ADDON_PDF';
  110. if (dolibarr_set_const($db, $constforval, $value, 'chaine', 0, '', $conf->entity)) {
  111. // The constant that was read before the new set
  112. // We therefore requires a variable to have a coherent view
  113. $conf->global->$constforval = $value;
  114. }
  115. // On active le modele
  116. $ret = delDocumentModel($value, $type);
  117. if ($ret > 0) {
  118. $ret = addDocumentModel($value, $type, $label, $scandir);
  119. }
  120. } elseif ($action == 'setmod') {
  121. // TODO Check if numbering module chosen can be activated
  122. // by calling method canBeActivated
  123. $tmpobjectkey = GETPOST('object');
  124. $constforval = 'WORKSTATION_'.strtoupper($tmpobjectkey)."_ADDON";
  125. dolibarr_set_const($db, $constforval, $value, 'chaine', 0, '', $conf->entity);
  126. }
  127. /*
  128. * View
  129. */
  130. $form = new Form($db);
  131. $help_url = '';
  132. $page_name = "WorkstationSetup";
  133. llxHeader('', $langs->trans($page_name), $help_url);
  134. // Subheader
  135. $linkback = '<a href="'.($backtopage ? $backtopage : DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1').'">'.$langs->trans("BackToModuleList").'</a>';
  136. print load_fiche_titre($langs->trans($page_name), $linkback, 'title_setup');
  137. // Configuration header
  138. $head = workstationAdminPrepareHead();
  139. print dol_get_fiche_head($head, 'settings', $langs->trans($page_name), -1, "workstation");
  140. // Setup page goes here
  141. //echo '<span class="opacitymedium">'.$langs->trans("WorkstationSetupPage").'</span><br><br>';
  142. if ($action == 'edit') {
  143. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  144. print '<input type="hidden" name="token" value="'.newToken().'">';
  145. print '<input type="hidden" name="action" value="update">';
  146. print '<table class="noborder centpercent">';
  147. print '<tr class="liste_titre"><td class="titlefield">'.$langs->trans("Parameter").'</td><td>'.$langs->trans("Value").'</td></tr>';
  148. foreach ($arrayofparameters as $key => $val) {
  149. print '<tr class="oddeven"><td>';
  150. $tooltiphelp = (($langs->trans($key.'Tooltip') != $key.'Tooltip') ? $langs->trans($key.'Tooltip') : '');
  151. print $form->textwithpicto($langs->trans($key), $tooltiphelp);
  152. print '</td><td><input name="'.$key.'" class="flat '.(empty($val['css']) ? 'minwidth200' : $val['css']).'" value="'.$conf->global->$key.'"></td></tr>';
  153. }
  154. print '</table>';
  155. print '<br><div class="center">';
  156. print '<input class="button button-save" type="submit" value="'.$langs->trans("Save").'">';
  157. print '</div>';
  158. print '</form>';
  159. print '<br>';
  160. } else {
  161. if (!empty($arrayofparameters)) {
  162. print '<table class="noborder centpercent">';
  163. print '<tr class="liste_titre"><td class="titlefield">'.$langs->trans("Parameter").'</td><td>'.$langs->trans("Value").'</td></tr>';
  164. foreach ($arrayofparameters as $key => $val) {
  165. $setupnotempty++;
  166. print '<tr class="oddeven"><td>';
  167. $tooltiphelp = (($langs->trans($key.'Tooltip') != $key.'Tooltip') ? $langs->trans($key.'Tooltip') : '');
  168. print $form->textwithpicto($langs->trans($key), $tooltiphelp);
  169. print '</td><td>'.$conf->global->$key.'</td></tr>';
  170. }
  171. print '</table>';
  172. print '<div class="tabsAction">';
  173. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=edit">'.$langs->trans("Modify").'</a>';
  174. print '</div>';
  175. }/* else {
  176. print '<br>'.$langs->trans("NothingToSetup");
  177. }*/
  178. }
  179. $moduledir = 'workstation';
  180. $myTmpObjects = array();
  181. $myTmpObjects['workstation'] = array('includerefgeneration'=>1, 'includedocgeneration'=>0);
  182. foreach ($myTmpObjects as $myTmpObjectKey => $myTmpObjectArray) {
  183. if ($myTmpObjectKey == 'MyObject') {
  184. continue;
  185. }
  186. if ($myTmpObjectArray['includerefgeneration']) {
  187. /*
  188. * Orders Numbering model
  189. */
  190. $setupnotempty++;
  191. print load_fiche_titre($langs->trans("NumberingModules", $myTmpObjectKey), '', '');
  192. print '<table class="noborder centpercent">';
  193. print '<tr class="liste_titre">';
  194. print '<td>'.$langs->trans("Name").'</td>';
  195. print '<td>'.$langs->trans("Description").'</td>';
  196. print '<td class="nowrap">'.$langs->trans("Example").'</td>';
  197. print '<td class="center" width="60">'.$langs->trans("Status").'</td>';
  198. print '<td class="center" width="16">'.$langs->trans("ShortInfo").'</td>';
  199. print '</tr>'."\n";
  200. clearstatcache();
  201. foreach ($dirmodels as $reldir) {
  202. $dir = dol_buildpath($reldir."core/modules/".$moduledir);
  203. if (is_dir($dir)) {
  204. $handle = opendir($dir);
  205. if (is_resource($handle)) {
  206. while (($file = readdir($handle)) !== false) {
  207. if (strpos($file, 'mod_'.strtolower($myTmpObjectKey).'_') === 0 && substr($file, dol_strlen($file) - 3, 3) == 'php') {
  208. $file = substr($file, 0, dol_strlen($file) - 4);
  209. require_once $dir.'/'.$file.'.php';
  210. $module = new $file($db);
  211. // Show modules according to features level
  212. if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) {
  213. continue;
  214. }
  215. if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) {
  216. continue;
  217. }
  218. if ($module->isEnabled()) {
  219. dol_include_once('/'.$moduledir.'/class/'.strtolower($myTmpObjectKey).'.class.php');
  220. print '<tr class="oddeven"><td>'.$module->name."</td><td>\n";
  221. print $module->info();
  222. print '</td>';
  223. // Show example of numbering model
  224. print '<td class="nowrap">';
  225. $tmp = $module->getExample();
  226. if (preg_match('/^Error/', $tmp)) {
  227. $langs->load("errors");
  228. print '<div class="error">'.$langs->trans($tmp).'</div>';
  229. } elseif ($tmp == 'NotConfigured') {
  230. print $langs->trans($tmp);
  231. } else {
  232. print $tmp;
  233. }
  234. print '</td>'."\n";
  235. print '<td class="center">';
  236. $constforvar = 'WORKSTATION_'.strtoupper($myTmpObjectKey).'_ADDON';
  237. if ($conf->global->$constforvar == $file) {
  238. print img_picto($langs->trans("Activated"), 'switch_on');
  239. } else {
  240. print '<a href="'.$_SERVER["PHP_SELF"].'?action=setmod&token='.newToken().'&object='.strtolower($myTmpObjectKey).'&value='.urlencode($file).'">';
  241. print img_picto($langs->trans("Disabled"), 'switch_off');
  242. print '</a>';
  243. }
  244. print '</td>';
  245. $mytmpinstance = new $myTmpObjectKey($db);
  246. $mytmpinstance->initAsSpecimen();
  247. // Info
  248. $htmltooltip = '';
  249. $htmltooltip .= ''.$langs->trans("Version").': <b>'.$module->getVersion().'</b><br>';
  250. $nextval = $module->getNextValue($mytmpinstance);
  251. if ("$nextval" != $langs->trans("NotAvailable")) { // Keep " on nextval
  252. $htmltooltip .= ''.$langs->trans("NextValue").': ';
  253. if ($nextval) {
  254. if (preg_match('/^Error/', $nextval) || $nextval == 'NotConfigured') {
  255. $nextval = $langs->trans($nextval);
  256. }
  257. $htmltooltip .= $nextval.'<br>';
  258. } else {
  259. $htmltooltip .= $langs->trans($module->error).'<br>';
  260. }
  261. }
  262. print '<td class="center">';
  263. print $form->textwithpicto('', $htmltooltip, 1, 0);
  264. print '</td>';
  265. print "</tr>\n";
  266. }
  267. }
  268. }
  269. closedir($handle);
  270. }
  271. }
  272. }
  273. print "</table><br>\n";
  274. }
  275. if ($myTmpObjectArray['includedocgeneration']) {
  276. /*
  277. * Document templates generators
  278. */
  279. $setupnotempty++;
  280. $type = strtolower($myTmpObjectKey);
  281. print load_fiche_titre($langs->trans("DocumentModules", $myTmpObjectKey), '', '');
  282. // Load array def with activated templates
  283. $def = array();
  284. $sql = "SELECT nom";
  285. $sql .= " FROM ".MAIN_DB_PREFIX."document_model";
  286. $sql .= " WHERE type = '".$db->escape($type)."'";
  287. $sql .= " AND entity = ".$conf->entity;
  288. $resql = $db->query($sql);
  289. if ($resql) {
  290. $i = 0;
  291. $num_rows = $db->num_rows($resql);
  292. while ($i < $num_rows) {
  293. $array = $db->fetch_array($resql);
  294. array_push($def, $array[0]);
  295. $i++;
  296. }
  297. } else {
  298. dol_print_error($db);
  299. }
  300. print "<table class=\"noborder\" width=\"100%\">\n";
  301. print "<tr class=\"liste_titre\">\n";
  302. print '<td>'.$langs->trans("Name").'</td>';
  303. print '<td>'.$langs->trans("Description").'</td>';
  304. print '<td class="center" width="60">'.$langs->trans("Status")."</td>\n";
  305. print '<td class="center" width="60">'.$langs->trans("Default")."</td>\n";
  306. print '<td class="center" width="38">'.$langs->trans("ShortInfo").'</td>';
  307. print '<td class="center" width="38">'.$langs->trans("Preview").'</td>';
  308. print "</tr>\n";
  309. clearstatcache();
  310. foreach ($dirmodels as $reldir) {
  311. foreach (array('', '/doc') as $valdir) {
  312. $realpath = $reldir."core/modules/".$moduledir.$valdir;
  313. $dir = dol_buildpath($realpath);
  314. if (is_dir($dir)) {
  315. $handle = opendir($dir);
  316. if (is_resource($handle)) {
  317. while (($file = readdir($handle)) !== false) {
  318. $filelist[] = $file;
  319. }
  320. closedir($handle);
  321. arsort($filelist);
  322. foreach ($filelist as $file) {
  323. if (preg_match('/\.modules\.php$/i', $file) && preg_match('/^(pdf_|doc_)/', $file)) {
  324. if (file_exists($dir.'/'.$file)) {
  325. $name = substr($file, 4, dol_strlen($file) - 16);
  326. $classname = substr($file, 0, dol_strlen($file) - 12);
  327. require_once $dir.'/'.$file;
  328. $module = new $classname($db);
  329. $modulequalified = 1;
  330. if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) {
  331. $modulequalified = 0;
  332. }
  333. if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) {
  334. $modulequalified = 0;
  335. }
  336. if ($modulequalified) {
  337. print '<tr class="oddeven"><td width="100">';
  338. print (empty($module->name) ? $name : $module->name);
  339. print "</td><td>\n";
  340. if (method_exists($module, 'info')) {
  341. print $module->info($langs);
  342. } else {
  343. print $module->description;
  344. }
  345. print '</td>';
  346. // Active
  347. if (in_array($name, $def)) {
  348. print '<td class="center">'."\n";
  349. print '<a href="'.$_SERVER["PHP_SELF"].'?action=del&amp;token='.newToken().'&amp;value='.$name.'">';
  350. print img_picto($langs->trans("Enabled"), 'switch_on');
  351. print '</a>';
  352. print '</td>';
  353. } else {
  354. print '<td class="center">'."\n";
  355. print '<a href="'.$_SERVER["PHP_SELF"].'?action=set&amp;token='.newToken().'&amp;value='.$name.'&amp;scan_dir='.$module->scandir.'&amp;label='.urlencode($module->name).'">'.img_picto($langs->trans("Disabled"), 'switch_off').'</a>';
  356. print "</td>";
  357. }
  358. // Default
  359. print '<td class="center">';
  360. $constforvar = 'WORKSTATION_'.strtoupper($myTmpObjectKey).'_ADDON';
  361. if ($conf->global->$constforvar == $name) {
  362. print img_picto($langs->trans("Default"), 'on');
  363. } else {
  364. print '<a href="'.$_SERVER["PHP_SELF"].'?action=setdoc&amp;token='.newToken().'&amp;value='.$name.'&amp;scan_dir='.$module->scandir.'&amp;label='.urlencode($module->name).'" alt="'.$langs->trans("Default").'">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
  365. }
  366. print '</td>';
  367. // Info
  368. $htmltooltip = ''.$langs->trans("Name").': '.$module->name;
  369. $htmltooltip .= '<br>'.$langs->trans("Type").': '.($module->type ? $module->type : $langs->trans("Unknown"));
  370. if ($module->type == 'pdf') {
  371. $htmltooltip .= '<br>'.$langs->trans("Width").'/'.$langs->trans("Height").': '.$module->page_largeur.'/'.$module->page_hauteur;
  372. }
  373. $htmltooltip .= '<br>'.$langs->trans("Path").': '.preg_replace('/^\//', '', $realpath).'/'.$file;
  374. $htmltooltip .= '<br><br><u>'.$langs->trans("FeaturesSupported").':</u>';
  375. $htmltooltip .= '<br>'.$langs->trans("Logo").': '.yn($module->option_logo, 1, 1);
  376. $htmltooltip .= '<br>'.$langs->trans("MultiLanguage").': '.yn($module->option_multilang, 1, 1);
  377. print '<td class="center">';
  378. print $form->textwithpicto('', $htmltooltip, 1, 0);
  379. print '</td>';
  380. // Preview
  381. print '<td class="center">';
  382. if ($module->type == 'pdf') {
  383. print '<a href="'.$_SERVER["PHP_SELF"].'?action=specimen&module='.$name.'&object='.$myTmpObjectKey.'">'.img_object($langs->trans("Preview"), 'generic').'</a>';
  384. } else {
  385. print img_object($langs->trans("PreviewNotAvailable"), 'generic');
  386. }
  387. print '</td>';
  388. print "</tr>\n";
  389. }
  390. }
  391. }
  392. }
  393. }
  394. }
  395. }
  396. }
  397. print '</table>';
  398. }
  399. }
  400. /*if (empty($setupnotempty)) {
  401. print '<br>'.$langs->trans("NothingToSetup");
  402. }*/
  403. // Page end
  404. print dol_get_fiche_end();
  405. llxFooter();
  406. $db->close();