Last active
October 30, 2015 09:46
-
-
Save maximzasorin/8b91914b4c3231e4646a to your computer and use it in GitHub Desktop.
Transfer shop items from one shop to group in another shop / Перенос товаров одного магазина в группу другого (HostCMS)
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 | |
class TransferShopItems | |
{ | |
private $_propertyDir = 0; | |
private $_sourceShop = 5; | |
private $_destShopGroup = 12591; | |
private $_transferedItems = 0; | |
public function makeTransfer() | |
{ | |
$oShop = Core_Entity::factory('shop', $this->_sourceShop); | |
$oShopItems = $oShop->Shop_Items; | |
$oShopItems | |
->queryBuilder() | |
->where('deleted', '=', 0) | |
->where('active', '=', 1); | |
print 'in source shop = ' . count($oShopItems->findAll()) . '<br />'; | |
$this->_transferShopGroups( | |
Core_Entity::factory('shop_group', $this->_destShopGroup) | |
); | |
print 'transfered = ' . $this->_transferedItems; | |
} | |
public function _transferShopGroups($oDestShopGroup) | |
{ | |
$this->_transferChildShopGroups(0, $oDestShopGroup); | |
} | |
private function _transferChildShopGroups($oSourceShopGroupId = 0, $oDestParentShopGroup) | |
{ | |
$oSourceShopGroups = Core_Entity::factory('shop_group'); | |
$oSourceShopGroups | |
->queryBuilder() | |
->where('parent_id', '=', $oSourceShopGroupId) | |
->where('shop_id', '=', $this->_sourceShop); | |
$aoSourceShopGroups = $oSourceShopGroups->findAll(); | |
foreach ($aoSourceShopGroups as $oSourceShopGroup) | |
{ | |
$oDestShopGroup = $this->_findSameGroup($oSourceShopGroup->name, $oDestParentShopGroup); | |
if (!$oDestShopGroup) | |
{ | |
$oDestShopGroup = clone $oSourceShopGroup; | |
$oDestShopGroup->path = ''; | |
$oDestShopGroup->guid = Core_Guid::get(); | |
$oDestShopGroup->shop_id = $oDestParentShopGroup->shop_id; | |
$oDestShopGroup->parent_id = $oDestParentShopGroup->id; | |
$oDestShopGroup->save(); | |
// Переносим изображения | |
$this->_transferImages($oSourceShopGroup, $oDestShopGroup); | |
} | |
// Переносим дополнительные свойства | |
$this->_transferProperties($oSourceShopGroup, $oDestShopGroup); | |
// Переносим товары | |
$this->_transferShopItems($oSourceShopGroup, $oDestShopGroup); | |
// Переносим подгруппы | |
$this->_transferChildShopGroups($oSourceShopGroup->id, $oDestShopGroup); | |
} | |
} | |
private function _transferShopItems($oSourceShopGroup, $oDestShopGroup) | |
{ | |
$aoSourceShopItems = $oSourceShopGroup->Shop_Items->findAll(); | |
foreach ($aoSourceShopItems as $oSourceShopItem) | |
{ | |
$oDestShopItem = $this->_findSameItem($oSourceShopItem->name, $oDestShopGroup); | |
if (!$oDestShopItem) | |
{ | |
$oDestShopItem = clone $oSourceShopItem; | |
$oDestShopItem->shop_id = $oDestShopGroup->shop_id; | |
$oDestShopItem->shop_group_id = $oDestShopGroup->id; | |
$oDestShopItem->guid = Core_Guid::get(); | |
$oDestShopItem->path = ''; | |
$oDestShopItem->save(); | |
// Переносим изображения | |
$this->_transferImages($oSourceShopItem, $oDestShopItem); | |
} | |
// exit(); | |
// Переносим дополнительные свойства | |
$this->_transferProperties($oSourceShopItem, $oDestShopItem, array(80, 817)); | |
// Добавляем доп. свойство в инф. элемент со ссылкой на товар | |
$oRedirectProperty = Core_Entity::factory('property', 817); | |
// Разрешаем доп. свойство редиректа в группе | |
/*$oDestShopItem->Shop | |
->Shop_Item_Property_For_Groups | |
->allowAccess($oRedirectProperty->Shop_Item_Property->id, $oSourceShopItem->shop_group_id);*/ | |
$oRedirectValues = $oRedirectProperty->getValues($oSourceShopItem->id); | |
$oRedirectValue = count($oRedirectValues) > 0 | |
? $oRedirectValues[0] | |
: $oRedirectProperty->createNewValue($oSourceShopItem->id); | |
$oRedirectValue->value = 'http://zarnitza.ru' . $oDestShopItem->Shop->Structure->getPath() . $oDestShopItem->getPath(); | |
$oRedirectValue->save(); | |
$this->_transferedItems++; | |
} | |
} | |
// | |
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 _transferProperties($oSourceObject, $oDestObject, $excludedProperties = array()) | |
{ | |
// Удаляем значения доп. свойств товара | |
$aPropertyValues = $oDestObject->getPropertyValues(FALSE); | |
foreach($aPropertyValues as $oPropertyValue) | |
{ | |
if ($oPropertyValue->Property->type == 2) | |
{ | |
$oPropertyValue->setDir($oDestObject->getItemPath()); | |
} | |
$oPropertyValue->delete(); | |
} | |
$aoPropertyValues = $oSourceObject->getPropertyValues(); | |
foreach ($aoPropertyValues as $oPropertyValue) | |
{ | |
if (!in_array($oPropertyValue->property_id, $excludedProperties)) | |
{ | |
$oProperty = $this->_findSameProperty($oPropertyValue->Property->name, $oDestObject); | |
if (!$oProperty) | |
{ | |
$oProperty = clone $oPropertyValue->Property; | |
$oProperty->shop_id = $oDestObject->shop_id; | |
$oProperty->property_dir_id = $this->_propertyDir; | |
$oProperty->guid = Core_Guid::get(); | |
$oProperty->save(); | |
// Связываем доп. свойство с товарами | |
$objectProperty = Core_Entity::factory($oDestObject->getModelName() . '_property') | |
->shop_id($oDestObject->shop_id) | |
->property_id($oProperty->id); | |
if ($oDestObject->getModelName() == 'shop_item') | |
{ | |
$objectProperty->show_in_item = 1; | |
} | |
$oProperty | |
->add( | |
$objectProperty | |
->save() | |
); | |
} | |
// Разрешаем доп. свойство группе | |
if ($oDestObject->getModelName() == 'shop_item') | |
{ | |
$oDestObject->Shop | |
->Shop_Item_Property_For_Groups | |
->allowAccess($oProperty->Shop_Item_Property->id, $oDestObject->shop_group_id); | |
} | |
$oNewPropertyValue = clone $oPropertyValue; | |
$oNewPropertyValue->entity_id = $oDestObject->id; | |
$oNewPropertyValue->property_id = $oProperty->id; | |
$oNewPropertyValue->save(); | |
if ($oNewPropertyValue->Property->type == 2) | |
{ | |
$oPropertyValue->setDir($oSourceObject->getItemPath()); | |
$oNewPropertyValue->setDir($oDestObject->getItemPath()); | |
if (is_file($oPropertyValue->getLargeFilePath())) | |
{ | |
try | |
{ | |
$oNewPropertyValue->file = $oDestObject->getTableName() . '_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_' . $oDestObject->getTableName() . '_property_' . $oNewPropertyValue->id . '.' . | |
Core_File::getExtension($oPropertyValue->file_small); | |
Core_File::copy($oPropertyValue->getSmallFilePath(), $oNewPropertyValue->getSmallFilePath()); | |
$oNewPropertyValue->save(); | |
} catch (Exception $e) {} | |
} | |
} | |
} | |
// exit(); | |
} | |
} | |
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, $object) | |
{ | |
$propertiesTable = $object->getModelName() . '_properties'; | |
$oProperty = Core_Entity::factory('property'); | |
$oProperty->queryBuilder() | |
->join($propertiesTable, | |
$propertiesTable . '.property_id', '=', 'properties.id') | |
->where($propertiesTable . '.shop_id', '=', $object->shop_id) | |
->where('deleted', '=', 0) | |
->where('name', '=', $propertyName) | |
->where('property_dir_id', '=', $this->_propertyDir) | |
; | |
$aoProperty = $oProperty->findAll(); | |
return count($aoProperty) > 0 | |
? $aoProperty[0] | |
: false; | |
} | |
} | |
$transferShopItems = new TransferShopItems; | |
$transferShopItems->makeTransfer(); | |
exit(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment