workstation.php 16 KB

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