Completed
Push — master ( d8ae4f...59728c )
by Iurii
01:58
created

Editor::backup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
1
<?php
2
3
/**
4
 * @package Theme editor
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2015, Iurii Makukh
7
 * @license https://www.gnu.org/licenses/gpl.html GNU/GPLv3
8
 */
9
10
namespace gplcart\modules\editor\models;
11
12
use gplcart\core\Model;
13
use gplcart\core\models\Backup as BackupModel,
14
    gplcart\core\models\Language as LanguageModel;
15
16
/**
17
 * Manages basic behaviors and data related to Theme Editor module
18
 */
19
class Editor extends Model
20
{
21
22
    /**
23
     * Language model instance
24
     * @var \gplcart\core\models\Language $language
25
     */
26
    protected $language;
27
28
    /**
29
     * Backup model instance
30
     * @var \gplcart\core\models\Backup $backup
31
     */
32
    protected $backup;
33
34
    /**
35
     * Constructor
36
     * @param LanguageModel $language
37
     * @param BackupModel $backup
38
     */
39
    public function __construct(LanguageModel $language, BackupModel $backup)
40
    {
41
        parent::__construct();
42
43
        $this->backup = $backup;
44
        $this->language = $language;
45
    }
46
47
    /**
48
     * Returns an array of editable files
49
     * @param array $module
50
     * @return array
51
     */
52
    public function getList(array $module)
53
    {
54
        $list = array();
55
        foreach (array('templates', 'css', 'js') as $folder) {
56
            $files = gplcart_file_scan_recursive("{$module['directory']}/$folder");
57
            sort($files);
58
            $list[$folder] = $files;
59
        }
60
61
        $this->hook->fire('module.editor.list', $list);
62
        return $list;
63
    }
64
65
    /**
66
     * Saves an edited file
67
     * @param array $data
68
     * @return boolean
69
     */
70
    public function save($data)
71
    {
72
        $this->hook->fire('module.editor.save.before', $data);
73
74
        if (empty($data)) {
75
            return false;
76
        }
77
78
        $has_backup = true;
79
        if (!$this->hasBackup($data['module'])) {
80
            $has_backup = $this->backup->backup('module', $data['module']);
81
        }
82
83
        if ($has_backup !== true) {
84
            return false;
85
        }
86
87
        $result = $this->write($data['content'], $data['path']);
88
89
        $this->hook->fire('module.editor.save.after', $data, $result);
90
        return $result;
91
    }
92
93
    /**
94
     * Writes a content to a file
95
     * @param string $content
96
     * @param string $file
97
     * @return boolean
98
     */
99
    protected function write($content, $file)
100
    {
101
        if (!file_exists($file)) {
102
            return false; // Do not create a new file
103
        }
104
105
        return file_put_contents($file, $content) !== false;
106
    }
107
108
    /**
109
     * Whether a module ID has a backup
110
     * @param array $module
111
     * @return boolean
112
     */
113
    public function hasBackup(array $module)
114
    {
115
        $conditions = array('module_id' => $module['id']);
116
        $existing = $this->backup->getList($conditions);
117
        return !empty($existing);
118
    }
119
120
}
121