1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
You may not change or alter any portion of this comment or credits of |
4
|
|
|
supporting developers from this source code or any supporting source code |
5
|
|
|
which is considered copyrighted (c) material of the original comment or credit |
6
|
|
|
authors. |
7
|
|
|
|
8
|
|
|
This program is distributed in the hope that it will be useful, but |
9
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of |
10
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
11
|
|
|
*/ |
12
|
|
|
/** |
13
|
|
|
* Display/Edit Random Term Block file |
14
|
|
|
* |
15
|
|
|
* @package module\xoopsfaq\blocks |
16
|
|
|
* @author hsalazar |
17
|
|
|
* @author ZySpec |
18
|
|
|
* @author XOOPS Module Development Team |
19
|
|
|
* @copyright Copyright (c) 2001-2017 {@link http://xoops.org XOOPS Project} |
20
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU Public License |
21
|
|
|
* @since:: 1.23 |
22
|
|
|
* |
23
|
|
|
* @see Xmf\Module\Helper |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Display Random FAQ Block |
28
|
|
|
* |
29
|
|
|
* @param array $options |
30
|
|
|
* [0] - number of chars in title to show (0 = unlimited) |
31
|
|
|
* [1] - comma separated list of categories to use/select from |
32
|
|
|
* @return array contains random FAQ parameters |
33
|
|
|
*/ |
34
|
|
|
function b_xoopsfaq_random_show($options) |
35
|
|
|
{ |
36
|
|
|
$moduleDirName = basename(dirname(__DIR__)); |
37
|
|
|
xoops_load('constants', $moduleDirName); |
38
|
|
|
|
39
|
|
|
$myts = MyTextSanitizer::getInstance(); |
40
|
|
|
|
41
|
|
|
/** @var XoopsfaqCategoryHandler $xfCatHandler */ |
42
|
|
|
/** @var XoopsfaqContentsHandler $xfFaqHandler */ |
43
|
|
|
/** @var Xmf\Module\Helper\GenericHelper $xfHelper */ |
44
|
|
|
$xfHelper = Xmf\Module\Helper::getHelper($moduleDirName); |
45
|
|
|
$permHelper = new Xmf\Module\Helper\Permission($moduleDirName); |
46
|
|
|
$xfFaqHandler = $xfHelper->getHandler('contents'); |
47
|
|
|
$block = array(); |
48
|
|
|
|
49
|
|
|
$criteria = new CriteriaCompo(); |
50
|
|
|
$criteria->add(new Criteria('contents_active', XoopsfaqConstants::ACTIVE, '=')); |
51
|
|
|
|
52
|
|
|
// Filter out cats based on group permissions |
53
|
|
|
$options[1] = isset($options[1]) ? $options[1] : array(0); |
54
|
|
|
$cTu = $catsToUse = (false === strpos($options[1], ',')) ? (array)$options[1] : explode(',', $options[1]); |
55
|
|
View Code Duplication |
if (in_array(0, $catsToUse) || empty($catsToUse)) { |
|
|
|
|
56
|
|
|
// Get a list of all cats |
57
|
|
|
$xfCatHandler = $xfHelper->getHandler('category'); |
58
|
|
|
$catListArray = $xfCatHandler->getList(); |
59
|
|
|
$cTu = $catsToUse = array_keys($catListArray); |
60
|
|
|
} |
61
|
|
|
// Remove any cats this user doesn't have rights to view |
62
|
|
View Code Duplication |
foreach ($cTu as $key => $thisCat) { |
|
|
|
|
63
|
|
|
if (false === $permHelper->checkPermission('viewcat', $thisCat)) { |
64
|
|
|
unset($catsToUse[$key]); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
View Code Duplication |
if (!empty($catsToUse)) { |
|
|
|
|
68
|
|
|
$criteria->add(new Criteria('contents_cid', '(' . implode(',', $catsToUse) . ')', 'IN')); |
69
|
|
|
} else { |
70
|
|
|
return $block; |
71
|
|
|
} |
72
|
|
|
$xpFaqObjArray = $xfFaqHandler->getAll($criteria); |
73
|
|
|
$faqCount = (is_array($xpFaqObjArray) && !empty($xpFaqObjArray)) ? count($xpFaqObjArray) : 0; |
74
|
|
|
|
75
|
|
|
if ($faqCount > 0) { |
76
|
|
|
$faqNum = array_rand($xpFaqObjArray, 1); |
77
|
|
|
$xpFaqObj = $xpFaqObjArray[$faqNum]; |
78
|
|
|
$faq = $myts->displayTarea($xpFaqObj->getVar('contents_title')); |
79
|
|
|
$txtAns = strip_tags($xpFaqObj->getVar('contents_contents')); // get rid of html for block |
80
|
|
|
$faqAns = $myts->displayTarea(xoops_substr($txtAns, 0, $options[0]), 0, 0 ,0 ,0 ,0); |
81
|
|
|
|
82
|
|
|
$xfCatHandler = $xfHelper->getHandler('category'); |
83
|
|
|
$catObj = $xfCatHandler->get($xpFaqObj->getVar('contents_cid')); |
84
|
|
|
$cid = $catObj->getVar('category_id'); |
85
|
|
|
$block = array( 'title' => _MB_XOOPSFAQ_RANDOM_TITLE, |
86
|
|
|
'faq' => $faq, |
87
|
|
|
'faqans' => $faqAns, |
88
|
|
|
'morelink' => $xfHelper->url('index.php?cat_id=' . $cid . '#q' . $xpFaqObj->getVar('contents_id')), |
89
|
|
|
'linktxt' => _MB_XOOPSFAQ_SEE_MORE, |
90
|
|
|
'catlink' => $xfHelper->url('index.php?cat_id=' . $cid), |
91
|
|
|
'cattxt' => $catObj->getVar('category_title') |
92
|
|
|
); |
93
|
|
|
unset($xpFaqObj, $catObj); |
94
|
|
|
} |
95
|
|
|
return $block; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Edit Random FAQ Block preferences |
100
|
|
|
* |
101
|
|
|
* @param array $options |
102
|
|
|
* [0] - number of chars in title to show (0 = unlimited) |
103
|
|
|
* [1] - comma separated list of categories to use/select from |
104
|
|
|
* @return string HTML entities to display for user input |
105
|
|
|
*/ |
106
|
|
|
function b_xoopsfaq_rand_edit($options) |
107
|
|
|
{ |
108
|
|
|
$moduleDirName = basename(dirname(__DIR__)); |
109
|
|
|
xoops_load('XoopsFormSelect'); |
110
|
|
|
|
111
|
|
|
/** @var XoopsfaqCategoryHandler $xfCatHandler */ |
112
|
|
|
/** @var Xmf\Module\Helper\GenericHelper $xfHelper */ |
113
|
|
|
$xfHelper = Xmf\Module\Helper::getHelper($moduleDirName); |
114
|
|
|
$xfCatHandler = $xfHelper->getHandler('category'); |
115
|
|
|
|
116
|
|
|
$catList = $xfCatHandler->getList(); |
117
|
|
|
$optionArray = array_merge(array(0 => _MB_XOOPSFAQ_ALL_CATS), $catList); |
118
|
|
|
$formSelect = new XoopsFormSelect('category', 'options[1]', null, 3, true); |
119
|
|
|
$formSelect->addOptionArray($optionArray); |
120
|
|
|
$selOptions = (false === strpos($options[1], ',')) ? $options[1] : explode(',', $options[1]); |
121
|
|
|
$formSelect->setValue($selOptions); |
122
|
|
|
$selectCat = $formSelect->render(); |
123
|
|
|
|
124
|
|
|
$form = '<div class="line140">' |
125
|
|
|
. _MB_XOOPSFAQ_CHARS . ' ' |
126
|
|
|
. '<input type="number" name="options[0]" value="' . $options[0] . '" style="width: 5em;" min="0" class="right"> ' |
127
|
|
|
. _MB_XOOPSFAQ_LENGTH . '<br><br>' . _MB_XOOPSFAQ_ALL_CATS_INTRO . ' ' . $selectCat |
128
|
|
|
. '</div>'; |
129
|
|
|
return $form; |
130
|
|
|
} |
131
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.