Completed
Pull Request — master (#206)
by Kiran
05:47
created

geodir_homepage_map   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 305
Duplicated Lines 2.62 %

Coupling/Cohesion

Components 0
Dependencies 0
Metric Value
wmc 27
lcom 0
cbo 0
dl 8
loc 305
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
F widget() 0 48 9
A update() 0 14 4
D form() 8 112 12
B geodir_home_map_add_script() 0 76 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * GeoDirectory GMap - Home page Widget
4
 *
5
 * This will display Google map on home page with use of Google Map Api V3.
6
 *
7
 * @since 1.0.0
8
 *
9
 * @package GeoDirectory
10
 */
11
12
/**
13
 * GeoDirectory home page map widget class.
14
 *
15
 * @since 1.0.0
16
 */
17
class geodir_homepage_map extends WP_Widget
18
{
19
    /**
20
	 * Register the home page map widget.
21
	 *
22
	 * @since 1.0.0
23
     * @since 1.5.1 Changed from PHP4 style constructors to PHP5 __construct.
24
	 */
25
    public function __construct() {
26
        $widget_ops = array('classname' => 'widget Google Map in Home page', 'description' => __('Google Map in Home page. It will show you google map V3 for Home page with category checkbox selection.', 'geodirectory'));
27
        parent::__construct(
28
            'geodir_map_v3_home_map', // Base ID
29
            __('GD > GMap - Home page', 'geodirectory'), // Name
30
            $widget_ops// Args
31
        );
32
    }
33
34
	/**
35
	 * Front-end display content for home page map widget.
36
	 *
37
	 * @since 1.0.0
38
     * @since 1.5.1 Declare function public.
39
	 *
40
	 * @param array $args     Widget arguments.
41
	 * @param array $instance Saved values from database.
42
	 */
43
    public function widget($args, $instance)
44
    {
45
        extract($args, EXTR_SKIP);
46
        /** This action is documented in geodirectory_shortcodes.php */
47
        $width = empty($instance['width']) ? '100%' : apply_filters('widget_width', $instance['width']);
48
        /** This action is documented in geodirectory_shortcodes.php */
49
        $height = empty($instance['heigh']) ? '425' : apply_filters('widget_heigh', $instance['heigh']);
50
        /** This action is documented in geodirectory_shortcodes.php */
51
        $maptype = empty($instance['maptype']) ? 'ROADMAP' : apply_filters('widget_maptype', $instance['maptype']);
52
        /** This action is documented in geodirectory_shortcodes.php */
53
        $zoom = empty($instance['zoom']) ? '13' : apply_filters('widget_zoom', $instance['zoom']);
54
        /** This action is documented in geodirectory_shortcodes.php */
55
        $autozoom = empty($instance['autozoom']) ? '' : apply_filters('widget_autozoom', $instance['autozoom']);
56
        /** This action is documented in geodirectory_shortcodes.php */
57
        $child_collapse = empty($instance['child_collapse']) ? '0' : apply_filters('widget_child_collapse', $instance['child_collapse']);
58
        /** This action is documented in geodirectory_shortcodes.php */
59
        $scrollwheel = empty($instance['scrollwheel']) ? '0' : apply_filters('widget_scrollwheel', $instance['scrollwheel']);
60
61
        $map_args = array();
62
        $map_args['map_canvas_name'] = str_replace('-', '_', $args['widget_id']); //'home_map_canvas'.$str ;
63
        $map_args['width'] = $width;
64
        $map_args['height'] = $height;
65
        $map_args['maptype'] = $maptype;
66
        $map_args['scrollwheel'] = $scrollwheel;
67
        $map_args['zoom'] = $zoom;
68
        $map_args['autozoom'] = $autozoom;
69
        $map_args['child_collapse'] = $child_collapse;
70
        $map_args['enable_cat_filters'] = true;
71
        $map_args['enable_text_search'] = true;
72
        $map_args['enable_post_type_filters'] = true;
73
        /** This action is documented in geodirectory_shortcodes.php */
74
        $map_args['enable_location_filters'] = apply_filters('geodir_home_map_enable_location_filters', false);
75
        $map_args['enable_jason_on_load'] = false;
76
        $map_args['enable_marker_cluster'] = false;
77
        $map_args['enable_map_resize_button'] = true;
78
        $map_args['map_class_name'] = 'geodir-map-home-page';
79
80
        $is_geodir_home_map_widget = true;
81
        $map_args['is_geodir_home_map_widget'] = $is_geodir_home_map_widget;
82
83
        geodir_draw_map($map_args);
84
85
        /* home map post type slider */
86
        if ($is_geodir_home_map_widget) {
87
            add_action('wp_footer', array($this, 'geodir_home_map_add_script'), 100);
88
        }
89
90
    }
91
92
	/**
93
	 * Sanitize home page map widget form values as they are saved.
94
	 *
95
	 * @since 1.0.0
96
     * @since 1.5.1 Declare function public.
97
	 *
98
	 * @param array $new_instance Values just sent to be saved.
99
	 * @param array $old_instance Previously saved values from database.
100
	 *
101
	 * @return array Updated safe values to be saved.
102
	 */
103
    public function update($new_instance, $old_instance)
104
    {
105
        //save the widget
106
        $instance = $old_instance;
107
        $instance['width'] = strip_tags($new_instance['width']);
108
        $instance['heigh'] = ($new_instance['heigh']);
109
        $instance['maptype'] = ($new_instance['maptype']);
110
        $instance['zoom'] = ($new_instance['zoom']);
111
        $instance['autozoom'] = isset($new_instance['autozoom']) ? $new_instance['autozoom'] : '';
112
        $instance['child_collapse'] = isset($new_instance['child_collapse']) ? ($new_instance['child_collapse']) : '';
113
        $instance['scrollwheel'] = isset($new_instance['scrollwheel']) ? ($new_instance['scrollwheel']) : '';
114
115
        return $instance;
116
    }
117
118
	/**
119
	 * Back-end home page map widget settings form.
120
	 *
121
	 * @since 1.0.0
122
     * @since 1.5.1 Declare function public.
123
	 *
124
	 * @param array $instance Previously saved values from database.
125
	 */
126
    public function form($instance)
127
    {
128
        //widgetform in backend
129
130
        $instance = wp_parse_args((array)$instance, array('width' => '', 'heigh' => '', 'maptype' => '', 'zoom' => '', 'autozoom' => '', 'child_collapse' => '0', 'scrollwheel' => '0'));
131
        $width = strip_tags($instance['width']);
132
        $heigh = strip_tags($instance['heigh']);
133
        $maptype = strip_tags($instance['maptype']);
134
        $zoom = strip_tags($instance['zoom']);
135
        $autozoom = strip_tags($instance['autozoom']);
136
        $child_collapse = strip_tags($instance['child_collapse']);
137
        $scrollwheel = strip_tags($instance['scrollwheel']);
138
        ?>
139
140
        <p>
141
            <label
142
                for="<?php echo $this->get_field_id('width'); ?>"><?php _e('Map Width <small>(Default is : 100%) you can use px or % here</small>', 'geodirectory'); ?>
143
                :
144
                <input class="widefat" id="<?php echo $this->get_field_id('width'); ?>"
145
                       name="<?php echo $this->get_field_name('width'); ?>" type="text"
146
                       value="<?php echo esc_attr($width); ?>"/>
147
            </label>
148
        </p>
149
        <p>
150
            <label
151
                for="<?php echo $this->get_field_id('heigh'); ?>"><?php _e('Map Height <small>(Default is : 425px) you can use px or vh here</small>', 'geodirectory'); ?>
152
                :
153
                <input class="widefat" id="<?php echo $this->get_field_id('heigh'); ?>"
154
                       name="<?php echo $this->get_field_name('heigh'); ?>" type="text"
155
                       value="<?php echo esc_attr($heigh); ?>"/>
156
            </label>
157
        </p>
158
159
        <p>
160
            <label
161
                for="<?php echo $this->get_field_id('maptype'); ?>"><?php _e(' Select Map View', 'geodirectory'); ?>
162
                :
163
                <select class="widefat" id="<?php echo $this->get_field_id('maptype'); ?>"
164
                        name="<?php echo $this->get_field_name('maptype'); ?>">
165
166
                    <option <?php if (isset($maptype) && $maptype == 'ROADMAP') {
167
                        echo 'selected="selected"';
168
                    } ?> value="ROADMAP"><?php _e('Road Map', 'geodirectory'); ?></option>
169
                    <option <?php if (isset($maptype) && $maptype == 'SATELLITE') {
170
                        echo 'selected="selected"';
171
                    } ?> value="SATELLITE"><?php _e('Satellite Map', 'geodirectory'); ?></option>
172
                    <option <?php if (isset($maptype) && $maptype == 'HYBRID') {
173
                        echo 'selected="selected"';
174
                    } ?> value="HYBRID"><?php _e('Hybrid Map', 'geodirectory'); ?></option>
175
					<option <?php selected($maptype, 'TERRAIN');?> 
176
							value="TERRAIN"><?php _e('Terrain Map', 'geodirectory'); ?></option>
177
                </select>
178
            </label>
179
        </p>
180
181
        <?php
182
        $map_zoom_level = geodir_map_zoom_level();
183
        ?>
184
185
        <p>
186
            <label
187
                for="<?php echo $this->get_field_id('zoom'); ?>"><?php _e('Map Zoom level', 'geodirectory'); ?>
188
                :
189
                <select class="widefat" id="<?php echo $this->get_field_id('zoom'); ?>"
190
                        name="<?php echo $this->get_field_name('zoom'); ?>"> <?php
191
192 View Code Duplication
                    foreach ($map_zoom_level as $level) {
193
                        $selected = '';
194
                        if ($level == $zoom)
195
                            $selected = 'selected="selected"';
196
197
                        echo '<option ' . $selected . ' value="' . $level . '">' . $level . '</option>';
198
199
                    } ?>
200
201
                </select>
202
            </label>
203
        </p>
204
205
206
        <p>
207
            <label
208
                for="<?php echo $this->get_field_id('autozoom'); ?>"><?php _e('Map Auto Zoom ?', 'geodirectory'); ?>
209
                :
210
                <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('autozoom'); ?>"
211
                       name="<?php echo $this->get_field_name('autozoom'); ?>"<?php if ($autozoom) {
212
                    echo 'checked="checked"';
213
                } ?> /></label>
214
        </p>
215
216
        <p>
217
            <label
218
                for="<?php echo $this->get_field_id('child_collapse'); ?>"><?php _e('Collapse child/sub categories ?', 'geodirectory'); ?>
219
                :
220
                <input id="<?php echo $this->get_field_id('child_collapse'); ?>"
221
                       name="<?php echo $this->get_field_name('child_collapse'); ?>" type="checkbox" value="1"
222
                       <?php if ($child_collapse){ ?>checked="checked" <?php } ?> />
223
            </label>
224
        </p>
225
226
        <p>
227
            <label
228
                for="<?php echo $this->get_field_id('scrollwheel'); ?>"><?php _e('Enable mouse scroll zoom ?', 'geodirectory'); ?>
229
                :
230
                <input id="<?php echo $this->get_field_id('scrollwheel'); ?>"
231
                       name="<?php echo $this->get_field_name('scrollwheel'); ?>" type="checkbox" value="1"
232
                       <?php if ($scrollwheel){ ?>checked="checked" <?php } ?> />
233
            </label>
234
        </p>
235
236
    <?php
237
    }
238
239
    /**
240
	 * Adds the javascript in the footer for home page map widget.
241
	 *
242
	 * @since 1.0.0
243
     * @since 1.5.1 Declare function public.
244
	 */
245
    public function geodir_home_map_add_script()
246
    {
247
        ?>
248
        <script type="text/javascript">
249
            jQuery(document).ready(function () {
250
                geoDirMapSlide();
251
                jQuery(window).resize(function () {
252
                    jQuery('.geodir_map_container.geodir-map-home-page').each(function () {
253
                        jQuery(this).find('.geodir-map-posttype-list').css({'width': 'auto'});
254
                        jQuery(this).find('.map-places-listing ul.place-list').css({'margin-left': '0px'});
255
                        geoDirMapPrepare(this);
256
                    });
257
                });
258
            });
259
            function geoDirMapPrepare($thisMap) {
260
                var $objMpList = jQuery($thisMap).find('.geodir-map-posttype-list');
261
                var $objPlList = jQuery($thisMap).find('.map-places-listing ul.place-list');
262
                var wArrL = parseFloat(jQuery($thisMap).find('.geodir-map-navigation .geodir-leftarrow').outerWidth(true));
263
                var wArrR = parseFloat(jQuery($thisMap).find('.geodir-map-navigation .geodir-rightarrow').outerWidth(true));
264
                var ptw1 = parseFloat($objMpList.outerWidth(true));
265
                $objMpList.css({'margin-left': wArrL + 'px'});
266
                $objMpList.attr('data-width', ptw1);
267
                ptw1 = ptw1 - (wArrL + wArrR);
268
                $objMpList.width(ptw1);
269
                var ptw = $objPlList.width();
270
                var ptw2 = 0;
271
                $objPlList.find('li').each(function () {
272
                    var ptw21 = jQuery(this).outerWidth(true);
273
                    ptw2 += parseFloat(ptw21);
274
                });
275
                var doMov = parseFloat(ptw * 0.75);
276
                ptw2 = ptw2 + ( ptw2 * 0.05 );
277
                var maxMargin = ptw2 - ptw;
278
                $objPlList.attr('data-domov', doMov);
279
                $objPlList.attr('data-maxMargin', maxMargin);
280
            }
281
            function geoDirMapSlide() {
282
                jQuery('.geodir_map_container.geodir-map-home-page').each(function () {
283
                    var $thisMap = this;
284
                    geoDirMapPrepare($thisMap);
285
                    var $objPlList = jQuery($thisMap).find('.map-places-listing ul.place-list');
286
                    jQuery($thisMap).find('.geodir-leftarrow a').click(function (e) {
287
                        e.preventDefault();
288
                        var cm = $objPlList.css('margin-left');
289
                        var doMov = parseFloat($objPlList.attr('data-domov'));
290
                        var maxMargin = parseFloat($objPlList.attr('data-maxMargin'));
291
                        cm = parseFloat(cm);
292
                        if (cm == 0 || maxMargin < 0) {
293
                            return;
294
                        }
295
                        domargin = cm + doMov;
296
                        if (domargin > 0) {
297
                            domargin = 0;
298
                        }
299
                        $objPlList.animate({'margin-left': domargin + 'px'}, 1000);
300
                    });
301
                    jQuery($thisMap).find('.geodir-rightarrow a').click(function (e) {
302
                        e.preventDefault();
303
                        var cm = $objPlList.css('margin-left');
304
                        var doMov = parseFloat($objPlList.attr('data-domov'));
305
                        var maxMargin = parseFloat($objPlList.attr('data-maxMargin'));
306
                        cm = parseFloat(cm);
307
                        domargin = cm - doMov;
308
                        if (cm == ( maxMargin * -1 ) || maxMargin < 0) {
309
                            return;
310
                        }
311
                        if (( domargin * -1 ) > maxMargin) {
312
                            domargin = maxMargin * -1;
313
                        }
314
                        $objPlList.animate({'margin-left': domargin + 'px'}, 1000);
315
                    });
316
                });
317
            }
318
        </script>
319
    <?php
320
    }
321
} // class geodir_homepage_map
322
323
register_widget('geodir_homepage_map');
324
?>