1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Icybee package. |
5
|
|
|
* |
6
|
|
|
* (c) Olivier Laviale <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Icybee\Modules\Files\Block; |
13
|
|
|
|
14
|
|
|
use ICanBoogie\I18n; |
15
|
|
|
use ICanBoogie\Operation; |
16
|
|
|
|
17
|
|
|
use Brickrouge\Element; |
18
|
|
|
use Brickrouge\Document; |
19
|
|
|
use Brickrouge\Form; |
20
|
|
|
|
21
|
|
|
use Icybee\Modules\Files as Root; |
22
|
|
|
use Icybee\Modules\Files\File; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @property File $record |
26
|
|
|
*/ |
27
|
|
|
class EditBlock extends \Icybee\Modules\Nodes\Block\EditBlock |
28
|
|
|
{ |
29
|
|
|
const ACCEPT = '#files-accept'; |
30
|
|
|
const UPLOADER_CLASS = 'uploader class'; |
31
|
|
|
|
32
|
|
|
protected $accept = null; |
33
|
|
|
protected $uploader_class = 'Icybee\Modules\Files\FileUpload'; |
34
|
|
|
|
35
|
|
|
static protected function add_assets(Document $document) |
36
|
|
|
{ |
37
|
|
|
parent::add_assets($document); |
38
|
|
|
|
39
|
|
|
$document->css->add(Root\DIR . 'public/edit.css'); |
40
|
|
|
$document->js->add(Root\DIR . 'public/edit.js'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function lazy_get_values() |
44
|
|
|
{ |
45
|
|
|
return parent::lazy_get_values() + [ |
46
|
|
|
|
47
|
|
|
File::NID => null, |
48
|
|
|
File::HTTP_FILE => null |
49
|
|
|
|
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function lazy_get_children() |
54
|
|
|
{ |
55
|
|
|
$folder = \ICanBoogie\REPOSITORY . 'tmp'; |
56
|
|
|
|
57
|
|
|
if (!is_writable($folder)) |
58
|
|
|
{ |
59
|
|
|
return [ |
|
|
|
|
60
|
|
|
|
61
|
|
|
Element::CHILDREN => [ |
62
|
|
|
|
63
|
|
|
$this->t('The folder %folder is not writable !', [ '%folder' => $folder ]) |
64
|
|
|
|
65
|
|
|
] |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$properties = $this->values; |
70
|
|
|
$nid = $properties[File::NID]; |
71
|
|
|
$path = \ICanBoogie\strip_root($properties[File::HTTP_FILE]); |
72
|
|
|
|
73
|
|
|
if (!$path && $this->record) |
74
|
|
|
{ |
75
|
|
|
try |
76
|
|
|
{ |
77
|
|
|
$path = $this->record->pathname->relative; |
78
|
|
|
} |
79
|
|
|
catch (\Exception $e) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
# |
82
|
|
|
# the associated file might have disappeared |
83
|
|
|
# |
84
|
|
|
|
85
|
|
|
\ICanBoogie\log_error($e->getMessage()); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->attributes = \ICanBoogie\array_merge_recursive($this->attributes, [ |
90
|
|
|
|
91
|
|
|
Form::HIDDENS => [ |
92
|
|
|
|
93
|
|
|
File::HTTP_FILE => $path |
94
|
|
|
|
95
|
|
|
], |
96
|
|
|
|
97
|
|
|
Form::VALUES => [ |
98
|
|
|
|
99
|
|
|
File::HTTP_FILE => $path |
100
|
|
|
|
101
|
|
|
] |
102
|
|
|
|
103
|
|
|
]); |
104
|
|
|
|
105
|
|
|
$options = [ |
106
|
|
|
|
107
|
|
|
self::ACCEPT => $this->accept, |
108
|
|
|
self::UPLOADER_CLASS => $this->uploader_class |
109
|
|
|
|
110
|
|
|
]; |
111
|
|
|
|
112
|
|
|
$uploader_class = $options[self::UPLOADER_CLASS]; |
113
|
|
|
|
114
|
|
|
return array_merge(parent::lazy_get_children(), [ |
115
|
|
|
|
116
|
|
|
File::HTTP_FILE => new $uploader_class([ |
117
|
|
|
|
118
|
|
|
Form::LABEL => 'file', |
|
|
|
|
119
|
|
|
Element::REQUIRED => empty($nid), |
120
|
|
|
\Brickrouge\File::FILE_WITH_LIMIT => $this->app->site->metas[$this->module->flat_id . '.max_file_size'], |
121
|
|
|
Element::WEIGHT => -100, |
122
|
|
|
\Brickrouge\File::T_UPLOAD_URL => Operation::encode($this->module->id . '/upload'), |
123
|
|
|
|
124
|
|
|
'value' => $path |
125
|
|
|
|
126
|
|
|
]), |
127
|
|
|
|
128
|
|
|
File::DESCRIPTION => $this->app->editors['rte']->from([ |
129
|
|
|
|
130
|
|
|
Form::LABEL => 'description', |
|
|
|
|
131
|
|
|
Element::WEIGHT => 50, |
132
|
|
|
|
133
|
|
|
'rows' => 5 |
134
|
|
|
|
135
|
|
|
]) |
136
|
|
|
]); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.