Open Journal Systems  3.3.0
FormComponent.inc.php
1 <?php
15 namespace PKP\components\forms;
16 
17 define('FIELD_POSITION_BEFORE', 'before');
18 define('FIELD_POSITION_AFTER', 'after');
19 
22  public $id = '';
23 
25  public $method = '';
26 
28  public $action = '';
29 
31  public $locales = [];
32 
34  public $fields = [];
35 
37  public $groups = [];
38 
40  public $pages = [];
41 
43  public $errors = [];
44 
53  public function __construct($id, $method, $action, $locales) {
54  $this->id = $id;
55  $this->action = $action;
56  $this->method = $method;
57  $this->locales = $locales;
58  }
59 
70  public function addField($field, $position = []) {
71  if (empty($position)) {
72  $this->fields[] = $field;
73  } else {
74  $this->fields = $this->addToPosition($position[1], $this->fields, $field, $position[0]);
75  }
76  return $this;
77  }
78 
85  public function removeField($fieldName) {
86  $this->fields = array_filter($this->fields, function($field) use ($fieldName) {
87  return $field->name !== $fieldName;
88  });
89  return $this;
90  }
91 
98  public function getField($fieldName) {
99  foreach ($this->fields as $field) {
100  if ($field->name === $fieldName) {
101  return $field;
102  }
103  }
104  return null;
105  }
106 
121  public function addGroup($args, $position = []) {
122  if (empty($args['id'])) {
123  fatalError('Tried to add a form group without an id.');
124  }
125  if (empty($position)) {
126  $this->groups[] = $args;
127  } else {
128  $this->groups = $this->addToPosition($position[1], $this->groups, $args, $position[0]);
129  }
130  return $this;
131  }
132 
139  public function removeGroup($groupId) {
140  $this->groups = array_filter($this->groups, function($group) use ($groupId) {
141  return $group['id'] !== $groupId;
142  });
143  $this->fields = array_filter($this->fields, function($field) use ($groupId) {
144  return $field['groupId'] !== $groupId;
145  });
146  return $this;
147  }
148 
164  public function addPage($args, $position = []) {
165  if (empty($args['id'])) {
166  fatalError('Tried to add a form page without an id.');
167  }
168  if (empty($position)) {
169  $this->pages[] = $args;
170  } else {
171  $this->pages = $this->addToPosition($position[1], $this->pages, $args, $position[0]);
172  }
173  return $this;
174  }
175 
182  public function removePage($pageId) {
183  $this->pages = array_filter($this->pages, function($page) use ($pageId) {
184  return $page['id'] !== $pageId;
185  });
186  foreach ($this->groups as $group) {
187  if ($group['pageId'] === $pageId) {
188  $this->removeGroup($group['id']);
189  }
190  }
191  return $this;
192  }
193 
203  public function addToPosition($id, $list, $item, $position) {
204  $index = count($list);
205  foreach ($list as $key => $val) {
206  if ((is_a($val, 'PKP\components\forms\Field') && $id === $val->name) || (!is_a($val, 'PKP\components\forms\Field') && $id === $val['id'])) {
207  $index = $key;
208  break;
209  }
210  }
211  if (!$index && $position === FIELD_POSITION_BEFORE) {
212  array_unshift($list, $item);
213  return $list;
214  }
215 
216  $slice = $position === FIELD_POSITION_BEFORE ? $index : $index + 1;
217 
218  return array_merge(
219  array_slice($list, 0, $slice),
220  [$item],
221  array_slice($list, $slice)
222  );
223  }
224 
231  public function getConfig() {
232 
233  if (empty($this->id) || empty($this->method) || empty($this->action)) {
234  throw new Exception('FormComponent::getConfig() was called but one or more required property is missing: id, method, action.');
235  }
236 
237  \HookRegistry::call('Form::config::before', $this);
238 
239  // Add a default page/group if none exist
240  if (!$this->groups) {
241  $this->addGroup(array('id' => 'default'));
242  $this->fields = array_map(function($field) {
243  $field->groupId = 'default';
244  return $field;
245  }, $this->fields);
246  }
247 
248  if (!$this->pages) {
249  $this->addPage(array('id' => 'default', 'submitButton' => array('label' => __('common.save'))));
250  $this->groups = array_map(function($group) {
251  $group['pageId'] = 'default';
252  return $group;
253  }, $this->groups);
254  }
255 
256  $fieldsConfig = array_map([$this, 'getFieldConfig'], $this->fields);
257 
258  $visibleLocales = [\AppLocale::getLocale()];
260  array_unshift($visibleLocales, \AppLocale::getPrimaryLocale());
261  }
262 
263  $config = array(
264  'id' => $this->id,
265  'method' => $this->method,
266  'action' => $this->action,
267  'fields' => $fieldsConfig,
268  'groups' => $this->groups,
269  'pages' => $this->pages,
270  'primaryLocale' => \AppLocale::getPrimaryLocale(),
271  'visibleLocales' => $visibleLocales,
272  'supportedFormLocales' => array_values($this->locales), // See #5690
273  'errors' => (object) [],
274  );
275 
276  \HookRegistry::call('Form::config::after', array(&$config, $this));
277 
278  return $config;
279  }
280 
287  public function getFieldConfig($field) {
288  $config = $field->getConfig();
289 
290  // Add a value property if the field does not include one
291  if (!array_key_exists('value', $config)) {
292  $config['value'] = $field->isMultilingual ? array() : $field->getEmptyValue();
293  }
294  if ($field->isMultilingual) {
295  foreach ($this->locales as $locale) {
296  if (!array_key_exists($locale['key'], $config['value'])) {
297  $config['value'][$locale['key']] = $field->getEmptyValue();
298  }
299  }
300  }
301 
302  return $config;
303  }
304 }
PKP\components\forms
PKP\components\forms\FormComponent\addGroup
addGroup($args, $position=[])
Definition: FormComponent.inc.php:145
PKP\components\forms\FormComponent\$groups
$groups
Definition: FormComponent.inc.php:55
PKP\components\forms\FormComponent\$fields
$fields
Definition: FormComponent.inc.php:49
AppLocale\getPrimaryLocale
static getPrimaryLocale()
Definition: env1/MockAppLocale.inc.php:95
PKP\components\forms\FormComponent\getFieldConfig
getFieldConfig($field)
Definition: FormComponent.inc.php:311
PKP\components\forms\FormComponent\$pages
$pages
Definition: FormComponent.inc.php:61
PKP\components\forms\FormComponent\removeGroup
removeGroup($groupId)
Definition: FormComponent.inc.php:163
PKP\components\forms\FormComponent\$action
$action
Definition: FormComponent.inc.php:37
PKP\components\forms\FormComponent\$id
$id
Definition: FormComponent.inc.php:25
PKP\components\forms\FormComponent\$errors
$errors
Definition: FormComponent.inc.php:67
PKP\components\forms\FormComponent\removeField
removeField($fieldName)
Definition: FormComponent.inc.php:109
PKP\components\forms\FormComponent
Definition: FormComponent.inc.php:20
PKP\components\forms\FormComponent\__construct
__construct($id, $method, $action, $locales)
Definition: FormComponent.inc.php:77
PKP\components\forms\FormComponent\removePage
removePage($pageId)
Definition: FormComponent.inc.php:206
PKP\components\forms\FormComponent\getConfig
getConfig()
Definition: FormComponent.inc.php:255
PKP\components\forms\FIELD_POSITION_BEFORE
const FIELD_POSITION_BEFORE
Definition: FormComponent.inc.php:17
PKP\components\forms\FormComponent\addPage
addPage($args, $position=[])
Definition: FormComponent.inc.php:188
PKP\components\forms\FormComponent\addToPosition
addToPosition($id, $list, $item, $position)
Definition: FormComponent.inc.php:227
PKP\components\forms\FormComponent\getField
getField($fieldName)
Definition: FormComponent.inc.php:122
PKP\components\forms\FormComponent\addField
addField($field, $position=[])
Definition: FormComponent.inc.php:94
PKP\components\forms\FormComponent\$locales
$locales
Definition: FormComponent.inc.php:43
fatalError
if(!function_exists('import')) fatalError($reason)
Definition: functions.inc.php:32
HookRegistry\call
static call($hookName, $args=null)
Definition: HookRegistry.inc.php:86
AppLocale\getLocale
static getLocale()
Definition: env1/MockAppLocale.inc.php:40
PKP\components\forms\FormComponent\$method
$method
Definition: FormComponent.inc.php:31