<?php
/**
* Subclass for representing a row from the 'product' table.
*
*
*
* @package lib.model.trustdev
*/
class Product extends BaseProduct
{
private $_category_id_old = null;
/**
* Сохраняем старую категорию.
*
* @param int $v
* @return Product
*/
public function setCategoryId ($v)
{
// для уверенности [sergey]
if(is_null($this->_category_id_old)) $this->_category_id_old = $this->getCategoryId();
return parent::setCategoryId($v);
}
public function getCategoryIdOld()
{
return $this->_category_id_old;
}
public function getStatusTitle()
{
if (!$status = $this->getProductStatus())
return '';
return (string) $status;
}
public function getShopTitle()
{
return $this->getShop()->getTitle();
}
public function getCategoryTitle()
{
return $this->getCategory()->getTitle();
}
public function getPriceWithMoney()
{
$shop = $this->getShop();
if (!$shop)
return $this->getPrice();
$currency = $shop->getCurrency();
if (!$currency)
return $this->getPrice();
return $this->getPrice() . $currency->getCode();
}
public function getPriceFull()
{
$priceWithMoney = $this->getPriceWithMoney();
if (!$priceWithMoney)
return $this->getPriceBase();
$currencyDefault = CurrencyPeer::retrieveByPK(sfConfig::get('app_currency_default_id'));
if (!$currencyDefault)
return $this->getPriceBase();
return sprintf('%d%s (%s)', $this->getPriceBase(), $currencyDefault->getCode(), $priceWithMoney);
}
public function save (PropelPDO $con = null)
{
$shopId = $this->getShopId();
$shop = $this->getShop();
// Сохраняем базовую цену (в дефолтной валюте)
$shopCurrencyRate = $shop->getCurrencyRate();
if ($shopCurrencyRate)
$this->setPriceBase($this->getPrice() / $shopCurrencyRate);
ShopCategoryPeer::onAddProductCategoryLink($shopId, $this->getCategoryId());
// Удаляем все категории товара
if (!$this->isNew()) {
$c = new Criteria();
$c->add(ProductCategoryPeer::PRODUCT_ID, $this->getId());
ProductCategoryPeer::doDelete($c);
}
// Существующий товар + изменена категория -> Уменьшить кол-во товаров в дереве старых категорий
if (!$this->isNew() && in_array(ProductPeer::CATEGORY_ID, $this->modifiedColumns)) {
$category = CategoryPeer::retrieveByPK($this->_category_id_old);
if ($category)
do {
$c = new Criteria();
$c->add(ShopCategoryPeer::SHOP_ID, $this->getShopId());
$c->add(ShopCategoryPeer::CATEGORY_ID, $category->getId());
if ($shopCategory = ShopCategoryPeer::doSelectOne($c)) {
if (1 == $shopCategory->getProductsCount()) { // Удаляем категорию магазина без товаров
$shopCategory->delete();
} else {
$shopCategory->setProductsCount($shopCategory->getProductsCount() - 1);
$shopCategory->save();
}
}
} while ($category = $category->getCategoryRelatedByParentId());
}
// Создаем новые категории магазина + обновляем кол-во товаров в категориях
if ($category = $this->getCategory())
do {
$c = new Criteria();
$c->add(ShopCategoryPeer::SHOP_ID, $this->getShopId());
$c->add(ShopCategoryPeer::CATEGORY_ID, $category->getId());
$shopCategory = ShopCategoryPeer::doSelectOne($c);
if (!$shopCategory) {
$shopCategory = new ShopCategory();
$shopCategory->setShopId($this->getShopId());
$shopCategory->setCategoryId($category->getId());
$shopCategory->setProductsCount(1);
$shopCategory->save();
} else {
// Новый товар или изменена категория -> Увеличить кол-во товаров в новой категории
if ($this->isNew() || in_array(ProductPeer::CATEGORY_ID, $this->modifiedColumns)) {
$shopCategory->setProductsCount($shopCategory->getProductsCount() + 1);
$shopCategory->save();
}
}
} while ($category = $category->getCategoryRelatedByParentId());
parent::save($con);
// Создаем категории товара
if ($category = $this->getCategory())
do {
$productCategory = new ProductCategory();
$productCategory->setProductId($this->getId());
$productCategory->setCategoryId($category->getId());
$productCategory->save();
} while ($category = $category->getCategoryRelatedByParentId());
ShopCategoryPeer::onDeleteProductCategoryLink($this->getShopId(), $this->getCategoryIdOld());
ShopPeer::rebuildShopHasApprovedProduct($shopId);
}
public function delete (PropelPDO $con = null)
{
$shopId = $this->getShopId();
// Удаляем все категории товара
$c = new Criteria();
$c->add(ProductCategoryPeer::PRODUCT_ID, $this->getId());
ProductCategoryPeer::doDelete($c);
// Существующий товар + изменена категория -> Уменьшить кол-во товаров в дереве старых категорий
if (!$this->isNew()) {
if ($category = $this->getCategory())
do {
$c = new Criteria();
$c->add(ShopCategoryPeer::SHOP_ID, $this->getShopId());
$c->add(ShopCategoryPeer::CATEGORY_ID, $category->getId());
if ($shopCategory = ShopCategoryPeer::doSelectOne($c)) {
if (1 == $shopCategory->getProductsCount()) { // Удаляем категорию магазина без товаров
$shopCategory->delete();
} else {
$shopCategory->setProductsCount($shopCategory->getProductsCount() - 1);
$shopCategory->save();
}
}
} while ($category = $category->getCategoryRelatedByParentId());
}
parent::delete($con);
ShopCategoryPeer::onDeleteProductCategoryLink($this->getShopId(), $this->getCategoryId());
ShopPeer::rebuildShopHasApprovedProduct($shopId);
}
public function getParamName ($param = 1, $id = 0)
{
$values = array();
if (1 == $param) {
$values = explode(',', $this->getParam1Values());
} elseif (2 == $param) {
$values = explode(',', $this->getParam2Values());
} elseif (3 == $param) {
$values = explode(',', $this->getParam3Values());
}
if (array_key_exists($id, $values)) {
return $values[$id];
}
}
public function getPhoto ()
{
$c = new Criteria();
$c->add(ProductImagePeer::PRODUCT_ID, $this->getId());
return ProductImagePeer::doSelectOne($c);
}
public function getPhotoFilename ()
{
$photo = $this->getPhoto();
if (!$photo)
return '';
$file = $photo->getFile();
return $file ? $file->getFilename() : '';
}
/**
* @param string $tmpImage
*/
public function addImage($tmpImage)
{
// Создаем запись файла, изначально без информации о файле
$file = new File();
$file->setServerId(1);
$file->save();
// Получаем ID файла, на его основе получаем и имя файла
$fileName = $file->getId() . time() . '.jpg';
$file->setFilename($fileName);
$file->save();
// Уменьшаем картину
$thumbnail = new sfThumbnail(300, 300);
$thumbnail->loadFile($tmpImage);
$thumbnail->save(sfConfig::get('sf_upload_dir') . '/products/' . $fileName, 'image/jpeg');
$thumbnail = new sfThumbnail(100, 100);
$thumbnail->loadFile($tmpImage);
$thumbnail->save(sfConfig::get('sf_upload_dir') . '/products/100x100/' . $fileName, 'image/jpeg');
$thumbnail = new sfThumbnail(50, 50);
$thumbnail->loadFile($tmpImage);
$thumbnail->save(sfConfig::get('sf_upload_dir') . '/products/50x50/' . $fileName, 'image/jpeg');
// Создаем связку
$productImage = new ProductImage();
$productImage->setProductId($this->getId());
$productImage->setFileId($file->getId());
$productImage->save();
}
public function cleanImages()
{
$c = new Criteria();
$c->add(ProductImagePeer::PRODUCT_ID, $this->getId());
ProductImagePeer::doDelete($c);
}
public function getTitleFull()
{
return $this->getProducer() . ' ' . $this->getTitle();
}
}
//xfPropelBehavior::register('Product', array('MainIndex'));