setup.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. <?php
  2. /* Copyright (C) 2004-2017 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2022 Alice Adminson <aadminson@example.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, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file bookcal/admin/setup.php
  20. * \ingroup bookcal
  21. * \brief BookCal setup page.
  22. */
  23. // Load Dolibarr environment
  24. require '../../main.inc.php';
  25. global $langs, $user;
  26. // Libraries
  27. require_once DOL_DOCUMENT_ROOT."/core/lib/admin.lib.php";
  28. require_once '../lib/bookcal.lib.php';
  29. //require_once "../class/myclass.class.php";
  30. // Translations
  31. $langs->loadLangs(array("admin", "agenda"));
  32. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  33. $hookmanager->initHooks(array('bookcalsetup', 'globalsetup'));
  34. // Access control
  35. if (!$user->admin) {
  36. accessforbidden();
  37. }
  38. // Parameters
  39. $action = GETPOST('action', 'aZ09');
  40. $backtopage = GETPOST('backtopage', 'alpha');
  41. $modulepart = GETPOST('modulepart', 'aZ09'); // Used by actions_setmoduleoptions.inc.php
  42. $value = GETPOST('value', 'alpha');
  43. $label = GETPOST('label', 'alpha');
  44. $scandir = GETPOST('scan_dir', 'alpha');
  45. $type = 'myobject';
  46. $error = 0;
  47. $setupnotempty = 0;
  48. // Set this to 1 to use the factory to manage constants. Warning, the generated module will be compatible with version v15+ only
  49. $useFormSetup = 1;
  50. if (!class_exists('FormSetup')) {
  51. // For retrocompatibility Dolibarr < 16.0
  52. if (floatval(DOL_VERSION) < 16.0 && !class_exists('FormSetup')) {
  53. require_once __DIR__.'/../backport/v16/core/class/html.formsetup.class.php';
  54. } else {
  55. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formsetup.class.php';
  56. }
  57. }
  58. $formSetup = new FormSetup($db);
  59. // Setup conf BOOKCAL_PUBLIC_INTERFACE_TOPIC
  60. $item = $formSetup->newItem('BOOKCAL_PUBLIC_INTERFACE_TOPIC');
  61. $item->defaultFieldValue = 'MyBigCompany public interface for Bookcal';
  62. /*// Setup conf BOOKCAL_MYPARAM8
  63. $item = $formSetup->newItem('BOOKCAL_MYPARAM8');
  64. $TField = array(
  65. 'test01' => $langs->trans('test01'),
  66. 'test02' => $langs->trans('test02'),
  67. 'test03' => $langs->trans('test03'),
  68. 'test04' => $langs->trans('test04'),
  69. 'test05' => $langs->trans('test05'),
  70. 'test06' => $langs->trans('test06'),
  71. );
  72. $item->setAsMultiSelect($TField);
  73. $item->helpText = $langs->transnoentities('BOOKCAL_MYPARAM8');
  74. // Setup conf BOOKCAL_MYPARAM9
  75. $formSetup->newItem('BOOKCAL_MYPARAM9')->setAsSelect($TField);
  76. // Setup conf BOOKCAL_MYPARAM10
  77. $item = $formSetup->newItem('BOOKCAL_MYPARAM10');
  78. $item->setAsColor();
  79. $item->defaultFieldValue = '#FF0000';
  80. $item->nameText = $item->getNameText().' more html text ';
  81. $item->fieldInputOverride = '';
  82. $item->helpText = $langs->transnoentities('AnHelpMessage');*/
  83. //$item->fieldValue = '';
  84. //$item->fieldAttr = array() ; // fields attribute only for compatible fields like input text
  85. //$item->fieldOverride = false; // set this var to override field output will override $fieldInputOverride and $fieldOutputOverride too
  86. //$item->fieldInputOverride = false; // set this var to override field input
  87. //$item->fieldOutputOverride = false; // set this var to override field output
  88. $setupnotempty =+ count($formSetup->items);
  89. $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
  90. /*
  91. * Actions
  92. */
  93. // For retrocompatibility Dolibarr < 15.0
  94. if ( versioncompare(explode('.', DOL_VERSION), array(15)) < 0 && $action == 'update' && !empty($user->admin)) {
  95. $formSetup->saveConfFromPost();
  96. }
  97. include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php';
  98. if ($action == 'updateMask') {
  99. $maskconst = GETPOST('maskconst', 'aZ09');
  100. $maskvalue = GETPOST('maskvalue', 'alpha');
  101. if ($maskconst && preg_match('/_MASK$/', $maskconst)) {
  102. $res = dolibarr_set_const($db, $maskconst, $maskvalue, 'chaine', 0, '', $conf->entity);
  103. if (!($res > 0)) {
  104. $error++;
  105. }
  106. }
  107. if (!$error) {
  108. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  109. } else {
  110. setEventMessages($langs->trans("Error"), null, 'errors');
  111. }
  112. } elseif ($action == 'specimen') {
  113. $modele = GETPOST('module', 'alpha');
  114. $tmpobjectkey = GETPOST('object');
  115. $tmpobject = new $tmpobjectkey($db);
  116. $tmpobject->initAsSpecimen();
  117. // Search template files
  118. $file = ''; $classname = ''; $filefound = 0;
  119. $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
  120. foreach ($dirmodels as $reldir) {
  121. $file = dol_buildpath($reldir."core/modules/bookcal/doc/pdf_".$modele."_".strtolower($tmpobjectkey).".modules.php", 0);
  122. if (file_exists($file)) {
  123. $filefound = 1;
  124. $classname = "pdf_".$modele."_".strtolower($tmpobjectkey);
  125. break;
  126. }
  127. }
  128. if ($filefound) {
  129. require_once $file;
  130. $module = new $classname($db);
  131. if ($module->write_file($tmpobject, $langs) > 0) {
  132. header("Location: ".DOL_URL_ROOT."/document.php?modulepart=bookcal-".strtolower($tmpobjectkey)."&file=SPECIMEN.pdf");
  133. return;
  134. } else {
  135. setEventMessages($module->error, null, 'errors');
  136. dol_syslog($module->error, LOG_ERR);
  137. }
  138. } else {
  139. setEventMessages($langs->trans("ErrorModuleNotFound"), null, 'errors');
  140. dol_syslog($langs->trans("ErrorModuleNotFound"), LOG_ERR);
  141. }
  142. } elseif ($action == 'setmod') {
  143. // TODO Check if numbering module chosen can be activated by calling method canBeActivated
  144. $tmpobjectkey = GETPOST('object');
  145. if (!empty($tmpobjectkey)) {
  146. $constforval = 'BOOKCAL_'.strtoupper($tmpobjectkey)."_ADDON";
  147. dolibarr_set_const($db, $constforval, $value, 'chaine', 0, '', $conf->entity);
  148. }
  149. } elseif ($action == 'set') {
  150. // Activate a model
  151. $ret = addDocumentModel($value, $type, $label, $scandir);
  152. } elseif ($action == 'del') {
  153. $ret = delDocumentModel($value, $type);
  154. if ($ret > 0) {
  155. $tmpobjectkey = GETPOST('object');
  156. if (!empty($tmpobjectkey)) {
  157. $constforval = 'BOOKCAL_'.strtoupper($tmpobjectkey).'_ADDON_PDF';
  158. if ($conf->global->$constforval == "$value") {
  159. dolibarr_del_const($db, $constforval, $conf->entity);
  160. }
  161. }
  162. }
  163. } elseif ($action == 'setdoc') {
  164. // Set or unset default model
  165. $tmpobjectkey = GETPOST('object');
  166. if (!empty($tmpobjectkey)) {
  167. $constforval = 'BOOKCAL_'.strtoupper($tmpobjectkey).'_ADDON_PDF';
  168. if (dolibarr_set_const($db, $constforval, $value, 'chaine', 0, '', $conf->entity)) {
  169. // The constant that was read before the new set
  170. // We therefore requires a variable to have a coherent view
  171. $conf->global->$constforval = $value;
  172. }
  173. // We disable/enable the document template (into llx_document_model table)
  174. $ret = delDocumentModel($value, $type);
  175. if ($ret > 0) {
  176. $ret = addDocumentModel($value, $type, $label, $scandir);
  177. }
  178. }
  179. } elseif ($action == 'unsetdoc') {
  180. $tmpobjectkey = GETPOST('object');
  181. if (!empty($tmpobjectkey)) {
  182. $constforval = 'BOOKCAL_'.strtoupper($tmpobjectkey).'_ADDON_PDF';
  183. dolibarr_del_const($db, $constforval, $conf->entity);
  184. }
  185. }
  186. /*
  187. * View
  188. */
  189. $form = new Form($db);
  190. $help_url = '';
  191. $page_name = "BookCalSetup";
  192. llxHeader('', $langs->trans($page_name), $help_url);
  193. // Subheader
  194. $linkback = '<a href="'.($backtopage ? $backtopage : DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1').'">'.$langs->trans("BackToModuleList").'</a>';
  195. print load_fiche_titre($langs->trans($page_name), $linkback, 'title_setup');
  196. // Configuration header
  197. $head = bookcalAdminPrepareHead();
  198. print dol_get_fiche_head($head, 'settings', $langs->trans($page_name), -1, "fa-calendar-check");
  199. // Setup page goes here
  200. echo '<span class="opacitymedium">'.$langs->trans("BookCalSetupPage").'</span><br><br>';
  201. if ($action == 'edit') {
  202. print $formSetup->generateOutput(true);
  203. print '<br>';
  204. } elseif (!empty($formSetup->items)) {
  205. print $formSetup->generateOutput();
  206. print '<div class="tabsAction">';
  207. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=edit&token='.newToken().'">'.$langs->trans("Modify").'</a>';
  208. print '</div>';
  209. } else {
  210. print '<br>'.$langs->trans("NothingToSetup");
  211. }
  212. $moduledir = 'bookcal';
  213. $myTmpObjects = array();
  214. $myTmpObjects['Booking'] = array('includerefgeneration'=>1, 'includedocgeneration'=>0);
  215. foreach ($myTmpObjects as $myTmpObjectKey => $myTmpObjectArray) {
  216. if ($myTmpObjectKey == 'MyObject') {
  217. continue;
  218. }
  219. if ($myTmpObjectArray['includerefgeneration']) {
  220. /*
  221. * Orders Numbering model
  222. */
  223. $setupnotempty++;
  224. print load_fiche_titre($langs->trans("NumberingModules", $myTmpObjectKey), '', '');
  225. print '<table class="noborder centpercent">';
  226. print '<tr class="liste_titre">';
  227. print '<td>'.$langs->trans("Name").'</td>';
  228. print '<td>'.$langs->trans("Description").'</td>';
  229. print '<td class="nowrap">'.$langs->trans("Example").'</td>';
  230. print '<td class="center" width="60">'.$langs->trans("Status").'</td>';
  231. print '<td class="center" width="16">'.$langs->trans("ShortInfo").'</td>';
  232. print '</tr>'."\n";
  233. clearstatcache();
  234. foreach ($dirmodels as $reldir) {
  235. $dir = dol_buildpath($reldir."core/modules/".$moduledir);
  236. if (is_dir($dir)) {
  237. $handle = opendir($dir);
  238. if (is_resource($handle)) {
  239. while (($file = readdir($handle)) !== false) {
  240. if (strpos($file, 'mod_'.strtolower($myTmpObjectKey).'_') === 0 && substr($file, dol_strlen($file) - 3, 3) == 'php') {
  241. $file = substr($file, 0, dol_strlen($file) - 4);
  242. require_once $dir.'/'.$file.'.php';
  243. $module = new $file($db);
  244. // Show modules according to features level
  245. if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) {
  246. continue;
  247. }
  248. if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) {
  249. continue;
  250. }
  251. if ($module->isEnabled()) {
  252. dol_include_once('/'.$moduledir.'/class/'.strtolower($myTmpObjectKey).'.class.php');
  253. print '<tr class="oddeven"><td>'.$module->name."</td><td>\n";
  254. print $module->info($langs);
  255. print '</td>';
  256. // Show example of numbering model
  257. print '<td class="nowrap">';
  258. $tmp = $module->getExample();
  259. if (preg_match('/^Error/', $tmp)) {
  260. $langs->load("errors");
  261. print '<div class="error">'.$langs->trans($tmp).'</div>';
  262. } elseif ($tmp == 'NotConfigured') {
  263. print $langs->trans($tmp);
  264. } else {
  265. print $tmp;
  266. }
  267. print '</td>'."\n";
  268. print '<td class="center">';
  269. $constforvar = 'BOOKCAL_'.strtoupper($myTmpObjectKey).'_ADDON';
  270. if (getDolGlobalString($constforvar) == $file) {
  271. print img_picto($langs->trans("Activated"), 'switch_on');
  272. } else {
  273. print '<a href="'.$_SERVER["PHP_SELF"].'?action=setmod&token='.newToken().'&object='.strtolower($myTmpObjectKey).'&value='.urlencode($file).'">';
  274. print img_picto($langs->trans("Disabled"), 'switch_off');
  275. print '</a>';
  276. }
  277. print '</td>';
  278. $mytmpinstance = new $myTmpObjectKey($db);
  279. $mytmpinstance->initAsSpecimen();
  280. // Info
  281. $htmltooltip = '';
  282. $htmltooltip .= ''.$langs->trans("Version").': <b>'.$module->getVersion().'</b><br>';
  283. $nextval = $module->getNextValue($mytmpinstance);
  284. if ("$nextval" != $langs->trans("NotAvailable")) { // Keep " on nextval
  285. $htmltooltip .= ''.$langs->trans("NextValue").': ';
  286. if ($nextval) {
  287. if (preg_match('/^Error/', $nextval) || $nextval == 'NotConfigured') {
  288. $nextval = $langs->trans($nextval);
  289. }
  290. $htmltooltip .= $nextval.'<br>';
  291. } else {
  292. $htmltooltip .= $langs->trans($module->error).'<br>';
  293. }
  294. }
  295. print '<td class="center">';
  296. print $form->textwithpicto('', $htmltooltip, 1, 0);
  297. print '</td>';
  298. print "</tr>\n";
  299. }
  300. }
  301. }
  302. closedir($handle);
  303. }
  304. }
  305. }
  306. print "</table><br>\n";
  307. }
  308. if ($myTmpObjectArray['includedocgeneration']) {
  309. /*
  310. * Document templates generators
  311. */
  312. $setupnotempty++;
  313. $type = strtolower($myTmpObjectKey);
  314. print load_fiche_titre($langs->trans("DocumentModules", $myTmpObjectKey), '', '');
  315. // Load array def with activated templates
  316. $def = array();
  317. $sql = "SELECT nom";
  318. $sql .= " FROM ".MAIN_DB_PREFIX."document_model";
  319. $sql .= " WHERE type = '".$db->escape($type)."'";
  320. $sql .= " AND entity = ".$conf->entity;
  321. $resql = $db->query($sql);
  322. if ($resql) {
  323. $i = 0;
  324. $num_rows = $db->num_rows($resql);
  325. while ($i < $num_rows) {
  326. $array = $db->fetch_array($resql);
  327. array_push($def, $array[0]);
  328. $i++;
  329. }
  330. } else {
  331. dol_print_error($db);
  332. }
  333. print "<table class=\"noborder\" width=\"100%\">\n";
  334. print "<tr class=\"liste_titre\">\n";
  335. print '<td>'.$langs->trans("Name").'</td>';
  336. print '<td>'.$langs->trans("Description").'</td>';
  337. print '<td class="center" width="60">'.$langs->trans("Status")."</td>\n";
  338. print '<td class="center" width="60">'.$langs->trans("Default")."</td>\n";
  339. print '<td class="center" width="38">'.$langs->trans("ShortInfo").'</td>';
  340. print '<td class="center" width="38">'.$langs->trans("Preview").'</td>';
  341. print "</tr>\n";
  342. clearstatcache();
  343. foreach ($dirmodels as $reldir) {
  344. foreach (array('', '/doc') as $valdir) {
  345. $realpath = $reldir."core/modules/".$moduledir.$valdir;
  346. $dir = dol_buildpath($realpath);
  347. if (is_dir($dir)) {
  348. $handle = opendir($dir);
  349. if (is_resource($handle)) {
  350. while (($file = readdir($handle)) !== false) {
  351. $filelist[] = $file;
  352. }
  353. closedir($handle);
  354. arsort($filelist);
  355. foreach ($filelist as $file) {
  356. if (preg_match('/\.modules\.php$/i', $file) && preg_match('/^(pdf_|doc_)/', $file)) {
  357. if (file_exists($dir.'/'.$file)) {
  358. $name = substr($file, 4, dol_strlen($file) - 16);
  359. $classname = substr($file, 0, dol_strlen($file) - 12);
  360. require_once $dir.'/'.$file;
  361. $module = new $classname($db);
  362. $modulequalified = 1;
  363. if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) {
  364. $modulequalified = 0;
  365. }
  366. if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) {
  367. $modulequalified = 0;
  368. }
  369. if ($modulequalified) {
  370. print '<tr class="oddeven"><td width="100">';
  371. print (empty($module->name) ? $name : $module->name);
  372. print "</td><td>\n";
  373. if (method_exists($module, 'info')) {
  374. print $module->info($langs);
  375. } else {
  376. print $module->description;
  377. }
  378. print '</td>';
  379. // Active
  380. if (in_array($name, $def)) {
  381. print '<td class="center">'."\n";
  382. print '<a href="'.$_SERVER["PHP_SELF"].'?action=del&token='.newToken().'&value='.urlencode($name).'">';
  383. print img_picto($langs->trans("Enabled"), 'switch_on');
  384. print '</a>';
  385. print '</td>';
  386. } else {
  387. print '<td class="center">'."\n";
  388. 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>';
  389. print "</td>";
  390. }
  391. // Default
  392. print '<td class="center">';
  393. $constforvar = 'BOOKCAL_'.strtoupper($myTmpObjectKey).'_ADDON';
  394. if (getDolGlobalString($constforvar) == $name) {
  395. //print img_picto($langs->trans("Default"), 'on');
  396. // Even if choice is the default value, we allow to disable it. Replace this with previous line if you need to disable unset
  397. print '<a href="'.$_SERVER["PHP_SELF"].'?action=unsetdoc&token='.newToken().'&object='.urlencode(strtolower($myTmpObjectKey)).'&value='.urlencode($name).'&scan_dir='.urlencode($module->scandir).'&label='.urlencode($module->name).'&amp;type='.urlencode($type).'" alt="'.$langs->trans("Disable").'">'.img_picto($langs->trans("Enabled"), 'on').'</a>';
  398. } else {
  399. print '<a href="'.$_SERVER["PHP_SELF"].'?action=setdoc&token='.newToken().'&object='.urlencode(strtolower($myTmpObjectKey)).'&value='.urlencode($name).'&scan_dir='.urlencode($module->scandir).'&label='.urlencode($module->name).'" alt="'.$langs->trans("Default").'">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
  400. }
  401. print '</td>';
  402. // Info
  403. $htmltooltip = ''.$langs->trans("Name").': '.$module->name;
  404. $htmltooltip .= '<br>'.$langs->trans("Type").': '.($module->type ? $module->type : $langs->trans("Unknown"));
  405. if ($module->type == 'pdf') {
  406. $htmltooltip .= '<br>'.$langs->trans("Width").'/'.$langs->trans("Height").': '.$module->page_largeur.'/'.$module->page_hauteur;
  407. }
  408. $htmltooltip .= '<br>'.$langs->trans("Path").': '.preg_replace('/^\//', '', $realpath).'/'.$file;
  409. $htmltooltip .= '<br><br><u>'.$langs->trans("FeaturesSupported").':</u>';
  410. $htmltooltip .= '<br>'.$langs->trans("Logo").': '.yn($module->option_logo, 1, 1);
  411. $htmltooltip .= '<br>'.$langs->trans("MultiLanguage").': '.yn($module->option_multilang, 1, 1);
  412. print '<td class="center">';
  413. print $form->textwithpicto('', $htmltooltip, 1, 0);
  414. print '</td>';
  415. // Preview
  416. print '<td class="center">';
  417. if ($module->type == 'pdf') {
  418. $newname = preg_replace('/_'.preg_quote(strtolower($myTmpObjectKey), '/').'/', '', $name);
  419. print '<a href="'.$_SERVER["PHP_SELF"].'?action=specimen&module='.urlencode($newname).'&object='.urlencode($myTmpObjectKey).'">'.img_object($langs->trans("Preview"), 'pdf').'</a>';
  420. } else {
  421. print img_object($langs->trans("PreviewNotAvailable"), 'generic');
  422. }
  423. print '</td>';
  424. print "</tr>\n";
  425. }
  426. }
  427. }
  428. }
  429. }
  430. }
  431. }
  432. }
  433. print '</table>';
  434. }
  435. }
  436. if (empty($setupnotempty)) {
  437. print '<br>'.$langs->trans("NothingToSetup");
  438. }
  439. // Page end
  440. print dol_get_fiche_end();
  441. llxFooter();
  442. $db->close();