*/
public function readData() {
if ($this->formObject !== null) {
- if (empty($_POST)) {
- $this->setFormObjectData();
- }
+ $this->setFormObjectData();
}
else if ($this->formAction === 'edit') {
throw new \UnexpectedValueException("Missing form object to update.");
* Sets the form data based on the current form object.
*/
protected function setFormObjectData() {
- $this->form->loadValuesFromObject($this->formObject);
+ $this->form->updatedObject($this->formObject, empty($_POST));
}
/**
/**
* @inheritDoc
*/
- public function loadValuesFromObject(IStorableObject $object) {
+ public function updatedObject(IStorableObject $object, $loadValues = true) {
if ($this->formMode === null) {
$this->formMode(self::FORM_MODE_UPDATE);
}
if ($node instanceof IFormField) {
if ($node->getObjectProperty() !== $node->getId()) {
try {
- $node->loadValue($data, $object);
+ $node->updatedObject($data, $object, $loadValues);
}
catch (\InvalidArgumentException $e) {
// if an object property is explicitly set,
}
}
else {
- $node->loadValue($data, $object);
+ $node->updatedObject($data, $object, $loadValues);
}
}
else if ($node instanceof IFormContainer) {
- $node->loadValues($data, $object);
+ $node->updatedObject($data, $object, $loadValues);
}
}
}
public function invalid($invalid = true);
/**
- * Loads the field values from the given object and returns this document.
+ * Sets the updated object (and loads the field values from the given object) and returns
+ * this document.
*
- * Per default, for each field, `IFormField::loadValueFromObject()` is called.
+ * Per default, for each field, `IFormField::updatedObject()` is called.
* This method automatically sets the form mode to `self::FORM_MODE_UPDATE`.
*
- * @param IStorableObject $object object used to load field values
+ * @param IStorableObject $object updated object
+ * @param boolean $loadValues indicates if the object's values are loaded
* @return static this document
*/
- public function loadValuesFromObject(IStorableObject $object);
+ public function updatedObject(IStorableObject $object, $loadValues = true);
/**
* Sets the `method` property of the HTML `form` element and returns this document.
/**
* @inheritDoc
*/
- public function loadValues(array $data, IStorableObject $object) {
+ public function updatedObject(array $data, IStorableObject $object, $loadValues = true) {
// does nothing
return $this;
*/
interface IFormContainer extends IFormChildNode, IFormElement, IFormParentNode {
/**
- * This method is called by `IFormDocument::loadValue()` to inform the container that object
- * data is being loaded.
+ * Informs the form container of the updated object and this method is called by
+ * `IFormDocument::updatedObject()` to inform the container that object data is being loaded.
+ *
+ * This method is *not* intended to generally call `IFormField::updatedObject()` on its form field
+ * children as these methods are already called by `IFormDocument::updatedObject()`.
*
- * This method is *not* intended to generally call `IFormField::loadValue()` on its form field
- * children as these methods are already called by `IFormDocument::loadValuesFromObject()`.
- *
* @param array $data data from which the values are extracted
* @param IStorableObject $object object the data belongs to
+ * @param bool $loadValues
+ *
* @return static this container
*/
- public function loadValues(array $data, IStorableObject $object);
+ public function updatedObject(array $data, IStorableObject $object, $loadValues = true);
}
/**
* @inheritDoc
*/
- public function loadValues(array $data, IStorableObject $object) {
+ public function updatedObject(array $data, IStorableObject $object, $loadValues = true) {
$this->objectId = $object->{$object::getDatabaseTableIndexName()};
- if ($this->attachmentData !== null) {
- // updated attachment handler with object id
- $this->attachmentField->attachmentHandler(
- new AttachmentHandler(
- $this->attachmentData['objectType'],
- $this->getObjectId(),
- '.',
- $this->attachmentData['parentObjectID']
- )
- );
- }
+ $this->setAttachmentHandler();
- return parent::loadValues($data, $object);
+ return parent::updatedObject($data, $object);
}
/**
]);
if ($this->attachmentData !== null) {
- $this->attachmentField->attachmentHandler(
- // the temporary hash may not be empty (at the same time as the
- // object id) and it will be changed anyway by the called method
- new AttachmentHandler(
- $this->attachmentData['objectType'],
- $this->getObjectId(),
- '.',
- $this->attachmentData['parentObjectID']
- )
- );
+ $this->setAttachmentHandler();
}
$this->getDocument()->addButton(
return $this;
}
+ /**
+ * Sets the attachment handler of the attachment form field.
+ */
+ protected function setAttachmentHandler() {
+ if ($this->attachmentData !== null) {
+ $this->attachmentField->attachmentHandler(
+ new AttachmentHandler(
+ $this->attachmentData['objectType'],
+ $this->getObjectId(),
+ '.',
+ $this->attachmentData['parentObjectID']
+ )
+ );
+ }
+ }
+
/**
* Sets if mentions are supported by the editor field and returns this form container.
*
/**
* @inheritDoc
*/
- public function loadValues(array $data, IStorableObject $object) {
- if ($data instanceof IPollContainer && $data->getPollID() !== null) {
+ public function updatedObject(array $data, IStorableObject $object, $loadValues = true) {
+ if ($loadValues && $data instanceof IPollContainer && $data->getPollID() !== null) {
$this->poll = new Poll($data->getPollID());
if (!$this->poll->pollID) {
$this->poll = null;
$this->getSortByVotesField()->value($this->poll->sortByVotes);
}
- return parent::loadValues($data, $object);
+ return parent::updatedObject($data, $object);
}
/**
/**
* @inheritDoc
*/
- public function loadValue(array $data, IStorableObject $object) {
- if (isset($data[$this->getObjectProperty()])) {
+ public function updatedObject(array $data, IStorableObject $object, $loadValues = true) {
+ if ($loadValues && isset($data[$this->getObjectProperty()])) {
$this->value($data[$this->getObjectProperty()]);
}
public function isRequired();
/**
- * Loads the field value from the given data and returns this field.
+ * Informs the form field of the updated object (and loads the field value from the given data)
+ * and returns this field.
*
* It is important to extract the value from the `$data` array instead of getting it directly
* from the object as the entries of `$data` have been processed by the data processors.
*
- * @param array $data data from which the value is extracted
- * @param IStorableObject $object object the data belongs to
+ * @param array $data object data
+ * @param IStorableObject $object updated object
+ * @param bool $loadValues indicates if object data is loaded
* @return static this field
*/
- public function loadValue(array $data, IStorableObject $object);
+ public function updatedObject(array $data, IStorableObject $object, $loadValues = true);
/**
* Sets the name of the object property this field represents. If an empty
/**
* @inheritDoc
*/
- public function loadValueFromObject(array $data, IStorableObject $object) {
- if (isset($data[$this->getObjectProperty()])) {
+ public function updatedObject(array $data, IStorableObject $object, $loadValues = true) {
+ if ($loadValues && isset($data[$this->getObjectProperty()])) {
$value = $data[$this->getObjectProperty()];
if ($this->isI18n()) {
*
* @throws \InvalidArgumentException if the getter for the value provides invalid values
*/
- public function loadValue(array $data, IStorableObject $object) {
- // first check, whether an getter for the field exists
- if (method_exists($object, 'get'. ucfirst($this->getObjectProperty()) . 'UploadFileLocations')) {
- $value = call_user_func([$object, 'get'. ucfirst($this->getObjectProperty()) . 'UploadFileLocations']);
- $method = "method '" . get_class($object) . "::get" . ucfirst($this->getObjectProperty()) . "UploadFileLocations()'";
- }
- else if (method_exists($object, 'get'. ucfirst($this->getObjectProperty()))) {
- $value = call_user_func([$object, 'get'. ucfirst($this->getObjectProperty())]);
- $method = "method '" . get_class($object) . "::get" . ucfirst($this->getObjectProperty()) . "()'";
- }
- else {
- $value = $data[$this->getObjectProperty()];
- $method = "variable '" . get_class($object) . "::$" . $this->getObjectProperty() . "'";
- }
-
- if (is_array($value)) {
- $value = array_map(function ($v) use ($method) {
- if (!is_string($v) || !file_exists($v)) {
- throw new \InvalidArgumentException("The " . $method . " must return an array of strings with the file locations.");
- }
- return new UploadFile($v, basename($v), UploadHandler::isValidImage($v, basename($v), $this->svgImageAllowed()), true, $this->svgImageAllowed());
- }, $value);
+ public function updatedObject(array $data, IStorableObject $object, $loadValues = true) {
+ if ($loadValues) {
+ // first check, whether an getter for the field exists
+ if (method_exists($object, 'get'. ucfirst($this->getObjectProperty()) . 'UploadFileLocations')) {
+ $value = call_user_func([$object, 'get'. ucfirst($this->getObjectProperty()) . 'UploadFileLocations']);
+ $method = "method '" . get_class($object) . "::get" . ucfirst($this->getObjectProperty()) . "UploadFileLocations()'";
+ }
+ else if (method_exists($object, 'get'. ucfirst($this->getObjectProperty()))) {
+ $value = call_user_func([$object, 'get'. ucfirst($this->getObjectProperty())]);
+ $method = "method '" . get_class($object) . "::get" . ucfirst($this->getObjectProperty()) . "()'";
+ }
+ else {
+ $value = $data[$this->getObjectProperty()];
+ $method = "variable '" . get_class($object) . "::$" . $this->getObjectProperty() . "'";
+ }
- $this->value($value);
- }
- else {
- throw new \InvalidArgumentException("The " . $method . " must return an array of strings with the file locations.");
+ if (is_array($value)) {
+ $value = array_map(function ($v) use ($method) {
+ if (!is_string($v) || !file_exists($v)) {
+ throw new \InvalidArgumentException("The " . $method . " must return an array of strings with the file locations.");
+ }
+ return new UploadFile($v, basename($v), UploadHandler::isValidImage($v, basename($v), $this->svgImageAllowed()), true, $this->svgImageAllowed());
+ }, $value);
+
+ $this->value($value);
+ }
+ else {
+ throw new \InvalidArgumentException("The " . $method . " must return an array of strings with the file locations.");
+ }
}
return $this;
/**
* @inheritDoc
*/
- public function loadValue(array $data, IStorableObject $object) {
+ public function updatedObject(array $data, IStorableObject $object, $loadValues = true) {
$this->objectID = $object->{$object::getDatabaseTableIndexName()};
if ($this->objectID === null) {
/**
* @inheritDoc
*/
- public function loadValue(array $data, IStorableObject $object) {
- $objectTypeID = $this->getObjectType()->objectTypeID;
- $objectID = $object->{$object::getDatabaseTableIndexName()};
-
- if (!isset(static::$loadedLabels[$objectTypeID])) {
- static::$loadedLabels[$objectTypeID] = [];
- }
- if (!isset(static::$loadedLabels[$objectTypeID][$objectID])) {
- static::$loadedLabels[$objectTypeID][$objectID] = LabelHandler::getInstance()->getAssignedLabels(
- $objectTypeID,
- [$objectID]
- )[$objectID];
- }
-
- $labelIDs = $this->getLabelGroup()->getLabelIDs();
- /** @var Label $label */
- foreach (static::$loadedLabels[$objectTypeID][$objectID] as $label) {
- if (in_array($label->labelID, $labelIDs)) {
- $this->value($label->labelID);
+ public function updatedObject(array $data, IStorableObject $object, $loadValues = true) {
+ if ($loadValues) {
+ $objectTypeID = $this->getObjectType()->objectTypeID;
+ $objectID = $object->{$object::getDatabaseTableIndexName()};
+
+ if (!isset(static::$loadedLabels[$objectTypeID])) {
+ static::$loadedLabels[$objectTypeID] = [];
+ }
+ if (!isset(static::$loadedLabels[$objectTypeID][$objectID])) {
+ static::$loadedLabels[$objectTypeID][$objectID] = LabelHandler::getInstance()->getAssignedLabels(
+ $objectTypeID,
+ [$objectID]
+ )[$objectID];
+ }
+
+ $labelIDs = $this->getLabelGroup()->getLabelIDs();
+ /** @var Label $label */
+ foreach (static::$loadedLabels[$objectTypeID][$objectID] as $label) {
+ if (in_array($label->labelID, $labelIDs)) {
+ $this->value($label->labelID);
+ }
}
}
/**
* @inheritDoc
*/
- public function loadValue(array $data, IStorableObject $object) {
- $objectID = $object->{$object::getDatabaseTableIndexName()};
-
- if ($objectID === null) {
- throw new \UnexpectedValueException("Cannot read object id from object of class '" . get_class($object). "'.");
- }
-
- if ($this->getObjectType() === null) {
- throw new \UnexpectedValueException("Missing taggable object type.");
- }
-
- $languageIDs = [];
-
- /** @noinspection PhpUndefinedFieldInspection */
- if (isset($data['languageID'])) {
- $languageIDs[] = $data['languageID'];
- }
-
- $tags = TagEngine::getInstance()->getObjectTags($this->getObjectType()->objectType, $objectID, $languageIDs);
-
- $this->value = [];
- foreach ($tags as $tag) {
- $this->value[] = $tag->name;
+ public function updatedObject(array $data, IStorableObject $object, $loadValues = true) {
+ if ($loadValues) {
+ $objectID = $object->{$object::getDatabaseTableIndexName()};
+
+ if ($objectID === null) {
+ throw new \UnexpectedValueException("Cannot read object id from object of class '" . get_class($object). "'.");
+ }
+
+ if ($this->getObjectType() === null) {
+ throw new \UnexpectedValueException("Missing taggable object type.");
+ }
+
+ $languageIDs = [];
+
+ /** @noinspection PhpUndefinedFieldInspection */
+ if (isset($data['languageID'])) {
+ $languageIDs[] = $data['languageID'];
+ }
+
+ $tags = TagEngine::getInstance()->getObjectTags($this->getObjectType()->objectType, $objectID, $languageIDs);
+
+ $this->value = [];
+ foreach ($tags as $tag) {
+ $this->value[] = $tag->name;
+ }
}
return $this;