Completed
Branch master (1b2f30)
by Michael
06:29 queued 03:22
created

admin/wffaq.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Module: SmartFAQ
4
 * Author: Marius Scurtescu <[email protected]>
5
 * Licence: GNU
6
 *
7
 * Import script from WF-FAQ to SmartFAQ.
8
 *
9
 * It was tested with WF-FAQ version 1.0.5 and SmartFAQ version 1.0 beta
10
 *
11
 */
12
13
include_once __DIR__ . '/admin_header.php';
14
15
$importFromModuleName = 'WF-FAQ';
16
$scriptname           = 'wffaq.php';
17
18
$op = 'start';
19
20 View Code Duplication
if (isset($_POST['op']) && ($_POST['op'] === 'go')) {
0 ignored issues
show
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...
21
    $op = $_POST['op'];
22
}
23
24
if ($op === 'start') {
25
    include_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
26
    xoops_cp_header();
27
    $result = $xoopsDB->query('select count(*) from ' . $xoopsDB->prefix('faqcategories'));
28
    list($totalCat) = $xoopsDB->fetchRow($result);
29
30
    sf_collapsableBar('bottomtable', 'bottomtableicon');
31
    echo "<img id='bottomtableicon' src=" . XOOPS_URL . '/modules/' . $xoopsModule->dirname() . "/assets/images/icon/close12.gif alt='' /></a>&nbsp;" . sprintf(_AM_SF_IMPORT_FROM, $importFromModuleName) . '</h3>';
32
    echo "<div id='bottomtable'>";
33
34
    if ($totalCat == 0) {
35
        echo "<span style=\"color: #567; margin: 3px 0 12px 0; font-size: small; display: block; \">" . _AM_SF_IMPORT_NO_CATEGORY . '</span>';
36
    } else {
37
        include_once XOOPS_ROOT_PATH . '/class/xoopstree.php';
38
39
        $result = $xoopsDB->query('select count(*) from ' . $xoopsDB->prefix('faqtopics'));
40
        list($totalFAQ) = $xoopsDB->fetchRow($result);
41
42
        if ($totalFAQ == 0) {
43
            echo "<span style=\"color: #567; margin: 3px 0 12px 0; font-size: small; display: block; \">" . sprintf(_AM_SF_IMPORT_MODULE_FOUND_NO_FAQ, $importFromModuleName, $totalCat) . '</span>';
44
        } else {
45
            echo "<span style=\"color: #567; margin: 3px 0 12px 0; font-size: small; display: block; \">" . sprintf(_AM_SF_IMPORT_MODULE_FOUND, $importFromModuleName, $totalCat, $totalFAQ) . '</span>';
46
47
            $form = new XoopsThemeForm(_AM_SF_IMPORT_SETTINGS, 'import_form', XOOPS_URL . '/modules/smartfaq/admin/' . $scriptname);
48
49
            // Categories to be imported
50
            $cat_cbox = new XoopsFormCheckBox(sprintf(_AM_SF_IMPORT_CATEGORIES, $importFromModuleName), 'import_category', -1);
51
            $result   = $xoopsDB->query('select c.catID, c.name, count(t.topicID) from ' . $xoopsDB->prefix('faqcategories') . ' as c, ' . $xoopsDB->prefix('faqtopics') . ' as t where c.catID=t.catID group by t.catID order by c.weight');
52
            while (list($cid, $cat_title, $count) = $xoopsDB->fetchRow($result)) {
53
                $cat_cbox->addOption($cid, "$cat_title ($count)<br\>");
54
            }
55
            $form->addElement($cat_cbox);
56
57
            // SmartFAQ parent category
58
            $mytree = new XoopsTree($xoopsDB->prefix('smartfaq_categories'), 'categoryid', 'parentid');
59
            ob_start();
60
            $mytree->makeMySelBox('name', 'weight', $preset_id = 0, $none = 1, $sel_name = 'parent_category');
61
            $form->addElement(new XoopsFormLabel(_AM_SF_IMPORT_PARENT_CATEGORY, ob_get_contents()));
62
            ob_end_clean();
63
64
            // Auto-Approve
65
            $form->addElement(new XoopsFormRadioYN(_AM_SF_IMPORT_AUTOAPPROVE, 'autoaprove', 1, ' ' . _AM_SF_YES . '', ' ' . _AM_SF_NO . ''));
66
67
            // Submitted and answered by
68
            $memberHandler = xoops_getHandler('member');
69
            $user_select   = new XoopsFormSelect(_AM_SF_IMPORTED_USER, 'uid', 0);
70
            $user_select->addOption(0, '----');
71
            //$criteria = new CriteriaCompo ();
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
72
            //$criteria->setSort ('uname');
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
73
            //$criteria->setOrder ('ASC');
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
74
            $user_select->addOptionArray($memberHandler->getUserList());
75
            $form->addElement($user_select);
76
77
            // Q&As can be commented?
78
            $form->addElement(new XoopsFormRadioYN(_AM_SF_IMPORT_ALLOWCOMMENTS, 'cancomment', 1, ' ' . _AM_SF_YES . '', ' ' . _AM_SF_NO . ''));
79
            $group_list      = $memberHandler->getGroupList();
80
            $groups_selected = array();
81
            $groups_checkbox = new XoopsFormCheckBox(_AM_SF_IMPORT_PERMISSIONS, 'groups_read');
82 View Code Duplication
            foreach ($group_list as $group_id => $group_name) {
83
                if ($group_id != XOOPS_GROUP_ADMIN) {
84
                    $groups_selected [] = $group_id;
85
                    $groups_checkbox->addOption($group_id, $group_name);
86
                }
87
            }
88
            $groups_checkbox->setValue($groups_selected);
89
            $form->addElement($groups_checkbox);
90
91
            $form->addElement(new XoopsFormHidden('op', 'go'));
92
            $form->addElement(new XoopsFormButton('', 'import', _AM_SF_IMPORT, 'submit'));
93
94
            $form->display();
95
        }
96
    }
97
98
    exit();
99
}
100
101
if ($op === 'go') {
102
    $import_category = (isset($_POST['import_category']) ? $_POST['import_category'] : null);
103
    if (!$import_category) {
104
        redirect_header($scriptname, 2, _AM_SF_NOCATSELECTED);
105
    }
106
107
    include_once __DIR__ . '/admin_header.php';
108
    xoops_cp_header();
109
110
    sf_collapsableBar('bottomtable', 'bottomtableicon');
111
    echo "<img id='bottomtableicon' src=" . XOOPS_URL . '/modules/' . $xoopsModule->dirname() . "/assets/images/icon/close12.gif alt='' /></a>&nbsp;" . sprintf(_AM_SF_IMPORT_FROM, $importFromModuleName) . '</h3>';
112
    echo "<div id='bottomtable'>";
113
    echo "<span style=\"color: #567; margin: 3px 0 12px 0; font-size: small; display: block; \">" . _AM_SF_IMPORT_RESULT . '</span>';
114
115
    $cnt_imported_cat = 0;
116
    $cnt_imported_faq = 0;
117
118
    $parentId    = $_POST['parent_category'];
119
    $groups_read = isset($_POST['groups_read']) ? $_POST['groups_read'] : array();
120
    $uid         = !empty($_POST['uid']) ? $_POST['uid'] : 0;
121
    $cancomment  = $_POST['cancomment'];
122
    $autoaprove  = $_POST['autoaprove'];
123
124 View Code Duplication
    if (is_array($_POST['import_category'])) {
125
        $import_category_list = implode(',', $_POST['import_category']);
126
    } else {
127
        $import_category_list = $_POST['import_category'];
128
    }
129
130
    $categoryHandler = sf_gethandler('category');
131
    $faqHandler      = sf_gethandler('faq');
132
    $answerHandler   = sf_gethandler('answer');
133
134
    /*echo "Parent Category ID: $parentId<br/>";
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
135
    echo "Groups Read: " . implode (",", $groups_read) . "<br/>";
136
    echo "Import Categories: $import_category_list<br/>";
137
    echo "User ID: $uid<br/>";
138
    echo "Can Comment: $cancomment<br/>";
139
    echo "Auto aprove: $autoaprove<br/>";*/
140
141
    $resultCat = $xoopsDB->query('select * from ' . $xoopsDB->prefix('faqcategories') . " where catID in ($import_category_list) order by weight");
142
143
    while ($arrCat = $xoopsDB->fetchArray($resultCat)) {
144
        extract($arrCat, EXTR_PREFIX_ALL, 'wfc');
145
146
        // insert category into SmartFAQ
147
        $categoryObj = $categoryHandler->create();
148
149
        $categoryObj->setVar('parentid', $parentId);
150
        $categoryObj->setVar('weight', $wfc_weight);
151
        $categoryObj->setGroups_read(explode(' ', trim($wfc_groupid)));
152
        $categoryObj->setVar('name', $wfc_name);
153
        $categoryObj->setVar('description', $wfc_description);
154
155
        if (!$categoryObj->store(false)) {
156
            echo sprintf(_AM_SF_IMPORT_CATEGORY_ERROR, $xcat_name) . '<br/>';
157
            continue;
158
        }
159
160
        sf_saveCategory_Permissions($categoryObj->getGroups_read(), $categoryObj->categoryid(), 'category_read');
161
162
        ++$cnt_imported_cat;
163
164
        echo sprintf(_AM_SF_IMPORT_CATEGORY_SUCCESS, $wfc_name) . "<br\>";
165
166
        $resultFAQ = $xoopsDB->query('select * from ' . $xoopsDB->prefix('faqtopics') . " where catID=$wfc_catID order by weight");
167
        while ($arrFAQ = $xoopsDB->fetchArray($resultFAQ)) {
168
            extract($arrFAQ, EXTR_PREFIX_ALL, 'wft');
169
170
            if ($autoaprove) {
171
                if ($wft_submit) {
172
                    $qstatus = _SF_STATUS_PUBLISHED;
173
                } else {
174
                    $qstatus = _SF_STATUS_SUBMITTED;
175
                }
176
            } else {
177
                $qstatus = _SF_STATUS_SUBMITTED;
178
            }
179
180
            // insert question into SmartFAQ
181
            $faqObj    = $faqHandler->create();
182
            $answerObj = $answerHandler->create();
183
184
            $faqObj->setGroups_read(explode(' ', trim($wft_groupid)));
185
            $faqObj->setVar('categoryid', $categoryObj->categoryid());
186
            $faqObj->setVar('question', $wft_question);
187
            $faqObj->setVar('uid', $wft_uid);
188
            $faqObj->setVar('status', $qstatus);
189
            $faqObj->setVar('weight', $wft_weight);
190
            $faqObj->setVar('html', $wft_html);
191
            $faqObj->setVar('smiley', $wft_smiley);
192
            $faqObj->setVar('xcodes', $wft_xcodes);
193
            $faqObj->setVar('cancomment', $cancomment);
194
            $faqObj->setVar('diduno', $wft_summary);
195
            $faqObj->setVar('exacturl', 0);
196
197 View Code Duplication
            if (!$faqObj->store(false)) {
198
                echo sprintf('  ' . _AM_SF_IMPORT_FAQ_ERROR, $wft_question) . '<br/>';
199
                continue;
200
            } else {
201
                $answerObj->setVar('faqid', $faqObj->faqid());
202
                $answerObj->setVar('answer', $wft_answer);
203
                $answerObj->setVar('uid', $wft_uid);
204
                $answerObj->setVar('status', _SF_AN_STATUS_APPROVED);
205
206
                if (!$answerObj->store()) {
207
                    echo sprintf('  ' . _AM_SF_IMPORT_FAQ_ERROR) . '<br/>';
208
                    continue;
209
                } else {
210
                    echo '&nbsp;&nbsp;' . sprintf(_AM_SF_IMPORTED_QUESTION, $faqObj->question(50)) . '<br />';
211
                    ++$cnt_imported_faq;
212
                }
213
            }
214
        }
215
216
        echo '<br/>';
217
    }
218
219
    echo 'Done.<br/>';
220
    echo sprintf(_AM_SF_IMPORTED_CATEGORIES, $cnt_imported_cat) . '<br/>';
221
    echo sprintf(_AM_SF_IMPORTED_QUESTIONS, $cnt_imported_faq) . '<br/>';
222
223
    exit();
224
}
225