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') |
||
174 | { |
||
175 | global $sids; |
||
176 | |||
177 | $faqdata = []; |
||
178 | |||
179 | if ($orderby == 'visits') { |
||
180 | $currentTable = 'fv'; |
||
181 | } else { |
||
182 | $currentTable = 'fd'; |
||
183 | } |
||
184 | |||
185 | $now = date('YmdHis'); |
||
186 | $query = sprintf(" |
||
187 | SELECT |
||
188 | fd.id AS id, |
||
189 | fd.lang AS lang, |
||
190 | fd.thema AS thema, |
||
191 | fd.content AS record_content, |
||
192 | fd.updated AS updated, |
||
193 | fcr.category_id AS category_id, |
||
194 | fv.visits AS visits, |
||
195 | fd.created AS created |
||
196 | FROM |
||
197 | %sfaqdata AS fd |
||
198 | LEFT JOIN |
||
199 | %sfaqcategoryrelations AS fcr |
||
200 | ON |
||
201 | fd.id = fcr.record_id |
||
202 | AND |
||
203 | fd.lang = fcr.record_lang |
||
204 | LEFT JOIN |
||
205 | %sfaqvisits AS fv |
||
206 | ON |
||
207 | fd.id = fv.id |
||
208 | AND |
||
209 | fv.lang = fd.lang |
||
210 | LEFT JOIN |
||
211 | %sfaqdata_group AS fdg |
||
212 | ON |
||
213 | fd.id = fdg.record_id |
||
214 | LEFT JOIN |
||
215 | %sfaqdata_user AS fdu |
||
216 | ON |
||
217 | fd.id = fdu.record_id |
||
218 | WHERE |
||
219 | fd.date_start <= '%s' |
||
220 | AND |
||
221 | fd.date_end >= '%s' |
||
222 | AND |
||
223 | fd.active = 'yes' |
||
224 | AND |
||
225 | fcr.category_id = %d |
||
226 | AND |
||
227 | fd.lang = '%s' |
||
228 | %s |
||
229 | ORDER BY |
||
230 | %s.%s %s", |
||
231 | PMF_Db::getTablePrefix(), |
||
232 | PMF_Db::getTablePrefix(), |
||
233 | PMF_Db::getTablePrefix(), |
||
234 | PMF_Db::getTablePrefix(), |
||
235 | PMF_Db::getTablePrefix(), |
||
236 | $now, |
||
237 | $now, |
||
238 | $category_id, |
||
239 | $this->_config->getLanguage()->getLanguage(), |
||
240 | $this->queryPermission($this->groupSupport), |
||
241 | $currentTable, |
||
242 | $this->_config->getDb()->escape($orderby), |
||
243 | $this->_config->getDb()->escape($sortby) |
||
244 | ); |
||
245 | |||
246 | $result = $this->_config->getDb()->query($query); |
||
247 | $num = $this->_config->getDb()->numRows($result); |
||
248 | |||
249 | if ($num > 0) { |
||
250 | $faqHelper = new PMF_Helper_Faq($this->_config); |
||
251 | while (($row = $this->_config->getDb()->fetchObject($result))) { |
||
252 | if (empty($row->visits)) { |
||
253 | $visits = 0; |
||
254 | } else { |
||
255 | $visits = $row->visits; |
||
256 | } |
||
257 | |||
258 | $url = sprintf( |
||
259 | '%sindex.php?%saction=artikel&cat=%d&id=%d&artlang=%s', |
||
260 | $this->_config->getDefaultUrl(), |
||
261 | $sids, |
||
262 | $row->category_id, |
||
263 | $row->id, |
||
264 | $row->lang |
||
265 | ); |
||
266 | $oLink = new PMF_Link($url, $this->_config); |
||
267 | $oLink->itemTitle = $oLink->text = $oLink->tooltip = $row->thema; |
||
268 | |||
269 | $faqdata[] = array( |
||
270 | 'record_id' => $row->id, |
||
271 | 'record_lang' => $row->lang, |
||
272 | 'category_id' => $row->category_id, |
||
273 | 'record_title' => $row->thema, |
||
274 | 'record_preview' => $faqHelper->renderAnswerPreview($row->record_content, 25), |
||
275 | 'record_link' => $oLink->toString(), |
||
276 | 'record_updated' => $row->updated, |
||
277 | 'visits' => $visits, |
||
278 | 'record_created' => $row->created, |
||
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 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') |
||
490 | |||
491 | /** |
||
492 | * This function returns all not expired records from the given record ids. |
||
493 | * |
||
494 | * @param array $recordIds 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 $recordIds, $orderBy = 'fd.id', $sortBy = 'ASC') |
||
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) |
||
671 | { |
||
672 | global $PMF_LANG; |
||
673 | |||
674 | $currentLanguage = $this->_config->getLanguage()->getLanguage(); |
||
675 | $defaultLanguage = $this->_config->getDefaultLanguage(); |
||
676 | |||
677 | $result = $this->getRecordResult($faqId, $currentLanguage, $faqRevisionId, $isAdmin); |
||
678 | |||
679 | if (0 === $this->_config->getDb()->numRows($result)) { |
||
680 | $result = $this->getRecordResult($faqId, $defaultLanguage, $faqRevisionId, $isAdmin); |
||
681 | } |
||
682 | |||
683 | if ($row = $this->_config->getDb()->fetchObject($result)) { |
||
684 | $question = nl2br($row->thema); |
||
685 | $answer = $row->content; |
||
686 | $active = ('yes' === $row->active); |
||
687 | $expired = (date('YmdHis') > $row->date_end); |
||
688 | |||
689 | if (!$isAdmin) { |
||
690 | if (!$active) { |
||
691 | $answer = $this->pmf_lang['err_inactiveArticle']; |
||
692 | } |
||
693 | if ($expired) { |
||
694 | $answer = $this->pmf_lang['err_expiredArticle']; |
||
695 | } |
||
696 | } |
||
697 | |||
698 | $this->faqRecord = [ |
||
699 | 'id' => $row->id, |
||
700 | 'lang' => $row->lang, |
||
701 | 'solution_id' => $row->solution_id, |
||
702 | 'revision_id' => $row->revision_id, |
||
703 | 'active' => $row->active, |
||
704 | 'sticky' => $row->sticky, |
||
705 | 'keywords' => $row->keywords, |
||
706 | 'title' => $question, |
||
707 | 'content' => $answer, |
||
708 | 'author' => $row->author, |
||
709 | 'email' => $row->email, |
||
710 | 'comment' => $row->comment, |
||
711 | 'date' => PMF_Date::createIsoDate($row->updated), |
||
712 | 'dateStart' => $row->date_start, |
||
713 | 'dateEnd' => $row->date_end, |
||
714 | 'linkState' => $row->links_state, |
||
715 | 'linkCheckDate' => $row->links_check_date, |
||
716 | 'notes' => $row->notes, |
||
717 | 'created' => $row->created, |
||
718 | ]; |
||
719 | } else { |
||
720 | $this->faqRecord = [ |
||
721 | 'id' => $faqId, |
||
722 | 'lang' => $currentLanguage, |
||
723 | 'solution_id' => 42, |
||
724 | 'revision_id' => $faqRevisionId, |
||
725 | 'active' => 'no', |
||
726 | 'sticky' => 0, |
||
727 | 'keywords' => '', |
||
728 | 'title' => '', |
||
729 | 'content' => $PMF_LANG['msgAccessDenied'], |
||
730 | 'author' => '', |
||
731 | 'email' => '', |
||
732 | 'comment' => '', |
||
733 | 'date' => PMF_Date::createIsoDate(date('YmdHis')), |
||
734 | 'dateStart' => '', |
||
735 | 'dateEnd' => '', |
||
736 | 'linkState' => '', |
||
737 | 'linkCheckDate' => '', |
||
738 | 'notes' => '', |
||
739 | 'created' => date('c'), |
||
740 | ]; |
||
741 | } |
||
742 | } |
||
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) |
||
789 | |||
790 | /** |
||
791 | * Return records from given IDs |
||
792 | * |
||
793 | * @param array $faqIds |
||
794 | * |
||
795 | * @return array |
||
796 | */ |
||
797 | public function getRecordsByIds(Array $faqIds) |
||
798 | { |
||
799 | $faqRecords = []; |
||
800 | |||
801 | $query = sprintf( |
||
802 | "SELECT |
||
803 | fd.id AS id, |
||
804 | fd.lang AS lang, |
||
805 | fd.thema AS question, |
||
806 | fd.content AS answer, |
||
807 | fd.updated AS updated, |
||
808 | fd.created AS created, |
||
809 | fcr.category_id AS category_id, |
||
810 | fv.visits AS visits |
||
811 | FROM |
||
812 | %sfaqdata fd |
||
813 | LEFT JOIN |
||
814 | %sfaqcategoryrelations fcr |
||
815 | ON |
||
816 | fd.id = fcr.record_id |
||
817 | AND |
||
818 | fd.lang = fcr.record_lang |
||
819 | LEFT JOIN |
||
820 | %sfaqdata_group fdg |
||
821 | ON |
||
822 | fd.id = fdg.record_id |
||
823 | LEFT JOIN |
||
824 | %sfaqvisits AS fv |
||
825 | ON |
||
826 | fd.id = fv.id |
||
827 | AND |
||
828 | fv.lang = fd.lang |
||
829 | LEFT JOIN |
||
830 | %sfaqdata_user fdu |
||
831 | ON |
||
832 | fd.id = fdu.record_id |
||
833 | WHERE |
||
834 | fd.id IN (%s) |
||
835 | AND |
||
836 | fd.lang = '%s' |
||
837 | %s", |
||
838 | PMF_Db::getTablePrefix(), |
||
839 | PMF_Db::getTablePrefix(), |
||
840 | PMF_Db::getTablePrefix(), |
||
841 | PMF_Db::getTablePrefix(), |
||
842 | PMF_Db::getTablePrefix(), |
||
843 | implode(',', $faqIds), |
||
844 | $this->_config->getLanguage()->getLanguage(), |
||
845 | $this->queryPermission($this->groupSupport) |
||
846 | ); |
||
847 | |||
848 | $result = $this->_config->getDb()->query($query); |
||
849 | |||
850 | $faqHelper = new PMF_Helper_Faq($this->_config); |
||
851 | while ($row = $this->_config->getDb()->fetchObject($result)) { |
||
852 | if (empty($row->visits)) { |
||
853 | $visits = 0; |
||
854 | } else { |
||
855 | $visits = $row->visits; |
||
856 | } |
||
857 | |||
858 | $url = sprintf( |
||
859 | '%sindex.php?action=artikel&cat=%d&id=%d&artlang=%s', |
||
860 | $this->_config->getDefaultUrl(), |
||
861 | $row->category_id, |
||
862 | $row->id, |
||
863 | $row->lang |
||
864 | ); |
||
865 | $oLink = new PMF_Link($url, $this->_config); |
||
866 | $oLink->itemTitle = $oLink->text = $oLink->tooltip = $row->question; |
||
867 | |||
868 | $faqRecords[] = [ |
||
869 | 'record_id' => (int)$row->id, |
||
870 | 'record_lang' => $row->lang, |
||
871 | 'category_id' => (int)$row->category_id, |
||
872 | 'record_title' => $row->question, |
||
873 | 'record_preview' => $faqHelper->renderAnswerPreview($row->answer, 25), |
||
874 | 'record_link' => $oLink->toString(), |
||
875 | 'record_updated' => PMF_Date::createIsoDate($row->updated).':00', |
||
876 | 'visits' => (int)$visits, |
||
877 | 'record_created' => $row->created |
||
878 | ]; |
||
879 | } |
||
880 | |||
881 | return $faqRecords; |
||
882 | } |
||
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) |
||
941 | { |
||
942 | // Update entry |
||
943 | $query = sprintf(" |
||
944 | UPDATE |
||
945 | %sfaqdata |
||
946 | SET |
||
947 | revision_id = %d, |
||
948 | active = '%s', |
||
949 | sticky = %d, |
||
950 | keywords = '%s', |
||
951 | thema = '%s', |
||
952 | content = '%s', |
||
953 | author = '%s', |
||
954 | email = '%s', |
||
955 | comment = '%s', |
||
956 | updated = '%s', |
||
957 | links_state = '%s', |
||
958 | links_check_date = %d, |
||
959 | date_start = '%s', |
||
960 | date_end = '%s', |
||
961 | notes = '%s' |
||
962 | WHERE |
||
963 | id = %d |
||
964 | AND |
||
965 | lang = '%s'", |
||
966 | PMF_Db::getTablePrefix(), |
||
967 | $data['revision_id'], |
||
968 | $data['active'], |
||
969 | $data['sticky'], |
||
970 | $this->_config->getDb()->escape($data['keywords']), |
||
971 | $this->_config->getDb()->escape($data['thema']), |
||
972 | $this->_config->getDb()->escape($data['content']), |
||
973 | $this->_config->getDb()->escape($data['author']), |
||
974 | $data['email'], |
||
975 | $data['comment'], |
||
976 | $data['date'], |
||
977 | $data['linkState'], |
||
978 | $data['linkDateCheck'], |
||
979 | $data['dateStart'], |
||
980 | $data['dateEnd'], |
||
981 | $data['notes'], |
||
982 | $data['id'], |
||
983 | $data['lang'] |
||
984 | ); |
||
985 | |||
986 | $this->_config->getDb()->query($query); |
||
987 | |||
988 | return true; |
||
989 | } |
||
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) |
||
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) |
||
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') |
||
1130 | { |
||
1131 | if ('news' == $record_type) { |
||
1132 | $table = 'faqnews'; |
||
1133 | } else { |
||
1134 | $table = 'faqdata'; |
||
1135 | } |
||
1136 | |||
1137 | $query = sprintf(" |
||
1138 | SELECT |
||
1139 | comment |
||
1140 | FROM |
||
1141 | %s%s |
||
1142 | WHERE |
||
1143 | id = %d |
||
1144 | AND |
||
1145 | lang = '%s'", |
||
1146 | PMF_Db::getTablePrefix(), |
||
1147 | $table, |
||
1148 | $record_id, |
||
1149 | $record_lang |
||
1150 | ); |
||
1151 | |||
1152 | $result = $this->_config->getDb()->query($query); |
||
1153 | |||
1154 | if ($row = $this->_config->getDb()->fetchObject($result)) { |
||
1155 | return ($row->comment === 'y') ? false : true; |
||
1156 | } else { |
||
1157 | return true; |
||
1158 | } |
||
1159 | } |
||
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) |
||
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) |
||
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 | View Code Duplication | 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) |
||
1243 | { |
||
1244 | $query = sprintf( |
||
1245 | 'SELECT |
||
1246 | * |
||
1247 | FROM |
||
1248 | %sfaqdata fd |
||
1249 | LEFT JOIN |
||
1250 | %sfaqdata_group fdg |
||
1251 | ON |
||
1252 | fd.id = fdg.record_id |
||
1253 | LEFT JOIN |
||
1254 | %sfaqdata_user fdu |
||
1255 | ON |
||
1256 | fd.id = fdu.record_id |
||
1257 | WHERE |
||
1258 | fd.solution_id = %d |
||
1259 | %s', |
||
1260 | PMF_Db::getTablePrefix(), |
||
1261 | PMF_Db::getTablePrefix(), |
||
1262 | PMF_Db::getTablePrefix(), |
||
1263 | $solutionId, |
||
1264 | $this->queryPermission($this->groupSupport) |
||
1265 | ); |
||
1266 | |||
1267 | $result = $this->_config->getDb()->query($query); |
||
1268 | |||
1269 | if ($row = $this->_config->getDb()->fetchObject($result)) { |
||
1270 | $question = nl2br($row->thema); |
||
1271 | $content = $row->content; |
||
1272 | $active = ('yes' == $row->active); |
||
1273 | $expired = (date('YmdHis') > $row->date_end); |
||
1274 | |||
1275 | if (!$active) { |
||
1276 | $content = $this->pmf_lang['err_inactiveArticle']; |
||
1277 | } |
||
1278 | if ($expired) { |
||
1279 | $content = $this->pmf_lang['err_expiredArticle']; |
||
1280 | } |
||
1281 | |||
1282 | $this->faqRecord = array( |
||
1283 | 'id' => $row->id, |
||
1284 | 'lang' => $row->lang, |
||
1285 | 'solution_id' => $row->solution_id, |
||
1286 | 'revision_id' => $row->revision_id, |
||
1287 | 'active' => $row->active, |
||
1288 | 'sticky' => $row->sticky, |
||
1289 | 'keywords' => $row->keywords, |
||
1290 | 'title' => $question, |
||
1291 | 'content' => $content, |
||
1292 | 'author' => $row->author, |
||
1293 | 'email' => $row->email, |
||
1294 | 'comment' => $row->comment, |
||
1295 | 'date' => PMF_Date::createIsoDate($row->updated), |
||
1296 | 'dateStart' => $row->date_start, |
||
1297 | 'dateEnd' => $row->date_end, |
||
1298 | 'linkState' => $row->links_state, |
||
1299 | 'linkCheckDate' => $row->links_check_date, |
||
1300 | 'notes' => $row->notes |
||
1301 | ); |
||
1302 | } |
||
1303 | } |
||
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 | fd.id, |
||
1317 | fd.lang, |
||
1318 | fd.thema AS question, |
||
1319 | fd.content, |
||
1320 | fcr.category_id AS category_id |
||
1321 | FROM |
||
1322 | %sfaqdata fd |
||
1323 | LEFT JOIN |
||
1324 | %sfaqcategoryrelations fcr |
||
1325 | ON |
||
1326 | fd.id = fcr.record_id |
||
1327 | AND |
||
1328 | fd.lang = fcr.record_lang |
||
1329 | WHERE |
||
1330 | fd.solution_id = %d', |
||
1331 | PMF_Db::getTablePrefix(), |
||
1332 | PMF_Db::getTablePrefix(), |
||
1333 | $solutionId |
||
1334 | ); |
||
1335 | |||
1336 | $result = $this->_config->getDb()->query($query); |
||
1337 | |||
1338 | if ($row = $this->_config->getDb()->fetchObject($result)) { |
||
1339 | return [ |
||
1340 | 'id' => $row->id, |
||
1341 | 'lang' => $row->lang, |
||
1342 | 'question' => $row->question, |
||
1343 | 'content' => $row->content, |
||
1344 | 'category_id' => $row->category_id |
||
1345 | ]; |
||
1346 | } |
||
1347 | |||
1348 | return []; |
||
1349 | } |
||
1350 | |||
1351 | /** |
||
1352 | * Returns the solution ID from a given ID and language |
||
1353 | * |
||
1354 | * @param integer $faqId |
||
1355 | * @param string $faqLang |
||
1356 | * |
||
1357 | * @return int |
||
1358 | */ |
||
1359 | public function getSolutionIdFromId($faqId, $faqLang) |
||
1383 | |||
1384 | /** |
||
1385 | * Gets the latest solution id for a FAQ record. |
||
1386 | * |
||
1387 | * @return int |
||
1388 | */ |
||
1389 | public function getSolutionId() |
||
1415 | |||
1416 | /** |
||
1417 | * Returns an array with all data from all FAQ records. |
||
1418 | * |
||
1419 | * @param int $sortType Sorting type |
||
1420 | * @param array $condition Condition |
||
1421 | * @param string $sortOrder Sorting order |
||
1422 | */ |
||
1423 | public function getAllRecords($sortType = FAQ_SORTING_TYPE_CATID_FAQID, Array $condition = null, $sortOrder = 'ASC') |
||
1424 | { |
||
1425 | $where = ''; |
||
1426 | if (!is_null($condition)) { |
||
1427 | $num = count($condition); |
||
1428 | $where = 'WHERE '; |
||
1429 | foreach ($condition as $field => $data) { |
||
1430 | --$num; |
||
1431 | $where .= $field; |
||
1432 | if (is_array($data)) { |
||
1433 | $where .= ' IN ('; |
||
1434 | $separator = ''; |
||
1435 | foreach ($data as $value) { |
||
1436 | $where .= $separator."'".$this->_config->getDb()->escape($value)."'"; |
||
1437 | $separator = ', '; |
||
1438 | } |
||
1439 | $where .= ')'; |
||
1440 | } else { |
||
1441 | $where .= " = '".$this->_config->getDb()->escape($data)."'"; |
||
1442 | } |
||
1443 | if ($num > 0) { |
||
1444 | $where .= ' AND '; |
||
1445 | } |
||
1446 | } |
||
1447 | } |
||
1448 | |||
1449 | switch ($sortType) { |
||
1450 | |||
1451 | case FAQ_SORTING_TYPE_CATID_FAQID: |
||
1452 | $orderBy = sprintf(' |
||
1453 | ORDER BY |
||
1454 | fcr.category_id, |
||
1455 | fd.id %s', |
||
1456 | $sortOrder); |
||
1457 | break; |
||
1458 | |||
1459 | case FAQ_SORTING_TYPE_FAQID: |
||
1460 | $orderBy = sprintf(' |
||
1461 | ORDER BY |
||
1462 | fd.id %s', |
||
1463 | $sortOrder); |
||
1464 | break; |
||
1465 | |||
1466 | case FAQ_SORTING_TYPE_FAQTITLE_FAQID: |
||
1467 | $orderBy = sprintf(' |
||
1468 | ORDER BY |
||
1469 | fcr.category_id, |
||
1470 | fd.thema %s', |
||
1471 | $sortOrder); |
||
1472 | break; |
||
1473 | |||
1474 | case FAQ_SORTING_TYPE_DATE_FAQID: |
||
1475 | $orderBy = sprintf(' |
||
1476 | ORDER BY |
||
1477 | fcr.category_id, |
||
1478 | fd.updated %s', |
||
1479 | $sortOrder); |
||
1480 | break; |
||
1481 | |||
1482 | default: |
||
1483 | $orderBy = ''; |
||
1484 | break; |
||
1485 | } |
||
1486 | |||
1487 | $query = sprintf(' |
||
1488 | SELECT |
||
1489 | fd.id AS id, |
||
1490 | fd.lang AS lang, |
||
1491 | fcr.category_id AS category_id, |
||
1492 | fd.solution_id AS solution_id, |
||
1493 | fd.revision_id AS revision_id, |
||
1494 | fd.active AS active, |
||
1495 | fd.sticky AS sticky, |
||
1496 | fd.keywords AS keywords, |
||
1497 | fd.thema AS thema, |
||
1498 | fd.content AS content, |
||
1499 | fd.author AS author, |
||
1500 | fd.email AS email, |
||
1501 | fd.comment AS comment, |
||
1502 | fd.updated AS updated, |
||
1503 | fd.links_state AS links_state, |
||
1504 | fd.links_check_date AS links_check_date, |
||
1505 | fd.date_start AS date_start, |
||
1506 | fd.date_end AS date_end, |
||
1507 | fd.sticky AS sticky, |
||
1508 | fd.created AS created, |
||
1509 | fd.notes AS notes |
||
1510 | FROM |
||
1511 | %sfaqdata fd |
||
1512 | LEFT JOIN |
||
1513 | %sfaqcategoryrelations fcr |
||
1514 | ON |
||
1515 | fd.id = fcr.record_id |
||
1516 | AND |
||
1517 | fd.lang = fcr.record_lang |
||
1518 | LEFT JOIN |
||
1519 | %sfaqdata_group AS fdg |
||
1520 | ON |
||
1521 | fd.id = fdg.record_id |
||
1522 | LEFT JOIN |
||
1523 | %sfaqdata_user AS fdu |
||
1524 | ON |
||
1525 | fd.id = fdu.record_id |
||
1526 | %s |
||
1527 | %s |
||
1528 | %s', |
||
1529 | PMF_Db::getTablePrefix(), |
||
1530 | PMF_Db::getTablePrefix(), |
||
1531 | PMF_Db::getTablePrefix(), |
||
1532 | PMF_Db::getTablePrefix(), |
||
1533 | $where, |
||
1534 | $this->queryPermission($this->groupSupport), |
||
1535 | $orderBy |
||
1536 | ); |
||
1537 | |||
1538 | $result = $this->_config->getDb()->query($query); |
||
1539 | |||
1540 | while ($row = $this->_config->getDb()->fetchObject($result)) { |
||
1541 | $content = $row->content; |
||
1542 | $active = ('yes' == $row->active); |
||
1543 | $expired = (date('YmdHis') > $row->date_end); |
||
1544 | |||
1545 | if (!$active) { |
||
1546 | $content = $this->pmf_lang['err_inactiveArticle']; |
||
1547 | } |
||
1548 | if ($expired) { |
||
1549 | $content = $this->pmf_lang['err_expiredArticle']; |
||
1550 | } |
||
1551 | |||
1552 | $this->faqRecords[] = [ |
||
1553 | 'id' => $row->id, |
||
1554 | 'category_id' => $row->category_id, |
||
1555 | 'lang' => $row->lang, |
||
1556 | 'solution_id' => $row->solution_id, |
||
1557 | 'revision_id' => $row->revision_id, |
||
1558 | 'active' => $row->active, |
||
1559 | 'sticky' => $row->sticky, |
||
1560 | 'keywords' => $row->keywords, |
||
1561 | 'title' => $row->thema, |
||
1562 | 'content' => $content, |
||
1563 | 'author' => $row->author, |
||
1564 | 'email' => $row->email, |
||
1565 | 'comment' => $row->comment, |
||
1566 | 'updated' => PMF_Date::createIsoDate($row->updated, 'Y-m-d H:i:s'), |
||
1567 | 'dateStart' => $row->date_start, |
||
1568 | 'dateEnd' => $row->date_end, |
||
1569 | 'created' => $row->created, |
||
1570 | 'notes' => $row->notes |
||
1571 | ]; |
||
1572 | } |
||
1573 | } |
||
1574 | |||
1575 | /** |
||
1576 | * Returns the FAQ record title from the ID and language. |
||
1577 | * |
||
1578 | * @param int $id Record id |
||
1579 | * |
||
1580 | * @return string |
||
1581 | */ |
||
1582 | public function getRecordTitle($id) |
||
1583 | { |
||
1584 | if (isset($this->faqRecord['id']) && ($this->faqRecord['id'] == $id)) { |
||
1585 | return $this->faqRecord['title']; |
||
1586 | } |
||
1587 | |||
1588 | $question = ''; |
||
1589 | |||
1590 | $query = sprintf( |
||
1591 | "SELECT |
||
1592 | thema AS question |
||
1593 | FROM |
||
1594 | %sfaqdata |
||
1595 | WHERE |
||
1596 | id = %d AND lang = '%s'", |
||
1597 | PMF_Db::getTablePrefix(), |
||
1598 | $id, |
||
1599 | $this->_config->getLanguage()->getLanguage() |
||
1600 | ); |
||
1601 | $result = $this->_config->getDb()->query($query); |
||
1602 | |||
1603 | if ($this->_config->getDb()->numRows($result) > 0) { |
||
1604 | while ($row = $this->_config->getDb()->fetchObject($result)) { |
||
1605 | $question = PMF_String::htmlspecialchars($row->question); |
||
1606 | } |
||
1607 | } else { |
||
1608 | $question = $this->pmf_lang['no_cats']; |
||
1609 | } |
||
1610 | |||
1611 | return $question; |
||
1612 | } |
||
1613 | |||
1614 | /** |
||
1615 | * Gets all revisions from a given record ID. |
||
1616 | * |
||
1617 | * @param int $recordId Record id |
||
1618 | * @param string $recordLang Record language |
||
1619 | * |
||
1620 | * @return array |
||
1621 | */ |
||
1622 | public function getRevisionIds($recordId, $recordLang) |
||
1656 | |||
1657 | /** |
||
1658 | * Adds a new revision from a given record ID. |
||
1659 | * |
||
1660 | * @param int $record_id Record id |
||
1661 | * @param string $record_lang Record language |
||
1662 | * |
||
1663 | * @return array |
||
1664 | */ |
||
1665 | View Code Duplication | public function addNewRevision($record_id, $record_lang) |
|
1666 | { |
||
1667 | $query = sprintf(" |
||
1668 | INSERT INTO |
||
1669 | %sfaqdata_revisions |
||
1670 | SELECT * FROM |
||
1671 | %sfaqdata |
||
1672 | WHERE |
||
1673 | id = %d |
||
1674 | AND |
||
1675 | lang = '%s'", |
||
1676 | PMF_Db::getTablePrefix(), |
||
1677 | PMF_Db::getTablePrefix(), |
||
1678 | $record_id, |
||
1679 | $record_lang); |
||
1680 | $this->_config->getDb()->query($query); |
||
1681 | |||
1682 | return true; |
||
1683 | } |
||
1684 | |||
1685 | /** |
||
1686 | * Returns the keywords of a FAQ record from the ID and language. |
||
1687 | * |
||
1688 | * @param int $id record id |
||
1689 | * |
||
1690 | * @return string |
||
1691 | */ |
||
1692 | public function getRecordKeywords($id) |
||
1718 | |||
1719 | /** |
||
1720 | * Returns a answer preview of the FAQ record. |
||
1721 | * |
||
1722 | * @param int $recordId FAQ record ID |
||
1723 | * @param int $wordCount Number of words, default: 12 |
||
1724 | * |
||
1725 | * @return string |
||
1726 | */ |
||
1727 | public function getRecordPreview($recordId, $wordCount = 12) |
||
1728 | { |
||
1729 | if (isset($this->faqRecord['id']) && ((int)$this->faqRecord['id'] === (int)$recordId)) { |
||
1730 | $answerPreview = $this->faqRecord['content']; |
||
1731 | |||
1732 | return PMF_Utils::makeShorterText($answerPreview, $wordCount); |
||
1733 | } |
||
1734 | |||
1735 | $query = sprintf(" |
||
1736 | SELECT |
||
1737 | content as answer |
||
1738 | FROM |
||
1739 | %sfaqdata |
||
1740 | WHERE |
||
1741 | id = %d |
||
1742 | AND |
||
1743 | lang = '%s'", |
||
1744 | PMF_Db::getTablePrefix(), |
||
1745 | $recordId, |
||
1746 | $this->_config->getLanguage()->getLanguage() |
||
1747 | ); |
||
1748 | |||
1749 | $result = $this->_config->getDb()->query($query); |
||
1750 | |||
1751 | if ($this->_config->getDb()->numRows($result) > 0) { |
||
1752 | $row = $this->_config->getDb()->fetchObject($result); |
||
1753 | $answerPreview = strip_tags($row->answer); |
||
1754 | } else { |
||
1755 | $answerPreview = $this->_config->get('main.metaDescription'); |
||
1756 | } |
||
1757 | |||
1758 | return PMF_Utils::makeShorterText($answerPreview, $wordCount); |
||
1759 | } |
||
1760 | |||
1761 | /** |
||
1762 | * Returns the number of activated and not expired records, optionally |
||
1763 | * not limited to the current language. |
||
1764 | * |
||
1765 | * @param string $language Language |
||
1766 | * |
||
1767 | * @return int |
||
1768 | */ |
||
1769 | public function getNumberOfRecords($language = null) |
||
1799 | |||
1800 | /** |
||
1801 | * This function generates a list with the most voted or most visited records. |
||
1802 | * |
||
1803 | * @param string $type Type definition visits/voted |
||
1804 | * |
||
1805 | * @since 2009-11-03 |
||
1806 | * |
||
1807 | * @author Max Köhler <[email protected]> |
||
1808 | * |
||
1809 | * @return array |
||
1810 | */ |
||
1811 | public function getTopTen($type = 'visits') |
||
1845 | |||
1846 | /** |
||
1847 | * This function generates the list with the latest published records. |
||
1848 | * |
||
1849 | * @return array |
||
1850 | */ |
||
1851 | public function getLatest() |
||
1870 | |||
1871 | /** |
||
1872 | * Deletes a question for the table faqquestions. |
||
1873 | * |
||
1874 | * @param int $questionId |
||
1875 | * |
||
1876 | * @return bool |
||
1877 | */ |
||
1878 | View Code Duplication | public function deleteQuestion($questionId) |
|
1879 | { |
||
1880 | $delete = sprintf(" |
||
1881 | DELETE FROM |
||
1882 | %sfaqquestions |
||
1883 | WHERE |
||
1884 | id = %d |
||
1885 | AND |
||
1886 | lang = '%s'", |
||
1887 | PMF_Db::getTablePrefix(), |
||
1888 | $questionId, |
||
1889 | $this->_config->getLanguage()->getLanguage() |
||
1890 | ); |
||
1891 | |||
1892 | $this->_config->getDb()->query($delete); |
||
1893 | |||
1894 | return true; |
||
1895 | } |
||
1896 | |||
1897 | /** |
||
1898 | * Returns the visibility of a question. |
||
1899 | * |
||
1900 | * @param int $questionId |
||
1901 | * |
||
1902 | * @return string |
||
1903 | */ |
||
1904 | View Code Duplication | public function getVisibilityOfQuestion($questionId) |
|
1905 | { |
||
1906 | $query = sprintf(" |
||
1907 | SELECT |
||
1908 | is_visible |
||
1909 | FROM |
||
1910 | %sfaqquestions |
||
1911 | WHERE |
||
1912 | id = %d |
||
1913 | AND |
||
1914 | lang = '%s'", |
||
1915 | PMF_Db::getTablePrefix(), |
||
1916 | $questionId, |
||
1917 | $this->_config->getLanguage()->getLanguage() |
||
1918 | ); |
||
1919 | |||
1920 | $result = $this->_config->getDb()->query($query); |
||
1921 | if ($this->_config->getDb()->numRows($result) > 0) { |
||
1922 | $row = $this->_config->getDb()->fetchObject($result); |
||
1923 | |||
1924 | return $row->is_visible; |
||
1925 | } |
||
1926 | |||
1927 | return; |
||
1928 | } |
||
1929 | |||
1930 | /** |
||
1931 | * Sets the visibility of a question. |
||
1932 | * |
||
1933 | * @param int $questionId |
||
1934 | * @param string $isVisible |
||
1935 | * |
||
1936 | * @return bool |
||
1937 | */ |
||
1938 | View Code Duplication | public function setVisibilityOfQuestion($questionId, $isVisible) |
|
1939 | { |
||
1940 | $query = sprintf(" |
||
1941 | UPDATE |
||
1942 | %sfaqquestions |
||
1943 | SET |
||
1944 | is_visible = '%s' |
||
1945 | WHERE |
||
1946 | id = %d |
||
1947 | AND |
||
1948 | lang = '%s'", |
||
1949 | PMF_Db::getTablePrefix(), |
||
1950 | $isVisible, |
||
1951 | $questionId, |
||
1952 | $this->_config->getLanguage()->getLanguage() |
||
1953 | ); |
||
1954 | |||
1955 | $this->_config->getDb()->query($query); |
||
1956 | |||
1957 | return true; |
||
1958 | } |
||
1959 | |||
1960 | /** |
||
1961 | * This function generates a data-set with the most voted FAQs. |
||
1962 | * |
||
1963 | * @param int $count Number of records |
||
1964 | * @param string $language Language |
||
1965 | * |
||
1966 | * @return array |
||
1967 | */ |
||
1968 | public function getTopVotedData($count = PMF_NUMBER_RECORDS_TOPTEN, $language = null) |
||
2055 | |||
2056 | /** |
||
2057 | * This function generates the Top Ten data with the mosted viewed records. |
||
2058 | * |
||
2059 | * @param int $count Number of records |
||
2060 | * @param int $categoryId Category ID |
||
2061 | * @param string $language Language |
||
2062 | * |
||
2063 | * @return array |
||
2064 | */ |
||
2065 | public function getTopTenData($count = PMF_NUMBER_RECORDS_TOPTEN, $categoryId = 0, $language = null) |
||
2066 | { |
||
2067 | global $sids; |
||
2068 | |||
2069 | $now = date('YmdHis'); |
||
2070 | $query = |
||
2071 | ' SELECT |
||
2072 | fd.id AS id, |
||
2073 | fd.lang AS lang, |
||
2074 | fd.thema AS question, |
||
2075 | fd.updated AS updated, |
||
2076 | fcr.category_id AS category_id, |
||
2077 | fv.visits AS visits, |
||
2078 | fv.last_visit AS last_visit, |
||
2079 | fdg.group_id AS group_id, |
||
2080 | fdu.user_id AS user_id |
||
2081 | FROM |
||
2082 | '.PMF_Db::getTablePrefix().'faqvisits fv, |
||
2083 | '.PMF_Db::getTablePrefix().'faqdata fd |
||
2084 | LEFT JOIN |
||
2085 | '.PMF_Db::getTablePrefix().'faqcategoryrelations fcr |
||
2086 | ON |
||
2087 | fd.id = fcr.record_id |
||
2088 | AND |
||
2089 | fd.lang = fcr.record_lang |
||
2090 | LEFT JOIN |
||
2091 | '.PMF_Db::getTablePrefix().'faqdata_group AS fdg |
||
2092 | ON |
||
2093 | fd.id = fdg.record_id |
||
2094 | LEFT JOIN |
||
2095 | '.PMF_Db::getTablePrefix().'faqdata_user AS fdu |
||
2096 | ON |
||
2097 | fd.id = fdu.record_id |
||
2098 | WHERE |
||
2099 | fd.date_start <= \''.$now.'\' |
||
2100 | AND fd.date_end >= \''.$now.'\' |
||
2101 | AND fd.id = fv.id |
||
2102 | AND fd.lang = fv.lang |
||
2103 | AND fd.active = \'yes\''; |
||
2104 | |||
2105 | View Code Duplication | if (isset($categoryId) && is_numeric($categoryId) && ($categoryId != 0)) { |
|
2106 | $query .= ' |
||
2107 | AND |
||
2108 | fcr.category_id = \''.$categoryId.'\''; |
||
2109 | } |
||
2110 | if (isset($language) && PMF_Language::isASupportedLanguage($language)) { |
||
2111 | $query .= ' |
||
2112 | AND |
||
2113 | fd.lang = \''.$language.'\''; |
||
2114 | } |
||
2115 | $query .= ' |
||
2116 | '.$this->queryPermission($this->groupSupport).' |
||
2117 | |||
2118 | GROUP BY |
||
2119 | fd.id, fd.lang, fd.thema, fd.updated, fcr.category_id, fv.visits, fv.last_visit, fdg.group_id, fdu.user_id |
||
2120 | ORDER BY |
||
2121 | fv.visits DESC'; |
||
2122 | |||
2123 | $result = $this->_config->getDb()->query($query); |
||
2124 | $topten = []; |
||
2125 | $data = []; |
||
2126 | |||
2127 | View Code Duplication | if ($result) { |
|
2128 | while ($row = $this->_config->getDb()->fetchObject($result)) { |
||
2129 | if ($this->groupSupport) { |
||
2130 | if (!in_array($row->user_id, array(-1, $this->user)) || !in_array($row->group_id, $this->groups)) { |
||
2131 | continue; |
||
2132 | } |
||
2133 | } else { |
||
2134 | if (!in_array($row->user_id, array(-1, $this->user))) { |
||
2135 | continue; |
||
2136 | } |
||
2137 | } |
||
2138 | |||
2139 | $data['visits'] = (int)$row->visits; |
||
2140 | $data['question'] = PMF_Filter::filterVar($row->question, FILTER_SANITIZE_STRING); |
||
2141 | $data['date'] = $row->updated; |
||
2142 | $data['last_visit'] = $row->last_visit; |
||
2143 | |||
2144 | $title = $row->question; |
||
2145 | $url = sprintf( |
||
2146 | '%sindex.php?%saction=artikel&cat=%d&id=%d&artlang=%s', |
||
2147 | $this->_config->getDefaultUrl(), |
||
2148 | $sids, |
||
2149 | $row->category_id, |
||
2150 | $row->id, |
||
2151 | $row->lang |
||
2152 | ); |
||
2153 | $oLink = new PMF_Link($url, $this->_config); |
||
2154 | $oLink->itemTitle = $row->question; |
||
2155 | $oLink->tooltip = $title; |
||
2156 | $data['url'] = $oLink->toString(); |
||
2157 | |||
2158 | $topten[$row->id] = $data; |
||
2159 | |||
2160 | if (count($topten) === $count) { |
||
2161 | break; |
||
2162 | } |
||
2163 | } |
||
2164 | |||
2165 | array_multisort($topten, SORT_DESC); |
||
2166 | } |
||
2167 | |||
2168 | return $topten; |
||
2169 | } |
||
2170 | |||
2171 | /** |
||
2172 | * This function generates an array with a specified number of most recent |
||
2173 | * published records. |
||
2174 | * |
||
2175 | * @param int $count Number of records |
||
2176 | * @param string $language Language |
||
2177 | * |
||
2178 | * @return array |
||
2179 | */ |
||
2180 | public function getLatestData($count = PMF_NUMBER_RECORDS_LATEST, $language = null) |
||
2181 | { |
||
2182 | global $sids; |
||
2183 | |||
2184 | $now = date('YmdHis'); |
||
2185 | $query = |
||
2186 | ' SELECT |
||
2187 | fd.id AS id, |
||
2188 | fd.lang AS lang, |
||
2189 | fcr.category_id AS category_id, |
||
2190 | fd.thema AS question, |
||
2191 | fd.content AS content, |
||
2192 | fd.updated AS updated, |
||
2193 | fv.visits AS visits, |
||
2194 | fdg.group_id AS group_id, |
||
2195 | fdu.user_id AS user_id |
||
2196 | FROM |
||
2197 | '.PMF_Db::getTablePrefix().'faqvisits fv, |
||
2198 | '.PMF_Db::getTablePrefix().'faqdata fd |
||
2199 | LEFT JOIN |
||
2200 | '.PMF_Db::getTablePrefix().'faqcategoryrelations fcr |
||
2201 | ON |
||
2202 | fd.id = fcr.record_id |
||
2203 | AND |
||
2204 | fd.lang = fcr.record_lang |
||
2205 | LEFT JOIN |
||
2206 | '.PMF_Db::getTablePrefix().'faqdata_group AS fdg |
||
2207 | ON |
||
2208 | fd.id = fdg.record_id |
||
2209 | LEFT JOIN |
||
2210 | '.PMF_Db::getTablePrefix().'faqdata_user AS fdu |
||
2211 | ON |
||
2212 | fd.id = fdu.record_id |
||
2213 | WHERE |
||
2214 | fd.date_start <= \''.$now.'\' |
||
2215 | AND fd.date_end >= \''.$now.'\' |
||
2216 | AND fd.id = fv.id |
||
2217 | AND fd.lang = fv.lang |
||
2218 | AND fd.active = \'yes\''; |
||
2219 | |||
2220 | if (isset($language) && PMF_Language::isASupportedLanguage($language)) { |
||
2221 | $query .= ' |
||
2222 | AND |
||
2223 | fd.lang = \''.$language.'\''; |
||
2224 | } |
||
2225 | $query .= ' |
||
2226 | '.$this->queryPermission($this->groupSupport).' |
||
2227 | GROUP BY |
||
2228 | fd.id, fd.lang, fcr.category_id, fd.thema, fd.content, fd.updated, fv.visits, fdg.group_id, fdu.user_id |
||
2229 | ORDER BY |
||
2230 | fd.updated DESC'; |
||
2231 | |||
2232 | $result = $this->_config->getDb()->query($query); |
||
2233 | $latest = []; |
||
2234 | $data = []; |
||
2235 | |||
2236 | View Code Duplication | if ($result) { |
|
2237 | while (($row = $this->_config->getDb()->fetchObject($result))) { |
||
2238 | if ($this->groupSupport) { |
||
2239 | if (!in_array($row->user_id, array(-1, $this->user)) || !in_array($row->group_id, $this->groups)) { |
||
2240 | continue; |
||
2241 | } |
||
2242 | } else { |
||
2243 | if (!in_array($row->user_id, array(-1, $this->user))) { |
||
2244 | continue; |
||
2245 | } |
||
2246 | } |
||
2247 | |||
2248 | $data['date'] = $row->updated; |
||
2249 | $data['question'] = PMF_Filter::filterVar($row->question, FILTER_SANITIZE_STRING); |
||
2250 | $data['answer'] = $row->content; |
||
2251 | $data['visits'] = $row->visits; |
||
2252 | |||
2253 | $title = $row->question; |
||
2254 | $url = sprintf( |
||
2255 | '%sindex.php?%saction=artikel&cat=%d&id=%d&artlang=%s', |
||
2256 | $this->_config->getDefaultUrl(), |
||
2257 | $sids, |
||
2258 | $row->category_id, |
||
2259 | $row->id, |
||
2260 | $row->lang |
||
2261 | ); |
||
2262 | $oLink = new PMF_Link($url, $this->_config); |
||
2263 | $oLink->itemTitle = $title; |
||
2264 | $oLink->tooltip = $title; |
||
2265 | $data['url'] = $oLink->toString(); |
||
2266 | |||
2267 | $latest[$row->id] = $data; |
||
2268 | |||
2269 | if (count($latest) === $count) { |
||
2270 | break; |
||
2271 | } |
||
2272 | } |
||
2273 | } |
||
2274 | |||
2275 | return $latest; |
||
2276 | } |
||
2277 | |||
2278 | /** |
||
2279 | * Reload locking for user votings. |
||
2280 | * |
||
2281 | * @param int $id FAQ record id |
||
2282 | * @param string $ip IP |
||
2283 | * |
||
2284 | * @return bool |
||
2285 | */ |
||
2286 | public function votingCheck($id, $ip) |
||
2306 | |||
2307 | /** |
||
2308 | * Returns the number of users from the table faqvotings. |
||
2309 | * |
||
2310 | * @param integer $record_id |
||
2311 | * |
||
2312 | * @return integer |
||
2313 | */ |
||
2314 | View Code Duplication | public function getNumberOfVotings($record_id) |
|
2333 | |||
2334 | /** |
||
2335 | * Adds a new voting record. |
||
2336 | * |
||
2337 | * @param array $votingData |
||
2338 | * |
||
2339 | * @return bool |
||
2340 | */ |
||
2341 | public function addVoting($votingData) |
||
2362 | |||
2363 | /** |
||
2364 | * Adds a new question. |
||
2365 | * |
||
2366 | * @param array $questionData |
||
2367 | * |
||
2368 | * @return bool |
||
2369 | */ |
||
2370 | public function addQuestion(Array $questionData) |
||
2371 | { |
||
2372 | $query = sprintf(" |
||
2373 | INSERT INTO |
||
2374 | %sfaqquestions |
||
2375 | (id, lang, username, email, category_id, question, created, is_visible, answer_id) |
||
2376 | VALUES |
||
2377 | (%d, '%s', '%s', '%s', %d, '%s', '%s', '%s', %d)", |
||
2378 | PMF_Db::getTablePrefix(), |
||
2379 | $this->_config->getDb()->nextId(PMF_Db::getTablePrefix().'faqquestions', 'id'), |
||
2380 | $this->_config->getLanguage()->getLanguage(), |
||
2381 | $this->_config->getDb()->escape($questionData['username']), |
||
2382 | $this->_config->getDb()->escape($questionData['email']), |
||
2383 | $questionData['category_id'], |
||
2384 | $this->_config->getDb()->escape($questionData['question']), |
||
2385 | date('YmdHis'), |
||
2386 | $questionData['is_visible'], |
||
2387 | 0 |
||
2388 | ); |
||
2389 | $this->_config->getDb()->query($query); |
||
2390 | |||
2391 | return true; |
||
2392 | } |
||
2393 | |||
2394 | /** |
||
2395 | * Returns a new question. |
||
2396 | * |
||
2397 | * @param int $questionId |
||
2398 | * |
||
2399 | * @return array |
||
2400 | */ |
||
2401 | public function getQuestion($questionId) |
||
2450 | |||
2451 | /** |
||
2452 | * Returns all open questions. |
||
2453 | * |
||
2454 | * @param boolean $all If true, then return visible and non-visible |
||
2455 | * questions; otherwise only visible ones |
||
2456 | * |
||
2457 | * @return array |
||
2458 | */ |
||
2459 | public function getAllOpenQuestions($all = true) |
||
2460 | { |
||
2461 | $questions = []; |
||
2462 | |||
2463 | $query = sprintf(" |
||
2464 | SELECT |
||
2465 | id, lang, username, email, category_id, question, created, answer_id, is_visible |
||
2466 | FROM |
||
2467 | %sfaqquestions |
||
2468 | WHERE |
||
2469 | lang = '%s' |
||
2470 | %s |
||
2471 | ORDER BY |
||
2472 | created ASC", |
||
2473 | PMF_Db::getTablePrefix(), |
||
2474 | $this->_config->getLanguage()->getLanguage(), |
||
2475 | ($all == false ? " AND is_visible = 'Y'" : '') |
||
2476 | ); |
||
2477 | |||
2478 | View Code Duplication | if ($result = $this->_config->getDb()->query($query)) { |
|
2479 | while ($row = $this->_config->getDb()->fetchObject($result)) { |
||
2480 | $questions[] = array( |
||
2481 | 'id' => $row->id, |
||
2482 | 'lang' => $row->lang, |
||
2483 | 'username' => $row->username, |
||
2484 | 'email' => $row->email, |
||
2485 | 'category_id' => $row->category_id, |
||
2486 | 'question' => $row->question, |
||
2487 | 'created' => $row->created, |
||
2488 | 'answer_id' => $row->answer_id, |
||
2489 | 'is_visible' => $row->is_visible, |
||
2490 | ); |
||
2491 | } |
||
2492 | } |
||
2493 | |||
2494 | return $questions; |
||
2495 | } |
||
2496 | |||
2497 | /** |
||
2498 | * Updates an existing voting record. |
||
2499 | * |
||
2500 | * @param array $votingData |
||
2501 | * |
||
2502 | * @return bool |
||
2503 | */ |
||
2504 | public function updateVoting($votingData) |
||
2529 | |||
2530 | /** |
||
2531 | * Adds a new changelog entry in the table faqchanges. |
||
2532 | * |
||
2533 | * @param int $id |
||
2534 | * @param int $userId |
||
2535 | * @param string $text |
||
2536 | * @param string $lang |
||
2537 | * @param int $revision_id |
||
2538 | * |
||
2539 | * @return bool |
||
2540 | */ |
||
2541 | public function createChangeEntry($id, $userId, $text, $lang, $revision_id = 0) |
||
2570 | |||
2571 | /** |
||
2572 | * Returns the changelog of a FAQ record. |
||
2573 | * |
||
2574 | * @param int $recordId |
||
2575 | * |
||
2576 | * @return array |
||
2577 | */ |
||
2578 | View Code Duplication | public function getChangeEntries($recordId) |
|
2579 | { |
||
2580 | $entries = []; |
||
2581 | |||
2582 | $query = sprintf(' |
||
2583 | SELECT |
||
2584 | DISTINCT revision_id, usr, datum, what |
||
2585 | FROM |
||
2586 | %sfaqchanges |
||
2587 | WHERE |
||
2588 | beitrag = %d |
||
2589 | ORDER BY revision_id DESC', |
||
2590 | PMF_Db::getTablePrefix(), |
||
2591 | $recordId |
||
2592 | ); |
||
2593 | |||
2594 | if ($result = $this->_config->getDb()->query($query)) { |
||
2595 | while ($row = $this->_config->getDb()->fetchObject($result)) { |
||
2596 | $entries[] = array( |
||
2597 | 'revision_id' => $row->revision_id, |
||
2598 | 'user' => $row->usr, |
||
2599 | 'date' => $row->datum, |
||
2600 | 'changelog' => $row->what, ); |
||
2601 | } |
||
2602 | } |
||
2603 | |||
2604 | return $entries; |
||
2605 | } |
||
2606 | |||
2607 | /** |
||
2608 | * Retrieve faq records according to the constraints provided. |
||
2609 | * |
||
2610 | * @param string $queryType |
||
2611 | * @param int $nCatid |
||
2612 | * @param bool $bDownwards |
||
2613 | * @param string $lang |
||
2614 | * @param string $date |
||
2615 | * |
||
2616 | * @return array |
||
2617 | */ |
||
2618 | public function get($queryType = FAQ_QUERY_TYPE_DEFAULT, $nCatid = 0, $bDownwards = true, $lang = '', $date = '') |
||
2652 | |||
2653 | /** |
||
2654 | * Build a logic sequence, for a WHERE statement, of those category IDs |
||
2655 | * children of the provided category ID, if any. |
||
2656 | * |
||
2657 | * @param $nCatid |
||
2658 | * @param $logicOp |
||
2659 | * @param $oCat |
||
2660 | * |
||
2661 | * @return string |
||
2662 | */ |
||
2663 | public function _getCatidWhereSequence($nCatid, $logicOp = 'OR', $oCat = null) |
||
2679 | |||
2680 | /** |
||
2681 | * Build the SQL query for retrieving faq records according to the constraints provided. |
||
2682 | * |
||
2683 | * @param $QueryType |
||
2684 | * @param $nCatid |
||
2685 | * @param $bDownwards |
||
2686 | * @param $lang |
||
2687 | * @param $date |
||
2688 | * @param $faqid |
||
2689 | * |
||
2690 | * @return array |
||
2691 | */ |
||
2692 | private function _getSQLQuery($QueryType, $nCatid, $bDownwards, $lang, $date, $faqid = 0) |
||
2693 | { |
||
2694 | $now = date('YmdHis'); |
||
2695 | $query = sprintf(" |
||
2696 | SELECT |
||
2697 | fd.id AS id, |
||
2698 | fd.solution_id AS solution_id, |
||
2699 | fd.revision_id AS revision_id, |
||
2700 | fd.lang AS lang, |
||
2701 | fcr.category_id AS category_id, |
||
2702 | fd.active AS active, |
||
2703 | fd.sticky AS sticky, |
||
2704 | fd.keywords AS keywords, |
||
2705 | fd.thema AS thema, |
||
2706 | fd.content AS content, |
||
2707 | fd.author AS author, |
||
2708 | fd.email AS email, |
||
2709 | fd.comment AS comment, |
||
2710 | fd.updated AS updated, |
||
2711 | fd.notes AS notes, |
||
2712 | fv.visits AS visits, |
||
2713 | fv.last_visit AS last_visit |
||
2714 | FROM |
||
2715 | %sfaqdata fd, |
||
2716 | %sfaqvisits fv, |
||
2717 | %sfaqcategoryrelations fcr |
||
2718 | WHERE |
||
2719 | fd.id = fcr.record_id |
||
2720 | AND |
||
2721 | fd.lang = fcr.record_lang |
||
2722 | AND |
||
2723 | fd.date_start <= '%s' |
||
2724 | AND |
||
2725 | fd.date_end >= '%s' |
||
2726 | AND ", |
||
2727 | PMF_Db::getTablePrefix(), |
||
2728 | PMF_Db::getTablePrefix(), |
||
2729 | PMF_Db::getTablePrefix(), |
||
2730 | $now, |
||
2731 | $now); |
||
2732 | // faqvisits data selection |
||
2733 | if (!empty($faqid)) { |
||
2734 | // Select ONLY the faq with the provided $faqid |
||
2735 | $query .= "fd.id = '".$faqid."' AND "; |
||
2736 | } |
||
2737 | $query .= 'fd.id = fv.id |
||
2738 | AND |
||
2739 | fd.lang = fv.lang'; |
||
2740 | $needAndOp = true; |
||
2741 | if ((!empty($nCatid)) && is_int($nCatid) && $nCatid > 0) { |
||
2742 | if ($needAndOp) { |
||
2743 | $query .= ' AND'; |
||
2744 | } |
||
2745 | $query .= ' (fcr.category_id = '.$nCatid; |
||
2746 | if ($bDownwards) { |
||
2747 | $query .= $this->_getCatidWhereSequence($nCatid, 'OR'); |
||
2748 | } |
||
2749 | $query .= ')'; |
||
2750 | $needAndOp = true; |
||
2751 | } |
||
2752 | View Code Duplication | if ((!empty($date)) && PMF_Utils::isLikeOnPMFDate($date)) { |
|
2753 | if ($needAndOp) { |
||
2754 | $query .= ' AND'; |
||
2755 | } |
||
2756 | $query .= " fd.updated LIKE '".$date."'"; |
||
2757 | $needAndOp = true; |
||
2758 | } |
||
2759 | View Code Duplication | if ((!empty($lang)) && PMF_Utils::isLanguage($lang)) { |
|
2760 | if ($needAndOp) { |
||
2761 | $query .= ' AND'; |
||
2762 | } |
||
2763 | $query .= " fd.lang = '".$lang."'"; |
||
2764 | $needAndOp = true; |
||
2765 | } |
||
2766 | switch ($QueryType) { |
||
2767 | case FAQ_QUERY_TYPE_APPROVAL: |
||
2768 | if ($needAndOp) { |
||
2769 | $query .= ' AND'; |
||
2770 | } |
||
2771 | $query .= " fd.active = '".FAQ_SQL_ACTIVE_NO."'"; |
||
2772 | break; |
||
2773 | case FAQ_QUERY_TYPE_EXPORT_PDF: |
||
2774 | case FAQ_QUERY_TYPE_EXPORT_XHTML: |
||
2775 | case FAQ_QUERY_TYPE_EXPORT_XML: |
||
2776 | if ($needAndOp) { |
||
2777 | $query .= ' AND'; |
||
2778 | } |
||
2779 | $query .= " fd.active = '".FAQ_SQL_ACTIVE_YES."'"; |
||
2780 | break; |
||
2781 | default: |
||
2782 | if ($needAndOp) { |
||
2783 | $query .= ' AND'; |
||
2784 | } |
||
2785 | $query .= " fd.active = '".FAQ_SQL_ACTIVE_YES."'"; |
||
2786 | break; |
||
2787 | } |
||
2788 | // Sort criteria |
||
2789 | switch ($QueryType) { |
||
2790 | case FAQ_QUERY_TYPE_EXPORT_PDF: |
||
2791 | case FAQ_QUERY_TYPE_EXPORT_XHTML: |
||
2792 | case FAQ_QUERY_TYPE_EXPORT_XML: |
||
2793 | $query .= "\nORDER BY fcr.category_id, fd.id"; |
||
2794 | break; |
||
2795 | case FAQ_QUERY_TYPE_RSS_LATEST: |
||
2796 | $query .= "\nORDER BY fd.updated DESC"; |
||
2797 | break; |
||
2798 | default: |
||
2799 | // Normal ordering |
||
2800 | $query .= "\nORDER BY fcr.category_id, fd.id"; |
||
2801 | break; |
||
2802 | } |
||
2803 | |||
2804 | return $query; |
||
2805 | } |
||
2806 | |||
2807 | /** |
||
2808 | * Adds the record permissions for users and groups. |
||
2809 | * |
||
2810 | * @param string $mode 'group' or 'user' |
||
2811 | * @param int $recordId ID of the current record |
||
2812 | * @param array $ids Array of group or user IDs |
||
2813 | * |
||
2814 | * @return bool |
||
2815 | */ |
||
2816 | public function addPermission($mode, $recordId, $ids) |
||
2841 | |||
2842 | /** |
||
2843 | * Deletes the record permissions for users and groups. |
||
2844 | * |
||
2845 | * @param string $mode 'group' or 'user' |
||
2846 | * @param int $record_id ID of the current record |
||
2847 | * |
||
2848 | * @return bool |
||
2849 | * |
||
2850 | * @author Thorsten Rinne <[email protected]> |
||
2851 | */ |
||
2852 | View Code Duplication | public function deletePermission($mode, $record_id) |
|
2873 | |||
2874 | /** |
||
2875 | * Returns the record permissions for users and groups. |
||
2876 | * |
||
2877 | * @param string $mode 'group' or 'user' |
||
2878 | * @param int $recordId |
||
2879 | * |
||
2880 | * @return array |
||
2881 | */ |
||
2882 | public function getPermission($mode, $recordId) |
||
2883 | { |
||
2884 | $permissions = []; |
||
2885 | |||
2886 | if (!($mode == 'user' || $mode == 'group')) { |
||
2887 | return false; |
||
2888 | } |
||
2889 | |||
2890 | $query = sprintf(' |
||
2891 | SELECT |
||
2892 | %s_id AS permission |
||
2893 | FROM |
||
2894 | %sfaqdata_%s |
||
2895 | WHERE |
||
2896 | record_id = %d', |
||
2897 | $mode, |
||
2898 | PMF_Db::getTablePrefix(), |
||
2899 | $mode, |
||
2900 | (int) $recordId); |
||
2901 | |||
2902 | $result = $this->_config->getDb()->query($query); |
||
2903 | |||
2904 | if ($this->_config->getDb()->numRows($result) > 0) { |
||
2905 | while (($row = $this->_config->getDb()->fetchObject($result))) { |
||
2906 | $permissions[] = (int) $row->permission; |
||
2907 | } |
||
2908 | } |
||
2909 | |||
2910 | return $permissions; |
||
2911 | } |
||
2912 | |||
2913 | /** |
||
2914 | * Returns all records of one category. |
||
2915 | * |
||
2916 | * @param int $category |
||
2917 | * |
||
2918 | * @return string |
||
2919 | */ |
||
2920 | public function showAllRecordsWoPaging($category) |
||
3010 | |||
3011 | /** |
||
3012 | * Prints the open questions as a XHTML table. |
||
3013 | * |
||
3014 | * @return string |
||
3015 | */ |
||
3016 | public function printOpenQuestions($user) |
||
3017 | { |
||
3018 | global $sids, $category; |
||
3116 | |||
3117 | /** |
||
3118 | * Set or unset a faq item flag. |
||
3119 | * |
||
3120 | * @param int $id Record id |
||
3121 | * @param string $lang language code which is valid with Language::isASupportedLanguage |
||
3122 | * @param bool $flag weither or not the record is set to sticky |
||
3123 | * @param string $type type of the flag to set, use the column name |
||
3124 | * |
||
3125 | * @return bool |
||
3126 | */ |
||
3127 | public function updateRecordFlag($id, $lang, $flag, $type) |
||
3168 | |||
3169 | /** |
||
3170 | * Returns the sticky records with URL and Title. |
||
3171 | * |
||
3172 | * @return array |
||
3173 | */ |
||
3174 | private function getStickyRecordsData() |
||
3252 | |||
3253 | /** |
||
3254 | * Prepares and returns the sticky records for the frontend. |
||
3255 | * |
||
3256 | * @return array |
||
3257 | */ |
||
3258 | public function getStickyRecords() |
||
3289 | |||
3290 | /** |
||
3291 | * Updates field answer_id in faqquestion. |
||
3292 | * |
||
3293 | * @param int $openQuestionId |
||
3294 | * @param int $faqId |
||
3295 | * @param int $categoryId |
||
3296 | * |
||
3297 | * @return bool |
||
3298 | */ |
||
3299 | View Code Duplication | public function updateQuestionAnswer($openQuestionId, $faqId, $categoryId) |
|
3311 | |||
3312 | /** |
||
3313 | * Returns a part of a query to check permissions. |
||
3314 | * |
||
3315 | * @param bool $hasGroupSupport |
||
3316 | * |
||
3317 | * @return string |
||
3318 | */ |
||
3319 | protected function queryPermission($hasGroupSupport = false) |
||
3350 | } |
||
3351 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.