Я использую opencart и использую бесплатное расширение для ежедневного обновления инвентаря.
Php Script / Extension загружает файл CSV и запускает его для обновления базы данных с обновленным количеством.
Вместо того, чтобы загружать каждый раз, я бы хотел, чтобы скрипт использовал файл OPENCART.CSV в базовом каталоге веб-сайта каждый раз, чтобы исключить дополнительный шаг.
Я хотел бы, чтобы скрипт запускался автоматически при вызове URL, чтобы я мог использовать его в cron.
Вот код
<?php
class ControllerToolInventoryUpdate extends Controller {
private $error = array();
public function index() {
$this->load->language('tool/inventory_update');
$this->load->model('tool/inventory_update');
$this->load->model('setting/setting');
if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validate())) {
$this->model_setting_setting->editSetting('inventory_update', $this->request->post);
if (isset($this->request->post['id_field'])) {
if ($this->ValidateFormFile()) {
//Select case to reference the product to be updated
switch ($this->request->post['id_field'])
{
case "model":
$column = "model";
break;
case "sku":
$column = "sku";
}
//Create an array of file content
$file_content = explode("\n",(file_get_contents($this->request->files['import']['tmp_name'])));
$this->data['error_filedata'] = '';
$error_reference = $this->language->get('error_reference');
$error_quantity = $this->language->get('error_quantity');
$error_recordnotfound = $this->language->get('error_recordnotfound');
$error_failedvalidation = $this->language->get('error_failedvalidation');
$error_fieldcount = $this->language->get('error_fieldcount');
$recordsUpdated = 0;
//Create second dimension to hold items using defined delimiter on lines that have data
foreach ($file_content as $key => $value) {
if (trim(strlen($value)) > 1) {
// $value = stripslashes($value);
$value = str_replace(array('\'', '"'), '', $value);
$line_records = explode(',', $value);
if ( (count($line_records) < 2) || (count($line_records) > 3) ){
$this->data['error_filedata'].= $error_fieldcount. $value."<br />";
} else {
$reference = trim($line_records[0]);
$quantity = trim($line_records[1]);
if (isset($line_records[2])) { $dbprice = trim($line_records[2]); } else { $dbprice = ''; }
if ($this->ValidateFileData($quantity) || $this->ValidateFileData($dbprice)) {
if ($this->model_tool_inventory_update->inventory_update($column,$reference,$quantity,$dbprice)) {
$recordsUpdated ++;
} else {
$this->data['error_filedata'].= $error_recordnotfound.$error_reference.$reference."<br />";
}
} else {
$this->data['error_filedata'].= $error_failedvalidation.$error_reference.$reference.$error_quantity.$quantity."<br />";
}
}
}
}
$this->session->data['success'] = $this->language->get('text_success');
} else {
$this->data['error_file']= $this->language->get('error_file');
}
} else {
$this->data['error_id_field'] = $this->language->get('error_id_field');
}
}
$this->data['heading_title'] = $this->language->get('heading_title');
$this->data['entry_file'] = $this->language->get('entry_file');
$this->data['entry_model'] = $this->language->get('entry_model');
$this->data['entry_sku'] = $this->language->get('entry_sku');
$this->data['text_howto'] = $this->language->get('text_howto');
$this->data['text_id_field'] = $this->language->get('text_id_field');
$this->data['button_update'] = $this->language->get('button_update');
$this->data['tab_general'] = $this->language->get('tab_general');
if (isset($this->request->post['id_field'])) {
$this->data['id_field'] = $this->request->post['id_field'];
} else {
$this->data['id_field'] = $this->config->get('id_field');
}
if (isset($this->data['error_filedata'])) {
$this->data['error_filedata'] = $this->language->get('error_filedata').$this->data['error_filedata'];
}
if (isset($this->error['warning'])) {
$this->data['error_warning'] = $this->error['warning'];
} else {
$this->data['error_warning'] = '';
}
if (isset($this->session->data['success'])) {
$this->data['success'] = $recordsUpdated.$this->session->data['success'];
unset($this->session->data['success']);
} else {
$this->data['success'] = '';
}
$this->document->breadcrumbs = array();
$this->document->breadcrumbs[] = array(
'href' => HTTPS_SERVER . 'index.php?route=common/home&token=' . $this->session->data['token'],
'text' => $this->language->get('text_home'),
'separator' => FALSE
);
$this->document->breadcrumbs[] = array(
'href' => HTTPS_SERVER . 'index.php?route=tool/inventory_update&token=' . $this->session->data['token'],
'text' => $this->language->get('heading_title'),
'separator' => ' :: '
);
$this->data['action'] = HTTPS_SERVER . 'index.php?route=tool/inventory_update&token=' . $this->session->data['token'];
$this->template = 'tool/inventory_update.tpl';
$this->children = array(
'common/header',
'common/footer'
);
$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
}
private function validate() {
if (!$this->user->hasPermission('modify', 'tool/inventory_update')) {
$this->error['warning'] = $this->language->get('error_permission');
}
if (!$this->error) {
return TRUE;
} else {
return FALSE;
}
}
private function ValidateFormFile() {
if (($this->request->files['import']) && ($this->request->files['import']['error']==0)) {
$filename = basename($this->request->files['import']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
if (($ext == "CSV") && ($this->request->files['import']['size'] < 1000000)) {
return file_get_contents($this->request->files['import']['tmp_name']);
} else {
return FALSE;
}
} else {
return FALSE;
}
}
function stripslashes_deep($value)
{
if(isset($value)) {
$value = is_array($value) ?
array_map('stripslashes_deep', $value) :
stripslashes($value);
}
return $value;
}
private function ValidateFileData ($value) {
if (strlen($value)<1 || strlen($value)>20) {
return false;
//} else if (!eregi('^[0-9]+$', $value)) {
//return false;
} else {
return true;
}
}
}
?>
Вот ссылка на оригинальное расширение.
Вы должны заменить все вхождения:
$this->request->files['import']['tmp_name']
с путем к файлу, например если файл в корне
$_SERVER['DOCUMENT_ROOT'] . "OPENCART.CSV"
Других решений пока нет …