MartinHotelCityHandler::insert()   B
last analyzed

Complexity

Conditions 8
Paths 26

Size

Total Lines 55

Duplication

Lines 55
Ratio 100 %

Importance

Changes 0
Metric Value
cc 8
nc 26
nop 2
dl 55
loc 55
rs 7.7373
c 0
b 0
f 0

How to fix   Long Method   

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
 * $Id: hotelcity.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 MartinHotelcity
16
 */
17
class MartinHotelcity extends XoopsObject
18
{
19
    public function MartinHotelcity()
20
    {
21
        $this->initVar("city_id", XOBJ_DTYPE_INT, null, false);
22
        $this->initVar("city_parentid", XOBJ_DTYPE_INT, null, false);
23
        $this->initVar("city_name", XOBJ_DTYPE_TXTBOX, null, true, 45);
24
        $this->initVar("city_alias", XOBJ_DTYPE_TXTBOX, null, false, 255);
25
        $this->initVar("city_level", XOBJ_DTYPE_TXTBOX, null, true, 45);
26
    }
27
28
    /**
29
     * @return mixed
30
     */
31
    public function city_id()
32
    {
33
        return $this->getVar("city_id");
34
    }
35
36
    /**
37
     * @return mixed
38
     */
39
    public function city_parentid()
40
    {
41
        return $this->getVar("city_parentid");
42
    }
43
44
    /**
45
     * @param string $format
46
     * @return mixed
47
     */
48
    public function city_name($format = 'S')
49
    {
50
        return $this->getVar("city_name", $format);
51
    }
52
53
    /**
54
     * @param string $format
55
     * @return mixed
56
     */
57
    public function city_alias($format = 'S')
58
    {
59
        return $this->getVar("city_alias", $format);
60
    }
61
62
    /**
63
     * @param string $format
64
     * @return mixed
65
     */
66
    public function city_level($format = 'S')
67
    {
68
        return $this->getVar("city_level", $format);
69
    }
70
}
71
72
/**
73
 * @method: HotelCityHandler
74
 * @license   http://www.blags.org/
75
 * @created   :2010年05月21日 20时40分
76
 * @copyright 1997-2010 The Martin Group
77
 * @author    Martin <[email protected]>
78
 * */
79
class MartinHotelCityHandler extends XoopsObjectHandler
80
{
81
    /**
82
     * create a new hotel city
83
     * @param bool $isNew flag the new objects as "new"?
84
     * @return object HotelCity
85
     */
86
    public function &create($isNew = true)
87
    {
88
        $hotelcity = new MartinHotelcity();
89
        if ($isNew) {
90
            $hotelcity->setNew();
91
        }
92
93
        return $hotelcity;
94
    }
95
96
    /**
97
     * retrieve a hotel city
98
     *
99
     * @param int $id hotelcityid of the hotelcity
100
     * @return mixed reference to the {@link HotelCity} object, FALSE if failed
101
     */
102 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...
103
    {
104
        if ((int)($id) <= 0) {
105
            return false;
106
        }
107
108
        $criteria = new CriteriaCompo(new Criteria('city_id', $id));
109
        $criteria->setLimit(1);
110
        $obj_array = $this->getObjects($criteria);
111
        if (count($obj_array) != 1) {
112
            $obj =& $this->create();
113
114
            return $obj;
115
        }
116
117
        return $obj_array[0];
118
    }
119
120
    /**
121
     * @得到列表
122
     * @method:
123
     * @license   http://www.blags.org/
124
     * @created   :2010年05月23日 14时59分
125
     * @copyright 1997-2010 The Martin Group
126
     * @author    Martin <[email protected]>
127
     * @param int    $limit
128
     * @param int    $start
129
     * @param int    $city_parentid
130
     * @param string $sort
131
     * @param string $order
132
     * @param bool   $id_as_key
133
     * @return array
134
     */
135
    public function &getHotelCitys($limit = 0, $start = 0, $city_parentid = 0, $sort = 'city_id', $order = 'ASC', $id_as_key = true)
136
    {
137
        $criteria = new CriteriaCompo();
138
139
        $criteria->setSort($sort);
140
        $criteria->setOrder($order);
141
142
        if ($city_parentid != -1) {
143
            $criteria->add(new Criteria('city_parentid', $city_parentid));
144
        }
145
146
        $criteria->setStart($start);
147
        $criteria->setLimit($limit);
148
149
        return $this->getObjects($criteria, $id_as_key);
150
    }
151
152
    /**
153
     * insert a new hotelcity in the database
154
     *
155
     * @param object $hotelcity reference to the {@link HotelCity} object
156
     * @param bool   $force
157
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
158
     */
159 View Code Duplication
    public function insert(&$hotelcity, $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...
160
    {
161
        if (strtolower(get_class($hotelcity)) !== 'martinhotelcity') {
162
            return false;
163
        }
164
165
        if (!$hotelcity->cleanVars()) {
166
            return false;
167
        }
168
169
        foreach ($hotelcity->cleanVars as $k => $v) {
170
            ${$k} = $v;
171
        }
172
173
        if ($hotelcity->isNew()) {
174
            $sql = sprintf("INSERT INTO %s (
175
                                city_id,
176
                                city_parentid,
177
                                city_name,
178
                                city_alias,
179
                                city_level
180
                            ) VALUES (
181
                                NULL,
182
                                %u,
183
                                %s,
184
                                %s,
185
                                %s
186
                            )", $this->db->prefix('martin_hotel_city'), $city_parentid, $this->db->quoteString($city_name), $this->db->quoteString($city_alias), $this->db->quoteString($city_level));
0 ignored issues
show
Bug introduced by
The variable $city_parentid 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 $city_name 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 $city_alias 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 $city_level 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...
187
        } else {
188
            $sql = sprintf("UPDATE %s SET
189
                                city_parentid = %u,
190
                                city_name = %s,
191
                                city_alias = %s,
192
                                city_level = %s
193
                            WHERE city_id = %u", $this->db->prefix('martin_hotel_city'), $city_parentid, $this->db->quoteString($city_name), $this->db->quoteString($city_alias), $this->db->quoteString($city_level), $city_id);
0 ignored issues
show
Bug introduced by
The variable $city_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...
194
        }
195
        //echo "<br />" . $sql . "<br />";
196
        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...
197
            $result = $this->db->queryF($sql);
198
        } else {
199
            $result = $this->db->query($sql);
200
        }
201
        if (!$result) {
202
            $hotelcity->setErrors('The query returned an error. ' . $this->db->error());
203
204
            return false;
205
        }
206
        if ($hotelcity->isNew()) {
207
            $hotelcity->assignVar('city_id', $this->db->getInsertId());
208
        }
209
210
        $hotelcity->assignVar('city_id', $city_id);
211
212
        return true;
213
    }
214
215
    /**
216
     * @删除一个城市
217
     * @method:delete(city_id)
218
     * @license   http://www.blags.org/
219
     * @created   :2010年05月21日 20时40分
220
     * @copyright 1997-2010 The Martin Group
221
     * @author    Martin <[email protected]>
222
     * @param object $hotelcity
223
     * @param bool   $force
224
     * @return bool|void
225
     */
226
    public function delete(&$hotelcity, $force = false)
227
    {
228
        if (strtolower(get_class($hotelcity)) !== 'martinhotelcity') {
229
            return false;
230
        }
231
232
        $subcats =& $this->getCityIds($hotelcity->city_id());
233
234
        $sql = "DELETE FROM " . $this->db->prefix("martin_hotel_city") . " WHERE city_id IN ( " . implode(",", $subcats) . " )";
235
236
        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...
237
            $result = $this->db->queryF($sql);
238
        } else {
239
            $result = $this->db->query($sql);
240
        }
241
242
        if (!$result) {
243
            return false;
244
        }
245
246
        return true;
247
    }
248
249
    /**
250
     * @ 得到所有子类
251
     * @license   http://www.blags.org/
252
     * @created   :2010年05月21日 20时40分
253
     * @copyright 1997-2010 The Martin Group
254
     * @author    Martin <[email protected]>
255
     * @param $city_id
256
     * @return array
257
     */
258
    public function getCityIds($city_id)
259
    {
260
        if (!is_array($city_id)) {
261
            $city_id = array((int)($city_id));
262
        }
263
        $cities = $city_id;
264
        //var_dump($cities);exit;
265
        $sql = "SELECT city_id FROM " . $this->db->prefix("martin_hotel_city") . " WHERE city_parentid IN (" . implode(",", $cities) . ")";
266
        //echo $sql;exit;
267
268
        $result = $this->db->query($sql);
269
        while ($row = $this->db->fetchArray($result)) {
270
            $cities[] = (int)$row['city_id'];
271
        }
272
        $cities = array_unique($cities);
273
        //var_dump($cities);exit;
274
        //var_dump($city_id);exit;
275
        $isOver = array_diff($cities, $city_id);
276
        //var_dump($isOver);exit;
277
        if (empty($isOver)) {
278
            return $cities;
279
        }
280
281
        return $this->getCityIds($cities);
282
    }
283
284
    /**
285
     * delete hotel cities matching a set of conditions
286
     *
287
     * @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...
288
     * @return bool FALSE if deletion failed
289
     */
290 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...
291
    {
292
        $sql = 'DELETE FROM ' . $this->db->prefix('martin_hotel_city');
293
        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...
294
            $sql .= ' ' . $criteria->renderWhere();
295
        }
296
        if (!$result = $this->db->query($sql)) {
297
            return false;
298
        }
299
300
        return true;
301
    }
302
303
    /**
304
     * count hotel cities matching a condition
305
     *
306
     * @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...
307
     * @return int count of categories
308
     */
309 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...
310
    {
311
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('martin_hotel_city');
312
        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...
313
            $sql .= ' ' . $criteria->renderWhere();
314
        }
315
        $result = $this->db->query($sql);
316
        if (!$result) {
317
            return 0;
318
        }
319
        list($count) = $this->db->fetchRow($result);
320
321
        return $count;
322
    }
323
324
    /**
325
     * @得到城市
326
     * @license   http://www.blags.org/
327
     * @created   :2010年05月21日 20时40分
328
     * @copyright 1997-2010 The Martin Group
329
     * @author    Martin <[email protected]>
330
     * @param null $criteria
331
     * @param bool $id_as_key
332
     * @return array
333
     */
334 View Code Duplication
    public function &getObjects($criteria = null, $id_as_key = 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...
335
    {
336
        $ret   = array();
337
        $limit = $start = 0;
338
        $sql   = 'SELECT * FROM ' . $this->db->prefix('martin_hotel_city');
339
        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...
340
            $sql .= ' ' . $criteria->renderWhere();
341
            if ($criteria->getSort() != '') {
342
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
343
            }
344
            $limit = $criteria->getLimit();
345
            $start = $criteria->getStart();
346
        }
347
        //echo "<br />" . $sql . "<br />";
348
        $result = $this->db->query($sql, $limit, $start);
349
350
        if (!$result) {
351
            return $ret;
352
        }
353
354
        $theObjects = array();
355
356
        while ($myrow = $this->db->fetchArray($result)) {
357
            $hotelcity = new MartinHotelcity();
358
            $hotelcity->assignVars($myrow);
359
            $theObjects[$myrow['city_id']] =& $hotelcity;
360
            //var_dump($hotelcity);
361
            unset($hotelcity);
362
        }
363
        //var_dump($theObjects);
364
365
        foreach ($theObjects as $theObject) {
366
            if (!$id_as_key) {
367
                $ret[] =& $theObject;
368
            } else {
369
                $ret[$theObject->city_id()] =& $theObject;
370
            }
371
            unset($theObject);
372
        }
373
374
        return $ret;
375
    }
376
377
    /**
378
     * @get       city tree
379
     * @license   http://www.blags.org/
380
     * @created   :2010年05月29日 11时31分
381
     * @copyright 1997-2010 The Martin Group
382
     * @author    Martin <[email protected]>
383
     * @param        $name
384
     * @param        $city_id
385
     * @param string $prefix
386
     * @return string
387
     */
388
    public function &getTree($name, $city_id, $prefix = '--')
389
    {
390
        $mytree = new XoopsTree($this->db->prefix("martin_hotel_city"), "city_id", "city_parentid");
391
        // Parent Category
392
        ob_start();
393
        $mytree->makeMySelBox("city_name", "", $city_id, 1, $name);
394
        //makeMySelBox($title,$order="",$preset_id=0, $none=0, $sel_name="", $onchange="")
395
        $str = ob_get_contents();
396
        ob_end_clean();
397
398
        return $str;
399
    }
400
}
401