Completed
Push — master ( 24f445...43b2cf )
by Iurii
01:25
created

Rename::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
/**
13
 * Contains methods for "rename" command
14
 */
15
class Rename extends Command
16
{
17
18
    /**
19
     * Whether the command is allowed for the file
20
     * @param \SplFileInfo $file
21
     * @return bool
22
     */
23
    public function allowed($file)
24
    {
25
        return in_array($file->getType(), array('file', 'dir')) && $file->isWritable() && !$this->isInitialPath($file);
26
    }
27
28
    /**
29
     * Returns an array data used to display "rename" command output
30
     * @param \SplFileInfo $file
31
     * @return array
32
     */
33
    public function view($file)
34
    {
35
        return array(
36
            'file_manager|commands/rename' => array(
37
                'name' => $file->getBasename(),
38
                'path' => dirname($this->getRelativePath($file->getRealPath()))
39
            ));
40
    }
41
42
    /**
43
     * Rename a file
44
     * @param \gplcart\core\Controller $controller
45
     * @return array
46
     */
47
    public function submit($controller)
48
    {
49
        set_time_limit(0);
50
51
        $submitted = $controller->getSubmitted();
52
        $file = reset($submitted['files']);
53
54
        $path = $file->getRealPath();
55
        $destination = $controller->getSubmitted('destination');
56
57
        $query = array(
58
            'cmd' => 'list',
59
            'path' => $this->getRelativeDirectory($path)
60
        );
61
62
        $errors = $success = 0;
63
        $this->move($path, $destination, $errors, $success);
64
65
        $vars = array('@num_success' => $success, '@num_errors' => $errors);
66
67
        return array(
68
            'redirect' => $controller->url('', $query),
69
            'severity' => empty($errors) ? 'success' : 'warning',
70
            'message' => $this->translation->text('Renamed @num_success, errors: @num_errors', $vars)
71
        );
72
    }
73
74
}
75