'name'); protected $_processFilterFields = array('Process' => 'name'); protected $_markItem = array('name', 'subDepartmentName', 'departmentName'); protected $_hasFilter = true; protected $_showEmptyDepartments = false; public $itemName = 'Department/Process'; public function setShowEmptyDepartments($showEmptyDepartments = true) { $this->_showEmptyDepartments = $showEmptyDepartments; } public function getListSelect($options = array()) { $select = $this->_getFullListSelect(); $this->_select = $select; $this->_prepareListOptions($options); return $this->_select; } protected function _getColumns($type = 'Process') { $type = new Zend_Db_Expr($this->_db->quote($type)); return array('id', 'name', 'type' => $type); } protected function _getProcessSelect($query) { $selectProcess= $this->_db->select()->reset(); $selectProcess->from($this->_getPair('Process'), $this->_getColumns()); Qs_Db_Filter::where($selectProcess, $this->_processFilterFields, $query); $selectProcess->joinLeft( array('SubDepartment' => $this->_getTableName('Department')), 'SubDepartment.id=' . $this->_db->quoteIdentifier('Process.departmentId'), array() ); return $this->_joinDepartment($selectProcess); } protected function _getDepartmentSelect($query) { $selectDepartment = $this->_db->select()->reset(); $selectDepartment->from(array('SubDepartment' => $this->_getTableName('Department')), $this->_getColumns('Department')); Qs_Db_Filter::where($selectDepartment, $this->_filterFields, $query); if (!$this->_showEmptyDepartments) { $innerSelect = $this->_db->select()->reset() ->from(array('InnerProcess' => $this->_getTableName('Process')), array('id')) ->join(array('InnerDepartment' => $this->_getTableName('Department')), 'InnerProcess.departmentId = InnerDepartment.id', array()) ->where('SubDepartment.id IN (InnerDepartment.id, InnerDepartment.parentId)'); $selectDepartment->where('EXISTS(' . $innerSelect . ')'); } return $this->_joinDepartment($selectDepartment); } protected function _joinDepartment(Zend_Db_Select $select) { $select->joinLeft( $this->_getPair('Department'), 'Department.id=SubDepartment.parentId', array( 'subDepartmentName' => DepartmentModel::getNameExpr(), 'subDepartmentId' => DepartmentModel::getIdExpr(), 'departmentName' => 'ISNULL(Department.name, SubDepartment.name)', 'departmentId' => 'ISNULL(Department.id, SubDepartment.id)', ) ); return $select; } protected function _getFullListSelect() { $query = $this->_filter['query']; $select = $this->_db->select()->reset()->union(array( $this->_getDepartmentSelect($query), $this->_getProcessSelect($query) )); return $select; } public function getCountMatchItem() { return count($this->_db->fetchAll($this->_getFullListSelect())); } protected function _prepareList(array &$list) { App_Search_Obj::markWords($list, $this->_markItem, $this->_filter['query']); return parent::_prepareList($list); } }