1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Similar plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* Similar for Craft lets you find elements, Entries, Categories, Commerce Products, etc, that are similar, based on... other related elements. |
6
|
|
|
* |
7
|
|
|
* @link https://nystudio107.com/ |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2018 nystudio107.com |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\similar; |
12
|
|
|
|
13
|
|
|
use craft\base\Element; |
14
|
|
|
use nystudio107\similar\behaviors\CountBehavior; |
15
|
|
|
use nystudio107\similar\services\Similar as SimilarService; |
16
|
|
|
use nystudio107\similar\variables\SimilarVariable; |
17
|
|
|
|
18
|
|
|
use Craft; |
19
|
|
|
use craft\base\Plugin; |
20
|
|
|
use craft\elements\db\ElementQuery; |
21
|
|
|
use craft\events\PluginEvent; |
22
|
|
|
use craft\events\PopulateElementEvent; |
23
|
|
|
use craft\services\Plugins; |
24
|
|
|
use craft\web\twig\variables\CraftVariable; |
25
|
|
|
|
26
|
|
|
use yii\base\Event; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class Similar |
30
|
|
|
* |
31
|
|
|
* @author nystudio107.com |
|
|
|
|
32
|
|
|
* @package Similar |
|
|
|
|
33
|
|
|
* @since 1.0.0 |
|
|
|
|
34
|
|
|
* |
35
|
|
|
* @property SimilarService $similar |
|
|
|
|
36
|
|
|
*/ |
|
|
|
|
37
|
|
|
class Similar extends Plugin |
38
|
|
|
{ |
39
|
|
|
// Static Properties |
40
|
|
|
// ========================================================================= |
41
|
|
|
|
42
|
|
|
/** |
|
|
|
|
43
|
|
|
* @var Similar |
44
|
|
|
*/ |
45
|
|
|
public static $plugin; |
46
|
|
|
|
47
|
|
|
// Static Methods |
48
|
|
|
// ========================================================================= |
49
|
|
|
|
50
|
|
|
/** |
|
|
|
|
51
|
|
|
* @inheritdoc |
52
|
|
|
*/ |
53
|
|
|
public function __construct($id, $parent = null, array $config = []) |
54
|
|
|
{ |
55
|
|
|
$config['components'] = [ |
56
|
|
|
'similar' => SimilarService::class, |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
parent::__construct($id, $parent, $config); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Public Properties |
63
|
|
|
// ========================================================================= |
64
|
|
|
|
65
|
|
|
/** |
|
|
|
|
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
public $schemaVersion = '1.0.0'; |
69
|
|
|
|
70
|
|
|
/** |
|
|
|
|
71
|
|
|
* @var bool |
72
|
|
|
*/ |
73
|
|
|
public $hasCpSection = false; |
74
|
|
|
|
75
|
|
|
/** |
|
|
|
|
76
|
|
|
* @var bool |
77
|
|
|
*/ |
78
|
|
|
public $hasCpSettings = false; |
79
|
|
|
|
80
|
|
|
// Public Methods |
81
|
|
|
// ========================================================================= |
82
|
|
|
|
83
|
|
|
/** |
|
|
|
|
84
|
|
|
* @inheritdoc |
85
|
|
|
*/ |
|
|
|
|
86
|
|
|
public function init() |
87
|
|
|
{ |
88
|
|
|
parent::init(); |
89
|
|
|
self::$plugin = $this; |
90
|
|
|
|
91
|
|
|
Event::on( |
92
|
|
|
CraftVariable::class, |
93
|
|
|
CraftVariable::EVENT_INIT, |
94
|
|
|
function (Event $event) { |
95
|
|
|
/** @var CraftVariable $variable */ |
|
|
|
|
96
|
|
|
$variable = $event->sender; |
97
|
|
|
$variable->set('similar', SimilarVariable::class); |
98
|
|
|
} |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
Event::on( |
102
|
|
|
ElementQuery::class, |
103
|
|
|
ElementQuery::EVENT_AFTER_POPULATE_ELEMENT, |
104
|
|
|
function(PopulateElementEvent $event) { |
|
|
|
|
105
|
|
|
$element = $event->element; |
106
|
|
|
/** @var Element $element */ |
|
|
|
|
107
|
|
|
$element->attachBehavior('myCountBehavior', CountBehavior::class); |
108
|
|
|
}); |
|
|
|
|
109
|
|
|
|
110
|
|
|
Event::on( |
111
|
|
|
Plugins::class, |
112
|
|
|
Plugins::EVENT_AFTER_INSTALL_PLUGIN, |
113
|
|
|
function (PluginEvent $event) { |
114
|
|
|
if ($event->plugin === $this) { |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
Craft::info( |
120
|
|
|
Craft::t( |
121
|
|
|
'similar', |
122
|
|
|
'{name} plugin loaded', |
123
|
|
|
['name' => $this->name] |
124
|
|
|
), |
125
|
|
|
__METHOD__ |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// Protected Methods |
130
|
|
|
// ========================================================================= |
131
|
|
|
} |
132
|
|
|
|