dolibarr/dev/skeletons/skeleton_script.php

167 lines
4.7 KiB
PHP
Raw Normal View History

#!/usr/bin/php
2011-01-23 02:00:45 +01:00
<?php
2013-03-12 16:34:20 +01:00
/* Copyright (C) 2007-2013 Laurent Destailleur <eldy@users.sourceforge.net>
2008-01-20 19:47:10 +01:00
* Copyright (C) ---Put here your own copyright and developer email---
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2011-08-01 00:21:57 +02:00
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file dev/skeletons/skeleton_script.php
2013-03-12 16:34:20 +01:00
* \ingroup mymodule
* \brief This file is an example for a command line script
2012-01-18 00:15:10 +01:00
* Put here some comments
*/
$sapi_type = php_sapi_name();
2009-10-22 03:04:23 +02:00
$script_file = basename(__FILE__);
$path=dirname(__FILE__).'/';
2009-10-22 03:04:23 +02:00
// Test if batch mode
if (substr($sapi_type, 0, 3) == 'cgi') {
2011-01-18 16:35:45 +01:00
echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
exit(-1);
}
// Global variables
2013-03-12 16:34:20 +01:00
$version='1.0';
2008-08-02 13:26:43 +02:00
$error=0;
2007-11-19 22:51:53 +01:00
// -------------------- START OF YOUR CODE HERE --------------------
@set_time_limit(0); // No timeout for this script
define('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".
2013-03-12 16:34:20 +01:00
// Include and load Dolibarr environment variables
2009-09-10 23:46:10 +02:00
require_once($path."../../htdocs/master.inc.php");
2013-03-12 16:34:20 +01:00
// After this $db, $mysoc, $langs, $conf and $hookmanager are defined (Opened $db handler to database will be closed at end of file).
// $user is created but empty.
2009-09-10 23:46:10 +02:00
2009-09-01 17:47:29 +02:00
//$langs->setDefaultLang('en_US'); // To change default language of $langs
$langs->load("main"); // To load language file for default language
2009-09-10 23:46:10 +02:00
// Load user and its permissions
$result=$user->fetch('','admin'); // Load user for login 'admin'. Comment line to run as anonymous user.
2009-09-12 16:24:56 +02:00
if (! $result > 0) { dol_print_error('',$user->error); exit; }
2009-09-10 23:46:10 +02:00
$user->getrights();
2008-08-02 13:26:43 +02:00
2014-07-31 10:21:47 +02:00
print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
2009-09-12 16:24:56 +02:00
if (! isset($argv[1])) { // Check parameters
2008-08-02 13:26:43 +02:00
print "Usage: ".$script_file." param1 param2 ...\n";
exit(-1);
}
2009-09-12 16:24:56 +02:00
print '--- start'."\n";
print 'Argument 1='.$argv[1]."\n";
print 'Argument 2='.$argv[2]."\n";
2009-03-07 02:03:20 +01:00
// Start of transaction
$db->begin();
// Examples for manipulating class skeleton_class
require_once(DOL_DOCUMENT_ROOT."/../dev/skeletons/skeleton_class.class.php");
2011-08-28 19:40:51 +02:00
$myobject=new Skeleton_Class($db);
2008-08-02 13:26:43 +02:00
// Example for inserting creating object in database
2007-11-19 22:51:53 +01:00
/*
dol_syslog($script_file." CREATE", LOG_DEBUG);
$myobject->prop1='value_prop1';
$myobject->prop2='value_prop2';
2008-08-02 13:26:43 +02:00
$id=$myobject->create($user);
2009-09-10 23:46:10 +02:00
if ($id < 0) { $error++; dol_print_error($db,$myobject->error); }
else print "Object created with id=".$id."\n";
2007-11-19 22:51:53 +01:00
*/
2008-08-02 13:26:43 +02:00
// Example for reading object from database
2007-11-19 22:51:53 +01:00
/*
dol_syslog($script_file." FETCH", LOG_DEBUG);
2008-08-02 13:26:43 +02:00
$result=$myobject->fetch($id);
2009-09-10 23:46:10 +02:00
if ($result < 0) { $error; dol_print_error($db,$myobject->error); }
else print "Object with id=".$id." loaded\n";
2007-11-19 22:51:53 +01:00
*/
// Example for updating object in database ($myobject must have been loaded by a fetch before)
2007-11-19 22:51:53 +01:00
/*
dol_syslog($script_file." UPDATE", LOG_DEBUG);
2007-11-19 22:51:53 +01:00
$myobject->prop1='newvalue_prop1';
$myobject->prop2='newvalue_prop2';
$result=$myobject->update($user);
2009-09-10 23:46:10 +02:00
if ($result < 0) { $error++; dol_print_error($db,$myobject->error); }
else print "Object with id ".$myobject->id." updated\n";
2007-11-19 22:51:53 +01:00
*/
// Example for deleting object in database ($myobject must have been loaded by a fetch before)
2007-11-19 22:51:53 +01:00
/*
dol_syslog($script_file." DELETE", LOG_DEBUG);
$result=$myobject->delete($user);
2009-09-10 23:46:10 +02:00
if ($result < 0) { $error++; dol_print_error($db,$myobject->error); }
else print "Object with id ".$myobject->id." deleted\n";
2007-11-19 22:51:53 +01:00
*/
2008-08-02 13:26:43 +02:00
// An example of a direct SQL read without using the fetch method
2007-11-19 22:51:53 +01:00
/*
$sql = "SELECT field1, field2";
2012-06-27 01:27:22 +02:00
$sql.= " FROM ".MAIN_DB_PREFIX."skeleton";
$sql.= " WHERE field3 = 'xxx'";
$sql.= " ORDER BY field1 ASC";
2014-06-12 11:31:53 +02:00
dol_syslog($script_file, LOG_DEBUG);
$resql=$db->query($sql);
if ($resql)
{
$num = $db->num_rows($resql);
$i = 0;
if ($num)
{
while ($i < $num)
{
$obj = $db->fetch_object($resql);
if ($obj)
{
// You can use here results
print $obj->field1;
print $obj->field2;
}
$i++;
}
}
}
else
{
2009-09-10 23:46:10 +02:00
$error++;
dol_print_error($db);
}
2007-11-19 22:51:53 +01:00
*/
// -------------------- END OF YOUR CODE --------------------
2009-03-07 02:03:20 +01:00
if (! $error)
{
$db->commit();
print '--- end ok'."\n";
}
else
{
print '--- end error code='.$error."\n";
$db->rollback();
}
2013-03-12 16:34:20 +01:00
$db->close(); // Close $db database opened handler
exit($error);