Passed
Push — master ( 2ecc51...c1b656 )
by Michael
04:26 queued 02:02
created

efqGmap::efqGmap()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 19

Duplication

Lines 10
Ratio 41.67 %

Importance

Changes 0
Metric Value
cc 4
eloc 19
nc 4
nop 1
dl 10
loc 24
rs 8.6845
c 0
b 0
f 0
1
<?php
2
3
/*
4
// ID: category.php 3-nov-2007 18:18:06 efqconsultancy
5
//  ------------------------------------------------------------------------ //
6
//                				EFQ Directory			                     //
7
//                    Copyright (c) 2006 EFQ Consultancy                     //
8
//                       <http://www.efqdirectory.com/>                      //
9
//  ------------------------------------------------------------------------ //
10
//  This program is free software; you can redistribute it and/or modify     //
11
//  it under the terms of the GNU General Public License as published by     //
12
//  the Free Software Foundation; either version 2 of the License, or        //
13
//  (at your option) any later version.                                      //
14
//                                                                           //
15
//  You may not change or alter any portion of this comment or credits       //
16
//  of supporting developers from this source code or any supporting         //
17
//  source code which is considered copyrighted (c) material of the          //
18
//  original comment or credit authors.                                      //
19
//                                                                           //
20
//  This program is distributed in the hope that it will be useful,          //
21
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
22
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
23
//  GNU General Public License for more details.                             //
24
//                                                                           //
25
//  You should have received a copy of the GNU General Public License        //
26
//  along with this program; if not, write to the Free Software              //
27
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
28
//  ------------------------------------------------------------------------ //
29
//	Part of the efqDirectory module provided by: wtravel					 //
30
// 	e-mail: [email protected]											 //
31
//	Purpose: Create a business directory for xoops.		 	 				 //
32
//	Based upon the mylinks and the mxDirectory modules						 //
33
// ------------------------------------------------------------------------- //
34
*/
35
36
/**
37
 * Class efqGmap
38
 * Manages operations for google maps
39
 *
40
 * @package   efqDirectory
41
 * @author    EFQ Consultancy <[email protected]>
42
 * @copyright EFQ Consultancy (c) 2007
43
 * @version   1.1.0
44
 */
45
class efqGmap extends XoopsObject
0 ignored issues
show
Bug introduced by
The type XoopsObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
46
{
47
    private $_lon; //longitude
0 ignored issues
show
Unused Code introduced by
The property $_lon is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
48
    private $_lat; //lattitude
0 ignored issues
show
Unused Code introduced by
The property $_lat is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
49
    private $_descr; //description
0 ignored issues
show
Unused Code introduced by
The property $_descr is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
50
    private $_key; //developer's key
51
    private $_zoomlevel; //zoom level of google map
0 ignored issues
show
Unused Code introduced by
The property $_zoomlevel is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
52
    private $_jsPointsArray; //javascript assigning points to google map
53
    private $_map; // generate output for showing the map on a web page
54
    public $db;
55
56
    /**
57
     * efqGmap constructor.
58
     * @param bool $gmap
59
     */
60
    public function __construct($gmap = false)
61
    {
62
        global $xoopsModuleConfig;
63
        $key = $xoopsModuleConfig['gmapkey'];
64
        $this->setKey($key);
65
        $this->setPointsJS('');
66
67
        $this->db = XoopsDatabaseFactory::getDatabaseConnection();
0 ignored issues
show
Bug introduced by
The type XoopsDatabaseFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
68
        $this->initVar('id', XOBJ_DTYPE_INT, null, false);
0 ignored issues
show
Bug introduced by
The constant XOBJ_DTYPE_INT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
69
        $this->initVar('dataid', XOBJ_DTYPE_INT, null, true);
70
        $this->initVar('lat', XOBJ_DTYPE_TXTBOX, null, true);
0 ignored issues
show
Bug introduced by
The constant XOBJ_DTYPE_TXTBOX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
71
        $this->initVar('lon', XOBJ_DTYPE_TXTBOX, null, true);
72
        $this->initVar('descr', XOBJ_DTYPE_TXTAREA);
0 ignored issues
show
Bug introduced by
The constant XOBJ_DTYPE_TXTAREA was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
73
74 View Code Duplication
        if ($gmap !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
75
            if (is_array($gmap)) {
76
                $this->assignVars($gmap);
77
            } else {
78
                $$gmapHandler = xoops_getModuleHandler('gmap', $moddir);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $moddir seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $gmapHandler seems to be never defined.
Loading history...
Bug introduced by
The function xoops_getModuleHandler was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
                $$gmapHandler = /** @scrutinizer ignore-call */ xoops_getModuleHandler('gmap', $moddir);
Loading history...
79
                $objGmap       =& $$gmapHandler->get($directory);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $directory seems to be never defined.
Loading history...
80
                foreach ($objGmap->vars as $k => $v) {
81
                    $this->assignVar($k, $v['value']);
82
                }
83
                unset($objGmap);
84
            }
85
        }
86
    }
87
88
    /**
89
     * @param string $key
90
     */
91
    public function setKey($key = '')
92
    {
93
        $this->_key = $key;
94
    }
95
96
    /**
97
     * Set the value of the script that triggers the points to be added to the google map.
98
     * @param $pointsJS
99
     */
100
    public function setPointsJS($pointsJS)
101
    {
102
        $this->_jsPointsArray = $pointsJS;
103
        $this->_map           = '';
104
    }
105
106
    /**
107
     * Get the value of the script that triggers the points to be added to the google map.
108
     */
109
    public function getPointsJS()
110
    {
111
        return $this->_jsPointsArray;
112
    }
113
114
    /**
115
     * @return mixed
116
     */
117
    public function showMap()
118
    {
119
        return $this->_map;
120
    }
121
122
    public function generateMap()
123
    {
124
        $this->_map .= $this->printPlaceHolder();
125
        $this->_map .= $this->printScript($this->_jsPointsArray, $this->_key);
126
        $this->_map .= $this->printTrigger();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->printTrigger() targeting efqGmap::printTrigger() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
127
    }
128
129
    /**
130
     * @param int $width
131
     * @param int $height
132
     * @return string
133
     */
134
    public function printPlaceHolder($width = 700, $height = 500)
135
    {
136
        return '<div id="map" style="width:' . $width . 'px; height:' . $height . 'px"></div>';
137
    }
138
139
    /**
140
     * @param string $jsPointsArray
141
     * @param string $key
142
     * @return string
143
     */
144
    public function printScript($jsPointsArray = '', $key = '')
145
    {
146
        global $icmsPreloadHandler;
147
148
        $icmsPreloadHandler->addPreloadEvents('gmap.php');
149
        $gmapScript = <<<EOH
150
<script type="text/javascript">
151
var mArray = Array();
152
var map;
153
var centerPoint = new GLatLng(40.078071,-101.689453);
154
155
function load() {
156
	doLoad();
157
	$jsPointsArray;
158
	addMarkers();
159
}
160
161
function doLoad() {
162
	if (GBrowserIsCompatible()) {
163
		map = new GMap2(document.getElementById("map"));
164
		map.setCenter(centerPoint, 7);
165
		map.addControl(new GScaleControl());
166
		map.addControl(new GLargeMapControl());
167
		map.addControl(new GMapTypeControl());
168
		GEvent.addListener(map, 'click', mapClick);
169
170
	}
171
}
172
173
function addMarkers() {
174
	if (mArray.length) {
175
		var bounds = new GLatLngBounds();
176
		for (n=0 ; n < mArray.length ; n++ ) {
177
			var mData = mArray[n].split(';');
178
			var point = new GLatLng(mData[0],mData[1]);
179
			bounds.extend(point);
180
			var marker = createMarker(point, mData[2]);
181
			map.addOverlay(marker);
182
		}
183
		map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds)); 
184
	}
185
}
186
187
function createMarker(point, title) {
188
	var marker = new GMarker(point,{title:title});
189
	GEvent.addListener(marker, "click", function() {
190
		marker.openInfoWindowHtml('<div style="width:250px;">' + title + '<hr>Lat: ' + point.y + '<br>Lon: ' + point.x + '</div>');
191
	});
192
	return marker;
193
}
194
195
function mapClick(marker, point) {
196
	if (!marker) {
197
		oLat = document.getElementById("lat");
198
		oLat.value = point.y;
199
		oLon = document.getElementById("lon");
200
		oLon.value = point.x;
201
		oDesc = document.getElementById("desc");
202
		oDesc.value = 'New point';
203
204
205
	}
206
}
207
</script>\n
208
EOH;
209
210
        return $gmapScript;
211
    }
212
213
    /**
214
     * Adds a script which triggers the other javascript code to execute
215
     */
216
    public function printTrigger()
217
    {
218
        $trigger = <<<EOH
0 ignored issues
show
Unused Code introduced by
The assignment to $trigger is dead and can be removed.
Loading history...
219
<script>		
220
if (window.onload){
221
    var oldonload = window.onload;
222
    window.onload=function(){
223
        oldonload();
224
        yourfunctionhere();
225
    }
226
}else{
227
    window.onload=function(){
228
        yourfunctionhere();
229
    }
230
}
231
</script>\n
232
EOH;
233
    }
234
235
    /**
236
     * Function setData sets class from array.
237
     * @author    EFQ Consultancy <[email protected]>
238
     * @copyright EFQ Consultancy (c) 2008
239
     * @version   1.0.0
240
     *
241
     * @param $arr
242
     * @return bool true if insertion is succesful, false if unsuccesful
243
     * @internal  param object $obj object
244
     *
245
     */
246 View Code Duplication
    public function setData($arr)
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...
247
    {
248
        if (is_array($arr)) {
249
            $vars = $this->getVars();
250
            foreach ($vars as $k => $v) {
251
                $this->setVar($k, $arr[$k]);
252
            }
253
        } else {
254
            return false;
255
        }
256
257
        return true;
258
    }
259
}
260
261
/**
262
 * Class efqGmapHandler
263
 * Manages database operations for google maps
264
 *
265
 * @package   efqDirectory
266
 * @author    EFQ Consultancy <[email protected]>
267
 * @copyright EFQ Consultancy (c) 2007
268
 * @version   1.1.0
269
 */
270
class efqGmapHandler extends XoopsObjectHandler
0 ignored issues
show
Bug introduced by
The type XoopsObjectHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
271
{
272
//    public $db;
273
274
    /**
275
     * efqGmapHandler constructor.
276
     */
277
    public function __construct()
278
    {
279
        $this->db = XoopsDatabaseFactory::getDatabaseConnection();
280
    }
281
282
    /**
283
     * create instance of efqGmap class or reset the existing instance.
284
     *
285
     * @param bool $isNew
286
     * @return efqGmap $efqGmap
287
     */
288
    public function &create($isNew = true)
289
    {
290
        $gmap = new efqGmap();
291
        if ($isNew) {
292
            $gmap->setNew();
293
        }
294
295
        return $gmap;
296
    }
297
298
    /**
299
     * retrieve all points from the database
300
     *
301
     * @param $gmap
302
     * @return mixed reference to the <a href='psi_element://efqGmap'>efqGmap</a> object, FALSE if failed
303
     * object, FALSE if failed
304
     * @internal param int $dirid ID of the directory
305
     */
306
    public function getPointsJS($gmap)
307
    {
308
        if (!is_object($gmap)) {
309
            return false;
310
        }
311
        $sql = 'SELECT * FROM ' . $this->db->prefix('efqdiralpha1_gmaps');
312
        if (!$result = $this->db->query($sql)) {
313
            return false;
314
        }
315
        $gmap       =& $this->create(false);
316
        $javaScript = '';
317
        while ($row = $this->db->fetchArray($result)) {
318
            //$row{'descr'} = addslashes($row{'descr'});
0 ignored issues
show
Unused Code Comprehensibility introduced by
74% 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...
319
            $row['descr'] = addslashes($row['descr']);
320
            //$row{'descr'} = str_replace(';',',',$row{'descr'});
0 ignored issues
show
Unused Code Comprehensibility introduced by
79% 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...
321
            $row['descr'] = str_replace(';', ',', $row['descr']);
322
            //$javaScript .= "mArray.push('{$row{'lat'}};{$row{'lon'}};{$row{'descr'}}')\n";
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
323
            //echo $row['lat'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% 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...
324
            $javaScript .= "mArray.push('{" . $row['lat'] . '};{' . $row['lon'] . '};{' . $row['descr'] . "}')\n";
325
        }
326
        $gmap->setPointsJS($javaScript);
327
328
        return true;
329
    }
330
331
    /**
332
     * @param int $id
333
     * @return array
334
     */
335
    public function getGmapById($id = 0)
336
    {
337
        $arr = array();
338
        $sql = sprintf('SELECT * FROM %s WHERE id=%u', $this->db->prefix('efqdiralpha1_gmaps'), (int)$id);
339
        $result = $this->db->query($sql) or $eh->show('0013');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $eh seems to be never defined.
Loading history...
340 View Code Duplication
        while (list($id, $lat, $lon, $descr, $dataid) = $this->db->fetchRow($result)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
341
            $arr = array('id' => $id, 'lat' => $lat, 'lon' => $lon, 'descr' => $descr, 'dataid' => $dataid);
342
        }
343
344
        return $arr;
345
    }
346
347
    /**
348
     * @param int $id
349
     * @return bool|efqGmap
350
     */
351 View Code Duplication
    public function getByDataId($id = 0)
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...
352
    {
353
        if ($id === false) {
354
            return false;
355
        }
356
        $id = (int)$id;
357
        echo $id;
358
        if ($id > 0) {
359
            $sql = 'SELECT * FROM ' . $this->db->prefix('efqdiralpha1_gmaps') . ' WHERE dataid=' . (int)$id;
360
            if (!$result = $this->db->query($sql)) {
361
                return false;
362
            }
363
            $gmap =& $this->create(false);
364
            $gmap->assignVars($this->db->fetchArray($result));
365
366
            return $gmap;
367
        }
368
369
        return false;
370
    }
371
372
    /**
373
     * Function insertGmap inserts google map data into DB
374
     * @author    EFQ Consultancy <[email protected]>
375
     * @copyright EFQ Consultancy (c) 2008
376
     * @version   1.0.0
377
     *
378
     * @param   efqGmap $obj object
379
     *
380
     * @param bool     $forceQuery
381
     * @return bool true if insertion is succesful, false if unsuccesful
382
     */
383 View Code Duplication
    public function insertGmap($obj, $forceQuery = 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...
384
    {
385
        $tablename = 'efqdiralpha1_gmaps';
386
        $keyName   = 'id';
387
        if ($obj instanceof efqGmap) {
388
            // Variable part of this function ends. From this line you can copy
389
            // this function for similar object handling functions.
390
            $obj->cleanVars();
391
            $cleanvars = $obj->cleanVars;
392
        } else {
393
            return false;
394
        }
395
        $countVars = count($cleanvars);
396
        $i         = 1;
397
        $strFields = '';
398
        $strValues = '';
399
        foreach ($cleanvars as $k => $v) {
400
            $strFields .= $k;
401
            $strValues .= "'" . $v . "'";
402
            if ($i < $countVars) {
403
                $strFields .= ', ';
404
                $strValues .= ', ';
405
            }
406
            $i++;
407
        }
408
        $sql = sprintf('INSERT INTO %s (%s) VALUES (%s)', $this->db->prefix($tablename), $strFields, $strValues);
409
        if ($forceQuery) {
410
            if ($this->db->queryF($sql)) {
411
                $itemid = $this->db->getInsertId();
412
                $obj->setVar($keyName, $itemid);
413
414
                return true;
415
            }
416
        } else {
417
            if ($this->db->query($sql)) {
418
                $itemid = $this->db->getInsertId();
419
                $obj->setVar($keyName, $itemid);
420
421
                return true;
422
            }
423
        }
424
425
        return false;
426
    }
427
428
    /**
429
     * Function updateGmap updates google map data
430
     * @author    EFQ Consultancy <[email protected]>
431
     * @copyright EFQ Consultancy (c) 2008
432
     * @version   1.0.0
433
     *
434
     * @param   efqGmap $obj object
435
     *
436
     * @param bool     $forceQuery
437
     * @return bool true if update is succesful, false if unsuccesful
438
     */
439 View Code Duplication
    public function updateGmap($obj, $forceQuery = 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...
440
    {
441
        $tablename = 'efqdiralpha1_gmaps';
442
        $keyName   = 'id';
443
        if ($obj instanceof efqGmap) {
444
            // Variable part of this function ends. From this line you can copy
445
            // this function for similar object handling functions.
446
            $obj->cleanVars();
447
            $cleanvars = $obj->cleanVars;
448
            $keyValue  = $obj->getVar($keyName);
449
        } else {
450
            return false;
451
        }
452
        $countVars = count($cleanvars);
453
        $i         = 1;
454
        $strSet    = '';
455
        $strValues = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $strValues is dead and can be removed.
Loading history...
456
        foreach ($cleanvars as $k => $v) {
457
            if ($k !== 'id') {
458
                $strSet .= $k . '=' . "'" . $v . "'";
459
                if ($i < $countVars) {
460
                    $strSet .= ', ';
461
                }
462
            }
463
            $i++;
464
        }
465
        $sql = sprintf('UPDATE %s SET %s WHERE %s = %u', $this->db->prefix($tablename), $strSet, $keyName, $keyValue);
466
        if ($forceQuery) {
467
            if ($this->db->queryF($sql)) {
468
                return true;
469
            }
470
        } else {
471
            if ($this->db->query($sql)) {
472
                return true;
473
            }
474
        }
475
476
        return false;
477
    }
478
}
479