| 1 |
mjordaan |
1 |
<?php namespace ProcessWire;
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* ProcessWire InputfieldCommentsAdmin
|
|
|
5 |
*
|
|
|
6 |
* An Inputfield for handling administration of Comments.
|
|
|
7 |
*
|
|
|
8 |
* ProcessWire 3.x, Copyright 2019 by Ryan Cramer
|
|
|
9 |
* https://processwire.com
|
|
|
10 |
*
|
|
|
11 |
* @method InputfieldMarkup renderItem(Comment $comment, $n)
|
|
|
12 |
*
|
|
|
13 |
*/
|
|
|
14 |
|
|
|
15 |
class InputfieldCommentsAdmin extends Inputfield implements InputfieldItemList {
|
|
|
16 |
|
|
|
17 |
public static function getModuleInfo() {
|
|
|
18 |
return array(
|
|
|
19 |
'title' => __('Comments Admin', __FILE__),
|
|
|
20 |
'version' => 104,
|
|
|
21 |
'summary' => __('Provides an administrative interface for working with comments', __FILE__),
|
|
|
22 |
'permanent' => false,
|
|
|
23 |
'requires' => 'FieldtypeComments',
|
|
|
24 |
);
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
protected $commentIDsToNumbers = array();
|
|
|
28 |
|
|
|
29 |
public function init() {
|
|
|
30 |
parent::init();
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* @param Comment $comment
|
|
|
35 |
* @param int $n
|
|
|
36 |
* @return array|mixed|null|_Module|Field|Fieldtype|Module|NullPage|Page|Permission|Role|Template|WireData|WireInputData|string
|
|
|
37 |
*
|
|
|
38 |
*/
|
|
|
39 |
protected function ___renderItem(Comment $comment, $n) {
|
|
|
40 |
|
|
|
41 |
$statuses = array(
|
|
|
42 |
Comment::statusFeatured => array($this->_x('Featured', 'comment-status'), 'trophy'),
|
|
|
43 |
Comment::statusApproved => array($this->_x('Approved', 'comment-status'), 'check-circle'),
|
|
|
44 |
Comment::statusPending => array($this->_x('Pending', 'comment-status'), 'question-circle'),
|
|
|
45 |
Comment::statusSpam => array($this->_x('Spam', 'comment-status'), 'warning'),
|
|
|
46 |
);
|
|
|
47 |
|
|
|
48 |
$name = $this->name;
|
|
|
49 |
$names = array(
|
|
|
50 |
'status' => "{$this->name}_status_{$comment->id}",
|
|
|
51 |
'parent' => "{$this->name}_parent_id_{$comment->id}",
|
|
|
52 |
'cite' => "{$name}_cite_{$comment->id}",
|
|
|
53 |
'email' => "{$name}_email_{$comment->id}",
|
|
|
54 |
'website' => "{$name}_website_{$comment->id}",
|
|
|
55 |
'stars' => "{$name}_stars_{$comment->id}",
|
|
|
56 |
'text' => "{$name}_text_{$comment->id}"
|
|
|
57 |
);
|
|
|
58 |
|
|
|
59 |
|
|
|
60 |
$sanitizer = $this->wire('sanitizer');
|
|
|
61 |
$statusName = '';
|
|
|
62 |
$statusIcon = '';
|
|
|
63 |
$statusOut = "<select id='$names[status]' name='$names[status]'>";
|
|
|
64 |
|
|
|
65 |
foreach($statuses as $status => $statusInfo) {
|
|
|
66 |
list($label, $icon) = $statusInfo;
|
|
|
67 |
if($comment->status == $status) {
|
|
|
68 |
$selected = " selected='selected'";
|
|
|
69 |
$statusName = $label;
|
|
|
70 |
$statusIcon = $icon;
|
|
|
71 |
} else {
|
|
|
72 |
$selected = '';
|
|
|
73 |
}
|
|
|
74 |
$statusOut .= "<option value='$status'$selected>$label</option>";
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
$statusOut .= "<option value='delete{$comment->id}'>" . $this->_('Delete') . "</option>";
|
|
|
78 |
$statusOut .= "</select>";
|
|
|
79 |
|
|
|
80 |
$parentOut = "<select name='$names[parent]' id='$names[parent]'><option value='0'></option>";
|
|
|
81 |
$parentID = $comment->parent_id;
|
|
|
82 |
$_n = 0;
|
|
|
83 |
foreach($this->value as $_comment) {
|
|
|
84 |
$_n++;
|
|
|
85 |
if($_comment->id == $comment->id) continue;
|
|
|
86 |
if($_comment->created > $comment->created) continue;
|
|
|
87 |
$selected = $parentID == $_comment->id ? " selected='selected'" : "";
|
|
|
88 |
$parentOut .=
|
|
|
89 |
"<option$selected value='$_comment->id'>" .
|
|
|
90 |
sprintf($this->_('Comment #%d (%s)'), $_n, $this->wire('sanitizer')->entities($_comment->cite)) .
|
|
|
91 |
"</option>";
|
|
|
92 |
}
|
|
|
93 |
$parentOut .= "</select>";
|
|
|
94 |
|
|
|
95 |
$notifyOut = '';
|
|
|
96 |
if($comment->getField()->useNotify) {
|
|
|
97 |
if($comment->flags & Comment::flagNotifyAll) $notifyOut = $this->_('ALL new comments');
|
|
|
98 |
else if($comment->flags & Comment::flagNotifyReply) $notifyOut = $this->_('REPLIES only');
|
|
|
99 |
else $notifyOut = $this->_('OFF');
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
$headLabel = $statusName;
|
|
|
103 |
$num = $n+1;
|
|
|
104 |
|
|
|
105 |
$liID = "CommentsAdminItem{$comment->id}";
|
|
|
106 |
/** @var InputfieldMarkup $f */
|
|
|
107 |
$f = $this->wire('modules')->get('InputfieldMarkup');
|
|
|
108 |
$f->attr('id', $liID);
|
|
|
109 |
$f->addClass("CommentsAdminItem$statusName");
|
|
|
110 |
$f->icon = $statusIcon;
|
|
|
111 |
|
|
|
112 |
if($comment->status == Comment::statusSpam) {
|
|
|
113 |
$f->addClass("CommentsAdminItemSpam InputfieldIsError InputfieldStateError", 'wrapClass');
|
|
|
114 |
$f->description = $this->_("Spam is automatically deleted after the amount of time specified in the field configuration.");
|
|
|
115 |
$f->collapsed = Inputfield::collapsedYes;
|
|
|
116 |
} else if($comment->status == Comment::statusApproved) {
|
|
|
117 |
$f->collapsed = Inputfield::collapsedYes;
|
|
|
118 |
$f->addClass('InputfieldIsSuccess');
|
|
|
119 |
} else if($comment->status == Comment::statusPending) {
|
|
|
120 |
$f->addClass('InputfieldIsPrimary');
|
|
|
121 |
$f->description = $this->_("This item is awaiting approval or deletion.");
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
$cite = $sanitizer->entities($comment->cite);
|
|
|
125 |
$email = $sanitizer->entities($comment->email);
|
|
|
126 |
$website = $sanitizer->entities($comment->website);
|
|
|
127 |
$stars = $sanitizer->entities($comment->stars);
|
|
|
128 |
$text = $sanitizer->entities(trim($comment->text));
|
|
|
129 |
|
|
|
130 |
$f->label = strtoupper($headLabel) . ': ' . sprintf(
|
|
|
131 |
$this->_('Comment #%1$d Posted %2$s by %3$s'),
|
|
|
132 |
$num,
|
|
|
133 |
wireRelativeTimeStr($comment->created),
|
|
|
134 |
$cite
|
|
|
135 |
);
|
|
|
136 |
|
|
|
137 |
if($this->hasField && $this->hasField->get('useStars')) {
|
|
|
138 |
$starsInput =
|
|
|
139 |
"<p class='CommentsAdminItemStars'>" .
|
|
|
140 |
"<label for='$names[stars]'>" .
|
|
|
141 |
"<span class='detail'>" . $this->_('Stars') . "</span>" .
|
|
|
142 |
"</label>" .
|
|
|
143 |
"<input type='number' min='0' max='5' id='$names[stars]' name='$names[stars]' value='$stars' />" .
|
|
|
144 |
"</p>";
|
|
|
145 |
} else {
|
|
|
146 |
$starsInput = '';
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
$f->value =
|
|
|
150 |
"<div class='ui-helper-clearfix'>" .
|
|
|
151 |
"<p class='CommentsAdminItemStatus'>" .
|
|
|
152 |
"<label for='$names[status]'><span class='detail'>" . $this->_('Status') . "</span></label>" .
|
|
|
153 |
$statusOut .
|
|
|
154 |
"</p>" .
|
|
|
155 |
"<p class='CommentsAdminItemParent'>" .
|
|
|
156 |
"<label for='$names[parent]'><span class='detail'>" . $this->_('Reply To') . "</span></label>" .
|
|
|
157 |
$parentOut .
|
|
|
158 |
"</p>" .
|
|
|
159 |
"</div><div class='ui-helper-clearfix'>" .
|
|
|
160 |
"<p class='CommentsAdminItemCite'>" .
|
|
|
161 |
"<label for='$names[cite]'><span class='detail'>" . $this->_('Cite') . "</span></label>" .
|
|
|
162 |
"<input type='text' id='$names[cite]' name='$names[cite]' value='$cite' />" .
|
|
|
163 |
"</p>" .
|
|
|
164 |
"<p class='CommentsAdminItemEmail'>" .
|
|
|
165 |
"<label for='$names[email]'><span class='detail'>" . $this->_('E-Mail') . "</span></label>" .
|
|
|
166 |
"<input type='text' id='$names[email]' name='$names[email]' value='$email' />" .
|
|
|
167 |
"</p>" .
|
|
|
168 |
"<p class='CommentsAdminItemWebsite'>" .
|
|
|
169 |
"<label for='$names[website]'><span class='detail'>" . $this->_('Website') . "</span></label>" .
|
|
|
170 |
"<input type='text' id='$names[website]' name='$names[website]' value='$website' />" .
|
|
|
171 |
"</p>" .
|
|
|
172 |
$starsInput .
|
|
|
173 |
"</div>" .
|
|
|
174 |
"<p class='CommentsAdminItemText'>" .
|
|
|
175 |
"<label for='$names[text]'><span class='detail'>" . $this->_('Text') . "</span></label>" .
|
|
|
176 |
"<textarea id='$names[text]' name='$names[text]' rows='5'>$text</textarea>" .
|
|
|
177 |
"</p>" .
|
|
|
178 |
($notifyOut ? "<p class='CommentsAdminItemNotify notes'>" . $this->_('Email notifications:') . " $notifyOut</p>" : "") .
|
|
|
179 |
"<input class='CommentsAdminItemSort' type='hidden' name='sort_{$this->name}_{$comment->id}' value='$n' />" .
|
|
|
180 |
"";
|
|
|
181 |
|
|
|
182 |
return $f;
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
public function ___render() {
|
|
|
186 |
|
|
|
187 |
if(!count($this->value)) return "<p>" . $this->_('There are currently no items to display.') . "</p>";
|
|
|
188 |
|
|
|
189 |
$n = 0;
|
|
|
190 |
foreach($this->value as $comment) {
|
|
|
191 |
$this->commentIDsToNumbers[$comment->id] = ++$n;
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
$fieldset = new InputfieldWrapper(); // wired
|
|
|
195 |
$this->wire($fieldset);
|
|
|
196 |
|
|
|
197 |
$n = 0;
|
|
|
198 |
foreach($this->value as $comment) {
|
|
|
199 |
$f = $this->renderItem($comment, $n++);
|
|
|
200 |
$f->addClass($this->wrapClass);
|
|
|
201 |
$fieldset->add($f);
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
$out = $fieldset->render();
|
|
|
205 |
//$this->addClass('InputfieldFieldset', 'wrapClass');
|
|
|
206 |
|
|
|
207 |
return $out;
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
public function ___processInput(WireInputData $input) {
|
|
|
211 |
|
|
|
212 |
$n = 1;
|
|
|
213 |
$names = array(
|
|
|
214 |
'cite',
|
|
|
215 |
'email',
|
|
|
216 |
'website',
|
|
|
217 |
'status',
|
|
|
218 |
'delete',
|
|
|
219 |
'parent_id',
|
|
|
220 |
'text',
|
|
|
221 |
'sort',
|
|
|
222 |
);
|
|
|
223 |
|
|
|
224 |
if($this->hasField && $this->hasField->get('useStars')) $names[] = 'stars';
|
|
|
225 |
|
|
|
226 |
foreach($this->value as $comment) {
|
|
|
227 |
/** @var Comment $comment */
|
|
|
228 |
|
|
|
229 |
$data = array();
|
|
|
230 |
foreach($names as $name) {
|
|
|
231 |
$inputName = $this->name . "_" . $name . "_" . $comment->id;
|
|
|
232 |
$value = isset($input[$inputName]) ? $input[$inputName] : '';
|
|
|
233 |
$data[$name] = $value;
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
if($data['status'] && $data['status'] == "delete{$comment->id}") {
|
|
|
237 |
$this->value->remove($comment);
|
|
|
238 |
$this->message(sprintf($this->_('Removed comment #%d'), $n));
|
|
|
239 |
$this->value->trackChange('remove');
|
|
|
240 |
continue;
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
foreach($data as $key => $value) {
|
|
|
244 |
if($key == 'text') $value = $comment->cleanCommentString($value);
|
|
|
245 |
if(($value || $key == 'status') && $comment->$key != $value) {
|
|
|
246 |
$comment->$key = $value;
|
|
|
247 |
$this->message(sprintf($this->_('Updated %s for comment #%d'), $key, $n));
|
|
|
248 |
$this->value->trackChange('update');
|
|
|
249 |
}
|
|
|
250 |
}
|
|
|
251 |
$n++;
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
return $this;
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
}
|