stocktransfer.php 17 KB

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