_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 = array('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); } public function getCartItem($id) { $select = $this->_db->select(); $select->from($this->_getPair('CartItem'), ['numbersText', 'numbersFile', 'productId', 'quantity']) ->where('id = ?', $id); return $select->query()->fetch(); } 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; } public function getListSelect() { if (null == $this->_select) { $select = parent::getListSelect(); if ($categoryId = $this->getCategoryId()) { $select->where('Product.categoryId = ?', $categoryId); } $select->reset(Zend_Db_Select::ORDER) ->join($this->_getPair('SignColor', 'sc'), 'sc.id = Product.colorId', ['colorName' => 'title']) ->join($this->_getPair('SignSize', 'ss'), 'ss.id = Product.sizeId', null) ->joinLeft( $this->_getPair('SignShape', 'ssh'), 'ssh.id = Product.shapeId', ['shapeImage' => 'image', 'shapeTitle' => 'title'] ) ->where('Product.show = "y"') ->order('ss.sorter') ->order('sc.sorter'); } return $this->_select; } protected function _getFromDbSelect($primaryKey) { return parent::_getFromDbSelect($primaryKey) ->join($this->_getPair('SignColor', 'sc'), 'sc.id = Product.colorId', ['colorName' => 'title']); } protected function _getAlternateImageSubSelect() { $select = $this->_db->select(); $select->from($this->_getPair($this->_tableAlias . 'Image'), array('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 * * @param string $item * @return App_ECommerce_Product_Obj */ public function prepareCartItemData(&$item) { $this->setPrimaryKey($item['productId']); $sku = $this->clearData()->getData('sku'); if ($sku) { //set selected category for item App_ECommerce_Product_Category_View::saveCategoryId($item['productCategoryId']); $item['show'] = $this->getData('show'); $item['deleted'] = 'n'; } else { $item['deleted'] = 'y'; } return $this; } public function validateCartItem($item) { return !(bool) ($item['deleted'] == 'y'); } }