Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace app\models\main_search;
- use app\models\Organization;
- use app\models\Programs;
- use Yii;
- use yii\base\Model;
- use yii\data\ActiveDataProvider;
- use yii\db\ActiveQuery;
- use yii\helpers\ArrayHelper;
- class CombinedSearch extends Model
- {
- const PAGE_LIMIT = 10;
- const TYPE_PROGRAM = 1;
- const TYPE_ORGANIZATION = 2;
- public $isSimpleSearch;
- public $name;
- public $page = 1;
- public function rules()
- {
- return [
- ['name', 'string'],
- ['page', 'integer'],
- ];
- }
- /**
- * @return array
- */
- public function search()
- {
- $programs = $this->selectPrograms();
- $organization = [];
- $countPageProgram = $this->getCountPagePrograms();
- if ($this->page >= $countPageProgram) {
- $limit = self::PAGE_LIMIT - $this->selectCountProgramsMaxOffset();
- $offset = $limit + ($this->page - $this->getCountPagePrograms() - 1) * self::PAGE_LIMIT;
- }
- if ($this->page == $countPageProgram) {
- $organization = $this->selectOrganization($limit, $offset);
- }
- if ($this->page > $countPageProgram) {
- $organization = $this->selectOrganization(self::PAGE_LIMIT, $offset);
- }
- return [
- 'items' => ArrayHelper::merge($this->addProgramLabel($programs), $this->addOrganizationLabel($organization)),
- 'current_page' => $this->page,
- 'next_page' => $this->page + 1
- ];
- }
- /**
- * return array
- */
- public function getProgramAndOrganizationModels()
- {
- $programQueryActiveProvider = $this->getProgramQueryActiveProvider(1);
- $programs = $programQueryActiveProvider->models;
- $organizationQueryActiveProvider = $this->getOrganizationQueryActiveProvider(1);
- $programs = $programQueryActiveProvider->models;
- }
- /**
- *
- */
- public function getProgramsCount($page)
- {
- $q = $this->getProgramQueryActiveProvider(1);
- $last = $q->pagination->totalCount - ($q->pagination->pageCount - 1) * $q->pagination->pageSize;
- if ($this->page >= $q->pagination->pageCount) {
- $pageSize = self::PAGE_LIMIT;
- if ($this->page == $q->pagination->pageCount) {
- $pageSize = self::PAGE_LIMIT - $last;
- }
- $w = $this->getOrganizationQueryActiveProvider($page, $pageSize);
- $r = ($w->totalCount + $q->totalCount)/self::PAGE_LIMIT;
- }
- return;
- }
- /**
- * @param array $programs
- * @return array
- */
- private function addProgramLabel(array $programs)
- {
- foreach ($programs as $key => $program) {
- $programs[$key]['type'] = self::TYPE_PROGRAM;
- }
- return $programs;
- }
- /**
- * @param array $organizations
- * @return array
- */
- private function addOrganizationLabel(array $organizations)
- {
- foreach ($organizations as $key => $organization) {
- $organizations[$key]['type'] = self::TYPE_PROGRAM;
- }
- return $organizations;
- }
- /**
- * @return \yii\db\ActiveQuery
- */
- private function getProgramQuery()
- {
- return Programs::find()
- ->select(['id', 'name', 'short_name'])
- ->where(['verification' => Programs::VERIFICATION_DONE])
- ->andwhere(['like', 'name', $this->name]);
- }
- /**
- * @return \yii\db\ActiveQuery
- */
- private function getOrganizationQuery()
- {
- return Organization::find()
- ->select(['id', 'name', 'full_name as short_name', 'cratedate as created_at'])
- ->where(['status' => Organization::STATUS_ACTIVE])
- ->andwhere(['like', 'name', $this->name]);
- }
- /**
- * @param int $page_size
- *
- * @return ActiveDataProvider
- */
- private function getProgramQueryActiveProvider($page, $page_size = 10)
- {
- $q = new ActiveDataProvider(['query' => $this->getProgramQuery(), 'pagination' => ['page' => $page, 'pageSize' => $page_size]]);
- $q->prepare();
- return $q;
- }
- /**
- * @param int $page_size
- *
- * @return ActiveDataProvider
- */
- private function getOrganizationQueryActiveProvider($page, $page_size = 10)
- {
- $q = new ActiveDataProvider(['query' => $this->getOrganizationQuery(), 'pagination' => ['page' => $page, 'pageSize' => $page_size]]);
- $q->prepare();
- return $q;
- }
- /**
- * @return array
- */
- private function selectPrograms()
- {
- return $this->getProgramQuery()
- ->limit(self::PAGE_LIMIT)
- ->offset(self::PAGE_LIMIT * ($this->page - 1))
- ->asArray()
- ->all();
- }
- private function selectCountProgramsMaxOffset()
- {
- $page = ($this->getCountPagePrograms() == 1) ? 0 : $this->getCountPagePrograms() - 1;
- $result = $this->getProgramQuery()
- ->limit(self::PAGE_LIMIT)
- ->offset(self::PAGE_LIMIT * $page)
- ->all();
- return count($result);
- }
- /**
- * @param int $limit
- * @return array
- */
- private function selectOrganization($limit, $offset)
- {
- return $this->getOrganizationQuery()
- ->limit($limit)
- ->offset($offset)
- ->all();
- }
- /**
- * @return int|string
- */
- private function getCountPrograms()
- {
- return $this->getProgramQuery()->count();
- }
- /**
- * @return int|string
- */
- private function getCountOrganization()
- {
- return $this->getOrganizationQuery()->count();
- }
- /**
- * @return float
- */
- private function getCountPagePrograms()
- {
- return ceil ( $this->getCountPrograms() / self::PAGE_LIMIT );
- }
- /**
- * @return float
- */
- private function getCountPageOrganization()
- {
- return ceil ( $this->getCountOrganization() / self::PAGE_LIMIT );
- }
- private function getOffsetOrganisation()
- {
- }
- }
Add Comment
Please, Sign In to add comment