Completed
Push — master ( 1db5db...8743f5 )
by Michael
06:44
created

bookshop_commands::bookshop_commands()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 21
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 23
rs 9.0856
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 29 and the first side effect is on line 27.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
//  ------------------------------------------------------------------------ //
3
//                      BOOKSHOP - MODULE FOR XOOPS 2                        //
4
//                  Copyright (c) 2007, 2008 Instant Zero                    //
5
//                     <http://www.instant-zero.com/>                        //
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 2 of the License, or        //
10
//  (at your option) any later version.                                      //
11
//                                                                           //
12
//  You may not change or alter any portion of this comment or credits       //
13
//  of supporting developers from this source code or any supporting         //
14
//  source code which is considered copyrighted (c) material of the          //
15
//  original comment or credit authors.                                      //
16
//                                                                           //
17
//  This program is distributed in the hope that it will be useful,          //
18
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
19
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
20
//  GNU General Public License for more details.                             //
21
//                                                                           //
22
//  You should have received a copy of the GNU General Public License        //
23
//  along with this program; if not, write to the Free Software              //
24
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
25
//  ------------------------------------------------------------------------ //
26
27
defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
28
29
define('COMMAND_STATE_NOINFORMATION', 0);    // Pas encore d'informations sur la commande
30
define("COMMAND_STATE_VALIDATED", 1);		// Commande validée par Paypal
31
define('COMMAND_STATE_PENDING', 2);            // En attente
32
define('COMMAND_STATE_FAILED', 3);            // Echec
33
define("COMMAND_STATE_CANCELED", 4);		// Annulée
34
define('COMMAND_STATE_FRAUD', 5);            // Fraude
35
36
include_once XOOPS_ROOT_PATH . '/kernel/object.php';
37
if (!class_exists('Bookshop_XoopsPersistableObjectHandler')) {
38
    include_once XOOPS_ROOT_PATH . '/modules/bookshop/class/PersistableObjectHandler.php';
39
}
40
41
/**
42
 * Class bookshop_commands
43
 */
44
class bookshop_commands extends Bookshop_Object
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
45
{
46
    public function __construct()
47
    {
48
        $this->initVar('cmd_id', XOBJ_DTYPE_INT, null, false);
49
        $this->initVar('cmd_uid', XOBJ_DTYPE_INT, null, false);
50
        $this->initVar('cmd_date', XOBJ_DTYPE_TXTBOX, null, false);
51
        $this->initVar('cmd_state', XOBJ_DTYPE_INT, null, false);
52
        $this->initVar('cmd_ip', XOBJ_DTYPE_TXTBOX, null, false);
53
        $this->initVar('cmd_lastname', XOBJ_DTYPE_TXTBOX, null, false);
54
        $this->initVar('cmd_firstname', XOBJ_DTYPE_TXTBOX, null, false);
55
        $this->initVar('cmd_adress', XOBJ_DTYPE_TXTAREA, null, false);
56
        $this->initVar('cmd_zip', XOBJ_DTYPE_TXTBOX, null, false);
57
        $this->initVar('cmd_town', XOBJ_DTYPE_TXTBOX, null, false);
58
        $this->initVar('cmd_country', XOBJ_DTYPE_TXTBOX, null, false);
59
        $this->initVar('cmd_telephone', XOBJ_DTYPE_TXTBOX, null, false);
60
        $this->initVar('cmd_email', XOBJ_DTYPE_TXTBOX, null, false);
61
        $this->initVar('cmd_articles_count', XOBJ_DTYPE_INT, null, false);
62
        $this->initVar('cmd_total', XOBJ_DTYPE_TXTBOX, null, false);
63
        $this->initVar('cmd_shipping', XOBJ_DTYPE_TXTBOX, null, false);
64
        $this->initVar('cmd_bill', XOBJ_DTYPE_INT, null, false);
65
        $this->initVar('cmd_password', XOBJ_DTYPE_TXTBOX, null, false);
66
        $this->initVar('cmd_text', XOBJ_DTYPE_TXTAREA, null, false);
67
        $this->initVar('cmd_cancel', XOBJ_DTYPE_TXTBOX, null, false);
68
    }
69
}
70
71
/**
72
 * Class BookshopBookshop_commandsHandler
73
 */
74
class BookshopBookshop_commandsHandler extends Bookshop_XoopsPersistableObjectHandler
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
75
{
76
    /**
77
     * @param $db
78
     */
79
    public function __construct($db)
80
    {    //                                             Table                   Classe           Id
81
        parent::__construct($db, 'bookshop_commands', 'bookshop_commands', 'cmd_id');
82
    }
83
84
    /**
85
     * Indique si c'est la premi�re commande d'un client
86
     *
87
     * @param  integer $uid Identifiant de l'utilisateur
88
     * @return boolean Indique si c'est le cas ou pas
89
     */
90
    public function isFirstCommand($uid)
91
    {
92
        $critere = new Criteria('cmd_uid', (int)$uid, '=');
93
        if ($this->getCount($critere) > 0) {
94
            return true;
95
        } else {
96
            return false;
97
        }
98
    }
99
100
    /**
101
	 * Indique si un livre a déajà été acheté par un utilisateur
102
     *
103
     * @param  integer $uid    Identifiant de l'utilisateur
104
     * @param  integer $bookId Identifiant du livre
105
     * @return boolean Indique si c'est le cas ou pas
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|boolean?

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...
106
     */
107
    public function BookAlreadyBought($uid, $bookId)
108
    {
109
        $sql    = 'SELECT Count(*) as cpt FROM ' . $this->db->prefix('bookshop_caddy') . ' c, ' . $this->db->prefix('bookshop_commands') . ' f WHERE c.caddy_book_id = ' . (int)$bookId . ' AND c.caddy_cmd_id = f.cmd_id AND f.cmd_uid = ' . (int)$uid;
110
        $result = $this->db->query($sql);
111
        if (!$result) {
112
            return 0;
113
        }
114
        list($count) = $this->db->fetchRow($result);
115
        if ($count > 0) {
116
            return true;
117
        } else {
118
            return false;
119
        }
120
    }
121
122
    /**
123
	 * Mise à jour des stocks pour chaque livre composant la commande
124
     * @param  integer $cmd_id Identifiant de la commande
125
     * @return void
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean?

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...
126
     */
127
    public function updateStocks($cmd_id)
128
    {
129
        global $h_bookshop_caddy, $h_bookshop_books;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
130
        // Recherche de tous les livres du caddy
131
        $caddy  = $h_bookshop_caddy->getCaddyFromCommand($cmd_id);
132
        $tblTmp = $tblBooks = array();
0 ignored issues
show
Unused Code introduced by
$tblBooks is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
133
        foreach ($caddy as $item) {
134
            $tblTmp[] = $item->getVar('caddy_book_id');
135
        }
136
        // Chargement de tous les livres
137
        $critere  = new Criteria('book_id', '(' . implode(',', $tblTmp) . ')', 'IN');
138
        $tblBooks = $h_bookshop_books->getObjects($critere, true);
139
		// Boucle sur le caddy pour mettre à jour les quantités
140
        foreach ($caddy as $item) {
141
            if (isset($tblBooks[$item->getVar('caddy_book_id')])) {
142
                $book = $tblBooks[$item->getVar('caddy_book_id')];
143
                $h_bookshop_books->decreaseStock($book, $item->getVar('caddy_qte'));
144
				$h_bookshop_books->verifyLowStock($book);	// Vérification du stock d'alerte
145
            }
146
        }
147
148
        return true;
149
    }
150
151
    /**
152
	 * Validation d'une commande et mise à jour des stocks
153
     *
154
     * @param  integer $cmd_id Identifiant de la commande
155
     * @return boolean Indique si la validation de la commande s'est bien faite ou pas
156
     */
157
    public function validateCommand($cmd_id)
158
    {
159
        $retval   = false;
160
        $commande =& $this->get($cmd_id);
161
        if (is_object($commande)) {
162
            $commande->setVar('cmd_state', COMMAND_STATE_VALIDATED);
163
            $retval = $this->insert($commande, true);
164
            if ($retval) {
165
                $this->updateStocks($cmd_id);
166
            }
167
        }
168
169
        return $retval;
170
    }
171
}
172