Passed
Push — master ( 2744eb...81ba93 )
by Michael
02:46
created

class/Faq.php (1 issue)

1
<?php namespace XoopsModules\Smartfaq;
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 23 and the first side effect is on line 12.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * Module: SmartFAQ
5
 * Author: The SmartFactory <www.smartfactory.ca>
6
 * Licence: GNU
7
 */
8
9
use XoopsModules\Smartfaq;
10
use XoopsModules\Smartfaq\Constants;
11
/** @var Smartfaq\Helper $helper */
12
$helper = Smartfaq\Helper::getInstance();
13
14
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
15
16
//require_once XOOPS_ROOT_PATH . '/modules/smartfaq/class/category.php';
17
18
19
20
/**
21
 * Class Faq
22
 */
23
class Faq extends \XoopsObject
24
{
25
26
    /**
27
     * @var Smartfaq\Category
28
     * @access private
29
     */
30
    private $category = null;
31
32
    /**
33
     * @var Answer
34
     * @access private
35
     */
36
    private $answer = null;
37
38
    /**
39
     * @var array
40
     * @access private
41
     */
42
    private $_notifications = null;
43
    // TODO : Create a seperated class for notifications
44
45
    /**
46
     * @var array
47
     * @access private
48
     */
49
    private $groups_read = null;
50
51
    /**
52
     * @var object
53
     * @access private
54
     */
55
    // Is this still usefull??
56
    private $_smartModule = null;
57
    private $_smartModuleConfig;
58
59
    /**
60
     * constructor
61
     * @param null $id
62
     */
63
    public function __construct($id = null)
64
    {
65
        $this->db = \XoopsDatabaseFactory::getDatabaseConnection();
66
        $this->initVar('faqid', XOBJ_DTYPE_INT, -1, false);
67
        $this->initVar('categoryid', XOBJ_DTYPE_INT, 0, false);
68
        $this->initVar('question', XOBJ_DTYPE_TXTBOX, null, true, 100000);
69
        $this->initVar('howdoi', XOBJ_DTYPE_TXTBOX, null, false, 255);
70
        $this->initVar('diduno', XOBJ_DTYPE_TXTBOX, null, false, 255);
71
        $this->initVar('uid', XOBJ_DTYPE_INT, 0, false);
72
        $this->initVar('datesub', XOBJ_DTYPE_INT, null, false);
73
        $this->initVar('status', XOBJ_DTYPE_INT, -1, false);
74
        $this->initVar('counter', XOBJ_DTYPE_INT, 0, false);
75
        $this->initVar('weight', XOBJ_DTYPE_INT, 0, false);
76
        $this->initVar('html', XOBJ_DTYPE_INT, 1, false);
77
        $this->initVar('smiley', XOBJ_DTYPE_INT, 1, false);
78
        $this->initVar('image', XOBJ_DTYPE_INT, 1, false);
79
        $this->initVar('linebreak', XOBJ_DTYPE_INT, 1, false);
80
        $this->initVar('xcodes', XOBJ_DTYPE_INT, 1, false);
81
        $this->initVar('cancomment', XOBJ_DTYPE_INT, 1, false);
82
        $this->initVar('comments', XOBJ_DTYPE_INT, 0, false);
83
        $this->initVar('notifypub', XOBJ_DTYPE_INT, 1, false);
84
        $this->initVar('modulelink', XOBJ_DTYPE_TXTBOX, 'None', false, 50);
85
        $this->initVar('contextpage', XOBJ_DTYPE_TXTBOX, null, false, 255);
86
        $this->initVar('exacturl', XOBJ_DTYPE_INT, 0, false);
87
        $this->initVar('partialview', XOBJ_DTYPE_INT, 0, false);
88
89
        if (null !== $id) {
90
            $faqHandler = new Smartfaq\FaqHandler($this->db);
91
            $faq        = $faqHandler->get($id);
92
            foreach ($faq->vars as $k => $v) {
93
                $this->assignVar($k, $v['value']);
94
            }
95
            $this->assignOtherProperties();
96
        }
97
    }
98
99
    public function assignOtherProperties()
100
    {
101
        $smartModule = Smartfaq\Utility::getModuleInfo();
102
        $module_id   = $smartModule->getVar('mid');
103
104
        $gpermHandler = xoops_getHandler('groupperm');
105
106
        $this->category    = new Smartfaq\Category($this->getVar('categoryid'));
107
        $this->groups_read = $gpermHandler->getGroupIds('item_read', $this->faqid(), $module_id);
108
    }
109
110
    /**
111
     * @return bool
112
     */
113
    public function checkPermission()
114
    {
115
//        require_once XOOPS_ROOT_PATH . '/modules/smartfaq/include/functions.php';
116
117
        $userIsAdmin = Smartfaq\Utility::userIsAdmin();
118
        if ($userIsAdmin) {
119
            return true;
120
        }
121
        /** @var Smartfaq\PermissionHandler $smartPermHandler */
122
        $smartPermHandler = Smartfaq\Helper::getInstance()->getHandler('Permission');
123
//        $smartPermHandler = xoops_getModuleHandler('permission', 'smartfaq');
124
125
        $faqsGranted = $smartPermHandler->getPermissions('item');
126
        if (in_array($this->categoryid(), $faqsGranted)) {
127
            $ret = true;
128
        }
129
130
        return $ret;
131
    }
132
133
    /**
134
     * @return array
135
     */
136
    public function getGroups_read()
137
    {
138
        if (count($this->groups_read) < 1) {
139
            $this->assignOtherProperties();
140
        }
141
142
        return $this->groups_read;
143
    }
144
145
    /**
146
     * @param array $groups_read
147
     */
148
    public function setGroups_read($groups_read = ['0'])
149
    {
150
        $this->groups_read = $groups_read;
151
    }
152
153
    /**
154
     * @return mixed
155
     */
156
    public function faqid()
157
    {
158
        return $this->getVar('faqid');
159
    }
160
161
    /**
162
     * @return mixed
163
     */
164
    public function categoryid()
165
    {
166
        return $this->getVar('categoryid');
167
    }
168
169
    /**
170
     * @return Smartfaq\Category
171
     */
172
    public function category()
173
    {
174
        return $this->category;
175
    }
176
177
    /**
178
     * @param  int    $maxLength
179
     * @param  string $format
180
     * @return mixed|string
181
     */
182
    public function question($maxLength = 0, $format = 'S')
183
    {
184
        $ret = $this->getVar('question', $format);
185
        if (('s' === $format) || ('S' === $format) || ('show' === $format)) {
186
            $myts = \MyTextSanitizer::getInstance();
187
            $ret  = $myts->displayTarea($ret);
188
        }
189
        if (0 != $maxLength) {
190
            if (!XOOPS_USE_MULTIBYTES) {
191
                if (strlen($ret) >= $maxLength) {
192
                    $ret = substr($ret, 0, $maxLength - 1) . '...';
193
                }
194
            }
195
        }
196
197
        return $ret;
198
    }
199
200
    /**
201
     * @param  string $format
202
     * @return mixed
203
     */
204
    public function howdoi($format = 'S')
205
    {
206
        $ret = $this->getVar('howdoi', $format);
207
        if (('s' === $format) || ('S' === $format) || ('show' === $format)) {
208
            $myts = \MyTextSanitizer::getInstance();
209
            $ret  = $myts->displayTarea($ret);
210
        }
211
212
        return $ret;
213
    }
214
215
    /**
216
     * @param  string $format
217
     * @return mixed
218
     */
219
    public function diduno($format = 'S')
220
    {
221
        $ret = $this->getVar('diduno', $format);
222
        if (('s' === $format) || ('S' === $format) || ('show' === $format)) {
223
            $myts = \MyTextSanitizer::getInstance();
224
            $ret  = $myts->displayTarea($ret);
225
        }
226
227
        return $ret;
228
    }
229
230
    /**
231
     * @return mixed
232
     */
233
    public function uid()
234
    {
235
        return $this->getVar('uid');
236
    }
237
238
    /**
239
     * @param  string $dateFormat
240
     * @param  string $format
241
     * @return string
242
     */
243
    public function datesub($dateFormat = 'none', $format = 'S')
244
    {
245
        if ('none' === $dateFormat) {
246
            $smartConfig = Smartfaq\Utility::getModuleConfig();
247
            $dateFormat  = $smartConfig['dateformat'];
248
        }
249
250
        return formatTimestamp($this->getVar('datesub', $format), $dateFormat);
251
    }
252
253
    /**
254
     * @return mixed
255
     */
256
    public function status()
257
    {
258
        return $this->getVar('status');
259
    }
260
261
    /**
262
     * @return mixed
263
     */
264
    public function counter()
265
    {
266
        return $this->getVar('counter');
267
    }
268
269
    /**
270
     * @return mixed
271
     */
272
    public function weight()
273
    {
274
        return $this->getVar('weight');
275
    }
276
277
    /**
278
     * @return mixed
279
     */
280
    public function html()
281
    {
282
        return $this->getVar('html');
283
    }
284
285
    /**
286
     * @return mixed
287
     */
288
    public function smiley()
289
    {
290
        return $this->getVar('smiley');
291
    }
292
293
    /**
294
     * @return mixed
295
     */
296
    public function xcodes()
297
    {
298
        return $this->getVar('xcodes');
299
    }
300
301
    /**
302
     * @return mixed
303
     */
304
    public function cancomment()
305
    {
306
        return $this->getVar('cancomment');
307
    }
308
309
    /**
310
     * @return mixed
311
     */
312
    public function comments()
313
    {
314
        return $this->getVar('comments');
315
    }
316
317
    /**
318
     * @return mixed
319
     */
320
    public function notifypub()
321
    {
322
        return $this->getVar('notifypub');
323
    }
324
325
    /**
326
     * @param  string $format
327
     * @return mixed
328
     */
329
    public function modulelink($format = 'S')
330
    {
331
        return $this->getVar('modulelink', $format);
332
    }
333
334
    /**
335
     * @param  string $format
336
     * @return mixed
337
     */
338
    public function contextpage($format = 'S')
339
    {
340
        return $this->getVar('contextpage', $format);
341
    }
342
343
    /**
344
     * @return mixed
345
     */
346
    public function exacturl()
347
    {
348
        return $this->getVar('exacturl');
349
    }
350
351
    /**
352
     * @return mixed
353
     */
354
    public function partialview()
355
    {
356
        return $this->getVar('partialview');
357
    }
358
359
    /**
360
     * @param  int $realName
361
     * @return string
362
     */
363
    public function posterName($realName = -1)
364
    {
365
        if (-1 == $realName) {
366
            $smartConfig = Smartfaq\Utility::getModuleConfig();
367
            $realName    = $smartConfig['userealname'];
368
        }
369
370
        return Smartfaq\Utility::getLinkedUnameFromId($this->uid(), $realName);
371
    }
372
373
    /**
374
     * @return mixed|object|Smartfaq\Answer
375
     */
376
    public function answer()
377
    {
378
        $answerHandler = new Smartfaq\AnswerHandler($this->db);
379
        switch ($this->status()) {
380
            case Constants::SF_STATUS_SUBMITTED:
381
                $theAnswers = $answerHandler->getAllAnswers($this->faqid(), Constants::SF_AN_STATUS_APPROVED, 1, 0);
382
                //echo "test";
383
                //exit;
384
                $this->answer =& $theAnswers[0];
385
                break;
386
387
            case Constants::SF_STATUS_ANSWERED:
388
                $theAnswers = $answerHandler->getAllAnswers($this->faqid(), Constants::SF_AN_STATUS_PROPOSED, 1, 0);
389
                //echo "test";
390
                //exit;
391
                $this->answer =& $theAnswers[0];
392
                break;
393
394
            case Constants::SF_STATUS_PUBLISHED:
395
            case Constants::SF_STATUS_NEW_ANSWER:
396
            case Constants::SF_STATUS_OFFLINE:
397
                $this->answer = $answerHandler->getOfficialAnswer($this->faqid());
398
                break;
399
400
            case Constants::SF_STATUS_ASKED:
401
                $this->answer = $answerHandler->create();
402
                break;
403
            case Constants::SF_STATUS_OPENED:
404
                $this->answer = $answerHandler->create();
405
                break;
406
        }
407
408
        if ($this->answer) {
409
            $this->answer->setVar('dohtml', $this->getVar('html'));
410
            $this->answer->setVar('doxcode', $this->getVar('xcodes'));
411
            $this->answer->setVar('dosmiley', $this->getVar('smiley'));
412
            $this->answer->setVar('doimage', $this->getVar('image'));
413
            $this->answer->setVar('dobr', $this->getVar('linebreak'));
414
        }
415
416
        return $this->answer;
417
    }
418
419
    /**
420
     * @return array
421
     */
422
    public function getAllAnswers()
423
    {
424
        $answerHandler = new Smartfaq\AnswerHandler($this->db);
425
426
        return $answerHandler->getAllAnswers($this->faqid());
427
    }
428
429
    /**
430
     * @return bool
431
     */
432
    public function updateCounter()
433
    {
434
        $faqHandler = new Smartfaq\FaqHandler($this->db);
435
436
        return $faqHandler->updateCounter($this->faqid());
437
    }
438
439
    /**
440
     * @param  bool $force
441
     * @return bool
442
     */
443
    public function store($force = true)
444
    {
445
        $faqHandler = new Smartfaq\FaqHandler($this->db);
446
447
        return $faqHandler->insert($this, $force);
448
    }
449
450
    /**
451
     * @return mixed
452
     */
453
    public function getCategoryName()
454
    {
455
        if (!isset($this->category)) {
456
            $this->category = new Smartfaq\Category($this->getVar('categoryid'));
457
        }
458
459
        return $this->category->name();
460
    }
461
462
    /**
463
     * @param array $notifications
464
     */
465
    public function sendNotifications($notifications = [])
466
    {
467
        $smartModule = Smartfaq\Utility::getModuleInfo();
468
469
        $myts                = \MyTextSanitizer::getInstance();
470
        $notificationHandler = xoops_getHandler('notification');
471
        //$categoryObj = $this->category();
472
473
        $tags                  = [];
474
        $tags['MODULE_NAME']   = $myts->displayTarea($smartModule->getVar('name'));
475
        $tags['FAQ_NAME']      = $this->question();
476
        $tags['CATEGORY_NAME'] = $this->getCategoryName();
477
        $tags['CATEGORY_URL']  = XOOPS_URL . '/modules/' . $smartModule->getVar('dirname') . '/category.php?categoryid=' . $this->categoryid();
478
        $tags['FAQ_QUESTION']  = $this->question();
479
        $answerObj             = $this->answer();
480
        if (is_object($answerObj)) {
481
            // TODO : Not sure about the 'formpreview' ...
482
            $tags['FAQ_ANSWER'] = $answerObj->answer('formpreview');
483
        }
484
        $tags['DATESUB'] = $this->datesub();
485
486
        foreach ($notifications as $notification) {
487
            switch ($notification) {
488
                case Constants::SF_NOT_FAQ_PUBLISHED:
489
                    $tags['FAQ_URL'] = XOOPS_URL . '/modules/' . $smartModule->getVar('dirname') . '/faq.php?faqid=' . $this->faqid();
490
491
                    $notificationHandler->triggerEvent('global_faq', 0, 'published', $tags);
492
                    $notificationHandler->triggerEvent('category_faq', $this->categoryid(), 'published', $tags);
493
                    $notificationHandler->triggerEvent('faq', $this->faqid(), 'approved', $tags);
494
                    break;
495
496
                case Constants::SF_NOT_FAQ_SUBMITTED:
497
                    $tags['WAITINGFILES_URL'] = XOOPS_URL . '/modules/' . $smartModule->getVar('dirname') . '/admin/faq.php?faqid=' . $this->faqid();
498
                    $notificationHandler->triggerEvent('global_faq', 0, 'submitted', $tags);
499
                    $notificationHandler->triggerEvent('category_faq', $this->categoryid(), 'submitted', $tags);
500
                    break;
501
502
                case Constants::SF_NOT_QUESTION_PUBLISHED:
503
                    $tags['FAQ_URL'] = XOOPS_URL . '/modules/' . $smartModule->getVar('dirname') . '/answer.php?faqid=' . $this->faqid();
504
                    $notificationHandler->triggerEvent('global_question', 0, 'published', $tags);
505
                    $notificationHandler->triggerEvent('category_question', $this->categoryid(), 'published', $tags);
506
                    $notificationHandler->triggerEvent('question', $this->faqid(), 'approved', $tags);
507
                    break;
508
509
                case Constants::SF_NOT_QUESTION_SUBMITTED:
510
                    $tags['WAITINGFILES_URL'] = XOOPS_URL . '/modules/' . $smartModule->getVar('dirname') . '/admin/question.php?op=mod&faqid=' . $this->faqid();
511
                    $notificationHandler->triggerEvent('global_question', 0, 'submitted', $tags);
512
                    $notificationHandler->triggerEvent('category_question', $this->categoryid(), 'submitted', $tags);
513
                    break;
514
515
                case Constants::SF_NOT_FAQ_REJECTED:
516
                    $notificationHandler->triggerEvent('faq', $this->faqid(), 'rejected', $tags);
517
                    break;
518
519
                case Constants::SF_NOT_NEW_ANSWER_PROPOSED:
520
                    $tags['WAITINGFILES_URL'] = XOOPS_URL . '/modules/' . $smartModule->getVar('dirname') . '/admin/answer.php?op=mod&faqid=' . $this->faqid();
521
                    $notificationHandler->triggerEvent('global_faq', 0, 'answer_proposed', $tags);
522
                    $notificationHandler->triggerEvent('category_faq', $this->categoryid(), 'answer_proposed', $tags);
523
                    break;
524
525
                case Constants::SF_NOT_NEW_ANSWER_PUBLISHED:
526
                    $notificationHandler->triggerEvent('global_faq', 0, 'answer_published', $tags);
527
                    $notificationHandler->triggerEvent('category_faq', $this->categoryid(), 'answer_published', $tags);
528
                    break;
529
530
                // TODO : I commented out this one because I'm not sure. The $this->faqid() should probably be the
531
                // answerid not the faqid....
532
                /*
533
                case Constants::SF_NOT_ANSWER_APPROVED:
534
                $notificationHandler->triggerEvent('faq', $this->faqid(), 'answer_approved', $tags);
535
                break;
536
                */
537
538
                // TODO : I commented out this one because I'm not sure. The $this->faqid() should probably be the
539
                // answerid not the faqid....
540
                /*
541
                case Constants::SF_NOT_ANSWER_REJECTED:
542
                $notificationHandler->triggerEvent('faq', $this->faqid(), 'answer_approved', $tags);
543
                break;
544
                */
545
546
                case -1:
547
                default:
548
                    break;
549
            }
550
        }
551
    }
552
553
    public function setDefaultPermissions()
554
    {
555
        $memberHandler = xoops_getHandler('member');
556
        $groups        = $memberHandler->getGroupList();
557
558
        $j         = 0;
559
        $group_ids = [];
560
        foreach (array_keys($groups) as $i) {
561
            $group_ids[$j] = $i;
562
            ++$j;
563
        }
564
        $this->groups_read = $group_ids;
565
    }
566
567
    /**
568
     * @param $group_ids
569
     */
570
    public function setPermissions($group_ids)
571
    {
572
        if (!isset($group_ids)) {
573
            $memberHandler = xoops_getHandler('member');
574
            $groups        = $memberHandler->getGroupList();
575
576
            $j         = 0;
577
            $group_ids = [];
578
            foreach (array_keys($groups) as $i) {
579
                $group_ids[$j] = $i;
580
                ++$j;
581
            }
582
        }
583
    }
584
585
    /**
586
     * @return bool
587
     */
588
    public function notLoaded()
589
    {
590
        return (-1 == $this->getVar('faqid'));
591
    }
592
593
    /**
594
     * @param  null  $answerObj
595
     * @param  array $users
596
     * @return string
597
     */
598
    public function getWhoAndWhen($answerObj = null, $users = [])
599
    {
600
        $smartModuleConfig = Smartfaq\Utility::getModuleConfig();
601
602
        $requester   = Smartfaq\Utility::getLinkedUnameFromId($this->uid(), $smartModuleConfig['userealname'], $users);
603
        $requestdate = $this->datesub();
604
605
        if ((Constants::SF_STATUS_PUBLISHED == $this->status()) || Constants::SF_STATUS_NEW_ANSWER == $this->status()) {
606
            if (null === $answerObj) {
607
                $answerObj = $this->answer();
608
            }
609
            $submitdate = $answerObj->datesub();
610
            if ($this->uid() == $answerObj->uid()) {
611
                $result = sprintf(_MD_SF_REQUESTEDANDANSWERED, $requester, $submitdate);
612
            } else {
613
                $submitter = Smartfaq\Utility::getLinkedUnameFromId($answerObj->uid(), $smartModuleConfig['userealname'], $users);
614
                $result    = sprintf(_MD_SF_REQUESTEDBYANDANSWEREDBY, $requester, $submitter, $submitdate);
615
            }
616
        } else {
617
            $result = sprintf(_MD_SF_REQUESTEDBY, $requester, $requestdate);
618
        }
619
620
        return $result;
621
    }
622
623
    /**
624
     * @return string
625
     */
626
    public function getComeFrom()
627
    {
628
        global $xoopsConfig;
629
        $text = _MD_SF_QUESTIONCOMEFROM;
630
        if ((Constants::SF_STATUS_PUBLISHED == $this->status()) || Constants::SF_STATUS_NEW_ANSWER == $this->status()) {
631
            $text = _MD_SF_FAQCOMEFROM;
632
        }
633
634
        return $text . $xoopsConfig['sitename'] . ' : <a href=' . XOOPS_URL . '/modules/smartfaq/faq.php?faqid=' . $this->faqid() . '>' . XOOPS_URL . '/modules/smartfaq/faq.php?faqid=' . $this->faqid() . '</a>';
635
    }
636
637
    /**
638
     * @param  array $faq
639
     * @param  null  $category
640
     * @param  bool  $linkInQuestion
641
     * @return array
642
     */
643
    public function toArray($faq = [], $category = null, $linkInQuestion = true)
644
    {
645
        /** @var Smartfaq\Helper $helper */
646
        $helper = Smartfaq\Helper::getInstance();
647
648
        $lastfaqsize = (int)$helper->getConfig('lastfaqsize');
649
650
        $faq['id']         = $this->faqid();
651
        $faq['categoryid'] = $this->categoryid();
652
        $faq['question']   = $this->question();
653
        $page              = (Constants::SF_STATUS_OPENED == $this->status()) ? 'answer.php' : 'faq.php';
654
655
        $faq['questionlink'] = "<a href='$page?faqid=" . $this->faqid() . "'>" . $this->question($lastfaqsize) . '</a>';
656
        if ($linkInQuestion) {
657
            $faq['fullquestionlink'] = "<a href='$page?faqid=" . $this->faqid() . "'>" . $this->question() . '</a>';
658
        } else {
659
            $faq['fullquestionlink'] = $this->question();
660
        }
661
        $faq['faqid']      = $this->faqid();
662
        $faq['counter']    = $this->counter();
663
        $faq['cancomment'] = $this->cancomment();
664
        $faq['comments']   = $this->comments();
665
        $faq['datesub']    = $this->datesub();
666
        if (null !== $category) {
667
            if (is_object($category) && 'xoopsmodules\smartfaq\category' === strtolower(get_class($category))) {
668
                $categoryObj = $category;
669
            } elseif (is_array($category)) {
670
                $categoryObj = $category[$this->categoryid()];
671
            }
672
            $faq['categoryname'] = $categoryObj->getVar('name');
673
            $faq['categorylink'] = "<a href='" . XOOPS_URL . '/modules/smartfaq/category.php?categoryid=' . $this->categoryid() . "'>" . $categoryObj->getVar('name') . '</a>';
674
        }
675
676
        return $faq;
677
    }
678
}
679