used cars process update: add argument support for reload images

This commit is contained in:
merelendor 2024-04-30 17:45:11 +03:00
parent a75b75f402
commit 0467da1473

View File

@ -23,6 +23,9 @@ use Bitrix\Seo\SitemapRuntimeTable;
Main\Loader::includeModule('seo');
ob_get_clean();
error_reporting(E_ERROR);
$arrContextOptions = [
"ssl" => [
"verify_peer"=>false,
@ -209,13 +212,13 @@ while($model_search_ob = $models_search_res->GetNextElement())
$bitrix_existed_models[] = $model_existed['ID'];
}
print "bitrix_existed_models:\n";
print_r($bitrix_existed_models);
print "\n\n";
//print "bitrix_existed_models:\n";
//print_r($bitrix_existed_models);
//print "\n\n";
print "bitrix_models:\n";
print_r($bitrix_models);
print "\n\n";
//print "bitrix_models:\n";
//print_r($bitrix_models);
//print "\n\n";
//добавляем новые марки из api
@ -226,7 +229,7 @@ foreach($api_data AS $brand_uid => $brand)
$brand_element = new CIBlockElement;
$brand_payload = [ "IBLOCK_ID" => IBLOCK_ID_BRANDS_USED, "NAME" => $brand['name'], "CODE" => $code, "PROPERTY_VALUES" => [ "UID" => $brand_uid ], "ACTIVE" => "Y", ];
print $brand['name']." - ".$brand_uid."\n";
// print $brand['name']." - ".$brand_uid."\n";
if($brand_new_id = $brand_element->Add($brand_payload))
{
$bitrix_brands[ $brand_uid ] = $brand_new_id;
@ -243,11 +246,11 @@ foreach($api_data AS $brand_uid => $brand)
$el->Update($brand_existed['ID'], ["ACTIVE" => "Y"]);
}
}
print "\n";
// print "\n";
print_r($brand['models']);
print "\n";
print "\n";
// print_r($brand['models']);
// print "\n";
// print "\n";
//добавляем новые модели из api
foreach($brand['models'] AS $model_uid => $model)
@ -273,12 +276,14 @@ foreach($api_data AS $brand_uid => $brand)
$el->Update($model_existed['ID'], ["ACTIVE" => "Y"]);
}
}
print "\n";
// print "\n";
}
}
function resize_image($image, $filename, $width, $height, $quality)
{
print "resize_image ".$filename." | ".$width."x".$height."\n";
$tempfile = tmpfile();
fwrite($tempfile, $image);
$tempfile_metadata = stream_get_meta_data($tempfile);
@ -305,8 +310,12 @@ function resize_image($image, $filename, $width, $height, $quality)
imagedestroy($resized);
imagedestroy($panel);
print "resize_image | make file array\n";
$file = CFile::MakeFileArray($tempfile_filename);
$file['name'] = $filename.".".$ext;
print "resize_image | make file array\n";
$saved_file_id = CFile::SaveFile($file, "used");
fclose($tempfile);
@ -314,6 +323,83 @@ function resize_image($image, $filename, $width, $height, $quality)
return $saved_file_id;
}
function set_images($code, $car_id, $delete = false, $properties = [])
{
if($delete)
{
if($properties['PHOTO']['PROPERTY_VALUE_ID'] !== "")
{
CIBlockElement::SetPropertyValueCode($car_id, "PHOTO", [ $properties['PHOTO']['PROPERTY_VALUE_ID'] => [ "VALUE" => [ "MODULE_ID" => "iblock", "del" => "Y" ] ] ] );
}
if(is_array($properties['PHOTOS_1080']['PROPERTY_VALUE_ID']) && count($properties['PHOTOS_1080']['PROPERTY_VALUE_ID']) > 0)
{
$photos = [];
foreach($properties['PHOTOS_1080']['PROPERTY_VALUE_ID'] AS $k => $v)
{
$photos[ $v ] = [ "VALUE" => [ "MODULE_ID" => "iblock", "del" => "Y" ] ];
}
CIBlockElement::SetPropertyValueCode($car_id, "PHOTOS_1080", $photos );
}
if(is_array($properties['PHOTOS_480']['PROPERTY_VALUE_ID']) && count($properties['PHOTOS_480']['PROPERTY_VALUE_ID']) > 0)
{
$photos = [];
foreach($properties['PHOTOS_480']['PROPERTY_VALUE_ID'] AS $k => $v)
{
$photos[ $v ] = [ "VALUE" => [ "MODULE_ID" => "iblock", "del" => "Y" ] ];
}
CIBlockElement::SetPropertyValueCode($car_id, "PHOTOS_480", $photos );
}
if(is_array($properties['PHOTOS_120']['PROPERTY_VALUE_ID']) && count($properties['PHOTOS_120']['PROPERTY_VALUE_ID']) > 0)
{
$photos = [];
foreach($properties['PHOTOS_120']['PROPERTY_VALUE_ID'] AS $k => $v)
{
$photos[ $v ] = [ "VALUE" => [ "MODULE_ID" => "iblock", "del" => "Y" ] ];
}
CIBlockElement::SetPropertyValueCode($car_id, "PHOTOS_120", $photos );
}
}
$images = zerof_get_images($code);
if(count($images) > 0)
{
$photo_property_value = null;
$photos_1080_property_values = [];
$photos_480_property_values = [];
$photos_120_property_values = [];
foreach($images AS $k => $image)
{
$saved_file_id_1080 = resize_image($image, $code."_".($k + 1)."_1080", 1440, 1080, 60);
$saved_file_id_480 = resize_image($image, $code."_".($k + 1)."_480", 640, 480, 60);
$saved_file_id_120 = resize_image($image, $code."_".($k + 1)."_120", 160, 120, 60);
$photos_1080_property_values[] = [ "VALUE" => $saved_file_id_1080, ];
$photos_480_property_values[] = [ "VALUE" => $saved_file_id_480, ];
$photos_120_property_values[] = [ "VALUE" => $saved_file_id_120, ];
if($k === 0)
{
$photo_property_value = resize_image($image, $code."_".($k + 1)."_500", 500, 300, 60);
}
}
print "set images | ".$code."\n";
CIBlockElement::SetPropertyValueCode($car_id, "PHOTO", $photo_property_value);
CIBlockElement::SetPropertyValueCode($car_id, "PHOTOS_1080", $photos_1080_property_values);
CIBlockElement::SetPropertyValueCode($car_id, "PHOTOS_480", $photos_480_property_values);
CIBlockElement::SetPropertyValueCode($car_id, "PHOTOS_120", $photos_120_property_values);
}
print "set_images END\n";
return;
}
$c = 0;
foreach($json as $car)
{
@ -369,36 +455,7 @@ foreach($json as $car)
if($new_used_car_id = $used_car_element->Add($used_car_payload))
{
//добавляем новое предложение в bitrix
$images = zerof_get_images($code);
if(count($images) > 0)
{
$photo_property_value = null;
$photos_1080_property_values = [];
$photos_480_property_values = [];
$photos_120_property_values = [];
foreach($images AS $k => $image)
{
$saved_file_id_1080 = resize_image($image, $code."_".($k + 1)."_1080", 1440, 1080, 60);
$saved_file_id_480 = resize_image($image, $code."_".($k + 1)."_480", 640, 480, 60);
$saved_file_id_120 = resize_image($image, $code."_".($k + 1)."_120", 160, 120, 60);
$photos_1080_property_values[] = [ "VALUE" => $saved_file_id_1080, ];
$photos_480_property_values[] = [ "VALUE" => $saved_file_id_480, ];
$photos_120_property_values[] = [ "VALUE" => $saved_file_id_120, ];
if($k === 0)
{
$photo_property_value = resize_image($image, $code."_".($k + 1)."_500", 500, 300, 60);
}
}
CIBlockElement::SetPropertyValueCode($new_used_car_id, "PHOTO", $photo_property_value);
CIBlockElement::SetPropertyValueCode($new_used_car_id, "PHOTOS_1080", $photos_1080_property_values);
CIBlockElement::SetPropertyValueCode($new_used_car_id, "PHOTOS_480", $photos_480_property_values);
CIBlockElement::SetPropertyValueCode($new_used_car_id, "PHOTOS_120", $photos_120_property_values);
}
set_images($code, $new_used_car_id, false);
print "\n";
}
@ -409,10 +466,16 @@ foreach($json as $car)
while($car_search_ob = $cars_search_res->GetNextElement())
{
$car_existed = $car_search_ob->GetFields();
$car_existed['PROPERTIES'] = $car_search_ob->GetProperties();
$el = new CIBlockElement;
$el->Update($car_existed['ID'], ["ACTIVE" => "Y", "NAME" => $name, "SORT" => 500]);
if(in_array("updateimages", $argv))
{
set_images($code, $car_existed['ID'], true, $car_existed['PROPERTIES']);
}
CIBlockElement::SetPropertyValuesEx($car_existed['ID'], IBLOCK_ID_CATALOG_CARS_USED, $PROPERTY_VALUES, []);
}
}
@ -423,9 +486,10 @@ foreach($json as $car)
//скрываем предложения, которые есть в bitrix но которых более нет в api
foreach($bitrix_cars AS $code => $car_id)
{
print "CAR ".$code." => ".$car_id."\n";
if(!in_array($code, $api_cars))
{
print "HIDE CAR ".$code." => ".$car_id."\n";
$el = new CIBlockElement;
$el->Update($car_id, ["ACTIVE" => "N", "SORT" => 1]);
}
@ -434,9 +498,10 @@ foreach($bitrix_cars AS $code => $car_id)
//скрываем марки, которые есть в bitrix но которых более нет в api
foreach($bitrix_existed_brands AS $brand_id)
{
print "BRAND ".$ibrand_idd."\n";
if(!in_array($brand_id, $api_existed_brands))
{
print "HIDE BRAND ".$ibrand_id."\n";
$el = new CIBlockElement;
$el->Update($brand_id, ["ACTIVE" => "N"]);
}
@ -445,9 +510,10 @@ foreach($bitrix_existed_brands AS $brand_id)
//скрываем модели, которые есть в bitrix но которых более нет в api
foreach($bitrix_existed_models AS $model_id)
{
print "MODEL ".$model_id."\n";
if(!in_array($model_id, $api_existed_models))
{
print "HIDE MODEL ".$model_id."\n";
$el = new CIBlockElement;
$el->Update($model_id, ["ACTIVE" => "N"]);
}