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 PMF_Faq 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 PMF_Faq, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
66 | class PMF_Faq |
||
67 | { |
||
68 | /** |
||
69 | * @var PMF_Configuration |
||
70 | */ |
||
71 | private $_config; |
||
72 | |||
73 | /** |
||
74 | * Language strings. |
||
75 | * |
||
76 | * @var string |
||
77 | */ |
||
78 | private $pmf_lang; |
||
79 | |||
80 | /** |
||
81 | * Plural form support. |
||
82 | * |
||
83 | * @var PMF_Language_Plurals |
||
84 | */ |
||
85 | private $plr; |
||
86 | |||
87 | /** |
||
88 | * The current FAQ record. |
||
89 | * |
||
90 | * @var array |
||
91 | */ |
||
92 | public $faqRecord = []; |
||
93 | |||
94 | /** |
||
95 | * All current FAQ records in an array. |
||
96 | * |
||
97 | * @var array |
||
98 | */ |
||
99 | public $faqRecords = []; |
||
100 | |||
101 | /** |
||
102 | * Users. |
||
103 | * |
||
104 | * @var int |
||
105 | */ |
||
106 | private $user = -1; |
||
107 | |||
108 | /** |
||
109 | * Groups. |
||
110 | * |
||
111 | * @var array |
||
112 | */ |
||
113 | private $groups = array(-1); |
||
114 | |||
115 | /** |
||
116 | * Flag for Group support. |
||
117 | * |
||
118 | * @var bool |
||
119 | */ |
||
120 | private $groupSupport = false; |
||
121 | |||
122 | /** |
||
123 | * Constructor. |
||
124 | * |
||
125 | * @param PMF_Configuration $config |
||
126 | * |
||
127 | * @return PMF_Faq |
||
128 | */ |
||
129 | public function __construct(PMF_Configuration $config) |
||
130 | { |
||
131 | global $PMF_LANG, $plr; |
||
132 | |||
133 | $this->_config = $config; |
||
134 | $this->pmf_lang = $PMF_LANG; |
||
135 | $this->plr = $plr; |
||
136 | |||
137 | if ($this->_config->get('security.permLevel') == 'medium') { |
||
138 | $this->groupSupport = true; |
||
139 | } |
||
140 | } |
||
141 | |||
142 | // |
||
143 | // |
||
144 | // PUBLIC METHODS |
||
145 | // |
||
146 | // |
||
147 | |||
148 | /** |
||
149 | * @param int $userId |
||
150 | */ |
||
151 | public function setUser($userId = -1) |
||
152 | { |
||
153 | $this->user = $userId; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @param array $groups |
||
158 | */ |
||
159 | public function setGroups(Array $groups) |
||
160 | { |
||
161 | $this->groups = $groups; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * This function returns all not expired records from one category. |
||
166 | * |
||
167 | * @param int $category_id Category ID |
||
168 | * @param string $orderby Order by |
||
169 | * @param string $sortby Sorty by |
||
170 | * |
||
171 | * @return array |
||
172 | */ |
||
173 | public function getAllRecordPerCategory($category_id, $orderby = 'id', $sortby = 'ASC') |
||
287 | |||
288 | /** |
||
289 | * This function returns all not expired records from one category. |
||
290 | * |
||
291 | * @param int $categoryId Category ID |
||
292 | * @param string $orderby Order by |
||
293 | * @param string $sortby Sorty by |
||
294 | * |
||
295 | * @return string |
||
296 | */ |
||
297 | public function showAllRecords($categoryId, $orderby = 'id', $sortby = 'ASC') |
||
298 | { |
||
299 | global $sids; |
||
300 | |||
301 | $numPerPage = $this->_config->get('records.numberOfRecordsPerPage'); |
||
302 | $page = PMF_Filter::filterInput(INPUT_GET, 'seite', FILTER_VALIDATE_INT, 1); |
||
303 | $output = ''; |
||
304 | $title = ''; |
||
305 | |||
306 | if ($orderby == 'visits') { |
||
307 | $currentTable = 'fv'; |
||
308 | } else { |
||
309 | $currentTable = 'fd'; |
||
310 | } |
||
311 | |||
312 | // If random FAQs are activated, we don't need an order |
||
313 | if (true === $this->_config->get('records.randomSort')) { |
||
314 | $order = ''; |
||
315 | } else { |
||
316 | $order = sprintf( |
||
317 | 'ORDER BY fd.sticky DESC, %s.%s %s', |
||
318 | $currentTable, |
||
319 | $this->_config->getDb()->escape($orderby), |
||
320 | $this->_config->getDb()->escape($sortby) |
||
321 | ); |
||
322 | } |
||
323 | |||
324 | $now = date('YmdHis'); |
||
325 | $query = sprintf(" |
||
326 | SELECT |
||
327 | fd.id AS id, |
||
328 | fd.lang AS lang, |
||
329 | fd.sticky AS sticky, |
||
330 | fd.thema AS thema, |
||
331 | fcr.category_id AS category_id, |
||
332 | fv.visits AS visits |
||
333 | FROM |
||
334 | %sfaqdata AS fd |
||
335 | LEFT JOIN |
||
336 | %sfaqcategoryrelations AS fcr |
||
337 | ON |
||
338 | fd.id = fcr.record_id |
||
339 | AND |
||
340 | fd.lang = fcr.record_lang |
||
341 | LEFT JOIN |
||
342 | %sfaqvisits AS fv |
||
343 | ON |
||
344 | fd.id = fv.id |
||
345 | AND |
||
346 | fv.lang = fd.lang |
||
347 | LEFT JOIN |
||
348 | %sfaqdata_group AS fdg |
||
349 | ON |
||
350 | fd.id = fdg.record_id |
||
351 | LEFT JOIN |
||
352 | %sfaqdata_user AS fdu |
||
353 | ON |
||
354 | fd.id = fdu.record_id |
||
355 | WHERE |
||
356 | fd.date_start <= '%s' |
||
357 | AND |
||
358 | fd.date_end >= '%s' |
||
359 | AND |
||
360 | fd.active = 'yes' |
||
361 | AND |
||
362 | fcr.category_id = %d |
||
363 | AND |
||
364 | fd.lang = '%s' |
||
365 | %s |
||
366 | %s", |
||
367 | PMF_Db::getTablePrefix(), |
||
368 | PMF_Db::getTablePrefix(), |
||
369 | PMF_Db::getTablePrefix(), |
||
370 | PMF_Db::getTablePrefix(), |
||
371 | PMF_Db::getTablePrefix(), |
||
372 | $now, |
||
373 | $now, |
||
374 | $categoryId, |
||
375 | $this->_config->getLanguage()->getLanguage(), |
||
376 | $this->queryPermission($this->groupSupport), |
||
377 | $order |
||
378 | ); |
||
379 | |||
380 | $result = $this->_config->getDb()->query($query); |
||
381 | $num = $this->_config->getDb()->numRows($result); |
||
382 | $pages = (int) ceil($num / $numPerPage); |
||
383 | |||
384 | if ($page == 1) { |
||
385 | $first = 0; |
||
386 | } else { |
||
387 | $first = $page * $numPerPage - $numPerPage; |
||
388 | } |
||
389 | |||
390 | if ($num > 0) { |
||
391 | View Code Duplication | if ($pages > 1) { |
|
392 | $output .= sprintf('<p><strong>%s %s %s</strong></p>', |
||
393 | $this->pmf_lang['msgPage'].$page, |
||
394 | $this->pmf_lang['msgVoteFrom'], |
||
395 | $pages.$this->pmf_lang['msgPages']); |
||
396 | } |
||
397 | $output .= '<ul class="phpmyfaq_ul">'; |
||
398 | |||
399 | $counter = 0; |
||
400 | $displayedCounter = 0; |
||
401 | $renderedItems = []; |
||
402 | while (($row = $this->_config->getDb()->fetchObject($result)) && $displayedCounter < $numPerPage) { |
||
403 | ++$counter; |
||
404 | if ($counter <= $first) { |
||
405 | continue; |
||
406 | } |
||
407 | ++$displayedCounter; |
||
408 | |||
409 | if (empty($row->visits)) { |
||
410 | $visits = 0; |
||
411 | } else { |
||
412 | $visits = $row->visits; |
||
413 | } |
||
414 | |||
415 | $title = $row->thema; |
||
416 | $url = sprintf( |
||
417 | '%s?%saction=artikel&cat=%d&id=%d&artlang=%s', |
||
418 | PMF_Link::getSystemRelativeUri(), |
||
419 | $sids, |
||
420 | $row->category_id, |
||
421 | $row->id, |
||
422 | $row->lang |
||
423 | ); |
||
424 | |||
425 | $oLink = new PMF_Link($url, $this->_config); |
||
426 | $oLink->itemTitle = $oLink->text = $oLink->tooltip = $title; |
||
427 | |||
428 | // If random FAQs are activated, we don't need sticky FAQs |
||
429 | if (true === $this->_config->get('records.randomSort')) { |
||
430 | $row->sticky = 0; |
||
431 | } |
||
432 | |||
433 | $renderedItems[$row->id] = sprintf( |
||
434 | '<li%s>%s<span id="viewsPerRecord"><br /><small>(%s)</small></span></li>', |
||
435 | ($row->sticky == 1) ? ' class="sticky-faqs"' : '', |
||
436 | $oLink->toHtmlAnchor(), |
||
437 | $this->plr->GetMsg('plmsgViews', $visits) |
||
438 | ); |
||
439 | } |
||
440 | |||
441 | // If random FAQs are activated, shuffle the FAQs :-) |
||
442 | if (true === $this->_config->get('records.randomSort')) { |
||
443 | shuffle($renderedItems); |
||
444 | } |
||
445 | |||
446 | $output .= implode("\n", $renderedItems); |
||
447 | $output .= '</ul><span class="totalFaqRecords hide">'.$num.'</span>'; |
||
448 | } else { |
||
449 | return false; |
||
450 | } |
||
451 | |||
452 | if ($pages > 1) { |
||
453 | // Set rewrite URL, if needed |
||
454 | if ($this->_config->get('main.enableRewriteRules')) { |
||
455 | $link = new PMF_Link(PMF_Link::getSystemRelativeUri('index.php'), $this->_config); |
||
456 | $useRewrite = true; |
||
457 | $rewriteUrl = sprintf( |
||
458 | '%scategory/%d/%%d/%s.html', |
||
459 | PMF_Link::getSystemRelativeUri('index.php'), |
||
460 | $categoryId, |
||
461 | $link->getSEOItemTitle($title) |
||
462 | ); |
||
463 | } else { |
||
464 | $useRewrite = false; |
||
465 | $rewriteUrl = ''; |
||
466 | } |
||
467 | $baseUrl = sprintf( |
||
468 | '%s?%saction=show&cat=%d&seite=%d', |
||
469 | PMF_Link::getSystemRelativeUri(), |
||
470 | (empty($sids) ? '' : $sids), |
||
471 | $categoryId, |
||
472 | $page |
||
473 | ); |
||
474 | |||
475 | $options = array( |
||
476 | 'baseUrl' => $baseUrl, |
||
477 | 'total' => $num, |
||
478 | 'perPage' => $this->_config->get('records.numberOfRecordsPerPage'), |
||
479 | 'useRewrite' => $useRewrite, |
||
480 | 'rewriteUrl' => $rewriteUrl, |
||
481 | 'pageParamName' => 'seite', |
||
482 | ); |
||
483 | |||
484 | $pagination = new PMF_Pagination($this->_config, $options); |
||
485 | $output .= $pagination->render(); |
||
486 | } |
||
487 | |||
488 | return $output; |
||
489 | } |
||
490 | |||
491 | /** |
||
492 | * This function returns all not expired records from the given record ids. |
||
493 | * |
||
494 | * @param array $record_ids Array of record ids |
||
495 | * @param string $orderby Order by |
||
496 | * @param string $sortby Sort by |
||
497 | * |
||
498 | * @return string |
||
499 | */ |
||
500 | public function showAllRecordsByIds(Array $record_ids, $orderby = 'fd.id', $sortby = 'ASC') |
||
501 | { |
||
502 | global $sids; |
||
503 | |||
504 | $records = implode(', ', $record_ids); |
||
505 | $page = PMF_Filter::filterInput(INPUT_GET, 'seite', FILTER_VALIDATE_INT, 1); |
||
506 | $tagging_id = PMF_Filter::filterInput(INPUT_GET, 'tagging_id', FILTER_VALIDATE_INT); |
||
507 | $output = ''; |
||
508 | |||
509 | $now = date('YmdHis'); |
||
510 | $query = sprintf(" |
||
511 | SELECT |
||
512 | fd.id AS id, |
||
513 | fd.lang AS lang, |
||
514 | fd.thema AS thema, |
||
515 | fcr.category_id AS category_id, |
||
516 | fv.visits AS visits |
||
517 | FROM |
||
518 | %sfaqdata AS fd |
||
519 | LEFT JOIN |
||
520 | %sfaqcategoryrelations AS fcr |
||
521 | ON |
||
522 | fd.id = fcr.record_id |
||
523 | AND |
||
524 | fd.lang = fcr.record_lang |
||
525 | LEFT JOIN |
||
526 | %sfaqvisits AS fv |
||
527 | ON |
||
528 | fd.id = fv.id |
||
529 | AND |
||
530 | fv.lang = fd.lang |
||
531 | LEFT JOIN |
||
532 | %sfaqdata_group AS fdg |
||
533 | ON |
||
534 | fd.id = fdg.record_id |
||
535 | LEFT JOIN |
||
536 | %sfaqdata_user AS fdu |
||
537 | ON |
||
538 | fd.id = fdu.record_id |
||
539 | WHERE |
||
540 | fd.date_start <= '%s' |
||
541 | AND |
||
542 | fd.date_end >= '%s' |
||
543 | AND |
||
544 | fd.active = 'yes' |
||
545 | AND |
||
546 | fd.id IN (%s) |
||
547 | AND |
||
548 | fd.lang = '%s' |
||
549 | %s |
||
550 | ORDER BY |
||
551 | %s %s", |
||
552 | PMF_Db::getTablePrefix(), |
||
553 | PMF_Db::getTablePrefix(), |
||
554 | PMF_Db::getTablePrefix(), |
||
555 | PMF_Db::getTablePrefix(), |
||
556 | PMF_Db::getTablePrefix(), |
||
557 | $now, |
||
558 | $now, |
||
559 | $records, |
||
560 | $this->_config->getLanguage()->getLanguage(), |
||
561 | $this->queryPermission($this->groupSupport), |
||
562 | $this->_config->getDb()->escape($orderby), |
||
563 | $this->_config->getDb()->escape($sortby)); |
||
564 | |||
565 | $result = $this->_config->getDb()->query($query); |
||
566 | |||
567 | $num = $this->_config->getDb()->numRows($result); |
||
568 | $pages = ceil($num / $this->_config->get('records.numberOfRecordsPerPage')); |
||
569 | |||
570 | if ($page == 1) { |
||
571 | $first = 0; |
||
572 | } else { |
||
573 | $first = ($page * $this->_config->get('records.numberOfRecordsPerPage')) - $this->_config->get('records.numberOfRecordsPerPage'); |
||
574 | } |
||
575 | |||
576 | if ($num > 0) { |
||
577 | View Code Duplication | if ($pages > 1) { |
|
578 | $output .= sprintf('<p><strong>%s %s %s</strong></p>', |
||
579 | $this->pmf_lang['msgPage'].$page, |
||
580 | $this->pmf_lang['msgVoteFrom'], |
||
581 | $pages.$this->pmf_lang['msgPages']); |
||
582 | } |
||
583 | $output .= '<ul class="phpmyfaq_ul">'; |
||
584 | $counter = 0; |
||
585 | $displayedCounter = 0; |
||
586 | |||
587 | $lastFaqId = 0; |
||
588 | while (($row = $this->_config->getDb()->fetchObject($result)) && $displayedCounter < $this->_config->get('records.numberOfRecordsPerPage')) { |
||
589 | ++$counter; |
||
590 | if ($counter <= $first) { |
||
591 | continue; |
||
592 | } |
||
593 | ++$displayedCounter; |
||
594 | |||
595 | if ($lastFaqId == $row->id) { |
||
596 | continue; // Don't show multiple FAQs |
||
597 | } |
||
598 | |||
599 | if (empty($row->visits)) { |
||
600 | $visits = 0; |
||
601 | } else { |
||
602 | $visits = $row->visits; |
||
603 | } |
||
604 | |||
605 | $title = $row->thema; |
||
606 | $url = sprintf( |
||
607 | '%s?%saction=artikel&cat=%d&id=%d&artlang=%s', |
||
608 | PMF_Link::getSystemRelativeUri(), |
||
609 | $sids, |
||
610 | $row->category_id, |
||
611 | $row->id, |
||
612 | $row->lang |
||
613 | ); |
||
614 | $oLink = new PMF_Link($url, $this->_config); |
||
615 | $oLink->itemTitle = $row->thema; |
||
616 | $oLink->text = $title; |
||
617 | $oLink->tooltip = $title; |
||
618 | $listItem = sprintf( |
||
619 | '<li>%s<br /><small>(%s)</small></li>', |
||
620 | $oLink->toHtmlAnchor(), |
||
621 | $this->plr->GetMsg('plmsgViews', $visits) |
||
622 | ); |
||
623 | |||
624 | $output .= $listItem; |
||
625 | |||
626 | $lastFaqId = $row->id; |
||
627 | } |
||
628 | $output .= '</ul><span id="totFaqRecords" style="display: none;">'.$num.'</span>'; |
||
629 | } else { |
||
630 | return false; |
||
631 | } |
||
632 | |||
633 | if ($num > $this->_config->get('records.numberOfRecordsPerPage')) { |
||
634 | $output .= '<p class="text-center"><strong>'; |
||
635 | if (!isset($page)) { |
||
636 | $page = 1; |
||
637 | } |
||
638 | $vor = $page - 1; |
||
639 | $next = $page + 1; |
||
640 | View Code Duplication | if ($vor != 0) { |
|
641 | $url = $sids.'&action=search&tagging_id='.$tagging_id.'&seite='.$vor; |
||
642 | $oLink = new PMF_Link(PMF_Link::getSystemRelativeUri().'?'.$url, $this->_config); |
||
643 | $oLink->itemTitle = 'tag'; |
||
644 | $oLink->text = $this->pmf_lang['msgPrevious']; |
||
645 | $oLink->tooltip = $this->pmf_lang['msgPrevious']; |
||
646 | $output .= '[ '.$oLink->toHtmlAnchor().' ]'; |
||
647 | } |
||
648 | $output .= ' '; |
||
649 | View Code Duplication | if ($next <= $pages) { |
|
650 | $url = $sids.'&action=search&tagging_id='.$tagging_id.'&seite='.$next; |
||
651 | $oLink = new PMF_Link(PMF_Link::getSystemRelativeUri().'?'.$url, $this->_config); |
||
652 | $oLink->itemTitle = 'tag'; |
||
653 | $oLink->text = $this->pmf_lang['msgNext']; |
||
654 | $oLink->tooltip = $this->pmf_lang['msgNext']; |
||
655 | $output .= '[ '.$oLink->toHtmlAnchor().' ]'; |
||
656 | } |
||
657 | $output .= '</strong></p>'; |
||
658 | } |
||
659 | |||
660 | return $output; |
||
661 | } |
||
662 | |||
663 | /** |
||
664 | * Returns an array with all data from a FAQ record. |
||
665 | * |
||
666 | * @param int $faqId FAQ ID |
||
667 | * @param int $faqRevisionId Revision ID |
||
668 | * @param bool $isAdmin Must be true if it is called by an admin/author context |
||
669 | */ |
||
670 | public function getRecord($faqId, $faqRevisionId = null, $isAdmin = false) |
||
743 | |||
744 | /** |
||
745 | * Executes a query to retrieve a single FAQ. |
||
746 | * |
||
747 | * @param int $faqId |
||
748 | * @param string $faqLanguage |
||
749 | * @param int $faqRevisionId |
||
750 | * @param bool $isAdmin |
||
751 | * |
||
752 | * @return mixed |
||
753 | */ |
||
754 | public function getRecordResult($faqId, $faqLanguage, $faqRevisionId = null, $isAdmin = false) |
||
755 | { |
||
756 | $query = sprintf( |
||
757 | "SELECT |
||
758 | id, lang, solution_id, revision_id, active, sticky, keywords, |
||
759 | thema, content, author, email, comment, updated, links_state, |
||
760 | links_check_date, date_start, date_end, created, notes |
||
761 | FROM |
||
762 | %s%s fd |
||
763 | LEFT JOIN |
||
764 | %sfaqdata_group fdg |
||
765 | ON |
||
766 | fd.id = fdg.record_id |
||
767 | LEFT JOIN |
||
768 | %sfaqdata_user fdu |
||
769 | ON |
||
770 | fd.id = fdu.record_id |
||
771 | WHERE |
||
772 | fd.id = %d |
||
773 | %s |
||
774 | AND |
||
775 | fd.lang = '%s' |
||
776 | %s", |
||
777 | PMF_Db::getTablePrefix(), |
||
778 | isset($faqRevisionId) ? 'faqdata_revisions' : 'faqdata', |
||
779 | PMF_Db::getTablePrefix(), |
||
780 | PMF_Db::getTablePrefix(), |
||
781 | $faqId, |
||
782 | isset($faqRevisionId) ? 'AND revision_id = '.$faqRevisionId : '', |
||
783 | $faqLanguage, |
||
784 | ($isAdmin) ? 'AND 1=1' : $this->queryPermission($this->groupSupport) |
||
785 | ); |
||
786 | |||
787 | return $this->_config->getDb()->query($query); |
||
788 | } |
||
789 | |||
790 | /** |
||
791 | * Return records from given IDs |
||
792 | * |
||
793 | * @param array $faqIds |
||
794 | * |
||
795 | * @return array |
||
796 | */ |
||
797 | public function getRecordsByIds(Array $faqIds) |
||
883 | |||
884 | /** |
||
885 | * Adds a new record. |
||
886 | * |
||
887 | * @param array $data Array of FAQ data |
||
888 | * @param bool $newRecord Do not create a new ID if false |
||
889 | * |
||
890 | * @return int |
||
891 | */ |
||
892 | public function addRecord(Array $data, $newRecord = true) |
||
893 | { |
||
894 | if ($newRecord) { |
||
895 | $recordId = $this->_config->getDb()->nextId(PMF_Db::getTablePrefix().'faqdata', 'id'); |
||
896 | } else { |
||
897 | $recordId = $data['id']; |
||
898 | } |
||
899 | |||
900 | // Add new entry |
||
901 | $query = sprintf(" |
||
902 | INSERT INTO |
||
903 | %sfaqdata |
||
904 | VALUES |
||
905 | (%d, '%s', %d, %d, '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s')", |
||
906 | PMF_Db::getTablePrefix(), |
||
907 | $recordId, |
||
908 | $data['lang'], |
||
909 | $this->getSolutionId(), |
||
910 | 0, |
||
911 | $data['active'], |
||
912 | $data['sticky'], |
||
913 | $this->_config->getDb()->escape($data['keywords']), |
||
914 | $this->_config->getDb()->escape($data['thema']), |
||
915 | $this->_config->getDb()->escape($data['content']), |
||
916 | $this->_config->getDb()->escape($data['author']), |
||
917 | $data['email'], |
||
918 | $data['comment'], |
||
919 | $data['date'], |
||
920 | $data['linkState'], |
||
921 | $data['linkDateCheck'], |
||
922 | $data['dateStart'], |
||
923 | $data['dateEnd'], |
||
924 | date('Y-m-d H:i:s'), |
||
925 | $data['notes'] |
||
926 | ); |
||
927 | |||
928 | $this->_config->getDb()->query($query); |
||
929 | |||
930 | return $recordId; |
||
931 | } |
||
932 | |||
933 | /** |
||
934 | * Updates a record. |
||
935 | * |
||
936 | * @param array $data Array of FAQ data |
||
937 | * |
||
938 | * @return bool |
||
939 | */ |
||
940 | public function updateRecord(Array $data) |
||
990 | |||
991 | /** |
||
992 | * Deletes a record and all the dependencies. |
||
993 | * |
||
994 | * @param int $recordId Record id |
||
995 | * @param string $recordLang Record language |
||
996 | * |
||
997 | * @return bool |
||
998 | */ |
||
999 | public function deleteRecord($recordId, $recordLang) |
||
1000 | { |
||
1001 | $solutionId = $this->getSolutionIdFromId($recordId, $recordLang); |
||
1002 | |||
1003 | $queries = array( |
||
1004 | sprintf( |
||
1005 | "DELETE FROM %sfaqchanges WHERE beitrag = %d AND lang = '%s'", |
||
1006 | PMF_Db::getTablePrefix(), |
||
1007 | $recordId, |
||
1008 | $recordLang |
||
1009 | ), |
||
1010 | sprintf( |
||
1011 | "DELETE FROM %sfaqcategoryrelations WHERE record_id = %d AND record_lang = '%s'", |
||
1012 | PMF_Db::getTablePrefix(), |
||
1013 | $recordId, |
||
1014 | $recordLang |
||
1015 | ), |
||
1016 | sprintf( |
||
1017 | "DELETE FROM %sfaqdata WHERE id = %d AND lang = '%s'", |
||
1018 | PMF_Db::getTablePrefix(), |
||
1019 | $recordId, |
||
1020 | $recordLang |
||
1021 | ), |
||
1022 | sprintf( |
||
1023 | "DELETE FROM %sfaqdata_revisions WHERE id = %d AND lang = '%s'", |
||
1024 | PMF_Db::getTablePrefix(), |
||
1025 | $recordId, |
||
1026 | $recordLang |
||
1027 | ), |
||
1028 | sprintf( |
||
1029 | "DELETE FROM %sfaqvisits WHERE id = %d AND lang = '%s'", |
||
1030 | PMF_Db::getTablePrefix(), |
||
1031 | $recordId, |
||
1032 | $recordLang |
||
1033 | ), |
||
1034 | sprintf( |
||
1035 | 'DELETE FROM %sfaqdata_user WHERE record_id = %d', |
||
1036 | PMF_Db::getTablePrefix(), |
||
1037 | $recordId, |
||
1038 | $recordLang |
||
1039 | ), |
||
1040 | sprintf( |
||
1041 | 'DELETE FROM %sfaqdata_group WHERE record_id = %d', |
||
1042 | PMF_Db::getTablePrefix(), |
||
1043 | $recordId, |
||
1044 | $recordLang |
||
1045 | ), |
||
1046 | sprintf( |
||
1047 | 'DELETE FROM %sfaqdata_tags WHERE record_id = %d', |
||
1048 | PMF_Db::getTablePrefix(), |
||
1049 | $recordId |
||
1050 | ), |
||
1051 | sprintf( |
||
1052 | 'DELETE FROM %sfaqdata_tags WHERE %sfaqdata_tags.record_id NOT IN (SELECT %sfaqdata.id FROM %sfaqdata)', |
||
1053 | PMF_Db::getTablePrefix(), |
||
1054 | PMF_Db::getTablePrefix(), |
||
1055 | PMF_Db::getTablePrefix(), |
||
1056 | PMF_Db::getTablePrefix() |
||
1057 | ), |
||
1058 | sprintf( |
||
1059 | 'DELETE FROM %sfaqcomments WHERE id = %d', |
||
1060 | PMF_Db::getTablePrefix(), |
||
1061 | $recordId |
||
1062 | ), |
||
1063 | sprintf( |
||
1064 | 'DELETE FROM %sfaqvoting WHERE artikel = %d', |
||
1065 | PMF_Db::getTablePrefix(), |
||
1066 | $recordId |
||
1067 | ), |
||
1068 | ); |
||
1069 | |||
1070 | foreach ($queries as $query) { |
||
1071 | $this->_config->getDb()->query($query); |
||
1072 | } |
||
1073 | |||
1074 | // Delete possible attachments |
||
1075 | $attId = PMF_Attachment_Factory::fetchByRecordId($this->_config, $recordId); |
||
1076 | $attachment = PMF_Attachment_Factory::create($attId); |
||
1077 | $attachment->delete(); |
||
1078 | |||
1079 | // Delete possible Elasticsearch documents |
||
1080 | if ($this->_config->get('search.enableElasticsearch')) { |
||
1081 | $esInstance = new PMF_Instance_Elasticsearch($this->_config); |
||
1082 | $esInstance->delete($solutionId); |
||
1083 | } |
||
1084 | |||
1085 | return true; |
||
1086 | } |
||
1087 | |||
1088 | /** |
||
1089 | * Checks if a record is already translated. |
||
1090 | * |
||
1091 | * @param int $record_id Record id |
||
1092 | * @param string $record_lang Record language |
||
1093 | * |
||
1094 | * @return bool |
||
1095 | */ |
||
1096 | public function isAlreadyTranslated($record_id, $record_lang) |
||
1097 | { |
||
1098 | $query = sprintf(" |
||
1099 | SELECT |
||
1100 | id, lang |
||
1101 | FROM |
||
1102 | %sfaqdata |
||
1103 | WHERE |
||
1104 | id = %d |
||
1105 | AND |
||
1106 | lang = '%s'", |
||
1107 | PMF_Db::getTablePrefix(), |
||
1108 | $record_id, |
||
1109 | $record_lang); |
||
1110 | |||
1111 | $result = $this->_config->getDb()->query($query); |
||
1112 | |||
1113 | if ($this->_config->getDb()->numRows($result)) { |
||
1114 | return true; |
||
1115 | } |
||
1116 | |||
1117 | return false; |
||
1118 | } |
||
1119 | |||
1120 | /** |
||
1121 | * Checks, if comments are disabled for the FAQ record. |
||
1122 | * |
||
1123 | * @param int $record_id Id of FAQ or news entry |
||
1124 | * @param string $record_lang Language |
||
1125 | * @param string $record_type Type of comment: faq or news |
||
1126 | * |
||
1127 | * @return bool true, if comments are disabled |
||
1128 | */ |
||
1129 | public function commentDisabled($record_id, $record_lang, $record_type = 'faq') |
||
1160 | |||
1161 | /** |
||
1162 | * Adds new category relations to a record. |
||
1163 | * |
||
1164 | * @param array $categories Array of categories |
||
1165 | * @param int $record_id Record id |
||
1166 | * @param string $language Language |
||
1167 | * |
||
1168 | * @return int |
||
1169 | */ |
||
1170 | public function addCategoryRelations(Array $categories, $record_id, $language) |
||
1171 | { |
||
1172 | if (!is_array($categories)) { |
||
1173 | return false; |
||
1174 | } |
||
1175 | |||
1176 | foreach ($categories as $_category) { |
||
1177 | $this->_config->getDb()->query(sprintf( |
||
1178 | "INSERT INTO |
||
1179 | %sfaqcategoryrelations |
||
1180 | VALUES |
||
1181 | (%d, '%s', %d, '%s')", |
||
1182 | PMF_Db::getTablePrefix(), |
||
1183 | $_category, |
||
1184 | $language, |
||
1185 | $record_id, |
||
1186 | $language)); |
||
1187 | } |
||
1188 | |||
1189 | return true; |
||
1190 | } |
||
1191 | |||
1192 | /** |
||
1193 | * Adds new category relation to a record. |
||
1194 | * |
||
1195 | * @param mixed $category Category or array of categories |
||
1196 | * @param int $record_id Record id |
||
1197 | * @param string $language Language |
||
1198 | * |
||
1199 | * @return bool |
||
1200 | */ |
||
1201 | public function addCategoryRelation($category, $record_id, $language) |
||
1202 | { |
||
1203 | // Just a fallback when (wrong case) $category is an array |
||
1204 | if (is_array($category)) { |
||
1205 | $this->addCategoryRelations($category, $record_id, $language); |
||
1206 | } |
||
1207 | $categories[] = $category; |
||
1208 | |||
1209 | return $this->addCategoryRelations($categories, $record_id, $language); |
||
1210 | } |
||
1211 | |||
1212 | /** |
||
1213 | * Deletes category relations to a record. |
||
1214 | * |
||
1215 | * @param int $record_id Record id |
||
1216 | * @param string $record_lang Language |
||
1217 | * |
||
1218 | * @return bool |
||
1219 | */ |
||
1220 | public function deleteCategoryRelations($record_id, $record_lang) |
||
|
|||
1221 | { |
||
1222 | $query = sprintf(" |
||
1223 | DELETE FROM |
||
1224 | %sfaqcategoryrelations |
||
1225 | WHERE |
||
1226 | record_id = %d |
||
1227 | AND |
||
1228 | record_lang = '%s'", |
||
1229 | PMF_Db::getTablePrefix(), |
||
1230 | $record_id, |
||
1231 | $record_lang); |
||
1232 | $this->_config->getDb()->query($query); |
||
1233 | |||
1234 | return true; |
||
1235 | } |
||
1236 | |||
1237 | /** |
||
1238 | * Returns an array with all data from a FAQ record. |
||
1239 | * |
||
1240 | * @param int $solutionId Solution ID |
||
1241 | */ |
||
1242 | public function getRecordBySolutionId($solutionId) |
||
1304 | |||
1305 | /** |
||
1306 | * Gets the record ID from a given solution ID. |
||
1307 | * |
||
1308 | * @param int $solutionId Solution ID |
||
1309 | * |
||
1310 | * @return array |
||
1311 | */ |
||
1312 | public function getIdFromSolutionId($solutionId) |
||
1313 | { |
||
1314 | $query = sprintf(' |
||
1315 | SELECT |
||
1316 | id, lang, content |
||
1317 | FROM |
||
1318 | %sfaqdata |
||
1319 | WHERE |
||
1320 | solution_id = %d', |
||
1321 | PMF_Db::getTablePrefix(), |
||
1322 | $solutionId |
||
1323 | ); |
||
1324 | |||
1325 | $result = $this->_config->getDb()->query($query); |
||
1326 | |||
1327 | if ($row = $this->_config->getDb()->fetchObject($result)) { |
||
1328 | return [ |
||
1329 | 'id' => $row->id, |
||
1330 | 'lang' => $row->lang, |
||
1331 | 'content' => $row->content |
||
1332 | ]; |
||
1333 | } |
||
1334 | |||
1335 | return []; |
||
1336 | } |
||
1337 | |||
1338 | /** |
||
1339 | * Returns the solution ID from a given ID and language |
||
1340 | * |
||
1341 | * @param integer $faqId |
||
1342 | * @param string $faqLang |
||
1343 | * |
||
1344 | * @return int |
||
1345 | */ |
||
1346 | public function getSolutionIdFromId($faqId, $faqLang) |
||
1347 | { |
||
1348 | $query = sprintf(" |
||
1349 | SELECT |
||
1350 | solution_id |
||
1351 | FROM |
||
1352 | %sfaqdata |
||
1353 | WHERE |
||
1354 | id = %d |
||
1355 | AND |
||
1356 | lang = '%s'", |
||
1357 | PMF_Db::getTablePrefix(), |
||
1358 | (int) $faqId, |
||
1359 | $this->_config->getDb()->escape($faqLang) |
||
1360 | ); |
||
1361 | |||
1362 | $result = $this->_config->getDb()->query($query); |
||
1363 | |||
1364 | if ($row = $this->_config->getDb()->fetchObject($result)) { |
||
1365 | return $row->solution_id; |
||
1366 | } |
||
1367 | |||
1368 | return $this->getSolutionId(); |
||
1369 | } |
||
1370 | |||
1371 | /** |
||
1372 | * Gets the latest solution id for a FAQ record. |
||
1373 | * |
||
1374 | * @return int |
||
1375 | */ |
||
1376 | public function getSolutionId() |
||
1377 | { |
||
1378 | $latestId = 0; |
||
1379 | |||
1380 | $query = sprintf(' |
||
1381 | SELECT |
||
1382 | MAX(solution_id) AS solution_id |
||
1383 | FROM |
||
1384 | %sfaqdata', |
||
1385 | PMF_Db::getTablePrefix() |
||
1386 | ); |
||
1387 | |||
1388 | $result = $this->_config->getDb()->query($query); |
||
1389 | |||
1390 | if ($result && $row = $this->_config->getDb()->fetchObject($result)) { |
||
1391 | $latestId = $row->solution_id; |
||
1392 | } |
||
1393 | |||
1394 | if ($latestId < PMF_SOLUTION_ID_START_VALUE) { |
||
1395 | $nextSolutionId = PMF_SOLUTION_ID_START_VALUE; |
||
1396 | } else { |
||
1397 | $nextSolutionId = $latestId + PMF_SOLUTION_ID_INCREMENT_VALUE; |
||
1398 | } |
||
1399 | |||
1400 | return $nextSolutionId; |
||
1401 | } |
||
1402 | |||
1403 | /** |
||
1404 | * Returns an array with all data from all FAQ records. |
||
1405 | * |
||
1406 | * @param int $sortType Sorting type |
||
1407 | * @param array $condition Condition |
||
1408 | * @param string $sortOrder Sorting order |
||
1409 | */ |
||
1410 | public function getAllRecords($sortType = FAQ_SORTING_TYPE_CATID_FAQID, Array $condition = null, $sortOrder = 'ASC') |
||
1549 | |||
1550 | /** |
||
1551 | * Returns the FAQ record title from the ID and language. |
||
1552 | * |
||
1553 | * @param int $id Record id |
||
1554 | * |
||
1555 | * @return string |
||
1556 | */ |
||
1557 | public function getRecordTitle($id) |
||
1588 | |||
1589 | /** |
||
1590 | * Gets all revisions from a given record ID. |
||
1591 | * |
||
1592 | * @param int $recordId Record id |
||
1593 | * @param string $recordLang Record language |
||
1594 | * |
||
1595 | * @return array |
||
1596 | */ |
||
1597 | public function getRevisionIds($recordId, $recordLang) |
||
1598 | { |
||
1599 | $revisionData = []; |
||
1600 | |||
1601 | $query = sprintf(" |
||
1602 | SELECT |
||
1603 | revision_id, updated, author |
||
1604 | FROM |
||
1605 | %sfaqdata_revisions |
||
1606 | WHERE |
||
1607 | id = %d |
||
1608 | AND |
||
1609 | lang = '%s' |
||
1610 | ORDER BY |
||
1611 | revision_id", |
||
1612 | PMF_Db::getTablePrefix(), |
||
1613 | $recordId, |
||
1614 | $recordLang |
||
1615 | ); |
||
1616 | |||
1617 | $result = $this->_config->getDb()->query($query); |
||
1618 | |||
1619 | if ($this->_config->getDb()->numRows($result) > 0) { |
||
1620 | while ($row = $this->_config->getDb()->fetchObject($result)) { |
||
1621 | $revisionData[] = [ |
||
1622 | 'revision_id' => $row->revision_id, |
||
1623 | 'updated' => $row->updated, |
||
1624 | 'author' => $row->author, |
||
1625 | ]; |
||
1626 | } |
||
1627 | } |
||
1628 | |||
1629 | return $revisionData; |
||
1630 | } |
||
1631 | |||
1632 | /** |
||
1633 | * Adds a new revision from a given record ID. |
||
1634 | * |
||
1635 | * @param int $record_id Record id |
||
1636 | * @param string $record_lang Record language |
||
1637 | * |
||
1638 | * @return array |
||
1639 | */ |
||
1640 | public function addNewRevision($record_id, $record_lang) |
||
1641 | { |
||
1642 | $query = sprintf(" |
||
1643 | INSERT INTO |
||
1644 | %sfaqdata_revisions |
||
1645 | SELECT * FROM |
||
1646 | %sfaqdata |
||
1647 | WHERE |
||
1648 | id = %d |
||
1649 | AND |
||
1650 | lang = '%s'", |
||
1651 | PMF_Db::getTablePrefix(), |
||
1652 | PMF_Db::getTablePrefix(), |
||
1653 | $record_id, |
||
1654 | $record_lang); |
||
1655 | $this->_config->getDb()->query($query); |
||
1656 | |||
1657 | return true; |
||
1658 | } |
||
1659 | |||
1660 | /** |
||
1661 | * Returns the keywords of a FAQ record from the ID and language. |
||
1662 | * |
||
1663 | * @param int $id record id |
||
1664 | * |
||
1665 | * @return string |
||
1666 | */ |
||
1667 | public function getRecordKeywords($id) |
||
1668 | { |
||
1669 | if (isset($this->faqRecord['id']) && ($this->faqRecord['id'] == $id)) { |
||
1670 | return $this->faqRecord['keywords']; |
||
1671 | } |
||
1672 | |||
1673 | $query = sprintf( |
||
1674 | "SELECT |
||
1675 | keywords |
||
1676 | FROM |
||
1677 | %sfaqdata |
||
1678 | WHERE id = %d AND lang = '%s'", |
||
1679 | PMF_Db::getTablePrefix(), |
||
1680 | $id, |
||
1681 | $this->_config->getLanguage()->getLanguage()); |
||
1682 | |||
1683 | $result = $this->_config->getDb()->query($query); |
||
1684 | |||
1685 | if ($this->_config->getDb()->numRows($result) > 0) { |
||
1686 | $row = $this->_config->getDb()->fetchObject($result); |
||
1687 | |||
1688 | return PMF_String::htmlspecialchars($row->keywords, ENT_QUOTES, 'utf-8'); |
||
1689 | } else { |
||
1690 | return ''; |
||
1691 | } |
||
1692 | } |
||
1693 | |||
1694 | /** |
||
1695 | * Returns a answer preview of the FAQ record. |
||
1696 | * |
||
1697 | * @param int $recordId FAQ record ID |
||
1698 | * @param int $wordCount Number of words, default: 12 |
||
1699 | * |
||
1700 | * @return string |
||
1701 | */ |
||
1702 | public function getRecordPreview($recordId, $wordCount = 12) |
||
1703 | { |
||
1704 | $answerPreview = ''; |
||
1705 | |||
1706 | if (isset($this->faqRecord['id']) && ($this->faqRecord['id'] == $recordId)) { |
||
1707 | $answerPreview = $this->faqRecord['content']; |
||
1708 | } |
||
1709 | |||
1710 | $query = sprintf(" |
||
1711 | SELECT |
||
1712 | content as answer |
||
1713 | FROM |
||
1714 | %sfaqdata |
||
1715 | WHERE |
||
1716 | id = %d |
||
1717 | AND |
||
1718 | lang = '%s'", |
||
1719 | PMF_Db::getTablePrefix(), |
||
1720 | $recordId, |
||
1721 | $this->_config->getLanguage()->getLanguage()); |
||
1722 | |||
1723 | $result = $this->_config->getDb()->query($query); |
||
1724 | |||
1725 | View Code Duplication | if ($this->_config->getDb()->numRows($result) > 0) { |
|
1726 | $row = $this->_config->getDb()->fetchObject($result); |
||
1727 | $answerPreview = strip_tags($row->answer); |
||
1728 | } else { |
||
1729 | $answerPreview = $this->_config->get('main.metaDescription'); |
||
1730 | } |
||
1731 | |||
1732 | return PMF_Utils::makeShorterText($answerPreview, $wordCount); |
||
1733 | } |
||
1734 | |||
1735 | /** |
||
1736 | * Returns the number of activated and not expired records, optionally |
||
1737 | * not limited to the current language. |
||
1738 | * |
||
1739 | * @param string $language Language |
||
1740 | * |
||
1741 | * @return int |
||
1742 | */ |
||
1743 | public function getNumberOfRecords($language = null) |
||
1773 | |||
1774 | /** |
||
1775 | * This function generates a list with the most voted or most visited records. |
||
1776 | * |
||
1777 | * @param string $type Type definition visits/voted |
||
1778 | * |
||
1779 | * @since 2009-11-03 |
||
1780 | * |
||
1781 | * @author Max Köhler <[email protected]> |
||
1782 | * |
||
1783 | * @return array |
||
1784 | */ |
||
1785 | public function getTopTen($type = 'visits') |
||
1786 | { |
||
1787 | if ('visits' == $type) { |
||
1788 | $result = $this->getTopTenData(PMF_NUMBER_RECORDS_TOPTEN, 0, $this->_config->getLanguage()->getLanguage()); |
||
1789 | } else { |
||
1790 | $result = $this->getTopVotedData(PMF_NUMBER_RECORDS_TOPTEN, $this->_config->getLanguage()->getLanguage()); |
||
1791 | } |
||
1792 | $output = []; |
||
1793 | |||
1794 | if (count($result) > 0) { |
||
1795 | foreach ($result as $row) { |
||
1796 | if ('visits' == $type) { |
||
1797 | $output['title'][] = PMF_Utils::makeShorterText($row['question'], 8); |
||
1798 | $output['preview'][] = $row['question']; |
||
1799 | $output['url'][] = $row['url']; |
||
1800 | $output['visits'][] = $this->plr->GetMsg('plmsgViews', $row['visits']); |
||
1801 | } else { |
||
1802 | $output['title'][] = PMF_Utils::makeShorterText($row['question'], 8); |
||
1803 | $output['preview'][] = $row['question']; |
||
1804 | $output['url'][] = $row['url']; |
||
1805 | $output['voted'][] = sprintf( |
||
1806 | '%s %s 5 - %s', |
||
1807 | round($row['avg'], 2), |
||
1808 | $this->pmf_lang['msgVoteFrom'], |
||
1809 | $this->plr->GetMsg('plmsgVotes', $row['user']) |
||
1810 | ); |
||
1811 | } |
||
1812 | } |
||
1813 | } else { |
||
1814 | $output['error'] = $this->pmf_lang['err_noTopTen']; |
||
1815 | } |
||
1816 | |||
1817 | return $output; |
||
1818 | } |
||
1819 | |||
1820 | /** |
||
1821 | * This function generates the list with the latest published records. |
||
1822 | * |
||
1823 | * @return array |
||
1824 | */ |
||
1825 | public function getLatest() |
||
1826 | { |
||
1827 | $date = new PMF_Date($this->_config); |
||
1828 | $result = $this->getLatestData(PMF_NUMBER_RECORDS_LATEST, $this->_config->getLanguage()->getLanguage()); |
||
1829 | $output = []; |
||
1830 | |||
1831 | if (count($result) > 0) { |
||
1832 | foreach ($result as $row) { |
||
1833 | $output['url'][] = $row['url']; |
||
1834 | $output['title'][] = PMF_Utils::makeShorterText($row['question'], 8); |
||
1835 | $output['preview'][] = $row['question']; |
||
1836 | $output['date'][] = $date->format(PMF_Date::createIsoDate($row['date'])); |
||
1837 | } |
||
1838 | } else { |
||
1839 | $output['error'] = $this->pmf_lang['err_noArticles']; |
||
1840 | } |
||
1841 | |||
1842 | return $output; |
||
1843 | } |
||
1844 | |||
1845 | /** |
||
1846 | * Deletes a question for the table faqquestions. |
||
1847 | * |
||
1848 | * @param int $questionId |
||
1849 | * |
||
1850 | * @return bool |
||
1851 | */ |
||
1852 | public function deleteQuestion($questionId) |
||
1853 | { |
||
1854 | $delete = sprintf(" |
||
1855 | DELETE FROM |
||
1856 | %sfaqquestions |
||
1857 | WHERE |
||
1858 | id = %d |
||
1859 | AND |
||
1860 | lang = '%s'", |
||
1861 | PMF_Db::getTablePrefix(), |
||
1862 | $questionId, |
||
1863 | $this->_config->getLanguage()->getLanguage() |
||
1864 | ); |
||
1865 | |||
1866 | $this->_config->getDb()->query($delete); |
||
1867 | |||
1868 | return true; |
||
1869 | } |
||
1870 | |||
1871 | /** |
||
1872 | * Returns the visibility of a question. |
||
1873 | * |
||
1874 | * @param int $questionId |
||
1875 | * |
||
1876 | * @return string |
||
1877 | */ |
||
1878 | public function getVisibilityOfQuestion($questionId) |
||
1879 | { |
||
1880 | $query = sprintf(" |
||
1881 | SELECT |
||
1882 | is_visible |
||
1883 | FROM |
||
1884 | %sfaqquestions |
||
1885 | WHERE |
||
1886 | id = %d |
||
1887 | AND |
||
1888 | lang = '%s'", |
||
1889 | PMF_Db::getTablePrefix(), |
||
1890 | $questionId, |
||
1891 | $this->_config->getLanguage()->getLanguage() |
||
1892 | ); |
||
1893 | |||
1894 | $result = $this->_config->getDb()->query($query); |
||
1895 | if ($this->_config->getDb()->numRows($result) > 0) { |
||
1896 | $row = $this->_config->getDb()->fetchObject($result); |
||
1897 | |||
1898 | return $row->is_visible; |
||
1899 | } |
||
1900 | |||
1901 | return; |
||
1902 | } |
||
1903 | |||
1904 | /** |
||
1905 | * Sets the visibility of a question. |
||
1906 | * |
||
1907 | * @param int $questionId |
||
1908 | * @param string $isVisible |
||
1909 | * |
||
1910 | * @return bool |
||
1911 | */ |
||
1912 | public function setVisibilityOfQuestion($questionId, $isVisible) |
||
1913 | { |
||
1914 | $query = sprintf(" |
||
1915 | UPDATE |
||
1916 | %sfaqquestions |
||
1917 | SET |
||
1918 | is_visible = '%s' |
||
1919 | WHERE |
||
1920 | id = %d |
||
1921 | AND |
||
1922 | lang = '%s'", |
||
1923 | PMF_Db::getTablePrefix(), |
||
1924 | $isVisible, |
||
1925 | $questionId, |
||
1926 | $this->_config->getLanguage()->getLanguage() |
||
1927 | ); |
||
1928 | |||
1929 | $this->_config->getDb()->query($query); |
||
1930 | |||
1931 | return true; |
||
1932 | } |
||
1933 | |||
1934 | /** |
||
1935 | * This function generates a data-set with the most voted FAQs. |
||
1936 | * |
||
1937 | * @param int $count Number of records |
||
1938 | * @param string $language Language |
||
1939 | * |
||
1940 | * @return array |
||
1941 | */ |
||
1942 | public function getTopVotedData($count = PMF_NUMBER_RECORDS_TOPTEN, $language = null) |
||
2029 | |||
2030 | /** |
||
2031 | * This function generates the Top Ten data with the mosted viewed records. |
||
2032 | * |
||
2033 | * @param int $count Number of records |
||
2034 | * @param int $categoryId Category ID |
||
2035 | * @param string $language Language |
||
2036 | * |
||
2037 | * @return array |
||
2038 | */ |
||
2039 | public function getTopTenData($count = PMF_NUMBER_RECORDS_TOPTEN, $categoryId = 0, $language = null) |
||
2040 | { |
||
2041 | global $sids; |
||
2042 | |||
2043 | $now = date('YmdHis'); |
||
2044 | $query = |
||
2045 | ' SELECT |
||
2046 | fd.id AS id, |
||
2047 | fd.lang AS lang, |
||
2048 | fd.thema AS question, |
||
2049 | fd.updated AS updated, |
||
2050 | fcr.category_id AS category_id, |
||
2051 | fv.visits AS visits, |
||
2052 | fv.last_visit AS last_visit, |
||
2053 | fdg.group_id AS group_id, |
||
2142 | |||
2143 | /** |
||
2144 | * This function generates an array with a specified number of most recent |
||
2145 | * published records. |
||
2146 | * |
||
2147 | * @param int $count Number of records |
||
2148 | * @param string $language Language |
||
2149 | * |
||
2150 | * @return array |
||
2151 | */ |
||
2152 | public function getLatestData($count = PMF_NUMBER_RECORDS_LATEST, $language = null) |
||
2249 | |||
2250 | /** |
||
2251 | * Reload locking for user votings. |
||
2252 | * |
||
2253 | * @param int $id FAQ record id |
||
2254 | * @param string $ip IP |
||
2255 | * |
||
2256 | * @return bool |
||
2257 | */ |
||
2258 | public function votingCheck($id, $ip) |
||
2278 | |||
2279 | /** |
||
2280 | * Returns the number of users from the table faqvotings. |
||
2281 | * |
||
2282 | * @param integer $record_id |
||
2283 | * |
||
2284 | * @return integer |
||
2285 | */ |
||
2286 | View Code Duplication | public function getNumberOfVotings($record_id) |
|
2305 | |||
2306 | /** |
||
2307 | * Adds a new voting record. |
||
2308 | * |
||
2309 | * @param array $votingData |
||
2310 | * |
||
2311 | * @return bool |
||
2312 | */ |
||
2313 | public function addVoting($votingData) |
||
2334 | |||
2335 | /** |
||
2336 | * Adds a new question. |
||
2337 | * |
||
2338 | * @param array $questionData |
||
2339 | * |
||
2340 | * @return bool |
||
2341 | */ |
||
2342 | public function addQuestion(Array $questionData) |
||
2364 | |||
2365 | /** |
||
2366 | * Returns a new question. |
||
2367 | * |
||
2368 | * @param int $questionId |
||
2369 | * |
||
2370 | * @return array |
||
2371 | */ |
||
2372 | public function getQuestion($questionId) |
||
2421 | |||
2422 | /** |
||
2423 | * Returns all open questions. |
||
2424 | * |
||
2425 | * @param boolean $all If true, then return visible and non-visible |
||
2426 | * questions; otherwise only visible ones |
||
2427 | * |
||
2428 | * @return array |
||
2429 | */ |
||
2430 | public function getAllOpenQuestions($all = true) |
||
2467 | |||
2468 | /** |
||
2469 | * Updates an existing voting record. |
||
2470 | * |
||
2471 | * @param array $votingData |
||
2472 | * |
||
2473 | * @return bool |
||
2474 | */ |
||
2475 | public function updateVoting($votingData) |
||
2500 | |||
2501 | /** |
||
2502 | * Adds a new changelog entry in the table faqchanges. |
||
2503 | * |
||
2504 | * @param int $id |
||
2505 | * @param int $userId |
||
2506 | * @param string $text |
||
2507 | * @param string $lang |
||
2508 | * @param int $revision_id |
||
2509 | * |
||
2510 | * @return bool |
||
2511 | */ |
||
2512 | public function createChangeEntry($id, $userId, $text, $lang, $revision_id = 0) |
||
2541 | |||
2542 | /** |
||
2543 | * Returns the changelog of a FAQ record. |
||
2544 | * |
||
2545 | * @param int $record_id |
||
2546 | * |
||
2547 | * @return array |
||
2548 | */ |
||
2549 | View Code Duplication | public function getChangeEntries($record_id) |
|
2577 | |||
2578 | /** |
||
2579 | * Retrieve faq records according to the constraints provided. |
||
2580 | * |
||
2581 | * @param string $queryType |
||
2582 | * @param int $nCatid |
||
2583 | * @param bool $bDownwards |
||
2584 | * @param string $lang |
||
2585 | * @param string $date |
||
2586 | * |
||
2587 | * @return array |
||
2588 | */ |
||
2589 | public function get($queryType = FAQ_QUERY_TYPE_DEFAULT, $nCatid = 0, $bDownwards = true, $lang = '', $date = '') |
||
2623 | |||
2624 | /** |
||
2625 | * Build a logic sequence, for a WHERE statement, of those category IDs |
||
2626 | * children of the provided category ID, if any. |
||
2627 | * |
||
2628 | * @param $nCatid |
||
2629 | * @param $logicOp |
||
2630 | * @param $oCat |
||
2631 | * |
||
2632 | * @return string |
||
2633 | */ |
||
2634 | public function _getCatidWhereSequence($nCatid, $logicOp = 'OR', $oCat = null) |
||
2650 | |||
2651 | /** |
||
2652 | * Build the SQL query for retrieving faq records according to the constraints provided. |
||
2653 | * |
||
2654 | * @param $QueryType |
||
2655 | * @param $nCatid |
||
2656 | * @param $bDownwards |
||
2657 | * @param $lang |
||
2658 | * @param $date |
||
2659 | * @param $faqid |
||
2660 | * |
||
2661 | * @return array |
||
2662 | */ |
||
2663 | private function _getSQLQuery($QueryType, $nCatid, $bDownwards, $lang, $date, $faqid = 0) |
||
2777 | |||
2778 | /** |
||
2779 | * Adds the record permissions for users and groups. |
||
2780 | * |
||
2781 | * @param string $mode 'group' or 'user' |
||
2782 | * @param int $recordId ID of the current record |
||
2783 | * @param array $ids Array of group or user IDs |
||
2784 | * |
||
2785 | * @return bool |
||
2786 | */ |
||
2787 | public function addPermission($mode, $recordId, $ids) |
||
2812 | |||
2813 | /** |
||
2814 | * Deletes the record permissions for users and groups. |
||
2815 | * |
||
2816 | * @param string $mode 'group' or 'user' |
||
2817 | * @param int $record_id ID of the current record |
||
2818 | * |
||
2819 | * @return bool |
||
2820 | * |
||
2821 | * @author Thorsten Rinne <[email protected]> |
||
2822 | */ |
||
2823 | View Code Duplication | public function deletePermission($mode, $record_id) |
|
2844 | |||
2845 | /** |
||
2846 | * Returns the record permissions for users and groups. |
||
2847 | * |
||
2848 | * @param string $mode 'group' or 'user' |
||
2849 | * @param int $recordId |
||
2850 | * |
||
2851 | * @return array |
||
2852 | */ |
||
2853 | public function getPermission($mode, $recordId) |
||
2883 | |||
2884 | /** |
||
2885 | * Returns all records of one category. |
||
2886 | * |
||
2887 | * @param int $category |
||
2888 | * |
||
2889 | * @return string |
||
2890 | */ |
||
2891 | public function showAllRecordsWoPaging($category) |
||
2969 | |||
2970 | /** |
||
2971 | * Prints the open questions as a XHTML table. |
||
2972 | * |
||
2973 | * @return string |
||
2974 | */ |
||
2975 | public function printOpenQuestions() |
||
3069 | |||
3070 | /** |
||
3071 | * Set or unset a faq item flag. |
||
3072 | * |
||
3073 | * @param int $id Record id |
||
3074 | * @param string $lang language code which is valid with Language::isASupportedLanguage |
||
3075 | * @param bool $flag weither or not the record is set to sticky |
||
3076 | * @param string $type type of the flag to set, use the column name |
||
3077 | * |
||
3078 | * @return bool |
||
3079 | */ |
||
3080 | public function updateRecordFlag($id, $lang, $flag, $type) |
||
3121 | |||
3122 | /** |
||
3123 | * Returns the sticky records with URL and Title. |
||
3124 | * |
||
3125 | * @return array |
||
3126 | */ |
||
3127 | private function getStickyRecordsData() |
||
3205 | |||
3206 | /** |
||
3207 | * Prepares and returns the sticky records for the frontend. |
||
3208 | * |
||
3209 | * @return array |
||
3210 | */ |
||
3211 | public function getStickyRecords() |
||
3242 | |||
3243 | /** |
||
3244 | * Updates field answer_id in faqquestion. |
||
3245 | * |
||
3246 | * @param int $openQuestionId |
||
3247 | * @param int $faqId |
||
3248 | * @param int $categoryId |
||
3249 | * |
||
3250 | * @return bool |
||
3251 | */ |
||
3252 | View Code Duplication | public function updateQuestionAnswer($openQuestionId, $faqId, $categoryId) |
|
3264 | |||
3265 | /** |
||
3266 | * Returns a part of a query to check permissions. |
||
3267 | * |
||
3268 | * @param bool $hasGroupSupport |
||
3269 | * |
||
3270 | * @return string |
||
3271 | */ |
||
3272 | protected function queryPermission($hasGroupSupport = false) |
||
3303 | } |
||
3304 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.