Elgg  Version 6.3
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)) {
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 === 'switch') {
89  $value = (bool) $value;
90  } elseif ($field_type === 'tags') {
92  } elseif ($name === 'title') {
94  }
95 
96  if ($field_type === 'file') {
97  $uploaded_file = elgg_get_uploaded_file($name, false);
98  if ($uploaded_file && !$uploaded_file->isValid()) {
99  throw new ValidationException(elgg_get_friendly_upload_error($uploaded_file->getError()));
100  }
101  }
102 
103  if ($field_type === 'url' && !elgg_is_empty($value) && !filter_var($value, FILTER_VALIDATE_URL)) {
104  throw new ValidationException(elgg_echo('ValidationException:field:url', [$name]));
105  }
106 
107  if (elgg_extract('required', $field) && elgg_is_empty($value)) {
108  throw new ValidationException(elgg_echo('ValidationException:field:required', [$name]));
109  }
110 
111  if ($field_type === 'file') {
112  // files need their own save logic for now
113  continue;
114  }
115 
116  $this->entity->{$name} = $value;
117  }
118  }
119 
125  protected function executeAfter(): void {
126  parent::executeAfter();
127 
128  if (!$this->entity->save()) {
129  throw new InternalServerErrorException(elgg_echo('save:fail'));
130  }
131 
132  if ($this->request->getParam('header_remove')) {
133  $this->entity->deleteIcon('header');
134  } else {
135  $this->entity->saveIconFromUploadedFile('header', 'header');
136  }
137  }
138 
144  protected function success(?string $forward_url = null): OkResponse {
145  //add to river only if new
146  if ($this->isNewEntity()) {
148  'action_type' => 'create',
149  'object_guid' => $this->entity->guid,
150  'target_guid' => $this->entity->container_guid,
151  ]);
152  }
153 
154  $success_keys = [
155  "entity:edit:{$this->entity->getType()}:{$this->entity->getSubtype()}:success",
156  "entity:edit:{$this->entity->getType()}:success",
157  ];
158 
159  $message = elgg_echo('entity:edit:success');
160  foreach ($success_keys as $success_key) {
161  if (elgg_language_key_exists($success_key)) {
162  $message = elgg_echo($success_key);
163  break;
164  }
165  }
166 
167  return elgg_ok_response('', $message, $forward_url ?: $this->entity->getURL());
168  }
169 
175  protected function isNewEntity(): bool {
176  return $this->is_new_entity;
177  }
178 }
$guid
Reset an ElggUpgrade.
Definition: reset.php:6
if(! $user||! $user->canDelete()) $name
Definition: delete.php:22
if(! $entity->save()) $success_keys
if(! $entity->delete(true, true)) $forward_url
Definition: delete.php:30
$subtype
Definition: delete.php:22
$type
Definition: delete.php:21
$container
Definition: delete.php:23
return[ 'admin/delete_admin_notices'=>['access'=> 'admin'], 'admin/menu/save'=>['access'=> 'admin'], 'admin/plugins/activate'=>['access'=> 'admin'], 'admin/plugins/activate_all'=>['access'=> 'admin'], 'admin/plugins/deactivate'=>['access'=> 'admin'], 'admin/plugins/deactivate_all'=>['access'=> 'admin'], 'admin/plugins/set_priority'=>['access'=> 'admin'], 'admin/security/security_txt'=>['access'=> 'admin'], 'admin/security/settings'=>['access'=> 'admin'], 'admin/security/regenerate_site_secret'=>['access'=> 'admin'], 'admin/site/cache/invalidate'=>['access'=> 'admin'], 'admin/site/flush_cache'=>['access'=> 'admin'], 'admin/site/icons'=>['access'=> 'admin'], 'admin/site/set_maintenance_mode'=>['access'=> 'admin'], 'admin/site/set_robots'=>['access'=> 'admin'], 'admin/site/theme'=>['access'=> 'admin'], 'admin/site/unlock_upgrade'=>['access'=> 'admin'], 'admin/site/settings'=>['access'=> 'admin'], 'admin/upgrade'=>['access'=> 'admin'], 'admin/upgrade/reset'=>['access'=> 'admin'], 'admin/user/ban'=>['access'=> 'admin'], 'admin/user/bulk/ban'=>['access'=> 'admin'], 'admin/user/bulk/delete'=>['access'=> 'admin'], 'admin/user/bulk/unban'=>['access'=> 'admin'], 'admin/user/bulk/validate'=>['access'=> 'admin'], 'admin/user/change_email'=>['access'=> 'admin'], 'admin/user/delete'=>['access'=> 'admin'], 'admin/user/login_as'=>['access'=> 'admin'], 'admin/user/logout_as'=>[], 'admin/user/makeadmin'=>['access'=> 'admin'], 'admin/user/resetpassword'=>['access'=> 'admin'], 'admin/user/removeadmin'=>['access'=> 'admin'], 'admin/user/unban'=>['access'=> 'admin'], 'admin/user/validate'=>['access'=> 'admin'], 'annotation/delete'=>[], 'avatar/upload'=>[], 'comment/save'=>[], 'diagnostics/download'=>['access'=> 'admin'], 'entity/chooserestoredestination'=>[], 'entity/delete'=>[], 'entity/mute'=>[], 'entity/restore'=>[], 'entity/subscribe'=>[], 'entity/trash'=>[], 'entity/unmute'=>[], 'entity/unsubscribe'=>[], 'login'=>['access'=> 'logged_out'], 'logout'=>[], 'notifications/mute'=>['access'=> 'public'], 'plugins/settings/remove'=>['access'=> 'admin'], 'plugins/settings/save'=>['access'=> 'admin'], 'plugins/usersettings/save'=>[], 'register'=>['access'=> 'logged_out', 'middleware'=>[\Elgg\Router\Middleware\RegistrationAllowedGatekeeper::class,],], 'river/delete'=>[], 'settings/notifications'=>[], 'settings/notifications/subscriptions'=>[], 'user/changepassword'=>['access'=> 'public'], 'user/requestnewpassword'=>['access'=> 'public'], 'useradd'=>['access'=> 'admin'], 'usersettings/save'=>[], 'widgets/add'=>[], 'widgets/delete'=>[], 'widgets/move'=>[], 'widgets/save'=>[],]
Definition: actions.php:73
getType()
Returns the entity type.
Definition: ElggEntity.php:478
canEdit(int $user_guid=0)
Can a user edit this entity?
Definition: ElggEntity.php:397
getSubtype()
Get the entity subtype.
Definition: ElggEntity.php:504
Generic entity edit action controller.
isNewEntity()
Is the entity being saved a new entity or being updated.
executeBefore()
{Preparation before executing the action.void}
execute(array $skip_field_names=[])
{}
executeAfter()
{Action part after the main execution.void}
success(?string $forward_url=null)
{}
Generic action controller.
Thrown when entity can not be edited or container permissions do not allow it to be written.
Thrown when the server encountered a generic error.
OK response builder.
Definition: OkResponse.php:8
$container_guid
if($item instanceof \ElggEntity) elseif($item instanceof \ElggRiverItem) elseif($item instanceof \ElggRelationship) elseif(is_callable([ $item, 'getType']))
Definition: item.php:48
if(elgg_extract('input_type', $vars)) if(elgg_extract('required', $vars)) if(elgg_extract('disabled', $vars)) $field
Definition: field.php:42
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:240
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_get_title_input(string $variable='title', string $default='')
Get an HTML-escaped title from input.
Definition: input.php:63
elgg_is_empty($value)
Check if a value isn't empty, but allow 0 and '0'.
Definition: input.php:176
get_entity(int $guid)
Loads and returns an entity object from a guid.
Definition: entities.php:68
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:19
elgg_get_uploaded_file(string $input_name, bool $check_for_validity=true)
Returns a single valid uploaded file object.
Definition: filestore.php:156
$value
Definition: generic.php:51
elgg_language_key_exists(string $key, string $language='en')
Check if a given language key exists.
Definition: languages.php:44
elgg_echo(string $message_key, array $args=[], string $language='')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
elgg_get_friendly_upload_error(int $error_code)
Returns a human-readable message for PHP's upload error codes.
Definition: output.php:272
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_create_river_item(array $options=[])
Elgg river.
Definition: river.php:28
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
Definition: robots.php:10
elgg_get_logged_in_user_guid()
Return the current logged in user by guid.
Definition: sessions.php:34