Files
2025-03-12 17:06:23 +01:00

444 lines
12 KiB
PHP

<?php
/**
* SOTESHOP/stProductOptionsPlugin
* Ten plik należy do aplikacji stProductOptionsPlugin opartej na licencji (Open License SOTE) Otwarta Licencja SOTE.
* Nie zmieniaj tego pliku, jeśli chcesz korzystać z automatycznych aktualizacji oprogramowania.
* Jeśli chcesz wprowadzać swoje modyfikacje do programu, zapoznaj się z dokumentacją, jak zmieniać
* oprogramowanie bez zmiany kodu bazowego http://www.sote.pl/modifications
*
* @author Daniel Mendalka <daniel.mendalka@sote.pl>
*
* @package stProductOptionsPlugin
* @subpackage libs
*/
class ProductOptionsValue extends BaseProductOptionsValue
{
protected static
$productPool = array();
protected $childOptions = null;
public $_duplicated = null;
protected $productUpdate = true;
protected $oldColorImagePath = null;
public function disableProductUpdate()
{
$this->productUpdate = false;
}
public function toArray($keyType = BasePeer::TYPE_PHPNAME)
{
$results = parent::toArray($keyType);
if ($keyType == BasePeer::TYPE_FIELDNAME)
{
$results['value'] = $this->getValue();
}
return $results;
}
public function enableProductUpdate()
{
$this->productUpdate = true;
}
public function copyInto($copyObj, $deepCopy = false)
{
parent::copyInto($copyObj, $deepCopy);
$copyObj->_duplicated = true;
}
/**
* Przeciazenie setPrice, zeby nie zapisywalo pustego stringa
*
* @return void
**/
public function setPrice($v)
{
if($v === '')
{
$v = null;
}
parent::setPrice($v);
}
public function setProductOptionsDefaultValueId($v)
{
$this->setProductOptionsValueId($v);
}
public function getChildOptions($hide_with_empty_stock = false)
{
if (null === $this->childOptions)
{
$c = new Criteria();
$c->addAscendingOrderByColumn(ProductOptionsFieldPeer::FIELD_ORDER);
$c->addAscendingOrderByColumn(ProductOptionsValuePeer::PRODUCT_OPTIONS_FIELD_ID);
$c->addAscendingOrderByColumn(ProductOptionsValuePeer::LFT);
$c->add(ProductOptionsValuePeer::PRODUCT_OPTIONS_VALUE_ID, $this->getId());
$c->add(ProductOptionsValuePeer::IS_ACTIVE, true);
if ($this->getProduct()->hasStockManagmentWithOptions() && $hide_with_empty_stock)
{
$c->add(ProductOptionsValuePeer::STOCK, 0, Criteria::GREATER_THAN);
}
$this->childOptions = ProductOptionsValuePeer::doSelectJoinProductOptionsField($c);
}
return $this->childOptions;
}
public function insertAsLastChildOf(ProductOptionsValue $value)
{
$this->setPriceType($value->getPriceType());
if (!$value->getIsActive())
{
$this->setIsActive(false);
}
return parent::insertAsLastChildOf($value);
}
public function getPriceType()
{
if (null === $this->price_type)
{
if ($this->isNew() || $this->isRoot())
{
$config = stConfig::getInstance('stProduct');
$this->setPriceType($config->get('global_price_netto') ? 'netto' : 'brutto');
}
else
{
$this->price_type = ProductOptionsValuePeer::doSelectRoot($this->getProduct())->getPriceType();
}
}
return $this->price_type;
}
/**
* Przeciążenie hydrate
*
* @param ResultSet $rs
* @param int $startcol
* @return object
*/
public function hydrate(ResultSet $rs, $startcol = 1)
{
$this->setCulture(stLanguage::getHydrateCulture());
return parent::hydrate($rs, $startcol);
}
/**
* Przeciążenie getValue
*
* @return string
*/
public function getValue()
{
if ($this->getCulture() == stLanguage::getOptLanguage())
{
return stLanguage::getDefaultValue($this, __METHOD__);
}
$v = parent::getValue();
if (is_null($v)) $v = stLanguage::getDefaultValue($this, __METHOD__);
return $v;
}
/**
* Przeciążenie setValue
*
* @param string $v
*/
public function setValue($v)
{
if ($this->getCulture() == stLanguage::getOptLanguage())
{
stLanguage::setDefaultValue($this, __METHOD__, $v);
}
parent::setValue($v);
}
/**
* Undocumented function
*
* @param Connection $con
* @return Product
*/
public function getProduct($con = null)
{
$id = $this->getProductId();
if ($id && !isset(self::$productPool[$id]))
{
self::$productPool[$id] = parent::getProduct($con);
}
return self::$productPool[$id];
}
/**
* Przeciążenie funkcji save
*
* @param $con
*/
public function save($con = null)
{
$removeOldColorImage = $this->isColumnModified(ProductOptionsValuePeer::COLOR);
if ($this->getOptVersion()!=ProductOptionsValuePeer::version)
{
$this->setOptVersion(ProductOptionsValuePeer::version);
}
if (!$this->isNew() && ($this->isColumnModified(ProductOptionsValuePeer::STOCK) || $this->isColumnModified(ProductOptionsValuePeer::PRICE)))
{
AllegroAuctionPeer::updateRequiresSync($this->getProductId(), $this->getId());
}
if (!$this->isNew() && $this->isColumnModified(ProductOptionsValuePeer::IS_ACTIVE))
{
ProductOptionsValuePeer::updateIsActive($this);
}
if (!isset($this->_duplicated) && $this->productUpdate)
{
if($this->isNew())
{
$con = Propel::getConnection();
$sql = sprintf('UPDATE %1$s SET %2$s = %2$s + 1 WHERE %3$s = %4$s',
ProductPeer::TABLE_NAME,
ProductPeer::OPT_HAS_OPTIONS,
ProductPeer::ID,
$this->getProductId()
);
$con->executeQuery($sql);
if (null === $this->opt_filter_id && $this->getProductOptionsField())
{
$filter_id = $this->getProductOptionsField()->getProductOptionsFilterId();
$this->setOptFilterId($filter_id);
}
}
if ($this->isRoot() && $this->isColumnModified(ProductOptionsValuePeer::PRICE_TYPE))
{
ProductOptionsValuePeer::doUpdatePriceType($this);
}
$modified = $this->modifiedColumns;
$ret = parent::save($con);
$this->modifiedColumns = $modified;
if ($this->product_id && !$this->isRoot())
{
if ($this->isColumnModified(ProductOptionsValuePeer::STOCK) || $this->isColumnModified(ProductOptionsValuePeer::IS_ACTIVE))
{
ProductOptionsValuePeer::updateStock($this->getProduct());
}
if ($this->isColumnModified(ProductOptionsValuePeer::IS_ACTIVE) || $this->isColumnModified(ProductOptionsValuePeer::STOCK) || $this->isColumnModified(ProductOptionsValuePeer::COLOR) || $this->isColumnModified(ProductOptionsValuePeer::SF_ASSET_ID) || $this->isColumnModified(ProductOptionsValuePeer::IS_ACTIVE))
{
ProductOptionsValuePeer::updateProductColor($this->product_id);
}
}
$this->resetModified();
}
else
{
$ret = parent::save($con);
}
if ($removeOldColorImage && null !== $this->oldColorImagePath && is_file($this->oldColorImagePath))
{
unlink($this->oldColorImagePath);
}
return $ret;
}
/**
* Przeciążenie funkcji delete
*
* @param $con
**/
public function delete($con = null)
{
$con = Propel::getConnection();
$sql = sprintf('UPDATE %1$s SET %2$s = %2$s - 1 WHERE %3$s = %4$s',
ProductPeer::TABLE_NAME,
ProductPeer::OPT_HAS_OPTIONS,
ProductPeer::ID,
$this->getProductId()
);
$con->executeQuery($sql);
$sql = sprintf('DELETE %1$s, %2$s FROM %1$s LEFT JOIN %2$s ON %8$s = %7$s, %3$s WHERE %10$s = %11$s AND %4$s > %5$s AND %4$s < %6$s AND %7$s = %9$s',
ProductOptionsFieldPeer::TABLE_NAME,
ProductOptionsFieldI18nPeer::TABLE_NAME,
ProductOptionsValuePeer::TABLE_NAME,
ProductOptionsValuePeer::LFT,
$this->getLft(),
$this->getRgt(),
ProductOptionsFieldPeer::ID,
ProductOptionsFieldI18nPeer::ID,
ProductOptionsValuePeer::PRODUCT_OPTIONS_FIELD_ID,
ProductOptionsValuePeer::PRODUCT_ID,
$this->getProductId()
);
$con->executeQuery($sql);
parent::delete($con);
$this->deleteColorImage();
if ($this->product_id)
{
ProductOptionsValuePeer::updateStock($this->product_id);
if ($this->color)
{
ProductOptionsValuePeer::updateProductColor($this->product_id);
}
}
}
/**
* Zwraca opcje nadrzędne ignorując korzeń
*
* @param bool $includeSelf Uwzględnij aktualną opcję w ścieżce
* @return ProductOptionsValue[]
*/
public function getPathWithoutRoot($includeSelf = false)
{
$results = $this->getPath('doSelectWithoutRoot');
if ($includeSelf)
{
$results[] = $this;
}
return $results;
}
public function getParentId()
{
return $this->getProductOptionsValueId();
}
public function isRoot()
{
return $this->product_options_value_id === null;
}
public function isLeaf()
{
return $this->rgt - $this->lft == 1;
}
public function getLevel()
{
return $this->depth;
}
public function hasChildren()
{
return $this->rgt - $this->lft > 1;
}
public function clearI18ns()
{
if ($this->collProductOptionsValueI18ns)
{
unset($this->collProductOptionsValueI18ns);
$this->collProductOptionsValueI18ns = null;
}
}
public static function setProductPool(Product $product)
{
self::$productPool[$product->getId()] = $product;
}
public function getColorImagePath($system = false)
{
return ProductOptionsValuePeer::getColorImagePath($this->getProductId(), $this->getId(), $this->getColor(), $system);
}
public function deleteColorImage()
{
if ($this->getUseImageAsColor() && is_file($this->getColorImagePath(true)))
{
unlink($this->getColorImagePath(true));
}
}
public function getColorImageDir($system = false)
{
return ProductOptionsValuePeer::getColorImageDir($this->getProductId(), $system);
}
public function setColorImage($v)
{
if (!$this->isNew() && null !== $this->getColor() && $this->getUseImageAsColor())
{
$this->oldColorImagePath = $this->getColorImagePath(true);
}
$this->setColor($v);
}
public function getColorImage()
{
return $this->getColor();
}
/**
* Undocumented function
*
* @return void
*/
public function getCurrency()
{
return $this->getProduct()->hasLocalCurrency() ? $this->getProduct()->getCurrency() : stCurrency::getInstance(sfContext::getInstance())->get();
}
public function getProductUom()
{
sfLoader::loadHelpers('stProduct', 'stProduct');
return st_product_uom($this->getProduct());
}
public static function clearStaticPool()
{
self::$productPool = array();
}
}
$columns = array('left' => ProductOptionsValuePeer::LFT,
'right' => ProductOptionsValuePeer::RGT,
'parent' => ProductOptionsValuePeer::PRODUCT_OPTIONS_VALUE_ID,
'scope' => ProductOptionsValuePeer::PRODUCT_ID,
'depth' => ProductOptionsValuePeer::DEPTH);
sfPropelBehavior::add('ProductOptionsValue', array('actasnestedset' => array('columns' => $columns)));