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 |
||
70 | class PMF_Faq |
||
71 | { |
||
72 | /** |
||
73 | * @var PMF_Configuration |
||
74 | */ |
||
75 | private $_config; |
||
76 | |||
77 | /** |
||
78 | * Language strings |
||
79 | * |
||
80 | * @var string |
||
81 | */ |
||
82 | private $pmf_lang; |
||
83 | |||
84 | /** |
||
85 | * Plural form support |
||
86 | * |
||
87 | * @var PMF_Language_Plurals |
||
88 | */ |
||
89 | private $plr; |
||
90 | |||
91 | /** |
||
92 | * The current FAQ record |
||
93 | * |
||
94 | * @var array |
||
95 | */ |
||
96 | public $faqRecord = []; |
||
97 | |||
98 | /** |
||
99 | * All current FAQ records in an array |
||
100 | * |
||
101 | * @var array |
||
102 | */ |
||
103 | public $faqRecords = []; |
||
104 | |||
105 | /** |
||
106 | * Users |
||
107 | * |
||
108 | * @var integer |
||
109 | */ |
||
110 | private $user = -1; |
||
111 | |||
112 | /** |
||
113 | * Groups |
||
114 | * |
||
115 | * @var array |
||
116 | */ |
||
117 | private $groups = array(-1); |
||
118 | |||
119 | /** |
||
120 | * Flag for Group support |
||
121 | * |
||
122 | * @var boolean |
||
123 | */ |
||
124 | private $groupSupport = false; |
||
125 | |||
126 | /** |
||
127 | * Constructor |
||
128 | * |
||
129 | * @param PMF_Configuration $config |
||
130 | * |
||
131 | * @return PMF_Faq |
||
132 | */ |
||
133 | public function __construct(PMF_Configuration $config) |
||
134 | { |
||
135 | global $PMF_LANG, $plr; |
||
136 | |||
137 | $this->_config = $config; |
||
138 | $this->pmf_lang = $PMF_LANG; |
||
139 | $this->plr = $plr; |
||
140 | |||
141 | if ($this->_config->get('security.permLevel') == 'medium') { |
||
142 | $this->groupSupport = true; |
||
143 | } |
||
144 | } |
||
145 | |||
146 | // |
||
147 | // |
||
148 | // PUBLIC METHODS |
||
149 | // |
||
150 | // |
||
151 | |||
152 | /** |
||
153 | * @param integer $userId |
||
154 | */ |
||
155 | public function setUser($userId = -1) |
||
156 | { |
||
157 | $this->user = $userId; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @param array $groups |
||
162 | */ |
||
163 | public function setGroups(Array $groups) |
||
164 | { |
||
165 | $this->groups = $groups; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * This function returns all not expired records from one category |
||
170 | * |
||
171 | * @param int $category_id Category ID |
||
172 | * @param string $orderby Order by |
||
173 | * @param string $sortby Sorty by |
||
174 | * @return array |
||
175 | */ |
||
176 | public function getAllRecordPerCategory($category_id, $orderby = 'id', $sortby = 'ASC') |
||
177 | { |
||
178 | global $sids; |
||
179 | |||
180 | $faqdata = []; |
||
181 | |||
182 | if ($orderby == 'visits') { |
||
183 | $current_table = 'fv'; |
||
184 | } else { |
||
185 | $current_table = 'fd'; |
||
186 | } |
||
187 | |||
188 | $now = date('YmdHis'); |
||
189 | $query = sprintf(" |
||
190 | SELECT |
||
191 | fd.id AS id, |
||
192 | fd.lang AS lang, |
||
193 | fd.thema AS thema, |
||
194 | fd.content AS record_content, |
||
195 | fd.datum AS record_date, |
||
196 | fcr.category_id AS category_id, |
||
197 | fv.visits AS visits |
||
198 | FROM |
||
199 | %sfaqdata AS fd |
||
200 | LEFT JOIN |
||
201 | %sfaqcategoryrelations AS fcr |
||
202 | ON |
||
203 | fd.id = fcr.record_id |
||
204 | AND |
||
205 | fd.lang = fcr.record_lang |
||
206 | LEFT JOIN |
||
207 | %sfaqvisits AS fv |
||
208 | ON |
||
209 | fd.id = fv.id |
||
210 | AND |
||
211 | fv.lang = fd.lang |
||
212 | LEFT JOIN |
||
213 | %sfaqdata_group AS fdg |
||
214 | ON |
||
215 | fd.id = fdg.record_id |
||
216 | LEFT JOIN |
||
217 | %sfaqdata_user AS fdu |
||
218 | ON |
||
219 | fd.id = fdu.record_id |
||
220 | WHERE |
||
221 | fd.date_start <= '%s' |
||
222 | AND |
||
223 | fd.date_end >= '%s' |
||
224 | AND |
||
225 | fd.active = 'yes' |
||
226 | AND |
||
227 | fcr.category_id = %d |
||
228 | AND |
||
229 | fd.lang = '%s' |
||
230 | %s |
||
231 | ORDER BY |
||
232 | %s.%s %s", |
||
233 | PMF_Db::getTablePrefix(), |
||
234 | PMF_Db::getTablePrefix(), |
||
235 | PMF_Db::getTablePrefix(), |
||
236 | PMF_Db::getTablePrefix(), |
||
237 | PMF_Db::getTablePrefix(), |
||
238 | $now, |
||
239 | $now, |
||
240 | $category_id, |
||
241 | $this->_config->getLanguage()->getLanguage(), |
||
242 | $this->queryPermission($this->groupSupport), |
||
243 | $current_table, |
||
244 | $this->_config->getDb()->escape($orderby), |
||
245 | $this->_config->getDb()->escape($sortby)); |
||
246 | |||
247 | $result = $this->_config->getDb()->query($query); |
||
248 | $num = $this->_config->getDb()->numRows($result); |
||
249 | |||
250 | if ($num > 0) { |
||
251 | while (($row = $this->_config->getDb()->fetchObject($result))) { |
||
252 | |||
253 | if (empty($row->visits)) { |
||
254 | $visits = 0; |
||
255 | } else { |
||
256 | $visits = $row->visits; |
||
257 | } |
||
258 | |||
259 | $url = sprintf( |
||
260 | '%s?%saction=artikel&cat=%d&id=%d&artlang=%s', |
||
261 | PMF_Link::getSystemRelativeUri(), |
||
262 | $sids, |
||
263 | $row->category_id, |
||
264 | $row->id, |
||
265 | $row->lang |
||
266 | ); |
||
267 | $oLink = new PMF_Link($url, $this->_config); |
||
268 | $oLink->itemTitle = $oLink->text = $oLink->tooltip = $row->thema;; |
||
269 | |||
270 | $faqdata[] = array( |
||
271 | 'record_id' => $row->id, |
||
272 | 'record_lang' => $row->lang, |
||
273 | 'category_id' => $row->category_id, |
||
274 | 'record_title' => $row->thema, |
||
275 | 'record_preview' => PMF_Utils::chopString(strip_tags($row->record_content), 25), |
||
276 | 'record_link' => $oLink->toString(), |
||
277 | 'record_date' => $row->record_date, |
||
278 | 'visits' => $visits |
||
279 | ); |
||
280 | } |
||
281 | } else { |
||
282 | return $faqdata; |
||
283 | } |
||
284 | |||
285 | return $faqdata; |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * This function returns all not expired records from one category |
||
290 | * |
||
291 | * @param integer $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') |
||
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 | * @return string |
||
498 | */ |
||
499 | public function showAllRecordsByIds(Array $record_ids, $orderby = 'fd.id', $sortby = 'ASC') |
||
500 | { |
||
501 | global $sids; |
||
502 | |||
503 | $records = implode(', ', $record_ids); |
||
504 | $page = PMF_Filter::filterInput(INPUT_GET, 'seite', FILTER_VALIDATE_INT, 1); |
||
505 | $tagging_id = PMF_Filter::filterInput(INPUT_GET, 'tagging_id', FILTER_VALIDATE_INT); |
||
506 | $output = ''; |
||
507 | |||
508 | $now = date('YmdHis'); |
||
509 | $query = sprintf(" |
||
510 | SELECT |
||
511 | fd.id AS id, |
||
512 | fd.lang AS lang, |
||
513 | fd.thema AS thema, |
||
514 | fcr.category_id AS category_id, |
||
515 | fv.visits AS visits |
||
516 | FROM |
||
517 | %sfaqdata AS fd |
||
518 | LEFT JOIN |
||
519 | %sfaqcategoryrelations AS fcr |
||
520 | ON |
||
521 | fd.id = fcr.record_id |
||
522 | AND |
||
523 | fd.lang = fcr.record_lang |
||
524 | LEFT JOIN |
||
525 | %sfaqvisits AS fv |
||
526 | ON |
||
527 | fd.id = fv.id |
||
528 | AND |
||
529 | fv.lang = fd.lang |
||
530 | LEFT JOIN |
||
531 | %sfaqdata_group AS fdg |
||
532 | ON |
||
533 | fd.id = fdg.record_id |
||
534 | LEFT JOIN |
||
535 | %sfaqdata_user AS fdu |
||
536 | ON |
||
537 | fd.id = fdu.record_id |
||
538 | WHERE |
||
539 | fd.date_start <= '%s' |
||
540 | AND |
||
541 | fd.date_end >= '%s' |
||
542 | AND |
||
543 | fd.active = 'yes' |
||
544 | AND |
||
545 | fd.id IN (%s) |
||
546 | AND |
||
547 | fd.lang = '%s' |
||
548 | %s |
||
549 | ORDER BY |
||
550 | %s %s", |
||
551 | PMF_Db::getTablePrefix(), |
||
552 | PMF_Db::getTablePrefix(), |
||
553 | PMF_Db::getTablePrefix(), |
||
554 | PMF_Db::getTablePrefix(), |
||
555 | PMF_Db::getTablePrefix(), |
||
556 | $now, |
||
557 | $now, |
||
558 | $records, |
||
559 | $this->_config->getLanguage()->getLanguage(), |
||
560 | $this->queryPermission($this->groupSupport), |
||
561 | $this->_config->getDb()->escape($orderby), |
||
562 | $this->_config->getDb()->escape($sortby)); |
||
563 | |||
564 | $result = $this->_config->getDb()->query($query); |
||
565 | |||
566 | $num = $this->_config->getDb()->numRows($result); |
||
567 | $pages = ceil($num / $this->_config->get('records.numberOfRecordsPerPage')); |
||
568 | |||
569 | if ($page == 1) { |
||
570 | $first = 0; |
||
571 | } else { |
||
572 | $first = ($page * $this->_config->get('records.numberOfRecordsPerPage')) - $this->_config->get('records.numberOfRecordsPerPage'); |
||
573 | } |
||
574 | |||
575 | if ($num > 0) { |
||
576 | View Code Duplication | if ($pages > 1) { |
|
577 | $output .= sprintf('<p><strong>%s %s %s</strong></p>', |
||
578 | $this->pmf_lang['msgPage'] . $page, |
||
579 | $this->pmf_lang['msgVoteFrom'], |
||
580 | $pages . $this->pmf_lang['msgPages']); |
||
581 | } |
||
582 | $output .= '<ul class="phpmyfaq_ul">'; |
||
583 | $counter = 0; |
||
584 | $displayedCounter = 0; |
||
585 | |||
586 | $lastFaqId = 0; |
||
587 | while (($row = $this->_config->getDb()->fetchObject($result)) && $displayedCounter < $this->_config->get('records.numberOfRecordsPerPage')) { |
||
588 | $counter ++; |
||
589 | if ($counter <= $first) { |
||
590 | continue; |
||
591 | } |
||
592 | $displayedCounter++; |
||
593 | |||
594 | if ($lastFaqId == $row->id) { |
||
595 | continue; // Don't show multiple FAQs |
||
596 | } |
||
597 | |||
598 | if (empty($row->visits)) { |
||
599 | $visits = 0; |
||
600 | } else { |
||
601 | $visits = $row->visits; |
||
602 | } |
||
603 | |||
604 | $title = $row->thema; |
||
605 | $url = sprintf( |
||
606 | '%s?%saction=artikel&cat=%d&id=%d&artlang=%s', |
||
607 | PMF_Link::getSystemRelativeUri(), |
||
608 | $sids, |
||
609 | $row->category_id, |
||
610 | $row->id, |
||
611 | $row->lang |
||
612 | ); |
||
613 | $oLink = new PMF_Link($url, $this->_config); |
||
614 | $oLink->itemTitle = $row->thema; |
||
615 | $oLink->text = $title; |
||
616 | $oLink->tooltip = $title; |
||
617 | $listItem = sprintf( |
||
618 | '<li>%s<br /><small>(%s)</small></li>', |
||
619 | $oLink->toHtmlAnchor(), |
||
620 | $this->plr->GetMsg('plmsgViews',$visits) |
||
621 | ); |
||
622 | |||
623 | $output .= $listItem; |
||
624 | |||
625 | $lastFaqId = $row->id; |
||
626 | } |
||
627 | $output .= '</ul><span id="totFaqRecords" style="display: none;">'.$num.'</span>'; |
||
628 | } else { |
||
629 | return false; |
||
630 | } |
||
631 | |||
632 | if ($num > $this->_config->get('records.numberOfRecordsPerPage')) { |
||
633 | $output .= "<p class=\"text-center\"><strong>"; |
||
634 | if (!isset($page)) { |
||
635 | $page = 1; |
||
636 | } |
||
637 | $vor = $page - 1; |
||
638 | $next = $page + 1; |
||
639 | View Code Duplication | if ($vor != 0) { |
|
640 | $url = $sids.'&action=search&tagging_id='.$tagging_id.'&seite='.$vor; |
||
641 | $oLink = new PMF_Link(PMF_Link::getSystemRelativeUri().'?'.$url, $this->_config); |
||
642 | $oLink->itemTitle = 'tag'; |
||
643 | $oLink->text = $this->pmf_lang["msgPrevious"]; |
||
644 | $oLink->tooltip = $this->pmf_lang["msgPrevious"]; |
||
645 | $output .= '[ '.$oLink->toHtmlAnchor().' ]'; |
||
646 | } |
||
647 | $output .= " "; |
||
648 | View Code Duplication | if ($next <= $pages) { |
|
649 | $url = $sids.'&action=search&tagging_id='.$tagging_id.'&seite='.$next; |
||
650 | $oLink = new PMF_Link(PMF_Link::getSystemRelativeUri().'?'.$url, $this->_config); |
||
651 | $oLink->itemTitle = 'tag'; |
||
652 | $oLink->text = $this->pmf_lang["msgNext"]; |
||
653 | $oLink->tooltip = $this->pmf_lang["msgNext"]; |
||
654 | $output .= '[ '.$oLink->toHtmlAnchor().' ]'; |
||
655 | } |
||
656 | $output .= "</strong></p>"; |
||
657 | } |
||
658 | |||
659 | return $output; |
||
660 | } |
||
661 | |||
662 | /** |
||
663 | * Returns an array with all data from a FAQ record |
||
664 | * |
||
665 | * @param integer $id Record id |
||
666 | * @param integer $revisionId Revision id |
||
667 | * @param boolean $isAdmin Must be true if it is called by an admin/author context |
||
668 | * |
||
669 | * @return void |
||
670 | */ |
||
671 | public function getRecord($id, $revisionId = null, $isAdmin = false) |
||
672 | { |
||
673 | global $PMF_LANG; |
||
674 | |||
675 | $query = sprintf( |
||
676 | "SELECT |
||
677 | id, lang, solution_id, revision_id, active, sticky, keywords, |
||
678 | thema, content, author, email, comment, datum, links_state, |
||
679 | links_check_date, date_start, date_end |
||
680 | FROM |
||
681 | %s%s fd |
||
682 | LEFT JOIN |
||
683 | %sfaqdata_group fdg |
||
684 | ON |
||
685 | fd.id = fdg.record_id |
||
686 | LEFT JOIN |
||
687 | %sfaqdata_user fdu |
||
688 | ON |
||
689 | fd.id = fdu.record_id |
||
690 | WHERE |
||
691 | fd.id = %d |
||
692 | %s |
||
693 | AND |
||
694 | fd.lang = '%s' |
||
695 | %s", |
||
696 | PMF_Db::getTablePrefix(), |
||
697 | isset($revisionId) ? 'faqdata_revisions': 'faqdata', |
||
698 | PMF_Db::getTablePrefix(), |
||
699 | PMF_Db::getTablePrefix(), |
||
700 | $id, |
||
701 | isset($revisionId) ? 'AND revision_id = '.$revisionId : '', |
||
702 | $this->_config->getLanguage()->getLanguage(), |
||
703 | ($isAdmin) ? 'AND 1=1' : $this->queryPermission($this->groupSupport) |
||
704 | ); |
||
705 | |||
706 | $result = $this->_config->getDb()->query($query); |
||
707 | |||
708 | if ($row = $this->_config->getDb()->fetchObject($result)) { |
||
709 | |||
710 | $question = nl2br($row->thema); |
||
711 | $content = $row->content; |
||
712 | $active = ('yes' == $row->active); |
||
713 | $expired = (date('YmdHis') > $row->date_end); |
||
714 | |||
715 | if (!$isAdmin) { |
||
716 | if (!$active) { |
||
717 | $content = $this->pmf_lang['err_inactiveArticle']; |
||
718 | } |
||
719 | if ($expired) { |
||
720 | $content = $this->pmf_lang['err_expiredArticle']; |
||
721 | } |
||
722 | } |
||
723 | |||
724 | $this->faqRecord = array( |
||
725 | 'id' => $row->id, |
||
726 | 'lang' => $row->lang, |
||
727 | 'solution_id' => $row->solution_id, |
||
728 | 'revision_id' => $row->revision_id, |
||
729 | 'active' => $row->active, |
||
730 | 'sticky' => $row->sticky, |
||
731 | 'keywords' => $row->keywords, |
||
732 | 'title' => $question, |
||
733 | 'content' => $content, |
||
734 | 'author' => $row->author, |
||
735 | 'email' => $row->email, |
||
736 | 'comment' => $row->comment, |
||
737 | 'date' => PMF_Date::createIsoDate($row->datum), |
||
738 | 'dateStart' => $row->date_start, |
||
739 | 'dateEnd' => $row->date_end, |
||
740 | 'linkState' => $row->links_state, |
||
741 | 'linkCheckDate' => $row->links_check_date |
||
742 | ); |
||
743 | } else { |
||
744 | $this->faqRecord = array( |
||
745 | 'id' => $id, |
||
746 | 'lang' => $this->_config->getLanguage()->getLanguage(), |
||
747 | 'solution_id' => 42, |
||
748 | 'revision_id' => 0, |
||
749 | 'active' => 'no', |
||
750 | 'sticky' => 0, |
||
751 | 'keywords' => '', |
||
752 | 'title' => '', |
||
753 | 'content' => $PMF_LANG['msgAccessDenied'], |
||
754 | 'author' => '', |
||
755 | 'email' => '', |
||
756 | 'comment' => '', |
||
757 | 'date' => PMF_Date::createIsoDate(date('YmdHis')), |
||
758 | 'dateStart' => '', |
||
759 | 'dateEnd' => '', |
||
760 | 'linkState' => '', |
||
761 | 'linkCheckDate' => '' |
||
762 | ); |
||
763 | } |
||
764 | } |
||
765 | |||
766 | /** |
||
767 | * Adds a new record |
||
768 | * |
||
769 | * @param array $data Array of FAQ data |
||
770 | * @param boolean $new_record New record? |
||
771 | * @return integer |
||
772 | */ |
||
773 | public function addRecord(Array $data, $new_record = true) |
||
774 | { |
||
775 | if ($new_record) { |
||
776 | $record_id = $this->_config->getDb()->nextId(PMF_Db::getTablePrefix().'faqdata', 'id'); |
||
777 | } else { |
||
778 | $record_id = $data['id']; |
||
779 | } |
||
780 | |||
781 | // Add new entry |
||
782 | $query = sprintf( |
||
783 | "INSERT INTO |
||
784 | %sfaqdata |
||
785 | VALUES |
||
786 | (%d, '%s', %d, %d, '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, '%s', '%s')", |
||
787 | PMF_Db::getTablePrefix(), |
||
788 | $record_id, |
||
789 | $data['lang'], |
||
790 | $this->getSolutionId(), |
||
791 | 0, |
||
792 | $data['active'], |
||
793 | $data['sticky'], |
||
794 | $this->_config->getDb()->escape($data['keywords']), |
||
795 | $this->_config->getDb()->escape($data['thema']), |
||
796 | $this->_config->getDb()->escape($data['content']), |
||
797 | $this->_config->getDb()->escape($data['author']), |
||
798 | $data['email'], |
||
799 | $data['comment'], |
||
800 | $data['date'], |
||
801 | $data['linkState'], |
||
802 | $data['linkDateCheck'], |
||
803 | $data['dateStart'], |
||
804 | $data['dateEnd']); |
||
805 | |||
806 | $this->_config->getDb()->query($query); |
||
807 | return $record_id; |
||
808 | } |
||
809 | |||
810 | /** |
||
811 | * Updates a record |
||
812 | * |
||
813 | * @param array $data Array of FAQ data |
||
814 | * @return boolean |
||
815 | */ |
||
816 | public function updateRecord(Array $data) |
||
817 | { |
||
818 | // Update entry |
||
819 | $query = sprintf(" |
||
820 | UPDATE |
||
821 | %sfaqdata |
||
822 | SET |
||
823 | revision_id = %d, |
||
824 | active = '%s', |
||
825 | sticky = %d, |
||
826 | keywords = '%s', |
||
827 | thema = '%s', |
||
828 | content = '%s', |
||
829 | author = '%s', |
||
830 | email = '%s', |
||
831 | comment = '%s', |
||
832 | datum = '%s', |
||
833 | links_state = '%s', |
||
834 | links_check_date = %d, |
||
835 | date_start = '%s', |
||
836 | date_end = '%s' |
||
837 | WHERE |
||
838 | id = %d |
||
839 | AND |
||
840 | lang = '%s'", |
||
841 | PMF_Db::getTablePrefix(), |
||
842 | $data['revision_id'], |
||
843 | $data['active'], |
||
844 | $data['sticky'], |
||
845 | $this->_config->getDb()->escape($data['keywords']), |
||
846 | $this->_config->getDb()->escape($data['thema']), |
||
847 | $this->_config->getDb()->escape($data['content']), |
||
848 | $this->_config->getDb()->escape($data['author']), |
||
849 | $data['email'], |
||
850 | $data['comment'], |
||
851 | $data['date'], |
||
852 | $data['linkState'], |
||
853 | $data['linkDateCheck'], |
||
854 | $data['dateStart'], |
||
855 | $data['dateEnd'], |
||
856 | $data['id'], |
||
857 | $data['lang']); |
||
858 | |||
859 | $this->_config->getDb()->query($query); |
||
860 | return true; |
||
861 | } |
||
862 | |||
863 | /** |
||
864 | * Deletes a record and all the dependencies |
||
865 | * |
||
866 | * @param integer $recordId Record id |
||
867 | * @param string $recordLang Record language |
||
868 | * |
||
869 | * @return boolean |
||
870 | */ |
||
871 | public function deleteRecord($recordId, $recordLang) |
||
951 | |||
952 | /** |
||
953 | * Checks if a record is already translated |
||
954 | * |
||
955 | * @param integer $record_id Record id |
||
956 | * @param string $record_lang Record language |
||
957 | * @return boolean |
||
958 | */ |
||
959 | public function isAlreadyTranslated($record_id, $record_lang) |
||
960 | { |
||
961 | $query = sprintf(" |
||
962 | SELECT |
||
963 | id, lang |
||
964 | FROM |
||
965 | %sfaqdata |
||
966 | WHERE |
||
967 | id = %d |
||
968 | AND |
||
969 | lang = '%s'", |
||
970 | PMF_Db::getTablePrefix(), |
||
971 | $record_id, |
||
972 | $record_lang); |
||
973 | |||
974 | $result = $this->_config->getDb()->query($query); |
||
975 | |||
976 | if ($this->_config->getDb()->numRows($result)) { |
||
977 | return true; |
||
978 | } |
||
979 | |||
980 | return false; |
||
981 | } |
||
982 | |||
983 | /** |
||
984 | * Checks, if comments are disabled for the FAQ record |
||
985 | * |
||
986 | * @param integer $record_id Id of FAQ or news entry |
||
987 | * @param string $record_lang Language |
||
988 | * @param string $record_type Type of comment: faq or news |
||
989 | * @return boolean true, if comments are disabled |
||
990 | */ |
||
991 | public function commentDisabled($record_id, $record_lang, $record_type = 'faq') |
||
992 | { |
||
993 | if ('news' == $record_type) { |
||
994 | $table = 'faqnews'; |
||
995 | } else { |
||
996 | $table = 'faqdata'; |
||
997 | } |
||
998 | |||
999 | $query = sprintf(" |
||
1000 | SELECT |
||
1001 | comment |
||
1002 | FROM |
||
1003 | %s%s |
||
1004 | WHERE |
||
1005 | id = %d |
||
1006 | AND |
||
1007 | lang = '%s'", |
||
1008 | PMF_Db::getTablePrefix(), |
||
1009 | $table, |
||
1010 | $record_id, |
||
1011 | $record_lang); |
||
1012 | |||
1013 | $result = $this->_config->getDb()->query($query); |
||
1014 | |||
1015 | if ($row = $this->_config->getDb()->fetchObject($result)) { |
||
1016 | return ($row->comment === 'y') ? false : true; |
||
1017 | } else { |
||
1018 | return true; |
||
1019 | } |
||
1020 | } |
||
1021 | |||
1022 | /** |
||
1023 | * Adds new category relations to a record |
||
1024 | * |
||
1025 | * @param array $categories Array of categories |
||
1026 | * @param integer $record_id Record id |
||
1027 | * @param string $language Language |
||
1028 | * @return integer |
||
1029 | */ |
||
1030 | public function addCategoryRelations(Array $categories, $record_id, $language) |
||
1031 | { |
||
1032 | if (!is_array($categories)) { |
||
1033 | return false; |
||
1034 | } |
||
1035 | |||
1036 | foreach ($categories as $_category) { |
||
1037 | $this->_config->getDb()->query(sprintf( |
||
1038 | "INSERT INTO |
||
1039 | %sfaqcategoryrelations |
||
1040 | VALUES |
||
1041 | (%d, '%s', %d, '%s')", |
||
1042 | PMF_Db::getTablePrefix(), |
||
1043 | $_category, |
||
1044 | $language, |
||
1045 | $record_id, |
||
1046 | $language)); |
||
1047 | } |
||
1048 | |||
1049 | return true; |
||
1050 | } |
||
1051 | |||
1052 | /** |
||
1053 | * Adds new category relation to a record |
||
1054 | * |
||
1055 | * @param mixed $category Category or array of categories |
||
1056 | * @param integer $record_id Record id |
||
1057 | * @param string $language Language |
||
1058 | * |
||
1059 | * @return boolean |
||
1060 | */ |
||
1061 | public function addCategoryRelation($category, $record_id, $language) |
||
1062 | { |
||
1063 | // Just a fallback when (wrong case) $category is an array |
||
1064 | if (is_array($category)) { |
||
1065 | $this->addCategoryRelations($category, $record_id, $language); |
||
1066 | } |
||
1067 | $categories[] = $category; |
||
1068 | |||
1069 | return $this->addCategoryRelations($categories, $record_id, $language); |
||
1070 | } |
||
1071 | |||
1072 | /** |
||
1073 | * Deletes category relations to a record |
||
1074 | * |
||
1075 | * @param integer $record_id Record id |
||
1076 | * @param string $record_lang Language |
||
1077 | * @return boolean |
||
1078 | */ |
||
1079 | View Code Duplication | public function deleteCategoryRelations($record_id, $record_lang) |
|
|
|||
1080 | { |
||
1081 | $query = sprintf(" |
||
1082 | DELETE FROM |
||
1083 | %sfaqcategoryrelations |
||
1084 | WHERE |
||
1085 | record_id = %d |
||
1086 | AND |
||
1087 | record_lang = '%s'", |
||
1088 | PMF_Db::getTablePrefix(), |
||
1089 | $record_id, |
||
1090 | $record_lang); |
||
1091 | $this->_config->getDb()->query($query); |
||
1092 | |||
1093 | return true; |
||
1094 | } |
||
1095 | |||
1096 | /** |
||
1097 | * Returns an array with all data from a FAQ record |
||
1098 | * |
||
1099 | * @param integer $solutionId Solution ID |
||
1100 | * @return void |
||
1101 | */ |
||
1102 | public function getRecordBySolutionId($solutionId) |
||
1103 | { |
||
1104 | $query = sprintf( |
||
1105 | "SELECT |
||
1106 | * |
||
1107 | FROM |
||
1108 | %sfaqdata fd |
||
1109 | LEFT JOIN |
||
1110 | %sfaqdata_group fdg |
||
1111 | ON |
||
1112 | fd.id = fdg.record_id |
||
1113 | LEFT JOIN |
||
1114 | %sfaqdata_user fdu |
||
1115 | ON |
||
1116 | fd.id = fdu.record_id |
||
1117 | WHERE |
||
1118 | fd.solution_id = %d |
||
1119 | %s", |
||
1120 | PMF_Db::getTablePrefix(), |
||
1121 | PMF_Db::getTablePrefix(), |
||
1122 | PMF_Db::getTablePrefix(), |
||
1123 | $solutionId, |
||
1124 | $this->queryPermission($this->groupSupport) |
||
1125 | ); |
||
1126 | |||
1127 | $result = $this->_config->getDb()->query($query); |
||
1128 | |||
1129 | if ($row = $this->_config->getDb()->fetchObject($result)) { |
||
1130 | |||
1131 | $question = nl2br($row->thema); |
||
1132 | $content = $row->content; |
||
1133 | $active = ('yes' == $row->active); |
||
1134 | $expired = (date('YmdHis') > $row->date_end); |
||
1135 | |||
1136 | if (!$active) { |
||
1137 | $content = $this->pmf_lang['err_inactiveArticle']; |
||
1138 | } |
||
1139 | if ($expired) { |
||
1140 | $content = $this->pmf_lang['err_expiredArticle']; |
||
1141 | } |
||
1142 | |||
1143 | $this->faqRecord = array( |
||
1144 | 'id' => $row->id, |
||
1145 | 'lang' => $row->lang, |
||
1146 | 'solution_id' => $row->solution_id, |
||
1147 | 'revision_id' => $row->revision_id, |
||
1148 | 'active' => $row->active, |
||
1149 | 'sticky' => $row->sticky, |
||
1150 | 'keywords' => $row->keywords, |
||
1151 | 'title' => $question, |
||
1152 | 'content' => $content, |
||
1153 | 'author' => $row->author, |
||
1154 | 'email' => $row->email, |
||
1155 | 'comment' => $row->comment, |
||
1156 | 'date' => PMF_Date::createIsoDate($row->datum), |
||
1157 | 'dateStart' => $row->date_start, |
||
1158 | 'dateEnd' => $row->date_end, |
||
1159 | 'linkState' => $row->links_state, |
||
1160 | 'linkCheckDate' => $row->links_check_date |
||
1161 | ); |
||
1162 | } |
||
1163 | } |
||
1164 | |||
1165 | /** |
||
1166 | * Gets the record ID from a given solution ID |
||
1167 | * |
||
1168 | * @param integer $solution_id Solution ID |
||
1169 | * @return array |
||
1170 | */ |
||
1171 | public function getIdFromSolutionId($solution_id) |
||
1172 | { |
||
1173 | $query = sprintf(" |
||
1174 | SELECT |
||
1175 | id, lang, content |
||
1176 | FROM |
||
1177 | %sfaqdata |
||
1178 | WHERE |
||
1179 | solution_id = %d", |
||
1180 | PMF_Db::getTablePrefix(), |
||
1181 | $solution_id); |
||
1182 | |||
1183 | $result = $this->_config->getDb()->query($query); |
||
1184 | |||
1185 | if ($row = $this->_config->getDb()->fetchObject($result)) { |
||
1186 | return array('id' => $row->id, |
||
1187 | 'lang' => $row->lang, |
||
1188 | 'content' => $row->content); |
||
1189 | } |
||
1190 | |||
1191 | return null; |
||
1192 | } |
||
1193 | |||
1194 | /** |
||
1195 | * Gets the latest solution id for a FAQ record |
||
1196 | * |
||
1197 | * @return integer |
||
1198 | */ |
||
1199 | public function getSolutionId() |
||
1200 | { |
||
1201 | $latest_id = 0; |
||
1202 | $next_solution_id = 0; |
||
1203 | |||
1204 | $query = sprintf(' |
||
1205 | SELECT |
||
1206 | MAX(solution_id) AS solution_id |
||
1207 | FROM |
||
1208 | %sfaqdata', |
||
1209 | PMF_Db::getTablePrefix()); |
||
1210 | $result = $this->_config->getDb()->query($query); |
||
1211 | |||
1212 | if ($result && $row = $this->_config->getDb()->fetchObject($result)) { |
||
1213 | $latest_id = $row->solution_id; |
||
1214 | } |
||
1215 | |||
1216 | if ($latest_id < PMF_SOLUTION_ID_START_VALUE) { |
||
1217 | $next_solution_id = PMF_SOLUTION_ID_START_VALUE; |
||
1218 | } else { |
||
1219 | $next_solution_id = $latest_id + PMF_SOLUTION_ID_INCREMENT_VALUE; |
||
1220 | } |
||
1221 | |||
1222 | return $next_solution_id; |
||
1223 | } |
||
1224 | |||
1225 | /** |
||
1226 | * Returns an array with all data from all FAQ records |
||
1227 | * |
||
1228 | * @param integer $sortType Sorting type |
||
1229 | * @param array $condition Condition |
||
1230 | * @param string $sortOrder Sorting order |
||
1231 | * @return void |
||
1232 | */ |
||
1233 | public function getAllRecords($sortType = FAQ_SORTING_TYPE_CATID_FAQID, Array $condition = null, $sortOrder = 'ASC') |
||
1234 | { |
||
1235 | $where = ''; |
||
1236 | if (!is_null($condition)) { |
||
1237 | $num = count($condition); |
||
1238 | $where = 'WHERE '; |
||
1239 | foreach ($condition as $field => $data) { |
||
1240 | $num--; |
||
1241 | $where .= $field; |
||
1242 | if (is_array($data)) { |
||
1243 | $where .= " IN ("; |
||
1244 | $separator = ""; |
||
1245 | foreach ($data as $value) { |
||
1246 | $where .= $separator."'".$this->_config->getDb()->escape($value)."'"; |
||
1247 | $separator = ", "; |
||
1248 | } |
||
1249 | $where .= ")"; |
||
1250 | } else { |
||
1251 | $where .= " = '".$this->_config->getDb()->escape($data)."'"; |
||
1252 | } |
||
1253 | if ($num > 0) { |
||
1254 | $where .= " AND "; |
||
1255 | } |
||
1256 | } |
||
1257 | } |
||
1258 | |||
1259 | switch ($sortType) { |
||
1260 | |||
1261 | case FAQ_SORTING_TYPE_CATID_FAQID: |
||
1262 | $orderBy = sprintf(" |
||
1263 | ORDER BY |
||
1264 | fcr.category_id, |
||
1265 | fd.id %s", |
||
1266 | $sortOrder); |
||
1267 | break; |
||
1268 | |||
1269 | case FAQ_SORTING_TYPE_FAQID: |
||
1270 | $orderBy = sprintf(" |
||
1271 | ORDER BY |
||
1272 | fd.id %s", |
||
1273 | $sortOrder); |
||
1274 | break; |
||
1275 | |||
1276 | case FAQ_SORTING_TYPE_FAQTITLE_FAQID: |
||
1277 | $orderBy = sprintf(" |
||
1278 | ORDER BY |
||
1279 | fcr.category_id, |
||
1280 | fd.thema %s", |
||
1281 | $sortOrder); |
||
1282 | break; |
||
1283 | |||
1284 | case FAQ_SORTING_TYPE_DATE_FAQID: |
||
1285 | $orderBy = sprintf(" |
||
1286 | ORDER BY |
||
1287 | fcr.category_id, |
||
1288 | fd.datum %s", |
||
1289 | $sortOrder); |
||
1290 | break; |
||
1291 | |||
1292 | default: |
||
1293 | $orderBy = ''; |
||
1294 | break; |
||
1295 | } |
||
1296 | |||
1297 | $query = sprintf(" |
||
1298 | SELECT |
||
1299 | fd.id AS id, |
||
1300 | fd.lang AS lang, |
||
1301 | fcr.category_id AS category_id, |
||
1302 | fd.solution_id AS solution_id, |
||
1303 | fd.revision_id AS revision_id, |
||
1304 | fd.active AS active, |
||
1305 | fd.sticky AS sticky, |
||
1306 | fd.keywords AS keywords, |
||
1307 | fd.thema AS thema, |
||
1308 | fd.content AS content, |
||
1309 | fd.author AS author, |
||
1310 | fd.email AS email, |
||
1311 | fd.comment AS comment, |
||
1312 | fd.datum AS datum, |
||
1313 | fd.links_state AS links_state, |
||
1314 | fd.links_check_date AS links_check_date, |
||
1315 | fd.date_start AS date_start, |
||
1316 | fd.date_end AS date_end, |
||
1317 | fd.sticky AS sticky |
||
1318 | FROM |
||
1319 | %sfaqdata fd |
||
1320 | LEFT JOIN |
||
1321 | %sfaqcategoryrelations fcr |
||
1322 | ON |
||
1323 | fd.id = fcr.record_id |
||
1324 | AND |
||
1325 | fd.lang = fcr.record_lang |
||
1326 | %s |
||
1327 | %s", |
||
1328 | PMF_Db::getTablePrefix(), |
||
1329 | PMF_Db::getTablePrefix(), |
||
1330 | $where, |
||
1331 | $orderBy |
||
1332 | ); |
||
1333 | |||
1334 | $result = $this->_config->getDb()->query($query); |
||
1335 | |||
1336 | while ($row = $this->_config->getDb()->fetchObject($result)) { |
||
1337 | $content = $row->content; |
||
1338 | $active = ('yes' == $row->active); |
||
1339 | $expired = (date('YmdHis') > $row->date_end); |
||
1340 | |||
1341 | if (!$active) { |
||
1342 | $content = $this->pmf_lang['err_inactiveArticle']; |
||
1343 | } |
||
1344 | if ($expired) { |
||
1345 | $content = $this->pmf_lang['err_expiredArticle']; |
||
1346 | } |
||
1347 | |||
1348 | $this->faqRecords[] = array( |
||
1349 | 'id' => $row->id, |
||
1350 | 'category_id' => $row->category_id, |
||
1351 | 'lang' => $row->lang, |
||
1352 | 'solution_id' => $row->solution_id, |
||
1353 | 'revision_id' => $row->revision_id, |
||
1354 | 'active' => $row->active, |
||
1355 | 'sticky' => $row->sticky, |
||
1356 | 'keywords' => $row->keywords, |
||
1357 | 'title' => $row->thema, |
||
1358 | 'content' => $content, |
||
1359 | 'author' => $row->author, |
||
1360 | 'email' => $row->email, |
||
1361 | 'comment' => $row->comment, |
||
1362 | 'date' => PMF_Date::createIsoDate($row->datum), |
||
1363 | 'dateStart' => $row->date_start, |
||
1364 | 'dateEnd' => $row->date_end |
||
1365 | ); |
||
1366 | } |
||
1367 | } |
||
1368 | |||
1369 | /** |
||
1370 | * Returns the FAQ record title from the ID and language |
||
1371 | * |
||
1372 | * @param integer $id Record id |
||
1373 | * @return string |
||
1374 | */ |
||
1375 | public function getRecordTitle($id) |
||
1376 | { |
||
1377 | if (isset($this->faqRecord['id']) && ($this->faqRecord['id'] == $id)) { |
||
1378 | return $this->faqRecord['title']; |
||
1379 | } |
||
1380 | |||
1381 | $query = sprintf( |
||
1382 | "SELECT |
||
1383 | thema |
||
1384 | FROM |
||
1385 | %sfaqdata |
||
1386 | WHERE |
||
1387 | id = %d AND lang = '%s'", |
||
1388 | PMF_Db::getTablePrefix(), |
||
1389 | $id, |
||
1390 | $this->_config->getLanguage()->getLanguage() |
||
1391 | ); |
||
1392 | $result = $this->_config->getDb()->query($query); |
||
1393 | |||
1394 | if ($this->_config->getDb()->numRows($result) > 0) { |
||
1395 | while ($row = $this->_config->getDb()->fetchObject($result)) { |
||
1396 | $output = $row->thema; |
||
1397 | } |
||
1398 | } else { |
||
1399 | $output = $this->pmf_lang['no_cats']; |
||
1400 | } |
||
1401 | |||
1402 | return $output; |
||
1403 | } |
||
1404 | |||
1405 | /** |
||
1406 | * Gets all revisions from a given record ID |
||
1407 | * |
||
1408 | * @param integer $record_id Record id |
||
1409 | * @param string $record_lang Record language |
||
1410 | * @return array |
||
1411 | */ |
||
1412 | public function getRevisionIds($record_id, $record_lang) |
||
1413 | { |
||
1414 | $revision_data = []; |
||
1415 | |||
1416 | $query = sprintf(" |
||
1417 | SELECT |
||
1418 | revision_id, datum, author |
||
1419 | FROM |
||
1420 | %sfaqdata_revisions |
||
1421 | WHERE |
||
1422 | id = %d |
||
1423 | AND |
||
1424 | lang = '%s' |
||
1425 | ORDER BY |
||
1426 | revision_id", |
||
1427 | PMF_Db::getTablePrefix(), |
||
1428 | $record_id, |
||
1429 | $record_lang); |
||
1430 | |||
1431 | $result = $this->_config->getDb()->query($query); |
||
1432 | |||
1433 | if ($this->_config->getDb()->numRows($result) > 0) { |
||
1434 | while ($row = $this->_config->getDb()->fetchObject($result)) { |
||
1435 | $revision_data[] = array( |
||
1436 | 'revision_id' => $row->revision_id, |
||
1437 | 'datum' => $row->datum, |
||
1438 | 'author' => $row->author); |
||
1439 | } |
||
1440 | } |
||
1441 | |||
1442 | return $revision_data; |
||
1443 | } |
||
1444 | |||
1445 | /** |
||
1446 | * Adds a new revision from a given record ID |
||
1447 | * |
||
1448 | * @param integer $record_id Record id |
||
1449 | * @param string $record_lang Record language |
||
1450 | * @return array |
||
1451 | */ |
||
1452 | View Code Duplication | public function addNewRevision($record_id, $record_lang) |
|
1453 | { |
||
1454 | $query = sprintf(" |
||
1455 | INSERT INTO |
||
1456 | %sfaqdata_revisions |
||
1457 | SELECT * FROM |
||
1458 | %sfaqdata |
||
1459 | WHERE |
||
1460 | id = %d |
||
1461 | AND |
||
1462 | lang = '%s'", |
||
1463 | PMF_Db::getTablePrefix(), |
||
1464 | PMF_Db::getTablePrefix(), |
||
1465 | $record_id, |
||
1466 | $record_lang); |
||
1467 | $this->_config->getDb()->query($query); |
||
1468 | |||
1469 | return true; |
||
1470 | } |
||
1471 | |||
1472 | |||
1473 | /** |
||
1474 | * Returns the keywords of a FAQ record from the ID and language |
||
1475 | * |
||
1476 | * @param integer $id record id |
||
1477 | * @return string |
||
1478 | */ |
||
1479 | public function getRecordKeywords($id) |
||
1480 | { |
||
1481 | if (isset($this->faqRecord['id']) && ($this->faqRecord['id'] == $id)) { |
||
1482 | return $this->faqRecord['keywords']; |
||
1483 | } |
||
1484 | |||
1485 | $query = sprintf( |
||
1486 | "SELECT |
||
1487 | keywords |
||
1488 | FROM |
||
1489 | %sfaqdata |
||
1490 | WHERE id = %d AND lang = '%s'", |
||
1491 | PMF_Db::getTablePrefix(), |
||
1492 | $id, |
||
1493 | $this->_config->getLanguage()->getLanguage()); |
||
1494 | |||
1495 | $result = $this->_config->getDb()->query($query); |
||
1496 | |||
1497 | if ($this->_config->getDb()->numRows($result) > 0) { |
||
1498 | $row = $this->_config->getDb()->fetchObject($result); |
||
1499 | return PMF_String::htmlspecialchars($row->keywords, ENT_QUOTES, 'utf-8'); |
||
1500 | } else { |
||
1501 | return ''; |
||
1502 | } |
||
1503 | } |
||
1504 | |||
1505 | /** |
||
1506 | * Returns a answer preview of the FAQ record |
||
1507 | * |
||
1508 | * @param integer $recordId FAQ record ID |
||
1509 | * @param integer $wordCount Number of words, default: 12 |
||
1510 | * |
||
1511 | * @return string |
||
1512 | */ |
||
1513 | public function getRecordPreview($recordId, $wordCount = 12) |
||
1514 | { |
||
1515 | $answerPreview = ''; |
||
1516 | |||
1517 | if (isset($this->faqRecord['id']) && ($this->faqRecord['id'] == $recordId)) { |
||
1518 | $answerPreview = $this->faqRecord['content']; |
||
1519 | } |
||
1520 | |||
1521 | $query = sprintf(" |
||
1522 | SELECT |
||
1523 | content as answer |
||
1524 | FROM |
||
1525 | %sfaqdata |
||
1526 | WHERE |
||
1527 | id = %d |
||
1528 | AND |
||
1529 | lang = '%s'", |
||
1530 | PMF_Db::getTablePrefix(), |
||
1531 | $recordId, |
||
1532 | $this->_config->getLanguage()->getLanguage()); |
||
1533 | |||
1534 | $result = $this->_config->getDb()->query($query); |
||
1535 | |||
1536 | if ($this->_config->getDb()->numRows($result) > 0) { |
||
1537 | $row = $this->_config->getDb()->fetchObject($result); |
||
1538 | $answerPreview = strip_tags($row->answer); |
||
1539 | } else { |
||
1540 | $answerPreview = $this->_config->get('main.metaDescription'); |
||
1541 | } |
||
1542 | |||
1543 | return PMF_Utils::makeShorterText($answerPreview, $wordCount); |
||
1544 | } |
||
1545 | |||
1546 | /** |
||
1547 | * Returns the number of activated and not expired records, optionally |
||
1548 | * not limited to the current language |
||
1549 | * |
||
1550 | * @param string $language Language |
||
1551 | * @return int |
||
1552 | */ |
||
1553 | public function getNumberOfRecords($language = null) |
||
1554 | { |
||
1555 | $now = date('YmdHis'); |
||
1556 | |||
1557 | $query = sprintf(" |
||
1558 | SELECT |
||
1559 | id |
||
1560 | FROM |
||
1561 | %sfaqdata |
||
1562 | WHERE |
||
1563 | active = 'yes' |
||
1564 | %s |
||
1565 | AND |
||
1566 | date_start <= '%s' |
||
1567 | AND |
||
1568 | date_end >= '%s'", |
||
1569 | PMF_Db::getTablePrefix(), |
||
1570 | null == $language ? '' : "AND lang = '".$language."'", |
||
1571 | $now, |
||
1572 | $now); |
||
1573 | |||
1574 | $num = $this->_config->getDb()->numRows($this->_config->getDb()->query($query)); |
||
1575 | |||
1576 | if ($num > 0) { |
||
1577 | return $num; |
||
1578 | } else { |
||
1579 | return 0; |
||
1580 | } |
||
1581 | } |
||
1582 | |||
1583 | /** |
||
1584 | * This function generates a list with the mosted voted or most visited records |
||
1585 | * |
||
1586 | * @param string $type Type definition visits/voted |
||
1587 | * @access public |
||
1588 | * @since 2009-11-03 |
||
1589 | * @author Max Köhler <[email protected]> |
||
1590 | * @return array |
||
1591 | */ |
||
1592 | public function getTopTen($type = 'visits') |
||
1626 | |||
1627 | /** |
||
1628 | * This function generates the list with the latest published records |
||
1629 | * |
||
1630 | * @return array |
||
1631 | */ |
||
1632 | public function getLatest() |
||
1651 | |||
1652 | /** |
||
1653 | * Deletes a question for the table faquestion |
||
1654 | * |
||
1655 | * @param integer $questionId |
||
1656 | * |
||
1657 | * @return boolean |
||
1658 | */ |
||
1659 | View Code Duplication | function deleteQuestion($questionId) |
|
1660 | { |
||
1661 | $delete = sprintf(' |
||
1662 | DELETE FROM |
||
1663 | %sfaqquestions |
||
1664 | WHERE |
||
1665 | id = %d', |
||
1666 | PMF_Db::getTablePrefix(), |
||
1667 | $questionId |
||
1668 | ); |
||
1669 | |||
1670 | $this->_config->getDb()->query($delete); |
||
1671 | return true; |
||
1672 | } |
||
1673 | |||
1674 | /** |
||
1675 | * Returns the visibilty of a question |
||
1676 | * |
||
1677 | * @param integer $question_id |
||
1678 | * @return string |
||
1679 | * @access public |
||
1680 | * @since 2006-11-04 |
||
1681 | * @author Thorsten Rinne <[email protected]> |
||
1682 | */ |
||
1683 | function getVisibilityOfQuestion($question_id) |
||
1684 | { |
||
1685 | $query = sprintf(' |
||
1686 | SELECT |
||
1687 | is_visible |
||
1688 | FROM |
||
1689 | %sfaqquestions |
||
1690 | WHERE |
||
1691 | id = %d', |
||
1692 | PMF_Db::getTablePrefix(), |
||
1693 | $question_id); |
||
1694 | |||
1695 | $result = $this->_config->getDb()->query($query); |
||
1696 | if ($this->_config->getDb()->numRows($result) > 0) { |
||
1697 | $row = $this->_config->getDb()->fetchObject($result); |
||
1698 | return $row->is_visible; |
||
1699 | } |
||
1700 | return null; |
||
1701 | } |
||
1702 | |||
1703 | /** |
||
1704 | * Sets the visibilty of a question |
||
1705 | * |
||
1706 | * @param integer $question_id |
||
1707 | * @param string $is_visible |
||
1708 | * @return boolean |
||
1709 | * @access public |
||
1710 | * @since 2006-11-04 |
||
1711 | * @author Thorsten Rinne <[email protected]> |
||
1712 | */ |
||
1713 | View Code Duplication | function setVisibilityOfQuestion($question_id, $is_visible) |
|
1714 | { |
||
1715 | $query = sprintf(" |
||
1716 | UPDATE |
||
1717 | %sfaqquestions |
||
1718 | SET |
||
1719 | is_visible = '%s' |
||
1720 | WHERE |
||
1721 | id = %d", |
||
1722 | PMF_Db::getTablePrefix(), |
||
1723 | $is_visible, |
||
1724 | $question_id); |
||
1725 | |||
1726 | $this->_config->getDb()->query($query); |
||
1727 | return true; |
||
1728 | } |
||
1729 | |||
1730 | /** |
||
1731 | * This function generates a data-set with the mosted voted recors |
||
1732 | * |
||
1733 | * @param integer $count Number of records |
||
1734 | * @param integer $category Category ID |
||
1735 | * @param string $language Language |
||
1736 | * @return array |
||
1737 | */ |
||
1738 | public function getTopVotedData($count = PMF_NUMBER_RECORDS_TOPTEN, $category = 0, $language = nuLL) |
||
1739 | { |
||
1740 | global $sids; |
||
1741 | |||
1742 | $now = date('YmdHis'); |
||
1743 | $query = |
||
1744 | ' SELECT |
||
1745 | fd.id AS id, |
||
1746 | fd.lang AS lang, |
||
1747 | fd.thema AS thema, |
||
1748 | fd.datum AS datum, |
||
1749 | fcr.category_id AS category_id, |
||
1750 | (fv.vote/fv.usr) AS avg, |
||
1751 | fv.usr AS user |
||
1752 | FROM |
||
1753 | '.PMF_Db::getTablePrefix().'faqvoting fv, |
||
1754 | '.PMF_Db::getTablePrefix().'faqdata fd |
||
1755 | LEFT JOIN |
||
1756 | '.PMF_Db::getTablePrefix().'faqcategoryrelations fcr |
||
1757 | ON |
||
1758 | fd.id = fcr.record_id |
||
1759 | AND |
||
1760 | fd.lang = fcr.record_lang |
||
1761 | LEFT JOIN |
||
1762 | '.PMF_Db::getTablePrefix().'faqdata_group AS fdg |
||
1763 | ON |
||
1764 | fd.id = fdg.record_id |
||
1765 | LEFT JOIN |
||
1766 | '.PMF_Db::getTablePrefix().'faqdata_user AS fdu |
||
1767 | ON |
||
1768 | fd.id = fdu.record_id |
||
1769 | WHERE |
||
1770 | fd.date_start <= \''.$now.'\' |
||
1771 | AND fd.date_end >= \''.$now.'\' |
||
1772 | AND fd.id = fv.artikel |
||
1773 | AND fd.active = \'yes\''; |
||
1774 | |||
1775 | View Code Duplication | if (isset($categoryId) && is_numeric($categoryId) && ($categoryId != 0)) { |
|
1776 | $query .= ' |
||
1777 | AND |
||
1778 | fcr.category_id = \''.$categoryId.'\''; |
||
1779 | } |
||
1780 | if (isset($language) && PMF_Language::isASupportedLanguage($language)) { |
||
1781 | $query .= ' |
||
1782 | AND |
||
1783 | fd.lang = \''.$language.'\''; |
||
1784 | } |
||
1785 | $query .= ' |
||
1786 | ' . $this->queryPermission($this->groupSupport) . ' |
||
1787 | ORDER BY |
||
1788 | avg DESC'; |
||
1789 | |||
1790 | $result = $this->_config->getDb()->query($query); |
||
1791 | $topten = []; |
||
1792 | $data = []; |
||
1793 | |||
1794 | $i = 1; |
||
1795 | $oldId = 0; |
||
1796 | while (($row = $this->_config->getDb()->fetchObject($result)) && $i <= $count) { |
||
1797 | if ($oldId != $row->id) { |
||
1798 | $data['avg'] = $row->avg; |
||
1799 | $data['thema'] = $row->thema; |
||
1800 | $data['date'] = $row->datum; |
||
1801 | $data['user'] = $row->user; |
||
1802 | |||
1803 | $title = $row->thema; |
||
1804 | $url = sprintf( |
||
1805 | '%s?%saction=artikel&cat=%d&id=%d&artlang=%s', |
||
1806 | PMF_Link::getSystemRelativeUri(), |
||
1807 | $sids, |
||
1808 | $row->category_id, |
||
1809 | $row->id, |
||
1810 | $row->lang |
||
1811 | ); |
||
1812 | $oLink = new PMF_Link($url, $this->_config); |
||
1813 | $oLink->itemTitle = $row->thema; |
||
1814 | $oLink->tooltip = $title; |
||
1815 | $data['url'] = $oLink->toString(); |
||
1816 | |||
1817 | $topten[] = $data; |
||
1818 | $i++; |
||
1819 | } |
||
1820 | $oldId = $row->id; |
||
1821 | } |
||
1822 | |||
1823 | return $topten; |
||
1824 | } |
||
1825 | |||
1826 | /** |
||
1827 | * This function generates the Top Ten data with the mosted viewed records |
||
1828 | * |
||
1829 | * @param integer $count Number of records |
||
1830 | * @param integer $categoryId Category ID |
||
1831 | * @param string $language Language |
||
1832 | * @return array |
||
1833 | */ |
||
1834 | public function getTopTenData($count = PMF_NUMBER_RECORDS_TOPTEN, $categoryId = 0, $language = null) |
||
1933 | |||
1934 | /** |
||
1935 | * This function generates an array with a specified number of most recent |
||
1936 | * published records |
||
1937 | * |
||
1938 | * @param integer $count Number of records |
||
1939 | * @param string $language Language |
||
1940 | * |
||
1941 | * @return array |
||
1942 | */ |
||
1943 | public function getLatestData($count = PMF_NUMBER_RECORDS_LATEST, $language = null) |
||
2035 | |||
2036 | /** |
||
2037 | * Reload locking for user votings |
||
2038 | * |
||
2039 | * @param integer $id FAQ record id |
||
2040 | * @param string $ip IP |
||
2041 | * @return boolean |
||
2042 | */ |
||
2043 | public function votingCheck($id, $ip) |
||
2044 | { |
||
2045 | $check = $_SERVER['REQUEST_TIME'] - 300; |
||
2046 | $query = sprintf( |
||
2047 | "SELECT |
||
2048 | id |
||
2049 | FROM |
||
2050 | %sfaqvoting |
||
2051 | WHERE |
||
2052 | artikel = %d AND (ip = '%s' AND datum > '%s')", |
||
2053 | PMF_Db::getTablePrefix(), |
||
2054 | $id, |
||
2055 | $ip, |
||
2056 | $check); |
||
2057 | if ($this->_config->getDb()->numRows($this->_config->getDb()->query($query))) { |
||
2058 | return false; |
||
2059 | } |
||
2060 | return true; |
||
2061 | } |
||
2062 | |||
2063 | /** |
||
2064 | * Returns the number of users from the table faqvotings |
||
2065 | * |
||
2066 | * @param integer $record_id |
||
2067 | * @return integer |
||
2068 | * @access public |
||
2069 | * @since 2006-06-18 |
||
2070 | * @author Thorsten Rinne <[email protected]> |
||
2071 | */ |
||
2072 | View Code Duplication | function getNumberOfVotings($record_id) |
|
2073 | { |
||
2074 | $query = sprintf( |
||
2075 | 'SELECT |
||
2076 | usr |
||
2077 | FROM |
||
2078 | %sfaqvoting |
||
2079 | WHERE |
||
2080 | artikel = %d', |
||
2081 | PMF_Db::getTablePrefix(), |
||
2082 | $record_id); |
||
2083 | if ($result = $this->_config->getDb()->query($query)) { |
||
2084 | if ($row = $this->_config->getDb()->fetchObject($result)) { |
||
2085 | return $row->usr; |
||
2086 | } |
||
2087 | } |
||
2088 | return 0; |
||
2089 | } |
||
2090 | |||
2091 | /** |
||
2092 | * Adds a new voting record |
||
2093 | * |
||
2094 | * @param array $votingData |
||
2095 | * @return boolean |
||
2096 | * @access public |
||
2097 | * @since 2006-06-18 |
||
2098 | * @author Thorsten Rinne <[email protected]> |
||
2099 | */ |
||
2100 | function addVoting($votingData) |
||
2101 | { |
||
2102 | if (!is_array($votingData)) { |
||
2103 | return false; |
||
2104 | } |
||
2105 | |||
2106 | $query = sprintf( |
||
2107 | "INSERT INTO |
||
2108 | %sfaqvoting |
||
2109 | VALUES |
||
2110 | (%d, %d, %d, 1, %d, '%s')", |
||
2111 | PMF_Db::getTablePrefix(), |
||
2112 | $this->_config->getDb()->nextId(PMF_Db::getTablePrefix().'faqvoting', 'id'), |
||
2113 | $votingData['record_id'], |
||
2114 | $votingData['vote'], |
||
2115 | $_SERVER['REQUEST_TIME'], |
||
2116 | $votingData['user_ip']); |
||
2117 | $this->_config->getDb()->query($query); |
||
2118 | |||
2119 | return true; |
||
2120 | } |
||
2121 | |||
2122 | /** |
||
2123 | * Adds a new question |
||
2124 | * |
||
2125 | * @param array $questionData |
||
2126 | * |
||
2127 | * @return boolean |
||
2128 | */ |
||
2129 | function addQuestion(Array $questionData) |
||
2130 | { |
||
2131 | $query = sprintf(" |
||
2132 | INSERT INTO |
||
2133 | %sfaqquestions |
||
2134 | VALUES |
||
2135 | (%d, '%s', '%s', %d, '%s', '%s', '%s', %d)", |
||
2136 | PMF_Db::getTablePrefix(), |
||
2137 | $this->_config->getDb()->nextId(PMF_Db::getTablePrefix().'faqquestions', 'id'), |
||
2138 | $this->_config->getDb()->escape($questionData['username']), |
||
2139 | $this->_config->getDb()->escape($questionData['email']), |
||
2140 | $questionData['category_id'], |
||
2141 | $this->_config->getDb()->escape($questionData['question']), |
||
2142 | date('YmdHis'), |
||
2143 | $questionData['is_visible'], |
||
2144 | 0 |
||
2145 | ); |
||
2146 | $this->_config->getDb()->query($query); |
||
2147 | |||
2148 | return true; |
||
2149 | } |
||
2150 | |||
2151 | |||
2152 | /** |
||
2153 | * Returns a new question |
||
2154 | * |
||
2155 | * @param integer $question_id |
||
2156 | * @return array |
||
2157 | * @access public |
||
2158 | * @since 2006-11-11 |
||
2159 | * @author Thorsten Rinne <[email protected]> |
||
2160 | */ |
||
2161 | function getQuestion($id_question) |
||
2162 | { |
||
2163 | $question = array( |
||
2164 | 'id' => 0, |
||
2165 | 'username' => '', |
||
2166 | 'email' => '', |
||
2167 | 'category_id' => '', |
||
2168 | 'question' => '', |
||
2169 | 'created' => '', |
||
2170 | 'is_visible' => ''); |
||
2171 | |||
2172 | if (!is_int($id_question)) { |
||
2173 | return $question; |
||
2174 | } |
||
2175 | |||
2176 | $question = []; |
||
2177 | |||
2178 | $query = sprintf(' |
||
2179 | SELECT |
||
2180 | id, username, email, category_id, question, created, is_visible |
||
2181 | FROM |
||
2182 | %sfaqquestions |
||
2183 | WHERE |
||
2184 | id = %d', |
||
2185 | PMF_Db::getTablePrefix(), |
||
2186 | $id_question); |
||
2187 | |||
2188 | View Code Duplication | if ($result = $this->_config->getDb()->query($query)) { |
|
2189 | if ($row = $this->_config->getDb()->fetchObject($result)) { |
||
2190 | $question = array( |
||
2191 | 'id' => $row->id, |
||
2192 | 'username' => $row->username, |
||
2193 | 'email' => $row->email, |
||
2194 | 'category_id' => $row->category_id, |
||
2195 | 'question' => $row->question, |
||
2196 | 'created' => $row->created, |
||
2197 | 'is_visible' => $row->is_visible); |
||
2198 | } |
||
2199 | } |
||
2200 | |||
2201 | return $question; |
||
2202 | } |
||
2203 | |||
2204 | /** |
||
2205 | * Returns all open questions |
||
2206 | * |
||
2207 | * @param $all boolean If true, then return visible and unvisble questions; otherwise only visible ones |
||
2208 | * @return array |
||
2209 | */ |
||
2210 | public function getAllOpenQuestions($all = true) |
||
2211 | { |
||
2212 | $questions = []; |
||
2213 | |||
2214 | $query = sprintf(" |
||
2215 | SELECT |
||
2216 | id, username, email, category_id, question, created, answer_id, is_visible |
||
2217 | FROM |
||
2218 | %sfaqquestions |
||
2219 | %s |
||
2220 | ORDER BY |
||
2221 | created ASC", |
||
2222 | PMF_Db::getTablePrefix(), |
||
2223 | ($all == false ? "WHERE is_visible = 'Y'" : '')); |
||
2224 | |||
2225 | View Code Duplication | if ($result = $this->_config->getDb()->query($query)) { |
|
2226 | while ($row = $this->_config->getDb()->fetchObject($result)) { |
||
2227 | $questions[] = array( |
||
2228 | 'id' => $row->id, |
||
2229 | 'username' => $row->username, |
||
2230 | 'email' => $row->email, |
||
2231 | 'category_id' => $row->category_id, |
||
2232 | 'question' => $row->question, |
||
2233 | 'created' => $row->created, |
||
2234 | 'answer_id' => $row->answer_id, |
||
2235 | 'is_visible' => $row->is_visible |
||
2236 | ); |
||
2237 | } |
||
2238 | } |
||
2239 | return $questions; |
||
2240 | } |
||
2241 | |||
2242 | /** |
||
2243 | * Updates an existing voting record |
||
2244 | * |
||
2245 | * @param array $votingData |
||
2246 | * @return boolean |
||
2247 | * @access public |
||
2248 | * @since 2006-06-18 |
||
2249 | * @author Thorsten Rinne <[email protected]> |
||
2250 | */ |
||
2251 | View Code Duplication | function updateVoting($votingData) |
|
2252 | { |
||
2253 | if (!is_array($votingData)) { |
||
2254 | return false; |
||
2255 | } |
||
2256 | |||
2257 | $query = sprintf( |
||
2258 | "UPDATE |
||
2259 | %sfaqvoting |
||
2260 | SET |
||
2261 | vote = vote + %d, |
||
2262 | usr = usr + 1, |
||
2263 | datum = %d, |
||
2264 | ip = '%s' |
||
2265 | WHERE |
||
2266 | artikel = %d", |
||
2267 | PMF_Db::getTablePrefix(), |
||
2268 | $votingData['vote'], |
||
2269 | $_SERVER['REQUEST_TIME'], |
||
2270 | $votingData['user_ip'], |
||
2271 | $votingData['record_id']); |
||
2272 | $this->_config->getDb()->query($query); |
||
2273 | |||
2274 | return true; |
||
2275 | } |
||
2276 | |||
2277 | |||
2278 | /** |
||
2279 | * Adds a new changelog entry in the table faqchanges |
||
2280 | * |
||
2281 | * @param integer $id |
||
2282 | * @param integer $userId |
||
2283 | * @param string $text |
||
2284 | * @param string $lang |
||
2285 | * @param integer $revision_id |
||
2286 | * @return boolean |
||
2287 | * @access private |
||
2288 | * @since 2006-08-18 |
||
2289 | * @author Thorsten Rinne <[email protected]> |
||
2290 | * @author Matteo Scaramuccia <[email protected]> |
||
2291 | */ |
||
2292 | function createChangeEntry($id, $userId, $text, $lang, $revision_id = 0) |
||
2293 | { |
||
2294 | if ( !is_numeric($id) |
||
2295 | && !is_numeric($userId) |
||
2296 | && !is_string($text) |
||
2297 | && !is_string($lang) |
||
2298 | ) { |
||
2299 | return false; |
||
2300 | } |
||
2301 | |||
2302 | $query = sprintf( |
||
2303 | "INSERT INTO |
||
2304 | %sfaqchanges |
||
2305 | (id, beitrag, lang, revision_id, usr, datum, what) |
||
2306 | VALUES |
||
2307 | (%d, %d, '%s', %d, %d, %d, '%s')", |
||
2308 | PMF_Db::getTablePrefix(), |
||
2309 | $this->_config->getDb()->nextId(PMF_Db::getTablePrefix().'faqchanges', 'id'), |
||
2310 | $id, |
||
2311 | $lang, |
||
2312 | $revision_id, |
||
2313 | $userId, |
||
2314 | $_SERVER['REQUEST_TIME'], |
||
2315 | $text); |
||
2316 | |||
2317 | $this->_config->getDb()->query($query); |
||
2318 | |||
2319 | return true; |
||
2320 | } |
||
2321 | |||
2322 | /** |
||
2323 | * Returns the changelog of a FAQ record |
||
2324 | * |
||
2325 | * @param integer $record_id |
||
2326 | * @return array |
||
2327 | * @access public |
||
2328 | * @since 2007-03-03 |
||
2329 | * @author Thorsten Rinne <[email protected]> |
||
2330 | */ |
||
2331 | View Code Duplication | function getChangeEntries($record_id) |
|
2332 | { |
||
2333 | $entries = []; |
||
2334 | |||
2335 | $query = sprintf(" |
||
2336 | SELECT |
||
2337 | DISTINCT revision_id, usr, datum, what |
||
2338 | FROM |
||
2339 | %sfaqchanges |
||
2340 | WHERE |
||
2341 | beitrag = %d |
||
2342 | ORDER BY id DESC", |
||
2343 | PMF_Db::getTablePrefix(), |
||
2344 | $record_id |
||
2345 | ); |
||
2346 | |||
2347 | if ($result = $this->_config->getDb()->query($query)) { |
||
2348 | while ($row = $this->_config->getDb()->fetchObject($result)) { |
||
2349 | $entries[] = array( |
||
2350 | 'revision_id' => $row->revision_id, |
||
2351 | 'user' => $row->usr, |
||
2352 | 'date' => $row->datum, |
||
2353 | 'changelog' => $row->what); |
||
2354 | } |
||
2355 | } |
||
2356 | |||
2357 | return $entries; |
||
2358 | } |
||
2359 | |||
2360 | /** |
||
2361 | * Retrieve faq records according to the constraints provided |
||
2362 | * |
||
2363 | * @param string $QueryType |
||
2364 | * @param integer $nCatid |
||
2365 | * @param bool $bDownwards |
||
2366 | * @param string $lang |
||
2367 | * @param string $date |
||
2368 | * |
||
2369 | * @return array |
||
2370 | */ |
||
2371 | function get($QueryType = FAQ_QUERY_TYPE_DEFAULT, $nCatid = 0, $bDownwards = true, $lang = '', $date = '') |
||
2372 | { |
||
2373 | $faqs = []; |
||
2374 | |||
2375 | $result = $this->_config->getDb()->query($this->_getSQLQuery($QueryType, $nCatid, $bDownwards, $lang, $date)); |
||
2376 | |||
2377 | if ($this->_config->getDb()->numRows($result) > 0) { |
||
2378 | $i = 0; |
||
2379 | while ($row = $this->_config->getDb()->fetchObject($result)) { |
||
2380 | $faq = []; |
||
2381 | $faq['id'] = $row->id; |
||
2382 | $faq['solution_id'] = $row->solution_id; |
||
2383 | $faq['revision_id'] = $row->revision_id; |
||
2384 | $faq['lang'] = $row->lang; |
||
2385 | $faq['category_id'] = $row->category_id; |
||
2386 | $faq['active'] = $row->active; |
||
2387 | $faq['sticky'] = $row->sticky; |
||
2388 | $faq['keywords'] = $row->keywords; |
||
2389 | $faq['topic'] = $row->thema; |
||
2390 | $faq['content'] = $row->content; |
||
2391 | $faq['author_name'] = $row->author; |
||
2392 | $faq['author_email'] = $row->email; |
||
2393 | $faq['comment_enable'] = $row->comment; |
||
2394 | $faq['lastmodified'] = $row->datum; |
||
2395 | $faq['hits'] = $row->visits; |
||
2396 | $faq['hits_last'] = $row->last_visit; |
||
2397 | $faqs[$i] = $faq; |
||
2398 | $i++; |
||
2399 | } |
||
2400 | } |
||
2401 | |||
2402 | return $faqs; |
||
2403 | } |
||
2404 | |||
2405 | /** |
||
2406 | * Build a logic sequence, for a WHERE statement, of those category IDs |
||
2407 | * children of the provided category ID, if any |
||
2408 | * |
||
2409 | * @param $nCatid |
||
2410 | * @param $logicOp |
||
2411 | * @param $oCat |
||
2412 | * @return string |
||
2413 | * @access private |
||
2414 | * @since 2005-11-02 |
||
2415 | * @author Matteo Scaramuccia <[email protected]> |
||
2416 | */ |
||
2417 | function _getCatidWhereSequence($nCatid, $logicOp = 'OR', $oCat = null) |
||
2418 | { |
||
2419 | $sqlWherefilter = ''; |
||
2420 | |||
2421 | if (!isset($oCat)) { |
||
2422 | $oCat = new PMF_Category($this->_config); |
||
2423 | } |
||
2424 | $aChildren = array_values($oCat->getChildren($nCatid)); |
||
2425 | |||
2426 | foreach ($aChildren as $catid) { |
||
2427 | $sqlWherefilter .= " ".$logicOp." fcr.category_id = ".$catid; |
||
2428 | $sqlWherefilter .= $this->_getCatidWhereSequence($catid, 'OR', $oCat); |
||
2429 | } |
||
2430 | |||
2431 | return $sqlWherefilter; |
||
2432 | } |
||
2433 | |||
2434 | /** |
||
2435 | * Build the SQL query for retrieving faq records according to the constraints provided |
||
2436 | * |
||
2437 | * @param $QueryType |
||
2438 | * @param $nCatid |
||
2439 | * @param $bDownwards |
||
2440 | * @param $lang |
||
2441 | * @param $date |
||
2442 | * @param $faqid |
||
2443 | * @return array |
||
2444 | * @access private |
||
2445 | * @since 2005-11-02 |
||
2446 | * @author Matteo Scaramuccia <[email protected]> |
||
2447 | */ |
||
2448 | private function _getSQLQuery($QueryType, $nCatid, $bDownwards, $lang, $date, $faqid = 0) |
||
2449 | { |
||
2450 | $now = date('YmdHis'); |
||
2451 | $query = sprintf(" |
||
2452 | SELECT |
||
2453 | fd.id AS id, |
||
2454 | fd.solution_id AS solution_id, |
||
2455 | fd.revision_id AS revision_id, |
||
2456 | fd.lang AS lang, |
||
2457 | fcr.category_id AS category_id, |
||
2458 | fd.active AS active, |
||
2459 | fd.sticky AS sticky, |
||
2460 | fd.keywords AS keywords, |
||
2461 | fd.thema AS thema, |
||
2462 | fd.content AS content, |
||
2463 | fd.author AS author, |
||
2464 | fd.email AS email, |
||
2465 | fd.comment AS comment, |
||
2466 | fd.datum AS datum, |
||
2467 | fv.visits AS visits, |
||
2468 | fv.last_visit AS last_visit |
||
2469 | FROM |
||
2470 | %sfaqdata fd, |
||
2471 | %sfaqvisits fv, |
||
2472 | %sfaqcategoryrelations fcr |
||
2473 | WHERE |
||
2474 | fd.id = fcr.record_id |
||
2475 | AND |
||
2476 | fd.lang = fcr.record_lang |
||
2477 | AND |
||
2478 | fd.date_start <= '%s' |
||
2479 | AND |
||
2480 | fd.date_end >= '%s' |
||
2481 | AND ", |
||
2482 | PMF_Db::getTablePrefix(), |
||
2483 | PMF_Db::getTablePrefix(), |
||
2484 | PMF_Db::getTablePrefix(), |
||
2485 | $now, |
||
2486 | $now); |
||
2487 | // faqvisits data selection |
||
2488 | if (!empty($faqid)) { |
||
2489 | // Select ONLY the faq with the provided $faqid |
||
2490 | $query .= "fd.id = '".$faqid."' AND "; |
||
2491 | } |
||
2492 | $query .= "fd.id = fv.id |
||
2493 | AND |
||
2494 | fd.lang = fv.lang"; |
||
2495 | $needAndOp = true; |
||
2496 | if ((!empty($nCatid)) && is_int($nCatid) && $nCatid > 0) { |
||
2497 | if ($needAndOp) { |
||
2498 | $query .= " AND"; |
||
2499 | } |
||
2500 | $query .= " (fcr.category_id = ".$nCatid; |
||
2501 | if ($bDownwards) { |
||
2502 | $query .= $this->_getCatidWhereSequence($nCatid, "OR"); |
||
2503 | } |
||
2504 | $query .= ")"; |
||
2505 | $needAndOp = true; |
||
2506 | } |
||
2507 | View Code Duplication | if ((!empty($date)) && PMF_Utils::isLikeOnPMFDate($date)) { |
|
2508 | if ($needAndOp) { |
||
2509 | $query .= " AND"; |
||
2510 | } |
||
2511 | $query .= " fd.datum LIKE '".$date."'"; |
||
2512 | $needAndOp = true; |
||
2513 | } |
||
2514 | View Code Duplication | if ((!empty($lang)) && PMF_Utils::isLanguage($lang)) { |
|
2515 | if ($needAndOp) { |
||
2516 | $query .= " AND"; |
||
2517 | } |
||
2518 | $query .= " fd.lang = '".$lang."'"; |
||
2519 | $needAndOp = true; |
||
2520 | } |
||
2521 | switch ($QueryType) { |
||
2522 | case FAQ_QUERY_TYPE_APPROVAL: |
||
2523 | if ($needAndOp) { |
||
2524 | $query .= " AND"; |
||
2525 | } |
||
2526 | $query .= " fd.active = '".FAQ_SQL_ACTIVE_NO."'"; |
||
2527 | $needAndOp = true; |
||
2528 | break; |
||
2529 | case FAQ_QUERY_TYPE_EXPORT_PDF: |
||
2530 | case FAQ_QUERY_TYPE_EXPORT_XHTML: |
||
2531 | View Code Duplication | case FAQ_QUERY_TYPE_EXPORT_XML: |
|
2532 | if ($needAndOp) { |
||
2533 | $query .= " AND"; |
||
2534 | } |
||
2535 | $query .= " fd.active = '".FAQ_SQL_ACTIVE_YES."'"; |
||
2536 | $needAndOp = true; |
||
2537 | break; |
||
2538 | View Code Duplication | default: |
|
2539 | if ($needAndOp) { |
||
2540 | $query .= " AND"; |
||
2541 | } |
||
2542 | $query .= " fd.active = '".FAQ_SQL_ACTIVE_YES."'"; |
||
2543 | $needAndOp = true; |
||
2544 | break; |
||
2545 | } |
||
2546 | // Sort criteria |
||
2547 | switch ($QueryType) { |
||
2548 | case FAQ_QUERY_TYPE_EXPORT_PDF: |
||
2549 | case FAQ_QUERY_TYPE_EXPORT_XHTML: |
||
2550 | case FAQ_QUERY_TYPE_EXPORT_XML: |
||
2551 | $query .= "\nORDER BY fcr.category_id, fd.id"; |
||
2552 | break; |
||
2553 | case FAQ_QUERY_TYPE_RSS_LATEST: |
||
2554 | $query .= "\nORDER BY fd.datum DESC"; |
||
2555 | break; |
||
2556 | default: |
||
2557 | // Normal ordering |
||
2558 | $query .= "\nORDER BY fcr.category_id, fd.id"; |
||
2559 | break; |
||
2560 | } |
||
2561 | |||
2562 | return $query; |
||
2563 | } |
||
2564 | |||
2565 | /** |
||
2566 | * Adds the record permissions for users and groups |
||
2567 | * |
||
2568 | * @param string $mode 'group' or 'user' |
||
2569 | * @param integer $recordId ID of the current record |
||
2570 | * @param array $ids Array of group or user IDs |
||
2571 | * |
||
2572 | * @return boolean |
||
2573 | */ |
||
2574 | function addPermission($mode, $recordId, $ids) |
||
2575 | { |
||
2576 | if ('user' !== $mode && 'group' !== $mode) { |
||
2577 | return false; |
||
2578 | } |
||
2579 | |||
2580 | foreach ($ids as $id) { |
||
2581 | $query = sprintf(" |
||
2582 | INSERT INTO |
||
2583 | %sfaqdata_%s |
||
2584 | (record_id, %s_id) |
||
2585 | VALUES |
||
2586 | (%d, %d)", |
||
2587 | PMF_Db::getTablePrefix(), |
||
2588 | $mode, |
||
2589 | $mode, |
||
2590 | $recordId, |
||
2591 | $id |
||
2592 | ); |
||
2593 | |||
2594 | $this->_config->getDb()->query($query); |
||
2595 | } |
||
2596 | |||
2597 | return true; |
||
2598 | } |
||
2599 | |||
2600 | /** |
||
2601 | * Deletes the record permissions for users and groups |
||
2602 | * |
||
2603 | * @param string $mode 'group' or 'user' |
||
2604 | * @param integer $record_id ID of the current record |
||
2605 | * @return boolean |
||
2606 | * @access public |
||
2607 | * @author Thorsten Rinne <[email protected]> |
||
2608 | */ |
||
2609 | View Code Duplication | function deletePermission($mode, $record_id) |
|
2610 | { |
||
2611 | if (!($mode == "user" || $mode == "group")) { |
||
2612 | return false; |
||
2613 | } |
||
2614 | if (!is_int($record_id)) { |
||
2615 | return false; |
||
2616 | } |
||
2617 | |||
2618 | $query = sprintf(" |
||
2619 | DELETE FROM |
||
2620 | %sfaqdata_%s |
||
2621 | WHERE |
||
2622 | record_id = %d", |
||
2623 | PMF_Db::getTablePrefix(), |
||
2624 | $mode, |
||
2625 | $record_id); |
||
2626 | $this->_config->getDb()->query($query); |
||
2627 | |||
2628 | return true; |
||
2629 | } |
||
2630 | |||
2631 | /** |
||
2632 | * Returns the record permissions for users and groups |
||
2633 | * |
||
2634 | * @param string $mode 'group' or 'user' |
||
2635 | * @param integer $recordId |
||
2636 | * @return array |
||
2637 | * @access boolean |
||
2638 | * @author Thorsten Rinne <[email protected]> |
||
2639 | */ |
||
2640 | function getPermission($mode, $recordId) |
||
2641 | { |
||
2642 | $permissions = []; |
||
2643 | |||
2644 | if (!($mode == 'user' || $mode == 'group')) { |
||
2645 | return false; |
||
2646 | } |
||
2647 | |||
2648 | $query = sprintf(" |
||
2649 | SELECT |
||
2650 | %s_id AS permission |
||
2651 | FROM |
||
2652 | %sfaqdata_%s |
||
2653 | WHERE |
||
2654 | record_id = %d", |
||
2655 | $mode, |
||
2656 | PMF_Db::getTablePrefix(), |
||
2657 | $mode, |
||
2658 | (int)$recordId); |
||
2659 | |||
2660 | $result = $this->_config->getDb()->query($query); |
||
2661 | |||
2662 | if ($this->_config->getDb()->numRows($result) > 0) { |
||
2663 | while (($row = $this->_config->getDb()->fetchObject($result))) { |
||
2664 | $permissions[] = (int)$row->permission; |
||
2665 | } |
||
2666 | } |
||
2667 | |||
2668 | return $permissions; |
||
2669 | } |
||
2670 | |||
2671 | /** |
||
2672 | * Returns all records of one category |
||
2673 | * |
||
2674 | * @param integer $category |
||
2675 | * @return string |
||
2676 | * @access public |
||
2677 | * @since 2007-04-04 |
||
2678 | * @author Georgi Korchev <[email protected]> |
||
2679 | */ |
||
2680 | function showAllRecordsWoPaging($category) { |
||
2758 | |||
2759 | /** |
||
2760 | * Prints the open questions as a XHTML table |
||
2761 | * |
||
2762 | * @return string |
||
2763 | * @access public |
||
2764 | * @since 2002-09-17 |
||
2765 | * @author Thorsten Rinne <[email protected]> |
||
2766 | */ |
||
2767 | function printOpenQuestions() |
||
2768 | { |
||
2769 | global $sids, $category; |
||
2770 | |||
2771 | $date = new PMF_Date($this->_config); |
||
2772 | $mail = new PMF_Mail($this->_config); |
||
2773 | |||
2774 | $query = sprintf(" |
||
2775 | SELECT |
||
2776 | COUNT(id) AS num |
||
2777 | FROM |
||
2778 | %sfaqquestions |
||
2779 | WHERE |
||
2780 | is_visible != 'Y'", |
||
2781 | PMF_Db::getTablePrefix() |
||
2782 | ); |
||
2783 | |||
2784 | $result = $this->_config->getDb()->query($query); |
||
2785 | $row = $this->_config->getDb()->fetchObject($result); |
||
2786 | $numOfInvisibles = $row->num; |
||
2787 | |||
2788 | if ($numOfInvisibles > 0) { |
||
2789 | $extraout = sprintf( |
||
2790 | '<tr><td colspan="3"><small>%s %s</small></td></tr>', |
||
2791 | $this->pmf_lang['msgQuestionsWaiting'], |
||
2792 | $numOfInvisibles |
||
2793 | ); |
||
2794 | } else { |
||
2795 | $extraout = ''; |
||
2796 | } |
||
2797 | |||
2798 | $query = sprintf(" |
||
2799 | SELECT |
||
2800 | * |
||
2801 | FROM |
||
2802 | %sfaqquestions |
||
2803 | WHERE |
||
2804 | is_visible = 'Y' |
||
2805 | ORDER BY |
||
2806 | created ASC", |
||
2807 | PMF_Db::getTablePrefix() |
||
2808 | ); |
||
2809 | |||
2810 | $result = $this->_config->getDb()->query($query); |
||
2811 | $output = ''; |
||
2812 | |||
2813 | if ($result && $this->_config->getDb()->numRows($result) > 0) { |
||
2814 | while ($row = $this->_config->getDb()->fetchObject($result)) { |
||
2815 | $output .= '<tr class="openquestions">'; |
||
2816 | $output .= sprintf( |
||
2817 | '<td><small>%s</small><br /><a href="mailto:%s">%s</a></td>', |
||
2818 | $date->format(PMF_Date::createIsoDate($row->created)), |
||
2819 | $mail->safeEmail($row->email), |
||
2820 | $row->username |
||
2821 | ); |
||
2822 | $output .= sprintf( |
||
2823 | '<td><strong>%s:</strong><br />%s</td>', |
||
2824 | isset($category->categoryName[$row->category_id]['name']) ? $category->categoryName[$row->category_id]['name'] : '', |
||
2825 | strip_tags($row->question) |
||
2826 | ); |
||
2827 | if ($this->_config->get('records.enableCloseQuestion') && $row->answer_id) { |
||
2828 | $output .= sprintf( |
||
2829 | '<td><a id="PMF_openQuestionAnswered" href="?%saction=artikel&cat=%d&id=%d">%s</a></td>', |
||
2830 | $sids, |
||
2831 | $row->category_id, |
||
2832 | $row->answer_id, |
||
2833 | $this->pmf_lang['msg2answerFAQ'] |
||
2834 | ); |
||
2835 | } else { |
||
2836 | $output .= sprintf( |
||
2837 | '<td><a class="btn btn-primary" href="?%saction=add&question=%d&cat=%d">%s</a></td>', |
||
2838 | $sids, |
||
2839 | $row->id, |
||
2840 | $row->category_id, |
||
2841 | $this->pmf_lang['msg2answer'] |
||
2842 | ); |
||
2843 | } |
||
2844 | $output .= '</tr>'; |
||
2845 | } |
||
2846 | } else { |
||
2847 | $output = sprintf( |
||
2848 | '<tr><td colspan="3">%s</td></tr>', |
||
2849 | $this->pmf_lang['msgNoQuestionsAvailable'] |
||
2850 | ); |
||
2851 | } |
||
2852 | |||
2853 | return $output . $extraout; |
||
2854 | } |
||
2855 | |||
2856 | /** |
||
2857 | * Set or unset a faq item flag |
||
2858 | * |
||
2859 | * @param integer $id Record id |
||
2860 | * @param string $lang language code which is valid with Language::isASupportedLanguage |
||
2861 | * @param boolean $flag weither or not the record is set to sticky |
||
2862 | * @param string $type type of the flag to set, use the column name |
||
2863 | * |
||
2864 | * @return boolean |
||
2865 | */ |
||
2866 | public function updateRecordFlag($id, $lang, $flag, $type) |
||
2867 | { |
||
2868 | $retval = false; |
||
2869 | |||
2870 | switch ($type) { |
||
2871 | case 'sticky': |
||
2872 | $flag = ($flag === 'checked' ? 1 : 0); |
||
2873 | break; |
||
2874 | |||
2875 | case 'active': |
||
2876 | $flag = ($flag === 'checked' ? "'yes'" : "'no'"); |
||
2877 | break; |
||
2878 | |||
2879 | default: |
||
2880 | // This is because we would run into unknown db column |
||
2881 | $flag = null; |
||
2882 | break; |
||
2883 | } |
||
2884 | |||
2885 | if (null !== $flag) { |
||
2886 | |||
2887 | $update = sprintf(" |
||
2888 | UPDATE |
||
2889 | %sfaqdata |
||
2890 | SET |
||
2891 | %s = %s |
||
2892 | WHERE |
||
2893 | id = %d |
||
2894 | AND |
||
2895 | lang = '%s'", |
||
2896 | PMF_Db::getTablePrefix(), |
||
2897 | $type, |
||
2898 | $flag, |
||
2899 | $id, |
||
2900 | $lang |
||
2901 | ); |
||
2902 | |||
2903 | $retval = (bool)$this->_config->getDb()->query($update); |
||
2904 | |||
2905 | } |
||
2906 | |||
2907 | return $retval; |
||
2908 | } |
||
2909 | |||
2910 | /** |
||
2911 | * Returns the sticky records with URL and Title |
||
2912 | * |
||
2913 | * @return array |
||
2914 | */ |
||
2915 | private function getStickyRecordsData() |
||
2916 | { |
||
2917 | global $sids; |
||
2918 | |||
2919 | $now = date('YmdHis'); |
||
2920 | $query = sprintf(" |
||
2921 | SELECT |
||
2922 | fd.id AS id, |
||
2923 | fd.lang AS lang, |
||
2924 | fd.thema AS thema, |
||
2925 | fcr.category_id AS category_id |
||
2926 | FROM |
||
2927 | %sfaqdata fd |
||
2928 | LEFT JOIN |
||
2929 | %sfaqcategoryrelations fcr |
||
2930 | ON |
||
2931 | fd.id = fcr.record_id |
||
2932 | AND |
||
2933 | fd.lang = fcr.record_lang |
||
2934 | LEFT JOIN |
||
2935 | %sfaqdata_group AS fdg |
||
2936 | ON |
||
2937 | fd.id = fdg.record_id |
||
2938 | LEFT JOIN |
||
2939 | %sfaqdata_user AS fdu |
||
2940 | ON |
||
2941 | fd.id = fdu.record_id |
||
2942 | WHERE |
||
2943 | fd.lang = '%s' |
||
2944 | AND |
||
2945 | fd.date_start <= '%s' |
||
2946 | AND |
||
2947 | fd.date_end >= '%s' |
||
2948 | AND |
||
2949 | fd.active = 'yes' |
||
2950 | AND |
||
2951 | fd.sticky = 1 |
||
2952 | %s", |
||
2953 | PMF_Db::getTablePrefix(), |
||
2954 | PMF_Db::getTablePrefix(), |
||
2955 | PMF_Db::getTablePrefix(), |
||
2956 | PMF_Db::getTablePrefix(), |
||
2957 | $this->_config->getLanguage()->getLanguage(), |
||
2958 | $now, |
||
2959 | $now, |
||
2960 | $this->queryPermission($this->groupSupport) |
||
2961 | ); |
||
2962 | |||
2963 | $result = $this->_config->getDb()->query($query); |
||
2964 | $sticky = []; |
||
2965 | $data = []; |
||
2966 | |||
2967 | $oldId = 0; |
||
2968 | while (($row = $this->_config->getDb()->fetchObject($result))) { |
||
2969 | if ($oldId != $row->id) { |
||
2970 | $data['thema'] = $row->thema; |
||
2971 | |||
2972 | $title = $row->thema; |
||
2973 | $url = sprintf( |
||
2974 | '%s?%saction=artikel&cat=%d&id=%d&artlang=%s', |
||
2975 | PMF_Link::getSystemRelativeUri(), |
||
2976 | $sids, |
||
2977 | $row->category_id, |
||
2978 | $row->id, |
||
2979 | $row->lang |
||
2980 | ); |
||
2981 | $oLink = new PMF_Link($url, $this->_config); |
||
2982 | $oLink->itemTitle = $row->thema; |
||
2983 | $oLink->tooltip = $title; |
||
2984 | $data['url'] = $oLink->toString(); |
||
2985 | |||
2986 | $sticky[] = $data; |
||
2987 | } |
||
2988 | $oldId = $row->id; |
||
2989 | } |
||
2990 | |||
2991 | return $sticky; |
||
2992 | } |
||
2993 | |||
2994 | /** |
||
2995 | * Prepares and returns the sticky records for the frontend |
||
2996 | * |
||
2997 | * @return array |
||
2998 | */ |
||
2999 | public function getStickyRecords() |
||
3029 | |||
3030 | /** |
||
3031 | * Updates field answer_id in faqquestion |
||
3032 | * |
||
3033 | * @param integer $openQuestionId |
||
3034 | * @param integer $faqId |
||
3035 | * @param integer $categoryId |
||
3036 | * |
||
3037 | * @return boolean |
||
3038 | */ |
||
3039 | View Code Duplication | public function updateQuestionAnswer($openQuestionId, $faqId, $categoryId) |
|
3040 | { |
||
3041 | $query = sprintf( |
||
3042 | 'UPDATE %sfaqquestions SET answer_id = %d, category_id= %d, WHERE id= %d', |
||
3043 | PMF_Db::getTablePrefix(), |
||
3044 | $faqId, |
||
3045 | $categoryId, |
||
3046 | $openQuestionId |
||
3047 | ); |
||
3048 | |||
3049 | return $this->_config->getDb()->query($query); |
||
3050 | } |
||
3051 | |||
3052 | /** |
||
3053 | * Returns a part of a query to check permissions |
||
3054 | * |
||
3055 | * @param boolean $hasGroupSupport |
||
3056 | * |
||
3057 | * @return string |
||
3058 | */ |
||
3059 | protected function queryPermission($hasGroupSupport = false) |
||
3060 | { |
||
3061 | if ($hasGroupSupport) { |
||
3062 | if (-1 === $this->user) { |
||
3063 | return sprintf( |
||
3064 | "AND fdg.group_id IN (%s)", |
||
3065 | implode(', ', $this->groups), |
||
3066 | $this->user, |
||
3067 | implode(', ', $this->groups)); |
||
3068 | View Code Duplication | } else { |
|
3069 | return sprintf( |
||
3070 | "AND ( fdg.group_id IN (%s) OR (fdu.user_id IN (-1, %d) OR fdg.group_id IN (%s)) )", |
||
3071 | implode(', ', $this->groups), |
||
3072 | $this->user, |
||
3073 | implode(', ', $this->groups) |
||
3074 | ); |
||
3075 | } |
||
3090 | } |
||
3091 |
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.