Elgg  Version 5.1
CropIcon.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Entity;
4 
6 
12 class CropIcon {
13 
21  public function __invoke(\Elgg\Event $event) {
22 
23  $crop_input = (array) get_input('_entity_edit_icon_crop');
24  if (empty($crop_input)) {
25  // BC fallback
26  $entity_guid = (int) get_input('_entity_edit_icon_crop_guid');
27  $input_name = (string) get_input('_entity_edit_icon_crop_input');
28  $icon_type = (string) get_input('_entity_edit_icon_crop_type');
29 
31  // not enough information
32  return;
33  }
34 
35  $crop_input[$input_name] = [
36  'guid' => $entity_guid,
37  'type' => $icon_type,
38  ];
39  }
40 
41  foreach ($crop_input as $input_name => $info) {
42  $entity_guid = (int) elgg_extract('guid', $info);
43  $icon_type = (string) elgg_extract('type', $info);
44 
46  continue;
47  }
48 
50  }
51  }
52 
62  protected function prepareUpload(int $entity_guid, string $input_name, string $icon_type): void {
63  if (get_input("{$input_name}_remove") || elgg_get_uploaded_file($input_name)) {
64  // user wanted to remove icon, or provided own icon
65  return;
66  }
67 
68  $entity = get_entity($entity_guid);
69  if (!$entity instanceof \ElggEntity || !$entity->hasIcon('master', $icon_type)) {
70  // entity doesn't have an icon
71  return;
72  }
73 
74  $current = [];
75  if ($icon_type === 'icon') {
76  $current = [
77  'x1' => $entity->x1,
78  'y1' => $entity->y1,
79  'x2' => $entity->x2,
80  'y2' => $entity->y2,
81  ];
82  } elseif (isset($entity->{"{$icon_type}_coords"})) {
83  $current = unserialize($entity->{"{$icon_type}_coords"});
84 
85  if (!is_array($current)) {
86  $current = [];
87  }
88  }
89 
90  // cast to ints
91  array_walk($current, function(&$value) {
92  $value = (int) $value;
93  });
94  // remove invalid values
95  $current = array_filter($current, function($value) {
96  return $value >= 0;
97  });
98 
99  $input_cropping_coords = [
100  'x1' => (int) get_input("{$input_name}_x1", get_input('x1')),
101  'y1' => (int) get_input("{$input_name}_y1", get_input('y1')),
102  'x2' => (int) get_input("{$input_name}_x2", get_input('x2')),
103  'y2' => (int) get_input("{$input_name}_y2", get_input('y2')),
104  ];
105 
106  $diff = array_diff_assoc($input_cropping_coords, $current);
107  if (empty($diff)) {
108  // no new cropping data
109  return;
110  }
111 
112  // get master image to fake an image upload with
113  $master = $entity->getIcon('master', $icon_type);
114 
115  // copy master to temp location
116  $tmp_file = new \ElggTempFile();
117  $tmp_file->open('write');
118  $tmp_file->write($master->grabFile());
119  $tmp_file->close();
120 
121  $file = [
122  'name' => basename($master->getFilenameOnFilestore()),
123  'type' => $master->getMimeType(),
124  'size' => $master->getSize(),
125  'tmp_name' => $tmp_file->getFilenameOnFilestore(),
126  'error' => UPLOAD_ERR_OK,
127  ];
128 
129  // store the 'new' file in PHP global
130  $_FILES[$input_name] = $file;
131 
132  // store the 'new' file in Elgg request filebag
133  $uploaded_file = $this->arrayToUploadedFile($file);
134 
135  $filebag = _elgg_services()->request->files;
136  $filebag->set($input_name, $uploaded_file);
137  }
138 
146  protected function arrayToUploadedFile($file_data) {
147 
148  if (!is_array($file_data)) {
149  return false;
150  }
151 
152  $req_fields = ['error', 'name', 'size', 'tmp_name', 'type'];
153  $keys = array_keys($file_data);
154  sort($keys);
155 
156  if ($keys !== $req_fields) {
157  return false;
158  }
159 
160  return new UploadedFile(
161  $file_data['tmp_name'],
162  $file_data['name'],
163  $file_data['type'],
164  $file_data['error'],
165  true
166  );
167  }
168 }
$input_name
Definition: crop.php:24
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
arrayToUploadedFile($file_data)
convert $_FILES array to UploadedFile
Definition: CropIcon.php:146
$keys
Definition: access.php:31
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof\ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
$value
Definition: generic.php:51
get_input(string $variable, $default=null, bool $filter_result=true)
Parameter input functions.
Definition: input.php:20
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:254
$entity_guid
Action for adding and editing comments.
Definition: save.php:6
$entity
Definition: reset.php:8
__invoke(\Elgg\Event $event)
Set inputs required to support cropping an existing icon.
Definition: CropIcon.php:21
get_entity(int $guid)
Loads and returns an entity object from a guid.
Definition: entities.php:67
$icon_type
Definition: crop.php:23
_elgg_services()
Get the global service provider.
Definition: elgglib.php:346
prepareUpload(int $entity_guid, string $input_name, string $icon_type)
Prepare a single existing icon for cropping.
Definition: CropIcon.php:62
Generic action listener to support cropping an existing icon.
Definition: CropIcon.php:12
Models an event passed to event handlers.
Definition: Event.php:11