Table of contents
Note: This image import option is only available for self-hosted systems. Customers which are hosted on the Xentral cloud have the possibility to import images via the file browser.
In order to import articles with the corresponding images, you can use the following example script. This script and the corresponding CSV file should be placed in the importer folder of the Xentrals folder. The present example script is for the example CSV file obst.csv, which is shown here as a table. In the table of the CSV file many columns for further properties can be created. In the following only the basis is shown. It should be noted: In the column Image the path to the images must be specified, but no links. This action is only if the images are not located in the same folder as the script. If the articles already exist in user-Xentral only the images are to be added, the article number and a column with the path for the image. The code has to be adapted accordingly. This script is called bilderimport.php artikel namedercsvdatei.csv in the command line in the folder importer of the respective Xental, if the script was saved.
Example CSV file: obst.csv
Example PHP file: imageimport.php
<?PHP // Report simple errors only error_reporting(E_ERROR | E_WARNING | E_PARSE);
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../xentral_autoloader.php';
$laravel = require_once __DIR__ . '/../bootstrap/app.php';
$kernel = $laravel->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap(); include_once("../conf/main.conf.php"); include_once("../phpwf/plugins/class.mysql.php"); include_once("../phpwf/plugins/class.string.php"); include_once("../phpwf/plugins/class.user.php"); include_once("../www/lib/class.erpapi.php"); //include_once("../www/lib/class.erpapi_custom.php"); class secure_t { var $id; function GetGET($val) { if($val == 'id')return ''; return $_GET[$val]; } } class app_t { var $DB; var $String; var $User; } if(empty($app)) { $app = new app_t(); } if(empty($app->Secure))$app->Secure = new secure_t(); if(empty($app->User))$app->User = new User($app); if(empty($app->Conf)) { $conf = new Config(); $app->Conf = $conf; } if(empty($app->DB))$app->DB = new DB($conf->WFdbhost,$conf->WFdbname,$conf->WFdbuser,$conf->WFdbpass); if(empty($app->String))$app->String = new WawiString(); if(empty($app->erp)) { $erp = new erpAPI($app); $app->erp = $erp; } if(isset($argv[1]) && isset($argv[2])) { $importer = new Importer($app, $argv[1] ,'./'.$argv[2], isset($argv[3])?$argv[3]:null); } else { echo "Please specify import_with_image.php type <file>.csv"; echo "possible types: article "; echo "\r\n" } class Importer { function __construct(&$app, $type, $file, $parameter) { $this->app = $app; $project = $this->app->DB->Select("SELECT standardproject FROM company WHERE id='".$this->app->User->GetCompany()."' LIMIT 1"); $projectid = $project; $this->country = $this->app->erp->GetSelectLaenderliste(true); $col = array(0,2,3); $csvarray = array(); if(file_exists($file) && is_file($file)) { $csvfile = $file; if (($handle = fopen($csvfile, "r")) !== FALSE) { $row = 0; while (($csv = fgetcsv($handle,0,';')) !== FALSE) { $row++; //If file is in ISO-8859-1 and not UTF-8 $num = count($csv); //Number of columns in CSV //execute from second column but only if there is more than one column if($row > 1 && $num >= 2) { foreach($csv as $k =>$v){ $ucsv[$k] = $v; } //parameters as escaped version for direct SQL statements foreach($csv as $k =>$v){ $csv[$k] = $this->app->DB->real_escape_string($v); } switch($type) { case 'article': $this->article($csv, $ucsv); break; } }elseif($row == 1){ } } fclose($handle); } } else { echo "File ".$file." does not exist"; exit; } } function article(&$csv, &$ucsv) { //Get the values from the $ucsv array whose indices correspond to the columns of the CSV file starting from 0 $number = $ucsv[0]; $name_en = $ucsv[1]; $anabregs_text = $ucsv[2]; $purchase_price = $ucsv[3]; $sales_price = $ucsv[4]; $vendor_name = $ucsv[5]; $order number = $ucsv[6]; $image = $ucsv[7]; //Variables are stored in a new array to insert items more easily. Keys correspond to column names of the article table in the database $art['number'] = $number; $art['name_en'] = $name_en; $art['anabregs_text'] = $anabregs_text; //inserts the article into the database $artid = $this->app->erp->InsertUpdateArticle($art); //Comma of purchase and sales prices are replaced with a dot $purchase price = str_replace(',','.',$purchaseprice); $sale price = str_replace(',','.',$saleprice); //Get the supplier id which is needed for the purchase price $address supplier = $this->app->DB->Select("SELECT id FROM address WHERE name_en = \ "$supplier_name\"); //If a supplier id exists, the purchase price will be added if($address_supplier != ""){ $this->app->erp->AddPurchasePrice($artid, 1, $address supplier, $order_number, $name_en, $purchase_price); } //sales price is added $this->app->erp->AddSellPrice($artid, 1, 0, $sellprice); //Get the path for the location of the images $path = $this->app->Conf->WFuserdata; $path = rtrim($path]; //Get the folder for the images location, should be the folder dms $path .= "/dms/"; //If the folder dms does not exist, it will be created if(!file_exists($path)){ mkdir($path, 0777, true); } $memorypath = $path.$this->app->Conf->WFdbname; if(!file_exists($memorypath)) { mkdir($memorypath, 0777, true); } //File is created and stored in xentral $fileid = $this->app->erp->CreateFile($image, $image, "", "", $image, "",true,$memorypath); $this->app->erp->AddFileKeyword($fileid, "image", "article", $artid); } } ?>
Example add images to existing articles
The following import script can be used to add new images to existing articles. For this, a unique property of an article is required, e.g. the article ID, the article number or the EAN. In the example the article number as unique property to find out the ID of the article, which is needed for linking the images to the article.
Example CSV file: obst.csv
Example PHP file: imageimport.php
<?PHP // Report simple errors only error_reporting(E_ERROR | E_WARNING | E_PARSE);
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../xentral_autoloader.php';
$laravel = require_once __DIR__ . '/../bootstrap/app.php';
$kernel = $laravel->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();
include_once("../conf/main.conf.php"); include_once("../phpwf/plugins/class.mysql.php"); include_once("../phpwf/plugins/class.string.php"); include_once("../phpwf/plugins/class.user.php"); include_once("../www/lib/class.erpapi.php"); //include_once("../www/lib/class.erpapi_custom.php"); class secure_t { var $id; function GetGET($val) { if($val == 'id')return ''; return $_GET[$val]; } } class app_t { var $DB; var $String; var $User; } if(empty($app)) { $app = new app_t(); } if(empty($app->Secure))$app->Secure = new secure_t(); if(empty($app->User))$app->User = new User($app); if(empty($app->Conf)) { $conf = new Config(); $app->Conf = $conf; } if(empty($app->DB))$app->DB = new DB($conf->WFdbhost,$conf->WFdbname,$conf->WFdbuser,$conf->WFdbpass); if(empty($app->String))$app->String = new WawiString(); if(empty($app->erp)) { $erp = new erpAPI($app); $app->erp = $erp; } if(isset($argv[1]) && isset($argv[2])) { $importer = new Importer($app, $argv[1] ,'./'.$argv[2], isset($argv[3])?$argv[3]:null); } else { echo "Please specify imageimport.php type <file>.csv"; echo "possible types: article "; echo "\r\n" } class Importer { function __construct(&$app, $type, $file, $parameter) { $this->app = $app; $project = $this->app->DB->Select("SELECT standardproject FROM company WHERE id='".$this->app->User->GetCompany()."' LIMIT 1"); $projectid = $project; $this->country = $this->app->erp->GetSelectLaenderliste(true); $col = array(0,2,3); $csvarray = array(); if(file_exists($file) && is_file($file)) { $csvfile = $file; if (($handle = fopen($csvfile, "r")) !== FALSE) { $row = 0; while (($csv = fgetcsv($handle,0,';')) !== FALSE) { $row++; //If file is in ISO-8859-1 and not UTF-8 $num = count($csv); //Number of columns in CSV //execute from second column but only if there is more than one column if($row > 1 && $num >= 2) { foreach($csv as $k =>$v){ $ucsv[$k] = $v; } //parameters as escaped version for direct SQL statements foreach($csv as $k =>$v){ $csv[$k] = $this->app->DB->real_escape_string($v); } switch($type) { case 'article': $this->article($csv, $ucsv); break; } }elseif($row == 1){ } } fclose($handle); } } else { echo "File ".$file." does not exist"; exit; } } function article(&$csv, &$ucsv) { //Get the values from the $ucsv array whose indices correspond to the columns of the CSV file starting from 0 $articlenumber = $ucsv[0]; // must be unique, like articleid, article number or EAN of the articles take $image = $ucsv[1]; //unique articleid will be fetched $articleid = $this->app->DB->Select("SELECT id FROM article WHERE number = '$article number' LIMIT 1"); //Get the path for the location of the images $path = $this->app->Conf->WFuserdata; $path = rtrim($path); //Get the folder for the images location, should be the folder dms $path .= "/dms/"; //If the folder dms does not exist, it will be created if(!file_exists($path)){ mkdir($path, 0777, true); } $memorypath = $path.$this->app->Conf->WFdbname; if(!file_exists($memorypath)) { mkdir($memorypath, 0777, true); } //File is created and stored in xentral $fileid = $this->app->erp->CreateFile($image, $image, "", "", $image, "",true,$memorypath); $this->app->erp->AddFileKeyword($fileid, "image", "article", $articleid); } } ?>