myscript.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #!/usr/bin/env php
  2. <?php
  3. /* <one line to give the program's name and a brief idea of what it does.>
  4. * Copyright (C) <year> <name of author>
  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/myscript.php
  21. * \ingroup mymodule
  22. * \brief Example command line script.
  23. *
  24. * Put detailed description here.
  25. */
  26. $sapi_type = php_sapi_name();
  27. $script_file = basename(__FILE__);
  28. $path = dirname(__FILE__) . '/';
  29. // Test if batch mode
  30. if (substr($sapi_type, 0, 3) == 'cgi') {
  31. echo "Error: You are using PHP for CGI. To execute ";
  32. echo $script_file;
  33. echo " from command line, you must use PHP for CLI mode.\n";
  34. exit;
  35. }
  36. // Global variables
  37. $version = '1.0.0';
  38. $error = 0;
  39. /*
  40. * -------------------- YOUR CODE STARTS HERE --------------------
  41. */
  42. /* Set this define to 0 if you want to allow execution of your script
  43. * even if dolibarr setup is "locked to admin user only". */
  44. define('EVEN_IF_ONLY_LOGIN_ALLOWED', 0);
  45. /* Include Dolibarr environment
  46. * Customize to your needs
  47. */
  48. require_once $path . '../../../master.inc.php';
  49. /* After this $db, $conf, $langs, $mysoc, $user and other Dolibarr utility variables should be defined.
  50. * Warning: this still requires a valid htdocs/conf.php file
  51. */
  52. global $conf, $db, $langs, $mysoc, $user;
  53. // No timeout for this script
  54. @set_time_limit(0);
  55. // Set the default language
  56. //$langs->setDefaultLang('en_US');
  57. // Load translations for the default language
  58. $langs->load("main");
  59. /* User and permissions loading
  60. * Loads user for login 'admin'.
  61. * Comment out to run as anonymous user. */
  62. $result = $user->fetch('', 'admin');
  63. if (! $result > 0) {
  64. dol_print_error('', $user->error);
  65. exit;
  66. }
  67. $user->getrights();
  68. // Display banner and help
  69. echo "***** " . $script_file . " (" . $version . ") pid=" . getmypid() . " *****\n";
  70. dol_syslog($script_file . " launched with arg " . join(',', $argv));
  71. if (! isset($argv[1])) {
  72. // Check parameters
  73. echo "Usage: " . $script_file . " param1 param2 ...\n";
  74. exit;
  75. }
  76. echo '--- start' . "\n";
  77. echo 'Argument 1=' . $argv[1] . "\n";
  78. echo 'Argument 2=' . $argv[2] . "\n";
  79. // Start database transaction
  80. $db->begin();
  81. // Examples for manipulating a class
  82. require_once '../class/myclass.class.php';
  83. $myobject = new MyClass($db);
  84. // Example for inserting creating object in database
  85. /*
  86. dol_syslog($script_file . " CREATE", LOG_DEBUG);
  87. $myobject->prop1 = 'value_prop1';
  88. $myobject->prop2 = 'value_prop2';
  89. $id = $myobject->create($user);
  90. if ($id < 0) {
  91. $error++;
  92. dol_print_error($db, $myobject->error);
  93. } else {
  94. echo "Object created with id=" . $id . "\n";
  95. }
  96. */
  97. // Example for reading object from database
  98. /*
  99. dol_syslog($script_file . " FETCH", LOG_DEBUG);
  100. $result = $myobject->fetch($id);
  101. if ($result < 0) {
  102. $error;
  103. dol_print_error($db, $myobject->error);
  104. } else {
  105. echo "Object with id=" . $id . " loaded\n";
  106. }
  107. */
  108. // Example for updating object in database
  109. // ($myobject must have been loaded by a fetch before)
  110. /*
  111. dol_syslog($script_file . " UPDATE", LOG_DEBUG);
  112. $myobject->prop1 = 'newvalue_prop1';
  113. $myobject->prop2 = 'newvalue_prop2';
  114. $result = $myobject->update($user);
  115. if ($result < 0) {
  116. $error++;
  117. dol_print_error($db, $myobject->error);
  118. } else {
  119. echo "Object with id " . $myobject->id . " updated\n";
  120. }
  121. */
  122. // Example for deleting object in database
  123. // ($myobject must have been loaded by a fetch before)
  124. /*
  125. dol_syslog($script_file . " DELETE", LOG_DEBUG);
  126. $result = $myobject->delete($user);
  127. if ($result < 0) {
  128. $error++;
  129. dol_print_error($db, $myobject->error);
  130. } else {
  131. echo "Object with id " . $myobject->id . " deleted\n";
  132. }
  133. */
  134. // An example of a direct SQL read without using the fetch method
  135. /*
  136. $sql = "SELECT field1, field2";
  137. $sql.= " FROM " . MAIN_DB_PREFIX . "c_pays";
  138. $sql.= " WHERE field3 = 'xxx'";
  139. $sql.= " ORDER BY field1 ASC";
  140. dol_syslog($script_file . " sql=" . $sql, LOG_DEBUG);
  141. $resql=$db->query($sql);
  142. if ($resql) {
  143. $num = $db->num_rows($resql);
  144. $i = 0;
  145. if ($num) {
  146. while ($i < $num) {
  147. $obj = $db->fetch_object($resql);
  148. if ($obj) {
  149. // You can use here results
  150. echo $obj->field1;
  151. echo $obj->field2;
  152. }
  153. $i++;
  154. }
  155. }
  156. } else {
  157. $error++;
  158. dol_print_error($db);
  159. }
  160. */
  161. /*
  162. * --------------------- YOUR CODE ENDS HERE ----------------------
  163. */
  164. // Error management
  165. if (! $error) {
  166. $db->commit();
  167. echo '--- end ok' . "\n";
  168. $exit_status = 0; // UNIX no errors exit status
  169. } else {
  170. echo '--- end error code=' . $error . "\n";
  171. $db->rollback();
  172. $exit_status = 1; // UNIX general error exit status
  173. }
  174. // Close database handler
  175. $db->close();
  176. // Return exit status code
  177. return $exit_status;