stocktransfer.php 16 KB

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