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

Report::clearCacheReport()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
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\controllers;
11
12
use gplcart\core\models\Oauth as OauthModel;
13
use gplcart\core\controllers\backend\Controller as BackendController;
14
use gplcart\modules\ga_report\models\Report as GaReportModuleReportModel;
15
16
/**
17
 * Handles incoming requests and outputs data related to Google Analytics Report module
18
 */
19
class Report extends BackendController
20
{
21
22
    /**
23
     * Google Analytics Report Report model instance
24
     * @var \gplcart\modules\ga_report\models\Report $ga_report_model
25
     */
26
    protected $ga_report_model;
27
28
    /**
29
     * Oauth model instance
30
     * @var \gplcart\core\models\Oauth $oauth
31
     */
32
    protected $oauth;
33
34
    /**
35
     * @param OauthModel $oauth
36
     * @param GaReportModuleReportModel $model
37
     */
38
    public function __construct(OauthModel $oauth,
39
            GaReportModuleReportModel $model)
40
    {
41
        parent::__construct();
42
43
        $this->oauth = $oauth;
44
        $this->ga_report_model = $model;
45
    }
46
47
    /**
48
     * Route page callback to display the Google Analytics report page
49
     */
50
    public function listReport()
51
    {
52
        $this->setTitleListReport();
53
        $this->setBreadcrumbListReport();
54
55
        $this->clearCacheReport();
56
57
        $this->setData('stores', $this->store->getList());
58
        $this->setData('panels', $this->getPanelsReport());
59
60
        $default = $this->config->module('ga_report', 'store_id');
61
        $store_id = $this->getQuery('ga.update.store_id', $default, 'string');
62
        $this->setData('ga_store_id', $store_id);
63
64
        $this->outputListReport();
65
    }
66
67
    /**
68
     * Clear GA cache
69
     */
70
    protected function clearCacheReport()
71
    {
72
        if ($this->isQuery('ga.update')) {
73
            $store_id = $this->getQuery('ga.update.store_id', '', 'string');
74
            $handler_id = $this->getQuery('ga.update.handler_id', '', 'string');
75
            $this->ga_report_model->clearCache($handler_id, $store_id);
76
        }
77
    }
78
79
    /**
80
     * Returns an array of Google Analytics panels
81
     * @return array
82
     */
83
    protected function getPanelsReport()
84
    {
85
        $settings = $this->config->module('ga_report');
86
        $store_id = $this->getQuery('ga.update.store_id', '', 'string');
87
88
        if (isset($store_id)) {
89
            $settings['store_id'] = $store_id;
90
        }
91
92
        if (empty($settings['ga_profile_id'][$settings['store_id']])) {
93
            $settings['query'] = array();
94
        } else {
95
            $settings['query'] = array(
96
                'max-results' => $settings['limit'],
97
                'end-date' => date('Y-m-d', strtotime($settings['end_date'])),
98
                'start-date' => date('Y-m-d', strtotime($settings['start_date'])),
99
                'ids' => 'ga:' . $settings['ga_profile_id'][$settings['store_id']]
100
            );
101
        }
102
103
        $panels = array();
104
        foreach ($this->ga_report_model->getHandlers() as $handler) {
105
            $report = $this->ga_report_model->get($handler['id'], $settings);
106
            if (isset($report['handler']['template'])) {
107
                // Place data under "content => data" kaey to make compatible with dashboard templates
108
                $data = array('content' => array('data' => array('report' => $report, 'settings' => $settings)));
109
                $panels[$handler['id']] = array('rendered' => $this->render($report['handler']['template'], $data));
110
            }
111
        }
112
113
        return gplcart_array_split($panels, 3);
114
    }
115
116
    /**
117
     * Set title on the Google Analytics report page
118
     */
119
    protected function setTitleListReport()
120
    {
121
        $this->setTitle($this->text('Google Analytics'));
122
    }
123
124
    /**
125
     * Set breadcrumbs on the Google Analytics report page
126
     */
127
    protected function setBreadcrumbListReport()
128
    {
129
        $this->setBreadcrumbBackend();
130
    }
131
132
    /**
133
     * Render and output the Google Analytics report page
134
     */
135
    protected function outputListReport()
136
    {
137
        $this->output('ga_report|list');
138
    }
139
140
}
141