<?php
/**
* СодеÑÐ¶Ð¸Ñ ÐºÐ»Ð°ÑÑ ProductEditor
*
* @package energine
* @subpackage shop
* @author dr.Pavka
* @copyright ColoCall 2006
* @version $Id: ProductEditor.class.php,v 1.20 2008/02/12 11:45:02 pavka Exp $
*/
//require_once('core/modules/share/components/Grid.class.php');
//require_once('core/modules/image/components/Image.class.php');
//require_once('core/modules/shop/components/ProductStatusEditor.class.php');
/**
* РедакÑÐ¾Ñ Ð¿ÑодÑкÑов
*
* @package energine
* @subpackage shop
*/
class ProductEditor extends Grid {
/**
* ÐеÑево Ñазделов
*
* @var DivisionEditor
* @access private
*/
private $divEditor;
/**
* ÐÐ¼Ñ ÑаблиÑÑ ÑодеÑжаÑей дополниÑелÑнÑе даннÑе(в ÑÑÑ ÑаблиÑÑ Ð±ÑдÑÑ Ð·Ð°Ð³ÑÑжаÑÑÑÑ Ð´Ð°Ð½Ð½Ñе из ÑиÑÑÐµÐ¼Ñ ÑÑеÑа)
*
* @var string
* @access private
*/
private $externalTableName;
/**
* РедакÑÐ¾Ñ Ð¿Ð°ÑамеÑÑов
*
* @var ParamValuesEditor
* @access private
*/
private $paramValueEditor = false;
/**
* ÐонÑÑÑÑкÑÐ¾Ñ ÐºÐ»Ð°ÑÑа
*
* @param string $name
* @param string $module
* @param Document $document
* @param array $params
* @access public
*/
public function __construct($name, $module, Document $document, array $params = null) {
parent::__construct($name, $module, $document, $params);
$this->setTableName('shop_products');
$this->externalTableName = 'shop_product_external_properties';
}
/**
* ÐозвÑаÑÐ°ÐµÑ Ð¸Ð¼Ñ ÑаблиÑÑ Ñ Ð´Ð¾Ð¿Ð¾Ð»Ð½Ð¸ÐµÐ»ÑнÑми даннÑми
*
* @return string
* @access protected
* @final
*/
final protected function getExternalTableName() {
return $this->externalTableName;
}
/**
* ÐÑÐ²Ð¾Ð´Ð¸Ñ Ð´ÐµÑево Ñазделов
*
* @return void
* @access protected
*/
protected function showTree() {
$this->request->setPathOffset($this->request->getPathOffset() + 1);
$_POST['selectorID'] = Sitemap::getInstance()->getIDByURI(array('shop'));
$this->divEditor = $this->document->componentManager->createComponent('divEditor', 'share', 'DivisionEditor', array('action' => 'selector'));
$this->divEditor->run();
}
/**
* ÐÐ»Ñ Ð¼ÐµÑода showTree вÑзÑваеÑÑÑ Ñвой билдеÑ
*
* @return DOMNode
* @access public
*/
public function build() {
if ($this->getAction() == 'showTree') {
$result = $this->divEditor->build();
}
elseif ($this->getAction() == 'showParams') {
$result = $this->paramValueEditor->build();
}
else {
$result = parent::build();
}
return $result;
}
/**
* ÐÐ»Ñ Ð¿Ð¾Ð»Ñ smap_id ÑоÑмиÑÑеÑÑÑ ÐеÑево Ñазделов
* ÐобавлÑÑÑÑÑ Ð¿Ð¾Ð»Ñ product_price, curr_id
*
* @return DataDescription
* @access protected
*/
protected function createDataDescription() {
$result = parent::createDataDescription();
if (in_array($this->getAction(), array('add', 'edit'))) {
$smapPIDFieldDescription = $result->getFieldDescriptionByName('smap_id');
$smapPIDFieldDescription->setType(FieldDescription::FIELD_TYPE_STRING);
$smapPIDFieldDescription->setMode(FieldDescription::FIELD_MODE_READ);
if ($productPriceFieldDescription = $result->getFieldDescriptionByName('product_price')) {
$productPriceFieldDescription->addProperty('nullable', false);
$productPriceFieldDescription->setType(FieldDescription::FIELD_TYPE_FLOAT);
$productPriceFieldDescription->addProperty('title', $this->translate('FIELD_PRODUCT_PRICE'));
$productPriceFieldDescription->addProperty('tableName', $this->getExternalTableName());
}
if ($productCurrFieldDescription = $result->getFieldDescriptionByName('curr_id')) {
$productCurrFieldDescription->setType(FieldDescription::FIELD_TYPE_SELECT );
$productCurrFieldDescription->addProperty('title', $this->translate('FIELD_CURR_ID'));
$productCurrFieldDescription->addProperty('tableName', $this->getExternalTableName());
$currencyOptions = $this->dbh->selectRequest(
'SELECT c.curr_id, curr_name FROM shop_currency c '.
'LEFT JOIN shop_currency_translation ct ON ct.curr_id = c.curr_id '.
'WHERE ct.lang_id = %s', Language::getInstance()->getCurrent()
);
$productCurrFieldDescription->loadAvailableValues($currencyOptions, 'curr_id', 'curr_name');
}
if($productCountFieldDescription = $result->getFieldDescriptionByName('product_count')){
$productCountFieldDescription->setType(FieldDescription::FIELD_TYPE_INT);
$productCountFieldDescription->addProperty('title', $this->translate('FIELD_PRODUCT_COUNT'));
$productCountFieldDescription->addProperty('tableName', $this->getExternalTableName());
}
}
elseif ($this->getAction() == 'getRawData' && ($productCountFieldDescription = $result->getFieldDescriptionByName('product_count'))) {
$productCountFieldDescription->setType(FieldDescription::FIELD_TYPE_INT);
$productCountFieldDescription->addProperty('title', $this->translate('FIELD_PRODUCT_COUNT'));
}
return $result;
}
/**
* ÐÐ»Ñ Ð¼ÐµÑода edit добавлÑем даннÑе о полÑÑ
из внеÑней ÑаблиÑÑ
*
* @return array
* @access protected
*/
protected function loadData() {
$result = parent::loadData();
if ($this->getType() == self::COMPONENT_TYPE_FORM_ALTER) {
$productCode = $result[0]['product_code'];
$res = $this->dbh->select($this->getExternalTableName(), array('product_price', 'product_count', 'curr_id'), array('product_code' => $productCode));
if (is_array($res)) {
list($res) = $res;
}
else {
$res = array(
'product_price' => 0,
'product_count' => 0,
'curr_id' => 1
);
}
foreach ($result as $langID => $data) {
$result[$langID]['product_price'] = $res['product_price'];
$result[$langID]['product_count'] = $res['product_count'];
$result[$langID]['curr_id'] = $res['curr_id'];
}
}
elseif ($this->getType() == self::COMPONENT_TYPE_FORM_ADD) {
foreach ($result as $langID => $data) {
if (isset($result[$langID]['ps_id'])) {
$result[$langID]['ps_id'] = ProductStatusEditor::getDefaultStatus();
}
}
}
elseif ($this->getAction() == 'getRawData' && is_array($result) && $this->getDataDescription()->getFieldDescriptionByName('product_count')) {
$ids = array_map(create_function('$row', 'return $row[\'product_id\'];'), $result);
$request = 'SELECT product_id, product_count FROM `shop_product_external_properties` prop LEFT JOIN shop_products p ON prop.`product_code` = p.`product_code` WHERE p.product_id IN ('.implode(',', $ids).')';
$res = convertDBResult($this->dbh->selectRequest($request), 'product_id', true);
foreach ($result as $key => $row) {
$result[$key]['product_count'] = $res[$row['product_id']]['product_count'];
}
}
return $result;
}
/**
* ÐÐ»Ñ Ð¼ÐµÑода ÑедакÑиÑÐ¾Ð²Ð°Ð½Ð¸Ñ Ð¸Ð·Ð¼ÐµÐ½Ñем даннÑе Ð´Ð»Ñ Ð¿Ð¾Ð»Ñ smap_id
*
* @return Builder
* @access protected
*/
protected function prepare() {
parent::prepare();
if ($this->getAction() == 'edit') {
$field = $this->getData()->getFieldByName('smap_id');
$res = $this->dbh->select('share_sitemap_translation', array('smap_name'), array('smap_id' => $field->getRowData(0), 'lang_id' => $this->document->getLang()));
if (!empty($res)) {
for ($i = 0; $i < count(Language::getInstance()->getLanguages()); $i++) {
$field->setRowProperty($i, 'data_name', simplifyDBResult($res, 'smap_name', true));
}
}
}
}
/**
* ÐÑи ÑоÑ
Ñанении генеÑим thumbnail
*
* @return mixed
* @access protected
*/
protected function saveData() {
$result = parent::saveData();
//ÐÑли пÑÑÑой ÑилÑÑÑ - знаÑÐ¸Ñ Ñ Ð½Ð°Ñ Ð¼ÐµÑод вÑÑавки
$filter = $this->saver->getFilter();
if (empty($filter)) {
$filter = array($this->getPK()=>$this->saver->getResult());
}
$res = $this->dbh->select($this->getTableName(), array('product_photo_img', 'product_code'), $filter);
if (!is_array($res) || empty($res)) {
throw new SystemException('ERR_BAD_DATA', SystemException::ERR_CRITICAL);
}
//Создаем thumbnail в Ñом ÑлÑÑае еÑли не Ñказан вÑвод маленÑкой ÑоÑки в ÑоÑме
//ÐолÑÑили Ð¸Ð¼Ñ Ñайла Ð´Ð»Ñ Ð¸ÑÑ
одного изобÑажениÑ
$sourceFileName = simplifyDBResult($res, 'product_photo_img', true);
if (
!$this->saver->getDataDescription()->getFieldDescriptionByName('product_thumb_img')
&& !empty($sourceFileName)
) {
$this->generateThumbnail($sourceFileName, 'product_thumb_img', 100, 100, $filter, false);
}
if (
$this->getConfigValue('watermark')
&& !empty($sourceFileName)
/*&& !file_exists(dirname($_SERVER['SCRIPT_FILENAME']).'/'.$sourceFileName)*/
) {
$path = dirname($_SERVER['SCRIPT_FILENAME']).'/core/ext/watermark/converter.rb -t '.$this->getConfigValue('project.name').' '.dirname($_SERVER['SCRIPT_FILENAME']).'/'.$sourceFileName;
shell_exec($path);
}
//СоÑ
ÑанÑем даннÑе в ÑаблиÑе дополниÑелÑнÑÑ
ÑвойÑÑв
$productCode = simplifyDBResult($res, 'product_code', true);
//УдалÑем вÑе запиÑи Ñ Ñаким кодом пÑодÑкÑа
$this->dbh->modify(QAL::DELETE, $this->getExternalTableName(), null, array('product_code' => $productCode));
$price = (isset($_POST[$this->getExternalTableName()]['product_price']))?$_POST[$this->getExternalTableName()]['product_price']:0;
$count = (isset($_POST[$this->getExternalTableName()]['product_count']))?$_POST[$this->getExternalTableName()]['product_count']:0;
$currency = (isset($_POST[$this->getExternalTableName()]['curr_id']))?$_POST[$this->getExternalTableName()]['curr_id']:0;
//ÐÑÑавлÑем запиÑи
$res = $this->dbh->modify(QAL::INSERT , $this->getExternalTableName(), array('product_code' => $productCode, 'product_price' => $price, 'product_count' => $count, 'curr_id'=> $currency));
//ÐÐ»Ñ Ñежима вÑÑавки в ÑаблиÑÑ Ð·Ð½Ð°Ñений паÑамеÑÑов Ð´Ð»Ñ Ð²ÑеÑ
ÑзÑков добавлÑем пÑÑÑÑе знаÑениÑ
if ($this->saver->getMode() == QAL::INSERT) {
$productID = $this->saver->getResult();
$res = $this->dbh->select('shop_products', 'pt_id' , array('product_id' => $productID));
$ptID = simplifyDBResult($res, 'pt_id', true);
$res = $this->dbh->select('shop_product_params', 'pp_id', array('pt_id'=>$ptID));
if (is_array($res)) {
$langAbbr = array_keys(Language::getInstance()->getLanguages());
foreach ($res as $paramInfo) {
$ppID = $paramInfo['pp_id'];
$ppvID = $this->dbh->modify(QAL::INSERT, 'shop_product_param_values', array('product_id' => $productID, 'pp_id' => $ppID));
foreach ($langAbbr as $langID) {
$this->dbh->modify(QAL::INSERT, 'shop_product_param_values_translation', array('ppv_id'=>$ppvID, 'lang_id'=>$langID, 'ppv_value'=>QAL::EMPTY_STRING));
}
}
}
}
return $result;
}
/**
* ÐÑÐ²Ð¾Ð´Ð¸Ñ ÑедакÑÐ¾Ñ Ð·Ð½Ð°Ñений паÑмеÑÑов
*
* @return void
* @access protected
*/
protected function showParams() {
$productID = $this->getActionParams();
$productID = $productID[0];
$this->request->setPathOffset($this->request->getPathOffset() + 2);
$this->paramValueEditor = $this->document->componentManager->createComponent('paramValuesEditor', 'shop', 'ParamValuesEditor', array('productID'=>$productID));
//$this->paramValueEditor->getAction();
$this->paramValueEditor->run();
}
}