Smartfaq::storeFaq()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 48
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 27
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 48
rs 9.488
1
<?php declare(strict_types=1);
2
3
/*
4
 * You may not change or alter any portion of this comment or credits
5
 * of supporting developers from this source code or any supporting source code
6
 * which is considered copyrighted (c) material of the original comment or credit authors.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
13
/**
14
 * @copyright    {@link https://xoops.org/ XOOPS Project}
15
 * @license      {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
16
 * @author       Brian Wahoff <[email protected]>
17
 * @author       Eric Juden <[email protected]>
18
 * @author       XOOPS Development Team
19
 */
20
21
namespace XoopsModules\Xhelp\Faq;
22
23
use XoopsModules\Smartfaq\Helper as AdapterHelper;
0 ignored issues
show
Bug introduced by
The type XoopsModules\Smartfaq\Helper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use XoopsModules\Smartfaq\Constants as AdapterConstants;
0 ignored issues
show
Bug introduced by
The type XoopsModules\Smartfaq\Constants was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use XoopsModules\Xhelp;
26
27
//Sanity Check: make sure that file is not being accessed directly
28
if (!\defined('XHELP_CLASS_PATH')) {
29
    exit();
30
}
31
32
// ** Define any site specific variables here **
33
\define('XHELP_SMARTFAQ_PATH', XOOPS_ROOT_PATH . '/modules/smartfaq');
34
\define('XHELP_SMARTFAQ_URL', XOOPS_URL . '/modules/smartfaq');
35
// ** End site specific variables **
36
37
//Include the base faqAdapter interface (required)
38
// require_once XHELP_CLASS_PATH . '/faqAdapter.php';
39
40
//These functions are required to work with the smartfaq application directly
41
//@require \XHELP_SMARTFAQ_PATH . '/include/functions.php';
42
43
/**
44
 * class Smartfaq
45
 */
46
class Smartfaq extends Xhelp\FaqAdapterAbstract
47
{
48
    /**
49
     * Does application support categories?
50
     * Possible Values:
51
     * XHELP_FAQ_CATEGORY_SING - entries can be in 1 category
52
     * XHELP_FAQ_CATEGORY_MULTI - entries can be in more than 1 category
53
     * XHELP_FAQ_CATEGORY_NONE - No category support
54
     */
55
    public $categoryType = \XHELP_FAQ_CATEGORY_SING;
56
    /**
57
     * Adapter Details
58
     * Required Values:
59
     * name - name of adapter
60
     * author - who wrote the plugin
61
     * author_email - contact email
62
     * version - version of this plugin
63
     * tested_versions - supported application versions
64
     * url - support url for plugin
65
     * module_dir - module directory name (not needed if class overloads the isActive() function from Xhelp\FaqAdapterAbstract)
66
     */
67
    public $meta = [
68
        'name'            => 'Smartfaq',
69
        'author'          => 'Eric Juden',
70
        'author_email'    => '[email protected]',
71
        'description'     => 'Create SmartFAQ entries from xHelp helpdesk tickets',
72
        'version'         => '1.0',
73
        'tested_versions' => '1.04',
74
        'url'             => 'https://www.smartfactory.ca/',
75
        'module_dir'      => 'smartfaq',
76
    ];
77
78
    /**
79
     * Class Constructor (Required)
80
     */
81
    public function __construct()
82
    {
83
        // Every class should call parent::init() to ensure that all class level
84
        // variables are initialized properly.
85
        if (\class_exists(AdapterHelper::class)) {
86
            $this->helper  = AdapterHelper::getInstance();
87
            $this->dirname = $this->helper->dirname();
88
        }
89
        $this->init();
90
    }
91
92
    /**
93
     * getCategories: retrieve the categories for the module
94
     * @return array|bool Array of Xhelp\FaqCategory
95
     */
96
    public function &getCategories()
97
    {
98
        $ret = false;
99
        //        if (!\class_exists('XoopsModules\Smartfaq\Helper')) {
100
        //            return false;
101
        //        }
102
103
        if (null === $this->helper) {
104
            return $ret;
105
        }
106
107
        $ret = [];
108
        // Create an instance of the Xhelp\FaqCategoryHandler
109
        $faqCategoryHandler = Xhelp\Helper::getInstance()
110
            ->getHandler('FaqCategory');
111
112
        // Get all the categories for the application
113
        $smartfaqCategoryHandler = $this->helper->getHandler('Category');
114
        $categories              = $smartfaqCategoryHandler->getCategories(0, 0, -1);
115
116
        //Convert the module specific category to the
117
        //Xhelp\FaqCategory object for standarization
118
        foreach ($categories as $category) {
119
            $faqcat = $faqCategoryHandler->create();
120
            $faqcat->setVar('id', $category->getVar('categoryid'));
121
            $faqcat->setVar('parent', $category->getVar('parentid'));
122
            $faqcat->setVar('name', $category->getVar('name'));
123
            $ret[] = $faqcat;
124
        }
125
        unset($categories);
126
127
        return $ret;
128
    }
129
130
    /**
131
     * storeFaq: store the FAQ in the application's specific database (required)
132
     * @param Xhelp\Faq|null $faq The faq to add
133
     * @return bool     true (success) / false (failure)
134
     */
135
    public function storeFaq(Xhelp\Faq $faq = null): bool
136
    {
137
        global $xoopsUser;
138
        $uid = $xoopsUser->getVar('uid');
139
140
        // Take Xhelp\Faq and create faq for smartfaq
141
        $faqHandler    = Xhelp\Helper::getInstance()
142
            ->getHandler('Faq');
143
        $answerHandler = Xhelp\Helper::getInstance()
144
            ->getHandler('Answer');
145
        $myFaq         = $faqHandler->create();
146
        $myAnswer      = $answerHandler->create();            // Creating the answer object
147
148
        //$faq->getVar('categories') is an array. If your application
149
        //only supports single categories use the first element
150
        //in the array
151
        $categories = $faq->getVar('categories');
0 ignored issues
show
Bug introduced by
The method getVar() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

151
        /** @scrutinizer ignore-call */ 
152
        $categories = $faq->getVar('categories');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
152
        $categories = (int)$categories[0];       // Change array of categories to 1 category
153
154
        $myFaq->setVar('uid', $uid);
155
        $myFaq->setVar('question', $faq->getVar('problem'));
156
        $myFaq->setVar('datesub', \time());
157
        $myFaq->setVar('categoryid', $categories);
158
        $myFaq->setVar('status', AdapterConstants::SF_STATUS_PUBLISHED);
159
160
        $ret = $faqHandler->insert($myFaq);
161
        $faq->setVar('id', $myFaq->getVar('faqid'));
162
163
        if ($ret) {   // If faq was stored, store answer
164
            // Trigger event for question being stored
165
166
            $myAnswer->setVar('status', AdapterConstants::SF_AN_STATUS_APPROVED);
167
            $myAnswer->setVar('faqid', $myFaq->faqid());
0 ignored issues
show
Bug introduced by
The method faqid() does not exist on XoopsObject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

167
            $myAnswer->setVar('faqid', $myFaq->/** @scrutinizer ignore-call */ faqid());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
168
            $myAnswer->setVar('answer', $faq->getVar('solution'));
169
            $myAnswer->setVar('uid', $uid);
170
171
            $ret = $answerHandler->insert($myAnswer);
172
        }
173
174
        if ($ret && null !== $faq) {
175
            // Set the new url for the saved FAQ
176
            $faq->setVar('url', $this->makeFaqUrl($faq));
177
178
            // Trigger any module events
179
            $myFaq->sendNotifications([AdapterConstants::SF_NOT_FAQ_PUBLISHED]);
0 ignored issues
show
Bug introduced by
The method sendNotifications() does not exist on XoopsObject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

179
            $myFaq->/** @scrutinizer ignore-call */ 
180
                    sendNotifications([AdapterConstants::SF_NOT_FAQ_PUBLISHED]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
180
        }
181
182
        return $ret;
183
    }
184
185
    /**
186
     * Create the url going to the faq article
187
     *
188
     * @param \XoopsModules\Xhelp\Faq $faq object
189
     * @return string
190
     */
191
    public function makeFaqUrl(Xhelp\Faq $faq): string
192
    {
193
        return $this->helper->url('/faq.php?faqid=' . $faq->getVar('id'));
194
    }
195
}
196