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) |
||
155 | |||
156 | /** |
||
157 | * @param array $groups |
||
158 | */ |
||
159 | public function setGroups(Array $groups) |
||
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 $record_ids Array of record ids |
||
495 | * @param string $orderby Order by |
||
496 | * @param string $sortby Sort by |
||
497 | * |
||
498 | * @return string |
||
499 | */ |
||
500 | public function showAllRecordsByIds(Array $record_ids, $orderby = 'fd.id', $sortby = 'ASC') |
||
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 | public function deleteCategoryRelations($record_id, $record_lang) |
||
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) |
||
1337 | |||
1338 | /** |
||
1339 | * Returns the solution ID from a given ID and language |
||
1340 | * |
||
1341 | * @param integer $faqId |
||
1342 | * @param string $faqLang |
||
1343 | * |
||
1344 | * @return int |
||
1345 | */ |
||
1346 | public function getSolutionIdFromId($faqId, $faqLang) |
||
1370 | |||
1371 | /** |
||
1372 | * Gets the latest solution id for a FAQ record. |
||
1373 | * |
||
1374 | * @return int |
||
1375 | */ |
||
1376 | public function getSolutionId() |
||
1402 | |||
1403 | /** |
||
1404 | * Returns an array with all data from all FAQ records. |
||
1405 | * |
||
1406 | * @param int $sortType Sorting type |
||
1407 | * @param array $condition Condition |
||
1408 | * @param string $sortOrder Sorting order |
||
1409 | */ |
||
1410 | public function getAllRecords($sortType = FAQ_SORTING_TYPE_CATID_FAQID, Array $condition = null, $sortOrder = 'ASC') |
||
1411 | { |
||
1412 | $where = ''; |
||
1413 | if (!is_null($condition)) { |
||
1414 | $num = count($condition); |
||
1415 | $where = 'WHERE '; |
||
1416 | foreach ($condition as $field => $data) { |
||
1417 | --$num; |
||
1418 | $where .= $field; |
||
1419 | if (is_array($data)) { |
||
1420 | $where .= ' IN ('; |
||
1421 | $separator = ''; |
||
1422 | foreach ($data as $value) { |
||
1423 | $where .= $separator."'".$this->_config->getDb()->escape($value)."'"; |
||
1424 | $separator = ', '; |
||
1425 | } |
||
1426 | $where .= ')'; |
||
1427 | } else { |
||
1428 | $where .= " = '".$this->_config->getDb()->escape($data)."'"; |
||
1429 | } |
||
1430 | if ($num > 0) { |
||
1431 | $where .= ' AND '; |
||
1432 | } |
||
1433 | } |
||
1434 | } |
||
1435 | |||
1436 | switch ($sortType) { |
||
1437 | |||
1438 | case FAQ_SORTING_TYPE_CATID_FAQID: |
||
1439 | $orderBy = sprintf(' |
||
1440 | ORDER BY |
||
1441 | fcr.category_id, |
||
1442 | fd.id %s', |
||
1443 | $sortOrder); |
||
1444 | break; |
||
1445 | |||
1446 | case FAQ_SORTING_TYPE_FAQID: |
||
1447 | $orderBy = sprintf(' |
||
1448 | ORDER BY |
||
1449 | fd.id %s', |
||
1450 | $sortOrder); |
||
1451 | break; |
||
1452 | |||
1453 | case FAQ_SORTING_TYPE_FAQTITLE_FAQID: |
||
1454 | $orderBy = sprintf(' |
||
1455 | ORDER BY |
||
1456 | fcr.category_id, |
||
1457 | fd.thema %s', |
||
1458 | $sortOrder); |
||
1459 | break; |
||
1460 | |||
1461 | case FAQ_SORTING_TYPE_DATE_FAQID: |
||
1462 | $orderBy = sprintf(' |
||
1463 | ORDER BY |
||
1464 | fcr.category_id, |
||
1465 | fd.updated %s', |
||
1466 | $sortOrder); |
||
1467 | break; |
||
1468 | |||
1469 | default: |
||
1470 | $orderBy = ''; |
||
1471 | break; |
||
1472 | } |
||
1473 | |||
1474 | $query = sprintf(' |
||
1475 | SELECT |
||
1476 | fd.id AS id, |
||
1477 | fd.lang AS lang, |
||
1478 | fcr.category_id AS category_id, |
||
1479 | fd.solution_id AS solution_id, |
||
1480 | fd.revision_id AS revision_id, |
||
1481 | fd.active AS active, |
||
1482 | fd.sticky AS sticky, |
||
1483 | fd.keywords AS keywords, |
||
1484 | fd.thema AS thema, |
||
1485 | fd.content AS content, |
||
1486 | fd.author AS author, |
||
1487 | fd.email AS email, |
||
1488 | fd.comment AS comment, |
||
1489 | fd.updated AS updated, |
||
1490 | fd.links_state AS links_state, |
||
1491 | fd.links_check_date AS links_check_date, |
||
1492 | fd.date_start AS date_start, |
||
1493 | fd.date_end AS date_end, |
||
1494 | fd.sticky AS sticky, |
||
1495 | fd.created AS created, |
||
1496 | fd.notes AS notes |
||
1497 | FROM |
||
1498 | %sfaqdata fd |
||
1499 | LEFT JOIN |
||
1500 | %sfaqcategoryrelations fcr |
||
1501 | ON |
||
1502 | fd.id = fcr.record_id |
||
1503 | AND |
||
1504 | fd.lang = fcr.record_lang |
||
1505 | %s |
||
1506 | %s', |
||
1507 | PMF_Db::getTablePrefix(), |
||
1508 | PMF_Db::getTablePrefix(), |
||
1509 | $where, |
||
1510 | $orderBy |
||
1511 | ); |
||
1512 | |||
1513 | $result = $this->_config->getDb()->query($query); |
||
1514 | |||
1515 | while ($row = $this->_config->getDb()->fetchObject($result)) { |
||
1516 | $content = $row->content; |
||
1517 | $active = ('yes' == $row->active); |
||
1518 | $expired = (date('YmdHis') > $row->date_end); |
||
1519 | |||
1520 | if (!$active) { |
||
1521 | $content = $this->pmf_lang['err_inactiveArticle']; |
||
1522 | } |
||
1523 | if ($expired) { |
||
1524 | $content = $this->pmf_lang['err_expiredArticle']; |
||
1525 | } |
||
1526 | |||
1527 | $this->faqRecords[] = [ |
||
1528 | 'id' => $row->id, |
||
1529 | 'category_id' => $row->category_id, |
||
1530 | 'lang' => $row->lang, |
||
1531 | 'solution_id' => $row->solution_id, |
||
1532 | 'revision_id' => $row->revision_id, |
||
1533 | 'active' => $row->active, |
||
1534 | 'sticky' => $row->sticky, |
||
1535 | 'keywords' => $row->keywords, |
||
1536 | 'title' => $row->thema, |
||
1537 | 'content' => $content, |
||
1538 | 'author' => $row->author, |
||
1539 | 'email' => $row->email, |
||
1540 | 'comment' => $row->comment, |
||
1541 | 'updated' => PMF_Date::createIsoDate($row->updated, 'Y-m-d H:i:s'), |
||
1542 | 'dateStart' => $row->date_start, |
||
1543 | 'dateEnd' => $row->date_end, |
||
1544 | 'created' => $row->created, |
||
1545 | 'notes' => $row->notes |
||
1546 | ]; |
||
1547 | } |
||
1548 | } |
||
1549 | |||
1550 | /** |
||
1551 | * Returns the FAQ record title from the ID and language. |
||
1552 | * |
||
1553 | * @param int $id Record id |
||
1554 | * |
||
1555 | * @return string |
||
1556 | */ |
||
1557 | public function getRecordTitle($id) |
||
1558 | { |
||
1559 | if (isset($this->faqRecord['id']) && ($this->faqRecord['id'] == $id)) { |
||
1560 | return $this->faqRecord['title']; |
||
1561 | } |
||
1562 | |||
1563 | $question = ''; |
||
1564 | |||
1565 | $query = sprintf( |
||
1566 | "SELECT |
||
1567 | thema AS question |
||
1568 | FROM |
||
1569 | %sfaqdata |
||
1570 | WHERE |
||
1571 | id = %d AND lang = '%s'", |
||
1572 | PMF_Db::getTablePrefix(), |
||
1573 | $id, |
||
1574 | $this->_config->getLanguage()->getLanguage() |
||
1575 | ); |
||
1576 | $result = $this->_config->getDb()->query($query); |
||
1577 | |||
1578 | View Code Duplication | if ($this->_config->getDb()->numRows($result) > 0) { |
|
1579 | while ($row = $this->_config->getDb()->fetchObject($result)) { |
||
1580 | $question = $row->question; |
||
1581 | } |
||
1582 | } else { |
||
1583 | $question = $this->pmf_lang['no_cats']; |
||
1584 | } |
||
1585 | |||
1586 | return $question; |
||
1587 | } |
||
1588 | |||
1589 | /** |
||
1590 | * Gets all revisions from a given record ID. |
||
1591 | * |
||
1592 | * @param int $recordId Record id |
||
1593 | * @param string $recordLang Record language |
||
1594 | * |
||
1595 | * @return array |
||
1596 | */ |
||
1597 | public function getRevisionIds($recordId, $recordLang) |
||
1631 | |||
1632 | /** |
||
1633 | * Adds a new revision from a given record ID. |
||
1634 | * |
||
1635 | * @param int $record_id Record id |
||
1636 | * @param string $record_lang Record language |
||
1637 | * |
||
1638 | * @return array |
||
1639 | */ |
||
1640 | public function addNewRevision($record_id, $record_lang) |
||
1659 | |||
1660 | /** |
||
1661 | * Returns the keywords of a FAQ record from the ID and language. |
||
1662 | * |
||
1663 | * @param int $id record id |
||
1664 | * |
||
1665 | * @return string |
||
1666 | */ |
||
1667 | public function getRecordKeywords($id) |
||
1693 | |||
1694 | /** |
||
1695 | * Returns a answer preview of the FAQ record. |
||
1696 | * |
||
1697 | * @param int $recordId FAQ record ID |
||
1698 | * @param int $wordCount Number of words, default: 12 |
||
1699 | * |
||
1700 | * @return string |
||
1701 | */ |
||
1702 | public function getRecordPreview($recordId, $wordCount = 12) |
||
1734 | |||
1735 | /** |
||
1736 | * Returns the number of activated and not expired records, optionally |
||
1737 | * not limited to the current language. |
||
1738 | * |
||
1739 | * @param string $language Language |
||
1740 | * |
||
1741 | * @return int |
||
1742 | */ |
||
1743 | public function getNumberOfRecords($language = null) |
||
1744 | { |
||
1745 | $now = date('YmdHis'); |
||
1746 | |||
1747 | $query = sprintf(" |
||
1748 | SELECT |
||
1749 | id |
||
1750 | FROM |
||
1751 | %sfaqdata |
||
1752 | WHERE |
||
1753 | active = 'yes' |
||
1754 | %s |
||
1755 | AND |
||
1756 | date_start <= '%s' |
||
1757 | AND |
||
1758 | date_end >= '%s'", |
||
1759 | PMF_Db::getTablePrefix(), |
||
1760 | null == $language ? '' : "AND lang = '".$language."'", |
||
1761 | $now, |
||
1762 | $now |
||
1763 | ); |
||
1764 | |||
1765 | $num = $this->_config->getDb()->numRows($this->_config->getDb()->query($query)); |
||
1766 | |||
1767 | if ($num > 0) { |
||
1768 | return $num; |
||
1769 | } else { |
||
1770 | return 0; |
||
1771 | } |
||
1772 | } |
||
1773 | |||
1774 | /** |
||
1775 | * This function generates a list with the most voted or most visited records. |
||
1776 | * |
||
1777 | * @param string $type Type definition visits/voted |
||
1778 | * |
||
1779 | * @since 2009-11-03 |
||
1780 | * |
||
1781 | * @author Max Köhler <[email protected]> |
||
1782 | * |
||
1783 | * @return array |
||
1784 | */ |
||
1785 | public function getTopTen($type = 'visits') |
||
1819 | |||
1820 | /** |
||
1821 | * This function generates the list with the latest published records. |
||
1822 | * |
||
1823 | * @return array |
||
1824 | */ |
||
1825 | public function getLatest() |
||
1844 | |||
1845 | /** |
||
1846 | * Deletes a question for the table faqquestions. |
||
1847 | * |
||
1848 | * @param int $questionId |
||
1849 | * |
||
1850 | * @return bool |
||
1851 | */ |
||
1852 | public function deleteQuestion($questionId) |
||
1870 | |||
1871 | /** |
||
1872 | * Returns the visibility of a question. |
||
1873 | * |
||
1874 | * @param int $questionId |
||
1875 | * |
||
1876 | * @return string |
||
1877 | */ |
||
1878 | public function getVisibilityOfQuestion($questionId) |
||
1903 | |||
1904 | /** |
||
1905 | * Sets the visibility of a question. |
||
1906 | * |
||
1907 | * @param int $questionId |
||
1908 | * @param string $isVisible |
||
1909 | * |
||
1910 | * @return bool |
||
1911 | */ |
||
1912 | public function setVisibilityOfQuestion($questionId, $isVisible) |
||
1933 | |||
1934 | /** |
||
1935 | * This function generates a data-set with the most voted FAQs. |
||
1936 | * |
||
1937 | * @param int $count Number of records |
||
1938 | * @param string $language Language |
||
1939 | * |
||
1940 | * @return array |
||
1941 | */ |
||
1942 | public function getTopVotedData($count = PMF_NUMBER_RECORDS_TOPTEN, $language = null) |
||
2029 | |||
2030 | /** |
||
2031 | * This function generates the Top Ten data with the mosted viewed records. |
||
2032 | * |
||
2033 | * @param int $count Number of records |
||
2034 | * @param int $categoryId Category ID |
||
2035 | * @param string $language Language |
||
2036 | * |
||
2037 | * @return array |
||
2038 | */ |
||
2039 | public function getTopTenData($count = PMF_NUMBER_RECORDS_TOPTEN, $categoryId = 0, $language = null) |
||
2140 | |||
2141 | /** |
||
2142 | * This function generates an array with a specified number of most recent |
||
2143 | * published records. |
||
2144 | * |
||
2145 | * @param int $count Number of records |
||
2146 | * @param string $language Language |
||
2147 | * |
||
2148 | * @return array |
||
2149 | */ |
||
2150 | public function getLatestData($count = PMF_NUMBER_RECORDS_LATEST, $language = null) |
||
2245 | |||
2246 | /** |
||
2247 | * Reload locking for user votings. |
||
2248 | * |
||
2249 | * @param int $id FAQ record id |
||
2250 | * @param string $ip IP |
||
2251 | * |
||
2252 | * @return bool |
||
2253 | */ |
||
2254 | public function votingCheck($id, $ip) |
||
2274 | |||
2275 | /** |
||
2276 | * Returns the number of users from the table faqvotings. |
||
2277 | * |
||
2278 | * @param integer $record_id |
||
2279 | * |
||
2280 | * @return integer |
||
2281 | */ |
||
2282 | View Code Duplication | public function getNumberOfVotings($record_id) |
|
2301 | |||
2302 | /** |
||
2303 | * Adds a new voting record. |
||
2304 | * |
||
2305 | * @param array $votingData |
||
2306 | * |
||
2307 | * @return bool |
||
2308 | */ |
||
2309 | public function addVoting($votingData) |
||
2330 | |||
2331 | /** |
||
2332 | * Adds a new question. |
||
2333 | * |
||
2334 | * @param array $questionData |
||
2335 | * |
||
2336 | * @return bool |
||
2337 | */ |
||
2338 | public function addQuestion(Array $questionData) |
||
2360 | |||
2361 | /** |
||
2362 | * Returns a new question. |
||
2363 | * |
||
2364 | * @param int $questionId |
||
2365 | * |
||
2366 | * @return array |
||
2367 | */ |
||
2368 | public function getQuestion($questionId) |
||
2417 | |||
2418 | /** |
||
2419 | * Returns all open questions. |
||
2420 | * |
||
2421 | * @param $all boolean If true, then return visible and unvisble questions; otherwise only visible ones |
||
2422 | * |
||
2423 | * @return array |
||
2424 | */ |
||
2425 | public function getAllOpenQuestions($all = true) |
||
2462 | |||
2463 | /** |
||
2464 | * Updates an existing voting record. |
||
2465 | * |
||
2466 | * @param array $votingData |
||
2467 | * |
||
2468 | * @return bool |
||
2469 | */ |
||
2470 | public function updateVoting($votingData) |
||
2495 | |||
2496 | /** |
||
2497 | * Adds a new changelog entry in the table faqchanges. |
||
2498 | * |
||
2499 | * @param int $id |
||
2500 | * @param int $userId |
||
2501 | * @param string $text |
||
2502 | * @param string $lang |
||
2503 | * @param int $revision_id |
||
2504 | * |
||
2505 | * @return bool |
||
2506 | */ |
||
2507 | public function createChangeEntry($id, $userId, $text, $lang, $revision_id = 0) |
||
2536 | |||
2537 | /** |
||
2538 | * Returns the changelog of a FAQ record. |
||
2539 | * |
||
2540 | * @param int $record_id |
||
2541 | * |
||
2542 | * @return array |
||
2543 | */ |
||
2544 | View Code Duplication | public function getChangeEntries($record_id) |
|
2572 | |||
2573 | /** |
||
2574 | * Retrieve faq records according to the constraints provided. |
||
2575 | * |
||
2576 | * @param string $queryType |
||
2577 | * @param int $nCatid |
||
2578 | * @param bool $bDownwards |
||
2579 | * @param string $lang |
||
2580 | * @param string $date |
||
2581 | * |
||
2582 | * @return array |
||
2583 | */ |
||
2584 | public function get($queryType = FAQ_QUERY_TYPE_DEFAULT, $nCatid = 0, $bDownwards = true, $lang = '', $date = '') |
||
2618 | |||
2619 | /** |
||
2620 | * Build a logic sequence, for a WHERE statement, of those category IDs |
||
2621 | * children of the provided category ID, if any. |
||
2622 | * |
||
2623 | * @param $nCatid |
||
2624 | * @param $logicOp |
||
2625 | * @param $oCat |
||
2626 | * |
||
2627 | * @return string |
||
2628 | */ |
||
2629 | public function _getCatidWhereSequence($nCatid, $logicOp = 'OR', $oCat = null) |
||
2645 | |||
2646 | /** |
||
2647 | * Build the SQL query for retrieving faq records according to the constraints provided. |
||
2648 | * |
||
2649 | * @param $QueryType |
||
2650 | * @param $nCatid |
||
2651 | * @param $bDownwards |
||
2652 | * @param $lang |
||
2653 | * @param $date |
||
2654 | * @param $faqid |
||
2655 | * |
||
2656 | * @return array |
||
2657 | */ |
||
2658 | private function _getSQLQuery($QueryType, $nCatid, $bDownwards, $lang, $date, $faqid = 0) |
||
2775 | |||
2776 | /** |
||
2777 | * Adds the record permissions for users and groups. |
||
2778 | * |
||
2779 | * @param string $mode 'group' or 'user' |
||
2780 | * @param int $recordId ID of the current record |
||
2781 | * @param array $ids Array of group or user IDs |
||
2782 | * |
||
2783 | * @return bool |
||
2784 | */ |
||
2785 | public function addPermission($mode, $recordId, $ids) |
||
2810 | |||
2811 | /** |
||
2812 | * Deletes the record permissions for users and groups. |
||
2813 | * |
||
2814 | * @param string $mode 'group' or 'user' |
||
2815 | * @param int $record_id ID of the current record |
||
2816 | * |
||
2817 | * @return bool |
||
2818 | * |
||
2819 | * @author Thorsten Rinne <[email protected]> |
||
2820 | */ |
||
2821 | View Code Duplication | public function deletePermission($mode, $record_id) |
|
2842 | |||
2843 | /** |
||
2844 | * Returns the record permissions for users and groups. |
||
2845 | * |
||
2846 | * @param string $mode 'group' or 'user' |
||
2847 | * @param int $recordId |
||
2848 | * |
||
2849 | * @return array |
||
2850 | */ |
||
2851 | public function getPermission($mode, $recordId) |
||
2881 | |||
2882 | /** |
||
2883 | * Returns all records of one category. |
||
2884 | * |
||
2885 | * @param int $category |
||
2886 | * |
||
2887 | * @return string |
||
2888 | */ |
||
2889 | public function showAllRecordsWoPaging($category) |
||
2967 | |||
2968 | /** |
||
2969 | * Prints the open questions as a XHTML table. |
||
2970 | * |
||
2971 | * @return string |
||
2972 | */ |
||
2973 | public function printOpenQuestions() |
||
3067 | |||
3068 | /** |
||
3069 | * Set or unset a faq item flag. |
||
3070 | * |
||
3071 | * @param int $id Record id |
||
3072 | * @param string $lang language code which is valid with Language::isASupportedLanguage |
||
3073 | * @param bool $flag weither or not the record is set to sticky |
||
3074 | * @param string $type type of the flag to set, use the column name |
||
3075 | * |
||
3076 | * @return bool |
||
3077 | */ |
||
3078 | public function updateRecordFlag($id, $lang, $flag, $type) |
||
3119 | |||
3120 | /** |
||
3121 | * Returns the sticky records with URL and Title. |
||
3122 | * |
||
3123 | * @return array |
||
3124 | */ |
||
3125 | private function getStickyRecordsData() |
||
3203 | |||
3204 | /** |
||
3205 | * Prepares and returns the sticky records for the frontend. |
||
3206 | * |
||
3207 | * @return array |
||
3208 | */ |
||
3209 | public function getStickyRecords() |
||
3240 | |||
3241 | /** |
||
3242 | * Updates field answer_id in faqquestion. |
||
3243 | * |
||
3244 | * @param int $openQuestionId |
||
3245 | * @param int $faqId |
||
3246 | * @param int $categoryId |
||
3247 | * |
||
3248 | * @return bool |
||
3249 | */ |
||
3250 | View Code Duplication | public function updateQuestionAnswer($openQuestionId, $faqId, $categoryId) |
|
3262 | |||
3263 | /** |
||
3264 | * Returns a part of a query to check permissions. |
||
3265 | * |
||
3266 | * @param bool $hasGroupSupport |
||
3267 | * |
||
3268 | * @return string |
||
3269 | */ |
||
3270 | protected function queryPermission($hasGroupSupport = false) |
||
3301 | } |
||
3302 |
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.