Completed
Push — development ( cfd391...deed4d )
by Andrij
12:03
created

Custom_model::getUrl()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 30
Code Lines 21

Duplication

Lines 30
Ratio 100 %

Importance

Changes 0
Metric Value
cc 7
eloc 21
nc 9
nop 2
dl 30
loc 30
rs 6.7272
c 0
b 0
f 0
1
<?php
2
use core\models\Route;
3
use core\models\RouteQuery;
4
use core\src\CoreFactory;
5
6
/**
7
 * Class Attendance_model for mod_stats module
8
 *
9
 * @uses          \CI_Model
10
 * @author        DevImageCms
11
 * @copyright (c) 2014, ImageCMS
12
 * @property CI_DB_active_record $db
13
 * @package       ImageCMSModule
14
 */
15
class Custom_model extends CI_Model
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...
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
16
{
17
18
    /**
19
     *
20
     * @return int
21
     */
22
    public function getAllTimeCountUnique() {
23
        $query
24
            = '
0 ignored issues
show
introduced by
Multi-line assignment not indented correctly; expected 10 spaces but found 12
Loading history...
25
            SELECT 
26
                COUNT(DISTINCT `id_user`) AS `count`
27
            FROM 
28
                `mod_stats_attendance`
29
        ';
30
31
        return $this->db->query($query)->row()->count;
32
    }
33
34
    /**
35
     *
36
     * @return int
37
     */
38
    public function getAllTimeCountUniqueRobots() {
39
        $query
40
            = '
0 ignored issues
show
introduced by
Multi-line assignment not indented correctly; expected 10 spaces but found 12
Loading history...
41
            SELECT 
42
                COUNT(DISTINCT `id_robot`) AS `count`
43
            FROM 
44
                `mod_stats_attendance_robots`
45
        ';
46
47
        return $this->db->query($query)->row()->count;
48
    }
49
50
    /**
51
     *
52
     * @return int
53
     */
54 View Code Duplication
    public function getLastViewedPage() {
55
        $locale = MY_Controller::getCurrentLocale();
56
57
        $query
58
                = "
0 ignored issues
show
introduced by
Multi-line assignment not indented correctly; expected 10 spaces but found 16
Loading history...
Coding Style introduced by
Multi-line assignment not indented correctly; expected 12 spaces but found 16
Loading history...
59
SELECT
60
    `users`.`username`,
61
    `type_id`,
62
    `route`.`type`,
63
    `id_entity`,
64
    (CASE `mod_stats_attendance`.`type_id` 
65
    WHEN 1 THEN `content`.`title` 
66
    WHEN 2 THEN `category`.`name`
67
    WHEN 3 THEN `shop_category_i18n`.`name` 
68
    WHEN 4 THEN `shop_products_i18n`.`name`
69
END) AS `page_name`
70
FROM
71
    `mod_stats_attendance`
72
LEFT JOIN
73
    route
74
ON
75
    route.entity_id = mod_stats_attendance.id_entity
76
LEFT JOIN
77
    `content`
78
ON
79
    `content`.`id` = `mod_stats_attendance`.`id_entity` AND `mod_stats_attendance`.`type_id` = 1
80
LEFT JOIN
81
    `category`
82
ON
83
    `category`.`id` = `mod_stats_attendance`.`id_entity` AND `mod_stats_attendance`.`type_id` = 2
84
LEFT JOIN
85
    `shop_category`
86
ON
87
    `shop_category`.`id` = `mod_stats_attendance`.`id_entity` AND `mod_stats_attendance`.`type_id` = 3
88
LEFT JOIN
89
    `shop_products`
90
ON
91
    `shop_products`.`id` = `mod_stats_attendance`.`id_entity` AND `mod_stats_attendance`.`type_id` = 4
92
LEFT JOIN
93
    `shop_category_i18n`
94
ON
95
    `shop_category`.`id` = `shop_category_i18n`.`id` AND `shop_category_i18n`.`locale` = '{$locale}'
96
LEFT JOIN
97
    `shop_products_i18n`
98
ON
99
    `shop_products`.`id` = `shop_products_i18n`.`id` AND `shop_products_i18n`.`locale` = '{$locale}'
100
LEFT JOIN
101
    `users`
102
ON
103
    `users`.`id` = `mod_stats_attendance`.`id_user`
104
ORDER BY
105
    `mod_stats_attendance`.`time_add`
106
DESC
107
LIMIT 1
108
        ";
109
        $result = $this->db->query($query);
110
        if ($result) {
111
            $result = $result->row_array();
112
            if ($result['type_id'] == Attendance::PAGE) {
113
                $result['url'] = $this->getUrl($result['id_entity'], Route::TYPE_PAGE);
114
            } elseif ($result['type_id'] == Attendance::CATEGORY) {
115
                $result['url'] = $this->getUrl($result['id_entity'], Route::TYPE_CATEGORY);
116
            } elseif ($result['type_id'] == Attendance::SHOP_CATEGORY) {
117
                $result['url'] = $this->getUrl($result['id_entity'], Route::TYPE_SHOP_CATEGORY);
118
            } elseif ($result['type_id'] == Attendance::PRODUCT) {
119
                $result['url'] = $this->getUrl($result['id_entity'], Route::TYPE_PRODUCT);
120
            }
121
122
            return $result;
123
        }
124
125
        return false;
126
    }
127
128 View Code Duplication
    protected function getUrl($id, $type) {
129
        $urlConfiguration = CoreFactory::getConfiguration()->getUrlRules();
130
        $url              = RouteQuery::create()->filterByEntityId($id)->filterByType($type)->findOneOrCreate();
131
        if ($type == Route::TYPE_SHOP_CATEGORY) {
132
            if ($urlConfiguration['shop_category']['parent'] === '1') {
133
                $url = $url->getFullUrl();
134
            } else {
135
                $url = $url->getUrl();
136
            }
137
            if ($urlConfiguration['shop_category']['prefix'] != '') {
138
                $url = rtrim($urlConfiguration['shop_category']['prefix'], '/') . '/' . $url;
139
            }
140
141
            return $url;
142
        } elseif ($type == Route::TYPE_PRODUCT) {
143
            if ($urlConfiguration['product']['parent'] === '1') {
144
                $url = $url->getFullUrl();
145
            } else {
146
                $url = $url->getUrl();
147
            }
148
            if ($urlConfiguration['product']['prefix'] != '') {
149
                $url = rtrim($urlConfiguration['product']['prefix'], '/') . '/' . $url;
150
            }
151
152
            return $url;
153
        } else {
154
            return $url->getFullUrl();
155
        }
156
157
    }
158
159
    protected function getCategoryUrl($id) {
160
        $res = $this->db->select(
161
            [
162
             'url',
163
             'parent_id',
164
            ]
165
        )->where('id', $id)->get('category');
166
        if ($res = $res->row_array()) {
167
            $parent_url = $this->getCategoryUrl($res['parent_id']);
168
169
            return ($parent_url ? $parent_url . '/' : '') . $res['url'];
170
        }
171
    }
172
173
}