Skip to content

Instantly share code, notes, and snippets.

@maximzasorin
Created October 30, 2015 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maximzasorin/6054249de3f542f6c7f6 to your computer and use it in GitHub Desktop.
Save maximzasorin/6054249de3f542f6c7f6 to your computer and use it in GitHub Desktop.
Transfer informationsystem items to shop group / Перенос элементов ИС в группу магазина (HostCMS)
<?php
class TransferItems {
// private $_propertyDir = 5;
private $_propertyDir = 0;
private $_informationsystem = 57;
private $_shopGroupId = 12012;
private $_transferedItems = 0;
public function makeTransfer() {
$this->_transferGroups(
Core_Entity::factory('shop_group', $this->_shopGroupId)
);
print 'transfered = ' . $this->_transferedItems;
}
// Переносим группы информационной системы в подгруппу $shopGroup
private function _transferGroups($shopGroup)
{
$this->_transferChildGroups(0, $shopGroup);
}
// Переносим подгруппы группы c индентиф. $parentInformationsystemId в группу $parentShopGroup
private function _transferChildGroups($parentInformationsystemId = 0, $parentShopGroup)
{
$oInformationsystemGroups = Core_Entity::factory('informationsystem_group');
$oInformationsystemGroups
->queryBuilder()
->where('parent_id', '=', $parentInformationsystemId)
->where('informationsystem_id', '=', $this->_informationsystem);
$aoInformationsystemGroups = $oInformationsystemGroups
->findAll();
foreach ($aoInformationsystemGroups as $oInformationsystemGroup)
{
$oShopGroup = $this->_findSameGroup($oInformationsystemGroup->name, $parentShopGroup);
if (!$oShopGroup)
{
// Создаем подгруппу в интернет-магазине с таким же именем
$oShopGroup = Core_Entity::factory('shop_group');
$oShopGroup->shop_id = $parentShopGroup->shop_id;
$oShopGroup->parent_id = $parentShopGroup->id;
$oShopGroup->name = $oInformationsystemGroup->name;
$oShopGroup->path = '';
$oShopGroup->sorting = $oInformationsystemGroup->sorting;
$oShopGroup->active = $oInformationsystemGroup->active;
$oShopGroup->save();
// Переносим изображения
$this->_transferImages($oInformationsystemGroup, $oShopGroup);
}
// Переносим элементы
$this->_transferItems(
$oInformationsystemGroup->Informationsystem_Items
->findAll(),
$oShopGroup
);
// Переносим подгруппы
$this->_transferChildGroups($oInformationsystemGroup->id, $oShopGroup);
}
}
//
private function _transferItems($aoInformationsystemItems, $oShopGroup)
{
foreach ($aoInformationsystemItems as $oInformationsystemItem)
{
/*if ($oInformationsystemItem->id != 914)
{
continue;
}*/
$oShopItem = $this->_findSameItem($oInformationsystemItem->name, $oShopGroup);
if (!$oShopItem)
{
// Создаем товар с данными инфрмационного элемента
$oShopItem = Core_Entity::factory('shop_item');
$oShopItem->shop_id = $oShopGroup->shop_id;
$oShopItem->shop_group_id = $oShopGroup->id;
$oShopItem->name = $oInformationsystemItem->name;
$oShopItem->guid = Core_Guid::get();
$oShopItem->type = 0;
$oShopItem->showed = $oInformationsystemItem->showed;
$oShopItem->sorting = $oInformationsystemItem->sorting;
$oShopItem->save();
// Артикул
$oMarkingProperty = Core_Entity::factory('property', 334);
$oMarkingValues = $oMarkingProperty->getValues($oInformationsystemItem->id);
if ($oMarkingValues && count($oMarkingValues) > 0)
{
$oShopItem->marking = $oMarkingValues[0]->value;
$oShopItem->save();
}
// Цена
$oPriceProperty = Core_Entity::factory('property', 557);
$oPriceValues = $oPriceProperty->getValues($oInformationsystemItem->id);
if ($oPriceValues && count($oPriceValues) > 0)
{
$oShopItem->price = $oPriceValues[0]->value;
$oShopItem->save();
}
// Переносим изображения
$this->_transferImages($oInformationsystemItem, $oShopItem);
}
// Переносим дополнительные свойства
$this->_transferProperties($oInformationsystemItem, $oShopItem, array(334, 557, 798));
// Добавляем доп. свойство в инф. элемент со ссылкой на товар
$oRedirectProperty = Core_Entity::factory('property', 798);
$oRedirectValues = $oRedirectProperty->getValues($oInformationsystemItem->id);
$oRedirectValue = count($oRedirectValues) > 0
? $oRedirectValues[0]
: $oRedirectProperty->createNewValue($oInformationsystemItem->id);
$oRedirectValue->value = 'http://zarnitza.ru' . $oShopItem->Shop->Structure->getPath() . $oShopItem->getPath();
$oRedirectValue->save();
$this->_transferedItems++;
// exit();
}
}
private function _transferProperties($oInformationsystemItem, $oShopItem, $excludedProperties = array())
{
// Удаляем значения доп. свойств товара
$aPropertyValues = $oShopItem->getPropertyValues(FALSE);
foreach($aPropertyValues as $oPropertyValue)
{
$oPropertyValue->Property->type == 2 && $oPropertyValue->setDir($oShopItem->getItemPath());
$oPropertyValue->delete();
}
$aoPropertyValues = $oInformationsystemItem->getPropertyValues();
foreach ($aoPropertyValues as $oPropertyValue)
{
if (!in_array($oPropertyValue->property_id, $excludedProperties))
{
$oProperty = $this->_findSameProperty($oPropertyValue->Property->name, $oShopItem);
if (!$oProperty)
{
$oProperty = Core_Entity::factory('property');
$oProperty->shop_id = $oShopItem->shop_id;
$oProperty->property_dir_id = $this->_propertyDir;
$oProperty->guid = Core_Guid::get();
$oProperty->name = $oPropertyValue->Property->name;
$oProperty->type = $oPropertyValue->Property->type;
$oProperty->description = $oPropertyValue->Property->description;
$oProperty->multiple = $oPropertyValue->Property->multiple;
$oProperty->tag_name = $oPropertyValue->Property->tag_name;
$oProperty->save();
// Связываем доп. свойство с товарами
$oProperty
->add(
Core_Entity::factory('shop_item_property')
->shop_id($oShopItem->shop_id)
->property_id($oProperty->id)
->show_in_item(1)
->show_in_group(1)
->save()
);
}
// Разрешаем доп. свойство группе
$oShopItem->Shop
->Shop_Item_Property_For_Groups
->allowAccess($oProperty->Shop_Item_Property->id, $oShopItem->shop_group_id);
$oNewPropertyValue = clone $oPropertyValue;
$oNewPropertyValue->entity_id = $oShopItem->id;
$oNewPropertyValue->property_id = $oProperty->id;
$oNewPropertyValue->save();
if ($oNewPropertyValue->Property->type == 2)
{
$oPropertyValue->setDir($oInformationsystemItem->getItemPath());
$oNewPropertyValue->setDir($oShopItem->getItemPath());
if (is_file($oPropertyValue->getLargeFilePath()))
{
try
{
$oNewPropertyValue->file = 'shop_items_property_' . $oNewPropertyValue->id . '.' .
Core_File::getExtension($oPropertyValue->file);
Core_File::copy($oPropertyValue->getLargeFilePath(), $oNewPropertyValue->getLargeFilePath());
$oNewPropertyValue->save();
} catch (Exception $e) {}
}
if (is_file($oPropertyValue->getSmallFilePath()))
{
try
{
$oNewPropertyValue->file_small = 'small_shop_items_property_' . $oNewPropertyValue->id . '.' .
Core_File::getExtension($oPropertyValue->file_small);
Core_File::copy($oPropertyValue->getSmallFilePath(), $oNewPropertyValue->getSmallFilePath());
$oNewPropertyValue->save();
} catch (Exception $e) {}
}
}
}
}
}
private function _transferImages($sourceObject, $destObject)
{
if (is_file($sourceObject->getLargeFilePath()))
{
try
{
$destObject->createDir();
$destObject->image_large = $destObject->getTableName() . '_' . $destObject->id . '.' .
Core_File::getExtension($sourceObject->image_large);
Core_File::copy($sourceObject->getLargeFilePath(), $destObject->getLargeFilePath());
$destObject->save();
}
catch (Exception $e) {}
}
if (is_file($sourceObject->getSmallFilePath()))
{
try
{
$destObject->createDir();
$destObject->image_small = 'small_' . $destObject->getTableName() . '_' . $destObject->id . '.' .
Core_File::getExtension($sourceObject->image_small);
Core_File::copy($sourceObject->getSmallFilePath(), $destObject->getSmallFilePath());
$destObject->save();
}
catch (Exception $e) {}
}
}
//
private function _findSameGroup($shopGroupName, $oParentShopGroup)
{
$oShopGroup = Core_Entity::factory('shop_group');
$oShopGroup->queryBuilder()
->where('name', '=', $shopGroupName)
->where('parent_id', '=', $oParentShopGroup->id)
->where('shop_id', '=', $oParentShopGroup->shop_id);
$aoShopGroup = $oShopGroup->findAll();
return count($aoShopGroup) > 0
? $aoShopGroup[0]
: false;
}
private function _findSameItem($shopItemName, $oParentShopGroup)
{
$oShopItem = Core_Entity::factory('shop_item');
$oShopItem->queryBuilder()
->where('name', '=', $shopItemName)
->where('shop_group_id', '=', $oParentShopGroup->id)
->where('shop_id', '=', $oParentShopGroup->shop_id);
$aoShopItem = $oShopItem->findAll();
return count($aoShopItem) > 0
? $aoShopItem[0]
: false;
}
private function _findSameProperty($propertyName, $oShopItem)
{
$oProperty = Core_Entity::factory('property');
$oProperty->queryBuilder()
->join('shop_item_properties',
'shop_item_properties.property_id', '=', 'properties.id')
->where('shop_item_properties.shop_id', '=', $oShopItem->shop_id)
->where('deleted', '=', 0)
->where('name', '=', $propertyName)
->where('property_dir_id', '=', $this->_propertyDir)
;
$aoProperty = $oProperty->findAll();
return count($aoProperty) > 0
? $aoProperty[0]
: false;
}
}
header('Content-Type: text/html; charset=utf-8');
$transferItems = new TransferItems;
$transferItems->makeTransfer();
exit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment