Completed
Push — master ( 7f0603...aacf62 )
by Iurii
01:13
created

Main::hookOauthProviders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
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\Container;
13
use gplcart\core\Module;
14
15
/**
16
 * Main class for Google Analytics Report module
17
 */
18
class Main
19
{
20
21
    /**
22
     * Module class instance
23
     * @var \gplcart\core\Module $model
24
     */
25
    protected $module;
26
27
    /**
28
     * @param Module $module
29
     */
30
    public function __construct(Module $module)
31
    {
32
        $this->module = $module;
33
    }
34
35
    /**
36
     * Implements hook "route.list"
37
     * @param array $routes
38
     */
39
    public function hookRouteList(array &$routes)
40
    {
41
        $routes['admin/module/settings/ga_report'] = array(
42
            'access' => 'module_edit',
43
            'handlers' => array(
44
                'controller' => array('gplcart\\modules\\ga_report\\controllers\\Settings', 'editSettings')
45
            )
46
        );
47
48
        $routes['admin/report/ga'] = array(
49
            'menu' => array(
50
                'admin' => 'Google Analytics' // @text
51
            ),
52
            'access' => 'ga_report',
53
            'handlers' => array(
54
                'controller' => array('gplcart\\modules\\ga_report\\controllers\\Report', 'listReport')
55
            )
56
        );
57
    }
58
59
    /**
60
     * Implements hook "user.role.permissions"
61
     * @param array $permissions
62
     */
63
    public function hookUserRolePermissions(array &$permissions)
64
    {
65
        $permissions['ga_report'] = 'Google Analytics Report: access'; // @text
66
    }
67
68
    /**
69
     * Implements hook "dashboard.handlers"
70
     * @param array $handlers
71
     */
72
    public function hookDashboardHandlers(array &$handlers)
73
    {
74
        $weight = count($handlers);
75
        $model = $this->getModel();
76
        $settings = $this->module->getSettings('ga_report');
77
78
        foreach ($model->getHandlers() as $id => $handler) {
79
80
            if (!in_array($id, $settings['dashboard'])) {
81
                continue;
82
            }
83
84
            $weight++;
85
86
            $report = $model->get($handler, $settings);
87
88
            $handlers["ga_$id"] = array(
89
                'status' => true,
90
                'weight' => $weight,
91
                'title' => $handler['name'],
92
                'template' => $handler['template'],
93
                'handlers' => array(
94
                    'data' => function () use ($handler, $report, $settings) {
95
                        return array(
96
                            'report' => $report,
97
                            'handler' => $handler,
98
                            'settings' => $settings
99
                        );
100
                    }
101
                )
102
            );
103
        }
104
    }
105
106
    /**
107
     * Implements hook "construct.controller.backend"
108
     * @param \gplcart\core\controllers\backend\Controller $controller
109
     */
110
    public function hookConstructControllerBackend($controller)
111
    {
112
        if ($controller->isQuery('ga.update')) {
113
            $store_id = $controller->getQuery('ga.update.store_id', '');
114
            $handler_id = $controller->getQuery('ga.update.handler_id', '');
115
            $this->getModel()->clearCache($handler_id, $store_id);
116
        }
117
    }
118
119
    /**
120
     * Returns an array of GA handlers
121
     * @return array
122
     */
123
    public function getHandlers()
124
    {
125
        return $this->getModel()->getHandlers();
126
    }
127
128
    /**
129
     * Returns the report model instance
130
     * @return \gplcart\modules\ga_report\models\Report
131
     */
132
    protected function getModel()
133
    {
134
        /** @var \gplcart\modules\ga_report\models\Report $instance */
135
        $instance = Container::get('gplcart\\modules\\ga_report\\models\\Report');
136
        return $instance;
137
    }
138
139
}
140