/
controller.php
251 lines (221 loc) · 7.87 KB
/
controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
namespace Concrete\Attribute\ImageFile;
use Concrete\Core\Attribute\FontAwesomeIconFormatter;
use Concrete\Core\Entity\Attribute\Key\Settings\ImageFileSettings;
use Concrete\Core\Entity\Attribute\Value\Value\ImageFileValue;
use Concrete\Core\Error\ErrorList\Error\Error;
use Concrete\Core\Error\ErrorList\Error\FieldNotPresentError;
use Concrete\Core\Error\ErrorList\Field\AttributeField;
use Concrete\Core\File\Importer;
use Core;
use File;
use Concrete\Core\Backup\ContentExporter;
use Concrete\Core\Attribute\Controller as AttributeTypeController;
class Controller extends AttributeTypeController
{
protected $searchIndexFieldDefinition = array('type' => 'integer', 'options' => array('default' => 0, 'notnull' => false));
public function getIconFormatter()
{
return new FontAwesomeIconFormatter('download');
}
public function saveKey($data)
{
/**
* @var $type ImageFileSettings
*/
$type = $this->getAttributeKeySettings();
$data += array(
'mode' => null,
);
$mode = $data['mode'];
if ($mode == ImageFileSettings::TYPE_HTML_INPUT) {
$type->setModeToHtmlInput();
} else {
$type->setModeToFileManager();
}
return $type;
}
public function type_form()
{
$this->set('form', \Core::make('helper/form'));
$this->set('mode', $this->getAttributeKeySettings()->getMode());
}
public function exportKey($akey)
{
/**
* @var $type ImageFileSettings
*/
$type = $this->getAttributeKeySettings();
if ($type->isModeHtmlInput()) {
$mode = 'html_input';
} else {
$mode = 'file_manager';
}
$akey->addChild('type')->addAttribute('mode', $mode);
return $akey;
}
public function getDisplayValue()
{
$f = $this->getAttributeValue()->getValue();
if (is_object($f)) {
return '<a href="' . $f->getDownloadURL() . '">' . $f->getTitle() . '</a>';
}
}
public function exportValue(\SimpleXMLElement $akn)
{
$av = $akn->addChild('value');
$fo = $this->getAttributeValue()->getValue();
if (is_object($fo)) {
$av->addChild('fID', ContentExporter::replaceFileWithPlaceHolder($fo->getFileID()));
} else {
$av->addChild('fID', 0);
}
}
public function searchForm($list)
{
$fileID = $this->request('value');
$list->filterByAttribute($this->attributeKey->getAttributeKeyHandle(), $fileID);
return $list;
}
public function getSearchIndexValue()
{
$value = $this->getAttributeValue();
if (is_object($value)) {
$value = $this->getAttributeValue()->getValue();
if (is_object($value)) {
return $value->getFileID();
}
}
}
public function search()
{
// search by file causes too many problems
//$al = Core::make('helper/concrete/asset_library');
//print $al->file('ccm-file-akID-' . $this->attributeKey->getAttributeKeyID(), $this->field('value'), t('Choose File'), $bf);
}
public function form()
{
$bf = false;
if (is_object($this->attributeValue)) {
$bf = $this->getAttributeValue()->getValue();
}
$this->set('mode', $this->getAttributeKeySettings()->getMode());
$this->set('file', $bf);
}
public function importKey(\SimpleXMLElement $akey)
{
$type = $this->getAttributeKeySettings();
/**
* @var $type ImageFileSettings
*/
if (isset($akey->type)) {
$mode = (string) $akey->type['mode'];
if ($mode == 'html_input') {
$type->setModeToHtmlInput();
}
}
return $type;
}
public function importValue(\SimpleXMLElement $akv)
{
if (isset($akv->value->fID)) {
$fIDVal = (string) $akv->value->fID;
$inspector = \Core::make('import/value_inspector');
$result = $inspector->inspect($fIDVal);
$fID = $result->getReplacedValue();
if ($fID) {
$f = File::getByID($fID);
if (is_object($f)) {
return $this->createAttributeValue($f);
}
}
}
}
// run when we call setAttribute(), instead of saving through the UI
public function createAttributeValue($obj)
{
if ($obj && !is_object($obj)) {
$obj = File::getByID($obj);
}
$value = new ImageFileValue();
$value->setFileObject($obj);
return $value;
}
public function validateValue()
{
$f = $this->getAttributeValue()->getValue();
$e = Core::make('helper/validation/error');
if (!is_object($f)) {
$e->add(t('You must specify a valid file for %s', $this->attributeKey->getAttributeKeyDisplayName()));
}
return $e;
}
public function validateForm($data)
{
if ($this->getAttributeKeySettings()->isModeFileManager()) {
if (intval($data['value']) > 0) {
$f = File::getByID(intval($data['value']));
if (is_object($f) && !$f->isError()) {
return true;
} else {
return new Error(t('You must specify a valid file for %s', $this->getAttributeKey()->getAttributeKeyDisplayName()),
new AttributeField($this->getAttributeKey())
);
}
} else {
return new FieldNotPresentError(new AttributeField($this->getAttributeKey()));
}
}
if ($this->getAttributeKeySettings()->isModeHtmlInput()) {
$tmp_name = $_FILES['akID']['tmp_name'][$this->attributeKey->getAttributeKeyID()]['value'];
$name = $_FILES['akID']['name'][$this->attributeKey->getAttributeKeyID()]['value'];
if (!empty($tmp_name) && is_uploaded_file($tmp_name)) {
$fh = \Core::make('helper/validation/file');
if (!$fh->file($tmp_name)) {
return new Error(t('You have not uploaded a valid file.'),
new AttributeField($this->getAttributeKey())
);
}
if (!$fh->extension($name)) {
return new Error(t('Invalid file extension.'),
new AttributeField($this->getAttributeKey())
);
}
return true;
} else {
return new FieldNotPresentError(new AttributeField($this->getAttributeKey()));
}
}
}
public function createAttributeValueFromRequest()
{
$data = $this->post();
if ($this->getAttributeKeySettings()->isModeFileManager()) {
if ($data['value'] > 0) {
$f = File::getByID($data['value']);
return $this->createAttributeValue($f);
}
}
if ($this->getAttributeKeySettings()->isModeHtmlInput()) {
// import the file.
$tmp_name = $_FILES['akID']['tmp_name'][$this->attributeKey->getAttributeKeyID()]['value'];
$name = $_FILES['akID']['name'][$this->attributeKey->getAttributeKeyID()]['value'];
if (!empty($tmp_name) && is_uploaded_file($tmp_name)) {
$importer = new Importer();
$f = $importer->import($tmp_name, $name);
if (is_object($f)) {
return $this->createAttributeValue($f->getFile());
}
}
}
return $this->createAttributeValue(null);
}
public function getAttributeValueClass()
{
return ImageFileValue::class;
}
public function getAttributeKeySettingsClass()
{
return ImageFileSettings::class;
}
}