Listing::getFilesListing()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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