Completed
Push — master ( 0791f7...9eb2ab )
by Iurii
01:12
created

GaReport::hookConstructControllerBackend()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
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 "route.list"
30
     * @param array $routes
31
     */
32
    public function hookRouteList(array &$routes)
33
    {
34
        $routes['admin/module/settings/ga_report'] = array(
35
            'access' => 'module_edit',
36
            'handlers' => array(
37
                'controller' => array('gplcart\\modules\\ga_report\\controllers\\Settings', 'editSettings')
38
            )
39
        );
40
41
        $routes['admin/report/ga'] = array(
42
            'menu' => array('admin' => 'Google Analytics'),
43
            'access' => 'ga_report',
44
            'handlers' => array(
45
                'controller' => array('gplcart\\modules\\ga_report\\controllers\\Report', 'listReport')
46
            )
47
        );
48
    }
49
50
    /**
51
     * Implements hook "user.role.permissions"
52
     * @param array $permissions
53
     */
54
    public function hookUserRolePermissions(array &$permissions)
55
    {
56
        $permissions['ga_report'] = 'Google Analytics reports: access';
57
    }
58
59
    /**
60
     * Implements hook "oauth.providers"
61
     * @param array $providers
62
     */
63
    public function hookOauthProviders(array &$providers)
64
    {
65
        $providers['ga'] = array(
66
            'name' => 'Google analytics',
67
            'settings' => $this->config->module('ga_report'),
68
            'url' => array(
69
                'process' => 'https://www.googleapis.com/analytics/v3/data/ga',
70
                'token' => 'https://www.googleapis.com/oauth2/v4/token'
71
            ),
72
            'scope' => 'https://www.googleapis.com/auth/analytics.readonly',
73
            'handlers' => array(
74
                'token' => array('gplcart\\modules\\ga_report\\handlers\\Api', 'token'),
75
                'process' => array('gplcart\\modules\\ga_report\\handlers\\Api', 'process'),
76
            )
77
        );
78
    }
79
80
    /**
81
     * Implements hook "dashboard.handlers"
82
     * @param array $handlers 
83
     */
84
    public function hookDashboardHandlers(array &$handlers)
85
    {
86
        $settings = $this->config->module('ga_report');
87
88
        /* @var $ga_model \gplcart\modules\ga_report\models\Report */
89
        $ga_model = $this->getInstance('gplcart\\modules\\ga_report\\models\\Report');
90
91
        $weight = count($handlers);
92
93
        foreach ($ga_model->getHandlers() as $id => $ga_handler) {
94
95
            if (!in_array($id, $settings['dashboard'])) {
96
                continue;
97
            }
98
99
            $weight++;
100
101
            $handlers["ga_{$ga_handler['id']}"] = array(
102
                'status' => true,
103
                'weight' => $weight,
104
                'title' => $ga_handler['name'],
105
                'template' => $ga_handler['template'],
106
                'handlers' => array(
107
                    'data' => function() use ($ga_handler, $ga_model, $settings) {
108
                        return array('report' => $ga_model->get($ga_handler['id'], $settings), 'settings' => $settings);
109
                    }
110
                )
111
            );
112
        }
113
    }
114
115
    /**
116
     * Implements hook "construct.controller.backend"
117
     * @param \gplcart\core\controllers\backend\Controller $controller
118
     */
119
    public function hookConstructControllerBackend($controller)
120
    {
121
        if ($controller->isQuery('ga.update')) {
122
123
            /* @var $ga_model \gplcart\modules\ga_report\models\Report */
124
            $ga_model = $this->getInstance('gplcart\\modules\\ga_report\\models\\Report');
125
126
            $store_id = $controller->getQuery('ga.update.store_id', '', 'string');
127
            $handler_id = $controller->getQuery('ga.update.handler_id', '', 'string');
128
            $ga_model->clearCache($handler_id, $store_id);
129
        }
130
    }
131
132
}
133