_applicationUserId = $id; return $this; } public function getApplicationUserId() { if (!$this->_applicationUserId) { throw new Exception('Application User ID is not set'); } return $this->_applicationUserId; } public function getApplicationUserData($field = false, $default = null) { static $data; if (null === $data) { $obj = new App_Application_User_Obj(); $obj = $obj->setPrimaryKey($this->getApplicationUserId()); $data = $obj->getData(); } return Qs_Array::get($data, $field, $default); } public function setUserType($value) { if (!in_array($value, array(self::USER_TYPE_REVIEWER, self::USER_TYPE_APPLICANT))) { throw new Exception('Unknown user type'); } $this->_userType = $value; return $this; } public function getUserType() { if (!$this->_userType) { throw new Exception('User is not set'); } return $this->_userType; } public function setHasFilter($value = true) { $this->_hasFilter = (bool) $value; return $this; } public function setFilterFields($fileds) { $this->_filterFields = $fileds; return $this; } public function idAllowedApplication($userId, $reviewerId) { $select = $this->_db->select(); $select->from($this->_getPair('ApplicationReviewer2User'), array(new Zend_Db_Expr('1'))); $select->where('`userId` = ?', (string) $userId, Qs_Db::INT_TYPE); $select->where('`reviewerId` = ?', (string) $reviewerId, Qs_Db::INT_TYPE); return (bool) $this->_db->fetchOne($select); } public static function getStatusPairs() { return array( self::STATUS_NOT_SUBMITTED => 'Not yet submitted', self::STATUS_SUBMITTED => 'Submitted', ); } public function getDocumentTypeById($id) { $select = $this->_db->select(); $select->from($this->_getPair('ApplicationDocumentType'), array('*')); $select->where('`id` = ?', $id); return $this->_db->fetchRow($select); } protected function _getFromColumns() { return array( new Zend_Db_Expr('`ApplicationDocumentType`.`id` AS `_documentTypeId`'), new Zend_Db_Expr('`ApplicationDocumentType`.`title`'), new Zend_Db_Expr('`ApplicationDocumentType`.`description`'), new Zend_Db_Expr('`ApplicationDocument`.*'), $this->_getStatusFromColumn(), ); } protected function _getStatusFromColumn() { $_submitted = $this->_db->quote(self::STATUS_SUBMITTED); $_notSubmitted = $this->_db->quote(self::STATUS_NOT_SUBMITTED); $_awaiting_review = $this->_db->quote(self::STATUS_AWAITING_REVIEW); $_review_completed = $this->_db->quote(self::STATUS_REVIEW_COMPLETED); if (self::USER_TYPE_APPLICANT == $this->getUserType()) { $column = new Zend_Db_Expr("IF (`ApplicationDocument`.`file` IS NULL, {$_notSubmitted}, {$_submitted}) AS `status`"); } else { $column = new Zend_Db_Expr("IF (`ApplicationDocument`.`reviewed` = 'y', {$_review_completed}, {$_awaiting_review}) AS `status`"); } return $column; } protected function _from(Zend_Db_Select $select = null) { if (null === $select) { $select = $this->select; } $select->from($this->_getPair('ApplicationDocumentType'), $this->_getFromColumns()); return $select; } protected function _join(Zend_Db_Select $select = null) { if (null === $select) { $select = $this->select; } $conditions[] = '`ApplicationDocumentType`.`id` = `ApplicationDocument`.`documentTypeId`'; $conditions[] = $this->_db->quoteInto('`' . $this->_tableAlias . '`.`applicationUserId` = ?', $this->getApplicationUserId(), Qs_Db::INT_TYPE); $select->joinLeft($this->pair, join(' AND ', $conditions), array()); return parent::_join($select); } protected function _where(Zend_Db_Select $select = null) { $select = parent::_where($select); if (self::USER_TYPE_REVIEWER == $this->getUserType()) { $select->where('`' . $this->_tableAlias . '`.`id` IS NOT NULL'); } return $select; } protected function _filterWhere(Zend_Db_Select $select) { parent::_filterWhere($select); if (!empty($this->_filter['status'])) { switch ($this->_filter['status']) { case self::STATUS_REVIEW_COMPLETED: $select->where('`reviewed` = "y"'); break; case self::STATUS_AWAITING_REVIEW: $select->where('`reviewed` = "n"'); } } return $select; } public function setReviewed($value) { if (false === ($row = $this->table->findRow($this->getPrimaryKey()))) { return false; } if ($value) { $value = 'y'; $reviewDate = date('Y-m-d H:i:s'); } else { $value = 'n'; $reviewDate = null; } $row->reviewed = $value; $row->reviewDate = $reviewDate; $row->save(); return true; } protected function _handleFiles() { $session = new Qs_Session_Namespace(CURRENT_PAGE); if (isset($session->files) && !empty($session->files)) { $adapter = new Qs_File_Transfer_Adapter_Db(); $adapter->setDefaultDestination(constant('WWW_PATH') . '/' . self::DOCUMENT_PATH); foreach ($session->files as $file => $name) { $adapter->delete($file, $name); } unset($session->files); } return $this; } protected function _deleteFiles($row = null) { if (is_array($this->_fileFields) && !empty($this->_fileFields)) { $adapter = new Qs_File_Transfer_Adapter_Db(); $adapter->setDefaultDestination(constant('WWW_PATH') . '/' . self::DOCUMENT_PATH); if (null === $row) { if (false !== ($row = $this->table->findRow($this->getPrimaryKey()))) { $row = $row->toArray(); } } if (is_array($row) && !empty($row)) { foreach ($this->_fileFields as $name) { if (isset($row[$name]) && !empty($row[$name])) { $adapter->delete($row[$name], $name); } } } unset($adapter); } } }