setSelectOptions(array('order' => 'sorter')); $list = parent::getList(); $committeeIds = \Qs_Array::fetchCol($list, 'id'); $this->_processLeaderships($committeeIds, $list); return $list; } public function getListSelect() { if (null === $this->_select) { $userId = \App_User_Auth::getInstance()->getData('id'); $joinCondition = '`uc`.`committeeId` = `Committee`.`id` ' . 'AND `uc`.`userId` = ' . $this->_db->quote($userId, \Qs_Db::INT_TYPE); $this->_select = parent::getListSelect(); $this->_select->join( $this->_getPair('UserCommittee', 'uc'), $joinCondition, array() ); $this->_select->join( $this->_getPair('UserCommitteeRole', 'ucr'), '`ucr`.`id` = `uc`.`roleId`', array('isLeadership' => 'ucr.leadership') ); } return $this->_select; } protected function _processLeaderships($committeeIds, &$list) { if (empty($committeeIds) || empty($list)) { return $this; } $types = array('chair', 'coChair', 'viceChair', 'none'); $select = $this->_db->select(); $select->from($this->_getPair('User', 'u'), array('uc.committeeId', 'uc.roleId', 'firstName', 'lastName', 'phone', 'email', 'id')) ->join($this->_getPair('UserCommittee', 'uc'), '`u`.`id` = `uc`.`userId`', array()) ->joinLeft($this->_getPair('UserGroup', 'ug'), '`ug`.`id` = `u`.`groupId`', array('groupTitle' => 'title')) ->where('`roleId` IN (?)', $types) ->where('`committeeId` IN (?)', $committeeIds, \Qs_Db::INT_TYPE); $groupedByCommittee = $this->_db->fetchAll($select, array(), \Qs_Db::FETCH_GROUP); $grouped = array(); $userId = \App_User_Auth::getInstance()->getData('id'); $canManageIds = array(); foreach ($groupedByCommittee as $committeeId => $groupUsers) { $grouped[$committeeId] = array(); foreach ($groupUsers as $user) { $grouped[$committeeId][$user['roleId']][] = $user; if ($user['id'] == $userId) { $canManageIds[] = $committeeId; } } } $canManageIds = array_unique($canManageIds); foreach ($list as &$item) { $item['leadership'] = $grouped[$item['id']]; $item['canManage'] = in_array($item['id'], $canManageIds); } unset($item); return $this; } }