forked from snytkine/LampCMS
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Retag.php
329 lines (267 loc) · 8.81 KB
/
Retag.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
/**
*
* License, TERMS and CONDITIONS
*
* This software is lisensed under the GNU LESSER GENERAL PUBLIC LICENSE (LGPL) version 3
* Please read the license here : http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* ATTRIBUTION REQUIRED
* 4. All web pages generated by the use of this software, or at least
* the page that lists the recent questions (usually home page) must include
* a link to the http://www.lampcms.com and text of the link must indicate that
* the website\'s Questions/Answers functionality is powered by lampcms.com
* An example of acceptable link would be "Powered by <a href="http://www.lampcms.com">LampCMS</a>"
* The location of the link is not important, it can be in the footer of the page
* but it must not be hidden by style attibutes
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This product includes GeoLite data created by MaxMind,
* available from http://www.maxmind.com/
*
*
* @author Dmitri Snytkine <cms@lampcms.com>
* @copyright 2005-2011 (or current year) ExamNotes.net inc.
* @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU LESSER GENERAL PUBLIC LICENSE (LGPL) version 3
* @link http://www.lampcms.com Lampcms.com project
* @version Release: @package_version@
*
*
*/
namespace Lampcms\Controllers;
use \Lampcms\Utf8String;
use \Lampcms\WebPage;
use \Lampcms\TagsTokenizer;
use \Lampcms\Request;
use \Lampcms\Responder;
/**
* Controller responsible
* for processing retag form
*
* @author Dmitri Snytkine
*
*/
class Retag extends WebPage
{
protected $membersOnly = true;
protected $requireToken = true;
protected $aRequired = array('qid', 'tags');
/**
* Question being deleted
*
* @var object of type Question
*/
protected $oQuestion;
/**
* Array of old value of a_tags
*
* @var array
*/
protected $aOldTags = array();
/**
* Array of tags that were added to
* Question as result of this retag
* Tags are not always added - a retag may
* be just a removal of some tags
* If any new tags were added this array will
* include new tags
*
* @var tags
*/
protected $aAddedTags = array();
/**
* Array of submitted tags
* after they are run through TagsTokenizer
*
* @var array
*/
protected $aSubmitted;
protected function main(){
$this->aSubmitted = TagsTokenizer::factory($this->oRequest->getUTF8('tags'))->getArrayCopy();
d('$this->aSubmitted: '.print_r($this->aSubmitted, 1));
$this->validateSubmitted()
->getQuestion()
->checkPermission()
->checkForChanges()
->removeOldTags()
->updateQuestion()
->addNewTags()
->postEvent()
->returnResult();
}
/**
* Validate to make sure
* submitted form contains between 1 and 5 tags
*
* @throws \Lampcms\Exception if no tags or more than 5 tags
*
* @return object $this
*/
protected function validateSubmitted(){
$min = $this->oRegistry->Ini->MIN_QUESTION_TAGS;
$max = $this->oRegistry->Ini->MAX_QUESTION_TAGS;
if(($min > 0) && empty($this->aSubmitted)){
/*
* @todo translate string
*/
throw new \Lampcms\Exception('No valid tags have been submitted. Please use words that best categorize this question');
}
$count = count($this->aSubmitted);
if($count < $min){
/**
* @todo Translate string
*/
throw new \Lampcms\Exception('Question must have at least '.$min.' tag(s)');
}
if($count > $max){
/**
* @todo translate string
*/
throw new \Lampcms\Exception('Question cannot have more than '.$max.' tags. Please remove some tags');
}
return $this;
}
/**
* Create $this->oQuestion object
*
* @throws \Lampcms\Exception if question
* not found or is marked as deleted
*
* @return object $this
*/
protected function getQuestion(){
$a = $this->oRegistry->Mongo->QUESTIONS->findOne(array('_id' => (int)$this->oRequest['qid']));
d('a: '.print_r($a, 1));
if(empty($a) || !empty($a['i_del_ts'])){
throw new \Lampcms\Exception('Question not found');
}
$this->oQuestion = new \Lampcms\Question($this->oRegistry, $a);
$this->aOldTags = $this->oQuestion['a_tags'];
return $this;
}
/**
* Check to make sure Viewer has permission
* to retag.
* Permitted to retag are: owner of question,
* moderator/admin or user with reputation
*
* @return object $this
*
*/
protected function checkPermission(){
if(!\Lampcms\isOwner($this->oRegistry->Viewer, $this->oQuestion)
&& ($this->oRegistry->Viewer->getReputation() < \Lampcms\Points::RETAG)){
$this->checkAccessPermission('retag');
}
return $this;
}
/**
* Make sure that new tags are
* different from tags that already
* in the question
*
* @throws \Lampcms\Exception in case tags
* has not changed
*
* @return object $this
*/
protected function checkForChanges(){
$this->aAddedTags = \array_diff($this->aSubmitted, $this->aOldTags);
$diff2 = \array_diff($this->aOldTags, $this->aSubmitted);
d('diff: '.print_r($this->aAddedTags, 1));
d('diff2: '.print_r($diff2, 1));
if(empty($this->aAddedTags) && empty($diff2)){
throw new \Lampcms\Exception('You have not changed any tags');
}
return $this;
}
/**
* Update USER_TAGS and QUESTION_TAGS and RELATED_TAGS collections
* to remove old tags that belong to this questions
*
* @return object $this
*/
protected function removeOldTags(){
\Lampcms\Qtagscounter::factory($this->oRegistry)->removeTags($this->oQuestion);
\Lampcms\UserTags::factory($this->oRegistry)->removeTags($this->oQuestion);
\Lampcms\Relatedtags::factory($this->oRegistry)->removeTags($this->oQuestion);
/**
* Also update UNANSWERED_TAGS if this question
* is unanswered
*/
if(0 === $this->oQuestion['i_sel_ans']){
d('going to remove from Unanswered tags');
\Lampcms\UnansweredTags::factory($this->oRegistry)->remove($this->oQuestion);
}
return $this;
}
/**
* Update USER_TAGS and QUESTION_TAGS collections
* to add new tags that belong to this questions
*
* @return object $this
*/
protected function addNewTags(){
if(count($this->aSubmitted) > 0){
\Lampcms\Qtagscounter::factory($this->oRegistry)->parse($this->oQuestion);
\Lampcms\UserTags::factory($this->oRegistry)->addTags($this->oQuestion['i_uid'], $this->oQuestion);
\Lampcms\Relatedtags::factory($this->oRegistry)->addTags($this->oQuestion);
if(0 === $this->oQuestion['i_sel_ans']){
d('going to add to Unanswered tags');
\Lampcms\UnansweredTags::factory($this->oRegistry)->set($this->oQuestion);
}
}
return $this;
}
/**
* Update question object with
* new values related to tags
*
* @return object $this
*/
protected function updateQuestion(){
$this->oQuestion->retag($this->oRegistry->Viewer, $this->aSubmitted)
->save();
return $this;
}
/**
* Post onRetag event
*
* @return object $this
*/
protected function postEvent(){
$this->oRegistry->Dispatcher->post($this->oQuestion, 'onRetag', $this->aAddedTags);
return $this;
}
protected function returnResult(){
/**
* @todo translate string
*/
$message = 'Question retagged successfully';
if(Request::isAjax()){
$ret = array('reload' => 100); //'alert' => $message,
Responder::sendJSON($ret);
}
Responder::redirectToPage($this->oQuestion->getUrl());
}
}