Completed
Push — master ( f354fc...5f392d )
by Iurii
01:17
created

Listing::renderIcon()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
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\handlers\commands;
11
12
use gplcart\core\Config;
13
use gplcart\core\models\Language as LanguageModel;
14
use gplcart\modules\file_manager\models\Command as FileManagerCommandModel;
15
use gplcart\modules\file_manager\models\Scanner as FileManagerScannerModel;
16
use gplcart\modules\file_manager\handlers\commands\Base as FileManagerBaseHandler;
17
18
/**
19
 * Contains methods for "list" command
20
 */
21
class Listing extends FileManagerBaseHandler
22
{
23
24
    /**
25
     * Controller class instance
26
     * @var \gplcart\core\Controller $controller
27
     */
28
    protected $controller;
29
30
    /**
31
     * @param Config $config
32
     * @param LanguageModel $language
33
     * @param FileManagerCommandModel $command
34
     * @param FileManagerScannerModel $scanner
35
     */
36
    public function __construct(Config $config, LanguageModel $language,
37
            FileManagerCommandModel $command, FileManagerScannerModel $scanner)
38
    {
39
        parent::__construct($config, $language, $command, $scanner);
40
    }
41
42
    /**
43
     * Whether the command is allowed for the file
44
     * @param \SplFileInfo $file
45
     * @return bool
46
     */
47
    public function allowed($file)
48
    {
49
        return $file->isDir() && $file->isReadable();
50
    }
51
52
    /**
53
     * Returns an array of data used to display the command
54
     * @param \SplFileInfo $file
55
     * @param \gplcart\core\Controller $controller
56
     * @return array
57
     */
58
    public function view($file, $controller)
59
    {
60
        $this->controller = $controller;
61
62
        $path = $file->getRealPath();
63
        $params = $controller->getQuery(null, array(), 'array');
64
65
        $pager_options = array(
66
            'max_pages' => 5,
67
            'query' => $params,
68
            'total' => $this->getTotal($path, $params),
69
            'limit' => $this->config->getFromModule('file_manager', 'limit')
70
        );
71
72
        $pager = $this->controller->getPager($pager_options);
73
        $params['limit'] = $pager['limit'];
74
75
        return array(
76
            'file_manager|commands/list' => array(
77
                'pager' => $pager['rendered'],
78
                'filters' => $this->scanner->getFilters(),
79
                'sorters' => $this->scanner->getSorters(),
80
                'files' => $this->getFilesListing($path, $params)
81
        ));
82
    }
83
84
    /**
85
     * Returns an array of scanned and prepared files
86
     * @param string $path
87
     * @param array $params
88
     * @return array
89
     */
90
    protected function getFilesListing($path, array $params)
91
    {
92
        $files = $this->getFiles($path, $params);
93
        return $this->prepareFiles($files);
94
    }
95
96
    /**
97
     * Prepares an array of scanned files
98
     * @param array $files
99
     * @return array
100
     */
101
    protected function prepareFiles(array $files)
102
    {
103
        $prepared = array();
104
        foreach ($files as $file) {
105
106
            $type = $file->getType();
107
            $path = $file->getRealPath();
108
            $relative_path = gplcart_file_relative($path);
109
110
            $item = array(
111
                'info' => $file,
112
                'type' => $type,
113
                'path' => $relative_path,
114
                'owner' => fileowner($path),
115
                'extension' => $file->getExtension(),
116
                'size' => gplcart_file_size($file->getSize()),
117
                'command' => $type === 'dir' ? 'list' : 'read',
118
                'commands' => $this->command->getAllowed($file),
119
                'permissions' => gplcart_file_perms($file->getPerms())
120
            );
121
122
            $prepared[$relative_path] = $item;
123
            $prepared[$relative_path]['icon'] = $this->renderIcon($item);
124
        }
125
126
        return $prepared;
127
    }
128
129
    /**
130
     * Returns a rendered icon for the given file extension and type
131
     * @param array $item
132
     * @return string
133
     */
134
    protected function renderIcon(array $item)
135
    {
136
        static $rendered = array();
137
138
        if (isset($rendered[$item['extension']])) {
139
            return $rendered[$item['extension']];
140
        }
141
142
        $template = "file_manager|icons/ext/{$item['extension']}";
143
144
        if ($item['type'] === 'dir') {
145
            $template = 'file_manager|icons/dir';
146
        }
147
148
        $data = array('item' => $item);
149
        $default = $this->controller->render('file_manager|icons/file', $data);
150
        $rendered[$item['extension']] = $this->controller->render($template, $data, true, $default);
151
152
        return $rendered[$item['extension']];
153
    }
154
155
}
156