-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEntryeditorsPlugin.php
124 lines (103 loc) · 3.04 KB
/
EntryeditorsPlugin.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
<?php
namespace Craft;
class EntryeditorsPlugin extends BasePlugin
{
public function getName()
{
return Craft::t('Entry Editors');
}
public function getVersion()
{
return '1.0';
}
public function getDeveloper()
{
return 'Yuri Salimovskiy';
}
public function getDeveloperUrl()
{
return 'http://www.intoeetive.com';
}
public function hasCpSection()
{
return false;
}
protected function defineSettings()
{
return array(
'field' => AttributeType::Mixed
);
}
public function getSettingsHtml()
{
$options = [];
foreach (craft()->fields->getAllFields('id') as $id=>$field)
{
if ($field->type=='Users')
{
$options[$id] = $field->name;
}
}
$input = craft()->templates->render('_includes/forms/select', array(
'id' => 'field',
'name' => 'field',
'options' => $options,
'value' => $this->getSettings()['field']
));
return craft()->templates->render('_includes/forms/field', array(
'label' => Craft::t('Select Editors Field'),
'instructions'=> Craft::t('Field that lists everyone that can edit entry (besides author and admins)'),
'input' => $input
));
}
public function init()
{
craft()->on('entries.onBeforeSaveEntry', function(Event $event)
{
$settings = $this->getSettings();
//is setting set?
if (!isset($settings->field) || empty($settings->field))
{
return true;
}
//skip check for new entries
if ($event->params['isNewEntry']===true)
{
return true;
}
$user = craft()->userSession->user;
//skip check for admin
if (isset($user->admin) && $user->admin==true )
{
return true;
}
$entry = $event->params['entry'];
//skip check for author
if ($user->id == $entry->author->id)
{
return true;
}
//does the field relate to entry users?
$criteria = craft()->elements->getCriteria(ElementType::User, ['relatedTo' => [
'element' => $entry,
'fieldId' => $settings->field
]]);
$editors = $criteria->find();
if (empty($editors))
{
return true;
}
//is logged in user in the list?
foreach ($editors as $editor)
{
if ($user->id == $editor->id)
{
return true;
}
}
//none of check passed, deny
$event->performAction = false;
throw new Exception(Craft::t('You are not allowed to edit this entry'));
});
}
}