Created
March 5, 2018 08:11
-
-
Save morozVA/4518a1b776be1e959f846e66bfd3c83f to your computer and use it in GitHub Desktop.
revo Импорт товаров из xls файла
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//include MODX API | |
define('MODX_API_MODE', true); | |
require_once($_SERVER['DOCUMENT_ROOT'].'/index.php'); | |
$modx=new modX(); | |
$modx->initialize('web'); | |
function debug($arr){ | |
echo '<pre>'; | |
var_dump($arr); | |
echo '</pre>'; | |
} | |
//transliteraton | |
function translit($str) { | |
$rus = array(' ','А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ё', 'Ж', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ъ', 'Ы', 'Ь', 'Э', 'Ю', 'Я', 'а', 'б', 'в', 'г', 'д', 'е', 'ё', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ы', 'ь', 'э', 'ю', 'я'); | |
$lat = array('_', 'a', 'b', 'v', 'g', 'd', 'e', 'e', 'gh', 'z', 'i', 'y', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'f', 'h', 'c', 'ch', 'sh', 'sch', 'y', 'y', 'y', 'e', 'yu', 'ya', 'a', 'b', 'v', 'g', 'd', 'e', 'e', 'gh', 'z', 'i', 'y', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'f', 'h', 'c', 'ch', 'sh', 'sch', 'y', 'y', 'y', 'e', 'yu', 'ya'); | |
return str_replace($rus, $lat, $str); | |
} | |
function makeAlias($url){ | |
$url = ereg_replace(' ','-',$url); | |
$url = translit($url); | |
$url = ereg_replace("[^a-z A-Z0-9-]*","",$url); | |
$url = ereg_replace("-{2,}","-",$url); | |
$url = ereg_replace(' ','-',$url); | |
$url = trim(strtolower($url)); | |
$url = ltrim($url,'-'); | |
$url = rtrim($url,'-'); | |
return $url; | |
} | |
$result = array(); | |
$product_arr = array(); //массив товаров | |
require_once '/Classes/PHPExcel.php'; | |
$filename = 'import.xls'; | |
// получаем тип файла (xls, xlsx), чтобы правильно его обработать | |
$file_type = PHPExcel_IOFactory::identify( $filename ); | |
$objReader = PHPExcel_IOFactory::createReader( $file_type ); | |
$objPHPExcel = $objReader->load( $filename ); // загружаем данные файла в объект | |
$result = $objPHPExcel->getActiveSheet()->toArray(); // выгружаем данные из объекта в массив | |
foreach($result as $row){ | |
$product_arr[] = $row; | |
}////Получение массива товаров////// | |
array_shift($product_arr); | |
array_shift($product_arr); | |
$i=0; | |
foreach ($product_arr as $product) { | |
debug($product); | |
} | |
die(); | |
$pagetitle = 'Новый $%^&*(@#$@#$) / " товар '; | |
$alias = makeAlias($pagetitle); | |
$response = $modx->runProcessor('resource/create', array( | |
'pagetitle' => $pagetitle, | |
'longtitle' => '', | |
'description' => '', | |
'introtext' => '', | |
'content' => 'Содержимое нового товара', | |
'alias' => $alias, | |
'template' => 3, | |
'published' => 1, | |
'parent' => 2, | |
'tv1' => '10', | |
'tv2' => '8', | |
'tv3' => '1 месяц', | |
'tv4' => '5 штук', | |
'tv5' => '30', | |
)); | |
if ($response->isError()) { | |
return $modx->error->failure($response->getMessage()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment