Completed
Branch develop (0458b7)
by
unknown
26:26
created

box_produits::loadBox()   F

Complexity

Conditions 18
Paths 345

Size

Total Lines 142
Code Lines 92

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
eloc 92
nc 345
nop 1
dl 0
loc 142
rs 3.6714
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
/* Copyright (C) 2003      Rodolphe Quiedeville <[email protected]>
3
 * Copyright (C) 2004-2011 Laurent Destailleur  <[email protected]>
4
 * Copyright (C) 2005-2012 Regis Houssin        <[email protected]>
5
 * Copyright (C) 2015      Frederic France      <[email protected]>
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
/**
22
 *	\file       htdocs/core/boxes/box_produits.php
23
 *	\ingroup    produits,services
24
 *	\brief      Module to generate box of last products/services
25
 */
26
27
include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
28
include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
29
30
31
/**
32
 * Class to manage the box to show last products
33
 */
34
class box_produits extends ModeleBoxes
35
{
36
	var $boxcode="lastproducts";
37
	var $boximg="object_product";
38
	var $boxlabel="BoxLastProducts";
39
	var $depends = array("produit");
40
41
	var $db;
42
	var $param;
43
44
	var $info_box_head = array();
45
	var $info_box_contents = array();
46
47
48
	/**
49
	 *  Load data into info_box_contents array to show array later.
50
	 *
51
	 *  @param	int		$max        Maximum number of records to load
52
     *  @return	void
53
	 */
54
	function loadBox($max=5)
55
	{
56
		global $user, $langs, $db, $conf, $hookmanager;
57
58
		$this->max=$max;
59
60
		include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
61
		$productstatic=new Product($db);
62
63
		$this->info_box_head = array('text' => $langs->trans("BoxTitleLastProducts",$max));
64
65
		if ($user->rights->produit->lire || $user->rights->service->lire)
66
		{
67
			$sql = "SELECT p.rowid, p.label, p.ref, p.price, p.price_base_type, p.price_ttc, p.fk_product_type, p.tms, p.tosell, p.tobuy, p.fk_price_expression, p.entity";
68
			$sql.= " FROM ".MAIN_DB_PREFIX."product as p";
69
			$sql.= ' WHERE p.entity IN ('.getEntity($productstatic->element, 1).')';
70
			if (empty($user->rights->produit->lire)) $sql.=' AND p.fk_product_type != 0';
71
			if (empty($user->rights->service->lire)) $sql.=' AND p.fk_product_type != 1';
72
			// Add where from hooks
73
			if (is_object($hookmanager))
74
			{
75
			    $parameters=array();
76
			    $reshook=$hookmanager->executeHooks('printFieldListWhere',$parameters);    // Note that $action and $object may have been modified by hook
77
			    $sql.=$hookmanager->resPrint;
78
			}
79
			$sql.= $db->order('p.datec', 'DESC');
80
			$sql.= $db->plimit($max, 0);
81
82
			$result = $db->query($sql);
83
			if ($result)
84
			{
85
				$num = $db->num_rows($result);
86
				$line = 0;
87
				while ($line < $num)
88
				{
89
					$objp = $db->fetch_object($result);
90
					$datem=$db->jdate($objp->tms);
91
92
					// Multilangs
93
					if (! empty($conf->global->MAIN_MULTILANGS)) // si l'option est active
94
					{
95
						$sqld = "SELECT label";
96
						$sqld.= " FROM ".MAIN_DB_PREFIX."product_lang";
97
						$sqld.= " WHERE fk_product=".$objp->rowid;
98
						$sqld.= " AND lang='". $langs->getDefaultLang() ."'";
99
						$sqld.= " LIMIT 1";
100
101
						$resultd = $db->query($sqld);
102
						if ($resultd)
103
						{
104
							$objtp = $db->fetch_object($resultd);
105
							if (isset($objtp->label) && $objtp->label != '')
106
								$objp->label = $objtp->label;
107
						}
108
					}
109
                    $productstatic->id = $objp->rowid;
110
                    $productstatic->ref = $objp->ref;
111
                    $productstatic->type = $objp->fk_product_type;
112
                    $productstatic->label = $objp->label;
113
					$productstatic->entity = $objp->entity;
114
115
					$this->info_box_contents[$line][] = array(
116
                        'td' => '',
117
                        'text' => $productstatic->getNomUrl(1),
118
                        'asis' => 1,
119
                    );
120
121
                    $this->info_box_contents[$line][] = array(
122
                        'td' => 'class="tdoverflowmax100 maxwidth100onsmartphone"',
123
                        'text' => $objp->label,
124
                    );
125
126
                    if (empty($conf->dynamicprices->enabled) || empty($objp->fk_price_expression)) {
127
                        $price_base_type=$langs->trans($objp->price_base_type);
128
                        $price=($objp->price_base_type == 'HT')?price($objp->price):$price=price($objp->price_ttc);
129
	                }
130
	                else //Parse the dynamic price
131
	               	{
132
						$productstatic->fetch($objp->rowid, '', '', 1);
133
	                    $priceparser = new PriceParser($this->db);
134
	                    $price_result = $priceparser->parseProduct($productstatic);
135
	                    if ($price_result >= 0) {
136
							if ($objp->price_base_type == 'HT')
137
							{
138
								$price_base_type=$langs->trans("HT");
139
							}
140
							else
141
							{
142
								$price_result = $price_result * (1 + ($productstatic->tva_tx / 100));
143
								$price_base_type=$langs->trans("TTC");
144
							}
145
							$price=price($price_result);
146
	                    }
147
	               	}
148
					$this->info_box_contents[$line][] = array(
149
                        'td' => 'class="right"',
150
                        'text' => $price,
0 ignored issues
show
Bug introduced by
The variable $price does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
151
                    );
152
153
					$this->info_box_contents[$line][] = array(
154
                        'td' => 'class="nowrap"',
155
                        'text' => $price_base_type,
0 ignored issues
show
Bug introduced by
The variable $price_base_type does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
156
                    );
157
158
					$this->info_box_contents[$line][] = array(
159
                        'td' => 'class="right"',
160
                        'text' => dol_print_date($datem,'day'),
161
                    );
162
163
					$this->info_box_contents[$line][] = array(
164
                        'td' => 'align="right" width="18"',
165
                        'text' => $productstatic->LibStatut($objp->tosell,3,0),
166
                    );
167
168
                    $this->info_box_contents[$line][] = array(
169
                        'td' => 'align="right" width="18"',
170
                        'text' => $productstatic->LibStatut($objp->tobuy,3,1),
171
                    );
172
173
                    $line++;
174
                }
175
                if ($num==0)
176
                    $this->info_box_contents[$line][0] = array(
177
                        'td' => 'align="center"',
178
                        'text'=>$langs->trans("NoRecordedProducts"),
179
                    );
180
181
                $db->free($result);
182
            } else {
183
                $this->info_box_contents[0][0] = array(
184
                    'td' => '',
185
                    'maxlength'=>500,
186
                    'text' => ($db->error().' sql='.$sql),
187
                );
188
            }
189
        } else {
190
            $this->info_box_contents[0][0] = array(
191
                'td' => '',
192
                'text' => $langs->trans("ReadPermissionNotAllowed"),
193
            );
194
        }
195
    }
196
197
	/**
198
	 *	Method to show box
199
	 *
200
	 *	@param	array	$head       Array with properties of box title
201
	 *	@param  array	$contents   Array with properties of box lines
202
	 *  @param	int		$nooutput	No print, only return string
203
	 *	@return	void
204
	 */
205
    function showBox($head = null, $contents = null, $nooutput=0)
206
    {
207
		parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
208
	}
209
210
}
211
212