Completed
Push — master ( 908c1f...2a26cf )
by Iurii
03:37
created

Module::hookModuleInstallBefore()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
/**
4
 * @package File manager
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\file_manager;
11
12
use gplcart\core\Config,
13
    gplcart\core\Container;
14
15
/**
16
 * Main class for File manager module
17
 */
18
class Module
19
{
20
21
    /**
22
     * Config class instance
23
     * @var \gplcart\core\Config $config
24
     */
25
    protected $config;
26
27
    /**
28
     * @param Config $config
29
     */
30
    public function __construct(Config $config)
31
    {
32
        $this->config = $config;
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/file-manager'] = array(
42
            'access' => 'module_edit',
43
            'handlers' => array(
44
                'controller' => array('gplcart\\modules\\file_manager\\controllers\\Settings', 'editSettings')
45
            )
46
        );
47
48
        $routes['admin/tool/file-manager'] = array(
49
            'access' => 'module_file_manager',
50
            'menu' => array('admin' => /* @text */'File manager'),
51
            'handlers' => array(
52
                'controller' => array('gplcart\\modules\\file_manager\\controllers\\FileManager', 'viewFileManager')
53
            )
54
        );
55
    }
56
57
    /**
58
     * Implements hook "validator.handlers"
59
     * @param array $handlers
60
     */
61
    public function hookValidatorHandlers(array &$handlers)
62
    {
63
        foreach (array_keys($this->getModel()->getHandlers()) as $command_id) {
64
            $class = str_replace('_', '', $command_id);
65
            $handlers["file_manager_$command_id"] = array(
66
                'handlers' => array(
67
                    'validate' => array("gplcart\\modules\\file_manager\\handlers\\validators\\$class", "validate$class")
68
                ),
69
            );
70
        }
71
72
        $handlers['file_manager_settings'] = array(
73
            'handlers' => array(
74
                'validate' => array('gplcart\\modules\\file_manager\\handlers\\validators\\Settings', 'validateSettings')
75
            ),
76
        );
77
    }
78
79
    /**
80
     * Implements hook "user.role.permissions"
81
     * @param array $permissions
82
     */
83
    public function hookUserRolePermissions(array &$permissions)
84
    {
85
        $translation = $this->getTranslationModel();
86
        $permissions['module_file_manager'] = $translation->text('File manager: access');
87
88
        foreach ($this->getModel()->getHandlers() as $command_id => $command) {
89
            $permissions["module_file_manager_$command_id"] = $translation->text('File manager: perform command @name', array('@name' => $command['name']));
90
        }
91
    }
92
93
    /**
94
     * Implements hook "module.install.before"
95
     * @param null|string $result
96
     */
97
    public function hookModuleInstallBefore(&$result)
98
    {
99
        if (!class_exists('ZipArchive')) {
100
            $result = $this->getTranslationModel()->text('Class ZipArchive does not exist');
101
        }
102
    }
103
104
    /**
105
     * Implements hook "module.uninstall.after"
106
     */
107
    public function hookModuleUninstallAfter()
108
    {
109
        foreach (array_keys($this->config->select()) as $key) {
110
            if (strpos($key, 'module_file_manager_') === 0) {
111
                $this->config->reset($key);
112
            }
113
        }
114
    }
115
116
    /**
117
     * Returns Command model instance
118
     * @return \gplcart\modules\file_manager\models\Command
119
     */
120
    protected function getModel()
121
    {
122
        return Container::get('gplcart\\modules\\file_manager\\models\\Command');
123
    }
124
125
    /**
126
     * Translation UI model class instance
127
     * @return \gplcart\core\models\Translation
128
     */
129
    protected function getTranslationModel()
130
    {
131
        return Container::get('gplcart\\core\\models\\Translation');
132
    }
133
134
}
135