You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

myscript.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #!/usr/bin/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 photocopies
  22. * \brief This file is an example command line script
  23. * Put some comments here
  24. */
  25. $sapi_type = php_sapi_name();
  26. $script_file = basename(__FILE__);
  27. $path = dirname(__FILE__) . '/';
  28. // Test if batch mode
  29. if (substr($sapi_type, 0, 3) == 'cgi') {
  30. echo "Error: You are using PHP for CGI. To execute ";
  31. echo $script_file;
  32. echo " from command line, you must use PHP for CLI mode.\n";
  33. exit;
  34. }
  35. // Global variables
  36. $version = '1.29';
  37. $error = 0;
  38. // -------------------- START OF YOUR CODE HERE --------------------
  39. // Include Dolibarr environment
  40. require_once $path . "../../htdocs/master.inc.php";
  41. // After this $db, $mysoc, $langs and $conf->entity are defined.
  42. // Opened handler to database will be closed at end of file.
  43. //$langs->setDefaultLang('en_US'); // To change default language of $langs
  44. $langs->load("main"); // To load language file for default language
  45. @set_time_limit(0); // No timeout for this script
  46. // Load user and its permissions
  47. // Load user for login 'admin'. Comment line to run as anonymous user.
  48. $result = $user->fetch('', 'admin');
  49. if (! $result > 0) {
  50. dol_print_error('', $user->error);
  51. exit;
  52. }
  53. $user->getrights();
  54. echo "***** " . $script_file . " (" . $version . ") *****\n";
  55. if (! isset($argv[1])) {
  56. // Check parameters
  57. echo "Usage: " . $script_file . " param1 param2 ...\n";
  58. exit;
  59. }
  60. echo '--- start' . "\n";
  61. echo 'Argument 1=' . $argv[1] . "\n";
  62. echo 'Argument 2=' . $argv[2] . "\n";
  63. // Start of transaction
  64. $db->begin();
  65. // Examples for manipulating class skeletonclass
  66. require_once DOL_DOCUMENT_ROOT . "/photocopies/myclass.class.php";
  67. $myobject = new SkeletonClass($db);
  68. // Example for inserting creating object in database
  69. /*
  70. dol_syslog($script_file." CREATE", LOG_DEBUG);
  71. $myobject->prop1='value_prop1';
  72. $myobject->prop2='value_prop2';
  73. $id=$myobject->create($user);
  74. if ($id < 0) { $error++; dol_print_error($db,$myobject->error); } else echo "Object created with id=".$id."\n";
  75. */
  76. // Example for reading object from database
  77. /*
  78. dol_syslog($script_file." FETCH", LOG_DEBUG);
  79. $result=$myobject->fetch($id);
  80. if ($result < 0) { $error; dol_print_error($db,$myobject->error); } else echo "Object with id=".$id." loaded\n";
  81. */
  82. // Example for updating object in database
  83. // ($myobject must have been loaded by a fetch before)
  84. /*
  85. dol_syslog($script_file." UPDATE", LOG_DEBUG);
  86. $myobject->prop1='newvalue_prop1';
  87. $myobject->prop2='newvalue_prop2';
  88. $result=$myobject->update($user);
  89. if ($result < 0) { $error++; dol_print_error($db,$myobject->error); } else echo "Object with id ".$myobject->id." updated\n";
  90. */
  91. // Example for deleting object in database
  92. // ($myobject must have been loaded by a fetch before)
  93. /*
  94. dol_syslog($script_file." DELETE", LOG_DEBUG);
  95. $result=$myobject->delete($user);
  96. if ($result < 0) { $error++; dol_print_error($db,$myobject->error); } else echo "Object with id ".$myobject->id." deleted\n";
  97. */
  98. // An example of a direct SQL read without using the fetch method
  99. /*
  100. $sql = "SELECT field1, field2";
  101. $sql.= " FROM ".MAIN_DB_PREFIX."c_pays";
  102. $sql.= " WHERE field3 = 'xxx'";
  103. $sql.= " ORDER BY field1 ASC";
  104. dol_syslog($script_file." sql=".$sql, LOG_DEBUG);
  105. $resql=$db->query($sql);
  106. if ($resql) {
  107. $num = $db->num_rows($resql);
  108. $i = 0;
  109. if ($num) {
  110. while ($i < $num) {
  111. $obj = $db->fetch_object($resql);
  112. if ($obj) {
  113. // You can use here results
  114. echo $obj->field1;
  115. echo $obj->field2;
  116. }
  117. $i++;
  118. }
  119. }
  120. } else {
  121. $error++;
  122. dol_print_error($db);
  123. }
  124. */
  125. // -------------------- END OF YOUR CODE --------------------
  126. if (! $error) {
  127. $db->commit();
  128. echo '--- end ok' . "\n";
  129. } else {
  130. echo '--- end error code=' . $error . "\n";
  131. $db->rollback();
  132. }
  133. $db->close(); // Close database opened handler
  134. return $error;