Guest User

Untitled

a guest
May 22nd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\models\main_search;
  4.  
  5. use app\models\Organization;
  6. use app\models\Programs;
  7. use Yii;
  8. use yii\base\Model;
  9. use yii\data\ActiveDataProvider;
  10. use yii\db\ActiveQuery;
  11. use yii\helpers\ArrayHelper;
  12.  
  13. class CombinedSearch extends Model
  14. {
  15. const PAGE_LIMIT = 10;
  16. const TYPE_PROGRAM = 1;
  17. const TYPE_ORGANIZATION = 2;
  18.  
  19. public $isSimpleSearch;
  20. public $name;
  21. public $page = 1;
  22.  
  23. public function rules()
  24. {
  25. return [
  26. ['name', 'string'],
  27. ['page', 'integer'],
  28. ];
  29. }
  30.  
  31. /**
  32. * @return array
  33. */
  34. public function search()
  35. {
  36. $programs = $this->selectPrograms();
  37. $organization = [];
  38.  
  39. $countPageProgram = $this->getCountPagePrograms();
  40.  
  41. if ($this->page >= $countPageProgram) {
  42. $limit = self::PAGE_LIMIT - $this->selectCountProgramsMaxOffset();
  43. $offset = $limit + ($this->page - $this->getCountPagePrograms() - 1) * self::PAGE_LIMIT;
  44.  
  45. }
  46.  
  47. if ($this->page == $countPageProgram) {
  48. $organization = $this->selectOrganization($limit, $offset);
  49. }
  50.  
  51. if ($this->page > $countPageProgram) {
  52. $organization = $this->selectOrganization(self::PAGE_LIMIT, $offset);
  53. }
  54.  
  55. return [
  56. 'items' => ArrayHelper::merge($this->addProgramLabel($programs), $this->addOrganizationLabel($organization)),
  57. 'current_page' => $this->page,
  58. 'next_page' => $this->page + 1
  59. ];
  60.  
  61. }
  62.  
  63. /**
  64. * return array
  65. */
  66. public function getProgramAndOrganizationModels()
  67. {
  68. $programQueryActiveProvider = $this->getProgramQueryActiveProvider(1);
  69. $programs = $programQueryActiveProvider->models;
  70.  
  71. $organizationQueryActiveProvider = $this->getOrganizationQueryActiveProvider(1);
  72. $programs = $programQueryActiveProvider->models;
  73.  
  74.  
  75. }
  76.  
  77. /**
  78. *
  79. */
  80. public function getProgramsCount($page)
  81. {
  82. $q = $this->getProgramQueryActiveProvider(1);
  83. $last = $q->pagination->totalCount - ($q->pagination->pageCount - 1) * $q->pagination->pageSize;
  84.  
  85. if ($this->page >= $q->pagination->pageCount) {
  86. $pageSize = self::PAGE_LIMIT;
  87. if ($this->page == $q->pagination->pageCount) {
  88. $pageSize = self::PAGE_LIMIT - $last;
  89. }
  90. $w = $this->getOrganizationQueryActiveProvider($page, $pageSize);
  91. $r = ($w->totalCount + $q->totalCount)/self::PAGE_LIMIT;
  92. }
  93.  
  94. return;
  95. }
  96.  
  97. /**
  98. * @param array $programs
  99. * @return array
  100. */
  101. private function addProgramLabel(array $programs)
  102. {
  103. foreach ($programs as $key => $program) {
  104. $programs[$key]['type'] = self::TYPE_PROGRAM;
  105. }
  106.  
  107. return $programs;
  108. }
  109.  
  110. /**
  111. * @param array $organizations
  112. * @return array
  113. */
  114. private function addOrganizationLabel(array $organizations)
  115. {
  116. foreach ($organizations as $key => $organization) {
  117. $organizations[$key]['type'] = self::TYPE_PROGRAM;
  118. }
  119.  
  120. return $organizations;
  121. }
  122.  
  123. /**
  124. * @return \yii\db\ActiveQuery
  125. */
  126. private function getProgramQuery()
  127. {
  128. return Programs::find()
  129. ->select(['id', 'name', 'short_name'])
  130. ->where(['verification' => Programs::VERIFICATION_DONE])
  131. ->andwhere(['like', 'name', $this->name]);
  132. }
  133.  
  134. /**
  135. * @return \yii\db\ActiveQuery
  136. */
  137. private function getOrganizationQuery()
  138. {
  139. return Organization::find()
  140. ->select(['id', 'name', 'full_name as short_name', 'cratedate as created_at'])
  141. ->where(['status' => Organization::STATUS_ACTIVE])
  142. ->andwhere(['like', 'name', $this->name]);
  143. }
  144.  
  145. /**
  146. * @param int $page_size
  147. *
  148. * @return ActiveDataProvider
  149. */
  150. private function getProgramQueryActiveProvider($page, $page_size = 10)
  151. {
  152. $q = new ActiveDataProvider(['query' => $this->getProgramQuery(), 'pagination' => ['page' => $page, 'pageSize' => $page_size]]);
  153. $q->prepare();
  154. return $q;
  155. }
  156.  
  157. /**
  158. * @param int $page_size
  159. *
  160. * @return ActiveDataProvider
  161. */
  162. private function getOrganizationQueryActiveProvider($page, $page_size = 10)
  163. {
  164. $q = new ActiveDataProvider(['query' => $this->getOrganizationQuery(), 'pagination' => ['page' => $page, 'pageSize' => $page_size]]);
  165. $q->prepare();
  166. return $q;
  167. }
  168.  
  169. /**
  170. * @return array
  171. */
  172. private function selectPrograms()
  173. {
  174. return $this->getProgramQuery()
  175. ->limit(self::PAGE_LIMIT)
  176. ->offset(self::PAGE_LIMIT * ($this->page - 1))
  177. ->asArray()
  178. ->all();
  179. }
  180.  
  181. private function selectCountProgramsMaxOffset()
  182. {
  183. $page = ($this->getCountPagePrograms() == 1) ? 0 : $this->getCountPagePrograms() - 1;
  184. $result = $this->getProgramQuery()
  185. ->limit(self::PAGE_LIMIT)
  186. ->offset(self::PAGE_LIMIT * $page)
  187. ->all();
  188.  
  189. return count($result);
  190. }
  191.  
  192.  
  193.  
  194. /**
  195. * @param int $limit
  196. * @return array
  197. */
  198. private function selectOrganization($limit, $offset)
  199. {
  200. return $this->getOrganizationQuery()
  201. ->limit($limit)
  202. ->offset($offset)
  203. ->all();
  204. }
  205.  
  206. /**
  207. * @return int|string
  208. */
  209. private function getCountPrograms()
  210. {
  211. return $this->getProgramQuery()->count();
  212. }
  213.  
  214. /**
  215. * @return int|string
  216. */
  217. private function getCountOrganization()
  218. {
  219. return $this->getOrganizationQuery()->count();
  220. }
  221.  
  222.  
  223. /**
  224. * @return float
  225. */
  226. private function getCountPagePrograms()
  227. {
  228. return ceil ( $this->getCountPrograms() / self::PAGE_LIMIT );
  229. }
  230.  
  231. /**
  232. * @return float
  233. */
  234. private function getCountPageOrganization()
  235. {
  236. return ceil ( $this->getCountOrganization() / self::PAGE_LIMIT );
  237. }
  238.  
  239.  
  240. private function getOffsetOrganisation()
  241. {
  242.  
  243. }
  244.  
  245. }
Add Comment
Please, Sign In to add comment