-
Notifications
You must be signed in to change notification settings - Fork 2
/
file.php
111 lines (97 loc) · 2.6 KB
/
file.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
<?php
/**
* Plugin Field - File based on Media Manger
* ------------------------------------------------------------------------
* Author it-conserv.de
* Copyright (C) 2021 it-conserv.de All Rights Reserved.
* License - http://www.gnu.org/licenses/gpl-3.0.html GNU/GPL
* Websites: it-conserv.de
*/
defined('_JEXEC') or die;
use Joomla\CMS\Form\Form;
/**
* Fields File Plugin
*
* @since 3.7.0
*/
class PlgFieldsFile extends \Joomla\Component\Fields\Administrator\Plugin\FieldsPlugin
{
/**
* Transforms the field into a DOM XML element and appends it as a child on the given parent.
*
* @param stdClass $field The field.
* @param DOMElement $parent The field node parent.
* @param Form $form The form.
*
* @return DOMElement
*
* @since 4.0.0
*/
public function onCustomFieldsPrepareDom($field, DOMElement $parent, Form $form)
{
$fieldNode = parent::onCustomFieldsPrepareDom($field, $parent, $form);
if (!$fieldNode)
{
return $fieldNode;
}
// see administrator/components/com_media/src/Model/ApiModel.php
$fieldNode->setAttribute('type', 'media');
$fieldNode->setAttribute('types', 'documents,videos,audios');
$fieldNode->setAttribute('preview', 'false');
return $fieldNode;
}
/**
* Before prepares the field value.
*
* @param string $context The context.
* @param \stdclass $item The item.
* @param \stdclass $field The field.
*
* @return void
*
* @since 4.0.0
*/
public function onCustomFieldsBeforePrepareField($context, $item, $field)
{
// Check if the field should be processed by us
if (!$this->isTypeSupported($field->type))
{
return;
}
// Check if the field value is an old (string) value
$field->value = $this->checkValue($field->value);
}
/**
* Before prepares the field value.
*
* @param string $value The value to check.
* @return array The checked value
* @since 4.0.0
*/
private function checkValue($value)
{
json_decode($value);
if (json_last_error() === JSON_ERROR_NONE)
{
return (array) json_decode($value, true);
}
return array('file' => $value, 'alt_text' => '');
}
/**
* Check the filesize
*
* @param string $rawvalue
* @return string The filesize with unit
* @since 4.0.0
*/
function get_file_size($file)
{
$filepath = str_replace('%20', ' ', $file);
$size = filesize($filepath);
$units = array("Byte","KB","MB","GB","TB","PB","EB","ZB","YB");
foreach($units as $pow => $unit) {
if ($size / pow(1024, $pow) < 1024)
return number_format($size / pow(1024, $pow), 1, ",", ".") .' ' . $unit;
}
}
}