Total Complexity | 169 |
Total Lines | 1147 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like Item often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Item, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class Item extends \XoopsObject |
||
38 | { |
||
39 | /** |
||
40 | * @var Publisher\Helper |
||
41 | */ |
||
42 | public $helper; |
||
43 | public $groupsRead = []; |
||
44 | |||
45 | /** |
||
46 | * @var Publisher\Category |
||
47 | */ |
||
48 | public $category; |
||
49 | |||
50 | /** |
||
51 | * @param int|null $id |
||
52 | */ |
||
53 | public function __construct($id = null) |
||
54 | { |
||
55 | /** @var \XoopsModules\Publisher\Helper $this->helper */ |
||
56 | $this->helper = \XoopsModules\Publisher\Helper::getInstance(); |
||
57 | /** @var \XoopsDatabase $this->db */ |
||
58 | $this->db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
|
|||
59 | $this->initVar('itemid', \XOBJ_DTYPE_INT, 0); |
||
60 | $this->initVar('categoryid', \XOBJ_DTYPE_INT, 0, false); |
||
61 | $this->initVar('title', \XOBJ_DTYPE_TXTBOX, '', true, 255); |
||
62 | $this->initVar('subtitle', \XOBJ_DTYPE_TXTBOX, '', false, 255); |
||
63 | $this->initVar('summary', \XOBJ_DTYPE_TXTAREA, '', false); |
||
64 | $this->initVar('body', \XOBJ_DTYPE_TXTAREA, '', false); |
||
65 | $this->initVar('uid', \XOBJ_DTYPE_INT, 0, false); |
||
66 | $this->initVar('author_alias', \XOBJ_DTYPE_TXTBOX, '', false, 255); |
||
67 | $this->initVar('datesub', \XOBJ_DTYPE_INT, '', false); |
||
68 | $this->initVar('dateexpire', \XOBJ_DTYPE_INT, '', false); |
||
69 | $this->initVar('status', \XOBJ_DTYPE_INT, -1, false); |
||
70 | $this->initVar('image', \XOBJ_DTYPE_INT, 0, false); |
||
71 | $this->initVar('images', \XOBJ_DTYPE_TXTBOX, '', false, 255); |
||
72 | $this->initVar('counter', \XOBJ_DTYPE_INT, 0, false); |
||
73 | $this->initVar('rating', \XOBJ_DTYPE_OTHER, 0, false); |
||
74 | $this->initVar('votes', \XOBJ_DTYPE_INT, 0, false); |
||
75 | $this->initVar('weight', \XOBJ_DTYPE_INT, 0, false); |
||
76 | $this->initVar('dohtml', \XOBJ_DTYPE_INT, 1, true); |
||
77 | $this->initVar('dosmiley', \XOBJ_DTYPE_INT, 1, true); |
||
78 | $this->initVar('doimage', \XOBJ_DTYPE_INT, 1, true); |
||
79 | $this->initVar('dobr', \XOBJ_DTYPE_INT, 1, false); |
||
80 | $this->initVar('doxcode', \XOBJ_DTYPE_INT, 1, true); |
||
81 | $this->initVar('cancomment', \XOBJ_DTYPE_INT, 1, true); |
||
82 | $this->initVar('comments', \XOBJ_DTYPE_INT, 0, false); |
||
83 | $this->initVar('notifypub', \XOBJ_DTYPE_INT, 1, false); |
||
84 | $this->initVar('meta_keywords', \XOBJ_DTYPE_TXTAREA, '', false); |
||
85 | $this->initVar('meta_description', \XOBJ_DTYPE_TXTAREA, '', false); |
||
86 | $this->initVar('short_url', \XOBJ_DTYPE_TXTBOX, '', false, 255); |
||
87 | $this->initVar('item_tag', \XOBJ_DTYPE_TXTAREA, '', false); |
||
88 | // Non consistent values |
||
89 | $this->initVar('pagescount', \XOBJ_DTYPE_INT, 0, false); |
||
90 | if (null !== $id) { |
||
91 | $item = $this->helper->getHandler('Item')->get($id); |
||
92 | foreach ($item->vars as $k => $v) { |
||
93 | $this->assignVar($k, $v['value']); |
||
94 | } |
||
95 | } |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @param string $method |
||
100 | * @param array $args |
||
101 | * |
||
102 | * @return mixed |
||
103 | */ |
||
104 | public function __call($method, $args) |
||
105 | { |
||
106 | $arg = $args[0] ?? null; |
||
107 | |||
108 | return $this->getVar($method, $arg); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @return null|Publisher\Category |
||
113 | */ |
||
114 | public function getCategory() |
||
115 | { |
||
116 | if (null === $this->category) { |
||
117 | $this->category = $this->helper->getHandler('Category')->get($this->getVar('categoryid')); |
||
118 | } |
||
119 | |||
120 | return $this->category; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @param int $maxLength |
||
125 | * @param string $format |
||
126 | * |
||
127 | * @return mixed|string |
||
128 | */ |
||
129 | public function getTitle($maxLength = 0, $format = 'S') |
||
130 | { |
||
131 | $ret = $this->getVar('title', $format); |
||
132 | if (0 != $maxLength) { |
||
133 | if (!XOOPS_USE_MULTIBYTES) { |
||
134 | if (mb_strlen($ret) >= $maxLength) { |
||
135 | $ret = Publisher\Utility::substr($ret, 0, $maxLength); |
||
136 | } |
||
137 | } |
||
138 | } |
||
139 | |||
140 | return $ret; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @param int $maxLength |
||
145 | * @param string $format |
||
146 | * |
||
147 | * @return mixed|string |
||
148 | */ |
||
149 | public function getSubtitle($maxLength = 0, $format = 'S') |
||
150 | { |
||
151 | $ret = $this->getVar('subtitle', $format); |
||
152 | if (0 != $maxLength) { |
||
153 | if (!XOOPS_USE_MULTIBYTES) { |
||
154 | if (mb_strlen($ret) >= $maxLength) { |
||
155 | $ret = Publisher\Utility::substr($ret, 0, $maxLength); |
||
156 | } |
||
157 | } |
||
158 | } |
||
159 | |||
160 | return $ret; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * @param int $maxLength |
||
165 | * @param string $format |
||
166 | * @param string $stripTags |
||
167 | * |
||
168 | * @return mixed|string |
||
169 | */ |
||
170 | public function getSummary($maxLength = 0, $format = 'S', $stripTags = '') |
||
171 | { |
||
172 | $ret = $this->getVar('summary', $format); |
||
173 | if (!empty($stripTags)) { |
||
174 | $ret = \strip_tags($ret, $stripTags); |
||
175 | } |
||
176 | if (0 != $maxLength) { |
||
177 | if (!XOOPS_USE_MULTIBYTES) { |
||
178 | if (mb_strlen($ret) >= $maxLength) { |
||
179 | //$ret = Publisher\Utility::substr($ret , 0, $maxLength); |
||
180 | // $ret = Publisher\Utility::truncateTagSafe($ret, $maxLength, $etc = '...', $breakWords = false); |
||
181 | $ret = Publisher\Utility::truncateHtml($ret, $maxLength, $etc = '...', $breakWords = false); |
||
182 | } |
||
183 | } |
||
184 | } |
||
185 | |||
186 | return $ret; |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * @param int $maxLength |
||
191 | * @param bool $fullSummary |
||
192 | * |
||
193 | * @return mixed|string |
||
194 | */ |
||
195 | public function getBlockSummary($maxLength = 0, $fullSummary = false) |
||
196 | { |
||
197 | if ($fullSummary) { |
||
198 | $ret = $this->getSummary(0, 's', '<br><br>'); |
||
199 | } else { |
||
200 | $ret = $this->getSummary($maxLength, 's', '<br><br>'); |
||
201 | } |
||
202 | //no summary? get body! |
||
203 | if ('' === $ret) { |
||
204 | $ret = $this->getBody($maxLength, 's', '<br><br>'); |
||
205 | } |
||
206 | |||
207 | return $ret; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * @param string $fileName |
||
212 | * |
||
213 | * @return string |
||
214 | */ |
||
215 | public function wrapPage($fileName) |
||
216 | { |
||
217 | $content = ''; |
||
218 | $page = Publisher\Utility::getUploadDir(true, 'content') . $fileName; |
||
219 | if (\file_exists($page)) { |
||
220 | // this page uses smarty template |
||
221 | \ob_start(); |
||
222 | require $page; |
||
223 | $content = \ob_get_clean(); |
||
224 | // Cleaning the content |
||
225 | $bodyStartPos = mb_strpos($content, '<body>'); |
||
226 | if ($bodyStartPos) { |
||
227 | $bodyEndPos = mb_strpos($content, '</body>', $bodyStartPos); |
||
228 | $content = mb_substr($content, $bodyStartPos + mb_strlen('<body>'), $bodyEndPos - mb_strlen('<body>') - $bodyStartPos); |
||
229 | } |
||
230 | // Check if ML Hack is installed, and if yes, parse the $content in formatForML |
||
231 | $myts = \MyTextSanitizer::getInstance(); |
||
232 | if (\method_exists($myts, 'formatForML')) { |
||
233 | $content = $myts->formatForML($content); |
||
234 | } |
||
235 | } |
||
236 | |||
237 | return $content; |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * This method returns the body to be displayed. Not to be used for editing |
||
242 | * |
||
243 | * @param int $maxLength |
||
244 | * @param string $format |
||
245 | * @param string $stripTags |
||
246 | * |
||
247 | * @return mixed|string |
||
248 | */ |
||
249 | public function getBody($maxLength = 0, $format = 'S', $stripTags = '') |
||
250 | { |
||
251 | $ret = $this->getVar('body', $format); |
||
252 | $wrapPos = mb_strpos($ret, '[pagewrap='); |
||
253 | if (!(false === $wrapPos)) { |
||
254 | $wrapPages = []; |
||
255 | $wrapCodeLength = mb_strlen('[pagewrap='); |
||
256 | while (!(false === $wrapPos)) { |
||
257 | $endWrapPos = mb_strpos($ret, ']', $wrapPos); |
||
258 | if ($endWrapPos) { |
||
259 | $wrap_page_name = mb_substr($ret, $wrapPos + $wrapCodeLength, $endWrapPos - $wrapCodeLength - $wrapPos); |
||
260 | $wrapPages[] = $wrap_page_name; |
||
261 | } |
||
262 | $wrapPos = mb_strpos($ret, '[pagewrap=', $endWrapPos - 1); |
||
263 | } |
||
264 | foreach ($wrapPages as $page) { |
||
265 | $wrapPageContent = $this->wrapPage($page); |
||
266 | $ret = \str_replace("[pagewrap={$page}]", $wrapPageContent, $ret); |
||
267 | } |
||
268 | } |
||
269 | if ($this->helper->getConfig('item_disp_blocks_summary')) { |
||
270 | $summary = $this->getSummary($maxLength, $format, $stripTags); |
||
271 | if ($summary) { |
||
272 | $ret = $this->getSummary() . '<br><br>' . $ret; |
||
273 | } |
||
274 | } |
||
275 | if (!empty($stripTags)) { |
||
276 | $ret = \strip_tags($ret, $stripTags); |
||
277 | } |
||
278 | if (0 != $maxLength) { |
||
279 | if (!XOOPS_USE_MULTIBYTES) { |
||
280 | if (mb_strlen($ret) >= $maxLength) { |
||
281 | //$ret = Publisher\Utility::substr($ret , 0, $maxLength); |
||
282 | $ret = Publisher\Utility::truncateHtml($ret, $maxLength, $etc = '...', $breakWords = false); |
||
283 | } |
||
284 | } |
||
285 | } |
||
286 | |||
287 | return $ret; |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * @param string $dateFormat |
||
292 | * @param string $format |
||
293 | * |
||
294 | * @return string |
||
295 | */ |
||
296 | public function getDatesub($dateFormat = '', $format = 'S') |
||
297 | { |
||
298 | if (empty($dateFormat)) { |
||
299 | $dateFormat = $this->helper->getConfig('format_date'); |
||
300 | } |
||
301 | |||
302 | return \formatTimestamp($this->getVar('datesub', $format), $dateFormat); |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * @param string $dateFormat |
||
307 | * @param string $format |
||
308 | * |
||
309 | * @return string |
||
310 | */ |
||
311 | public function getDateExpire($dateFormat = '', $format = 'S') |
||
312 | { |
||
313 | if (empty($dateFormat)) { |
||
314 | $dateFormat = $this->helper->getConfig('format_date'); |
||
315 | } |
||
316 | if (0 == $this->getVar('dateexpire')) { |
||
317 | return false; |
||
318 | } |
||
319 | |||
320 | return \formatTimestamp($this->getVar('dateexpire', $format), $dateFormat); |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * @param int $realName |
||
325 | * |
||
326 | * @return string |
||
327 | */ |
||
328 | public function posterName($realName = -1) |
||
329 | { |
||
330 | \xoops_load('XoopsUserUtility'); |
||
331 | if (-1 == $realName) { |
||
332 | $realName = $this->helper->getConfig('format_realname'); |
||
333 | } |
||
334 | $ret = $this->author_alias(); |
||
335 | if ('' == $ret) { |
||
336 | $ret = \XoopsUserUtility::getUnameFromId($this->uid(), $realName); |
||
337 | } |
||
338 | |||
339 | return $ret; |
||
340 | } |
||
341 | |||
342 | /** |
||
343 | * @return string |
||
344 | */ |
||
345 | public function posterAvatar() |
||
346 | { |
||
347 | $ret = 'blank.gif'; |
||
348 | $memberHandler = \xoops_getHandler('member'); |
||
349 | $thisUser = $memberHandler->getUser($this->uid()); |
||
350 | if (\is_object($thisUser)) { |
||
351 | $ret = $thisUser->getVar('user_avatar'); |
||
352 | } |
||
353 | |||
354 | return $ret; |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * @return string |
||
359 | */ |
||
360 | public function getLinkedPosterName() |
||
361 | { |
||
362 | \xoops_load('XoopsUserUtility'); |
||
363 | $ret = $this->author_alias(); |
||
364 | if ('' === $ret) { |
||
365 | $ret = \XoopsUserUtility::getUnameFromId($this->uid(), $this->helper->getConfig('format_realname'), true); |
||
366 | } |
||
367 | |||
368 | return $ret; |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * @return mixed |
||
373 | */ |
||
374 | public function updateCounter() |
||
375 | { |
||
376 | return $this->helper->getHandler('Item')->updateCounter($this->itemid()); |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * @param bool $force |
||
381 | * |
||
382 | * @return bool |
||
383 | */ |
||
384 | public function store($force = true) |
||
385 | { |
||
386 | $isNew = $this->isNew(); |
||
387 | if (!$this->helper->getHandler('Item')->insert($this, $force)) { |
||
388 | return false; |
||
389 | } |
||
390 | if ($isNew && Constants::PUBLISHER_STATUS_PUBLISHED == $this->getVar('status')) { |
||
391 | // Increment user posts |
||
392 | $userHandler = \xoops_getHandler('user'); |
||
393 | $memberHandler = \xoops_getHandler('member'); |
||
394 | $poster = $userHandler->get($this->uid()); |
||
395 | if (\is_object($poster) && !$poster->isNew()) { |
||
396 | $poster->setVar('posts', $poster->getVar('posts') + 1); |
||
397 | if (!$memberHandler->insertUser($poster, true)) { |
||
398 | $this->setErrors('Article created but could not increment user posts.'); |
||
399 | |||
400 | return false; |
||
401 | } |
||
402 | } |
||
403 | } |
||
404 | |||
405 | return true; |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * @return string |
||
410 | */ |
||
411 | public function getCategoryName() |
||
412 | { |
||
413 | return $this->getCategory()->name(); |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * @return string |
||
418 | */ |
||
419 | public function getCategoryUrl() |
||
420 | { |
||
421 | return $this->getCategory()->getCategoryUrl(); |
||
422 | } |
||
423 | |||
424 | /** |
||
425 | * @return string |
||
426 | */ |
||
427 | public function getCategoryLink() |
||
428 | { |
||
429 | return $this->getCategory()->getCategoryLink(); |
||
430 | } |
||
431 | |||
432 | /** |
||
433 | * @param bool $withAllLink |
||
434 | * |
||
435 | * @return string |
||
436 | */ |
||
437 | public function getCategoryPath($withAllLink = true) |
||
438 | { |
||
439 | return $this->getCategory()->getCategoryPath($withAllLink); |
||
440 | } |
||
441 | |||
442 | /** |
||
443 | * @return string |
||
444 | */ |
||
445 | public function getCategoryImagePath() |
||
446 | { |
||
447 | return Publisher\Utility::getImageDir('category', false) . $this->getCategory()->getImage(); |
||
448 | } |
||
449 | |||
450 | /** |
||
451 | * @return mixed |
||
452 | */ |
||
453 | public function getFiles() |
||
454 | { |
||
455 | return $this->helper->getHandler('File')->getAllFiles($this->itemid(), Constants::PUBLISHER_STATUS_FILE_ACTIVE); |
||
456 | } |
||
457 | |||
458 | /** |
||
459 | * @return string |
||
460 | */ |
||
461 | public function getAdminLinks() |
||
462 | { |
||
463 | $adminLinks = ''; |
||
464 | if (\is_object($GLOBALS['xoopsUser']) |
||
465 | && (Publisher\Utility::userIsAdmin() || Publisher\Utility::userIsAuthor($this) |
||
466 | || $this->helper->getHandler('Permission')->isGranted('item_submit', $this->categoryid()))) { |
||
467 | if (Publisher\Utility::userIsAdmin() || Publisher\Utility::userIsAuthor($this) || Publisher\Utility::userIsModerator($this)) { |
||
468 | if ($this->helper->getConfig('perm_edit') || Publisher\Utility::userIsModerator($this) || Publisher\Utility::userIsAdmin()) { |
||
469 | // Edit button |
||
470 | $adminLinks .= "<a href='" . PUBLISHER_URL . '/submit.php?itemid=' . $this->itemid() . "'><img src='" . PUBLISHER_URL . "/assets/images/links/edit.gif'" . " title='" . \_CO_PUBLISHER_EDIT . "' alt='" . \_CO_PUBLISHER_EDIT . "'></a>"; |
||
471 | $adminLinks .= ' '; |
||
472 | } |
||
473 | if ($this->helper->getConfig('perm_delete') || Publisher\Utility::userIsModerator($this) || Publisher\Utility::userIsAdmin()) { |
||
474 | // Delete button |
||
475 | $adminLinks .= "<a href='" . PUBLISHER_URL . '/submit.php?op=del&itemid=' . $this->itemid() . "'><img src='" . PUBLISHER_URL . "/assets/images/links/delete.png'" . " title='" . \_CO_PUBLISHER_DELETE . "' alt='" . \_CO_PUBLISHER_DELETE . "'></a>"; |
||
476 | $adminLinks .= ' '; |
||
477 | } |
||
478 | } |
||
479 | if ($this->helper->getConfig('perm_clone') || Publisher\Utility::userIsModerator($this) || Publisher\Utility::userIsAdmin()) { |
||
480 | // Duplicate button |
||
481 | $adminLinks .= "<a href='" . PUBLISHER_URL . '/submit.php?op=clone&itemid=' . $this->itemid() . "'><img src='" . PUBLISHER_URL . "/assets/images/links/clone.gif'" . " title='" . \_CO_PUBLISHER_CLONE . "' alt='" . \_CO_PUBLISHER_CLONE . "'></a>"; |
||
482 | $adminLinks .= ' '; |
||
483 | } |
||
484 | } |
||
485 | |||
486 | return $adminLinks; |
||
487 | } |
||
488 | |||
489 | /** |
||
490 | * @return string |
||
491 | */ |
||
492 | public function getPdfButton() |
||
493 | { |
||
494 | $pdfButton = ''; |
||
495 | // PDF button |
||
496 | if (!\is_file(XOOPS_ROOT_PATH . '/class/libraries/vendor/tecnickcom/tcpdf/tcpdf.php')) { |
||
497 | // if (is_object($GLOBALS['xoopsUser']) && Publisher\Utility::userIsAdmin()) { |
||
498 | // $GLOBALS['xoTheme']->addStylesheet('/modules/system/css/jquery.jgrowl.min.css'); |
||
499 | // $GLOBALS['xoTheme']->addScript('browse.php?Frameworks/jquery/plugins/jquery.jgrowl.js'); |
||
500 | // $adminLinks .= '<script type="text/javascript"> |
||
501 | // (function($){ |
||
502 | // $(document).ready(function(){ |
||
503 | // $.jGrowl("' . _MD_PUBLISHER_ERROR_NO_PDF . '");}); |
||
504 | // })(jQuery); |
||
505 | // </script>'; |
||
506 | // } |
||
507 | } else { |
||
508 | $pdfButton .= "<a href='" . PUBLISHER_URL . '/makepdf.php?itemid=' . $this->itemid() . "' rel='nofollow' target='_blank'><img src='" . PUBLISHER_URL . "/assets/images/links/pdf.gif'" . " title='" . \_CO_PUBLISHER_PDF . "' alt='" . \_CO_PUBLISHER_PDF . "'></a> "; |
||
509 | $pdfButton .= ' '; |
||
510 | } |
||
511 | return $pdfButton; |
||
512 | } |
||
513 | |||
514 | /** |
||
515 | * @return string |
||
516 | */ |
||
517 | public function getPrintLinks() |
||
518 | { |
||
519 | $printLinks = ''; |
||
520 | // Print button |
||
521 | $printLinks .= "<a href='" . Publisher\Seo::generateUrl('print', $this->itemid(), $this->short_url()) . "' rel='nofollow' target='_blank'><img src='" . PUBLISHER_URL . "/assets/images/links/print.gif' title='" . \_CO_PUBLISHER_PRINT . "' alt='" . \_CO_PUBLISHER_PRINT . "'></a> "; |
||
522 | $printLinks .= ' '; |
||
523 | return $printLinks; |
||
524 | } |
||
525 | |||
526 | /** |
||
527 | * @param array $notifications |
||
528 | */ |
||
529 | public function sendNotifications($notifications = []) |
||
530 | { |
||
531 | /** @var \XoopsNotificationHandler $notificationHandler */ |
||
532 | $notificationHandler = \xoops_getHandler('notification'); |
||
533 | $tags = []; |
||
534 | |||
535 | $tags['MODULE_NAME'] = $this->helper->getModule()->getVar('name'); |
||
536 | $tags['ITEM_NAME'] = $this->getTitle(); |
||
537 | $tags['ITEM_SUBNAME'] = $this->getSubtitle(); |
||
538 | $tags['CATEGORY_NAME'] = $this->getCategoryName(); |
||
539 | $tags['CATEGORY_URL'] = PUBLISHER_URL . '/category.php?categoryid=' . $this->categoryid(); |
||
540 | $tags['ITEM_BODY'] = $this->body(); |
||
541 | $tags['DATESUB'] = $this->getDatesub(); |
||
542 | foreach ($notifications as $notification) { |
||
543 | switch ($notification) { |
||
544 | case Constants::PUBLISHER_NOTIFY_ITEM_PUBLISHED: |
||
545 | $tags['ITEM_URL'] = PUBLISHER_URL . '/item.php?itemid=' . $this->itemid(); |
||
546 | $notificationHandler->triggerEvent('global_item', 0, 'published', $tags, [], $this->helper->getModule()->getVar('mid')); |
||
547 | $notificationHandler->triggerEvent('category_item', $this->categoryid(), 'published', $tags, [], $this->helper->getModule()->getVar('mid')); |
||
548 | $notificationHandler->triggerEvent('item', $this->itemid(), 'approved', $tags, [], $this->helper->getModule()->getVar('mid')); |
||
549 | break; |
||
550 | case Constants::PUBLISHER_NOTIFY_ITEM_SUBMITTED: |
||
551 | $tags['WAITINGFILES_URL'] = PUBLISHER_URL . '/admin/item.php?itemid=' . $this->itemid(); |
||
552 | $notificationHandler->triggerEvent('global_item', 0, 'submitted', $tags, [], $this->helper->getModule()->getVar('mid')); |
||
553 | $notificationHandler->triggerEvent('category_item', $this->categoryid(), 'submitted', $tags, [], $this->helper->getModule()->getVar('mid')); |
||
554 | break; |
||
555 | case Constants::PUBLISHER_NOTIFY_ITEM_REJECTED: |
||
556 | $notificationHandler->triggerEvent('item', $this->itemid(), 'rejected', $tags, [], $this->helper->getModule()->getVar('mid')); |
||
557 | break; |
||
558 | case -1: |
||
559 | default: |
||
560 | break; |
||
561 | } |
||
562 | } |
||
563 | } |
||
564 | |||
565 | /** |
||
566 | * Sets default permissions for this item |
||
567 | */ |
||
568 | public function setDefaultPermissions() |
||
569 | { |
||
570 | $memberHandler = \xoops_getHandler('member'); |
||
571 | $groups = $memberHandler->getGroupList(); |
||
572 | $groupIds = 0 < \count($groups) ? \array_keys($groups) : []; |
||
573 | /* |
||
574 | $j = 0; |
||
575 | $groupIds = []; |
||
576 | foreach (array_keys($groups) as $i) { |
||
577 | $groupIds[$j] = $i; |
||
578 | ++$j; |
||
579 | } |
||
580 | */ |
||
581 | $this->groupsRead = $groupIds; |
||
582 | } |
||
583 | |||
584 | /** |
||
585 | * @param $groupIds |
||
586 | * @deprecated - NOT USED |
||
587 | * |
||
588 | * @todo look at this |
||
589 | */ |
||
590 | public function setPermissions($groupIds) |
||
594 | /* |
||
595 | $memberHandler = xoops_getHandler('member'); |
||
596 | $groups = $memberHandler->getGroupList(); |
||
597 | $j = 0; |
||
598 | $groupIds = []; |
||
599 | foreach (array_keys($groups) as $i) { |
||
600 | $groupIds[$j] = $i; |
||
601 | ++$j; |
||
602 | } |
||
603 | */ |
||
604 | } |
||
605 | } |
||
606 | |||
607 | /** |
||
608 | * @return bool |
||
609 | */ |
||
610 | public function notLoaded() |
||
611 | { |
||
612 | return -1 == $this->getVar('itemid'); |
||
613 | } |
||
614 | |||
615 | /** |
||
616 | * @return string |
||
617 | */ |
||
618 | public function getItemUrl() |
||
619 | { |
||
620 | return Publisher\Seo::generateUrl('item', $this->itemid(), $this->short_url()); |
||
621 | } |
||
622 | |||
623 | /** |
||
624 | * @param bool $class |
||
625 | * @param int $maxsize |
||
626 | * |
||
627 | * @return string |
||
628 | */ |
||
629 | public function getItemLink($class = false, $maxsize = 0) |
||
630 | { |
||
631 | if ($class) { |
||
632 | return '<a class="' . $class . '" href="' . $this->getItemUrl() . '">' . $this->getTitle($maxsize) . '</a>'; |
||
633 | } |
||
634 | |||
635 | return '<a href="' . $this->getItemUrl() . '">' . $this->getTitle($maxsize) . '</a>'; |
||
636 | } |
||
637 | |||
638 | /** |
||
639 | * @return string |
||
640 | */ |
||
641 | public function getWhoAndWhen() |
||
642 | { |
||
643 | $posterName = $this->getLinkedPosterName(); |
||
644 | $postdate = $this->getDatesub(); |
||
645 | |||
646 | return \sprintf(\_CO_PUBLISHER_POSTEDBY, $posterName, $postdate); |
||
647 | } |
||
648 | |||
649 | /** |
||
650 | * @return string |
||
651 | */ |
||
652 | public function getWho() |
||
653 | { |
||
654 | $posterName = $this->getLinkedPosterName(); |
||
655 | |||
656 | return $posterName; |
||
657 | } |
||
658 | |||
659 | /** |
||
660 | * @return string |
||
661 | */ |
||
662 | public function getWhen() |
||
663 | { |
||
664 | $postdate = $this->getDatesub(); |
||
665 | |||
666 | return $postdate; |
||
667 | } |
||
668 | |||
669 | /** |
||
670 | * @param null|string $body |
||
671 | * |
||
672 | * @return string |
||
673 | */ |
||
674 | public function plainMaintext($body = null) |
||
675 | { |
||
676 | $ret = ''; |
||
677 | if (!$body) { |
||
678 | $body = $this->body(); |
||
679 | } |
||
680 | $ret .= \str_replace('[pagebreak]', '<br><br>', $body); |
||
681 | |||
682 | return $ret; |
||
683 | } |
||
684 | |||
685 | /** |
||
686 | * @param int $itemPageId |
||
687 | * @param null|string $body |
||
688 | * |
||
689 | * @return string |
||
690 | */ |
||
691 | public function buildMainText($itemPageId = -1, $body = null) |
||
692 | { |
||
693 | if (null === $body) { |
||
694 | $body = $this->body(); |
||
695 | } |
||
696 | $bodyParts = \explode('[pagebreak]', $body); |
||
697 | $this->setVar('pagescount', \count($bodyParts)); |
||
698 | if (\count($bodyParts) <= 1) { |
||
699 | return $this->plainMaintext($body); |
||
700 | } |
||
701 | $ret = ''; |
||
702 | if (-1 == $itemPageId) { |
||
703 | $ret .= \trim($bodyParts[0]); |
||
704 | |||
705 | return $ret; |
||
706 | } |
||
707 | if ($itemPageId >= \count($bodyParts)) { |
||
708 | $itemPageId = \count($bodyParts) - 1; |
||
709 | } |
||
710 | $ret .= \trim($bodyParts[$itemPageId]); |
||
711 | |||
712 | return $ret; |
||
713 | } |
||
714 | |||
715 | /** |
||
716 | * @return mixed |
||
717 | */ |
||
718 | public function getImages() |
||
719 | { |
||
720 | static $ret; |
||
721 | $itemid = $this->getVar('itemid'); |
||
722 | if (!isset($ret[$itemid])) { |
||
723 | $ret[$itemid]['main'] = ''; |
||
724 | $ret[$itemid]['others'] = []; |
||
725 | $imagesIds = []; |
||
726 | $image = $this->getVar('image'); |
||
727 | $images = $this->getVar('images'); |
||
728 | if ('' != $images) { |
||
729 | $imagesIds = \explode('|', $images); |
||
730 | } |
||
731 | if ($image > 0) { |
||
732 | $imagesIds = \array_merge($imagesIds, [$image]); |
||
733 | } |
||
734 | $imageObjs = []; |
||
735 | if (\count($imagesIds) > 0) { |
||
736 | $imageHandler = \xoops_getHandler('image'); |
||
737 | $criteria = new \CriteriaCompo(new \Criteria('image_id', '(' . \implode(',', $imagesIds) . ')', 'IN')); |
||
738 | $imageObjs = $imageHandler->getObjects($criteria, true); |
||
739 | unset($criteria); |
||
740 | } |
||
741 | foreach ($imageObjs as $id => $imageObj) { |
||
742 | if ($id == $image) { |
||
743 | $ret[$itemid]['main'] = $imageObj; |
||
744 | } else { |
||
745 | $ret[$itemid]['others'][] = $imageObj; |
||
746 | } |
||
747 | unset($imageObj); |
||
748 | } |
||
749 | unset($imageObjs); |
||
750 | } |
||
751 | |||
752 | return $ret[$itemid]; |
||
753 | } |
||
754 | |||
755 | /** |
||
756 | * @param string $display |
||
757 | * @param int $maxCharTitle |
||
758 | * @param int $maxCharSummary |
||
759 | * @param bool $fullSummary |
||
760 | * |
||
761 | * @return array |
||
762 | */ |
||
763 | public function toArraySimple($display = 'default', $maxCharTitle = 0, $maxCharSummary = 300, $fullSummary = false) |
||
764 | { |
||
765 | $itemPageId = -1; |
||
766 | if (\is_numeric($display)) { |
||
767 | $itemPageId = $display; |
||
768 | $display = 'all'; |
||
769 | } |
||
770 | $item['itemid'] = $this->itemid(); |
||
771 | $item['uid'] = $this->uid(); |
||
772 | $item['itemurl'] = $this->getItemUrl(); |
||
773 | $item['titlelink'] = $this->getItemLink('titlelink', $maxCharTitle); |
||
774 | $item['subtitle'] = $this->subtitle(); |
||
775 | $item['datesub'] = $this->getDatesub(); |
||
776 | $item['dateexpire'] = $this->getDateExpire(); |
||
777 | $item['counter'] = $this->counter(); |
||
778 | $item['hits'] = ' ' . $this->counter() . ' ' . _READS . ''; |
||
779 | $item['who'] = $this->getWho(); |
||
780 | $item['when'] = $this->getWhen(); |
||
781 | $item['category'] = $this->getCategoryName(); |
||
782 | $item['categorylink'] = $this->getCategoryLink(); |
||
783 | $item['cancomment'] = $this->cancomment(); |
||
784 | $comments = $this->comments(); |
||
785 | if ($comments > 0) { |
||
786 | //shows 1 comment instead of 1 comm. if comments ==1 |
||
787 | //langugage file modified accordingly |
||
788 | if (1 == $comments) { |
||
789 | $item['comments'] = ' ' . \_MD_PUBLISHER_ONECOMMENT . ' '; |
||
790 | } else { |
||
791 | $item['comments'] = ' ' . $comments . ' ' . \_MD_PUBLISHER_COMMENTS . ' '; |
||
792 | } |
||
793 | } else { |
||
794 | $item['comments'] = ' ' . \_MD_PUBLISHER_NO_COMMENTS . ' '; |
||
795 | } |
||
796 | $item = $this->getMainImage($item); |
||
797 | switch ($display) { |
||
798 | case 'summary': |
||
799 | $item = $this->toArrayFull($item); |
||
800 | $item = $this->toArrayAll($item, $itemPageId); |
||
801 | case 'list': |
||
802 | $item = $this->toArrayFull($item); |
||
803 | $item = $this->toArrayAll($item, $itemPageId); |
||
804 | //break; |
||
805 | case 'full': |
||
806 | $item = $this->toArrayFull($item); |
||
807 | $item = $this->toArrayAll($item, $itemPageId); |
||
808 | case 'wfsection': |
||
809 | $item = $this->toArrayFull($item); |
||
810 | $item = $this->toArrayAll($item, $itemPageId); |
||
811 | case 'default': |
||
812 | $item = $this->toArrayFull($item); |
||
813 | $item = $this->toArrayAll($item, $itemPageId); |
||
814 | $summary = $this->getSummary($maxCharSummary); |
||
815 | if (!$summary) { |
||
816 | $summary = $this->getBody($maxCharSummary); |
||
817 | } |
||
818 | $item['summary'] = $summary; |
||
819 | $item['truncated'] = $maxCharSummary > 0 && mb_strlen($summary) > $maxCharSummary; |
||
820 | $item = $this->toArrayFull($item); |
||
821 | break; |
||
822 | case 'all': |
||
823 | $item = $this->toArrayFull($item); |
||
824 | $item = $this->toArrayAll($item, $itemPageId); |
||
825 | break; |
||
826 | } |
||
827 | // Highlighting searched words |
||
828 | $highlight = true; |
||
829 | if ($highlight && Request::getString('keywords', '', 'GET')) { |
||
830 | $myts = \MyTextSanitizer::getInstance(); |
||
831 | $keywords = $myts->htmlSpecialChars(\trim(\urldecode(Request::getString('keywords', '', 'GET')))); |
||
832 | $fields = ['title', 'maintext', 'summary']; |
||
833 | foreach ($fields as $field) { |
||
834 | if (isset($item[$field])) { |
||
835 | $item[$field] = $this->highlight($item[$field], $keywords); |
||
836 | } |
||
837 | } |
||
838 | } |
||
839 | |||
840 | return $item; |
||
841 | } |
||
842 | |||
843 | /** |
||
844 | * @param array $item |
||
845 | * |
||
846 | * @return array |
||
847 | */ |
||
848 | public function toArrayFull($item) |
||
849 | { |
||
850 | $item['title'] = $this->getTitle(); |
||
851 | $item['clean_title'] = $this->getTitle(); |
||
852 | $item['itemurl'] = $this->getItemUrl(); |
||
853 | |||
854 | $item['adminlink'] = $this->getAdminLinks(); |
||
855 | $item['pdfbutton'] = $this->getPdfButton(); |
||
856 | $item['printlink'] = $this->getPrintLinks(); |
||
857 | $item['categoryPath'] = $this->getCategoryPath($this->helper->getConfig('format_linked_path')); |
||
858 | $item['who_when'] = $this->getWhoAndWhen(); |
||
859 | $item['who'] = $this->getWho(); |
||
860 | $item['when'] = $this->getWhen(); |
||
861 | $item['category'] = $this->getCategoryName(); |
||
862 | $item['body'] = $this->getBody(); |
||
863 | $item['more'] = $this->getItemUrl(); |
||
864 | $item = $this->getMainImage($item); |
||
865 | |||
866 | return $item; |
||
867 | } |
||
868 | |||
869 | /** |
||
870 | * @param array $item |
||
871 | * @param int $itemPageId |
||
872 | * |
||
873 | * @return array |
||
874 | */ |
||
875 | public function toArrayAll($item, $itemPageId) |
||
876 | { |
||
877 | $item['maintext'] = $this->buildMainText($itemPageId, $this->getBody()); |
||
878 | $item = $this->getOtherImages($item); |
||
879 | |||
880 | return $item; |
||
881 | } |
||
882 | |||
883 | /** |
||
884 | * @param array $item |
||
885 | * |
||
886 | * @return array |
||
887 | */ |
||
888 | public function getMainImage($item = []) |
||
889 | { |
||
890 | $images = $this->getImages(); |
||
891 | $item['image_path'] = ''; |
||
892 | $item['image_name'] = ''; |
||
893 | if (\is_object($images['main'])) { |
||
894 | $dimensions = \getimagesize($GLOBALS['xoops']->path('uploads/' . $images['main']->getVar('image_name'))); |
||
895 | $item['image_width'] = $dimensions[0]; |
||
896 | $item['image_height'] = $dimensions[1]; |
||
897 | $item['image_path'] = XOOPS_URL . '/uploads/' . $images['main']->getVar('image_name'); |
||
898 | // check to see if GD function exist |
||
899 | if (!\function_exists('imagecreatetruecolor')) { |
||
900 | $item['image_thumb'] = XOOPS_URL . '/uploads/' . $images['main']->getVar('image_name'); |
||
901 | } else { |
||
902 | $item['image_thumb'] = PUBLISHER_URL . '/thumb.php?src=' . XOOPS_URL . '/uploads/' . $images['main']->getVar('image_name') . '&h=180'; |
||
903 | } |
||
904 | $item['image_name'] = $images['main']->getVar('image_nicename'); |
||
905 | } |
||
906 | |||
907 | return $item; |
||
908 | } |
||
909 | |||
910 | /** |
||
911 | * @param array $item |
||
912 | * |
||
913 | * @return array |
||
914 | */ |
||
915 | public function getOtherImages($item = []) |
||
916 | { |
||
917 | $images = $this->getImages(); |
||
918 | $item['images'] = []; |
||
919 | $i = 0; |
||
920 | foreach ($images['others'] as $image) { |
||
921 | $dimensions = \getimagesize($GLOBALS['xoops']->path('uploads/' . $image->getVar('image_name'))); |
||
922 | $item['images'][$i]['width'] = $dimensions[0]; |
||
923 | $item['images'][$i]['height'] = $dimensions[1]; |
||
924 | $item['images'][$i]['path'] = XOOPS_URL . '/uploads/' . $image->getVar('image_name'); |
||
925 | // check to see if GD function exist |
||
926 | if (!\function_exists('imagecreatetruecolor')) { |
||
927 | $item['images'][$i]['thumb'] = XOOPS_URL . '/uploads/' . $image->getVar('image_name'); |
||
928 | } else { |
||
929 | $item['images'][$i]['thumb'] = PUBLISHER_URL . '/thumb.php?src=' . XOOPS_URL . '/uploads/' . $image->getVar('image_name') . '&w=240'; |
||
930 | } |
||
931 | $item['images'][$i]['name'] = $image->getVar('image_nicename'); |
||
932 | ++$i; |
||
933 | } |
||
934 | |||
935 | return $item; |
||
936 | } |
||
937 | |||
938 | /** |
||
939 | * @param string $content |
||
940 | * @param string|array $keywords |
||
941 | * |
||
942 | * @return string Text |
||
943 | */ |
||
944 | public function highlight($content, $keywords) |
||
945 | { |
||
946 | $color = $this->helper->getConfig('format_highlight_color'); |
||
947 | if (0 !== mb_strpos($color, '#')) { |
||
948 | $color = '#' . $color; |
||
949 | } |
||
950 | require_once __DIR__ . '/Highlighter.php'; |
||
951 | $highlighter = new Highlighter(); |
||
952 | $highlighter->setReplacementString('<span style="font-weight: bolder; background-color: ' . $color . ';">\1</span>'); |
||
953 | |||
954 | return $highlighter->highlight($content, $keywords); |
||
955 | } |
||
956 | |||
957 | /** |
||
958 | * Create metada and assign it to template |
||
959 | */ |
||
960 | public function createMetaTags() |
||
961 | { |
||
962 | $publisherMetagen = new Publisher\Metagen($this->getTitle(), $this->meta_keywords(), $this->meta_description(), $this->category->categoryPath); |
||
963 | $publisherMetagen->createMetaTags(); |
||
964 | } |
||
965 | |||
966 | /** |
||
967 | * @param string $str |
||
968 | * |
||
969 | * @return string |
||
970 | */ |
||
971 | protected function convertForJapanese($str) |
||
972 | { |
||
973 | // no action, if not flag |
||
974 | if (!\defined('_PUBLISHER_FLAG_JP_CONVERT')) { |
||
975 | return $str; |
||
976 | } |
||
977 | // no action, if not Japanese |
||
978 | if ('japanese' !== $GLOBALS['xoopsConfig']['language']) { |
||
979 | return $str; |
||
980 | } |
||
981 | // presume OS Browser |
||
982 | $agent = Request::getString('HTTP_USER_AGENT', '', 'SERVER'); |
||
983 | $os = ''; |
||
984 | $browser = ''; |
||
985 | // if (preg_match("/Win/i", $agent)) { |
||
986 | if (false !== mb_stripos($agent, 'Win')) { |
||
987 | $os = 'win'; |
||
988 | } |
||
989 | // if (preg_match("/MSIE/i", $agent)) { |
||
990 | if (false !== mb_stripos($agent, 'MSIE')) { |
||
991 | $browser = 'msie'; |
||
992 | } |
||
993 | // if msie |
||
994 | if (('win' === $os) && ('msie' === $browser)) { |
||
995 | // if multibyte |
||
996 | if (\function_exists('mb_convert_encoding')) { |
||
997 | $str = mb_convert_encoding($str, 'SJIS', 'EUC-JP'); |
||
998 | $str = \rawurlencode($str); |
||
999 | } |
||
1000 | } |
||
1001 | |||
1002 | return $str; |
||
1003 | } |
||
1004 | |||
1005 | /** |
||
1006 | * @param string $title |
||
1007 | * @param bool $checkperm |
||
1008 | * |
||
1009 | * @return \XoopsModules\Publisher\Form\ItemForm |
||
1010 | */ |
||
1011 | public function getForm($title = 'default', $checkperm = true) |
||
1019 | } |
||
1020 | |||
1021 | /** |
||
1022 | * Checks if a user has access to a selected item. if no item permissions are |
||
1023 | * set, access permission is denied. The user needs to have necessary category |
||
1024 | * permission as well. |
||
1025 | * Also, the item needs to be Published |
||
1026 | * |
||
1027 | * @return bool : TRUE if the no errors occured |
||
1028 | */ |
||
1029 | public function accessGranted() |
||
1030 | { |
||
1031 | if (Publisher\Utility::userIsAdmin()) { |
||
1032 | return true; |
||
1033 | } |
||
1034 | if (Constants::PUBLISHER_STATUS_PUBLISHED != $this->getVar('status')) { |
||
1035 | return false; |
||
1036 | } |
||
1037 | // Do we have access to the parent category |
||
1038 | if ($this->helper->getHandler('Permission')->isGranted('category_read', $this->categoryid())) { |
||
1039 | return true; |
||
1040 | } |
||
1041 | |||
1042 | return false; |
||
1043 | } |
||
1044 | |||
1045 | /** |
||
1046 | * The name says it all |
||
1047 | */ |
||
1048 | public function setVarsFromRequest() |
||
1184 | } |
||
1185 | } |
||
1186 |