1#!/usr/bin/env php 2<?php 3/* Copyright (C) 2007-2017 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 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/** 21 * \file htdocs/modulebuilder/template/scripts/mymodule.php 22 * \ingroup mymodule 23 * \brief This file is an example for a command line script for module MyModule 24 */ 25 26$sapi_type = php_sapi_name(); 27$script_file = basename(__FILE__); 28$path = __DIR__.'/'; 29 30// Test if batch mode 31if (substr($sapi_type, 0, 3) == 'cgi') { 32 echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n"; 33 exit(-1); 34} 35 36// Global variables 37$version = '1.0'; 38$error = 0; 39 40 41// -------------------- START OF YOUR CODE HERE -------------------- 42@set_time_limit(0); // No timeout for this script 43define('EVEN_IF_ONLY_LOGIN_ALLOWED', 1); // Set this define to 0 if you want to lock your script when dolibarr setup is "locked to admin user only". 44 45// Load Dolibarr environment 46$res = 0; 47// Try master.inc.php into web root detected using web root calculated from SCRIPT_FILENAME 48$tmp = empty($_SERVER['SCRIPT_FILENAME']) ? '' : $_SERVER['SCRIPT_FILENAME']; $tmp2 = realpath(__FILE__); $i = strlen($tmp) - 1; $j = strlen($tmp2) - 1; 49while ($i > 0 && $j > 0 && isset($tmp[$i]) && isset($tmp2[$j]) && $tmp[$i] == $tmp2[$j]) { $i--; $j--; } 50if (!$res && $i > 0 && file_exists(substr($tmp, 0, ($i + 1))."/master.inc.php")) $res = @include substr($tmp, 0, ($i + 1))."/master.inc.php"; 51if (!$res && $i > 0 && file_exists(dirname(substr($tmp, 0, ($i + 1)))."/master.inc.php")) $res = @include dirname(substr($tmp, 0, ($i + 1)))."/master.inc.php"; 52// Try master.inc.php using relative path 53if (!$res && file_exists("../master.inc.php")) $res = @include "../master.inc.php"; 54if (!$res && file_exists("../../master.inc.php")) $res = @include "../../master.inc.php"; 55if (!$res && file_exists("../../../master.inc.php")) $res = @include "../../../master.inc.php"; 56if (!$res) { 57 print "Include of master fails"; 58 exit(-1); 59} 60// After this $db, $mysoc, $langs, $conf and $hookmanager are defined (Opened $db handler to database will be closed at end of file). 61// $user is created but empty. 62 63//$langs->setDefaultLang('en_US'); // To change default language of $langs 64$langs->load("main"); // To load language file for default language 65 66// Load user and its permissions 67$result = $user->fetch('', 'admin'); // Load user for login 'admin'. Comment line to run as anonymous user. 68if (!$result > 0) { dol_print_error('', $user->error); exit; } 69$user->getrights(); 70 71 72print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n"; 73if (!isset($argv[1])) { // Check parameters 74 print "Usage: ".$script_file." param1 param2 ...\n"; 75 exit(-1); 76} 77print '--- start'."\n"; 78print 'Argument 1='.$argv[1]."\n"; 79print 'Argument 2='.$argv[2]."\n"; 80 81 82// Start of transaction 83$db->begin(); 84 85 86// Examples for manipulating class MyObject 87//dol_include_once("/mymodule/class/myobject.class.php"); 88//$myobject=new MyObject($db); 89 90// Example for inserting creating object in database 91/* 92dol_syslog($script_file." CREATE", LOG_DEBUG); 93$myobject->prop1='value_prop1'; 94$myobject->prop2='value_prop2'; 95$id=$myobject->create($user); 96if ($id < 0) { $error++; dol_print_error($db,$myobject->error); } 97else print "Object created with id=".$id."\n"; 98*/ 99 100// Example for reading object from database 101/* 102dol_syslog($script_file." FETCH", LOG_DEBUG); 103$result=$myobject->fetch($id); 104if ($result < 0) { $error; dol_print_error($db,$myobject->error); } 105else print "Object with id=".$id." loaded\n"; 106*/ 107 108// Example for updating object in database ($myobject must have been loaded by a fetch before) 109/* 110dol_syslog($script_file." UPDATE", LOG_DEBUG); 111$myobject->prop1='newvalue_prop1'; 112$myobject->prop2='newvalue_prop2'; 113$result=$myobject->update($user); 114if ($result < 0) { $error++; dol_print_error($db,$myobject->error); } 115else print "Object with id ".$myobject->id." updated\n"; 116*/ 117 118// Example for deleting object in database ($myobject must have been loaded by a fetch before) 119/* 120dol_syslog($script_file." DELETE", LOG_DEBUG); 121$result=$myobject->delete($user); 122if ($result < 0) { $error++; dol_print_error($db,$myobject->error); } 123else print "Object with id ".$myobject->id." deleted\n"; 124*/ 125 126 127// An example of a direct SQL read without using the fetch method 128/* 129$sql = "SELECT field1, field2"; 130$sql.= " FROM ".MAIN_DB_PREFIX."myobject"; 131$sql.= " WHERE field3 = 'xxx'"; 132$sql.= " ORDER BY field1 ASC"; 133 134dol_syslog($script_file, LOG_DEBUG); 135$resql=$db->query($sql); 136if ($resql) 137{ 138 $num = $db->num_rows($resql); 139 $i = 0; 140 if ($num) 141 { 142 while ($i < $num) 143 { 144 $obj = $db->fetch_object($resql); 145 if ($obj) 146 { 147 // You can use here results 148 print $obj->field1; 149 print $obj->field2; 150 } 151 $i++; 152 } 153 } 154} 155else 156{ 157 $error++; 158 dol_print_error($db); 159} 160*/ 161 162 163// -------------------- END OF YOUR CODE -------------------- 164 165if (!$error) 166{ 167 $db->commit(); 168 print '--- end ok'."\n"; 169} else { 170 print '--- end error code='.$error."\n"; 171 $db->rollback(); 172} 173 174$db->close(); // Close $db database opened handler 175 176exit($error); 177