Demo::submitDemo()   B
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 5
nc 3
nop 0
1
<?php
2
3
/**
4
 * @package Demo 
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\demo\controllers;
11
12
use gplcart\modules\demo\models\Demo as DemoModuleModel;
13
use gplcart\core\controllers\backend\Controller as BackendController;
14
15
/**
16
 * Handles incoming requests and outputs data related to Demo module
17
 */
18
class Demo extends BackendController
19
{
20
21
    /**
22
     * Demo module model instance
23
     * @var \gplcart\modules\demo\models\Demo $demo
24
     */
25
    protected $demo;
26
27
    /**
28
     * @param DemoModuleModel $demo
29
     */
30
    public function __construct(DemoModuleModel $demo)
31
    {
32
        parent::__construct();
33
34
        $this->demo = $demo;
35
    }
36
37
    /**
38
     * Route page callback
39
     */
40
    public function editDemo()
41
    {
42
        $this->setTitleEditDemo();
43
        $this->setBreadcrumbEditDemo();
44
45
        $this->setData('stores', $this->store->getNames());
46
        $this->setData('handlers', $this->demo->getHandlers());
47
48
        $this->submitDemo();
49
        $this->outputEditDemo();
50
    }
51
52
    /**
53
     * Set title on the demo content creation page
54
     */
55
    protected function setTitleEditDemo()
56
    {
57
        $this->setTitle($this->text('Demo content'));
58
    }
59
60
    /**
61
     * Set breadcrumbs on the demo content creation page
62
     */
63
    protected function setBreadcrumbEditDemo()
64
    {
65
        $breadcrumb = array(
66
            'text' => $this->text('Dashboard'),
67
            'url' => $this->url('admin')
68
        );
69
70
        $this->setBreadcrumb($breadcrumb);
71
    }
72
73
    /**
74
     * Handles submitted data
75
     */
76
    protected function submitDemo()
77
    {
78
        if ($this->isPosted('create') && $this->validateDemo()) {
79
            $this->createDemo();
80
        } else if ($this->isPosted('delete') && $this->validateDemo()) {
81
            $this->deleteDemo();
82
        }
83
    }
84
85
    /**
86
     * Validates submitted data
87
     * @return boolean
88
     */
89
    protected function validateDemo()
90
    {
91
        $this->setSubmitted('demo');
92
        $handler_id = $this->getSubmitted('handler_id');
93
94
        if (empty($handler_id) || !$this->demo->getHandler($handler_id)) {
95
            $this->setError('handler_id', $this->language->text('Unknown handler'));
96
        }
97
98
        return !$this->hasErrors();
99
    }
100
101
    /**
102
     * Creates the demo content
103
     */
104
    protected function createDemo()
105
    {
106
        $this->controlAccessSuperAdmin();
107
        $store_id = $this->getSubmitted('store_id', 1);
108
        $handler_id = $this->getSubmitted('handler_id');
109
110
        $result = $this->demo->create($handler_id, $store_id);
111
112
        if ($result === true) {
113
            $this->redirect('', $this->text('Demo content has been created'), 'success');
114
        }
115
116
        $this->redirect('', $result, 'warning');
117
    }
118
119
    /**
120
     * Delete the previously created demo content
121
     */
122
    protected function deleteDemo()
123
    {
124
        $this->controlAccessSuperAdmin();
125
126
        $store_id = $this->getSubmitted('store_id', 1);
127
        $handler_id = $this->getSubmitted('handler_id');
128
        $result = $this->demo->delete($handler_id, $store_id);
129
130
        if ($result === true) {
131
            $this->redirect('', $this->text('Demo content has been deleted'), 'success');
132
        }
133
134
        $this->redirect('', $result, 'warning');
135
    }
136
137
    /**
138
     * Render and output the demo content creation page
139
     */
140
    protected function outputEditDemo()
141
    {
142
        $this->output('demo|edit');
143
    }
144
145
}
146