_getDocumentsCountExpr(); $columns['nextMeetingDate'] = $this->_getNextMeetingsDayExpr(); $columns['membersCount'] = $this->_getMembersCountExpr(); return $columns; } protected function _getMembersCountExpr() { $select = $this->_db->select(); $select->from( $this->_getPair('UserCommittee', 'uc'), array('membersCount' => 'COUNT(uc.userId)') ); $select->where('`uc`.`committeeId` = `Committee`.`id`'); return new \Zend_Db_Expr('(' . $select . ')'); } protected function _getNextMeetingsDayExpr() { $select = $this->_db->select(); $select->from($this->_getPair('Event'), 'startDate'); $select->where('`Event`.`committeeId` = `Committee`.`id`') ->where('`Event`.`startDate` > ?', date('Y-m-d')) ->order('Event.startDate ASC') ->limit(1); \App\Event\Admin\Obj::filterEvents($select, 'Event'); return new \Zend_Db_Expr('(' . $select . ')'); } protected function _getDocumentsCountExpr() { $select = $this->_db->select(); $select->from($this->_getPair('Document'), 'COUNT(`Document`.`id`)') ->where('`Document`.`committeeId` = `Committee`.`id`'); return new \Zend_Db_Expr('('. $select . ')'); } protected function _addDependenciesFromDb(array &$data) { $data['members'] = $this->_getMembersFromDb(); return parent::_addDependenciesFromDb($data); } protected function _getMembersFromDb() { $select = $this->_db->select(); $select->from($this->_getPair('UserCommittee'), array('userId', 'roleId')) ->join( $this->_getPair('User'), '`User`.`id` = `UserCommittee`.`userId`', array('name' => 'CONCAT(`User`.`firstName`, " ", `User`.`lastName`)') ) ->where('`UserCommittee`.`committeeId` = ?', $this->_primaryKey, \Qs_Db::INT_TYPE) ->order(array('User.lastName', 'User.firstName')); return $this->_db->fetchAll($select); } protected function _updateDependency() { parent::_updateDependency(); $this->_dispatchEvent(); $this->_deleteIds('UserCommittee', 'committeeId'); $this->_insertMembers($this->_data['members']); } protected function _dispatchEvent() { \Qs_Event_Dispatcher::getInstance()->dispatch('postPublish', null, $this); } protected function _deleteDependency() { $this->_dispatchEvent(); return parent::_deleteDependency(); } protected function _deleteMembers($membersId) { if (!$membersId) { return $this; } $where = '`userId` IN (' . $this->_db->quote($membersId, \Qs_Db::INT_TYPE) . ')' . ' AND `committeeId` = ' . $this->_db->quote($this->_primaryKey, \Qs_Db::INT_TYPE); $this->_getTable('UserCommittee')->delete($where); return $this; } protected function _insertDependency() { parent::_insertDependency(); $this->_dispatchEvent(); if (empty($this->_data['members'])) { return $this; } $this->_insertMembers($this->_data['members']); return $this; } protected function _insertMembers(array $members) { if (!$members) { return $this; } $membersString = array(); foreach ($members as $member) { $membersString[] = '(' . $this->_db->quote($member['userId'], \Qs_Db::INT_TYPE) . ', ' . $this->_db->quote($member['roleId']) . ', ' . $this->_db->quote($this->_primaryKey, \Qs_Db::INT_TYPE) . ')'; } $sql = 'INSERT INTO ' . $this->_getTableName('UserCommittee') . ' (`userId`, `roleId`, `committeeId`)' . ' VALUES ' . implode(', ', $membersString); $this->_db->query($sql); return $this; } public function initFromForm(array $data) { $data['members'] = $this->_filterMembersFromForm($data['members']); return parent::initFromForm($data); } protected function _filterMembersFromForm($members) { $mergedMembers = array(); foreach ($members as $member) { if ($member['userId']) { $mergedMembers[] = array( 'userId' => $member['userId'], 'roleId' => $member['roleId'], ); } } return $mergedMembers; } protected function _hasDependentEvents() { $select = $this->_db->select(); $select->from($this->_getPair('Event'), 'id') ->where('`committeeId` = ?', $this->_primaryKey, \Qs_Db::INT_TYPE) ->where('`deleted` = "n"') ->limit(1); return (bool) $this->_db->fetchOne($select); } public function delete() { $this->_clearErrors(); $this->_clearErrors(); if (null === $this->getData()) { $this->_addError(static::MSG_INVALID_RECORD_ID); return false; } if ($this->_hasDependentEvents()) { $msg = "You can't remove this Committee cause it's linked with Event(s). " . "Please remove the connection first."; $this->_addError($msg); return false; } if ($this->getCheckForeignKeyDependencies()) { $foreignKeyLocks = $this->getForeignKeysLockItems(); if ($foreignKeyLocks) { $this->_addError($this->_getForeignKeyLocksMessages($foreignKeyLocks)); return false; } } $this->_deleteDependency(); $this->_deleteFiles(); return $this->_getTable()->deleteByKey($this->_primaryKey); } }