forked from XoopsModules25x/tag
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathview.tag.php
226 lines (200 loc) · 10.1 KB
/
view.tag.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
<?php declare(strict_types=1);
/*
You may not change or alter any portion of this comment or credits
of supporting developers from this source code or any supporting source code
which is considered copyrighted (c) material of the original comment or credit authors.
This program 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.
*/
/**
* XOOPS tag management module
*
* @copyright XOOPS Project (https://xoops.org)
* @license GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
* @since 1.00
* @author Taiwen Jiang <phppp@users.sourceforge.net>
* */
use Xmf\Request;
use XoopsModules\Tag\{
Common,
Constants,
Tagbar,
Utility
};
require_once __DIR__ . '/header.php';
//@todo refactor this code - it "works" but it's not right. Look at previous revs using $args_num to see what it's suppose to do
if (Utility::tag_parse_args($args, $args_string)) {
$args['tag'] = !empty($args['tag']) ? $args['tag'] : (is_numeric($args_string[0]) ? $args_string[0] : Constants::DEFAULT_ID);
$args['term'] = !empty($args['term']) ? $args['term'] : (!empty($args_string[0]) ? $args_string[0] : null);
$args['modid'] = !empty($args['modid']) ? $args['modid'] : Constants::DEFAULT_ID;
$args['catid'] = !empty($args['catid']) ? $args['catid'] : Constants::DEFAULT_ID;
$args['start'] = !empty($args['start']) ? $args['start'] : Constants::BEGINNING;
}
/*
$tagid = (int)(empty($_GET['tag']) ? @$args['tag'] : $_GET['tag']);
$tag_term = empty($_GET['term']) ? @$args['term'] : Request::getString('term', '', 'GET');
$modid = (int)(empty($_GET['modid']) ? @$args['modid'] : $_GET['modid']);
$catid = (int)(empty($_GET['catid']) ? @$args['catid'] : $_GET['catid']);
$start = (int)(empty($_GET['start']) ? @$args['start'] : $_GET['start']);
*/
$tagid = (empty($_GET['tag'])) ? @$args['tag'] : Request::getInt('tag', Constants::DEFAULT_ID, 'GET');
$tag_term = empty($_GET['term']) ? @$args['term'] : Request::getString('term', '', 'GET');
$modid = (int)(empty($_GET['modid'])) ? @$args['modid'] : Request::getInt('modid', Constants::DEFAULT_ID, 'GET');
$catid = (int)(empty($_GET['catid'])) ? @$args['catid'] : Request::getInt('catid', Constants::DEFAULT_ID, 'GET');
$start = (int)(empty($_GET['start'])) ? @$args['start'] : Request::getInt('start', Constants::BEGINNING, 'GET');
if (empty($modid) && ($GLOBALS['xoopsModule'] instanceof \XoopsModule)
&& 'tag' !== $GLOBALS['xoopsModule']->getVar('dirname', 'n')) {
$modid = $GLOBALS['xoopsModule']->getVar('mid');
}
/** @var \XoopsModules\Tag\TagHandler $tagHandler */
$tagHandler = $helper->getHandler('Tag');
if (!empty($tagid)) { // have a tag_id, so check to see if it yields a valid Tag object
if ((!$tagObj = $tagHandler->get((int)$tagid)) || $tagObj->isNew()) {
$helper->redirect('index.php', Constants::REDIRECT_DELAY_MEDIUM, _MD_TAG_INVALID);
}
$tag_term = $tagObj->getVar('tag_term', 'n');
} elseif (!empty($tag_term)) {
if (!$tags_obj = $tagHandler->getObjects(new \Criteria('tag_term', $GLOBALS['xoopsDB']->escape(trim($tag_term))))) {
$helper->redirect('index.php', Constants::REDIRECT_DELAY_MEDIUM, _MD_TAG_INVALID);
}
$tagObj = $tags_obj[0];
$tagid = $tagObj->getVar('tag_id');
} else {
$helper->redirect('index.php', Constants::REDIRECT_DELAY_MEDIUM, _MD_TAG_INVALID);
}
// made it here, so now we have a valid $tagid and $tag_term
$tag_term = mb_convert_case($tag_term, MB_CASE_TITLE, 'UTF-8');
// @todo: where does $tag_desc come from? - looks like it will always be empty
if (!empty($tag_desc)) {
$page_title = $tag_desc;
} else {
$module_name = ('tag' === $GLOBALS['xoopsModule']->getVar('dirname', 'n')) ? $GLOBALS['xoopsConfig']['sitename'] : $GLOBALS['xoopsModule']->getVar('name', 'n');
$module_name = mb_convert_case($module_name, MB_CASE_TITLE, 'UTF-8');
$page_title = sprintf(_MD_TAG_TAGVIEW, htmlspecialchars($tag_term, ENT_QUOTES | ENT_HTML5), $module_name);
}
$GLOBALS['xoopsOption']['template_main'] = 'tag_view.tpl';
$GLOBALS['xoopsOption']['xoops_pagetitle'] = strip_tags($page_title);
require_once $GLOBALS['xoops']->path('header.php');
$GLOBALS['xoTheme']->addStylesheet("browse.php?modules/{$moduleDirName}/assets/css/style.css");
$tag_config = Utility::tag_load_config();
Utility::tag_define_url_delimiter();
$limit = empty($tag_config['items_perpage']) ? Constants::DEFAULT_LIMIT : $tag_config['items_perpage'];
$criteria = new \CriteriaCompo(new \Criteria('o.tag_id', (string)$tagid));
$criteria->setSort('time');
$criteria->order = 'DESC'; // set order directly, XOOPS 2.5x does not set order correctly using Criteria::setOrder() method
$criteria->setStart($start);
$criteria->setLimit($limit);
if (!empty($modid)) {
$criteria->add(new \Criteria('o.tag_modid', (string)$modid));
if ($catid >= Constants::DEFAULT_ID) {
$criteria->add(new \Criteria('o.tag_catid', (string)$catid));
}
}
$items_array = $tagHandler->getItems($criteria);
$module_item_array = [];
$module_obj_array = [];
if (0 < count($items_array)) {
foreach ($items_array as $this_item) {
$module_item_array[$this_item['modid']][$this_item['catid']][$this_item['itemid']] = [];
}
/** @var \XoopsModuleHandler $moduleHandler */
$moduleHandler = xoops_getHandler('module');
$module_obj_array = $moduleHandler->getObjects(new \Criteria('mid', '(' . implode(', ', array_keys($module_item_array)) . ')', 'IN'), true);
foreach ($module_obj_array as $mid => $moduleObj) {
$dirname = $moduleObj->getVar('dirname', 'n');
//$dirname = $module_obj_array[$mid]->getVar('dirname', 'n');
if (file_exists($GLOBALS['xoops']->path("modules/{$dirname}/class/plugins/plugin.tag.php"))) {
require_once $GLOBALS['xoops']->path("modules/{$dirname}/class/plugins/plugin.tag.php");
} elseif (file_exists($GLOBALS['xoops']->path("modules/{$dirname}/include/plugin.tag.php"))) {
require_once $GLOBALS['xoops']->path("modules/{$dirname}/include/plugin.tag.php");
} elseif (file_exists($GLOBALS['xoops']->path("modules/tag/plugin/{$dirname}.php"))) {
require_once $GLOBALS['xoops']->path("modules/tag/plugin/{$dirname}.php");
} else {
continue;
}
$func_tag = "{$dirname}_tag_iteminfo";
if (function_exists($func_tag)) {
// Return related item infomation: title, content, time, uid, all tags
$res = $func_tag($module_item_array[$mid]);
}
}
}
$items_data = [];
$uids = [];
$tagbar = new Tagbar();
foreach ($items_array as $key => $myItem) {
/**
* Get item fields:
* title
* content
* time
* uid
* tags
*/
if (!$item = @$module_item_array[$myItem['modid']][$myItem['catid']][$myItem['itemid']]) {
continue;
}
$item['module'] = $module_obj_array[$myItem['modid']]->getVar('name');
$item['dirname'] = $module_obj_array[$myItem['modid']]->getVar('dirname', 'n');
$time = empty($item['time']) ? $myItem['time'] : $item['time'];
$item['timestamp'] = $time; //needed for sorting
$item['time'] = formatTimestamp($time, 's');
$item['tags'] = $tagbar->getTagbar($item['tags']);
$items_data[] = $item;
// @todo: fix this to use xoops user id, if present otherwise to 1st admin
$uids[$item['uid']] = 1;
}
unset($item);
// sorting of the array by timestamp (the newest item on top)
$timestamp = array_column($items_data, 'timestamp');
// Sort the data with timestamp descending
// Add $items_data as the last parameter, to sort by the common key
array_multisort($timestamp, SORT_DESC, $items_data);
xoops_load('XoopsUserUtility');
$users = \XoopsUserUtility::getUnameFromIds(array_keys($uids));
foreach ($items_data as $key => $item) {
$items_data[$key]['uname'] = $users[$item['uid']];
}
if (!empty($start) || count($items_data) >= $limit) {
$count_item = $tagHandler->getItemCount($tagid, $modid, $catid); // Tag, modid, catid
require_once $GLOBALS['xoops']->path('/class/pagenav.php');
$nav = new \XoopsPageNav($count_item, $limit, $start, 'start', "tag={$tagid}&catid={$catid}");
$pagenav = $nav->renderNav(4);
} else {
$pagenav = '';
}
//add-ons to tag externally (e.g. Google, Flickr)
$tag_addon = [];
if (!empty($GLOBALS['_MD_TAG_ADDONS'])) {
$tag_addon['title'] = _MD_TAG_TAG_ON;
foreach ($GLOBALS['_MD_TAG_ADDONS'] as $key => $_tag) {
$_term = (empty($_tag['function']) || !function_exists($_tag['function'])) ? $tag_term : $_tag['function']($tag_term);
$tag_addon['addons'][] = "<a href='" . sprintf($_tag['link'], urlencode($_term)) . "' target='{$key}' title='{$_tag['title']}'>{$_tag['title']}</a>";
}
}
// Breadcrumb
$breadcrumb = new Common\Breadcrumb();
$breadcrumb->addLink($helper->getModule()->getVar('name'), $helper->url());
$breadcrumb->addLink(_MD_TAG_TAGS, $helper->url('list.tag.php'));
//$breadcrumb->addLink(htmlspecialchars($tag_term, ENT_QUOTES | ENT_HTML5), $helper->url('view.tag.php' . URL_DELIMITER . urlencode($tag_term)));
$breadcrumb->addLink(htmlspecialchars($tag_term, ENT_QUOTES | ENT_HTML5));
$GLOBALS['xoopsTpl']->assign(
[
'module_name' => $GLOBALS['xoopsModule']->getVar('name'),
'tag_id' => $tagid,
'tag_term' => urlencode($tag_term),
'tag_title' => htmlspecialchars($tag_term, ENT_QUOTES | ENT_HTML5),
'tag_page_title' => $page_title,
'tag_breadcrumb' => $breadcrumb->render(),
// Loading module meta data, NOT THE RIGHT WAY DOING IT
'xoops_pagetitle' => $GLOBALS['xoopsOption']['xoops_pagetitle'],
//'xoops_module_header' => $GLOBALS['xoopsOption']['xoops_module_header'],
'xoops_meta_description' => $GLOBALS['xoopsOption']['xoops_pagetitle'],
]
);
//@todo do these need to be assign by ref?
$xoopsTpl->assign_by_ref('tag_addon', $tag_addon);
$xoopsTpl->assign_by_ref('tag_articles', $items_data);
$xoopsTpl->assign_by_ref('pagenav', $pagenav);
require_once __DIR__ . '/footer.php';