dolibarr/dev/examples/code/create_product.php

106 lines
3.1 KiB
PHP
Raw Permalink Normal View History

Better Travis CI NEW: Cleaned up routines for better readability of both declaration and results. PHP versions now really covered. The old code forced install of PHP and didn't use Travis provided versions. This resulted in the process not being executed with the declared PHP version. Dropped MySQL in favor of MariaDB. This is now the FLOSS community standard. This should help avoid problems with buggy MySQL releases. Fast finish enabled to show results faster. Optimized tools installation with composer. The right version of the tool is installed for the PHP version under test. New PHP linter to check for syntax errors. Parallelized for better speed. Apache + PHP FPM for testing webservices. The previous mod_php configuration was not supported on Travis. New global DEBUG environment variable to show verbose output with configuration files content. IRC notification on #dolibarr@freenode for community awareness. FIXES: Bug in scripts preventing execution with environmentalized PHP. Wrong detection of MAIN_URL_ROOT under specific circumstances. $_SERVER["DOCUMENT_ROOT"] empty and $_SERVER["SCRIPT_NAME"] populated. Relative ignore directive in coding style ruleset to avoid bypassing test. Unit test errors without an exit status. This prevented the CI from properly detecting and reporting the error. TODOS: PostgreSQL support. This one is tricky since we only have a MySQL dump and the syntax is not directly compatible. SQLite support. Disabled in core at the moment. Nginx + PHP FPM support. Test webservices on the second most popular webserver. Run dev/* checks. We have a nice collection of scripts we could leverage. Check Javascript. Check CSS. Check SQL.
2015-12-11 04:08:32 +00:00
#!/usr/bin/env php
<?php
2009-09-12 14:38:48 +00:00
/* Copyright (C) 2009 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
2009-09-12 14:38:48 +00:00
*
* 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
2009-09-12 14:38:48 +00:00
* (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
2019-06-23 13:30:35 +00:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2009-09-12 14:38:48 +00:00
*/
/**
* \file dev/examples/create_product.php
2009-09-12 14:38:48 +00:00
* \brief This file is an example for a command line script
2012-08-23 07:27:01 +00:00
* \author Put author's name here
2009-09-12 14:38:48 +00:00
* \remarks Put here some comments
*/
$sapi_type = php_sapi_name();
2009-10-22 01:04:23 +00:00
$script_file = basename(__FILE__);
2023-12-04 09:25:02 +00:00
$path = dirname(__FILE__).'/';
2009-09-12 14:38:48 +00:00
2009-10-22 01:04:23 +00:00
// Test if batch mode
2009-09-12 14:38:48 +00:00
if (substr($sapi_type, 0, 3) == 'cgi') {
2021-01-14 14:09:08 +00: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;
2009-09-12 14:38:48 +00:00
}
// Global variables
2023-12-04 09:25:02 +00:00
$version = '1.10';
$error = 0;
2009-09-12 14:38:48 +00:00
// -------------------- START OF YOUR CODE HERE --------------------
// Include Dolibarr environment
2023-10-02 22:31:13 +00:00
require_once $path."../../../htdocs/master.inc.php";
2009-09-12 14:38:48 +00:00
// After this $db, $mysoc, $langs and $conf->entity are defined. Opened handler to database will be closed at end of file.
//$langs->setDefaultLang('en_US'); // To change default language of $langs
$langs->load("main"); // To load language file for default language
@set_time_limit(0);
// Load user and its permissions
2023-12-04 09:25:02 +00:00
$result = $user->fetch('', 'admin'); // Load user for login 'admin'. Comment line to run as anonymous user.
if (!$result > 0) {
2024-01-20 08:22:38 +00:00
dol_print_error(null, $user->error);
2021-01-14 14:09:08 +00:00
exit;
}
$user->loadRights();
2009-09-12 14:38:48 +00:00
print "***** ".$script_file." (".$version.") *****\n";
// Start of transaction
$db->begin();
2018-07-26 09:57:25 +00:00
require_once DOL_DOCUMENT_ROOT."/product/class/product.class.php";
2009-09-12 14:38:48 +00:00
// Create instance of object
2023-12-04 09:25:02 +00:00
$myproduct = new Product($db);
2009-09-12 14:38:48 +00:00
2012-08-23 07:27:01 +00:00
// Definition of product instance properties
2009-09-12 14:38:48 +00:00
$myproduct->ref = '1234';
$myproduct->label = 'label';
2009-09-12 14:38:48 +00:00
$myproduct->price = '10';
$myproduct->price_base_type = 'HT';
$myproduct->tva_tx = '19.6';
$myproduct->type = Product::TYPE_PRODUCT;
2009-09-12 14:38:48 +00:00
$myproduct->status = 1;
$myproduct->description = 'Description';
$myproduct->note = 'Note';
$myproduct->weight = 10;
$myproduct->weight_units = 0;
// Create product in database
$idobject = $myproduct->create($user);
2021-01-14 14:09:08 +00:00
if ($idobject > 0) {
2009-09-12 14:38:48 +00:00
print "OK Object created with id ".$idobject."\n";
2020-05-21 13:05:19 +00:00
} else {
2009-09-12 14:38:48 +00:00
$error++;
dol_print_error($db, $myproduct->error);
2009-09-12 14:38:48 +00:00
}
// -------------------- END OF YOUR CODE --------------------
2023-12-04 09:25:02 +00:00
if (!$error) {
2009-09-12 14:38:48 +00:00
$db->commit();
print '--- end ok'."\n";
2020-05-21 13:05:19 +00:00
} else {
2009-09-12 14:38:48 +00:00
print '--- end error code='.$error."\n";
$db->rollback();
}
$db->close();
return $error;