|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created for IG Monitoring. |
|
4
|
|
|
* User: jakim <[email protected]> |
|
5
|
|
|
* Date: 14.05.2018 |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace app\modules\api\v1\models; |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
use app\models\AccountStats; |
|
|
|
|
|
|
12
|
|
|
use app\models\AccountTag; |
|
13
|
|
|
use yii\base\Model; |
|
14
|
|
|
use yii\data\ActiveDataFilter; |
|
15
|
|
|
use yii\data\ActiveDataProvider; |
|
16
|
|
|
use yii\helpers\ArrayHelper; |
|
17
|
|
|
use yii\helpers\Inflector; |
|
18
|
|
|
use yii\helpers\StringHelper; |
|
19
|
|
|
|
|
20
|
|
|
class AccountSearch extends Model |
|
21
|
|
|
{ |
|
22
|
|
|
public function search(array $params) |
|
23
|
|
|
{ |
|
24
|
|
|
$query = Account::find() |
|
25
|
|
|
->leftJoin( |
|
26
|
|
|
AccountStats::tableName(), |
|
27
|
|
|
'account.id=account_stats.account_id and account_stats.id = (SELECT MAX(id) FROM account_stats WHERE account_stats.account_id=account.id)' |
|
28
|
|
|
); |
|
29
|
|
|
|
|
30
|
|
|
$tags = ArrayHelper::getValue($params, 'filter.tags'); |
|
31
|
|
|
unset($params['filter']['tags']); |
|
32
|
|
|
|
|
33
|
|
|
if ($tags) { |
|
34
|
|
|
$tags = StringHelper::explode($tags, ',', true, true); |
|
35
|
|
|
$tag = array_shift($tags); |
|
36
|
|
|
$accountIds = $this->getTaggedAccountIds($tag); |
|
37
|
|
|
|
|
38
|
|
|
foreach ($tags as $tag) { |
|
39
|
|
|
$accountIds = array_intersect($accountIds, $this->getTaggedAccountIds($tag)); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$query->andWhere(['account.id' => $accountIds]); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$dataFilter = \Yii::createObject([ |
|
46
|
|
|
'class' => ActiveDataFilter::class, |
|
47
|
|
|
'searchModel' => DataFilterForm::class, |
|
48
|
|
|
]); |
|
49
|
|
|
if ($dataFilter->load($params)) { |
|
50
|
|
|
$filter = $dataFilter->build(); |
|
51
|
|
|
if ($filter === false) { |
|
52
|
|
|
return $dataFilter; |
|
53
|
|
|
} |
|
54
|
|
|
$query->andWhere($filter); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$dataProvider = new ActiveDataProvider([ |
|
58
|
|
|
'query' => $query, |
|
59
|
|
|
]); |
|
60
|
|
|
|
|
61
|
|
|
$dataProvider->sort->attributes['followed_by'] = [ |
|
62
|
|
|
'asc' => ['account_stats.followed_by' => SORT_ASC], |
|
63
|
|
|
'desc' => ['account_stats.followed_by' => SORT_DESC], |
|
64
|
|
|
]; |
|
65
|
|
|
$dataProvider->sort->attributes['follows'] = [ |
|
66
|
|
|
'asc' => ['account_stats.follows' => SORT_ASC], |
|
67
|
|
|
'desc' => ['account_stats.follows' => SORT_DESC], |
|
68
|
|
|
]; |
|
69
|
|
|
$dataProvider->sort->attributes['media'] = [ |
|
70
|
|
|
'asc' => ['account_stats.media' => SORT_ASC], |
|
71
|
|
|
'desc' => ['account_stats.media' => SORT_DESC], |
|
72
|
|
|
]; |
|
73
|
|
|
$dataProvider->sort->attributes['er'] = [ |
|
74
|
|
|
'asc' => ['account_stats.er' => SORT_ASC], |
|
75
|
|
|
'desc' => ['account_stats.er' => SORT_DESC], |
|
76
|
|
|
]; |
|
77
|
|
|
|
|
78
|
|
|
return $dataProvider; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param $tag |
|
83
|
|
|
* @return array |
|
84
|
|
|
*/ |
|
85
|
|
|
private function getTaggedAccountIds($tag): array |
|
86
|
|
|
{ |
|
87
|
|
|
$accountIds = AccountTag::find() |
|
88
|
|
|
->select('account_id') |
|
89
|
|
|
->innerJoinWith('tag') |
|
90
|
|
|
->andFilterWhere(['tag.slug' => Inflector::slug($tag)]) |
|
91
|
|
|
->column(); |
|
92
|
|
|
|
|
93
|
|
|
return $accountIds; |
|
94
|
|
|
} |
|
95
|
|
|
} |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: