Completed
Push — master ( bf55f1...98b554 )
by Iurii
01:14
created

GaReport::getReportModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * @package Google Analytics Report
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2017, Iurii Makukh <[email protected]>
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\ga_report;
11
12
use gplcart\core\Module;
13
14
/**
15
 * Main class for Google Analytics Report module
16
 */
17
class GaReport extends Module
18
{
19
20
    /**
21
     * Constructor
22
     */
23
    public function __construct()
24
    {
25
        parent::__construct();
26
    }
27
28
    /**
29
     * Implements hook "module.install.before"
30
     */
31
    public function hookModuleInstallBefore(&$result)
32
    {
33
        if (!function_exists('curl_init')) {
34
            $result = $this->getLanguage()->text('CURL library is not enabled');
35
        }
36
    }
37
38
    /**
39
     * Implements hook "route.list"
40
     * @param array $routes
41
     */
42
    public function hookRouteList(array &$routes)
43
    {
44
        $routes['admin/module/settings/ga_report'] = array(
45
            'access' => 'module_edit',
46
            'handlers' => array(
47
                'controller' => array('gplcart\\modules\\ga_report\\controllers\\Settings', 'editSettings')
48
            )
49
        );
50
51
        $routes['admin/report/ga'] = array(
52
            'menu' => array('admin' => 'Google Analytics'),
53
            'access' => 'ga_report',
54
            'handlers' => array(
55
                'controller' => array('gplcart\\modules\\ga_report\\controllers\\Report', 'listReport')
56
            )
57
        );
58
    }
59
60
    /**
61
     * Implements hook "user.role.permissions"
62
     * @param array $permissions
63
     */
64
    public function hookUserRolePermissions(array &$permissions)
65
    {
66
        $permissions['ga_report'] = 'Google Analytics reports: access';
67
    }
68
69
    /**
70
     * Implements hook "oauth.providers"
71
     * @param array $providers
72
     */
73
    public function hookOauthProviders(array &$providers)
74
    {
75
        $providers['ga'] = array(
76
            'name' => 'Google analytics',
77
            'settings' => $this->config->module('ga_report'),
78
            'url' => array(
79
                'process' => 'https://www.googleapis.com/analytics/v3/data/ga',
80
                'token' => 'https://www.googleapis.com/oauth2/v4/token'
81
            ),
82
            'scope' => 'https://www.googleapis.com/auth/analytics.readonly',
83
            'handlers' => array(
84
                'token' => array('gplcart\\modules\\ga_report\\handlers\\Api', 'token'),
85
                'process' => array('gplcart\\modules\\ga_report\\handlers\\Api', 'process'),
86
            )
87
        );
88
    }
89
90
    /**
91
     * Implements hook "dashboard.handlers"
92
     * @param array $handlers
93
     */
94
    public function hookDashboardHandlers(array &$handlers)
95
    {
96
        $weight = count($handlers);
97
        $model = $this->getReportModel();
98
        $settings = $this->config->module('ga_report');
99
100
        foreach ($model->getHandlers() as $id => $handler) {
101
102
            if (!in_array($id, $settings['dashboard'])) {
103
                continue;
104
            }
105
106
            $weight++;
107
108
            $report = $model->get($id, $settings);
109
110
            $handlers["ga_$id"] = array(
111
                'status' => true,
112
                'weight' => $weight,
113
                'title' => $handler['name'],
114
                'template' => $handler['template'],
115
                'handlers' => array(
116
                    'data' => function() use ($report, $settings) {
117
                        return array('report' => $report, 'settings' => $settings);
118
                    }
119
                )
120
            );
121
        }
122
    }
123
124
    /**
125
     * Implements hook "construct.controller.backend"
126
     * @param \gplcart\core\controllers\backend\Controller $controller
127
     */
128
    public function hookConstructControllerBackend($controller)
129
    {
130
        if ($controller->isQuery('ga.update')) {
131
            $store_id = $controller->getQuery('ga.update.store_id', '', 'string');
132
            $handler_id = $controller->getQuery('ga.update.handler_id', '', 'string');
133
            $this->getReportModel()->clearCache($handler_id, $store_id);
134
        }
135
    }
136
137
    /**
138
     * Returns the report model instance
139
     * @return \gplcart\modules\ga_report\models\Report
140
     */
141
    protected function getReportModel()
142
    {
143
        return $this->getModel('Report', 'ga_report');
144
    }
145
146
    /**
147
     * Returns an array of GA handlers
148
     * @return array
149
     */
150
    public function getHandlers()
151
    {
152
        return $this->getReportModel()->getHandlers();
153
    }
154
155
}
156