_filterFields = array($this->_tableAlias => 'title'); return $this; } public function setCategoryId($id) { $select = $this->_db->select(); $select->from($this->_getPair('GalleryCategory'), array('title')); $select->where('`id` = ?', $id, \Qs_Db::INT_TYPE); $this->categoryTitle = $this->_db->fetchOne($select); $this->categoryId = $id; return ($this->categoryTitle) ? true : false; } protected function _where4Sorter(\Zend_Db_Select $select = null) { $select->where('idCategory = ?', $this->categoryId, \Qs_Db::INT_TYPE); return $select; } public function insert(array $data = null) { $this->_initCategoryOptions(); if (null === $data) { $data = $this->_data; } else { $this->_data = $data; } if ('y' == $data['mainImage']) { $this->_updateMainImage(); } $result = parent::insert($data); $this->_updateCategoryStatus(); return $result; } public function update(array $data = null) { $this->_initCategoryOptions(); $result = parent::update($data); $this->_updateCategoryStatus(); $this->_updateMainImage(); return $result; } public function delete() { $this->_initCategoryOptions(); $result = parent::delete(); $this->_updateCategoryStatus(); return $result; } public function changeEnumOption($name) { $this->_initCategoryOptions(); $result = parent::changeEnumOption($name); $this->_updateCategoryStatus(); if ('mainImage' == $name) { $this->_updateMainImage('n'); } return $result; } protected function _updateMainImage($isOption = 'y') { if (($isOption == $this->getData('mainImage')) && $this->getData('image')){ $where = array( '`idCategory` = ' . $this->_db->quote($this->getData('idCategory'), \Zend_Db::INT_TYPE), '`id` != ' . $this->_db->quote($this->getData('id'), \Zend_Db::INT_TYPE) ); $this->_getTable()->update(array('mainImage' => 'n'), $where); } return $this; } protected function _getPhotosCountSelect() { $select = $this->_db->select(); $select->from($this->_getPair($this->_tableAlias), 'COUNT(*)'); $select->where('`' . $this->_tableAlias . '`.`idCategory` = `' . $this->_tableAlias . 'Category`.`id`'); return $select; } protected function _initCategoryOptions() { if ($this->categoryId) { $columns = array( 'show', 'photosCount' => new \Zend_Db_Expr('(' . $this->_getPhotosCountSelect() . ')') ); $select = $this->_db->select(); $select->from($this->_getPair($this->_tableAlias . 'Category'), $columns); $select->where('`' . $this->_tableAlias . 'Category`.`id` = ?', $this->categoryId, \Qs_Db::INT_TYPE); $this->_categoryOptions = $this->_db->fetchRow($select); } return $this; } protected function _updateCategoryStatus() { if ($this->_categoryOptions) { $prevOptions = $this->_categoryOptions; $this->_initCategoryOptions(); $options = $this->_categoryOptions; $show = null; if (0 == $options['photosCount'] && 'y' == $options['show']) { $show = 'n'; } else if (0 < $options['photosCount'] && 0 == $prevOptions['photosCount']) { $show = 'y'; } if ($show) { $this->_getTable($this->_tableAlias . 'Category')->updateByKey( array('show' => $show), $this->categoryId ); } } return $this; } }