Completed
Push — development ( a150a5...f82eb6 )
by Andrij
17:01
created

widget_helper.php ➔ widget()   D

Complexity

Conditions 9
Paths 18

Size

Total Lines 40
Code Lines 28

Duplication

Lines 5
Ratio 12.5 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 28
c 1
b 0
f 0
nc 18
nop 2
dl 5
loc 40
rs 4.909
1
<?php
2
3
if (!defined('BASEPATH')) {
4
    exit('No direct script access allowed');
5
}
6
7
/**
8
 * ImageCMS
9
 * Widgets helper
10
 * @property CI_DB_active_record $db
11
 */
12
if (!function_exists('widget')) {
13
14
    /**
15
     * Run widget
16
     *
17
     * @param bool|string $name - widget name
18
     * @param bool|int $cache - cache ttl in minutes
19
     * @return array
20
     */
21
    function widget($name = FALSE, $cache = FALSE) {
22
        $ci = &get_instance();
23
        $widget = [];
24
25
        $query = $ci->db->limit(1)->get_where('widgets', ['name' => $name]);
26
27 View Code Duplication
        if ($query->num_rows() == 1) {
28
            $widget = $query->row_array();
29
        } else {
30
            log_message('error', 'Can\'t run widget <b>' . $name . '</b>');
31
        }
32
33
        if (($data = $ci->cache->fetch('widget' . $name, 'widgets')) != FALSE AND $cache != FALSE) {
34
            return $data;
35
        } else {
36
            $widget['settings'] = unserialize($widget['settings']);
37
38
            switch ($widget['type']) {
39
                case 'module':
40
                    $subpath = isset($widget['settings']['subpath']) ? $widget['settings']['subpath'] . '/' : '';
41
                    $method = $widget['method'];
42
                    $result = $ci->load->module($widget['data'] . '/' . $subpath . $widget['data'] . '_widgets')->$method($widget);
43
                    break;
44
45
                case 'html':
46
                    $locale = MY_Controller::getCurrentLocale();
47
                    $id = $widget['id'];
48
                    $sql = "select * from widget_i18n where locale = '$locale' and id = '$id'";
49
                    $w_i18 = $ci->db->query($sql)->row_array();
50
                    $result = $w_i18['data'];
51
                    break;
52
            }
53
54
            if ($cache != FALSE AND is_integer($cache)) {
0 ignored issues
show
introduced by
is_integer() is a function name alias, use is_int() instead
Loading history...
55
                $ci->cache->store('widget' . $name, $result, $cache * 60, 'widgets');
56
            }
57
58
            return $result;
59
        }
60
    }
61
62
}
63
64
if (!function_exists('widget_ajax')) {
65
66
    /**
67
     * @param string $name
68
     * @param string $container
69
     */
70
    function widget_ajax($name, $container) {
71
72
        echo "
73
                <script type=text/javascript>
74
                    $(window).load(function(){
75
                            $.ajax({
76
                                async : 'false',
77
                                type : 'post',
78
                                url : locale+'/shop/ajax/widget/$name',
79
                                success : function(data){
80
                                    $('$container').html(data);
81
                                    $(document).trigger({type: 'widget_ajax', el: $('$container')})
82
                                }
83
                            })
84
                      })
85
86
                 </script>
87
            ";
88
    }
89
90
}
91
92
if (!function_exists('getWidgetName')) {
93
94
    /**
95
     *
96
     * @param string $name
97
     * @return string
98
     */
99
    function getWidgetName($name) {
0 ignored issues
show
introduced by
Invalid function name, expected get_widget_name but found getWidgetName
Loading history...
100
        $ci = &get_instance();
101
102
        $query = $ci->db->limit(1)->get_where('widgets', ['name' => $name]);
103
104 View Code Duplication
        if ($query->num_rows() == 1) {
105
            $widget = $query->row_array();
106
        } else {
107
            log_message('error', 'Can\'t run widget <b>' . $name . '</b>');
108
        }
109
110
        $widget = unserialize($widget['settings']);
111
112
        return $widget['title'];
113
    }
114
115
}
116
117
if (!function_exists('getWidgeTitle')) {
118
119
    /**
120
     * @param string $name
121
     * @return string
122
     */
123
    function getWidgetTitle($name) {
0 ignored issues
show
introduced by
Invalid function name, expected get_widget_title but found getWidgetTitle
Loading history...
124
        $ci = &get_instance();
125
126
        $locale = MY_Controller::getCurrentLocale();
127
        $query = $ci->db
128
            ->join('widget_i18n', 'widget_i18n.id=widgets.id AND locale="' . $locale . '"', 'left')
129
            ->get_where('widgets', ['name' => $name]);
130
131
        if ($query->num_rows() == 1) {
132
            $widget = $query->row_array();
133
            $title = $widget['title'];
134
135
            $settings = @unserialize($widget[settings]);
136
            if ($settings) {
137
                $title = $title ? : $settings['title'];
0 ignored issues
show
introduced by
There must be no space between ? and :
Loading history...
138
            }
139
140
            return $title;
141
        } else {
142
            log_message('error', 'Can\'t run widget <b>' . $name . '</b>');
143
        }
144
145
        return '';
146
    }
147
148
}
149
150
if (!function_exists('getProductViewsCount')) {
151
152
    /**
153
     * @return int
154
     */
155
    function getProductViewsCount() {
0 ignored issues
show
introduced by
Invalid function name, expected get_product_views_count but found getProductViewsCount
Loading history...
156
        $ci = &get_instance();
157
158
        $views = $ci->session->userdata('page');
159
160
        if ($views) {
161
            $count = count($views);
162
        } else {
163
            $count = 0;
164
        }
165
166
        return $count;
167
    }
168
169
}
170
/* End of widget_helper.php */