GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — 2.9 ( 10510f...e3ac59 )
by Thorsten
13:06
created

PMF_Faq::getTopTenData()   D

Complexity

Conditions 13
Paths 24

Size

Total Lines 103
Code Lines 51

Duplication

Lines 43
Ratio 41.75 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
cc 13
eloc 51
c 2
b 2
f 0
nc 24
nop 3
dl 43
loc 103
rs 4.9922

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * The main FAQ class.
5
 *
6
 * PHP Version 5.5
7
 *
8
 * This Source Code Form is subject to the terms of the Mozilla Public License,
9
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
10
 * obtain one at http://mozilla.org/MPL/2.0/.
11
 *
12
 * @category  phpMyFAQ
13
 * @author    Thorsten Rinne <[email protected]>
14
 * @author    Matteo Scaramuccia <[email protected]>
15
 * @author    Georgi Korchev <[email protected]>
16
 * @author    Adrianna Musiol <[email protected]>
17
 * @author    Peter Caesar <[email protected]>
18
 * @copyright 2005-2016 phpMyFAQ Team
19
 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
20
 * @link      http://www.phpmyfaq.de
21
 * @since     2005-12-20
22
 */
23
if (!defined('IS_VALID_PHPMYFAQ')) {
24
    exit();
25
}
26
27
/*
28
 * SQL constants definitions
29
 */
30
define('FAQ_SQL_ACTIVE_YES', 'yes');
31
define('FAQ_SQL_ACTIVE_NO',  'no');
32
33
/*
34
 * Query type definitions
35
 */
36
define('FAQ_QUERY_TYPE_DEFAULT',      'faq_default');
37
define('FAQ_QUERY_TYPE_APPROVAL',     'faq_approval');
38
define('FAQ_QUERY_TYPE_EXPORT_PDF',   'faq_export_pdf');
39
define('FAQ_QUERY_TYPE_EXPORT_XHTML', 'faq_export_xhtml');
40
define('FAQ_QUERY_TYPE_EXPORT_XML',   'faq_export_xml');
41
define('FAQ_QUERY_TYPE_RSS_LATEST',   'faq_rss_latest');
42
43
/*
44
 * Sorting type definitions
45
 */
46
define('FAQ_SORTING_TYPE_NONE', 0);
47
define('FAQ_SORTING_TYPE_CATID_FAQID', 1);
48
define('FAQ_SORTING_TYPE_FAQTITLE_FAQID', 2);
49
define('FAQ_SORTING_TYPE_DATE_FAQID', 3);
50
define('FAQ_SORTING_TYPE_FAQID', 4);
51
52
/**
53
 * The main FAQ class - 3K LOC of funny things for phpMyFAQ.
54
 *
55
 * @category  phpMyFAQ
56
 * @author    Thorsten Rinne <[email protected]>
57
 * @author    Matteo Scaramuccia <[email protected]>
58
 * @author    Georgi Korchev <[email protected]>
59
 * @author    Adrianna Musiol <[email protected]>
60
 * @author    Peter Caesar <[email protected]>
61
 * @copyright 2005-2016 phpMyFAQ Team
62
 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
63
 * @link      http://www.phpmyfaq.de
64
 * @since     2005-12-20
65
 */
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')
298
    {
299
        global $sids;
300
301
        $numPerPage = $this->_config->get('records.numberOfRecordsPerPage');
302
        $page = PMF_Filter::filterInput(INPUT_GET, 'seite', FILTER_VALIDATE_INT, 1);
303
        $output = '';
304
        $title = '';
305
306
        if ($orderby == 'visits') {
307
            $currentTable = 'fv';
308
        } else {
309
            $currentTable = 'fd';
310
        }
311
312
        // If random FAQs are activated, we don't need an order
313
        if (true === $this->_config->get('records.randomSort')) {
314
            $order = '';
315
        } else {
316
            $order = sprintf(
317
                'ORDER BY fd.sticky DESC, %s.%s %s',
318
                $currentTable,
319
                $this->_config->getDb()->escape($orderby),
320
                $this->_config->getDb()->escape($sortby)
321
            );
322
        }
323
324
        $now = date('YmdHis');
325
        $query = sprintf("
326
            SELECT
327
                fd.id AS id,
328
                fd.lang AS lang,
329
                fd.sticky AS sticky,
330
                fd.thema AS thema,
331
                fcr.category_id AS category_id,
332
                fv.visits AS visits
333
            FROM
334
                %sfaqdata AS fd
335
            LEFT JOIN
336
                %sfaqcategoryrelations AS fcr
337
            ON
338
                fd.id = fcr.record_id
339
            AND
340
                fd.lang = fcr.record_lang
341
            LEFT JOIN
342
                %sfaqvisits AS fv
343
            ON
344
                fd.id = fv.id
345
            AND
346
                fv.lang = fd.lang
347
            LEFT JOIN
348
                %sfaqdata_group AS fdg
349
            ON
350
                fd.id = fdg.record_id
351
            LEFT JOIN
352
                %sfaqdata_user AS fdu
353
            ON
354
                fd.id = fdu.record_id
355
            WHERE
356
                fd.date_start <= '%s'
357
            AND
358
                fd.date_end   >= '%s'
359
            AND
360
                fd.active = 'yes'
361
            AND
362
                fcr.category_id = %d
363
            AND
364
                fd.lang = '%s'
365
            %s
366
            %s",
367
            PMF_Db::getTablePrefix(),
368
            PMF_Db::getTablePrefix(),
369
            PMF_Db::getTablePrefix(),
370
            PMF_Db::getTablePrefix(),
371
            PMF_Db::getTablePrefix(),
372
            $now,
373
            $now,
374
            $categoryId,
375
            $this->_config->getLanguage()->getLanguage(),
376
            $this->queryPermission($this->groupSupport),
377
            $order
378
        );
379
380
        $result = $this->_config->getDb()->query($query);
381
        $num = $this->_config->getDb()->numRows($result);
382
        $pages = (int) ceil($num / $numPerPage);
383
384
        if ($page == 1) {
385
            $first = 0;
386
        } else {
387
            $first = $page * $numPerPage - $numPerPage;
388
        }
389
390
        if ($num > 0) {
391 View Code Duplication
            if ($pages > 1) {
392
                $output .= sprintf('<p><strong>%s %s %s</strong></p>',
393
                    $this->pmf_lang['msgPage'].$page,
394
                    $this->pmf_lang['msgVoteFrom'],
395
                    $pages.$this->pmf_lang['msgPages']);
396
            }
397
            $output .= '<ul class="phpmyfaq_ul">';
398
399
            $counter = 0;
400
            $displayedCounter = 0;
401
            $renderedItems = [];
402
            while (($row = $this->_config->getDb()->fetchObject($result)) && $displayedCounter < $numPerPage) {
403
                ++$counter;
404
                if ($counter <= $first) {
405
                    continue;
406
                }
407
                ++$displayedCounter;
408
409
                if (empty($row->visits)) {
410
                    $visits = 0;
411
                } else {
412
                    $visits = $row->visits;
413
                }
414
415
                $title = $row->thema;
416
                $url = sprintf(
417
                    '%s?%saction=artikel&amp;cat=%d&amp;id=%d&amp;artlang=%s',
418
                    PMF_Link::getSystemRelativeUri(),
419
                    $sids,
420
                    $row->category_id,
421
                    $row->id,
422
                    $row->lang
423
                );
424
425
                $oLink = new PMF_Link($url, $this->_config);
426
                $oLink->itemTitle = $oLink->text = $oLink->tooltip = $title;
427
428
                // If random FAQs are activated, we don't need sticky FAQs
429
                if (true === $this->_config->get('records.randomSort')) {
430
                    $row->sticky = 0;
431
                }
432
433
                $renderedItems[$row->id] = sprintf(
434
                    '<li%s>%s<span id="viewsPerRecord"><br /><small>(%s)</small></span></li>',
435
                    ($row->sticky == 1) ? ' class="sticky-faqs"' : '',
436
                    $oLink->toHtmlAnchor(),
437
                    $this->plr->GetMsg('plmsgViews', $visits)
438
                );
439
            }
440
441
            // If random FAQs are activated, shuffle the FAQs :-)
442
            if (true === $this->_config->get('records.randomSort')) {
443
                shuffle($renderedItems);
444
            }
445
446
            $output .= implode("\n", $renderedItems);
447
            $output .= '</ul><span class="totalFaqRecords hide">'.$num.'</span>';
448
        } else {
449
            return false;
450
        }
451
452
        if ($pages > 1) {
453
            // Set rewrite URL, if needed
454
            if ($this->_config->get('main.enableRewriteRules')) {
455
                $link = new PMF_Link(PMF_Link::getSystemRelativeUri('index.php'), $this->_config);
456
                $useRewrite = true;
457
                $rewriteUrl = sprintf(
458
                    '%scategory/%d/%%d/%s.html',
459
                    PMF_Link::getSystemRelativeUri('index.php'),
460
                    $categoryId,
461
                    $link->getSEOItemTitle($title)
462
                );
463
            } else {
464
                $useRewrite = false;
465
                $rewriteUrl = '';
466
            }
467
            $baseUrl = sprintf(
468
                '%s?%saction=show&amp;cat=%d&amp;seite=%d',
469
                PMF_Link::getSystemRelativeUri(),
470
                (empty($sids) ? '' : $sids),
471
                $categoryId,
472
                $page
473
            );
474
475
            $options = array(
476
                'baseUrl' => $baseUrl,
477
                'total' => $num,
478
                'perPage' => $this->_config->get('records.numberOfRecordsPerPage'),
479
                'useRewrite' => $useRewrite,
480
                'rewriteUrl' => $rewriteUrl,
481
                'pageParamName' => 'seite',
482
            );
483
484
            $pagination = new PMF_Pagination($this->_config, $options);
485
            $output    .= $pagination->render();
486
        }
487
488
        return $output;
489
    }
490
491
    /**
492
     * This function returns all not expired records from the given record ids.
493
     *
494
     * @param array  $record_ids Array of record ids
495
     * @param string $orderby    Order by
496
     * @param string $sortby     Sort by
497
     *
498
     * @return string
499
     */
500
    public function showAllRecordsByIds(Array $record_ids, $orderby = 'fd.id', $sortby = 'ASC')
501
    {
502
        global $sids;
503
504
        $records = implode(', ', $record_ids);
505
        $page = PMF_Filter::filterInput(INPUT_GET, 'seite', FILTER_VALIDATE_INT, 1);
506
        $tagging_id = PMF_Filter::filterInput(INPUT_GET, 'tagging_id', FILTER_VALIDATE_INT);
507
        $output = '';
508
509
        $now = date('YmdHis');
510
        $query = sprintf("
511
            SELECT
512
                fd.id AS id,
513
                fd.lang AS lang,
514
                fd.thema AS thema,
515
                fcr.category_id AS category_id,
516
                fv.visits AS visits
517
            FROM
518
                %sfaqdata AS fd
519
            LEFT JOIN
520
                %sfaqcategoryrelations AS fcr
521
            ON
522
                fd.id = fcr.record_id
523
            AND
524
                fd.lang = fcr.record_lang
525
            LEFT JOIN
526
                %sfaqvisits AS fv
527
            ON
528
                fd.id = fv.id
529
            AND
530
                fv.lang = fd.lang
531
            LEFT JOIN
532
                %sfaqdata_group AS fdg
533
            ON
534
                fd.id = fdg.record_id
535
            LEFT JOIN
536
                %sfaqdata_user AS fdu
537
            ON
538
                fd.id = fdu.record_id
539
            WHERE
540
                fd.date_start <= '%s'
541
            AND
542
                fd.date_end   >= '%s'
543
            AND
544
                fd.active = 'yes'
545
            AND
546
                fd.id IN (%s)
547
            AND
548
                fd.lang = '%s'
549
                %s
550
            ORDER BY
551
                %s %s",
552
            PMF_Db::getTablePrefix(),
553
            PMF_Db::getTablePrefix(),
554
            PMF_Db::getTablePrefix(),
555
            PMF_Db::getTablePrefix(),
556
            PMF_Db::getTablePrefix(),
557
            $now,
558
            $now,
559
            $records,
560
            $this->_config->getLanguage()->getLanguage(),
561
            $this->queryPermission($this->groupSupport),
562
            $this->_config->getDb()->escape($orderby),
563
            $this->_config->getDb()->escape($sortby));
564
565
        $result = $this->_config->getDb()->query($query);
566
567
        $num = $this->_config->getDb()->numRows($result);
568
        $pages = ceil($num / $this->_config->get('records.numberOfRecordsPerPage'));
569
570
        if ($page == 1) {
571
            $first = 0;
572
        } else {
573
            $first = ($page * $this->_config->get('records.numberOfRecordsPerPage')) - $this->_config->get('records.numberOfRecordsPerPage');
574
        }
575
576
        if ($num > 0) {
577 View Code Duplication
            if ($pages > 1) {
578
                $output .= sprintf('<p><strong>%s %s %s</strong></p>',
579
                    $this->pmf_lang['msgPage'].$page,
580
                    $this->pmf_lang['msgVoteFrom'],
581
                    $pages.$this->pmf_lang['msgPages']);
582
            }
583
            $output .= '<ul class="phpmyfaq_ul">';
584
            $counter = 0;
585
            $displayedCounter = 0;
586
587
            $lastFaqId = 0;
588
            while (($row = $this->_config->getDb()->fetchObject($result)) && $displayedCounter < $this->_config->get('records.numberOfRecordsPerPage')) {
589
                ++$counter;
590
                if ($counter <= $first) {
591
                    continue;
592
                }
593
                ++$displayedCounter;
594
595
                if ($lastFaqId == $row->id) {
596
                    continue; // Don't show multiple FAQs
597
                }
598
599
                if (empty($row->visits)) {
600
                    $visits = 0;
601
                } else {
602
                    $visits = $row->visits;
603
                }
604
605
                $title = $row->thema;
606
                $url = sprintf(
607
                    '%s?%saction=artikel&amp;cat=%d&amp;id=%d&amp;artlang=%s',
608
                    PMF_Link::getSystemRelativeUri(),
609
                    $sids,
610
                    $row->category_id,
611
                    $row->id,
612
                    $row->lang
613
                );
614
                $oLink = new PMF_Link($url, $this->_config);
615
                $oLink->itemTitle = $row->thema;
616
                $oLink->text = $title;
617
                $oLink->tooltip = $title;
618
                $listItem = sprintf(
619
                    '<li>%s<br /><small>(%s)</small></li>',
620
                    $oLink->toHtmlAnchor(),
621
                    $this->plr->GetMsg('plmsgViews', $visits)
622
                );
623
624
                $output .= $listItem;
625
626
                $lastFaqId = $row->id;
627
            }
628
            $output .= '</ul><span id="totFaqRecords" style="display: none;">'.$num.'</span>';
629
        } else {
630
            return false;
631
        }
632
633
        if ($num > $this->_config->get('records.numberOfRecordsPerPage')) {
634
            $output .= '<p class="text-center"><strong>';
635
            if (!isset($page)) {
636
                $page = 1;
637
            }
638
            $vor = $page - 1;
639
            $next = $page + 1;
640 View Code Duplication
            if ($vor != 0) {
641
                $url = $sids.'&amp;action=search&amp;tagging_id='.$tagging_id.'&amp;seite='.$vor;
642
                $oLink = new PMF_Link(PMF_Link::getSystemRelativeUri().'?'.$url, $this->_config);
643
                $oLink->itemTitle = 'tag';
644
                $oLink->text = $this->pmf_lang['msgPrevious'];
645
                $oLink->tooltip = $this->pmf_lang['msgPrevious'];
646
                $output          .= '[ '.$oLink->toHtmlAnchor().' ]';
647
            }
648
            $output .= ' ';
649 View Code Duplication
            if ($next <= $pages) {
650
                $url = $sids.'&amp;action=search&amp;tagging_id='.$tagging_id.'&amp;seite='.$next;
651
                $oLink = new PMF_Link(PMF_Link::getSystemRelativeUri().'?'.$url, $this->_config);
652
                $oLink->itemTitle = 'tag';
653
                $oLink->text = $this->pmf_lang['msgNext'];
654
                $oLink->tooltip = $this->pmf_lang['msgNext'];
655
                $output          .= '[ '.$oLink->toHtmlAnchor().' ]';
656
            }
657
            $output .= '</strong></p>';
658
        }
659
660
        return $output;
661
    }
662
663
    /**
664
     * Returns an array with all data from a FAQ record.
665
     *
666
     * @param int  $faqId         FAQ ID
667
     * @param int  $faqRevisionId Revision ID
668
     * @param bool $isAdmin       Must be true if it is called by an admin/author context
669
     */
670
    public function getRecord($faqId, $faqRevisionId = null, $isAdmin = false)
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)
755
    {
756
        $query = sprintf(
757
            "SELECT
758
                 id, lang, solution_id, revision_id, active, sticky, keywords,
759
                 thema, content, author, email, comment, updated, links_state,
760
                 links_check_date, date_start, date_end, created, notes
761
            FROM
762
                %s%s fd
763
            LEFT JOIN
764
                %sfaqdata_group fdg
765
            ON
766
                fd.id = fdg.record_id
767
            LEFT JOIN
768
                %sfaqdata_user fdu
769
            ON
770
                fd.id = fdu.record_id
771
            WHERE
772
                fd.id = %d
773
            %s
774
            AND
775
                fd.lang = '%s'
776
                %s",
777
            PMF_Db::getTablePrefix(),
778
            isset($faqRevisionId) ? 'faqdata_revisions' : 'faqdata',
779
            PMF_Db::getTablePrefix(),
780
            PMF_Db::getTablePrefix(),
781
            $faqId,
782
            isset($faqRevisionId) ? 'AND revision_id = '.$faqRevisionId : '',
783
            $faqLanguage,
784
            ($isAdmin) ? 'AND 1=1' : $this->queryPermission($this->groupSupport)
785
        );
786
787
        return $this->_config->getDb()->query($query);
788
    }
789
790
    /**
791
     * Return records from given IDs
792
     *
793
     * @param array $faqIds
794
     *
795
     * @return array
796
     */
797
    public function getRecordsByIds(Array $faqIds)
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)
1000
    {
1001
        $solutionId = $this->getSolutionIdFromId($recordId, $recordLang);
1002
1003
        $queries = array(
1004
            sprintf(
1005
                "DELETE FROM %sfaqchanges WHERE beitrag = %d AND lang = '%s'",
1006
                PMF_Db::getTablePrefix(),
1007
                $recordId,
1008
                $recordLang
1009
            ),
1010
            sprintf(
1011
                "DELETE FROM %sfaqcategoryrelations WHERE record_id = %d AND record_lang = '%s'",
1012
                PMF_Db::getTablePrefix(),
1013
                $recordId,
1014
                $recordLang
1015
            ),
1016
            sprintf(
1017
                "DELETE FROM %sfaqdata WHERE id = %d AND lang = '%s'",
1018
                PMF_Db::getTablePrefix(),
1019
                $recordId,
1020
                $recordLang
1021
            ),
1022
            sprintf(
1023
                "DELETE FROM %sfaqdata_revisions WHERE id = %d AND lang = '%s'",
1024
                PMF_Db::getTablePrefix(),
1025
                $recordId,
1026
                $recordLang
1027
            ),
1028
            sprintf(
1029
                "DELETE FROM %sfaqvisits WHERE id = %d AND lang = '%s'",
1030
                PMF_Db::getTablePrefix(),
1031
                $recordId,
1032
                $recordLang
1033
            ),
1034
            sprintf(
1035
                'DELETE FROM %sfaqdata_user WHERE record_id = %d',
1036
                PMF_Db::getTablePrefix(),
1037
                $recordId,
1038
                $recordLang
1039
            ),
1040
            sprintf(
1041
                'DELETE FROM %sfaqdata_group WHERE record_id = %d',
1042
                PMF_Db::getTablePrefix(),
1043
                $recordId,
1044
                $recordLang
1045
            ),
1046
            sprintf(
1047
                'DELETE FROM %sfaqdata_tags WHERE record_id = %d',
1048
                PMF_Db::getTablePrefix(),
1049
                $recordId
1050
            ),
1051
            sprintf(
1052
                'DELETE FROM %sfaqdata_tags WHERE %sfaqdata_tags.record_id NOT IN (SELECT %sfaqdata.id FROM %sfaqdata)',
1053
                PMF_Db::getTablePrefix(),
1054
                PMF_Db::getTablePrefix(),
1055
                PMF_Db::getTablePrefix(),
1056
                PMF_Db::getTablePrefix()
1057
            ),
1058
            sprintf(
1059
                'DELETE FROM %sfaqcomments WHERE id = %d',
1060
                PMF_Db::getTablePrefix(),
1061
                $recordId
1062
            ),
1063
            sprintf(
1064
                'DELETE FROM %sfaqvoting WHERE artikel = %d',
1065
                PMF_Db::getTablePrefix(),
1066
                $recordId
1067
            ),
1068
        );
1069
1070
        foreach ($queries as $query) {
1071
            $this->_config->getDb()->query($query);
1072
        }
1073
1074
        // Delete possible attachments
1075
        $attId = PMF_Attachment_Factory::fetchByRecordId($this->_config, $recordId);
1076
        $attachment = PMF_Attachment_Factory::create($attId);
1077
        $attachment->delete();
1078
1079
        // Delete possible Elasticsearch documents
1080
        if ($this->_config->get('search.enableElasticsearch')) {
1081
            $esInstance = new PMF_Instance_Elasticsearch($this->_config);
1082
            $esInstance->delete($solutionId);
1083
        }
1084
1085
        return true;
1086
    }
1087
1088
    /**
1089
     * Checks if a record is already translated.
1090
     *
1091
     * @param int    $record_id   Record id
1092
     * @param string $record_lang Record language
1093
     *
1094
     * @return bool
1095
     */
1096
    public function isAlreadyTranslated($record_id, $record_lang)
1097
    {
1098
        $query = sprintf("
1099
            SELECT
1100
                id, lang
1101
            FROM
1102
                %sfaqdata
1103
            WHERE
1104
                id = %d
1105
            AND
1106
                lang = '%s'",
1107
            PMF_Db::getTablePrefix(),
1108
            $record_id,
1109
            $record_lang);
1110
1111
        $result = $this->_config->getDb()->query($query);
1112
1113
        if ($this->_config->getDb()->numRows($result)) {
1114
            return true;
1115
        }
1116
1117
        return false;
1118
    }
1119
1120
    /**
1121
     * Checks, if comments are disabled for the FAQ record.
1122
     *
1123
     * @param int    $record_id   Id of FAQ or news entry
1124
     * @param string $record_lang Language
1125
     * @param string $record_type Type of comment: faq or news
1126
     *
1127
     * @return bool true, if comments are disabled
1128
     */
1129
    public function commentDisabled($record_id, $record_lang, $record_type = 'faq')
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)
1171
    {
1172
        if (!is_array($categories)) {
1173
            return false;
1174
        }
1175
1176
        foreach ($categories as $_category) {
1177
            $this->_config->getDb()->query(sprintf(
1178
                "INSERT INTO
1179
                    %sfaqcategoryrelations
1180
                VALUES
1181
                    (%d, '%s', %d, '%s')",
1182
                PMF_Db::getTablePrefix(),
1183
                $_category,
1184
                $language,
1185
                $record_id,
1186
                $language));
1187
        }
1188
1189
        return true;
1190
    }
1191
1192
    /**
1193
     * Adds new category relation to a record.
1194
     *
1195
     * @param mixed  $category  Category or array of categories
1196
     * @param int    $record_id Record id
1197
     * @param string $language  Language
1198
     *
1199
     * @return bool
1200
     */
1201
    public function addCategoryRelation($category, $record_id, $language)
1202
    {
1203
        // Just a fallback when (wrong case) $category is an array
1204
        if (is_array($category)) {
1205
            $this->addCategoryRelations($category, $record_id, $language);
1206
        }
1207
        $categories[] = $category;
1208
1209
        return $this->addCategoryRelations($categories, $record_id, $language);
1210
    }
1211
1212
    /**
1213
     * Deletes category relations to a record.
1214
     *
1215
     * @param int    $record_id   Record id
1216
     * @param string $record_lang Language
1217
     *
1218
     * @return bool
1219
     */
1220
    public function deleteCategoryRelations($record_id, $record_lang)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
                id, lang, content
1317
            FROM
1318
                %sfaqdata
1319
            WHERE
1320
                solution_id = %d',
1321
            PMF_Db::getTablePrefix(),
1322
            $solutionId
1323
        );
1324
1325
        $result = $this->_config->getDb()->query($query);
1326
1327
        if ($row = $this->_config->getDb()->fetchObject($result)) {
1328
            return [
1329
                'id' => $row->id,
1330
                'lang' => $row->lang,
1331
                'content' => $row->content
1332
            ];
1333
        }
1334
1335
        return [];
1336
    }
1337
1338
    /**
1339
     * Returns the solution ID from a given ID and language
1340
     *
1341
     * @param integer $faqId
1342
     * @param string $faqLang
1343
     *
1344
     * @return int
1345
     */
1346
    public function getSolutionIdFromId($faqId, $faqLang)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
1347
    {
1348
        $query = sprintf("
1349
            SELECT
1350
                solution_id
1351
            FROM
1352
                %sfaqdata
1353
            WHERE
1354
                id = %d
1355
                AND
1356
                lang = '%s'",
1357
            PMF_Db::getTablePrefix(),
1358
            (int) $faqId,
1359
            $this->_config->getDb()->escape($faqLang)
1360
        );
1361
1362
        $result = $this->_config->getDb()->query($query);
1363
1364
        if ($row = $this->_config->getDb()->fetchObject($result)) {
1365
            return $row->solution_id;
1366
        }
1367
1368
        return $this->getSolutionId();
1369
    }
1370
1371
    /**
1372
     * Gets the latest solution id for a FAQ record.
1373
     *
1374
     * @return int
1375
     */
1376
    public function getSolutionId()
1377
    {
1378
        $latestId = 0;
1379
1380
        $query = sprintf('
1381
            SELECT
1382
                MAX(solution_id) AS solution_id
1383
            FROM
1384
                %sfaqdata',
1385
            PMF_Db::getTablePrefix()
1386
        );
1387
1388
        $result = $this->_config->getDb()->query($query);
1389
1390
        if ($result && $row = $this->_config->getDb()->fetchObject($result)) {
1391
            $latestId = $row->solution_id;
1392
        }
1393
1394
        if ($latestId < PMF_SOLUTION_ID_START_VALUE) {
1395
            $nextSolutionId = PMF_SOLUTION_ID_START_VALUE;
1396
        } else {
1397
            $nextSolutionId = $latestId + PMF_SOLUTION_ID_INCREMENT_VALUE;
1398
        }
1399
1400
        return $nextSolutionId;
1401
    }
1402
1403
    /**
1404
     * Returns an array with all data from all FAQ records.
1405
     *
1406
     * @param int    $sortType  Sorting type
1407
     * @param array  $condition Condition
1408
     * @param string $sortOrder Sorting order
1409
     */
1410
    public function getAllRecords($sortType = FAQ_SORTING_TYPE_CATID_FAQID, Array $condition = null, $sortOrder = 'ASC')
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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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)
1598
    {
1599
        $revisionData = [];
1600
1601
        $query = sprintf("
1602
            SELECT
1603
                revision_id, updated, author
1604
            FROM
1605
                %sfaqdata_revisions
1606
            WHERE
1607
                id = %d
1608
            AND
1609
                lang = '%s'
1610
            ORDER BY
1611
                revision_id",
1612
            PMF_Db::getTablePrefix(),
1613
            $recordId,
1614
            $recordLang
1615
        );
1616
1617
        $result = $this->_config->getDb()->query($query);
1618
1619
        if ($this->_config->getDb()->numRows($result) > 0) {
1620
            while ($row = $this->_config->getDb()->fetchObject($result)) {
1621
                $revisionData[] = [
1622
                    'revision_id' => $row->revision_id,
1623
                    'updated' => $row->updated,
1624
                    'author' => $row->author,
1625
                ];
1626
            }
1627
        }
1628
1629
        return $revisionData;
1630
    }
1631
1632
    /**
1633
     * Adds a new revision from a given record ID.
1634
     *
1635
     * @param int    $record_id   Record id
1636
     * @param string $record_lang Record language
1637
     *
1638
     * @return array
1639
     */
1640
    public function addNewRevision($record_id, $record_lang)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
1641
    {
1642
        $query = sprintf("
1643
            INSERT INTO
1644
                %sfaqdata_revisions
1645
            SELECT * FROM
1646
                %sfaqdata
1647
            WHERE
1648
                id = %d
1649
            AND
1650
                lang = '%s'",
1651
            PMF_Db::getTablePrefix(),
1652
            PMF_Db::getTablePrefix(),
1653
            $record_id,
1654
            $record_lang);
1655
        $this->_config->getDb()->query($query);
1656
1657
        return true;
1658
    }
1659
1660
    /**
1661
     * Returns the keywords of a FAQ record from the ID and language.
1662
     *
1663
     * @param int $id record id
1664
     *
1665
     * @return string
1666
     */
1667
    public function getRecordKeywords($id)
1668
    {
1669
        if (isset($this->faqRecord['id']) && ($this->faqRecord['id'] == $id)) {
1670
            return $this->faqRecord['keywords'];
1671
        }
1672
1673
        $query = sprintf(
1674
            "SELECT
1675
                keywords
1676
            FROM
1677
                %sfaqdata
1678
            WHERE id = %d AND lang = '%s'",
1679
            PMF_Db::getTablePrefix(),
1680
            $id,
1681
            $this->_config->getLanguage()->getLanguage());
1682
1683
        $result = $this->_config->getDb()->query($query);
1684
1685
        if ($this->_config->getDb()->numRows($result) > 0) {
1686
            $row = $this->_config->getDb()->fetchObject($result);
1687
1688
            return PMF_String::htmlspecialchars($row->keywords, ENT_QUOTES, 'utf-8');
1689
        } else {
1690
            return '';
1691
        }
1692
    }
1693
1694
    /**
1695
     * Returns a answer preview of the FAQ record.
1696
     *
1697
     * @param int $recordId  FAQ record ID
1698
     * @param int $wordCount Number of words, default: 12
1699
     *
1700
     * @return string
1701
     */
1702
    public function getRecordPreview($recordId, $wordCount = 12)
1703
    {
1704
        $answerPreview = '';
1705
1706
        if (isset($this->faqRecord['id']) && ($this->faqRecord['id'] == $recordId)) {
1707
            $answerPreview = $this->faqRecord['content'];
1708
        }
1709
1710
        $query = sprintf("
1711
            SELECT
1712
                content as answer
1713
            FROM
1714
                %sfaqdata
1715
            WHERE 
1716
                id = %d 
1717
            AND 
1718
                lang = '%s'",
1719
            PMF_Db::getTablePrefix(),
1720
            $recordId,
1721
            $this->_config->getLanguage()->getLanguage());
1722
1723
        $result = $this->_config->getDb()->query($query);
1724
1725 View Code Duplication
        if ($this->_config->getDb()->numRows($result) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
1726
            $row = $this->_config->getDb()->fetchObject($result);
1727
            $answerPreview = strip_tags($row->answer);
1728
        } else {
1729
            $answerPreview = $this->_config->get('main.metaDescription');
1730
        }
1731
1732
        return PMF_Utils::makeShorterText($answerPreview, $wordCount);
1733
    }
1734
1735
    /**
1736
     * Returns the number of activated and not expired records, optionally
1737
     * not limited to the current language.
1738
     *
1739
     * @param string $language Language
1740
     *
1741
     * @return int
1742
     */
1743
    public function getNumberOfRecords($language = null)
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')
1786
    {
1787
        if ('visits' == $type) {
1788
            $result = $this->getTopTenData(PMF_NUMBER_RECORDS_TOPTEN, 0, $this->_config->getLanguage()->getLanguage());
1789
        } else {
1790
            $result = $this->getTopVotedData(PMF_NUMBER_RECORDS_TOPTEN, $this->_config->getLanguage()->getLanguage());
1791
        }
1792
        $output = [];
1793
1794
        if (count($result) > 0) {
1795
            foreach ($result as $row) {
1796
                if ('visits' == $type) {
1797
                    $output['title'][] = PMF_Utils::makeShorterText($row['question'], 8);
1798
                    $output['preview'][] = $row['question'];
1799
                    $output['url'][] = $row['url'];
1800
                    $output['visits'][] = $this->plr->GetMsg('plmsgViews', $row['visits']);
1801
                } else {
1802
                    $output['title'][] = PMF_Utils::makeShorterText($row['question'], 8);
1803
                    $output['preview'][] = $row['question'];
1804
                    $output['url'][] = $row['url'];
1805
                    $output['voted'][] = sprintf(
1806
                        '%s %s 5 - %s',
1807
                        round($row['avg'], 2),
1808
                        $this->pmf_lang['msgVoteFrom'],
1809
                        $this->plr->GetMsg('plmsgVotes', $row['user'])
1810
                    );
1811
                }
1812
            }
1813
        } else {
1814
            $output['error'] = $this->pmf_lang['err_noTopTen'];
1815
        }
1816
1817
        return $output;
1818
    }
1819
1820
    /**
1821
     * This function generates the list with the latest published records.
1822
     *
1823
     * @return array
1824
     */
1825
    public function getLatest()
1826
    {
1827
        $date = new PMF_Date($this->_config);
1828
        $result = $this->getLatestData(PMF_NUMBER_RECORDS_LATEST, $this->_config->getLanguage()->getLanguage());
1829
        $output = [];
1830
1831
        if (count($result) > 0) {
1832
            foreach ($result as $row) {
1833
                $output['url'][] = $row['url'];
1834
                $output['title'][] = PMF_Utils::makeShorterText($row['question'], 8);
1835
                $output['preview'][] = $row['question'];
1836
                $output['date'][] = $date->format(PMF_Date::createIsoDate($row['date']));
1837
            }
1838
        } else {
1839
            $output['error'] = $this->pmf_lang['err_noArticles'];
1840
        }
1841
1842
        return $output;
1843
    }
1844
1845
    /**
1846
     * Deletes a question for the table faqquestions.
1847
     *
1848
     * @param int $questionId
1849
     *
1850
     * @return bool
1851
     */
1852
    public function deleteQuestion($questionId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
1853
    {
1854
        $delete = sprintf("
1855
            DELETE FROM
1856
                %sfaqquestions
1857
            WHERE
1858
                id = %d
1859
            AND
1860
                lang = '%s'",
1861
            PMF_Db::getTablePrefix(),
1862
            $questionId,
1863
            $this->_config->getLanguage()->getLanguage()
1864
        );
1865
1866
        $this->_config->getDb()->query($delete);
1867
1868
        return true;
1869
    }
1870
1871
     /**
1872
      * Returns the visibility of a question.
1873
      *
1874
      * @param   int $questionId
1875
      *
1876
      * @return  string
1877
      */
1878
     public function getVisibilityOfQuestion($questionId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
1879
     {
1880
         $query = sprintf("
1881
            SELECT
1882
                is_visible
1883
            FROM
1884
                %sfaqquestions
1885
            WHERE
1886
                id = %d
1887
            AND
1888
                lang = '%s'",
1889
            PMF_Db::getTablePrefix(),
1890
            $questionId,
1891
            $this->_config->getLanguage()->getLanguage()
1892
        );
1893
1894
         $result = $this->_config->getDb()->query($query);
1895
         if ($this->_config->getDb()->numRows($result) > 0) {
1896
             $row = $this->_config->getDb()->fetchObject($result);
1897
1898
             return $row->is_visible;
1899
         }
1900
1901
         return;
1902
     }
1903
1904
    /**
1905
     * Sets the visibility of a question.
1906
     *
1907
     * @param int    $questionId
1908
     * @param string $isVisible
1909
     *
1910
     * @return bool
1911
     */
1912
    public function setVisibilityOfQuestion($questionId, $isVisible)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
1913
    {
1914
        $query = sprintf("
1915
            UPDATE
1916
                %sfaqquestions
1917
            SET
1918
                is_visible = '%s'
1919
            WHERE
1920
                id = %d
1921
            AND
1922
                lang = '%s'",
1923
            PMF_Db::getTablePrefix(),
1924
            $isVisible,
1925
            $questionId,
1926
            $this->_config->getLanguage()->getLanguage()
1927
        );
1928
1929
        $this->_config->getDb()->query($query);
1930
1931
        return true;
1932
    }
1933
1934
    /**
1935
     * This function generates a data-set with the most voted FAQs.
1936
     *  
1937
     * @param int    $count    Number of records
1938
     * @param string $language Language
1939
     *
1940
     * @return array
1941
     */
1942
    public function getTopVotedData($count = PMF_NUMBER_RECORDS_TOPTEN, $language = null)
1943
    {
1944
        global $sids;
1945
1946
        $topten = $data = [];
1947
1948
        $now = date('YmdHis');
1949
        $query =
1950
'            SELECT
1951
                fd.id AS id,
1952
                fd.lang AS lang,
1953
                fd.thema AS thema,
1954
                fd.updated AS updated,
1955
                fcr.category_id AS category_id,
1956
                (fv.vote/fv.usr) AS avg,
1957
                fv.usr AS user
1958
            FROM
1959
                '.PMF_Db::getTablePrefix().'faqvoting fv,
1960
                '.PMF_Db::getTablePrefix().'faqdata fd
1961
            LEFT JOIN
1962
                '.PMF_Db::getTablePrefix().'faqcategoryrelations fcr
1963
            ON
1964
                fd.id = fcr.record_id
1965
            AND
1966
                fd.lang = fcr.record_lang
1967
            LEFT JOIN
1968
                '.PMF_Db::getTablePrefix().'faqdata_group AS fdg
1969
            ON
1970
                fd.id = fdg.record_id
1971
            LEFT JOIN
1972
                '.PMF_Db::getTablePrefix().'faqdata_user AS fdu
1973
            ON
1974
                fd.id = fdu.record_id
1975
            WHERE
1976
                    fd.date_start <= \''.$now.'\'
1977
                AND fd.date_end   >= \''.$now.'\'
1978
                AND fd.id = fv.artikel
1979
                AND fd.active = \'yes\'';
1980
1981 View Code Duplication
        if (isset($categoryId) && is_numeric($categoryId) && ($categoryId != 0)) {
0 ignored issues
show
Bug introduced by
The variable $categoryId seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
1982
            $query .= '
1983
            AND
1984
                fcr.category_id = \''.$categoryId.'\'';
1985
        }
1986
        if (isset($language) && PMF_Language::isASupportedLanguage($language)) {
1987
            $query .= '
1988
            AND
1989
                fd.lang = \''.$language.'\'';
1990
        }
1991
        $query .= '
1992
                '.$this->queryPermission($this->groupSupport).'
1993
            ORDER BY
1994
                avg DESC';
1995
1996
        $result = $this->_config->getDb()->query($query);
1997
1998
        $i = 1;
1999
        $oldId = 0;
2000
        while (($row = $this->_config->getDb()->fetchObject($result)) && $i <= $count) {
2001
            if ($oldId != $row->id) {
2002
                $data['avg'] = $row->avg;
2003
                $data['question'] = $row->thema;
2004
                $data['date'] = $row->updated;
2005
                $data['user'] = $row->user;
2006
2007
                $title = $row->thema;
2008
                $url = sprintf(
2009
                    '%s?%saction=artikel&amp;cat=%d&amp;id=%d&amp;artlang=%s',
2010
                    PMF_Link::getSystemRelativeUri(),
2011
                    $sids,
2012
                    $row->category_id,
2013
                    $row->id,
2014
                    $row->lang
2015
                );
2016
                $oLink = new PMF_Link($url, $this->_config);
2017
                $oLink->itemTitle = $row->thema;
2018
                $oLink->tooltip = $title;
2019
                $data['url'] = $oLink->toString();
2020
2021
                $topten[] = $data;
2022
                ++$i;
2023
            }
2024
            $oldId = $row->id;
2025
        }
2026
2027
        return $topten;
2028
    }
2029
2030
    /**
2031
     * This function generates the Top Ten data with the mosted viewed records.
2032
     *
2033
     * @param int    $count      Number of records
2034
     * @param int    $categoryId Category ID
2035
     * @param string $language   Language
2036
     *
2037
     * @return array
2038
     */
2039
    public function getTopTenData($count = PMF_NUMBER_RECORDS_TOPTEN, $categoryId = 0, $language = null)
2040
    {
2041
        global $sids;
2042
2043
        $now = date('YmdHis');
2044
        $query =
2045
'            SELECT
2046
                fd.id AS id,
2047
                fd.lang AS lang,
2048
                fd.thema AS question,
2049
                fd.updated AS updated,
2050
                fcr.category_id AS category_id,
2051
                fv.visits AS visits,
2052
                fv.last_visit AS last_visit,
2053
                fdg.group_id AS group_id,
2054
                fdu.user_id AS user_id
2055
            FROM
2056
                '.PMF_Db::getTablePrefix().'faqvisits fv,
2057
                '.PMF_Db::getTablePrefix().'faqdata fd
2058
            LEFT JOIN
2059
                '.PMF_Db::getTablePrefix().'faqcategoryrelations fcr
2060
            ON
2061
                fd.id = fcr.record_id
2062
            AND
2063
                fd.lang = fcr.record_lang
2064
            LEFT JOIN
2065
                '.PMF_Db::getTablePrefix().'faqdata_group AS fdg
2066
            ON
2067
                fd.id = fdg.record_id
2068
            LEFT JOIN
2069
                '.PMF_Db::getTablePrefix().'faqdata_user AS fdu
2070
            ON
2071
                fd.id = fdu.record_id
2072
            WHERE
2073
                    fd.date_start <= \''.$now.'\'
2074
                AND fd.date_end   >= \''.$now.'\'
2075
                AND fd.id = fv.id
2076
                AND fd.lang = fv.lang
2077
                AND fd.active = \'yes\'';
2078
2079 View Code Duplication
        if (isset($categoryId) && is_numeric($categoryId) && ($categoryId != 0)) {
2080
            $query .= '
2081
            AND
2082
                fcr.category_id = \''.$categoryId.'\'';
2083
        }
2084
        if (isset($language) && PMF_Language::isASupportedLanguage($language)) {
2085
            $query .= '
2086
            AND
2087
                fd.lang = \''.$language.'\'';
2088
        }
2089
        $query .= '
2090
                '.$this->queryPermission($this->groupSupport).'
2091
2092
            GROUP BY
2093
                fd.id,fd.lang,fcr.category_id,fv.visits,fv.last_visit,fdg.group_id,fdu.user_id
2094
            ORDER BY
2095
                fv.visits DESC';
2096
2097
        $result = $this->_config->getDb()->query($query);
2098
        $topten = [];
2099
        $data = [];
2100
2101 View Code Duplication
        if ($result) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
2102
            while ($row = $this->_config->getDb()->fetchObject($result)) {
2103
                if ($this->groupSupport) {
2104
                    if (!in_array($row->user_id, array(-1, $this->user)) || !in_array($row->group_id, $this->groups)) {
2105
                        continue;
2106
                    }
2107
                } else {
2108
                    if (!in_array($row->user_id, array(-1, $this->user))) {
2109
                        continue;
2110
                    }
2111
                }
2112
2113
                $data['visits'] = $row->visits;
2114
                $data['question'] = $row->question;
2115
                $data['date'] = $row->updated;
2116
                $data['last_visit'] = $row->last_visit;
2117
2118
                $title = $row->question;
2119
                $url = sprintf(
2120
                    '%sindex.php?%saction=artikel&cat=%d&id=%d&artlang=%s',
2121
                    $this->_config->getDefaultUrl(),
2122
                    $sids,
2123
                    $row->category_id,
2124
                    $row->id,
2125
                    $row->lang
2126
                );
2127
                $oLink = new PMF_Link($url, $this->_config);
2128
                $oLink->itemTitle = $row->question;
2129
                $oLink->tooltip = $title;
2130
                $data['url'] = $oLink->toString();
2131
2132
                $topten[$row->id] = $data;
2133
2134
                if (count($topten) === $count) {
2135
                    break;
2136
                }
2137
            }
2138
        }
2139
2140
        return $topten;
2141
    }
2142
2143
    /**
2144
     * This function generates an array with a specified number of most recent
2145
     * published records.
2146
     *
2147
     * @param int    $count    Number of records
2148
     * @param string $language Language
2149
     *
2150
     * @return array
2151
     */
2152
    public function getLatestData($count = PMF_NUMBER_RECORDS_LATEST, $language = null)
2153
    {
2154
        global $sids;
2155
2156
        $now = date('YmdHis');
2157
        $query =
2158
'            SELECT
2159
                fd.id AS id,
2160
                fd.lang AS lang,
2161
                fcr.category_id AS category_id,
2162
                fd.thema AS question,
2163
                fd.content AS content,
2164
                fd.updated AS updated,
2165
                fv.visits AS visits,
2166
                fdg.group_id AS group_id,
2167
                fdu.user_id AS user_id
2168
            FROM
2169
                '.PMF_Db::getTablePrefix().'faqvisits fv,
2170
                '.PMF_Db::getTablePrefix().'faqdata fd
2171
            LEFT JOIN
2172
                '.PMF_Db::getTablePrefix().'faqcategoryrelations fcr
2173
            ON
2174
                fd.id = fcr.record_id
2175
            AND
2176
                fd.lang = fcr.record_lang
2177
            LEFT JOIN
2178
                '.PMF_Db::getTablePrefix().'faqdata_group AS fdg
2179
            ON
2180
                fd.id = fdg.record_id
2181
            LEFT JOIN
2182
                '.PMF_Db::getTablePrefix().'faqdata_user AS fdu
2183
            ON
2184
                fd.id = fdu.record_id
2185
            WHERE
2186
                    fd.date_start <= \''.$now.'\'
2187
                AND fd.date_end   >= \''.$now.'\'
2188
                AND fd.id = fv.id
2189
                AND fd.lang = fv.lang
2190
                AND fd.active = \'yes\'';
2191
2192
        if (isset($language) && PMF_Language::isASupportedLanguage($language)) {
2193
            $query .= '
2194
            AND
2195
                fd.lang = \''.$language.'\'';
2196
        }
2197
        $query .= '
2198
                '.$this->queryPermission($this->groupSupport).'
2199
            GROUP BY
2200
                fd.id,fd.lang,fcr.category_id,fv.visits,fdg.group_id,fdu.user_id
2201
            ORDER BY
2202
                fd.updated DESC';
2203
2204
        $result = $this->_config->getDb()->query($query);
2205
        $latest = [];
2206
        $data = [];
2207
2208 View Code Duplication
        if ($result) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
2209
            while (($row = $this->_config->getDb()->fetchObject($result))) {
2210
                if ($this->groupSupport) {
2211
                    if (!in_array($row->user_id, array(-1, $this->user)) || !in_array($row->group_id, $this->groups)) {
2212
                        continue;
2213
                    }
2214
                } else {
2215
                    if (!in_array($row->user_id, array(-1, $this->user))) {
2216
                        continue;
2217
                    }
2218
                }
2219
2220
                $data['date'] = $row->updated;
2221
                $data['question'] = $row->question;
2222
                $data['answer'] = $row->content;
2223
                $data['visits'] = $row->visits;
2224
2225
                $title = $row->question;
2226
                $url = sprintf(
2227
                    '%sindex.php?%saction=artikel&cat=%d&id=%d&artlang=%s',
2228
                    $this->_config->getDefaultUrl(),
2229
                    $sids,
2230
                    $row->category_id,
2231
                    $row->id,
2232
                    $row->lang
2233
                );
2234
                $oLink = new PMF_Link($url, $this->_config);
2235
                $oLink->itemTitle = $row->question;
2236
                $oLink->tooltip = $title;
2237
                $data['url'] = $oLink->toString();
2238
2239
                $latest[$row->id] = $data;
2240
2241
                if (count($latest) === $count) {
2242
                    break;
2243
                }
2244
            }
2245
        }
2246
        
2247
        return $latest;
2248
    }
2249
2250
    /**
2251
     * Reload locking for user votings.
2252
     *
2253
     * @param int    $id FAQ record id
2254
     * @param string $ip IP
2255
     *
2256
     * @return bool
2257
     */
2258
    public function votingCheck($id, $ip)
2259
    {
2260
        $check = $_SERVER['REQUEST_TIME'] - 300;
2261
        $query = sprintf(
2262
            "SELECT
2263
                id
2264
            FROM
2265
                %sfaqvoting
2266
            WHERE
2267
                artikel = %d AND (ip = '%s' AND datum > '%s')",
2268
            PMF_Db::getTablePrefix(),
2269
            $id,
2270
            $ip,
2271
            $check);
2272
        if ($this->_config->getDb()->numRows($this->_config->getDb()->query($query))) {
2273
            return false;
2274
        }
2275
2276
        return true;
2277
    }
2278
2279
    /**
2280
     * Returns the number of users from the table faqvotings.
2281
     *
2282
     * @param integer $record_id
2283
     *
2284
     * @return integer
2285
     */
2286 View Code Duplication
    public function getNumberOfVotings($record_id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
2287
    {
2288
        $query = sprintf(
2289
            'SELECT
2290
                usr
2291
            FROM
2292
                %sfaqvoting
2293
            WHERE
2294
                artikel = %d',
2295
            PMF_Db::getTablePrefix(),
2296
            $record_id);
2297
        if ($result = $this->_config->getDb()->query($query)) {
2298
            if ($row = $this->_config->getDb()->fetchObject($result)) {
2299
                return $row->usr;
2300
            }
2301
        }
2302
2303
        return 0;
2304
    }
2305
2306
    /**
2307
     * Adds a new voting record.
2308
     *
2309
     * @param array $votingData
2310
     *
2311
     * @return bool
2312
     */
2313
    public function addVoting($votingData)
2314
    {
2315
        if (!is_array($votingData)) {
2316
            return false;
2317
        }
2318
2319
        $query = sprintf(
2320
            "INSERT INTO
2321
                %sfaqvoting
2322
            VALUES
2323
                (%d, %d, %d, 1, %d, '%s')",
2324
            PMF_Db::getTablePrefix(),
2325
            $this->_config->getDb()->nextId(PMF_Db::getTablePrefix().'faqvoting', 'id'),
2326
            $votingData['record_id'],
2327
            $votingData['vote'],
2328
            $_SERVER['REQUEST_TIME'],
2329
            $votingData['user_ip']);
2330
        $this->_config->getDb()->query($query);
2331
2332
        return true;
2333
    }
2334
2335
    /**
2336
     * Adds a new question.
2337
     *
2338
     * @param array $questionData
2339
     *
2340
     * @return bool
2341
     */
2342
    public function addQuestion(Array $questionData)
2343
    {
2344
        $query = sprintf("
2345
            INSERT INTO
2346
                %sfaqquestions
2347
            VALUES
2348
                (%d, '%s', '%s', '%s', %d, '%s', '%s', '%s', %d)",
2349
            PMF_Db::getTablePrefix(),
2350
            $this->_config->getDb()->nextId(PMF_Db::getTablePrefix().'faqquestions', 'id'),
2351
            $this->_config->getLanguage()->getLanguage(),
2352
            $this->_config->getDb()->escape($questionData['username']),
2353
            $this->_config->getDb()->escape($questionData['email']),
2354
            $questionData['category_id'],
2355
            $this->_config->getDb()->escape($questionData['question']),
2356
            date('YmdHis'),
2357
            $questionData['is_visible'],
2358
            0
2359
        );
2360
        $this->_config->getDb()->query($query);
2361
2362
        return true;
2363
    }
2364
2365
    /**
2366
     * Returns a new question.
2367
     *
2368
     * @param int $questionId
2369
     *
2370
     * @return array
2371
     */
2372
    public function getQuestion($questionId)
2373
    {
2374
        $question = [
2375
            'id' => 0,
2376
            'lang' => '',
2377
            'username' => '',
2378
            'email' => '',
2379
            'category_id' => '',
2380
            'question' => '',
2381
            'created' => '',
2382
            'is_visible' => '',
2383
        ];
2384
2385
        if (!is_int($questionId)) {
2386
            return $question;
2387
        }
2388
2389
        $question = [];
2390
2391
        $query = sprintf("
2392
            SELECT
2393
                 id, lang, username, email, category_id, question, created, is_visible
2394
            FROM
2395
                %sfaqquestions
2396
            WHERE
2397
                id = %d
2398
            AND
2399
                lang = '%s'",
2400
            PMF_Db::getTablePrefix(),
2401
            $questionId,
2402
            $this->_config->getLanguage()->getLanguage()
2403
        );
2404
2405 View Code Duplication
        if ($result = $this->_config->getDb()->query($query)) {
2406
            if ($row = $this->_config->getDb()->fetchObject($result)) {
2407
                $question = array(
2408
                    'id' => $row->id,
2409
                    'lang' => $row->lang,
2410
                    'username' => $row->username,
2411
                    'email' => $row->email,
2412
                    'category_id' => $row->category_id,
2413
                    'question' => $row->question,
2414
                    'created' => $row->created,
2415
                    'is_visible' => $row->is_visible, );
2416
            }
2417
        }
2418
2419
        return $question;
2420
    }
2421
2422
    /**
2423
     * Returns all open questions.
2424
     *
2425
     * @param boolean $all If true, then return visible and non-visible
2426
     *                     questions; otherwise only visible ones
2427
     *
2428
     * @return array
2429
     */
2430
    public function getAllOpenQuestions($all = true)
2431
    {
2432
        $questions = [];
2433
2434
        $query = sprintf("
2435
            SELECT
2436
                id, lang, username, email, category_id, question, created, answer_id, is_visible
2437
            FROM
2438
                %sfaqquestions
2439
            WHERE
2440
                lang = '%s'
2441
                %s
2442
            ORDER BY 
2443
                created ASC",
2444
            PMF_Db::getTablePrefix(),
2445
            $this->_config->getLanguage()->getLanguage(),
2446
            ($all == false ? " AND is_visible = 'Y'" : '')
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
2447
        );
2448
2449 View Code Duplication
        if ($result = $this->_config->getDb()->query($query)) {
2450
            while ($row = $this->_config->getDb()->fetchObject($result)) {
2451
                $questions[] = array(
2452
                    'id' => $row->id,
2453
                    'lang' => $row->lang,
2454
                    'username' => $row->username,
2455
                    'email' => $row->email,
2456
                    'category_id' => $row->category_id,
2457
                    'question' => $row->question,
2458
                    'created' => $row->created,
2459
                    'answer_id' => $row->answer_id,
2460
                    'is_visible' => $row->is_visible,
2461
                );
2462
             }
2463
        }
2464
2465
        return $questions;
2466
    }
2467
2468
    /**
2469
     * Updates an existing voting record.
2470
     *
2471
     * @param array $votingData
2472
     *
2473
     * @return bool
2474
     */
2475
    public function updateVoting($votingData)
2476
    {
2477
        if (!is_array($votingData)) {
2478
            return false;
2479
        }
2480
2481
        $query = sprintf(
2482
            "UPDATE
2483
                %sfaqvoting
2484
            SET
2485
                vote    = vote + %d,
2486
                usr     = usr + 1,
2487
                datum   = %d,
2488
                ip      = '%s'
2489
            WHERE
2490
                artikel = %d",
2491
            PMF_Db::getTablePrefix(),
2492
            $votingData['vote'],
2493
            $_SERVER['REQUEST_TIME'],
2494
            $votingData['user_ip'],
2495
            $votingData['record_id']);
2496
        $this->_config->getDb()->query($query);
2497
2498
        return true;
2499
    }
2500
2501
    /**
2502
     * Adds a new changelog entry in the table faqchanges.
2503
     *
2504
     * @param int    $id
2505
     * @param int    $userId
2506
     * @param string $text
2507
     * @param string $lang
2508
     * @param int    $revision_id
2509
     *
2510
     * @return bool
2511
     */
2512
    public function createChangeEntry($id, $userId, $text, $lang, $revision_id = 0)
2513
    {
2514
        if (!is_numeric($id)
2515
            && !is_numeric($userId)
2516
            && !is_string($text)
2517
            && !is_string($lang)
2518
            ) {
2519
            return false;
2520
        }
2521
2522
        $query = sprintf(
2523
            "INSERT INTO
2524
                %sfaqchanges
2525
            (id, beitrag, lang, revision_id, usr, datum, what)
2526
                VALUES
2527
            (%d, %d, '%s', %d, %d, %d, '%s')",
2528
            PMF_Db::getTablePrefix(),
2529
            $this->_config->getDb()->nextId(PMF_Db::getTablePrefix().'faqchanges', 'id'),
2530
            $id,
2531
            $lang,
2532
            $revision_id,
2533
            $userId,
2534
            $_SERVER['REQUEST_TIME'],
2535
            $text);
2536
2537
        $this->_config->getDb()->query($query);
2538
2539
        return true;
2540
    }
2541
2542
    /**
2543
     * Returns the changelog of a FAQ record.
2544
     *
2545
     * @param int $record_id
2546
     *
2547
     * @return array
2548
     */
2549 View Code Duplication
    public function getChangeEntries($record_id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
2550
    {
2551
        $entries = [];
2552
2553
        $query = sprintf('
2554
            SELECT
2555
                DISTINCT revision_id, usr, datum, what
2556
            FROM
2557
                %sfaqchanges
2558
            WHERE
2559
                beitrag = %d
2560
            ORDER BY id DESC',
2561
            PMF_Db::getTablePrefix(),
2562
            $record_id
2563
            );
2564
2565
        if ($result = $this->_config->getDb()->query($query)) {
2566
            while ($row = $this->_config->getDb()->fetchObject($result)) {
2567
                $entries[] = array(
2568
                    'revision_id' => $row->revision_id,
2569
                    'user' => $row->usr,
2570
                    'date' => $row->datum,
2571
                    'changelog' => $row->what, );
2572
            }
2573
        }
2574
2575
        return $entries;
2576
    }
2577
2578
    /**
2579
     * Retrieve faq records according to the constraints provided.
2580
     *
2581
     * @param string $queryType
2582
     * @param int    $nCatid
2583
     * @param bool   $bDownwards
2584
     * @param string $lang
2585
     * @param string $date
2586
     *
2587
     * @return array
2588
     */
2589
    public function get($queryType = FAQ_QUERY_TYPE_DEFAULT, $nCatid = 0, $bDownwards = true, $lang = '', $date = '')
2590
    {
2591
        $faqs = [];
2592
2593
        $result = $this->_config->getDb()->query($this->_getSQLQuery($queryType, $nCatid, $bDownwards, $lang, $date));
2594
2595
        if ($this->_config->getDb()->numRows($result) > 0) {
2596
            $i = 0;
2597
            while ($row = $this->_config->getDb()->fetchObject($result)) {
2598
                $faq = [];
2599
                $faq['id'] = $row->id;
2600
                $faq['solution_id'] = $row->solution_id;
2601
                $faq['revision_id'] = $row->revision_id;
2602
                $faq['lang'] = $row->lang;
2603
                $faq['category_id'] = $row->category_id;
2604
                $faq['active'] = $row->active;
2605
                $faq['sticky'] = $row->sticky;
2606
                $faq['keywords'] = $row->keywords;
2607
                $faq['topic'] = $row->thema;
2608
                $faq['content'] = $row->content;
2609
                $faq['author_name'] = $row->author;
2610
                $faq['author_email'] = $row->email;
2611
                $faq['comment_enable'] = $row->comment;
2612
                $faq['lastmodified'] = $row->updated;
2613
                $faq['hits'] = $row->visits;
2614
                $faq['hits_last'] = $row->last_visit;
2615
                $faq['notes'] = $row->notes;
2616
                $faqs[$i] = $faq;
2617
                ++$i;
2618
            }
2619
        }
2620
2621
        return $faqs;
2622
    }
2623
2624
    /**
2625
     * Build a logic sequence, for a WHERE statement, of those category IDs
2626
     * children of the provided category ID, if any.
2627
     *
2628
     * @param   $nCatid
2629
     * @param   $logicOp
2630
     * @param   $oCat
2631
     *
2632
     * @return string
2633
     */
2634
    public function _getCatidWhereSequence($nCatid, $logicOp = 'OR', $oCat = null)
2635
    {
2636
        $sqlWherefilter = '';
2637
2638
        if (!isset($oCat)) {
2639
            $oCat = new PMF_Category($this->_config);
2640
        }
2641
        $aChildren = array_values($oCat->getChildren($nCatid));
2642
2643
        foreach ($aChildren as $catid) {
2644
            $sqlWherefilter .= ' '.$logicOp.' fcr.category_id = '.$catid;
2645
            $sqlWherefilter .= $this->_getCatidWhereSequence($catid, 'OR', $oCat);
2646
        }
2647
2648
        return $sqlWherefilter;
2649
    }
2650
2651
    /**
2652
     * Build the SQL query for retrieving faq records according to the constraints provided.
2653
     *
2654
     * @param   $QueryType
2655
     * @param   $nCatid
2656
     * @param   $bDownwards
2657
     * @param   $lang
2658
     * @param   $date
2659
     * @param   $faqid
2660
     *
2661
     * @return array
2662
     */
2663
    private function _getSQLQuery($QueryType, $nCatid, $bDownwards, $lang, $date, $faqid = 0)
2664
    {
2665
        $now = date('YmdHis');
2666
        $query = sprintf("
2667
            SELECT
2668
                fd.id AS id,
2669
                fd.solution_id AS solution_id,
2670
                fd.revision_id AS revision_id,
2671
                fd.lang AS lang,
2672
                fcr.category_id AS category_id,
2673
                fd.active AS active,
2674
                fd.sticky AS sticky,
2675
                fd.keywords AS keywords,
2676
                fd.thema AS thema,
2677
                fd.content AS content,
2678
                fd.author AS author,
2679
                fd.email AS email,
2680
                fd.comment AS comment,
2681
                fd.updated AS updated,
2682
                fd.notes AS notes,
2683
                fv.visits AS visits,
2684
                fv.last_visit AS last_visit
2685
            FROM
2686
                %sfaqdata fd,
2687
                %sfaqvisits fv,
2688
                %sfaqcategoryrelations fcr
2689
            WHERE
2690
                fd.id = fcr.record_id
2691
            AND
2692
                fd.lang = fcr.record_lang
2693
            AND
2694
                fd.date_start <= '%s'
2695
            AND
2696
                fd.date_end   >= '%s'
2697
            AND ",
2698
            PMF_Db::getTablePrefix(),
2699
            PMF_Db::getTablePrefix(),
2700
            PMF_Db::getTablePrefix(),
2701
            $now,
2702
            $now);
2703
        // faqvisits data selection
2704
        if (!empty($faqid)) {
2705
            // Select ONLY the faq with the provided $faqid
2706
            $query .= "fd.id = '".$faqid."' AND ";
2707
        }
2708
        $query .= 'fd.id = fv.id
2709
            AND
2710
                fd.lang = fv.lang';
2711
        $needAndOp = true;
2712
        if ((!empty($nCatid)) && is_int($nCatid) && $nCatid > 0) {
2713
            if ($needAndOp) {
2714
                $query .= ' AND';
2715
            }
2716
            $query .= ' (fcr.category_id = '.$nCatid;
2717
            if ($bDownwards) {
2718
                $query .= $this->_getCatidWhereSequence($nCatid, 'OR');
2719
            }
2720
            $query .= ')';
2721
            $needAndOp = true;
2722
        }
2723 View Code Duplication
        if ((!empty($date)) && PMF_Utils::isLikeOnPMFDate($date)) {
2724
            if ($needAndOp) {
2725
                $query .= ' AND';
2726
            }
2727
            $query .= " fd.updated LIKE '".$date."'";
2728
            $needAndOp = true;
2729
        }
2730 View Code Duplication
        if ((!empty($lang)) && PMF_Utils::isLanguage($lang)) {
2731
            if ($needAndOp) {
2732
                $query .= ' AND';
2733
            }
2734
            $query .= " fd.lang = '".$lang."'";
2735
            $needAndOp = true;
2736
        }
2737
        switch ($QueryType) {
2738
            case FAQ_QUERY_TYPE_APPROVAL:
2739
                if ($needAndOp) {
2740
                    $query .= ' AND';
2741
                }
2742
                $query .= " fd.active = '".FAQ_SQL_ACTIVE_NO."'";
2743
                break;
2744
            case FAQ_QUERY_TYPE_EXPORT_PDF:
2745
            case FAQ_QUERY_TYPE_EXPORT_XHTML:
2746
            case FAQ_QUERY_TYPE_EXPORT_XML:
2747
                if ($needAndOp) {
2748
                    $query .= ' AND';
2749
                }
2750
                $query .= " fd.active = '".FAQ_SQL_ACTIVE_YES."'";
2751
                break;
2752
            default:
2753
                if ($needAndOp) {
2754
                    $query .= ' AND';
2755
                }
2756
                $query .= " fd.active = '".FAQ_SQL_ACTIVE_YES."'";
2757
                break;
2758
        }
2759
        // Sort criteria
2760
        switch ($QueryType) {
2761
            case FAQ_QUERY_TYPE_EXPORT_PDF:
2762
            case FAQ_QUERY_TYPE_EXPORT_XHTML:
2763
            case FAQ_QUERY_TYPE_EXPORT_XML:
2764
                $query .= "\nORDER BY fcr.category_id, fd.id";
2765
                break;
2766
            case FAQ_QUERY_TYPE_RSS_LATEST:
2767
                $query .= "\nORDER BY fd.updated DESC";
2768
                break;
2769
            default:
2770
                // Normal ordering
2771
                $query .= "\nORDER BY fcr.category_id, fd.id";
2772
                break;
2773
        }
2774
2775
        return $query;
2776
    }
2777
2778
    /**
2779
     * Adds the record permissions for users and groups.
2780
     *
2781
     * @param string $mode     'group' or 'user'
2782
     * @param int    $recordId ID of the current record
2783
     * @param array  $ids      Array of group or user IDs
2784
     *
2785
     * @return bool
2786
     */
2787
    public function addPermission($mode, $recordId, $ids)
2788
    {
2789
        if ('user' !== $mode && 'group' !== $mode) {
2790
            return false;
2791
        }
2792
2793
        foreach ($ids as $id) {
2794
            $query = sprintf('
2795
            INSERT INTO
2796
                %sfaqdata_%s
2797
            (record_id, %s_id)
2798
                VALUES
2799
            (%d, %d)',
2800
                PMF_Db::getTablePrefix(),
2801
                $mode,
2802
                $mode,
2803
                $recordId,
2804
                $id
2805
            );
2806
2807
            $this->_config->getDb()->query($query);
2808
        }
2809
2810
        return true;
2811
    }
2812
2813
    /**
2814
     * Deletes the record permissions for users and groups.
2815
     *
2816
     * @param string $mode      'group' or 'user'
2817
     * @param int    $record_id ID of the current record
2818
     *
2819
     * @return bool
2820
     *
2821
     * @author  Thorsten Rinne <[email protected]>
2822
     */
2823 View Code Duplication
    public function deletePermission($mode, $record_id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
2824
    {
2825
        if (!($mode == 'user' || $mode == 'group')) {
2826
            return false;
2827
        }
2828
        if (!is_int($record_id)) {
2829
            return false;
2830
        }
2831
2832
        $query = sprintf('
2833
            DELETE FROM
2834
                %sfaqdata_%s
2835
            WHERE
2836
                record_id = %d',
2837
            PMF_Db::getTablePrefix(),
2838
            $mode,
2839
            $record_id);
2840
        $this->_config->getDb()->query($query);
2841
2842
        return true;
2843
    }
2844
2845
    /**
2846
     * Returns the record permissions for users and groups.
2847
     *
2848
     * @param string $mode     'group' or 'user'
2849
     * @param int    $recordId
2850
     *
2851
     * @return array
2852
     */
2853
    public function getPermission($mode, $recordId)
2854
    {
2855
        $permissions = [];
2856
2857
        if (!($mode == 'user' || $mode == 'group')) {
2858
            return false;
2859
        }
2860
2861
        $query = sprintf('
2862
            SELECT
2863
                %s_id AS permission
2864
            FROM
2865
                %sfaqdata_%s
2866
            WHERE
2867
                record_id = %d',
2868
            $mode,
2869
            PMF_Db::getTablePrefix(),
2870
            $mode,
2871
            (int) $recordId);
2872
2873
        $result = $this->_config->getDb()->query($query);
2874
2875 View Code Duplication
        if ($this->_config->getDb()->numRows($result) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
2876
            while (($row = $this->_config->getDb()->fetchObject($result))) {
2877
                $permissions[] = (int) $row->permission;
2878
            }
2879
        }
2880
2881
        return $permissions;
2882
    }
2883
2884
    /**
2885
     * Returns all records of one category.
2886
     *
2887
     * @param int $category
2888
     *
2889
     * @return string
2890
     */
2891
    public function showAllRecordsWoPaging($category)
2892
    {
2893
        global $sids;
2894
2895
        $now = date('YmdHis');
2896
        $query = '
2897
            SELECT
2898
                fd.id AS id,
2899
                fd.lang AS lang,
2900
                fd.thema AS thema,
2901
                fcr.category_id AS category_id,
2902
                fv.visits AS visits
2903
            FROM
2904
                '.PMF_Db::getTablePrefix().'faqdata fd
2905
            LEFT JOIN
2906
                '.PMF_Db::getTablePrefix().'faqcategoryrelations fcr
2907
            ON
2908
                fd.id = fcr.record_id
2909
            AND
2910
                fd.lang = fcr.record_lang
2911
            LEFT JOIN
2912
                '.PMF_Db::getTablePrefix().'faqvisits fv
2913
            ON
2914
                fd.id = fv.id
2915
            AND
2916
                fv.lang = fd.lang
2917
            LEFT JOIN
2918
                '.PMF_Db::getTablePrefix().'faqdata_group fdg
2919
            ON
2920
                fd.id = fdg.record_id
2921
            LEFT JOIN
2922
                '.PMF_Db::getTablePrefix().'faqdata_user fdu
2923
            ON
2924
                fd.id = fdu.record_id
2925
            WHERE
2926
                fd.date_start <= \''.$now.'\'
2927
            AND
2928
                fd.date_end   >= \''.$now.'\'
2929
            AND
2930
                fd.active = \'yes\'
2931
            AND
2932
                fcr.category_id = '.$category.'
2933
            AND
2934
                fd.lang = \''.$this->_config->getLanguage()->getLanguage().'\'
2935
            GROUP BY
2936
                fd.id,fd.lang,fcr.category_id,fv.visits
2937
            ORDER BY
2938
                fd.id';
2939
2940
        $result = $this->_config->getDb()->query($query);
2941
2942
        $output = '<ul class="phpmyfaq_ul">';
2943
2944
        while (($row = $this->_config->getDb()->fetchObject($result))) {
2945
            $title = $row->thema;
2946
            $url = sprintf(
2947
                '%s?%saction=artikel&amp;cat=%d&amp;id=%d&amp;artlang=%s',
2948
                PMF_Link::getSystemRelativeUri(),
2949
                $sids,
2950
                $row->category_id,
2951
                $row->id,
2952
                $row->lang
2953
            );
2954
2955
            $oLink = new PMF_Link($url, $this->_config);
2956
            $oLink->itemTitle = $row->thema;
2957
            $oLink->text = $title;
2958
            $oLink->tooltip = $title;
2959
            $listItem = sprintf('<li>%s</li>', $oLink->toHtmlAnchor(), $this->pmf_lang['msgViews']);
2960
            $listItem = '<li>'.$oLink->toHtmlAnchor().'</li>';
2961
2962
            $output .= $listItem;
2963
        }
2964
2965
        $output .= '</ul>';
2966
2967
        return $output;
2968
    }
2969
2970
    /**
2971
     * Prints the open questions as a XHTML table.
2972
     *
2973
     * @return string
2974
     */
2975
    public function printOpenQuestions()
2976
    {
2977
        global $sids, $category;
2978
2979
        $date = new PMF_Date($this->_config);
2980
        $mail = new PMF_Mail($this->_config);
2981
2982
        $query = sprintf("
2983
            SELECT
2984
                COUNT(id) AS num
2985
            FROM
2986
                %sfaqquestions
2987
            WHERE
2988
                lang = '%s'
2989
            AND
2990
                is_visible != 'Y'",
2991
            PMF_Db::getTablePrefix(),
2992
            $this->_config->getLanguage()->getLanguage()
2993
        );
2994
2995
        $result = $this->_config->getDb()->query($query);
2996
        $row = $this->_config->getDb()->fetchObject($result);
2997
        $numOfInvisibles = $row->num;
2998
2999
        if ($numOfInvisibles > 0) {
3000
            $extraout = sprintf(
3001
                '<tr><td colspan="3"><small>%s %s</small></td></tr>',
3002
                $this->pmf_lang['msgQuestionsWaiting'],
3003
                $numOfInvisibles
3004
            );
3005
        } else {
3006
            $extraout = '';
3007
        }
3008
3009
        $query = sprintf("
3010
            SELECT
3011
                *
3012
            FROM
3013
                %sfaqquestions
3014
            WHERE
3015
                lang = '%s'
3016
            AND
3017
                is_visible = 'Y'
3018
            ORDER BY
3019
                created ASC",
3020
            PMF_Db::getTablePrefix(),
3021
            $this->_config->getLanguage()->getLanguage()
3022
        );
3023
3024
        $result = $this->_config->getDb()->query($query);
3025
        $output = '';
3026
3027
        if ($result && $this->_config->getDb()->numRows($result) > 0) {
3028
            while ($row = $this->_config->getDb()->fetchObject($result)) {
3029
                $output .= '<tr class="openquestions">';
3030
                $output .= sprintf(
3031
                    '<td><small>%s</small><br /><a href="mailto:%s">%s</a></td>',
3032
                    $date->format(PMF_Date::createIsoDate($row->created)),
3033
                    $mail->safeEmail($row->email),
3034
                    $row->username
3035
                );
3036
                $output .= sprintf(
3037
                    '<td><strong>%s:</strong><br />%s</td>',
3038
                    isset($category->categoryName[$row->category_id]['name']) ? $category->categoryName[$row->category_id]['name'] : '',
3039
                    strip_tags($row->question)
3040
                );
3041
                if ($this->_config->get('records.enableCloseQuestion') && $row->answer_id) {
3042
                    $output .= sprintf(
3043
                        '<td><a id="PMF_openQuestionAnswered" href="?%saction=artikel&amp;cat=%d&amp;id=%d">%s</a></td>',
3044
                        $sids,
3045
                        $row->category_id,
3046
                        $row->answer_id,
3047
                        $this->pmf_lang['msg2answerFAQ']
3048
                    );
3049
                } else {
3050
                    $output .= sprintf(
3051
                        '<td><a class="btn btn-primary" href="?%saction=add&amp;question=%d&amp;cat=%d">%s</a></td>',
3052
                        $sids,
3053
                        $row->id,
3054
                        $row->category_id,
3055
                        $this->pmf_lang['msg2answer']
3056
                    );
3057
                }
3058
                $output .= '</tr>';
3059
            }
3060
        } else {
3061
            $output = sprintf(
3062
                '<tr><td colspan="3">%s</td></tr>',
3063
                $this->pmf_lang['msgNoQuestionsAvailable']
3064
            );
3065
        }
3066
3067
        return $output.$extraout;
3068
    }
3069
3070
    /**
3071
     * Set or unset a faq item flag.
3072
     *
3073
     * @param int    $id   Record id
3074
     * @param string $lang language code which is valid with Language::isASupportedLanguage
3075
     * @param bool   $flag weither or not the record is set to sticky
3076
     * @param string $type type of the flag to set, use the column name
3077
     *
3078
     * @return bool
3079
     */
3080
    public function updateRecordFlag($id, $lang, $flag, $type)
3081
    {
3082
        $retval = false;
3083
3084
        switch ($type) {
3085
            case 'sticky':
3086
                $flag = ($flag === 'checked' ? 1 : 0);
3087
                break;
3088
3089
            case 'active':
3090
                $flag = ($flag === 'checked' ? "'yes'" : "'no'");
3091
                break;
3092
3093
            default:
3094
                // This is because we would run into unknown db column
3095
                $flag = null;
3096
                break;
3097
        }
3098
3099
        if (null !== $flag) {
3100
            $update = sprintf("
3101
                UPDATE 
3102
                    %sfaqdata 
3103
                SET 
3104
                    %s = %s 
3105
                WHERE 
3106
                    id = %d 
3107
                AND 
3108
                    lang = '%s'",
3109
                PMF_Db::getTablePrefix(),
3110
                $type,
3111
                $flag,
3112
                $id,
3113
                $lang
3114
            );
3115
3116
            $retval = (bool) $this->_config->getDb()->query($update);
3117
        }
3118
3119
        return $retval;
3120
    }
3121
3122
    /**
3123
     * Returns the sticky records with URL and Title.
3124
     *
3125
     * @return array
3126
     */
3127
    private function getStickyRecordsData()
3128
    {
3129
        global $sids;
3130
3131
        $now = date('YmdHis');
3132
        $query = sprintf("
3133
            SELECT
3134
                fd.id AS id,
3135
                fd.lang AS lang,
3136
                fd.thema AS thema,
3137
                fcr.category_id AS category_id
3138
            FROM
3139
                %sfaqdata fd
3140
            LEFT JOIN
3141
                %sfaqcategoryrelations fcr
3142
            ON
3143
                fd.id = fcr.record_id
3144
            AND
3145
                fd.lang = fcr.record_lang
3146
            LEFT JOIN
3147
                %sfaqdata_group AS fdg
3148
            ON
3149
                fd.id = fdg.record_id
3150
            LEFT JOIN
3151
                %sfaqdata_user AS fdu
3152
            ON
3153
                fd.id = fdu.record_id
3154
            WHERE
3155
                fd.lang = '%s'
3156
            AND 
3157
                fd.date_start <= '%s'
3158
            AND 
3159
                fd.date_end   >= '%s'
3160
            AND 
3161
                fd.active = 'yes'
3162
            AND 
3163
                fd.sticky = 1
3164
            %s",
3165
            PMF_Db::getTablePrefix(),
3166
            PMF_Db::getTablePrefix(),
3167
            PMF_Db::getTablePrefix(),
3168
            PMF_Db::getTablePrefix(),
3169
            $this->_config->getLanguage()->getLanguage(),
3170
            $now,
3171
            $now,
3172
            $this->queryPermission($this->groupSupport)
3173
        );
3174
3175
        $result = $this->_config->getDb()->query($query);
3176
        $sticky = [];
3177
        $data = [];
3178
3179
        $oldId = 0;
3180
        while (($row = $this->_config->getDb()->fetchObject($result))) {
3181
            if ($oldId != $row->id) {
3182
                $data['thema'] = $row->thema;
3183
3184
                $title = $row->thema;
3185
                $url = sprintf(
3186
                    '%s?%saction=artikel&amp;cat=%d&amp;id=%d&amp;artlang=%s',
3187
                    PMF_Link::getSystemRelativeUri(),
3188
                    $sids,
3189
                    $row->category_id,
3190
                    $row->id,
3191
                    $row->lang
3192
                );
3193
                $oLink = new PMF_Link($url, $this->_config);
3194
                $oLink->itemTitle = $row->thema;
3195
                $oLink->tooltip = $title;
3196
                $data['url'] = $oLink->toString();
3197
3198
                $sticky[] = $data;
3199
            }
3200
            $oldId = $row->id;
3201
        }
3202
3203
        return $sticky;
3204
    }
3205
3206
    /**
3207
     * Prepares and returns the sticky records for the frontend.
3208
     *
3209
     * @return array
3210
     */
3211
    public function getStickyRecords()
3212
    {
3213
        $result = $this->getStickyRecordsData();
3214
        $output = [];
3215
3216
        if (count($result) > 0) {
3217
            foreach ($result as $row) {
3218
                $output[] = array(
3219
                    'title' => PMF_Utils::makeShorterText($row['thema'], 8),
3220
                    'preview' => $row['thema'],
3221
                    'url' => $row['url'],
3222
                );
3223
            }
3224
        } else {
3225
            $output['error'] = sprintf('<li>%s</li>', $this->pmf_lang['err_noTopTen']);
3226
        }
3227
        if (!isset($output['error'])) {
3228
            $html = '';
3229
            foreach ($output as $entry) {
3230
                $html .= sprintf(
3231
                    '<li><a class="sticky-faqs" data-toggle="tooltip" data-placement="top" title="%s" href="%s">%s</a></li>',
3232
                    $entry['preview'],
3233
                    $entry['url'],
3234
                    $entry['title']
3235
                );
3236
            }
3237
            $output['html'] = $html;
3238
        }
3239
3240
        return $output;
3241
    }
3242
3243
    /**
3244
     * Updates field answer_id in faqquestion.
3245
     *
3246
     * @param int $openQuestionId
3247
     * @param int $faqId
3248
     * @param int $categoryId
3249
     *
3250
     * @return bool
3251
     */
3252 View Code Duplication
    public function updateQuestionAnswer($openQuestionId, $faqId, $categoryId)
3253
    {
3254
        $query = sprintf(
3255
            'UPDATE %sfaqquestions SET answer_id = %d, category_id= %d WHERE id= %d',
3256
            PMF_Db::getTablePrefix(),
3257
            $faqId,
3258
            $categoryId,
3259
            $openQuestionId
3260
        );
3261
3262
        return $this->_config->getDb()->query($query);
3263
    }
3264
3265
    /**
3266
     * Returns a part of a query to check permissions.
3267
     *
3268
     * @param bool $hasGroupSupport
3269
     *
3270
     * @return string
3271
     */
3272
    protected function queryPermission($hasGroupSupport = false)
3273
    {
3274
        if ($hasGroupSupport) {
3275
            if (-1 === $this->user) {
3276
                return sprintf(
3277
                    'AND fdg.group_id IN (%s)',
3278
                    implode(', ', $this->groups),
3279
                    $this->user,
3280
                    implode(', ', $this->groups));
3281 View Code Duplication
            } else {
3282
                return sprintf(
3283
                    'AND ( fdg.group_id IN (%s) OR (fdu.user_id = %d OR fdg.group_id IN (%s)) )',
3284
                    implode(', ', $this->groups),
3285
                    $this->user,
3286
                    implode(', ', $this->groups)
3287
                );
3288
            }
3289
        } else {
3290
            if (-1 !== $this->user) {
3291
                return sprintf(
3292
                    'AND ( fdu.user_id = %d OR fdu.user_id = -1 )',
3293
                    $this->user
3294
                );
3295
            } else {
3296
                return sprintf(
3297
                    'AND fdu.user_id = -1',
3298
                    $this->user
3299
                );
3300
            }
3301
        }
3302
    }
3303
}
3304