Elgg  Version 6.2
EntityEditAction.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Controllers;
4 
9 
16 
17  protected \ElggEntity $entity;
18 
19  protected bool $is_new_entity = true;
20 
27  protected function executeBefore(): void {
28  parent::executeBefore();
29 
30  $type = $this->request->getHttpRequest()->getRoute()?->getOption('entity_type');
31  $subtype = $this->request->getHttpRequest()->getRoute()?->getOption('entity_subtype');
32  if (!isset($type, $subtype)) {
33  throw new InternalServerErrorException(elgg_echo('actionnotfound', [(string) $this->request->getHttpRequest()->getRoute()?->getName()]));
34  }
35 
36  $entity_class = elgg_get_entity_class($type, $subtype);
37  if (empty($entity_class)) {
38  throw new InternalServerErrorException();
39  }
40 
41  $guid = (int) $this->request->getParam('guid');
42  if (!empty($guid)) {
43  $entity = get_entity($guid);
44  if (!$entity instanceof $entity_class) {
45  throw new InternalServerErrorException();
46  }
47 
48  if (!$entity->canEdit()) {
49  throw new EntityPermissionsException();
50  }
51 
52  $this->is_new_entity = false;
53  } else {
54  /* @var \ElggEntity $entity */
55  $entity = new $entity_class();
56 
57  $container_guid = (int) $this->request->getParam('container_guid', elgg_get_logged_in_user_guid());
59  if (!$container || !$container->canWriteToContainer(0, $entity->getType(), $entity->getSubtype())) {
60  throw new EntityPermissionsException();
61  }
62 
63  $entity->container_guid = $container->guid;
64  }
65 
66  $this->entity = $entity;
67  }
68 
76  protected function execute(array $skip_field_names = []): void {
77  parent::execute();
78 
79  foreach ($this->entity->getFields() as $field) {
80  $name = (string) elgg_extract('name', $field);
81  if (in_array($name, $skip_field_names)) {
82  continue;
83  }
84 
85  $field_type = (string) elgg_extract('#type', $field);
86  $value = $this->request->getParam($name);
87 
88  if ($field_type === 'tags') {
90  } elseif ($name === 'title') {
92  }
93 
94  if ($field_type === 'file') {
95  $uploaded_file = elgg_get_uploaded_file($name, false);
96  if ($uploaded_file && !$uploaded_file->isValid()) {
97  throw new ValidationException(elgg_get_friendly_upload_error($uploaded_file->getError()));
98  }
99  }
100 
101  if ($field_type === 'url' && !filter_var($value, FILTER_VALIDATE_URL)) {
102  throw new ValidationException(elgg_echo('ValidationException:field:url', [$name]));
103  }
104 
105  if (elgg_extract('required', $field) && elgg_is_empty($value)) {
106  throw new ValidationException(elgg_echo('ValidationException:field:required', [$name]));
107  }
108 
109  if ($field_type === 'file') {
110  // files need their own save logic for now
111  continue;
112  }
113 
114  $this->entity->{$name} = $value;
115  }
116  }
117 
123  protected function executeAfter(): void {
124  parent::executeAfter();
125 
126  if (!$this->entity->save()) {
127  throw new InternalServerErrorException(elgg_echo('save:fail'));
128  }
129 
130  if ($this->request->getParam('header_remove')) {
131  $this->entity->deleteIcon('header');
132  } else {
133  $this->entity->saveIconFromUploadedFile('header', 'header');
134  }
135  }
136 
142  protected function success(?string $forward_url = null): OkResponse {
143  //add to river only if new
144  if ($this->isNewEntity()) {
146  'action_type' => 'create',
147  'object_guid' => $this->entity->guid,
148  'target_guid' => $this->entity->container_guid,
149  ]);
150  }
151 
152  $success_keys = [
153  "entity:edit:{$this->entity->getType()}:{$this->entity->getSubtype()}:success",
154  "entity:edit:{$this->entity->getType()}:success",
155  ];
156 
157  $message = elgg_echo('entity:edit:success');
158  foreach ($success_keys as $success_key) {
159  if (elgg_language_key_exists($success_key)) {
160  $message = elgg_echo($success_key);
161  break;
162  }
163  }
164 
165  return elgg_ok_response('', $message, $forward_url ?: $this->entity->getURL());
166  }
167 
173  protected function isNewEntity(): bool {
174  return $this->is_new_entity;
175  }
176 }
elgg_get_title_input(string $variable= 'title', string $default= '')
Get an HTML-escaped title from input.
Definition: input.php:63
elgg_get_entity_class(string $type, string $subtype)
Return the class name registered as a constructor for an entity of a given type and subtype...
Definition: entities.php:21
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
elgg_get_uploaded_file(string $input_name, bool $check_for_validity=true)
Returns a single valid uploaded file object.
Definition: filestore.php:156
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
if(elgg_extract('input_type', $vars)) if(elgg_extract('required', $vars)) if(elgg_extract('disabled', $vars)) $field
Definition: field.php:42
$type
Definition: delete.php:21
elgg_string_to_array(string $string)
Takes in a comma-separated string and returns an array of uniquely trimmed and stripped strings...
Definition: input.php:226
elgg_create_river_item(array $options=[])
Elgg river.
Definition: river.php:28
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof\ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
success(?string $forward_url=null)
{}
$value
Definition: generic.php:51
elgg_is_empty($value)
Check if a value isn&#39;t empty, but allow 0 and &#39;0&#39;.
Definition: input.php:176
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:256
execute(array $skip_field_names=[])
{}
elgg_ok_response($content= '', string|array $message= '',?string $forward_url=null, int $status_code=ELGG_HTTP_OK)
Prepares a successful response to be returned by a page or an action handler.
elgg_language_key_exists(string $key, string $language= 'en')
Check if a given language key exists.
Definition: languages.php:44
get_entity(int $guid)
Loads and returns an entity object from a guid.
Definition: entities.php:70
Thrown when entity can not be edited or container permissions do not allow it to be written...
Generic entity edit action controller.
Thrown when the server encountered a generic error.
if(!$entity->delete(true, true)) $forward_url
Definition: delete.php:30
$container
Definition: delete.php:23
elgg_get_friendly_upload_error(int $error_code)
Returns a human-readable message for PHP&#39;s upload error codes.
Definition: output.php:272
Generic action controller.
$subtype
Definition: delete.php:22
if(!$entity->save()) $success_keys
$container_guid
OK response builder.
Definition: OkResponse.php:8
elgg_get_logged_in_user_guid()
Return the current logged in user by guid.
Definition: sessions.php:34
isNewEntity()
Is the entity being saved a new entity or being updated.
$guid
Reset an ElggUpgrade.
Definition: reset.php:6