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