Completed
Branch develop (59ab9a)
by
unknown
27:43
created

DictionaryEvents::index()   D

Complexity

Conditions 10
Paths 76

Size

Total Lines 46
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 27
nc 76
nop 7
dl 0
loc 46
rs 4.983
c 0
b 0
f 0

How to fix   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
/* Copyright (C) 2017	Regis Houssin	<[email protected]>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
use Luracast\Restler\RestException;
19
20
require_once DOL_DOCUMENT_ROOT.'/main.inc.php';
21
22
/**
23
 * API class for events type (content of the actioncomm dictionary)
24
 *
25
 * @access protected
26
 * @class DolibarrApiAccess {@requires user,external}
27
 */
28
class DictionaryEvents extends DolibarrApi
29
{
30
    /**
31
     * Constructor
32
     */
33
    function __construct()
34
    {
35
        global $db;
36
        $this->db = $db;
37
    }
38
39
    /**
40
     * Get the list of events types.
41
     *
42
     * @param string    $sortfield  Sort field
43
     * @param string    $sortorder  Sort order
44
     * @param int       $limit      Number of items per page
45
     * @param int       $page       Page number (starting from zero)
46
     * @param string    $type       To filter on type of event
47
     * @param string    $module     To filter on module events
48
     * @param string    $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
49
     * @return List of events types
50
     *
51
     * @throws RestException
52
     */
53
    function index($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $type = '', $module = '', $sqlfilters = '')
54
    {
55
        $list = array();
56
57
        $sql = "SELECT id, code, type, libelle as label, module";
58
        $sql.= " FROM ".MAIN_DB_PREFIX."c_actioncomm as t";
59
        $sql.= " WHERE t.active = 1";
60
        if ($type) $sql.=" AND t.type LIKE '%" . $this->db->escape($type) . "%'";
61
        if ($module)    $sql.=" AND t.module LIKE '%" . $this->db->escape($module) . "%'";
62
        // Add sql filters
63
        if ($sqlfilters)
64
        {
65
            if (! DolibarrApi::_checkFilters($sqlfilters))
66
            {
67
                throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
68
            }
69
	        $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
70
            $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
71
        }
72
73
74
        $sql.= $this->db->order($sortfield, $sortorder);
75
76
        if ($limit) {
77
            if ($page < 0) {
78
                $page = 0;
79
            }
80
            $offset = $limit * $page;
81
82
            $sql .= $this->db->plimit($limit, $offset);
83
        }
84
85
        $result = $this->db->query($sql);
86
87
        if ($result) {
88
            $num = $this->db->num_rows($result);
89
            $min = min($num, ($limit <= 0 ? $num : $limit));
90
            for ($i = 0; $i < $min; $i++) {
91
                $list[] = $this->db->fetch_object($result);
92
            }
93
        } else {
94
            throw new RestException(503, 'Error when retrieving list of events types : '.$this->db->lasterror());
95
        }
96
97
        return $list;
98
    }
99
100
}
101