skeleton_script.php 4.6 KB

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