stocktransfer.php 17 KB

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