repeatable.php
3.73 KB
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
<?php
/**
* @package Joomla.Plugin
* @subpackage Fields.Repeatable
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
defined('_JEXEC') or die;
JLoader::import('components.com_fields.libraries.fieldsplugin', JPATH_ADMINISTRATOR);
/**
* Repeatable plugin.
*
* @since 3.9.0
*/
class PlgFieldsRepeatable extends 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 JForm $form The form.
*
* @return DOMElement
*
* @since 3.9.0
*/
public function onCustomFieldsPrepareDom($field, DOMElement $parent, JForm $form)
{
$fieldNode = parent::onCustomFieldsPrepareDom($field, $parent, $form);
if (!$fieldNode)
{
return $fieldNode;
}
$readonly = false;
if (!FieldsHelper::canEditFieldValue($field))
{
$readonly = true;
}
$fieldNode->setAttribute('type', 'subform');
$fieldNode->setAttribute('multiple', 'true');
$fieldNode->setAttribute('layout', 'joomla.form.field.subform.repeatable-table');
// Build the form source
$fieldsXml = new SimpleXMLElement('<form/>');
$fields = $fieldsXml->addChild('fields');
// Get the form settings
$formFields = $field->fieldparams->get('fields');
// Add the fields to the form
foreach ($formFields as $index => $formField)
{
$child = $fields->addChild('field');
$child->addAttribute('name', $formField->fieldname);
$child->addAttribute('type', $formField->fieldtype);
$child->addAttribute('readonly', $readonly);
if (isset($formField->fieldfilter))
{
$child->addAttribute('filter', $formField->fieldfilter);
}
}
$fieldNode->setAttribute('formsource', $fieldsXml->asXML());
// Return the node
return $fieldNode;
}
/**
* The save event.
*
* @param string $context The context
* @param JTable $item The article data
* @param boolean $isNew Is new item
* @param array $data The validated data
*
* @return boolean
*
* @since 3.9.0
*/
public function onContentAfterSave($context, $item, $isNew, $data = array())
{
// Create correct context for category
if ($context == 'com_categories.category')
{
$context = $item->get('extension') . '.categories';
// Set the catid on the category to get only the fields which belong to this category
$item->set('catid', $item->get('id'));
}
// Check the context
$parts = FieldsHelper::extract($context, $item);
if (!$parts)
{
return true;
}
// Compile the right context for the fields
$context = $parts[0] . '.' . $parts[1];
// Loading the fields
$fields = FieldsHelper::getFields($context, $item);
if (!$fields)
{
return true;
}
// Get the fields data
$fieldsData = !empty($data['com_fields']) ? $data['com_fields'] : array();
// Loading the model
/** @var FieldsModelField $model */
$model = BaseDatabaseModel::getInstance('Field', 'FieldsModel', array('ignore_request' => true));
// Loop over the fields
foreach ($fields as $field)
{
// Find the field of this type repeatable
if ($field->type !== $this->_name)
{
continue;
}
// Determine the value if it is available from the data
$value = key_exists($field->name, $fieldsData) ? $fieldsData[$field->name] : null;
// Handle json encoded values
if (!is_array($value))
{
$value = json_decode($value, true);
}
// Setting the value for the field and the item
$model->setFieldValue($field->id, $item->get('id'), json_encode($value));
}
return true;
}
}