-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMBVFaq.module.php
More file actions
470 lines (425 loc) · 11.8 KB
/
Copy pathMBVFaq.module.php
File metadata and controls
470 lines (425 loc) · 11.8 KB
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
<?php
#------------------------------------------------------------------------
# Module: MBVFaq - a FAQ handling module for CMS Made Simple
# Mostly copyright (C) 2011-2016 Tom Phane <@>
# Derived from beta release, copyright (C) 2005 Martin B. Vestergaard (mbvdk) <mbv@nospam.dk>
# This project's forge-page is: http://dev.cmsmadesimple.org/projects/faqsimple
#
# This module is free software; you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option)
# any later version.
#
# This module is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License (www.gnu.org/licenses/licenses.html#AGPL)
# for more details
#-----------------------------------------------------------------------
class MBVFaq extends CMSModule
{
public $dbHandle;
public $CatTable;
public $ItemTable;
public $UserTable;
public $before111;
public $before20;
public $oldtemplates;
protected $PermAdminName = 'MBVFaq Admin';
protected $PermAddName = 'MBVFaq Add';
protected $PermModName = 'MBVFaq Modify';
protected $PermDelName = 'MBVFaq Delete';
protected $PermSeeName = 'MBVFaq View';
public function __construct()
{
parent::__construct();
$this->RegisterModulePlugin();
$this->dbHandle = cmsms()->GetDb();
$pre = cms_db_prefix();
$this->CatTable = $pre.'module_MBVFaq_category';
$this->ItemTable = $pre.'module_MBVFaq_question';
$this->UserTable = $pre.'users';
global $CMS_VERSION;
$this->before111 = (version_compare($CMS_VERSION, '1.11') < 0);
$this->before20 = (version_compare($CMS_VERSION, '2.0') < 0);
$this->oldtemplates = $this->before20 || 1; //TODO
}
public function AllowAutoInstall()
{
return FALSE;
}
public function AllowAutoUpgrade()
{
return FALSE;
}
/*
For 1.11+
*/
public function AllowSmartyCaching()
{
return TRUE;
}
public function GetName()
{
return 'MBVFaq';
}
public function GetFriendlyName()
{
return $this->Lang('friendlyname');
}
public function GetHelp()
{
return $this->Lang('help');
}
public function GetVersion()
{
return '1.2.1';
}
public function GetAuthor()
{
return 'tomphantoo';
}
public function GetAuthorEmail()
{
return 'tpgww@onepost.net';
}
public function GetChangeLog()
{
$fn = cms_join_path(dirname(__FILE__), 'lib','doc', 'changelog.htm');
return @file_get_contents($fn);
}
public function IsPluginModule()
{
return TRUE;
}
public function HasCapability($capability, $params = array())
{
switch ($capability) {
case CmsCoreCapabilities::PLUGIN_MODULE:
return TRUE;
}
return FALSE;
}
public function HasAdmin()
{
return TRUE;
}
/*
For 1.10+
*/
public function LazyLoadAdmin()
{
return TRUE;
}
public function GetAdminSection()
{
return 'content';
}
public function GetAdminDescription()
{
return $this->Lang('moddescription');
}
public function VisibleToAdminUser()
{
return $this->_CheckAccess();
}
/* function AdminStyle()
{
}
*/
public function GetHeaderHTML()
{
return '<link rel="stylesheet" type="text/css" id="adminstyler" href="'.$this->GetModuleURLPath().'/css/admin.css" />';
}
public function SuppressAdminOutput(&$request)
{
//prevent output of general admin content when doing an export,
//and when updating the database via an ajax call
if (isset($request['mact'])) {
if (strpos($request['mact'], 'moveitem', 6)) {
return TRUE;
}
if (strpos($request['mact'], 'movecategory', 6)) {
return TRUE;
}
if (strpos($request['mact'], 'export', 6)) {
return TRUE;
}
}
if (isset($request['m1_export'])) {
return TRUE;
}
return FALSE;
}
public function GetDependencies()
{
return array();
}
/*
For 1.10+
*/
public function LazyLoadFrontend()
{
return FALSE; //needed to support route-registration
}
public function MinimumCMSVersion()
{
return '1.9';
}
/*public function MaximumCMSVersion()
{
return '1.12.99';
}
*/
public function InstallPostMessage()
{
return $this->Lang('postinstall');
}
public function UninstallPreMessage()
{
return $this->Lang('really_uninstall');
}
public function UninstallPostMessage()
{
return $this->Lang('postuninstall');
}
/*
For pre-1.10
*/
public function SetParameters()
{
$this->InitializeAdmin();
$this->InitializeFrontend();
}
/*
Partial setup for 1.10
*/
public function InitializeFrontend()
{
$this->RestrictUnknownParams();
$this->SetParameterType('cat', CLEAN_STRING);
$this->SetParameterType('category', CLEAN_STRING);
$this->SetParameterType('faq', CLEAN_INT);
$this->SetParameterType('faq_id', CLEAN_INT);
$this->SetParameterType('pattern', CLEAN_STRING);
$this->SetParameterType('regex', CLEAN_STRING);
/* register 'routes' to use for pretty url parsing
these regexes are for site-root-url-relative 'paths', they translate
url-element(s) to $param[](s) be supplied to the specified actions
(default calls ->DisplayModuleOutput()) so the routes need to conform
to parameter-usage in handler-func(s).
(?P<name>regex) captures the text matched by "regex" into the group "name",
which can contain letters and numbers but must start with a letter.
*/
//NB the correct page id is needed in the URL to display generated content
//on the correct page! TODO find a dynamic way around this, or at worst
//a static preference e.g. $onpage = $this->GetPreference('site_page',-1);
//and include 'returnid'=>$onpage in the parameters arrays
// for showing the contents of a specific category
$this->RegisterRoute('/[mM][bB][vV][fF]aq\/cat(egory)?(?P<cat>.*?)\/(?P<returnid>[0-9]+)$/', array('action'=>'default'));
// for showing all the details for a specific question
$this->RegisterRoute('/[mM][bB][vV][fF]aq\/faq(_id)?(?P<faq>[0-9]+)\/(?P<returnid>[0-9]+)$/', array('action'=>'default'));
// for doing nothing i.e. ignored links
$this->RegisterRoute('/[mM][bB][vV][fF]aq\/(?P<returnid>[0-9]+)$/', array('action'=>'default'));
}
/*
Partial setup for 1.10+
*/
public function InitializeAdmin()
{
$this->CreateParameter('cat', '', $this->Lang('help_cat'));
$this->CreateParameter('category', '', $this->Lang('help_category'));
$this->CreateParameter('faq', '', $this->Lang('help_faq'));
$this->CreateParameter('faq_id', '', $this->Lang('help_faq_id'));
$this->CreateParameter('pattern', '', $this->Lang('help_pattern'));
$this->CreateParameter('regex', '', $this->Lang('help_regex'));
}
/*
DoAction:
No permission-checks are done here or in related action files, as capabilities
are governed by which actionable widgets are displayed
- and those are permission-checked before creation
*/
public function DoAction($action, $id, $params, $returnid=-1)
{
switch ($action) {
case 'default':
case 'defaultadmin':
case 'movecategory': //process reorder by DnD
case 'swapcats':
case 'processcats': //update, delete, sort, export cats
case 'openitem': //initiate an edit or add
case 'updateitem': //submit item changes
case 'moveitem': //process reorder by DnD
case 'swapitems':
case 'processitems': //export, sort, activate, delete Q's
case 'setprefs':
break;
case 'deleteitem':
if (isset($params['item_id']) && ($params['item_id'] > -1)) {
$this->_DeleteItem($params['item_id']);
} //trivial, several uses, don't bother with separate action file
$action = 'defaultadmin';
$params = array();
break;
case 'toggleitem': //[de]activate
$this->_ActivateItem($id, $params, $returnid); //trivial func, don't bother with separate action file
$action = 'defaultadmin';
$params = array();
break;
case 'addcategory':
$action = 'defaultadmin';
$params = array('showtab' => 1,'extracat' => TRUE);
break;
case 'deletecategory':
if (isset($params['category_id']) && ($params['category_id'] > 0)) {
$this->_DeleteCategory($params['category_id']);
} //several uses, don't bother with separate action file
$action = 'defaultadmin';
$params = array('showtab' => 1);
break;
default:
return;
}
parent::DoAction($action, $id, $params, $returnid);
}
/**
_CheckAccess:
@permission: string specifying what to check, default=''
@warn: whether to show a warning message if permission not valid, default=FALSE
NOT PART OF THE MODULE API
*/
public function _CheckAccess($permission='', $warn=FALSE)
{
switch ($permission) {
case '': //anything relevant
$name = '';
$ok = $this->CheckPermission($this->PermSeeName);
if (!$ok) {
$ok = $this->CheckPermission($this->PermAddName);
}
if (!$ok) {
$ok = $this->CheckPermission($this->PermDelName);
}
if (!$ok) {
$ok = $this->CheckPermission($this->PermModName);
}
if (!$ok) {
$ok = $this->CheckPermission($this->PermAdminName);
}
break;
case 'admin':
$name = $this->PermAdminName;
$ok = $this->CheckPermission($name);
break;
case 'add':
$name = $this->PermAddName;
$ok = $this->CheckPermission($name);
break;
case 'modify':
$name = $this->PermModName;
$ok = $this->CheckPermission($name);
break;
case 'delete':
$name = $this->PermDelName;
$ok = $this->CheckPermission($name);
break;
default:
$name = '';
$ok = FALSE;
}
if (!$ok && $warn) {
if ($name == '') {
$name = $this->Lang('perm_some');
}
echo '<p class="error">'.$this->Lang('accessdenied2', $name).'</p>';
}
return $ok;
}
/**
_DeleteCategory:
@category_id:
Delete a category, either with all its questions, or after setting all its
questions to category 0
Confirmation upstream, not here
*/
public function _DeleteCategory($category_id)
{
if ($category_id > 0) { //no deleting the default category
$db = $this->dbHandle;
$all = $this->GetPreference('clear_category', FALSE);
if ($all) { // first delete the contents
$sql = "DELETE FROM $this->ItemTable WHERE category_id=?";
} else { // first set the category_id of the affected contents to 0
$sql = "UPDATE $this->ItemTable SET category_id=0 WHERE category_id=?";
}
$db->Execute($sql, array($category_id));
$sql = "DELETE FROM $this->CatTable WHERE category_id=?";
$db->Execute($sql, array($category_id));
}
}
/**
_ActivateItem:
@id:
@params:
@returnid:
[de]activate the item passed in @params
*/
public function _ActivateItem($id, &$params, $returnid)
{
if (isset($params['item_id'])) {
$querydata = array();
if (isset($params['active'])) {
if ($params['active']) {
$querydata[] = 0;
} else {
$querydata[] = 1;
}
} else {
$querydata[] = 0;
}
$querydata[] = $params['item_id'];
$sql = "UPDATE $this->ItemTable SET active=? WHERE item_id=?";
$this->dbHandle->Execute($sql, $querydata);
}
}
/**
_DeleteItem:
@$item_id:
Delete from the db the question passed in @item_id
*/
public function _DeleteItem($item_id)
{
$sql = "DELETE FROM $this->ItemTable WHERE item_id=?";
$this->dbHandle->Execute($sql, array($item_id));
}
/**
ellipsize:
@str: string to ellipsize
@max_length: max length of @str
@position: int (1|0) or float, .5, .2, etc for position to split Default 1
@ellipsis: string for ellipsis Default '...'
Split @str at its max_length and ellipsize
Returns: ellipsized string
*/
public function ellipsize($str, $max_length, $position = 1, $ellipsis = '…')
{
$str = trim($str);
// Is the string long enough to ellipsize?
if (strlen($str) <= $max_length) {
return $str;
}
if ($position > 1) {
$position = 1;
} elseif ($position < 0) {
$position = 0;
}
$beg = substr($str, 0, floor($max_length * $position));
if ($position === 1) {
$end = substr($str, 0, -($max_length - strlen($beg)));
} else {
$end = substr($str, -($max_length - strlen($beg)));
}
return $beg.$ellipsis.$end;
}
}