|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* ActiveRecord for API |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/yii2-hiart |
|
6
|
|
|
* @package yii2-hiart |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hiqdev\hiart; |
|
12
|
|
|
|
|
13
|
|
|
use yii\base\InvalidConfigException; |
|
14
|
|
|
use yii\db\QueryInterface; |
|
15
|
|
|
|
|
16
|
|
|
class ActiveDataProvider extends \yii\data\ActiveDataProvider |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var ActiveQuery the query that is used to fetch data models and [[totalCount]] |
|
20
|
|
|
* if it is not explicitly set |
|
21
|
|
|
*/ |
|
22
|
|
|
public $query; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* To improve performance, implemented grid summary and pager loading via AJAX when this attribute is `false` |
|
26
|
|
|
* There is a possibility set this attribute via DI |
|
27
|
|
|
* @see \hipanel\base\SearchModelTrait::search() |
|
28
|
|
|
* |
|
29
|
|
|
* @var bool |
|
30
|
|
|
*/ |
|
31
|
|
|
public bool $countSynchronously = false; |
|
32
|
|
|
|
|
33
|
|
|
public function enableSynchronousCount(): void |
|
34
|
|
|
{ |
|
35
|
|
|
$this->countSynchronously = true; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* When receiving the pager and summary through AJAX, to calculate the limit and offset of Grid, |
|
40
|
|
|
* you need to get the maximum possible total count, otherwise the Grid pages will not switch |
|
41
|
|
|
* |
|
42
|
|
|
* @return int |
|
43
|
|
|
* @throws \yii\base\InvalidConfigException |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function prepareTotalCount(): int |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->countSynchronously ? parent::prepareTotalCount() : PHP_INT_MAX; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function refresh(bool $keepTotalCount = false): void |
|
51
|
|
|
{ |
|
52
|
|
|
$tc = $this->getTotalCount(); |
|
53
|
|
|
parent::refresh(); |
|
54
|
|
|
if ($keepTotalCount) { |
|
55
|
|
|
$this->setTotalCount($tc); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function prepareQuery(): QueryInterface |
|
60
|
|
|
{ |
|
61
|
|
|
if (!$this->query instanceof QueryInterface) { |
|
|
|
|
|
|
62
|
|
|
throw new InvalidConfigException('The "query" property must be an instance of a class that implements the QueryInterface e.g. yii\db\Query or its subclasses.'); |
|
63
|
|
|
} |
|
64
|
|
|
$query = clone $this->query; |
|
65
|
|
|
if (($pagination = $this->getPagination()) !== false) { |
|
66
|
|
|
$pagination->totalCount = $this->getTotalCount(); |
|
67
|
|
|
$query->limit($pagination->getLimit())->offset($pagination->getOffset()); |
|
68
|
|
|
} |
|
69
|
|
|
if (($sort = $this->getSort()) !== false) { |
|
70
|
|
|
$query->addOrderBy($sort->getOrders()); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $query; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|