_data && $this->getPrimaryKey() && $data = $this->_getFromDb($this->getPrimaryKey())) { $this->_data = $data; if (!$this->_data['image']) { if (!empty($this->_data['additionalImages'])) { $image = reset($this->_data['additionalImages']); $this->_data['image'] = $image['image']; } } else { $image = ['id' => 0, 'image' => $this->_data['image'], 'title' => $this->_data['title']]; array_unshift($this->_data['additionalImages'], $image); } } if (null === $this->_data) { return null; } return Qs_Array::get($this->_data, $field, $default); } protected function _addDependenciesFromDb(array &$data) { parent::_addDependenciesFromDb($data); $data['additionalImages'] = $this->_getProductImages($data['id']); return $this; } /** * Return additional images for current product * * @param int $productId * @return array */ protected function _getProductImages($productId) { $productImagesObj = new App_ECommerce_Product_Image_Admin_Obj(); $productImagesObj->setProductId($this->getPrimaryKey()); $productImagesObj->getListSelect()->where('`productId` = ?', $productId, Qs_Db::INT_TYPE); $productImagesObj->getListSelect()->where('`show` = ?', 'y'); $productImagesObj->getListSelect()->order('sorter'); return $productImagesObj->getList(); } /** * Set current category * * @param string $alias * @return boolean */ public function setCategoryByAlias($alias) { $categoryId = App_ECommerce_Product_Category_Obj::getCategoryIdByAlias($alias); if (false !== $categoryId) { $this->_categoryId = $categoryId; return true; } return false; } public function getCategoryId() { return $this->_categoryId; } protected function _where(Zend_Db_Select $select) { $select->where('`' . $this->_tableAlias . '`.`enabled` = ?', 'y'); $this->_filterByCategory($select, $this->_categoryId); return parent::_filter($select); } protected function _getAlternateImageSubSelect() { $select = $this->_db->select(); $select->from($this->_getPair($this->_tableAlias . 'Image'), ['image']); $select->where('`' . $this->_tableAlias . 'Image`.`productId` = `' . $this->_tableAlias . '`.`id`'); $select->where('`' . $this->_tableAlias . 'Image`.`show` = "y"'); $select->order($this->_tableAlias . 'Image.sorter'); $select->limit(1); return $select; } protected function _getFromColumns() { $columns = parent::_getFromColumns(); $imageColumn = $this->_db->quoteIdentifier($this->_tableAlias . '.image'); $imageSubSelect = $this->_getAlternateImageSubSelect(); $image = new Zend_Db_Expr('IF (' . $imageColumn . ' IS NULL, (' . $imageSubSelect . '), ' . $imageColumn . ')'); $columns['image'] = $image; return $columns; } /** * Prepare data for item added in shopping cart * @return App_ECommerce_Product_Obj */ public function prepareCartItemData() { $this->setPrimaryKey($this->_cartItemData['productId']); $alias = $this->clearData()->getData('alias'); if ($alias) { //set selected category for item App_ECommerce_Product_Category_View::saveCategoryId($this->_cartItemData['productCategoryId'], $alias); //add url for product $this->_cartItemData['url'] = App_ECommerce_Product_View::getViewUrl() . '/' . $alias; $this->_cartItemData['soldOut'] = $this->getData('soldOut'); $this->_cartItemData['show'] = $this->getData('enabled'); $this->_cartItemData['deleted'] = 'n'; $this->_cartItemData['typeTitle'] = Entity::CART_ITEM_TYPE_TITLE; $this->_cartItemData['showDescription'] = 'n'; $this->_cartItemData['allowChangeQuantity'] = in_array(App_ECommerce_Cart_ItemObjFactory::CAPABILITY_CHANGE_QTY, $this->getCartItemCapabilities()) ? 'y' : 'n'; } else { $this->_cartItemData['deleted'] = 'y'; } return $this->_cartItemData; } public function beforeValidateCart() { } public function validateCartItem() { $valid = !(bool) ($this->_cartItemData['soldOut'] == 'y' || $this->_cartItemData['deleted'] == 'y'); if (!$valid) { $this->_addError('Product is not available now'); } return $valid; } /** * @return array [name => bool value, ...] */ public static function getCartItemCapabilities() { return [App_ECommerce_Cart_ItemObjFactory::CAPABILITY_CHANGE_QTY]; } /** * @param array $data ['id' =>, 'cartId' =>, 'productId' =>, ...] * * @return mixed */ public function setCartItemData(array $data) { $this->setPrimaryKey($data['productId']); $this->_cartItemData = $data; return $this; } public function completeItemList(array $itemList) { return $this; } public function afterCartItemRemove() { return $this; } }