-
Notifications
You must be signed in to change notification settings - Fork 2
/
BorisPlugin.php
executable file
·267 lines (224 loc) · 7.36 KB
/
BorisPlugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
/**
* Boris plugin for Craft CMS
*
* Make your entries invincible!
*
* @author Nathaniel Hammond - @nfourtythree - webdna
* @copyright Copyright (c) 2017 Nathaniel Hammond - @nfourtythree - webdna
* @link https://webdna.co.uk
* @package Boris
* @since 1.0.0
*/
namespace Craft;
class BorisPlugin extends BasePlugin
{
/**
* Called after the plugin class is instantiated; do any one-time initialization here such as hooks and events:
*
* craft()->on('entries.saveEntry', function(Event $event) {
* // ...
* });
*
* or loading any third party Composer packages via:
*
* require_once __DIR__ . '/vendor/autoload.php';
*
* @return mixed
*/
public function init()
{
parent::init();
craft()->on( 'entries.onBeforeDeleteEntry', function( Event $event ) {
$settings = craft()->boris->unprepSettings( $this->getSettings() );
if ( $event->params[ 'entry' ] and ( $settings[ 'entryIds' ] ) and !empty( $settings[ 'entryIds' ] ) ) {
$titles = craft()->boris->invincible( array( $event->params[ 'entry' ]->id ), $settings[ 'entryIds' ] );
if ( count( $titles ) ) {
craft()->boris->showInvincibleNotice( $titles );
$event->performAction = false;
}
}
} );
craft()->on( 'categories.onBeforeDeleteCategory', function( Event $event ) {
$settings = craft()->boris->unprepSettings( $this->getSettings() );
if ( $event->params[ 'category' ] and ( $settings[ 'categoryIds' ] ) and count( $settings[ 'categoryIds' ] ) ) {
$titles = craft()->boris->invincible( array( $event->params[ 'category' ]->id ), $settings[ 'categoryIds' ] );
if ( count( $titles ) ) {
craft()->boris->showInvincibleNotice( $titles );
Craft::app()->getRequest()->redirect( UrlHelper::getCpUrl( 'categories' ) );
}
}
} );
craft()->on( 'elements.onBeforePerformAction', function( Event $event ) {
$settings = craft()->boris->unprepSettings( $this->getSettings() );
if ( $event->params[ 'action' ]->classHandle == 'Delete' ) {
$titles = craft()->boris->invincible( $event->params[ 'criteria' ]->ids(), $settings[ 'entryIds' ] );
if ( count( $titles ) ) {
// TODO: Figure out how to pass back notice on performAction
// craft()->boris->showInvincibleNotice( $titles );
$event->performAction = false;
}
$titles = craft()->boris->invincible( $event->params[ 'criteria' ]->ids(), $settings[ 'categoryIds' ] );
if ( count( $titles ) ) {
// TODO: Figure out how to pass back notice on performAction
craft()->boris->showInvincibleNotice( $titles );
$event->performAction = false;
}
}
} );
}
/**
* Returns the user-facing name.
*
* @return mixed
*/
public function getName()
{
$settings = $this->getSettings();
if ( $settings->name ) {
return $settings->name;
}
return Craft::t( 'Boris' );
}
/**
* Plugins can have descriptions of themselves displayed on the Plugins page by adding a getDescription() method
* on the primary plugin class:
*
* @return mixed
*/
public function getDescription()
{
return Craft::t( 'Make your entries invincible!' );
}
/**
* Plugins can have links to their documentation on the Plugins page by adding a getDocumentationUrl() method on
* the primary plugin class:
*
* @return string
*/
public function getDocumentationUrl()
{
return 'https://github.com/webdna/boris/blob/master/README.md';
}
/**
* Plugins can now take part in Craft’s update notifications, and display release notes on the Updates page, by
* providing a JSON feed that describes new releases, and adding a getReleaseFeedUrl() method on the primary
* plugin class.
*
* @return string
*/
public function getReleaseFeedUrl()
{
return 'https://raw.githubusercontent.com/webdna/boris/master/releases.json';
}
/**
* Returns the version number.
*
* @return string
*/
public function getVersion()
{
return '1.1.2';
}
/**
* As of Craft 2.5, Craft no longer takes the whole site down every time a plugin’s version number changes, in
* case there are any new migrations that need to be run. Instead plugins must explicitly tell Craft that they
* have new migrations by returning a new (higher) schema version number with a getSchemaVersion() method on
* their primary plugin class:
*
* @return string
*/
public function getSchemaVersion()
{
return '1.0.0';
}
/**
* Returns the developer’s name.
*
* @return string
*/
public function getDeveloper()
{
return 'Nathaniel Hammond - @nfourtythree - webdna';
}
/**
* Returns the developer’s website URL.
*
* @return string
*/
public function getDeveloperUrl()
{
return 'https://webdna.co.uk';
}
/**
* Returns whether the plugin should get its own tab in the CP header.
*
* @return bool
*/
public function hasCpSection()
{
return false;
}
/**
* Called right before your plugin’s row gets stored in the plugins database table, and tables have been created
* for it based on its records.
*/
public function onBeforeInstall()
{
}
/**
* Called right after your plugin’s row has been stored in the plugins database table, and tables have been
* created for it based on its records.
*/
public function onAfterInstall()
{
}
/**
* Called right before your plugin’s record-based tables have been deleted, and its row in the plugins table
* has been deleted.
*/
public function onBeforeUninstall()
{
}
/**
* Called right after your plugin’s record-based tables have been deleted, and its row in the plugins table
* has been deleted.
*/
public function onAfterUninstall()
{
}
/**
* Defines the attributes that model your plugin’s available settings.
*
* @return array
*/
protected function defineSettings()
{
return array(
'name' => array( AttributeType::String, 'label' => 'Plugin Name', 'default' => 'Boris' ),
'entryIds' => array( AttributeType::Mixed, 'label' => 'Invincible Entries', 'default' => serialize( array() ) ),
'categoryIds' => array( AttributeType::Mixed, 'label' => 'Invincible Categories', 'default' => serialize( array() ) ),
);
}
/**
* Returns the HTML that displays your plugin’s settings.
*
* @return mixed
*/
public function getSettingsHtml()
{
return craft()->templates->render( 'boris/settings', craft()->boris->getTemplateVars( $this->getSettings() ) );
}
/**
* If you need to do any processing on your settings’ post data before they’re saved to the database, you can
* do it with the prepSettings() method:
*
* @param mixed $settings The Widget's settings
*
* @return mixed
*/
public function prepSettings( $settings )
{
return craft()->boris->prepSettings( $settings );
}
}