major refactoring of almost all files
This commit is contained in:
@@ -1,67 +1,72 @@
|
||||
<?php
|
||||
namespace Froxlor\Bulk;
|
||||
|
||||
/**
|
||||
* This file is part of the Froxlor project.
|
||||
* Copyright (c) 2010 the Froxlor Team (see authors).
|
||||
*
|
||||
* For the full copyright and license information, please view the COPYING
|
||||
* file that was distributed with this source code. You can also view the
|
||||
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
|
||||
* 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 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* @copyright (c) the authors
|
||||
* @author Michael Kaufmann <mkaufmann@nutime.de>
|
||||
* @author Froxlor team <team@froxlor.org> (2010-)
|
||||
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
|
||||
* @package Cron
|
||||
*
|
||||
* @since 0.10.0
|
||||
*
|
||||
* 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
|
||||
* along with this program; if not, you can also view it online at
|
||||
* https://files.froxlor.org/misc/COPYING.txt
|
||||
*
|
||||
* @copyright the authors
|
||||
* @author Froxlor team <team@froxlor.org>
|
||||
* @license https://files.froxlor.org/misc/COPYING.txt GPLv2
|
||||
*/
|
||||
|
||||
namespace Froxlor\Bulk;
|
||||
|
||||
use Exception;
|
||||
use Froxlor\FileDir;
|
||||
|
||||
/**
|
||||
* Abstract Class BulkAction to mass-import entities
|
||||
*
|
||||
* @author Michael Kaufmann (d00p) <d00p@froxlor.org>
|
||||
*
|
||||
*
|
||||
*/
|
||||
abstract class BulkAction
|
||||
{
|
||||
|
||||
/**
|
||||
* complete path including filename of file to be imported
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $impFile = null;
|
||||
|
||||
/**
|
||||
* api-function to call for addingg entity
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $api_call = null;
|
||||
|
||||
/**
|
||||
* api-function parameter names, read from import-file (first line)
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $api_params = null;
|
||||
|
||||
/**
|
||||
* errors while importing
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $errors = array();
|
||||
|
||||
/**
|
||||
* logged in user
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $userinfo = array();
|
||||
protected $userinfo = [];
|
||||
/**
|
||||
* complete path including filename of file to be imported
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $impFile = null;
|
||||
/**
|
||||
* api-function to call for addingg entity
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $api_call = null;
|
||||
/**
|
||||
* api-function parameter names, read from import-file (first line)
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $api_params = null;
|
||||
/**
|
||||
* errors while importing
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $errors = [];
|
||||
|
||||
/**
|
||||
* class constructor, optionally sets file and customer-id
|
||||
@@ -71,10 +76,10 @@ abstract class BulkAction
|
||||
*
|
||||
* @return object BulkAction instance
|
||||
*/
|
||||
protected function __construct($import_file = null, $userinfo = array())
|
||||
protected function __construct($import_file = null, $userinfo = [])
|
||||
{
|
||||
if (! empty($import_file)) {
|
||||
$this->impFile = \Froxlor\FileDir::makeCorrectFile($import_file);
|
||||
if (!empty($import_file)) {
|
||||
$this->impFile = FileDir::makeCorrectFile($import_file);
|
||||
}
|
||||
$this->userinfo = $userinfo;
|
||||
}
|
||||
@@ -99,7 +104,7 @@ abstract class BulkAction
|
||||
*/
|
||||
public function setImportFile($import_file = null)
|
||||
{
|
||||
$this->impFile = \Froxlor\FileDir::makeCorrectFile($import_file);
|
||||
$this->impFile = FileDir::makeCorrectFile($import_file);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -126,12 +131,14 @@ abstract class BulkAction
|
||||
|
||||
protected function importEntity($data_array = null)
|
||||
{
|
||||
if (empty($data_array)) return null;
|
||||
if (empty($data_array)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$module = '\\Froxlor\\Api\\Commands\\' . substr($this->api_call, 0, strpos($this->api_call, "."));
|
||||
$function = substr($this->api_call, strpos($this->api_call, ".") + 1);
|
||||
|
||||
$new_data = array();
|
||||
$new_data = [];
|
||||
foreach ($this->api_params as $idx => $param) {
|
||||
if (isset($data_array[$idx])) {
|
||||
$new_data[$param] = $data_array[$idx];
|
||||
@@ -142,10 +149,10 @@ abstract class BulkAction
|
||||
try {
|
||||
$json_result = $module::getLocal($this->userinfo, $new_data)->$function();
|
||||
$result = json_decode($json_result, true)['data'];
|
||||
} catch (\Exception $e) {
|
||||
} catch (Exception $e) {
|
||||
$this->errors[] = $e->getMessage();
|
||||
}
|
||||
return ! empty($result);
|
||||
return !empty($result);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -159,28 +166,28 @@ abstract class BulkAction
|
||||
protected function parseImportFile($separator = ";")
|
||||
{
|
||||
if (empty($this->impFile)) {
|
||||
throw new \Exception("No file was given for import");
|
||||
throw new Exception("No file was given for import");
|
||||
}
|
||||
|
||||
if (! file_exists($this->impFile)) {
|
||||
throw new \Exception("The file '" . $this->impFile . "' could not be found");
|
||||
if (!file_exists($this->impFile)) {
|
||||
throw new Exception("The file '" . $this->impFile . "' could not be found");
|
||||
}
|
||||
|
||||
if (! is_readable($this->impFile)) {
|
||||
throw new \Exception("Unable to read file '" . $this->impFile . "'");
|
||||
if (!is_readable($this->impFile)) {
|
||||
throw new Exception("Unable to read file '" . $this->impFile . "'");
|
||||
}
|
||||
|
||||
if (empty($separator) || strlen($separator) != 1) {
|
||||
throw new \Exception("Invalid separator specified: '" . $separator . "'");
|
||||
throw new Exception("Invalid separator specified: '" . $separator . "'");
|
||||
}
|
||||
|
||||
$file_data = array();
|
||||
$file_data = [];
|
||||
$is_params_line = true;
|
||||
$fh = @fopen($this->impFile, "r");
|
||||
if ($fh) {
|
||||
while (($line = fgets($fh)) !== false) {
|
||||
$tmp_arr = explode($separator, $line);
|
||||
$data_arr = array();
|
||||
$data_arr = [];
|
||||
foreach ($tmp_arr as $idx => $data) {
|
||||
if ($is_params_line) {
|
||||
$this->api_params[$idx] = $data;
|
||||
@@ -196,7 +203,7 @@ abstract class BulkAction
|
||||
}
|
||||
$this->api_params = array_map("trim", $this->api_params);
|
||||
} else {
|
||||
throw new \Exception("Unable to open file '" . $this->impFile . "'");
|
||||
throw new Exception("Unable to open file '" . $this->impFile . "'");
|
||||
}
|
||||
fclose($fh);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user