Completed
Push — master ( 5be1c3...395766 )
by Michael
26:05
created

MartinHotelPromotionHandler::insert()   C

Complexity

Conditions 7
Paths 18

Size

Total Lines 43
Code Lines 18

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 43
rs 6.7273
cc 7
eloc 18
nc 18
nop 2
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 17 and the first side effect is on line 9.

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
 * $Id: promotion.php,v 1.42 2007/02/04 15:01:40 malanciault Exp $
4
 * Module:martin
5
 * Licence: GNU
6
 */
7
8
if (!defined("XOOPS_ROOT_PATH")) {
9
    die("XOOPS root path not defined");
10
}
11
12
include_once XOOPS_ROOT_PATH . '/modules/martin/include/common.php';
13
14
/**
15
 * Class MartinHotelPromotion
16
 */
17
class MartinHotelPromotion extends XoopsObject
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...
18
{
19
    public function MartinHotelPromotion()
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
20
    {
21
        $this->initVar("promotion_id", XOBJ_DTYPE_INT, null, false);
22
        $this->initVar("hotel_id", XOBJ_DTYPE_INT, null, false);
23
        $this->initVar("hotel_name", XOBJ_DTYPE_TXTBOX, null, true, 255);
24
        $this->initVar("promotion_start_date", XOBJ_DTYPE_INT, null, false);
25
        $this->initVar("promotion_end_date", XOBJ_DTYPE_INT, null, false);
26
        $this->initVar("promotion_description", XOBJ_DTYPE_TXTAREA, null, false);
27
        $this->initVar("promotion_add_time", XOBJ_DTYPE_INT, null, false);
28
    }
29
30
    /**
31
     * @return mixed
32
     */
33
    public function promotion_id()
34
    {
35
        return $this->getVar("promotion_id");
36
    }
37
38
    /**
39
     * @return mixed
40
     */
41
    public function hotel_id()
42
    {
43
        return $this->getVar("hotel_id");
44
    }
45
46
    /**
47
     * @return mixed
48
     */
49
    public function hotel_name()
50
    {
51
        return $this->getVar("hotel_name");
52
    }
53
54
    /**
55
     * @return mixed
56
     */
57
    public function promotion_start_date()
58
    {
59
        return $this->getVar("promotion_start_date");
60
    }
61
62
    /**
63
     * @return mixed
64
     */
65
    public function promotion_end_date()
66
    {
67
        return $this->getVar("promotion_end_date");
68
    }
69
70
    /**
71
     * @param string $format
72
     * @return mixed
73
     */
74
    public function promotion_description($format = 'edit')
75
    {
76
        return $this->getVar("promotion_description", $format);
77
    }
78
79
    /**
80
     * @return mixed
81
     */
82
    public function promotion_add_time()
83
    {
84
        return $this->getVar("promotion_add_time");
85
    }
86
}
87
88
/**
89
 * @method: MartinHotelPromotionHandler
90
 * @license   http://www.blags.org/
91
 * @created   :2010年05月21日 20时40分
92
 * @copyright 1997-2010 The Martin promotion
93
 * @author    Martin <[email protected]>
94
 * */
95
class MartinHotelPromotionHandler extends XoopsObjectHandler
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...
96
{
97
    /**
98
     * create a new hotel city
99
     * @param bool $isNew flag the new objects as "new"?
100
     * @return object promotion
101
     */
102
    public function &create($isNew = true)
103
    {
104
        $promotion = new MartinHotelPromotion();
105
        if ($isNew) {
106
            $promotion->setNew();
107
        }
108
109
        return $promotion;
110
    }
111
112
    /**
113
     * retrieve a hotel city
114
     *
115
     * @param int $id promotionid of the hotel promotion
116
     * @return mixed reference to the {@link promotion} object, FALSE if failed
117
     */
118 View Code Duplication
    public function &get($id)
0 ignored issues
show
Duplication introduced by
This method 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...
119
    {
120
        if ((int)($id) <= 0) {
121
            return false;
122
        }
123
124
        $criteria = new CriteriaCompo(new Criteria('promotion_id', $id));
125
        $criteria->setLimit(1);
126
        $obj_array =& $this->getObjects($criteria);
127
        if (count($obj_array) != 1) {
128
            $obj =& $this->create();
129
130
            return $obj;
131
        }
132
133
        return $obj_array[0];
134
    }
135
136
    /**
137
     * @得到列表
138
     * @method:
139
     * @license   http://www.blags.org/
140
     * @created   :2010年05月23日 14时59分
141
     * @copyright 1997-2010 The Martin promotion
142
     * @author    Martin <[email protected]>
143
     * @param int    $limit
144
     * @param int    $start
145
     * @param string $sort
146
     * @param string $order
147
     * @param bool   $id_as_key
148
     * @return array
149
     */
150 View Code Duplication
    public function &getPromotions($limit = 0, $start = 0, $sort = 'promotion_add_time', $order = 'DESC', $id_as_key = true)
0 ignored issues
show
Duplication introduced by
This method 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...
151
    {
152
        $criteria = new CriteriaCompo();
153
154
        $criteria->setSort($sort);
155
        $criteria->setOrder($order);
156
157
        $criteria->setStart($start);
158
        $criteria->setLimit($limit);
159
160
        return $this->getObjects($criteria, $id_as_key);
161
    }
162
163
    /**
164
     * insert a new promotion in the database
165
     *
166
     * @param object $promotion reference to the {@link hotel promotion} object
167
     * @param bool   $force
168
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
169
     */
170
    public function insert(&$promotion, $force = false)
171
    {
172
        if (strtolower(get_class($promotion)) !== 'martinhotelpromotion') {
173
            return false;
174
        }
175
176
        if (!$promotion->cleanVars()) {
177
            return false;
178
        }
179
180
        foreach ($promotion->cleanVars as $k => $v) {
181
            ${$k} = $v;
182
        }
183
184
        if ($promotion->isNew()) {
185
            $sql = sprintf("INSERT INTO %s (
186
                                promotion_id,
187
                                promotion_description,
188
                                hotel_id,
189
                                promotion_start_date,
190
                                promotion_end_date,
191
                                promotion_add_time
192
                            ) VALUES (
193
                                NULL,
194
                                %s,%u,%u,%u,%u
195
                            )", $this->db->prefix('martin_hotel_promotions'), $this->db->quoteString($promotion_description), $hotel_id, $promotion_start_date, $promotion_end_date, $promotion_add_time);
0 ignored issues
show
Bug introduced by
The variable $promotion_description does not exist. Did you mean $promotion?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Bug introduced by
The variable $hotel_id does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $promotion_start_date does not exist. Did you mean $promotion?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Bug introduced by
The variable $promotion_end_date does not exist. Did you mean $promotion?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Bug introduced by
The variable $promotion_add_time does not exist. Did you mean $promotion?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
196
        } else {
197
            $sql = sprintf("UPDATE %s SET
198
                                promotion_description = %s,
199
                                hotel_id = %u,
200
                                promotion_start_date = %u,
201
                                promotion_end_date = %u
202
                            WHERE promotion_id = %u", $this->db->prefix('martin_hotel_promotions'), $this->db->quoteString($promotion_description), $hotel_id, $promotion_start_date, $promotion_end_date, $promotion_id);
0 ignored issues
show
Bug introduced by
The variable $promotion_description does not exist. Did you mean $promotion?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Bug introduced by
The variable $promotion_start_date does not exist. Did you mean $promotion?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Bug introduced by
The variable $promotion_end_date does not exist. Did you mean $promotion?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Bug introduced by
The variable $promotion_id does not exist. Did you mean $promotion?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
203
        }
204
        //echo $sql;exit;
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% 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...
205
        if (false != $force) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
206
            $result = $this->db->queryF($sql);
0 ignored issues
show
Unused Code introduced by
$result 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...
207
        } else {
208
            $result = $this->db->query($sql);
0 ignored issues
show
Unused Code introduced by
$result 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...
209
        }
210
211
        return $promotion_id > 0 ? $promotion_id : $this->db->getInsertId();
0 ignored issues
show
Bug introduced by
The variable $promotion_id does not exist. Did you mean $promotion?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
212
    }
213
214
    /**
215
     * @删除一个城市
216
     * @method:delete(promotion_id)
217
     * @license   http://www.blags.org/
218
     * @created   :2010年05月21日 20时40分
219
     * @copyright 1997-2010 The Martin promotion
220
     * @author    Martin <[email protected]>
221
     * @param object $promotion
222
     * @param bool   $force
223
     * @return bool|void
224
     */
225 View Code Duplication
    public function delete(&$promotion, $force = false)
0 ignored issues
show
Duplication introduced by
This method 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...
226
    {
227
        if (strtolower(get_class($promotion)) !== 'martinhotelpromotion') {
228
            return false;
229
        }
230
231
        $sql = "DELETE FROM " . $this->db->prefix("martin_hotel_promotions") . " WHERE promotion_id = " . $promotion->promotion_id();
232
233
        if (false != $force) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
234
            $result = $this->db->queryF($sql);
235
        } else {
236
            $result = $this->db->query($sql);
237
        }
238
239
        if (!$result) {
240
            return false;
241
        }
242
243
        return true;
244
    }
245
246
    /**
247
     * delete hotel cities matching a set of conditions
248
     *
249
     * @param object $criteria {@link CriteriaElement}
0 ignored issues
show
Documentation introduced by
Should the type for parameter $criteria not be object|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
250
     * @return bool FALSE if deletion failed
251
     */
252 View Code Duplication
    public function deleteAll($criteria = null)
0 ignored issues
show
Duplication introduced by
This method 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...
253
    {
254
        $sql = 'DELETE FROM ' . $this->db->prefix('martin_hotel_promotions');
255
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
256
            $sql .= ' ' . $criteria->renderWhere();
257
        }
258
        if (!$result = $this->db->query($sql)) {
259
            return false;
260
        }
261
262
        return true;
263
    }
264
265
    /**
266
     * count hotel cities matching a condition
267
     *
268
     * @param object $criteria {@link CriteriaElement} to match
0 ignored issues
show
Documentation introduced by
Should the type for parameter $criteria not be object|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
269
     * @return int count of categories
270
     */
271 View Code Duplication
    public function getCount($criteria = null)
0 ignored issues
show
Duplication introduced by
This method 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...
272
    {
273
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('martin_hotel_promotions');
274
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
275
            $sql .= ' ' . $criteria->renderWhere();
276
        }
277
        $result = $this->db->query($sql);
278
        if (!$result) {
279
            return 0;
280
        }
281
        list($count) = $this->db->fetchRow($result);
282
283
        return $count;
284
    }
285
286
    /**
287
     * @得到城市
288
     * @license   http://www.blags.org/
289
     * @created   :2010年05月21日 20时40分
290
     * @copyright 1997-2010 The Martin promotion
291
     * @author    Martin <[email protected]>
292
     * @param null $criteria
293
     * @param bool $id_as_key
294
     * @return array
295
     */
296
    public function &getObjects($criteria = null, $id_as_key = false)
297
    {
298
        $ret   = array();
299
        $limit = $start = 0;
300
        $sql   = 'SELECT p.*,h.hotel_name FROM ' . $this->db->prefix('martin_hotel_promotions') . ' p ';
301
        $sql .= ' left join ' . $this->db->prefix('martin_hotel') . ' h on ( h.hotel_id = p.hotel_id ) ';
302
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
303
            $sql .= ' ' . $criteria->renderWhere();
304
            if ($criteria->getSort() != '') {
305
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
306
            }
307
            $limit = $criteria->getLimit();
308
            $start = $criteria->getStart();
309
        }
310
        $sql .= " order by p.promotion_id DESC ";
311
        //echo "<br />" . $sql . "<br />";
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% 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...
312
        $result = $this->db->query($sql, $limit, $start);
313
314
        if (!$result) {
315
            return $ret;
316
        }
317
318
        $theObjects = array();
319
320
        while ($myrow = $this->db->fetchArray($result)) {
321
            $promotion = new MartinHotelPromotion();
322
            $promotion->assignVars($myrow);
323
            $theObjects[$myrow['promotion_id']] =& $promotion;
324
            //var_dump($promotion);
325
            unset($promotion);
326
        }
327
        //var_dump($theObjects);
328
329
        foreach ($theObjects as $theObject) {
330
            if (!$id_as_key) {
331
                $ret[] =& $theObject;
332
            } else {
333
                $ret[$theObject->promotion_id()] =& $theObject;
334
            }
335
            unset($theObject);
336
        }
337
338
        return $ret;
339
    }
340
341
    /**
342
     * @get       hotel promotion
343
     * @license   http://www.blags.org/
344
     * @created   :2010年06月14日 20时47分
345
     * @copyright 1997-2010 The Martin Group
346
     * @author    Martin <[email protected]>
347
     * @param $hotel_id
348
     * @return string
349
     */
350
    public function getHotelPromotion($hotel_id)
351
    {
352
        global $xoopsDB;
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...
353
        if (!$hotel_id > 0) {
354
            return '';
355
        }
356
        $sql = 'SELECT * FROM ' . $xoopsDB->prefix('martin_hotel_promotions') . ' WHERE
357
            ' . time() . ' BETWEEN  promotion_start_date AND promotion_end_date  AND hotel_id = ' . $hotel_id . '  limit 1 ';
358
359
        return $xoopsDB->fetchArray($xoopsDB->query($sql));
360
    }
361
}
362