Elgg  Version 4.3
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\Hook $hook) {
22 
23  $entity_guid = (int) get_input('_entity_edit_icon_crop_guid');
24  $input_name = get_input('_entity_edit_icon_crop_input');
25  $icon_type = get_input('_entity_edit_icon_crop_type');
26 
28  // not enough information
29  return;
30  }
31 
32  if (get_input("{$input_name}_remove") || elgg_get_uploaded_file($input_name)) {
33  // user wanted to remove icon, or provided own icon
34  return;
35  }
36 
38  if (!$entity instanceof \ElggEntity || !$entity->hasIcon('master', $icon_type)) {
39  // entity doesn't have an icon
40  return;
41  }
42 
43  $current = [];
44  if ($icon_type === 'icon') {
45  $current = [
46  'x1' => $entity->x1,
47  'y1' => $entity->y1,
48  'x2' => $entity->x2,
49  'y2' => $entity->y2,
50  ];
51  } elseif (isset($entity->{"{$icon_type}_coords"})) {
52  $current = unserialize($entity->{"{$icon_type}_coords"});
53 
54  if (!is_array($current)) {
55  $current = [];
56  }
57  }
58 
59  // cast to ints
60  array_walk($current, function(&$value) {
61  $value = (int) $value;
62  });
63  // remove invalid values
64  $current = array_filter($current, function($value) {
65  return $value >= 0;
66  });
67 
68  $input_cropping_coords = [
69  'x1' => (int) get_input('x1'),
70  'y1' => (int) get_input('y1'),
71  'x2' => (int) get_input('x2'),
72  'y2' => (int) get_input('y2'),
73  ];
74 
75  $diff = array_diff_assoc($input_cropping_coords, $current);
76  if (empty($diff)) {
77  // no new cropping data
78  return;
79  }
80 
81  // get master image to fake an image upload with
82  $master = $entity->getIcon('master', $icon_type);
83 
84  // copy master to temp location
85  $tmp_file = new \ElggTempFile();
86  $tmp_file->open('write');
87  $tmp_file->write($master->grabFile());
88  $tmp_file->close();
89 
90  $file = [
91  'name' => basename($master->getFilenameOnFilestore()),
92  'type' => $master->getMimeType(),
93  'size'=> $master->getSize(),
94  'tmp_name'=> $tmp_file->getFilenameOnFilestore(),
95  'error'=> UPLOAD_ERR_OK,
96  ];
97 
98  // store the 'new' file in PHP global
99  $_FILES[$input_name] = $file;
100 
101  // store the 'new' file in Elgg request filebag
102  $uploaded_file = $this->arrayToUploadedFile($file);
103 
104  $filebag = _elgg_services()->request->files;
105  $filebag->set($input_name, $uploaded_file);
106  }
107 
115  protected function arrayToUploadedFile($file_data) {
116 
117  if (!is_array($file_data)) {
118  return false;
119  }
120 
121  $req_fields = ['error', 'name', 'size', 'tmp_name', 'type'];
122  $keys = array_keys($file_data);
123  sort($keys);
124 
125  if ($keys !== $req_fields) {
126  return false;
127  }
128 
129  return new UploadedFile(
130  $file_data['tmp_name'],
131  $file_data['name'],
132  $file_data['type'],
133  $file_data['error'],
134  true
135  );
136  }
137 }
$input_name
Definition: crop.php:24
arrayToUploadedFile($file_data)
convert $_FILES array to UploadedFile
Definition: CropIcon.php:115
__invoke(\Elgg\Hook $hook)
Set inputs required to support cropping an existing icon.
Definition: CropIcon.php:21
$keys
Definition: access.php:31
$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:179
$entity_guid
Action for adding and editing comments.
Definition: save.php:6
Models an event passed to hook handlers.
Definition: Hook.php:11
elgg_get_uploaded_file($input_name, $check_for_validity=true)
Returns a single valid uploaded file object.
Definition: filestore.php:161
$entity
Definition: reset.php:8
get_input($variable, $default=null, $filter_result=true)
Parameter input functions.
Definition: input.php:20
$icon_type
Definition: crop.php:23
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
_elgg_services()
Get the global service provider.
Definition: elgglib.php:638
Generic action listener to support cropping an existing icon.
Definition: CropIcon.php:12
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:69