php 脚本读取xml文件并将项添加到db import object mcity
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 脚本读取xml文件并将项添加到db import object mcity相关的知识,希望对你有一定的参考价值。
<?
$_SERVER["DOCUMENT_ROOT"] = "/domains/city.rent";
$DOCUMENT_ROOT = $_SERVER["DOCUMENT_ROOT"];
define("NO_KEEP_STATISTIC", true);
define("NOT_CHECK_PERMISSIONS", true);
set_time_limit(0);
require_once $_SERVER["DOCUMENT_ROOT"].'/bitrix/modules/main/include/prolog_before.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/local/templates/.default/functions.php';
$save = 1;
$write_log = 1;
$preview_pic_w = 407;
$preview_pic_h = 176;
$preview_pic_param = array(
'resize_dir '=> '/upload/tmp/wm/pre',
'resize_type' => BX_RESIZE_IMAGE_EXACT,
'resize_watermark' => '/local/templates/.default/images/watermark_3m.png',
);
$pic_p = array(
'resize_watermark' => '/local/templates/.default/images/watermark_3m.png'
);
$file = $_SERVER['DOCUMENT_ROOT'].'/upload/feed-crm/feed_mskcity_card.xml';
if (file_exists($file))
{
//$log = '/domains/feed_crm_log/feed-card.log';
//$log = '/domains/br_to_crm.log';
$log = $_SERVER['DOCUMENT_ROOT'].'/upload/log/feed-crm/feed-card.log';
$xml = simplexml_load_file($file);
CModule::IncludeModule('iblock');
$arCardID = $arCard = $arField = array();
foreach($xml->offer as $card)
{
$arCardID[] = (int)$card->ID;
}
$arFilter = array('IBLOCK_ID'=>1, 'PROPERTY_CRMID'=>$arCardID);
$select = array('ID','NAME','PROPERTY_*','ACTIVE','DETAIL_PICTURE','PREVIEW_PICTURE');
$res = CIBlockElement::GetList($sort, $arFilter, false, false, $select);
while($ob = $res->GetNextElement())
{
$card = $ob->GetFields();
$prop = $ob->GetProperties();
$arCard[$prop['CRMID']['VALUE']] = array(
'ID' => $card['ID'],
'NAME' => $card['NAME'],
'DETAIL_PICTURE' => $card['DETAIL_PICTURE'],
'PREVIEW_PICTURE' => $card['PREVIEW_PICTURE'],
'ACTIVE' => $card['ACTIVE'],
'prop' => $prop,
);
}
// объекты которые не выгружаются из CRM но есть на CREMAP - снимаем активность
$delCartArr = array();
$arFilter = array('IBLOCK_ID'=>1, 'ACTIVE'=>'Y', '!PROPERTY_CRMID'=>$arCardID, 'PROPERTY_TYPEOBJ'=>array(1));
$select = array('ID','NAME', 'ACTIVE');
$res = CIBlockElement::GetList($sort, $arFilter, false, false, $select);
while($ob = $res->GetNextElement())
{
$card = $ob->GetFields();
$delCartArr[$card['ID']] = $card['NAME'].' - '.$card['ACTIVE'];
$elUpdate = new CIBlockElement;
$arLoadProductArray = Array(
"ACTIVE" => "N",
);
$resUpdate = $elUpdate->Update($card['ID'], $arLoadProductArray);
}
$propListType = get_type_props(array(1));
//na($arCity);
global $USER;
foreach($xml->offer as $el)
{
$crm_id = (int)$el->ID;
// if ($crm_id != 61161) continue;
// na($crm_id);
$card = isset($arCard[$crm_id]['prop']) ? $arCard[$crm_id]['prop'] : false;
$card_id = $arCard[$crm_id]['ID'];
$name = (string)$el->NAME;
// $active = (string)$el->ACTIVE=='Y' ? 'Y' : 'N';
//na($card);
//na($name);
if($card AND $card_id)
{
$card_field = new CIBlockElement;
$arLoadCard = array();
$detailPic = false;
//if($active != $arCard[$crm_id]['ACTIVE'] AND $active == 'Y') {
if($arCard[$crm_id]['ACTIVE'] != 'Y') {
$arLoadCard['MODIFIED_BY'] = 1;
//$arLoadCard['ACTIVE'] = $active;
$arLoadCard['ACTIVE'] = 'Y';
}
// $act_val = $active=='Y' ? 'Y' : false;
// if($act_val != $card['ADMINACTIVE']['VALUE'])
// {
// $enum_list = get_ib_enum_props('ADMINACTIVE', 1, false, 'value');
// $props['ADMINACTIVE'] = $enum_list[$act_val];
// if($save == 1)
// CIBlockElement::SetPropertyValues($card_id, 1, $enum_list[$act_val], 'ADMINACTIVE');
// }
if((string)$el->DETAIL_PICTURE)
{
$str_detail_pic = array_pop(explode('/', (string)$el->DETAIL_PICTURE));
$el->DETAIL_PICTURE = str_replace('/upload/', '/upload/crm_iblock/', (string)$el->DETAIL_PICTURE);
if($arCard[$crm_id]['DETAIL_PICTURE'])
{
// $detailPic = CFile::GetFileArray($arCard[$crm_id]['DETAIL_PICTURE']);
// if($str_detail_pic != $detailPic['FILE_NAME']) {
// $detail_pic_wm = pic_add_watermark((string)$el->DETAIL_PICTURE, 2500, 2500, $pic_p);
// $arLoadCard['DETAIL_PICTURE'] = CFile::MakeFileArray($detail_pic_wm);
// }
}
else
{
if((string)$el->DETAIL_PICTURE)
{
$detail_pic_wm = pic_add_watermark((string)$el->DETAIL_PICTURE, 2500, 2500, $pic_p);
$arLoadCard['DETAIL_PICTURE'] = CFile::MakeFileArray($detail_pic_wm);
}
}
}
elseif(!(string)$el->DETAIL_PICTURE) {
// if ($arCard[$crm_id]['DETAIL_PICTURE']) $arLoadCard['DETAIL_PICTURE'] = array('del'=>'Y');
}
if(sizeof($arLoadCard))
{
if($save == 1)
{
if($field_res = $card_field->Update($card_id, $arLoadCard))
{
if($detail_pic_wm)
unlink($detail_pic_wm);
$arLoadCardPrevPic = array();
if((string)$el->DETAIL_PICTURE)
{
if($arCard[$crm_id]['PREVIEW_PICTURE'])
{
$detailPic = CFile::GetFileArray($arCard[$crm_id]['PREVIEW_PICTURE']);
if($str_detail_pic != $detailPic['FILE_NAME']) {
$preview_pic_wm = pic_add_watermark((string)$el->DETAIL_PICTURE, $preview_pic_w, $preview_pic_h, $preview_pic_param);
$arLoadCardPrevPic['PREVIEW_PICTURE'] = CFile::MakeFileArray($preview_pic_wm);
}
}
else
{
if((string)$el->DETAIL_PICTURE)
{
$preview_pic_wm = pic_add_watermark((string)$el->DETAIL_PICTURE, $preview_pic_w, $preview_pic_h, $preview_pic_param);
$arLoadCardPrevPic['PREVIEW_PICTURE'] = CFile::MakeFileArray($preview_pic_wm);
}
}
}
elseif(!(string)$el->DETAIL_PICTURE) {
if ($arCard[$crm_id]['PREVIEW_PICTURE']) $arLoadCardPrevPic['PREVIEW_PICTURE'] = array('del'=>'Y');
}
if(sizeof($arLoadCardPrevPic)) {
if($field_res = $card_field->Update($card_id, $arLoadCardPrevPic))
{
if($preview_pic_wm)
unlink($preview_pic_wm);
}
}
}
else {
}
}
}
if(sizeof($arLoadCard)) {
//na($card_id);
//na($arLoadCard);
foreach($arLoadCard as $k=>$val) {
if(is_array($val) AND $val['del'] == 'Y') {
$log_str = 'del - '.$k.' ('.$card_id.')';
na($log_str);
if($write_log == 1) write_log_str($log, $log_str);
}
}
if($arLoadCard['ACTIVE']) {
$log_str = 'activate - '.(string)$el->props->TYPEOBJ->VALUE.' ('.(string)$el->NAME.' - '.(int)$el->ID.') ';
na($log_str);
if($write_log == 1) write_log_str($log, $log_str);
}
if($arLoadCard['DETAIL_PICTURE']) {
$log_str = 'write - '.(string)$el->props->TYPEOBJ->VALUE.' DETAIL_PICTURE ('.(string)$el->NAME.' = '.$card_id.')'
.' | '.$arLoadCard['DETAIL_PICTURE']['name'];
na($log_str);
if($write_log == 1) write_log_str($log, $log_str);
}
}
foreach($el->props->children() as $code=>$p)
{
$value = $write = $desc = $enum_list = $del_val = false;
$arPic = $fileName = $arFileWrite = $size_metro = $arValue = array();
if($card[$code]['MULTIPLE'] == 'Y')
{
$desc = (array)$p->DESCRIPTION->DESCRIPTION;
if($code == 'METRO' AND sizeof($card[$code]['DESCRIPTION']) AND $card[$code]['DESCRIPTION']) {
foreach($card[$code]['DESCRIPTION'] as $n=>$sm) {
$size_metro[$card[$code]['VALUE'][$n]] = $sm;
}
}
//сделать desc
//na(array_diff((array)$p->VALUE->VALUE, $card[$code]['VALUE']));
if(($code == 'PHOTOS' OR $code == 'PHOTOSIN') AND $card[$code]['VALUE'] AND sizeof($card[$code]['VALUE']))
{
foreach($card[$code]['VALUE'] as $pic)
{
$picArray = array();
$picArray = CFile::GetFileArray($pic);
$arPic[$code][$picArray['FILE_NAME']] = $picArray;
$fileName[$code][] = $picArray['FILE_NAME'];
}
//na($arPic[$code]);
}
else {
//na($card);
}
foreach((array)$p->VALUE->VALUE as $v)
{
if($code == 'METRO') {
if($arMetro[$v]) {
$value[] = $arMetro[$v];
$arValue[] = array(
'VALUE' => $arMetro[$v],
'DESCRIPTION' => $size_metro[$arMetro[$v]],
);
}
}
elseif($code == 'PHOTOS' OR $code == 'PHOTOSIN')
{
$str_name = $arFile = false;
if($v)
{
$str_name = array_pop(explode('/', $v));
if(in_array($str_name, $fileName[$code])) {
unset($arPic[$code][$str_name]);
}
else
{
$v = str_replace('/upload/', '/upload/crm_iblock/', $v);
$more_pic_wm = pic_add_watermark($v, 2500, 2500, $pic_p);
$arFile = CFile::MakeFileArray($more_pic_wm);
$arFile['MODULE_ID'] = 'iblock';
$value[] = array(
'VALUE' => $arFile,
'DESCRIPTION'=>'',
);
//$break = 1;
if($break) break;
}
}
}
else
{
if(in_array($code, $propListType[$ib]['Y']))
{
$enum_list = get_ib_enum_props($code, $ib, false, 'value');
$value[] = $enum_list[$v];
$str_code = 'VALUE_ENUM_ID';
}
else {
$value[] = $v;
$str_code = '~VALUE';
}
}
//na($v);
}
if(sizeof(array_diff($value, $card[$code]['VALUE']))
OR ($code != 'PHOTOS' AND $code != 'PHOTOSIN' AND sizeof($value) != sizeof($card[$code]['VALUE']))
OR (($code == 'PHOTOS' OR $code == 'PHOTOSIN') AND $value)
OR sizeof($arPic))
{
$write = 1;
if($code == 'METRO') {
$value = $arValue;
$metro_debug_str = false;
foreach($value as $mds) {
$metro_debug_str.='v-'.$mds['VALUE'].' d-'.$mds['DESCRIPTION'].', ';
}
$log_str = 'write - '.$card['TYPEOBJ']['VALUE'].' '.$code.' ('.$name.' = '.$card_id.')'
.' | '.$metro_debug_str.' = '.implode(', ', $card[$code]['VALUE']);
na($log_str);
if($write_log == 1) write_log_str($log, $log_str);
}
else {
if($code == 'PHOTOS' OR $code == 'PHOTOSIN') {
$val = array();
foreach($value as $arVal) {
$val[] = $arVal['VALUE']['name'];
}
}
else {
$val = $value;
}
$log_str = 'write - '.$card['TYPEOBJ']['VALUE'].' '.$code.' ('.$name.' = '.$card_id.')'
.' | '.implode(', ', $val).' = '.implode(', ', $card[$code]['VALUE']);
na($log_str);
if($write_log == 1) write_log_str($log, $log_str);
}
}
//na($code);
}
else
{
$v = (string)$p->VALUE;
if($card[$code]['DESCRIPTION']) {
$log_str = 'miss - '.$code.' ('.$name.' = '.$card_id.') '
.$card[$code]['DESCRIPTION'];
na($log_str);
if($write_log == 1) write_log_str($log, $log_str);
}
//na($code);
if(isset($card[$code]))
{
if($card[$code]['PROPERTY_TYPE'] == 'G')
{
$log_str = $code;
na($log_str);
if($write_log == 1) write_log_str($log, $log_str);
//na($code);
//na($v);
}
elseif($card[$code]['PROPERTY_TYPE'] == 'E')
{
if($code == 'LOCATION' OR $code == 'DISTRICT')
{
if($arDistr[$v] != $card[$code]['VALUE'])
{
$value = $arDistr[$v];
$write = 1;
//if($code != 'DISTRICT')
$log_str = 'write - '.$card['TYPEOBJ']['VALUE'].' '.$code.' ('.$name.' = '.$card_id.')'
.' | '.$arDistr[$v].' = '.$card[$code]['VALUE'];
na($log_str);
if($write_log == 1) write_log_str($log, $log_str);
}
}
elseif($code == 'DIRECTION')
{
if($arDirection[$v] != $card[$code]['VALUE'])
{
$value = $arDirection[$v];
$write = 1;
$log_str = 'write - '.$card['TYPEOBJ']['VALUE'].' '.$code.' ('.$name.' = '.$card_id.')'
.' | '.$arDirection[$v].' = '.$card[$code]['VALUE'];
na($log_str);
if($write_log == 1) write_log_str($log, $log_str);
}
}
else {
$log_str = $code;
na($log_str);
if($write_log == 1) write_log_str($log, $log_str);
}
}
elseif($card[$code]['PROPERTY_TYPE'] == 'L' AND $v != $card[$code]['VALUE'])
{
$enum_list = get_ib_enum_props($code, 1, false, 'value');
$value = $enum_list[$v];
$write = 1;
$log_str = 'write - '.$card['TYPEOBJ']['VALUE'].' '.$code.' ('.$name.' = '.$card_id.')'
.' | '.$v.' = '.$card[$code]['VALUE'];
na($log_str);
if($write_log == 1) write_log_str($log, $log_str);
}
elseif($card[$code]['PROPERTY_TYPE'] == 'S' AND $card[$code]['USER_TYPE'] == 'Date' AND $v != $card[$code]['VALUE'])
{
if ($v != '') {
$value = date("m/d/Y", strtotime($v));
} else {
$value = $v;
}
$write = 1;
$log_str = 'write - '.$card['TYPEOBJ']['VALUE'].' '.$code.' ('.$name.' = '.$card_id.')'
.' | '.$v.' = '.$card[$code]['VALUE'];
na($log_str);
if($write_log == 1) write_log_str($log, $log_str);
}
elseif($v != $card[$code]['VALUE']) {
$value = $v;
$write = 1;
//if($code != 'ENNAME' AND $code != 'ENADSTREET' AND $code != 'ADSTREET' AND $code != 'ADCORP'
// AND $code != 'MAPXY' AND $code != 'SEARCHWORD' AND $code != 'ADSTROEN')
$log_str = 'write - '.$card['TYPEOBJ']['VALUE'].' '.$code.' ('.$name.' = '.$card_id.')'
.' | '.$v.' = '.$card[$code]['VALUE'];
na($log_str);
if($write_log == 1) write_log_str($log, $log_str);
}
}
}
//cho($write);
if($card_id AND $code AND $write)
{
if($save == 1) {
if($code == 'PHOTOS' OR $code == 'PHOTOSIN')
{
//if(sizeof($arPic[$code]))
CIBlockElement::SetPropertyValueCode($card_id, $code, $value);
if(sizeof($arPic[$code]))
{
foreach($arPic[$code] as $del_p) {
if($del_p['ID'])
{
$value_id = array();
$db_props = CIBlockElement::GetProperty(1, $card_id, array(), Array("CODE"=>$code,'VALUE'=>$del_p['ID']));
while($ar_props = $db_props->Fetch()) {
$value_id[$ar_props['VALUE']] = $ar_props['PROPERTY_VALUE_ID'];
}
if($value_id[$del_p['ID']])
{
$log_str = 'delete file - '.$card['TYPEOBJ']['VALUE'].' '.$code.' ('.$name.' = '.$card_id.')';
na($log_str);
if($write_log == 1) write_log_str($log, $log_str);
$del_val = array(
$value_id[$del_p['ID']] => array(
'VALUE' => array(
'MODULE_ID' => 'iblock',
'del' => 'Y',
)
)
);
CIBlockElement::SetPropertyValueCode($card_id, $code, $del_val);
}
}
}
}
}
else {
CIBlockElement::SetPropertyValues($card_id, 1, $value, $code);
}
}
}
if($break) break;
}
}
else
{
$log_str = 'new - '.(string)$el->props->TYPEOBJ->VALUE.' ('.(string)$el->NAME.' '.(int)$el->ID.') ';
na($log_str);
if($write_log == 1) write_log_str($log, $log_str);
//na($el);
$new_counter++;
global $USER;
$add = new CIBlockElement;
$props = array(
'CRMID' => $crm_id,
);
foreach($el->props->children() as $code=>$p)
{
//na($code);
$value = $desc = false;
if($p->VALUE->VALUE)
{
$desc = (array)$p->DESCRIPTION->DESCRIPTION;
//сделать desc
foreach((array)$p->VALUE->VALUE as $v)
{
if($code == 'METRO') {
if($arMetro[$v]) $props[$code][] = $arMetro[$v];
}
elseif($code == 'PHOTOS' OR $code == 'PHOTOSIN')
{
$v = str_replace('/upload/', '/upload/crm_iblock/', $v);
$more_pic_wm = pic_add_watermark($v, 2500, 2500, $pic_p);
$arFile = CFile::MakeFileArray($more_pic_wm);
$arFile['MODULE_ID'] = 'iblock';
$props[$code][] = array(
'VALUE' => $arFile,
'DESCRIPTION'=>'',
);
}
else {
if($v) $props[$code][] = $v;
}
}
}
else
{
$v = (string)$p->VALUE;
if($code == 'LOCATION')
{
if($arDistr[$v]) $props[$code] = $arDistr[$v];
}
elseif($code == 'DIRECTION')
{
if($arDirection[$v]) $props[$code] = $arDirection[$v];
}
elseif($code == 'CLASS' OR $code == 'TYPEOBJ' OR $code == 'ADMINACTIVE')
{
$enum_list = get_ib_enum_props($code, 1, false, 'value');
$props[$code] = $enum_list[$v];
}
else {
$props[$code] = $v;
}
}
}
// if($active == 'Y') {
// $enum_list = get_ib_enum_props('ADMINACTIVE', 1, false, 'value');
// $props['ADMINACTIVE'] = $enum_list['Y'];
// }
$code = translit(trim($name));
$code = mb_strtolower($code, 'UTF-8');
$code = str_replace(' ', '', $code);
$code = str_replace('--', '-', $code);
$code = str_replace('--', '-', $code);
$code = str_replace('--', '-', $code);
//cho($code);
$addNewCard = Array(
'MODIFIED_BY' => 1,
'IBLOCK_SECTION_ID' => 1,
'IBLOCK_ID' => 1,
'PROPERTY_VALUES' => $props,
'NAME' => $name,
'ACTIVE' => (string)$el->ACTIVE,
'SORT' => 500,
'CODE' => $code,
);
//print_r($addNewCard);
if((string)$el->DETAIL_PICTURE) {
$el->DETAIL_PICTURE = str_replace('/upload/', '/upload/crm_iblock/', (string)$el->DETAIL_PICTURE);
$detail_pic_wm = pic_add_watermark((string)$el->DETAIL_PICTURE, 2500, 2500, $pic_p);
$addNewCard['DETAIL_PICTURE'] = CFile::MakeFileArray($detail_pic_wm);
}
if (!$addNewCard['PROPERTY_VALUES']['LETTER']) {
$firstLetter = strtolower(substr($addNewCard['NAME'], 0, 1));
$addNewCard['PROPERTY_VALUES']['LETTER'] = $firstLetter;
}
if($save == 1)
{
if($NEW_ID = $add->Add($addNewCard))
{
$arLoadCard = array();
if((string)$el->DETAIL_PICTURE)
{
$preview_pic_wm = pic_add_watermark((string)$el->DETAIL_PICTURE, $preview_pic_w, $preview_pic_h, $preview_pic_param);
$arLoadCard['PREVIEW_PICTURE'] = CFile::MakeFileArray($preview_pic_wm);
}
if(sizeof($arLoadCard))
{
if($field_res = $card_field->Update($NEW_ID, $arLoadCard))
{
if($detail_pic_wm)
unlink($detail_pic_wm);
if($preview_pic_wm)
unlink($preview_pic_wm);
}
}
}
else {
echo "<br>Ошибка: ".$add->LAST_ERROR;
print_r($addNewCard);
if(strpos($add->LAST_ERROR, 'с таким символьным кодом уже существует') !== false) {
$add2 = new CIBlockElement;
$addNewCard['CODE'] = ($code.$crm_id);
if($NEW_ID = $add2->Add($addNewCard))
{
$arLoadCard = array();
if((string)$el->DETAIL_PICTURE)
{
$preview_pic_wm = pic_add_watermark((string)$el->DETAIL_PICTURE, $preview_pic_w, $preview_pic_h, $preview_pic_param);
$arLoadCard['PREVIEW_PICTURE'] = CFile::MakeFileArray($preview_pic_wm);
}
if(sizeof($arLoadCard))
{
if($field_res = $card_field->Update($NEW_ID, $arLoadCard))
{
if($detail_pic_wm)
unlink($detail_pic_wm);
if($preview_pic_wm)
unlink($preview_pic_wm);
}
}
}
}
}
}
}
if($more_pic_wm)
unlink($more_pic_wm);
// $arFilter = array('IBLOCK_ID'=>1, 'NAME'=>$el->NAME);
// $select = array('ID',);
// $res = CIBlockElement::GetList($sort, $arFilter, false, false, $select);
// if($ob = $res->GetNextElement())
// {
// $card = $ob->GetFields();
// if($card['ID'] AND (int)$el->ID) {
// na($card['ID']);
// na((int)$el->ID);
// //CIBlockElement::SetPropertyValues($card['ID'], 1, (int)$el->ID, 'CRMID');
// }
// }
// break;
}
}
else
{
exit('Не удалось открыть файл');
}
if($write_log == 2) cho($new_counter);
require_once $_SERVER["DOCUMENT_ROOT"].'/bitrix/modules/main/include/epilog_after.php';
以上是关于php 脚本读取xml文件并将项添加到db import object mcity的主要内容,如果未能解决你的问题,请参考以下文章
使用 StAX / Kettle (Pentaho) 读取 XML 文件