_idPost = $id; return $this; } public function getIdPost() { return $this->_idPost; } public function getPostTitle() { if (!($id = $this->getIdPost())) { return false; } $select = $this->_db->select(); $select->from($this->_getPair('Post'), array()); Qs_Db::joinTranslation( $select, 'Post', $this->getLanguage(), 'PostContent', array('title') ); $select->where('`Post`.`id` = ?', $id, \Zend_Db::INT_TYPE); $select->limit(1); return $this->_db->fetchOne($select); } public function getListSelect() { if (null === $this->_select) { parent::getListSelect(); $this->_select->where('`' . $this->_tableAlias . '`.`idPost` = ?', $this->getIdPost(), \Zend_Db::INT_TYPE); $this->_joinFromDb($this->_select); } return $this->_select; } protected function _getFromDbSelect($primaryKey) { $select = parent::_getFromDbSelect($primaryKey); $this->_joinFromDb($select); return $select; } protected function _joinFromDb(\Zend_Db_Select $select) { $select->joinLeft($this->_getPair('PostTag'), "`{$this->_tableAlias}`.`idTag` = `PostTag`.`id`", 'value'); return $this; } public function insert(array $data = null) { if (null === $data) { $data = $this->_data; } else { $this->_data = $data; } unset($data['id']); $this->_saveTag($data['value']); $this->_cleanCache(); return $this->_primaryKey; } protected function _saveTag($value, $mode = 'insert') { $this->_db->beginTransaction(); try { if ($mode == 'update' && $this->_primaryKey) { $this->_getTable()->deleteByKey($this->_primaryKey); } if (($id = $this->getTagId($value))) { $this->_primaryKey = $this->_insertTagLink($id); } else { $id = $this->_insertTag($value); $this->_primaryKey = $this->_insertTagLink($id); } $this->_deleteUnusedTags(); $this->_db->commit(); } catch (\Exception $e) { $this->_db->rollBack(); false; } return $this->_primaryKey; } public function update(array $data = null) { if (null === $data) { $data = $this->_data; } else { $this->_data = $data; } unset($data['id']); $this->_saveTag($data['value'], 'update'); $this->_cleanCache(); return $this->_primaryKey; } protected function _deletePostTagLinks() { if (($idPost = $this->getIdPost())) { $this->_getTable()->delete($this->_db->quoteInto('`idPost` = ?', $idPost, \Zend_Db::INT_TYPE)); return true; } return false; } public function getTagId($value, $idPost = null, $skipCurrent = false) { $select = $this->_db->select(); $select->from($this->_getPair('PostTag'), 'id'); $select->where('`PostTag`.`value` = ?', $value); if ($idPost) { $idPost = (is_bool($idPost)) ? $this->getIdPost() : $idPost; $select->joinLeft($this->_getPair('Post2Tag'), '`PostTag`.`id` = `Post2Tag`.`idTag`', null); $select->where('`Post2Tag`.`idPost` = ?', $idPost, \Zend_Db::INT_TYPE); if ($skipCurrent && $this->_primaryKey) { $select->where('`Post2Tag`.`id` <> ?', $this->_primaryKey, \Zend_Db::INT_TYPE); } } $select->limit(1); return $this->_db->fetchOne($select); } protected function _insertTagLink($id) { if (!($idPost = $this->getIdPost())) { throw new \Exception('Can not get idPost'); } return $this->_getTable()->insert(array('idPost' => $idPost, 'idTag' => $id)); } protected function _insertTag($value) { return $this->_getTable('PostTag')->insert(array('value' => $value)); } protected function _deleteUnusedTags() { $sql = 'DELETE `PostTag` ' . 'FROM `' . $this->_getTableName('PostTag') . '` AS `PostTag` ' . 'LEFT JOIN `' . $this->_getTableName($this->_tableAlias) . '` AS `' . $this->_tableAlias . '` ' . ' ON `PostTag`.`id` = `' . $this->_tableAlias . '`.`idTag` ' . 'WHERE `PostTag`.`id` IS NULL'; return $this->_db->query($sql); } public function delete() { $this->_getTable()->deleteByKey($this->_primaryKey); $this->_deleteUnusedTags(); $this->_cleanCache(); return $this; } public function getAutocompleteList($query, $limit = 10) { $query = (string) $query; $limit = (int) $limit; $limit = (!$limit) ? 10 : $limit; $select = $this->_db->select(); $select->from($this->_getPair('PostTag'), array('value')); $select->where('`PostTag`.`value` LIKE ?', '%' . $query . '%'); $select->limit($limit); return $this->_db->fetchCol($select); } protected function _cleanCache() { \App\Blog\Admin\Obj::cleanCache(); return $this; } public function getObjectInfo() { $info = parent::getObjectInfo(); if ($info) { $info['itemTitle'] = $info['value']; } return $info; } }