1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE |
6
|
|
|
* @link https://github.com/flipboxfactory/craft-ember/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\craft\ember\services\elements; |
10
|
|
|
|
11
|
|
|
use craft\base\ElementInterface; |
12
|
|
|
use craft\errors\ElementNotFoundException; |
13
|
|
|
use flipbox\craft\ember\helpers\SiteHelper; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Flipbox Factory <[email protected]> |
17
|
|
|
* @since 2.0.0 |
18
|
|
|
*/ |
19
|
|
|
trait MultiSiteElementAccessorTrait |
20
|
|
|
{ |
21
|
|
|
use ElementAccessorTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param $identifier |
25
|
|
|
* @param int|null $siteId |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
|
|
protected function identifierCondition($identifier, int $siteId = null): array |
29
|
|
|
{ |
30
|
|
|
$base = [ |
31
|
|
|
'siteId' => SiteHelper::ensureSiteId($siteId), |
32
|
|
|
'status' => null |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
if (is_array($identifier)) { |
36
|
|
|
return array_merge($base, $identifier); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$base['id'] = $identifier; |
40
|
|
|
|
41
|
|
|
return $base; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/******************************************* |
45
|
|
|
* FIND / GET |
46
|
|
|
*******************************************/ |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param int|null $siteId |
50
|
|
|
* @return ElementInterface[] |
51
|
|
|
*/ |
52
|
|
|
public function findAll(int $siteId = null) |
53
|
|
|
{ |
54
|
|
|
$config = []; |
55
|
|
|
if ($siteId !== null) { |
56
|
|
|
$config['siteId'] = $siteId; |
57
|
|
|
} |
58
|
|
|
return $this->getQuery($config)->all(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param $identifier |
63
|
|
|
* @param int|null $siteId |
64
|
|
|
* @return ElementInterface|null |
65
|
|
|
*/ |
66
|
|
|
public function find($identifier, int $siteId = null) |
67
|
|
|
{ |
68
|
|
|
if ($identifier instanceof ElementInterface) { |
69
|
|
|
return $identifier; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->findByQuery($this->getQuery( |
73
|
|
|
$this->identifierCondition($identifier, $siteId) |
74
|
|
|
)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param $identifier |
79
|
|
|
* @param int $siteId |
80
|
|
|
* @return ElementInterface |
81
|
|
|
* @throws ElementNotFoundException |
82
|
|
|
*/ |
83
|
|
|
public function get($identifier, int $siteId = null): ElementInterface |
84
|
|
|
{ |
85
|
|
|
if (!$object = $this->find($identifier, $siteId)) { |
86
|
|
|
$this->notFoundException(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $object; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|