initmodule.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * Copyright (C) 2005-2013 Laurent Destailleur <eldy@users.sourceforge.net>
  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 <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file scripts/modulebuilder/initmodule.php
  21. * \ingroup modulebuilder
  22. * \brief Script to initialize a module.
  23. */
  24. $sapi_type = php_sapi_name();
  25. $script_file = basename(__FILE__);
  26. $path=dirname(__FILE__).'/';
  27. // Test if batch mode
  28. if (substr($sapi_type, 0, 3) == 'cgi') {
  29. echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  30. exit(-1);
  31. }
  32. if (! isset($argv[1]) || ! $argv[1]) {
  33. print "Usage: ".$script_file." ModuleName\n";
  34. exit(-1);
  35. }
  36. $modulename=$argv[1];
  37. require_once $path."../../htdocs/master.inc.php";
  38. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  39. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  40. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
  41. require_once DOL_DOCUMENT_ROOT.'/core/lib/modulebuilder.lib.php';
  42. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  43. $langs->loadLangs(array("admin", "modulebuilder", "other", "cron"));
  44. // Global variables
  45. $version=DOL_VERSION;
  46. $error=0;
  47. // Dir for custom dirs
  48. $tmp=explode(',', $dolibarr_main_document_root_alt);
  49. $dirins = $tmp[0];
  50. $dirread = $dirins;
  51. $forceddirread = 0;
  52. $tmpdir = explode('@', $module);
  53. if (! empty($tmpdir[1]))
  54. {
  55. $module=$tmpdir[0];
  56. $dirread=$tmpdir[1];
  57. $forceddirread=1;
  58. }
  59. $FILEFLAG='modulebuilder.txt';
  60. $now=dol_now();
  61. $newmask = 0;
  62. if (empty($newmask) && ! empty($conf->global->MAIN_UMASK)) $newmask=$conf->global->MAIN_UMASK;
  63. if (empty($newmask)) // This should no happen
  64. {
  65. $newmask='0664';
  66. }
  67. /*
  68. * Main
  69. */
  70. @set_time_limit(0);
  71. print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
  72. print "modulename=".$modulename."\n";
  73. print "dirins=".$dirins."\n";
  74. if (preg_match('/[^a-z0-9_]/i', $modulename))
  75. {
  76. $error++;
  77. print 'Error '.$langs->trans("SpaceOrSpecialCharAreNotAllowed")."\n";
  78. exit(1);
  79. }
  80. if (! $error)
  81. {
  82. $srcdir = DOL_DOCUMENT_ROOT.'/modulebuilder/template';
  83. $destdir = $dirins.'/'.strtolower($modulename);
  84. $arrayreplacement=array(
  85. 'mymodule'=>strtolower($modulename),
  86. 'MyModule'=>$modulename
  87. );
  88. $result = dolCopyDir($srcdir, $destdir, 0, 0, $arrayreplacement);
  89. //dol_mkdir($destfile);
  90. if ($result <= 0)
  91. {
  92. if ($result < 0)
  93. {
  94. $error++;
  95. $langs->load("errors");
  96. print $langs->trans("ErrorFailToCopyDir", $srcdir, $destdir)."\n";
  97. exit(2);
  98. }
  99. else // $result == 0
  100. {
  101. print $langs->trans("AllFilesDidAlreadyExist", $srcdir, $destdir)."\n";
  102. }
  103. }
  104. // Delete some files
  105. dol_delete_file($destdir.'/myobject_card.php');
  106. dol_delete_file($destdir.'/myobject_note.php');
  107. dol_delete_file($destdir.'/myobject_document.php');
  108. dol_delete_file($destdir.'/myobject_agenda.php');
  109. dol_delete_file($destdir.'/myobject_list.php');
  110. dol_delete_file($destdir.'/lib/myobject.lib.php');
  111. dol_delete_file($destdir.'/test/phpunit/MyObjectTest.php');
  112. dol_delete_file($destdir.'/sql/llx_mymodule_myobject.sql');
  113. dol_delete_file($destdir.'/sql/llx_mymodule_myobject_extrafields.sql');
  114. dol_delete_file($destdir.'/sql/llx_mymodule_myobject.key.sql');
  115. dol_delete_file($destdir.'/scripts/myobject.php');
  116. dol_delete_file($destdir.'/img/object_myobject.png');
  117. dol_delete_file($destdir.'/class/myobject.class.php');
  118. dol_delete_file($destdir.'/class/api_mymodule.class.php');
  119. }
  120. // Edit PHP files
  121. if (! $error)
  122. {
  123. $listofphpfilestoedit = dol_dir_list($destdir, 'files', 1, '\.(php|MD|js|sql|txt|xml|lang)$', '', 'fullname', SORT_ASC, 0, 1);
  124. foreach($listofphpfilestoedit as $phpfileval)
  125. {
  126. //var_dump($phpfileval['fullname']);
  127. $arrayreplacement=array(
  128. 'mymodule'=>strtolower($modulename),
  129. 'MyModule'=>$modulename,
  130. 'MYMODULE'=>strtoupper($modulename),
  131. 'My module'=>$modulename,
  132. 'my module'=>$modulename,
  133. 'Mon module'=>$modulename,
  134. 'mon module'=>$modulename,
  135. 'htdocs/modulebuilder/template'=>strtolower($modulename),
  136. '---Put here your own copyright and developer email---'=>dol_print_date($now,'%Y').' '.$user->getFullName($langs).($user->email?' <'.$user->email.'>':'')
  137. );
  138. $result=dolReplaceInFile($phpfileval['fullname'], $arrayreplacement);
  139. //var_dump($result);
  140. if ($result < 0)
  141. {
  142. print $langs->trans("ErrorFailToMakeReplacementInto", $phpfileval['fullname'])."\n";
  143. exit(3);
  144. }
  145. }
  146. }
  147. print 'Module initialized'."\n";
  148. exit(0);