[bitrix] import images

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
set_time_limit(0);
CModule::IncludeModule("iblock");
use \Verdant\XML2Array;
$el = new CIBlockElement();
$path = $_SERVER["DOCUMENT_ROOT"] . "/upload/full_export_iblock_12/products.xml";
$handle = fopen($path, 'r+');
if (!$handle) {
echo "файл не открылся<br />";
} else {
$content = fread($handle, filesize($path));
fclose($handle);
$content=str_replace("&","&amp;",$content);
if ($content) { // получили данные торговых предложений
$data = XML2Array::createArray($content);
$cnt = 0;
$not_updated = array();
$not_found = array();
foreach($data["products"]["item"] as $product) {
$result = null;
if($product["name"]) {
$result["name"] = str_replace("&amp;", "&", trim($product["name"]));
$result["code"] = str_replace("&amp;", "&", trim($product["code"]));
$result["preview_text"] = html_entity_decode(str_replace("&amp;", "&", trim($product["preview_text"])));
$result["detail_text"] = html_entity_decode(str_replace("&amp;", "&", trim($product["detail_text"])));
$result["megavisor"] = str_replace("&amp;", "&", trim($product["megavisor"]));
$result["video"] = str_replace("&amp;", "&", trim($product["video"]));
$res = CIBlockElement::GetList(
array("show_counter" => "desc", "ID" => "ASC"),
array(
"IBLOCK_ID" => $OPTIONS::getInstance()->getProperty("iblock_products"),
"=NAME" => $result["name"]
),
false,
array("nTopCount" => 1),
array("ID","IBLOCK_ID")
);
if (($fields = $res->Fetch())) {
$ar2save = array(
"NAME"=>$result["name"],
"CODE"=>$result["code"],
"PREVIEW_TEXT"=>$result["preview_text"],
"DETAIL_TEXT"=>$result["detail_text"],
);
if($product["preview_picture"]) {
$ar2save["PREVIEW_PICTURE"] = CFile::MakeFileArray($_SERVER["DOCUMENT_ROOT"] . $product["preview_picture"]);
}
if($product["detail_picture"]) {
$ar2save["DETAIL_PICTURE"] = CFile::MakeFileArray($_SERVER["DOCUMENT_ROOT"] . $product["detail_picture"]);
}
if($result["megavisor"])
CIBlockElement::SetPropertyValueCode($fields["ID"], "MEGAVISOR", $result["megavisor"]);
if($result["video"])
CIBlockElement::SetPropertyValueCode($fields["ID"], "VIDEO_URL", $result["video"]);
if($product["more_pictures"]) {
$images = array();
foreach($product["more_pictures"]["file"] as $img) {
$imgEdit = str_replace("&amp;", "&", trim($img));
$images[] = array("VALUE" => CFile::MakeFileArray($_SERVER["DOCUMENT_ROOT"].$imgEdit),"DESCRIPTION"=>"");
}
CIBlockElement::SetPropertyValuesEx($fields["ID"], $OPTIONS::getInstance()->getProperty("iblock_products"), array("MORE_PHOTO" => $images));
}
if($el->Update($fields["ID"], $ar2save)) {
$cnt++;
} else {
$not_updated[] = $fields["ID"];
}
} else {
$not_found[] = $result["name"];
}
}
}
//DebugInfo::d($product);
echo "Обновлено: " . $cnt;
}
}
DebugInfo::d($not_found,"не найдено!");
DebugInfo::d($not_updated,"не обновлено!");