Completed
Pull Request — master (#27)
by Michael
01:42
created

csv.php ➔ xnewsletter_plugin_getdata_csv()   C

Complexity

Conditions 17
Paths 72

Size

Total Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
nc 72
nop 7
dl 0
loc 70
rs 5.2166
c 0
b 0
f 0

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
 *  - A Project by Developers TEAM For Xoops - ( https://xoops.org )
5
 * ****************************************************************************
6
 *  XNEWSLETTER - MODULE FOR XOOPS
7
 *  Copyright (c) 2007 - 2012
8
 *  Goffy ( wedega.com )
9
 *
10
 *  You may not change or alter any portion of this comment or credits
11
 *  of supporting developers from this source code or any supporting
12
 *  source code which is considered copyrighted (c) material of the
13
 *  original comment or credit authors.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *  ---------------------------------------------------------------------------
20
 * @copyright  Goffy ( wedega.com )
21
 * @license    GPL 2.0
22
 * @package    xnewsletter
23
 * @author     Goffy ( [email protected] )
24
 *
25
 * ****************************************************************************
26
 */
27
28
use XoopsModules\Xnewsletter;
29
30
// defined("XOOPS_ROOT_PATH") || die("XOOPS root path not defined");
31
require_once dirname(__DIR__) . '/include/common.php';
32
33
/**
34
 * @return array
35
 */
36 View Code Duplication
function xnewsletter_plugin_getinfo_csv()
0 ignored issues
show
Duplication introduced by
This function 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...
37
{
38
    global $xoopsDB;
39
40
    $pluginInfo         = [];
41
    $pluginInfo['name'] = 'csv';
42
    $pluginInfo['icon'] = XNEWSLETTER_URL . '/plugins/csv.png';
43
    //$pluginInfo['modulepath'] = XNEWSLETTER_ROOT_PATH . "/plugins/csv.php";
44
    $pluginInfo['tables'][0] = '';
45
    $pluginInfo['descr']     = 'Import CSV';
46
    $pluginInfo['hasform']   = 1;
47
48
    return $pluginInfo;
49
}
50
51
/**
52
 * @param $cat_id
53
 * @param $action_after_read
54
 * @param $limitCheck
55
 * @param $skipCatsubscrExist
56
 * @param $file
57
 * @param $delimiter
58
 * @param $header
59
 *
60
 * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be null|integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
61
 */
62
function xnewsletter_plugin_getdata_csv(
63
    $cat_id,
64
    $action_after_read,
65
    $limitCheck,
66
    $skipCatsubscrExist,
67
    $file,
68
    $delimiter,
69
    $header = true)
70
{
71
    global $xoopsDB;
72
    $helper = Xnewsletter\Helper::getInstance();
73
74
    //$table_import = $xoopsDB->prefix('xnewsletter_import');
75
    $import_status = 0 == $action_after_read ? true : false;
76
    $i             = 0;
77
    $j             = 0;
78
79
    if (false !== ($handle = fopen($file, 'rb'))) {
80
        while (false !== ($lineArray = fgetcsv($handle, 4000, $delimiter))) {
81
            if (true === $header || 0 == $i) {
82
                // remove header line
83
                // NOP
84
            } else {
85
                $email     = $lineArray[0];
86
                $sex       = isset($lineArray[1]) ? $lineArray[1] : '';
87
                $firstname = isset($lineArray[2]) ? $lineArray[2] : '';
88
                $lastname  = isset($lineArray[3]) ? $lineArray[3] : '';
89
90
                if ('' != $email) {
91
                    $subscr_id    = xnewsletter_pluginCheckEmail($email);
92
                    $catsubscr_id = xnewsletter_pluginCheckCatSubscr($subscr_id, $cat_id);
93
94
                    if (true === $skipCatsubscrExist && $catsubscr_id > 0) {
95
                        //skip existing subscriptions
96
                        // NOP
97
                    } else {
98
                        $current_cat_id = $catsubscr_id > 0 ? 0 : $cat_id;
99
                        $importObj      = $helper->getHandler('Import')->create();
100
                        $importObj->setVar('import_email', $email);
101
                        $importObj->setVar('import_sex', $sex);
102
                        $importObj->setVar('import_firstname', $firstname);
103
                        $importObj->setVar('import_lastname', $lastname);
104
                        $importObj->setVar('import_cat_id', $current_cat_id);
105
                        $importObj->setVar('import_subscr_id', $subscr_id);
106
                        $importObj->setVar('import_catsubscr_id', $catsubscr_id);
107
                        $importObj->setVar('import_status', $import_status);
108
                        if (!$helper->getHandler('Import')->insert($importObj)) {
109
                            echo $importObj->getHtmlErrors();
110
                            exit();
111
                        }
112
                        //                    $sql = "INSERT INTO {$table_import} (import_email, import_sex, import_firstname, import_lastname, import_cat_id, import_subscr_id, import_catsubscr_id, import_status)";
113
                        //                    $sql .= " VALUES ('$email', '$sex', '$firstname', '$lastname', $current_cat_id, $subscr_id, $catsubscr_id, $import_status)";
114
                        //                    $result_insert = $xoopsDB->query($sql) or die ("MySQL-Error: " . $GLOBALS['xoopsDB']->error());
115
                        ++$j;
116
                    }
117
                }
118
            }
119
            ++$i;
120
            if (100000 == $j) {
121
                break;
122
            } //maximum number of processing to avoid cache overflow
123
            if ($limitCheck > 0 && $j == $limitCheck) {
124
                $import_status = false;
125
            }
126
        }
127
        fclose($handle);
128
    }
129
130
    return $j;
131
}
132
133
/**
134
 * @param      $cat_id
135
 * @param      $action_after_read
136
 * @param      $limitCheck
137
 * @param      $skipCatsubscrExist
138
 * @param bool $action
139
 *
140
 * @return XoopsThemeForm
141
 */
142
function xnewsletter_plugin_getform_csv(
143
    $cat_id,
144
    $action_after_read,
145
    $limitCheck,
146
    $skipCatsubscrExist,
147
    $action = false)
148
{
149
    if (false === $action) {
150
        $action = $_SERVER['REQUEST_URI'];
151
    }
152
153
    $title = _AM_XNEWSLETTER_IMPORT_CSV_OPT;
154
155
    require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
156
    $form = new \XoopsThemeForm($title, 'form_add_csv', $action, 'post', true);
157
    $form->setExtra('enctype="multipart/form-data"');
158
159
    $form->addElement(new \XoopsFormLabel('Info', _AM_XNEWSLETTER_IMPORT_CSV));
160
161
    //limit file size 16 MB
162
    $form->addElement(new \XoopsFormFile(_AM_XNEWSLETTER_IMPORT_CSV_FILE, 'csv_file', '16777216'), true);
163
    $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_IMPORT_CSV_DELIMITER, 'csv_delimiter', 10, 1, ','), true);
164
    $form->addElement(new \XoopsFormRadioYN(_AM_XNEWSLETTER_IMPORT_CSV_HEADER, 'csv_header', 1, _YES, _NO), false);
165
166
    $form->addElement(new \XoopsFormHidden('plugin', 'csv'));
167
    $form->addElement(new \XoopsFormHidden('cat_id', $cat_id));
168
    $form->addElement(new \XoopsFormHidden('action_after_read', $action_after_read));
169
    $form->addElement(new \XoopsFormHidden('limitcheck', $limitCheck));
170
    $form->addElement(new \XoopsFormHidden('skipcatsubscrexist', $skipCatsubscrExist));
171
    $form->addElement(new \XoopsFormHidden('op', 'searchdata'));
172
    $form->addElement(new \XoopsFormButton('', 'submit', _AM_XNEWSLETTER_IMPORT_CONTINUE, 'submit'));
173
174
    return $form;
175
}
176