Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like PublisherItem 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 PublisherItem, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class PublisherItem extends XoopsObject |
||
32 | { |
||
33 | /** |
||
34 | * @var Publisher |
||
35 | * @access public |
||
36 | */ |
||
37 | public $publisher = null; |
||
38 | |||
39 | /** |
||
40 | * @var PublisherCategory |
||
41 | * @access public |
||
42 | */ |
||
43 | public $_category = null; |
||
44 | |||
45 | /** |
||
46 | * @param int|null $id |
||
47 | */ |
||
48 | public function __construct($id = null) |
||
49 | { |
||
50 | $this->publisher = Publisher::getInstance(); |
||
51 | $this->initVar("itemid", XOBJ_DTYPE_INT, 0); |
||
52 | $this->initVar("categoryid", XOBJ_DTYPE_INT, 0, false); |
||
53 | $this->initVar("title", XOBJ_DTYPE_TXTBOX, '', true, 255); |
||
54 | $this->initVar("subtitle", XOBJ_DTYPE_TXTBOX, '', false, 255); |
||
55 | $this->initVar("summary", XOBJ_DTYPE_TXTAREA, '', false); |
||
56 | $this->initVar("body", XOBJ_DTYPE_TXTAREA, '', false); |
||
57 | $this->initVar("uid", XOBJ_DTYPE_INT, 0, false); |
||
58 | $this->initVar("author_alias", XOBJ_DTYPE_TXTBOX, '', false, 255); |
||
59 | $this->initVar("datesub", XOBJ_DTYPE_INT, '', false); |
||
60 | $this->initVar("status", XOBJ_DTYPE_INT, -1, false); |
||
61 | $this->initVar("image", XOBJ_DTYPE_INT, 0, false); |
||
62 | $this->initVar("images", XOBJ_DTYPE_TXTBOX, '', false, 255); |
||
63 | $this->initVar("counter", XOBJ_DTYPE_INT, 0, false); |
||
64 | $this->initVar("rating", XOBJ_DTYPE_OTHER, 0, false); |
||
65 | $this->initVar("votes", XOBJ_DTYPE_INT, 0, false); |
||
66 | $this->initVar("weight", XOBJ_DTYPE_INT, 0, false); |
||
67 | $this->initVar("dohtml", XOBJ_DTYPE_INT, 1, true); |
||
68 | $this->initVar("dosmiley", XOBJ_DTYPE_INT, 1, true); |
||
69 | $this->initVar("doimage", XOBJ_DTYPE_INT, 1, true); |
||
70 | $this->initVar("dobr", XOBJ_DTYPE_INT, 1, false); |
||
71 | $this->initVar("doxcode", XOBJ_DTYPE_INT, 1, true); |
||
72 | $this->initVar("cancomment", XOBJ_DTYPE_INT, 1, true); |
||
73 | $this->initVar("comments", XOBJ_DTYPE_INT, 0, false); |
||
74 | $this->initVar("notifypub", XOBJ_DTYPE_INT, 1, false); |
||
75 | $this->initVar("meta_keywords", XOBJ_DTYPE_TXTAREA, '', false); |
||
76 | $this->initVar("meta_description", XOBJ_DTYPE_TXTAREA, '', false); |
||
77 | $this->initVar("short_url", XOBJ_DTYPE_TXTBOX, '', false, 255); |
||
78 | $this->initVar("item_tag", XOBJ_DTYPE_TXTAREA, '', false); |
||
79 | // Non consistent values |
||
80 | $this->initVar("pagescount", XOBJ_DTYPE_INT, 0, false); |
||
81 | if (isset($id)) { |
||
82 | $item = $this->publisher->getItemHandler()->get($id); |
||
83 | foreach ($item->vars as $k => $v) { |
||
84 | $this->assignVar($k, $v['value']); |
||
85 | } |
||
86 | } |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @return null|PublisherCategory |
||
91 | */ |
||
92 | public function category() |
||
93 | { |
||
94 | if (!isset($this->_category)) { |
||
95 | $this->_category = $this->publisher->getCategoryHandler()->get($this->getVar('categoryid')); |
||
96 | } |
||
97 | return $this->_category; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param int $maxLength |
||
102 | * @param string $format |
||
103 | * |
||
104 | * @return string |
||
105 | */ |
||
106 | public function title($maxLength = 0, $format = "S") |
||
107 | { |
||
108 | $ret = $this->getVar("title", $format); |
||
109 | if ($maxLength != 0) { |
||
110 | if (!XoopsLocale::isMultiByte()) { |
||
111 | if (strlen($ret) >= $maxLength) { |
||
112 | $ret = PublisherUtils::substr($ret, 0, $maxLength); |
||
113 | } |
||
114 | } |
||
115 | } |
||
116 | return $ret; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @param int $maxLength |
||
121 | * @param string $format |
||
122 | * |
||
123 | * @return mixed|string |
||
124 | */ |
||
125 | public function subtitle($maxLength = 0, $format = "S") |
||
126 | { |
||
127 | $ret = $this->getVar("subtitle", $format); |
||
128 | if ($maxLength != 0) { |
||
129 | if (!XoopsLocale::isMultiByte()) { |
||
130 | if (strlen($ret) >= $maxLength) { |
||
131 | $ret = PublisherUtils::substr($ret, 0, $maxLength); |
||
132 | } |
||
133 | } |
||
134 | } |
||
135 | return $ret; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @param int $maxLength |
||
140 | * @param string $format |
||
141 | * @param string $stripTags |
||
142 | * |
||
143 | * @return mixed|string |
||
144 | */ |
||
145 | public function summary($maxLength = 0, $format = "S", $stripTags = '') |
||
146 | { |
||
147 | $ret = $this->getVar("summary", $format); |
||
148 | if (!empty($stripTags)) { |
||
149 | $ret = strip_tags($ret, $stripTags); |
||
150 | } |
||
151 | if ($maxLength != 0) { |
||
152 | if (!XoopsLocale::isMultiByte()) { |
||
153 | if (strlen($ret) >= $maxLength) { |
||
154 | //$ret = PublisherUtils::substr($ret , 0, $maxLength); |
||
155 | $ret = PublisherUtils::truncateTagSafe($ret, $maxLength, $etc = '...', $break_words = false); |
||
156 | } |
||
157 | } |
||
158 | } |
||
159 | return $ret; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @param int $maxLength |
||
164 | * @param bool $fullSummary |
||
165 | * |
||
166 | * @return mixed|string |
||
167 | */ |
||
168 | public function getBlockSummary($maxLength = 0, $fullSummary = false) |
||
169 | { |
||
170 | if ($fullSummary) { |
||
171 | $ret = $this->summary(0, 's', '<br></ br>'); |
||
172 | } else { |
||
173 | $ret = $this->summary($maxLength, 's', '<br></ br>'); |
||
174 | } |
||
175 | //no summary? get body! |
||
176 | if (strlen($ret) == 0) { |
||
177 | $ret = $this->body($maxLength, 's', '<br></ br>'); |
||
178 | } |
||
179 | return $ret; |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * @param string $file_name |
||
184 | * |
||
185 | * @return string |
||
186 | */ |
||
187 | public function wrappage($file_name) |
||
188 | { |
||
189 | $content = ''; |
||
190 | $page = PublisherUtils::getUploadDir(true, 'content') . $file_name; |
||
191 | if (XoopsLoad::fileExists($page)) { |
||
192 | // this page uses smarty template |
||
193 | ob_start(); |
||
194 | include($page); |
||
195 | $content = ob_get_contents(); |
||
196 | ob_end_clean(); |
||
197 | // Cleaning the content |
||
198 | $body_start_pos = strpos($content, '<body>'); |
||
199 | if ($body_start_pos) { |
||
200 | $body_end_pos = strpos($content, '</body>', $body_start_pos); |
||
201 | $content = substr($content, $body_start_pos + strlen('<body>'), $body_end_pos - strlen('<body>') - $body_start_pos); |
||
202 | } |
||
203 | // Check if ML Hack is installed, and if yes, parse the $content in formatForML |
||
204 | $myts = \Xoops\Core\Text\Sanitizer::getInstance(); |
||
205 | if (method_exists($myts, 'formatForML')) { |
||
206 | $content = $myts->formatForML($content); |
||
207 | } |
||
208 | } |
||
209 | return $content; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * This method returns the body to be displayed. Not to be used for editing |
||
214 | * |
||
215 | * @param int $maxLength |
||
216 | * @param string $format |
||
217 | * @param string $stripTags |
||
218 | * |
||
219 | * @return mixed|string |
||
220 | */ |
||
221 | public function body($maxLength = 0, $format = 'S', $stripTags = '') |
||
222 | { |
||
223 | $ret = $this->getVar('body', $format); |
||
224 | $wrap_pos = strpos($ret, '[pagewrap='); |
||
225 | if (!($wrap_pos === false)) { |
||
226 | $wrap_pages = array(); |
||
227 | $wrap_code_length = strlen('[pagewrap='); |
||
228 | while (!($wrap_pos === false)) { |
||
229 | $end_wrap_pos = strpos($ret, ']', $wrap_pos); |
||
230 | if ($end_wrap_pos) { |
||
231 | $wrap_page_name = substr($ret, $wrap_pos + $wrap_code_length, $end_wrap_pos - $wrap_code_length - $wrap_pos); |
||
232 | $wrap_pages[] = $wrap_page_name; |
||
233 | } |
||
234 | $wrap_pos = strpos($ret, '[pagewrap=', $end_wrap_pos - 1); |
||
235 | } |
||
236 | foreach ($wrap_pages as $page) { |
||
237 | $wrap_page_content = $this->wrappage($page); |
||
238 | $ret = str_replace("[pagewrap={$page}]", $wrap_page_content, $ret); |
||
239 | } |
||
240 | } |
||
241 | if ($this->publisher->getConfig('item_disp_blocks_summary')) { |
||
242 | $summary = $this->summary($maxLength, $format, $stripTags); |
||
243 | if ($summary) { |
||
244 | $ret = $this->summary() . '<br /><br />' . $ret; |
||
245 | } |
||
246 | } |
||
247 | if (!empty($stripTags)) { |
||
248 | $ret = strip_tags($ret, $stripTags); |
||
249 | } |
||
250 | if ($maxLength != 0) { |
||
251 | if (!XoopsLocale::isMultiByte()) { |
||
252 | if (strlen($ret) >= $maxLength) { |
||
253 | //$ret = PublisherUtils::substr($ret , 0, $maxLength); |
||
254 | $ret = PublisherUtils::truncateTagSafe($ret, $maxLength, $etc = '...', $break_words = false); |
||
255 | } |
||
256 | } |
||
257 | } |
||
258 | return $ret; |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * @param string $dateFormat |
||
263 | * @param string $format |
||
264 | * |
||
265 | * @return string |
||
266 | */ |
||
267 | public function datesub($dateFormat = '', $format = 'S') |
||
268 | { |
||
269 | if (empty($dateformat)) { |
||
270 | $dateFormat = $this->publisher->getConfig('format_date'); |
||
271 | } |
||
272 | return XoopsLocale::formatTimestamp($this->getVar('datesub', $format), $dateFormat); |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * @param int $realName |
||
277 | * |
||
278 | * @return string |
||
279 | */ |
||
280 | public function posterName($realName = -1) |
||
281 | { |
||
282 | if ($realName == -1) { |
||
283 | $realName = $this->publisher->getConfig('format_realname'); |
||
284 | } |
||
285 | $ret = $this->getVar('author_alias'); |
||
286 | if ($ret == '') { |
||
287 | $ret = XoopsUserUtility::getUnameFromId($this->getVar('uid'), $realName); |
||
288 | } |
||
289 | return $ret; |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * @return string |
||
294 | */ |
||
295 | public function posterAvatar() |
||
296 | { |
||
297 | $xoops = Xoops::getInstance(); |
||
298 | $ret = 'blank.gif'; |
||
299 | $member_handler = $xoops->getHandlerMember(); |
||
300 | $thisUser = $member_handler->getUser($this->getVar('uid')); |
||
301 | if (is_object($thisUser)) { |
||
302 | $ret = $xoops->service('avatar')->getAvatarUrl($thisUser)->getValue(); |
||
303 | } |
||
304 | return $ret; |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * @return string |
||
309 | */ |
||
310 | public function linkedPosterName() |
||
311 | { |
||
312 | $ret = $this->getVar('author_alias'); |
||
313 | if ($ret == '') { |
||
314 | $ret = XoopsUserUtility::getUnameFromId($this->getVar('uid'), $this->publisher->getConfig('format_realname'), true); |
||
315 | } |
||
316 | return $ret; |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * @return mixed |
||
321 | */ |
||
322 | public function updateCounter() |
||
323 | { |
||
324 | return $this->publisher->getItemHandler()->updateCounter($this->getVar('itemid')); |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * @param bool $force |
||
329 | * |
||
330 | * @return bool |
||
331 | */ |
||
332 | public function store($force = true) |
||
333 | { |
||
334 | $xoops = Xoops::getInstance(); |
||
335 | $isNew = $this->isNew(); |
||
336 | if (!$this->publisher->getItemHandler()->insert($this, $force)) { |
||
337 | return false; |
||
338 | } |
||
339 | if ($isNew && $this->getVar('status') == _PUBLISHER_STATUS_PUBLISHED) { |
||
340 | // Increment user posts |
||
341 | $user_handler = $xoops->getHandlerUser(); |
||
342 | $member_handler = $xoops->getHandlerMember(); |
||
343 | $poster = $user_handler->get($this->getVar('uid')); |
||
344 | if (is_object($poster) && !$poster->isNew()) { |
||
345 | $poster->setVar('posts', $poster->getVar('posts') + 1); |
||
346 | if (!$member_handler->insertUser($poster, true)) { |
||
347 | $this->setErrors('Article created but could not increment user posts.'); |
||
348 | return false; |
||
349 | } |
||
350 | } |
||
351 | } |
||
352 | return true; |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * @return string |
||
357 | */ |
||
358 | public function getCategoryName() |
||
359 | { |
||
360 | return $this->category()->getVar('name'); |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * @return string |
||
365 | */ |
||
366 | public function getCategoryUrl() |
||
367 | { |
||
368 | return $this->category()->getCategoryUrl(); |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * @return string |
||
373 | */ |
||
374 | public function getCategoryLink() |
||
375 | { |
||
376 | return $this->category()->getCategoryLink(); |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * @param bool $withAllLink |
||
381 | * |
||
382 | * @return string |
||
383 | */ |
||
384 | public function getCategoryPath($withAllLink = true) |
||
385 | { |
||
386 | return $this->category()->getCategoryPath($withAllLink); |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * @return string |
||
391 | */ |
||
392 | public function getCategoryImagePath() |
||
393 | { |
||
394 | return PublisherUtils::getImageDir('category', false) . $this->category()->image(); |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * @return mixed |
||
399 | */ |
||
400 | public function getFiles() |
||
401 | { |
||
402 | return $this->publisher->getFileHandler()->getAllFiles($this->getVar('itemid'), _PUBLISHER_STATUS_FILE_ACTIVE); |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * @return string |
||
407 | */ |
||
408 | public function getAdminLinks() |
||
409 | { |
||
410 | $xoops = Xoops::getInstance(); |
||
411 | $adminLinks = ''; |
||
412 | if ($xoops->isUser() && (PublisherUtils::IsUserAdmin() || PublisherUtils::IsUserAuthor($this) || $this->publisher->getPermissionHandler()->isGranted('item_submit', $this->getVar('categoryid')))) { |
||
413 | if (PublisherUtils::IsUserAdmin() || PublisherUtils::IsUserAuthor($this) || PublisherUtils::IsUserModerator($this)) { |
||
414 | if ($this->publisher->getConfig('perm_edit') || PublisherUtils::IsUserModerator($this) || PublisherUtils::IsUserAdmin()) { |
||
415 | // Edit button |
||
416 | $adminLinks .= "<a href='" . PUBLISHER_URL . "/submit.php?itemid=" . $this->getVar('itemid') . "'><img src='" . PUBLISHER_URL . "/images/links/edit.gif'" . " title='" . _CO_PUBLISHER_EDIT . "' alt='" . _CO_PUBLISHER_EDIT . "'/></a>"; |
||
417 | $adminLinks .= " "; |
||
418 | } |
||
419 | if ($this->publisher->getConfig('perm_delete') || PublisherUtils::IsUserModerator($this) || PublisherUtils::IsUserAdmin()) { |
||
420 | // Delete button |
||
421 | $adminLinks .= "<a href='" . PUBLISHER_URL . "/submit.php?op=del&itemid=" . $this->getVar('itemid') . "'><img src='" . PUBLISHER_URL . "/images/links/delete.png'" . " title='" . _CO_PUBLISHER_DELETE . "' alt='" . _CO_PUBLISHER_DELETE . "' /></a>"; |
||
422 | $adminLinks .= " "; |
||
423 | } |
||
424 | } |
||
425 | if ($this->publisher->getConfig('perm_clone') || PublisherUtils::IsUserModerator($this) || PublisherUtils::IsUserAdmin()) { |
||
426 | // Duplicate button |
||
427 | $adminLinks .= "<a href='" . PUBLISHER_URL . "/submit.php?op=clone&itemid=" . $this->getVar('itemid') . "'><img src='" . PUBLISHER_URL . "/images/links/clone.gif'" . " title='" . _CO_PUBLISHER_CLONE . "' alt='" . _CO_PUBLISHER_CLONE . "' /></a>"; |
||
428 | $adminLinks .= " "; |
||
429 | } |
||
430 | } |
||
431 | // PDF button |
||
432 | if ($xoops->service('htmltopdf')->isAvailable()) { |
||
433 | $adminLinks .= "<a href='" . PUBLISHER_URL . "/makepdf.php?itemid=" . $this->getVar('itemid') . "' rel='nofollow' target='_blank'><img src='" . PUBLISHER_URL . "/images/links/pdf.gif' title='" . _CO_PUBLISHER_PDF . "' alt='" . _CO_PUBLISHER_PDF . "' /></a>"; |
||
434 | $adminLinks .= " "; |
||
435 | } |
||
436 | // Print button |
||
437 | $adminLinks .= "<a href='" . PublisherUtils::seoGenUrl("print", $this->getVar('itemid'), $this->getVar('short_url')) . "' rel='nofollow' target='_blank'><img src='" . PUBLISHER_URL . "/images/links/print.gif' title='" . _CO_PUBLISHER_PRINT . "' alt='" . _CO_PUBLISHER_PRINT . "' /></a>"; |
||
438 | $adminLinks .= " "; |
||
439 | // Email button |
||
440 | if ($xoops->isActiveModule('tellafriend')) { |
||
441 | $subject = sprintf(_CO_PUBLISHER_INTITEMFOUND, $xoops->getConfig('sitename')); |
||
442 | $subject = $this->_convert_for_japanese($subject); |
||
443 | $maillink = PublisherUtils::tellafriend($subject); |
||
444 | $adminLinks .= '<a href="' . $maillink . '"><img src="' . PUBLISHER_URL . '/images/links/friend.gif" title="' . _CO_PUBLISHER_MAIL . '" alt="' . _CO_PUBLISHER_MAIL . '" /></a>'; |
||
445 | $adminLinks .= " "; |
||
446 | } |
||
447 | return $adminLinks; |
||
448 | } |
||
449 | |||
450 | /** |
||
451 | * @param array $notifications |
||
452 | */ |
||
453 | public function sendNotifications($notifications = array()) |
||
454 | { |
||
455 | $xoops = Xoops::getInstance(); |
||
456 | if ($xoops->isActiveModule('notifications')) { |
||
457 | |||
458 | $notification_handler = Notifications::getInstance()->getHandlerNotification(); |
||
459 | $tags = array(); |
||
460 | $tags['MODULE_NAME'] = $this->publisher->getModule()->getVar('name'); |
||
461 | $tags['ITEM_NAME'] = $this->title(); |
||
462 | $tags['CATEGORY_NAME'] = $this->getCategoryName(); |
||
463 | $tags['CATEGORY_URL'] = PUBLISHER_URL . '/category.php?categoryid=' . $this->getVar('categoryid'); |
||
464 | $tags['ITEM_BODY'] = $this->body(); |
||
465 | $tags['DATESUB'] = $this->datesub(); |
||
466 | foreach ($notifications as $notification) { |
||
467 | switch ($notification) { |
||
468 | case _PUBLISHER_NOT_ITEM_PUBLISHED : |
||
469 | $tags['ITEM_URL'] = PUBLISHER_URL . '/item.php?itemid=' . $this->getVar('itemid'); |
||
470 | $notification_handler->triggerEvent('global', 0, 'published', $tags, array(), $this->publisher->getModule()->getVar('mid')); |
||
471 | $notification_handler->triggerEvent('category', $this->getVar('categoryid'), 'published', $tags, array(), $this->publisher->getModule()->getVar('mid')); |
||
472 | $notification_handler->triggerEvent('item', $this->getVar('itemid'), 'approved', $tags, array(), $this->publisher->getModule()->getVar('mid')); |
||
473 | break; |
||
474 | case _PUBLISHER_NOT_ITEM_SUBMITTED : |
||
475 | $tags['WAITINGFILES_URL'] = PUBLISHER_URL . '/admin/item.php?itemid=' . $this->getVar('itemid'); |
||
476 | $notification_handler->triggerEvent('global', 0, 'submitted', $tags, array(), $this->publisher->getModule()->getVar('mid')); |
||
477 | $notification_handler->triggerEvent('category', $this->getVar('categoryid'), 'submitted', $tags, array(), $this->publisher->getModule()->getVar('mid')); |
||
478 | break; |
||
479 | case _PUBLISHER_NOT_ITEM_REJECTED : |
||
480 | $notification_handler->triggerEvent('item', $this->getVar('itemid'), 'rejected', $tags, array(), $this->publisher->getModule()->getVar('mid')); |
||
481 | break; |
||
482 | case -1 : |
||
483 | default: |
||
484 | break; |
||
485 | } |
||
486 | } |
||
487 | } |
||
488 | } |
||
489 | |||
490 | /** |
||
491 | * @return bool |
||
492 | */ |
||
493 | public function notLoaded() |
||
494 | { |
||
495 | return $this->getVar('itemid') == -1; |
||
496 | } |
||
497 | |||
498 | /** |
||
499 | * @return string |
||
500 | */ |
||
501 | public function getItemUrl() |
||
502 | { |
||
503 | return PublisherUtils::seoGenUrl('item', $this->getVar('itemid'), $this->getVar('short_url')); |
||
504 | } |
||
505 | |||
506 | /** |
||
507 | * @param bool $class |
||
508 | * @param int $maxsize |
||
509 | * |
||
510 | * @return string |
||
511 | */ |
||
512 | public function getItemLink($class = false, $maxsize = 0) |
||
513 | { |
||
514 | if ($class) { |
||
515 | return '<a class=' . $class . ' href="' . $this->getItemUrl() . '">' . $this->title($maxsize) . '</a>'; |
||
516 | } else { |
||
517 | return '<a href="' . $this->getItemUrl() . '">' . $this->title($maxsize) . '</a>'; |
||
518 | } |
||
519 | } |
||
520 | |||
521 | /** |
||
522 | * @return string |
||
523 | */ |
||
524 | public function getWhoAndWhen() |
||
525 | { |
||
526 | $posterName = $this->linkedPosterName(); |
||
527 | $postdate = $this->datesub(); |
||
528 | return sprintf(_CO_PUBLISHER_POSTEDBY, $posterName, $postdate); |
||
529 | } |
||
530 | |||
531 | /** |
||
532 | * @param null|string $body |
||
533 | * |
||
534 | * @return string |
||
535 | */ |
||
536 | public function plain_maintext($body = null) |
||
537 | { |
||
538 | $ret = ''; |
||
539 | if (!$body) { |
||
540 | $body = $this->body(); |
||
541 | } |
||
542 | $ret .= str_replace('[pagebreak]', '<br /><br />', $body); |
||
543 | return $ret; |
||
544 | } |
||
545 | |||
546 | /** |
||
547 | * @param int $item_page_id |
||
548 | * @param null|string $body |
||
549 | * |
||
550 | * @return string |
||
551 | */ |
||
552 | public function buildmaintext($item_page_id = -1, $body = null) |
||
553 | { |
||
554 | if (!$body) { |
||
555 | $body = $this->body(); |
||
556 | } |
||
557 | $body_parts = explode('[pagebreak]', $body); |
||
558 | $this->setVar('pagescount', count($body_parts)); |
||
559 | if (count($body_parts) <= 1) { |
||
560 | return $this->plain_maintext($body); |
||
561 | } |
||
562 | $ret = ''; |
||
563 | if ($item_page_id == -1) { |
||
564 | $ret .= trim($body_parts[0]); |
||
565 | return $ret; |
||
566 | } |
||
567 | if ($item_page_id >= count($body_parts)) { |
||
568 | $item_page_id = count($body_parts) - 1; |
||
569 | } |
||
570 | $ret .= trim($body_parts[$item_page_id]); |
||
571 | return $ret; |
||
572 | } |
||
573 | |||
574 | /** |
||
575 | * @return mixed |
||
576 | */ |
||
577 | public function getImages() |
||
578 | { |
||
579 | static $ret; |
||
580 | |||
581 | $xoops = Xoops::getInstance(); |
||
582 | if (!$xoops->isActiveModule('images')) { |
||
583 | return array(); |
||
584 | } |
||
585 | $itemid = $this->getVar('itemid'); |
||
586 | if (!isset($ret[$itemid])) { |
||
587 | $ret[$itemid]['main'] = ''; |
||
588 | $ret[$itemid]['others'] = array(); |
||
589 | $images_ids = array(); |
||
590 | $image = $this->getVar('image'); |
||
591 | $images = $this->getVar('images'); |
||
592 | if ($images != '') { |
||
593 | $images_ids = explode('|', $images); |
||
594 | } |
||
595 | if ($image > 0) { |
||
596 | $images_ids = array_merge($images_ids, array($image)); |
||
597 | } |
||
598 | $imageObjs = array(); |
||
599 | if (count($images_ids) > 0) { |
||
600 | $image_handler = Images::getInstance()->getHandlerImages(); |
||
601 | $criteria = new CriteriaCompo(new Criteria('image_id', '(' . implode(',', $images_ids) . ')', 'IN')); |
||
602 | $imageObjs = $image_handler->getObjects($criteria, true); |
||
603 | unset($criteria); |
||
604 | } |
||
605 | foreach ($imageObjs as $id => $imageObj) { |
||
606 | if ($id == $image) { |
||
607 | $ret[$itemid]['main'] = $imageObj; |
||
608 | } else { |
||
609 | $ret[$itemid]['others'][] = $imageObj; |
||
610 | } |
||
611 | unset($imageObj); |
||
612 | } |
||
613 | unset($imageObjs); |
||
614 | } |
||
615 | return $ret[$itemid]; |
||
616 | } |
||
617 | |||
618 | /** |
||
619 | * @param string $display |
||
620 | * @param int $max_char_title |
||
621 | * @param int $max_char_summary |
||
622 | * @param bool $full_summary |
||
623 | * |
||
624 | * @return array |
||
625 | */ |
||
626 | public function toArray($display = 'default', $max_char_title = 0, $max_char_summary = 0, $full_summary = false) |
||
627 | { |
||
628 | $item_page_id = -1; |
||
629 | if (is_numeric($display)) { |
||
630 | $item_page_id = $display; |
||
631 | $display = 'all'; |
||
632 | } |
||
633 | $item['itemid'] = $this->getVar('itemid'); |
||
634 | $item['uid'] = $this->getVar('uid'); |
||
635 | $item['titlelink'] = $this->getItemLink(false, $max_char_title); |
||
636 | $item['subtitle'] = $this->subtitle(); |
||
637 | $item['datesub'] = $this->datesub(); |
||
638 | $item['counter'] = $this->getVar('counter'); |
||
639 | switch ($display) { |
||
640 | case 'summary': |
||
641 | case 'list': |
||
642 | break; |
||
643 | case 'full': |
||
644 | case 'wfsection': |
||
645 | case 'default': |
||
646 | $summary = $this->summary($max_char_summary); |
||
647 | if (!$summary) { |
||
648 | $summary = $this->body($max_char_summary); |
||
649 | } |
||
650 | $item['summary'] = $summary; |
||
651 | $item = $this->toArrayFull($item); |
||
652 | break; |
||
653 | case 'all': |
||
654 | $item = $this->toArrayFull($item); |
||
655 | $item = $this->toArrayAll($item, $item_page_id); |
||
656 | break; |
||
657 | } |
||
658 | // Highlighting searched words |
||
659 | $highlight = true; |
||
660 | if ($highlight && isset($_GET['keywords'])) { |
||
661 | $myts = \Xoops\Core\Text\Sanitizer::getInstance(); |
||
662 | $keywords = $myts->htmlSpecialChars(trim(urldecode($_GET['keywords']))); |
||
663 | $fields = array('title', 'maintext', 'summary'); |
||
664 | foreach ($fields as $field) { |
||
665 | if (isset($item[$field])) { |
||
666 | $item[$field] = $this->highlight($item[$field], $keywords); |
||
667 | } |
||
668 | } |
||
669 | } |
||
670 | return $item; |
||
671 | } |
||
672 | |||
673 | /** |
||
674 | * @param array $item |
||
675 | * |
||
676 | * @return array |
||
677 | */ |
||
678 | public function toArrayFull($item) |
||
679 | { |
||
680 | $item['title'] = $this->title(); |
||
681 | $item['clean_title'] = $this->title(); |
||
682 | $item['itemurl'] = $this->getItemUrl(); |
||
683 | $item['cancomment'] = $this->getVar('cancomment'); |
||
684 | $item['comments'] = $this->getVar('comments'); |
||
685 | $item['adminlink'] = $this->getAdminLinks(); |
||
686 | $item['categoryPath'] = $this->getCategoryPath($this->publisher->getConfig('format_linked_path')); |
||
687 | $item['who_when'] = $this->getWhoAndWhen(); |
||
688 | $item = $this->getMainImage($item); |
||
689 | return $item; |
||
690 | } |
||
691 | |||
692 | /** |
||
693 | * @param array $item |
||
694 | * @param int $item_page_id |
||
695 | * |
||
696 | * @return array |
||
697 | */ |
||
698 | public function toArrayAll($item, $item_page_id) |
||
699 | { |
||
700 | $item['maintext'] = $this->buildmaintext($item_page_id, $this->body()); |
||
701 | $item = $this->getOtherImages($item); |
||
702 | return $item; |
||
703 | } |
||
704 | |||
705 | /** |
||
706 | * @param array $item |
||
707 | * |
||
708 | * @return array |
||
709 | */ |
||
710 | public function getMainImage($item = array()) |
||
731 | } |
||
732 | |||
733 | /** |
||
734 | * @param array $item |
||
735 | * |
||
736 | * @return array |
||
737 | */ |
||
738 | public function getOtherImages($item = array()) |
||
739 | { |
||
740 | $thumbService = \Xoops::getInstance()->service('thumbnail'); |
||
741 | $images = $this->getImages(); |
||
742 | $item['images'] = array(); |
||
743 | $i = 0; |
||
744 | /* @var $image ImagesImage */ |
||
745 | foreach ($images['others'] as $image) { |
||
746 | $dimensions = getimagesize(\XoopsBaseConfig::get('root-path') . '/uploads/' . $image->getVar('image_name')); |
||
747 | $item['images'][$i]['width'] = $dimensions[0]; |
||
748 | $item['images'][$i]['height'] = $dimensions[1]; |
||
749 | $item['images'][$i]['path'] = \XoopsBaseConfig::get('url') . '/uploads/' . $image->getVar('image_name'); |
||
750 | $item['images'][$i]['thumb'] = $thumbService |
||
751 | ->getImgUrl('uploads/' . $image->getVar('image_name'), 240, 0) |
||
752 | ->getValue(); |
||
753 | $item['images'][$i]['name'] = $image->getVar('image_nicename'); |
||
754 | ++$i; |
||
755 | } |
||
756 | return $item; |
||
757 | } |
||
758 | |||
759 | /** |
||
760 | * @param string $content |
||
761 | * @param string|array $keywords |
||
762 | * |
||
763 | * @return string Text |
||
764 | */ |
||
765 | public function highlight($content, $keywords) |
||
766 | { |
||
767 | $color = $this->publisher->getConfig('format_highlight_color'); |
||
768 | if (substr($color, 0, 1) !== '#') { |
||
769 | $color = '#' . $color; |
||
770 | } |
||
771 | $pre = '<span style="font-weight: bolder; background-color: ' . $color . ';">'; |
||
772 | $post = '</span>'; |
||
773 | return \Xmf\Highlighter::apply($keywords, $content, $pre, $post); |
||
774 | } |
||
775 | |||
776 | /** |
||
777 | * Create metada and assign it to template |
||
778 | */ |
||
779 | public function createMetaTags() |
||
783 | } |
||
784 | |||
785 | /** |
||
786 | * @param string $str |
||
787 | * |
||
788 | * @return string |
||
789 | */ |
||
790 | public function _convert_for_japanese($str) |
||
791 | { |
||
792 | global $xoopsConfig; |
||
793 | // no action, if not flag |
||
794 | if (!defined('_PUBLISHER_FLAG_JP_CONVERT')) { |
||
795 | return $str; |
||
796 | } |
||
797 | // no action, if not Japanese |
||
798 | if ($xoopsConfig['language'] !== 'japanese') { |
||
799 | return $str; |
||
800 | } |
||
801 | // presume OS Browser |
||
802 | $agent = $_SERVER["HTTP_USER_AGENT"]; |
||
803 | $os = ''; |
||
804 | $browser = ''; |
||
805 | if (preg_match("/Win/i", $agent)) { |
||
806 | $os = 'win'; |
||
807 | } |
||
808 | if (preg_match("/MSIE/i", $agent)) { |
||
809 | $browser = 'msie'; |
||
810 | } |
||
811 | // if msie |
||
812 | if (($os === 'win') && ($browser === 'msie')) { |
||
813 | // if multibyte |
||
814 | if (function_exists('mb_convert_encoding')) { |
||
815 | $str = mb_convert_encoding($str, 'SJIS', 'EUC-JP'); |
||
816 | $str = rawurlencode($str); |
||
817 | } |
||
818 | } |
||
819 | return $str; |
||
820 | } |
||
821 | |||
822 | /** |
||
823 | * Checks if a user has access to a selected item. if no item permissions are |
||
824 | * set, access permission is denied. The user needs to have necessary category |
||
825 | * permission as well. |
||
826 | * Also, the item needs to be Published |
||
827 | * |
||
828 | * @return boolean : TRUE if the no errors occured |
||
829 | */ |
||
830 | public function accessGranted() |
||
831 | { |
||
832 | if (PublisherUtils::IsUserAdmin()) { |
||
833 | return true; |
||
834 | } |
||
835 | if ($this->getVar('status') != _PUBLISHER_STATUS_PUBLISHED) { |
||
836 | return false; |
||
837 | } |
||
838 | // Do we have access to the parent category |
||
839 | if ($this->publisher->getPermissionHandler()->isGranted('category_read', $this->getVar('categoryid'))) { |
||
840 | return true; |
||
841 | } |
||
842 | return false; |
||
843 | } |
||
844 | |||
845 | /** |
||
846 | * The name says it all |
||
847 | */ |
||
848 | public function setVarsFromRequest() |
||
961 | } |
||
962 | } |
||
963 | } |
||
964 | |||
965 | /** |
||
966 | * Items handler class. |
||
967 | * This class is responsible for providing data access mechanisms to the data source |
||
968 | * of Q&A class objects. |
||
969 | * |
||
970 | * @author marcan <[email protected]> |
||
971 | * @package Publisher |
||
972 | */ |
||
973 | class PublisherItemHandler extends XoopsPersistableObjectHandler |
||
1653 |
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: