where('`' . $this->_tableAlias . '`.`parentId` = ?', (int) $this->getPrimaryKey()); return $select; } public function getInStockCategories4Select() { return $this->getProductCategory4Select( ['id', 'title'], 'typeId = ' . self::PRODUCT_IN_STOCK_TYPE_ID ); } public function initFromForm(array $data) { if ($data['action'] == 'update' && !$data['albumOrientation']) { unset($data['albumOrientation']); } if (isset($data['typeId'])) { $typeId = (int) $data['typeId']; if (self::PRODUCT_IN_STOCK_TYPE_ID === $typeId) { $data['priceList'] = null; $data['availableShapes'] = null; } else if (self::PRODUCT_CUSTOM_TYPE_ID === $typeId) { $data['priceList'] = null; $data['availableShapes'] = null; $data['description'] = null; $data['productImages'] = null; } } return parent::initFromForm($data); } public function isAllowDelete() { $select = $this->_db->select(); $select->from($this->_getPair(), array('COUNT(Product.id) + COUNT(ParentCategory.id)')); $select->where('`' . $this->_tableAlias . '`.`id` = ?', $this->getPrimaryKey(), Qs_Db::INT_TYPE); $select->group($this->_tableAlias . '.id'); $select->joinLeft( array('ParentCategory' => $this->_getTableName($this->_tableAlias)), '`ParentCategory`.`parentId`=`' . $this->_tableAlias . '`.`id`', array() ); $select->joinLeft( $this->_getPair('Product'), '`Product`.`categoryId` = `ProductCategory`.`id`', array() ); return !(bool) $this->_db->fetchOne($select); } protected function _addDependenciesFromDb(array &$data) { $this->_addIndustryType($data); $this->_addColors($data); $this->_addShapes($data); $this->_addSizes($data); return $this; } protected function _addIndustryType(array &$data) { $select = $this->_getDependencySelect( $this->_getTableName(), new Zend_Db_Expr('`t`.`industryTypeId`'), $this->_getTableName($this->_tableAlias . '2IndustryType'), 'categoryId', ['categoryId' => $data['id']] ); $data['industryType'] = Qs_Array::fetchCol($select->query()->fetchAll(), 'industryTypeId'); return $this; } protected function _addColors(array &$data) { $select = $this->_getDependencySelect( $this->_getTableName(), new Zend_Db_Expr('`t`.`signColorId`'), $this->_getTableName($this->_tableAlias . '2SignColor'), 'categoryId', ['categoryId' => $data['id']] ); $data['color'] = Qs_Array::fetchCol($select->query()->fetchAll(), 'signColorId'); return $this; } protected function _addShapes(array &$data) { $select = $this->_getDependencySelect( $this->_getTableName(), new Zend_Db_Expr('`t`.`signShapeId`'), $this->_getTableName($this->_tableAlias . '2SignShape'), 'categoryId', ['categoryId' => $data['id']] ); $data['shape'] = Qs_Array::fetchCol($select->query()->fetchAll(), 'signShapeId'); return $this; } protected function _addSizes(array &$data) { $select = $this->_db->select(); $select->from($this->_getTableName($this->_signSizeTableAlias), ['id', 'title']) ->where('`productCategoryId` = ' . (int) $data['id']) ->order('sorter'); $data['size'] = $this->_db->fetchPairs($select); return $this; } protected function _updateDependency() { $this->_saveDependency(); return parent::_updateDependency(); } protected function _insertDependency() { $this->_saveDependency(); return parent::_insertDependency(); } protected function _saveDependency() { //Industry type if (array_key_exists('industryType', $this->_data)) { $this->_updateIds( $this->_tableAlias . '2IndustryType', 'categoryId', 'industryTypeId', $this->_data['industryType'] ); } //Linked colors if (array_key_exists('color', $this->_data) && array_key_exists('typeId', $this->_data)) { if (self::PRODUCT_CUSTOM_TYPE_ID == $this->_data['typeId']) { $this->_data['color'] = array(); } $this->_updateIds( $this->_tableAlias . '2SignColor', 'categoryId', 'signColorId', $this->_data['color'] ); } //Linked shapes if (array_key_exists('shape', $this->_data) && array_key_exists('typeId', $this->_data)) { if (self::PRODUCT_IN_STOCK_TYPE_ID != $this->_data['typeId']) { $this->_data['shape'] = array(); } $this->_updateIds( $this->_tableAlias . '2SignShape', 'categoryId', 'signShapeId', $this->_data['shape'] ); } //Linked sizes if (array_key_exists('size', $this->_data)) { $this->_updateSizes($this->_data['size']); } return $this; } protected function _updateSizes($sizes) { $categoryId = $this->getPrimaryKey(); $newSizes = []; $updateSizes = []; $sorter = 0; foreach ($sizes as $id => $title) { if ($id > 0) { $updateSizes[$id] = ['title' => $title, 'sorter' => $sorter++]; } else { $newSizes[] = ['title' => $title, 'sorter' => $sorter++]; } } $table = $this->_getTable('SignSize'); //Try delete exists sizes $existsIds = array_keys($updateSizes); $where = 'productCategoryId = ' . (int) $categoryId; if (!empty($existsIds)) { $where .= $this->_db->quoteInto(' AND id NOT IN (?)', $existsIds); } try { $table->delete($where); } catch (Zend_Db_Exception $e) { if ($e->getCode() === self::INTEGRITY_CONSTRAINTS_VIOLATION_EXCEPTION_CODE) { throw new Exception('This size is assigned to product(s) and can\'t be removed. ' . 'Please remove the connections first.'); } throw $e; } //Update exists sizes foreach ($updateSizes as $id => $sizeInfo) { $table->update([ 'title' => $sizeInfo['title'], 'sorter' => $sizeInfo['sorter'], ], $this->_db->quoteInto('id = ?', $id)); } //Insert new sizes foreach ($newSizes as $sizeInfo) { $table->insert([ 'title' => $sizeInfo['title'], 'productCategoryId' => $categoryId, 'sorter' => $sizeInfo['sorter'], ]); } return $this; } public function getCanChangeAlbumOrientation() { $select = $this->_db->select(); $select->from($this->_getPair('GalleryImage', 'gi'), ['COUNT(*)']) ->where('gi.productCategoryId = ?', $this->getPrimaryKey()) ->limit(1); $result = $select->query()->fetch(); return !reset($result); } public function getDeleteSizeConstraints() { $subSelect = $this->_db->select(); $subSelect->from($this->_getPair('Product', 'p'), [new Zend_Db_Expr('1')]) ->where('p.categoryId = ?', $this->getPrimaryKey()) ->where('p.sizeId = ss.id') ->limit(1); $select = $this->_db->select(); $select->from($this->_getPair('SignSize', 'ss'), ['id']) ->where(new Zend_Db_Expr('EXISTS(' . $subSelect . ')')); return $this->_db->fetchCol($select); } protected function _deleteDependency() { $this->_deleteImages(); //Industry type $this->_deleteIds($this->_tableAlias . '2IndustryType', 'categoryId'); //Colors $this->_deleteIds($this->_tableAlias . '2SignColor', 'categoryId'); //Shapes $this->_deleteIds($this->_tableAlias . '2SignShape', 'categoryId'); //Sizes $this->_deleteIds($this->_signSizeTableAlias, 'productCategoryId'); return parent::_deleteDependency(); } protected function _getImagesIds() { $select = $this->_db->select(); $select->from($this->_getPair($this->_imagesGalleryTableAlias), array('id')); $select->where('`productCategoryId` = ?', $this->getPrimaryKey(), Qs_Db::INT_TYPE); return $this->_db->fetchCol($select); } protected function _deleteImages() { $imagesIds = $this->_getImagesIds(); $imageObj = new App\Gallery\Image\Admin\Obj(); $imageObj->setProductCategoryId($this->getPrimaryKey()); foreach ($imagesIds as $imageId) { $imageObj->setPrimaryKey($imageId); $imageObj->delete(false); } return $this; } }