Total Complexity | 61 |
Total Lines | 633 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like ImageHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ImageHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | class ImageHandler extends XoopsPersistableObjectHandler |
||
50 | { |
||
51 | public $helper; |
||
52 | public $isAdmin; |
||
53 | |||
54 | /** |
||
55 | * Constructor |
||
56 | * @param \XoopsDatabase|null $xoopsDatabase |
||
57 | * @param \XoopsModules\Suico\Helper|null $helper |
||
58 | */ |
||
59 | public function __construct( |
||
60 | ?XoopsDatabase $xoopsDatabase = null, |
||
61 | $helper = null |
||
62 | ) { |
||
63 | /** @var \XoopsModules\Suico\Helper $this->helper */ |
||
64 | if (null === $helper) { |
||
65 | $this->helper = Helper::getInstance(); |
||
66 | } else { |
||
67 | $this->helper = $helper; |
||
68 | } |
||
69 | $isAdmin = $this->helper->isUserAdmin(); |
||
|
|||
70 | parent::__construct($xoopsDatabase, 'suico_images', Image::class, 'image_id', 'title', 'caption'); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * create a new Groups |
||
75 | * |
||
76 | * @param bool $isNew flag the new objects as "new"? |
||
77 | * @return \XoopsObject Groups |
||
78 | */ |
||
79 | public function create( |
||
80 | $isNew = true |
||
81 | ) { |
||
82 | $obj = parent::create($isNew); |
||
83 | // if ($isNew) { |
||
84 | // $obj->setDefaultPermissions(); |
||
85 | // } |
||
86 | if ($isNew) { |
||
87 | $obj->setNew(); |
||
88 | } else { |
||
89 | $obj->unsetNew(); |
||
90 | } |
||
91 | $obj->helper = $this->helper; |
||
92 | return $obj; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * retrieve a Image |
||
97 | * |
||
98 | * @param int|null $id of the Image |
||
99 | * @param null $fields |
||
100 | * @return mixed reference to the {@link Image} object, FALSE if failed |
||
101 | */ |
||
102 | public function get2( |
||
103 | $id = null, |
||
104 | $fields = null |
||
105 | ) { |
||
106 | $sql = 'SELECT * FROM ' . $this->db->prefix('suico_images') . ' WHERE image_id=' . $id; |
||
107 | if (!$result = $this->db->query($sql)) { |
||
108 | return false; |
||
109 | } |
||
110 | $numrows = $this->db->getRowsNum($result); |
||
111 | if (1 === $numrows) { |
||
112 | $image = new Image(); |
||
113 | $image->assignVars($this->db->fetchArray($result)); |
||
114 | return $image; |
||
115 | } |
||
116 | return false; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * insert a new Image in the database |
||
121 | * |
||
122 | * @param \XoopsObject $xoopsObject reference to the {@link Image} object |
||
123 | * @param bool $force |
||
124 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
125 | */ |
||
126 | public function insert2( |
||
127 | XoopsObject $xoopsObject, |
||
128 | $force = false |
||
129 | ) { |
||
130 | global $xoopsConfig; |
||
131 | if (!$xoopsObject instanceof Image) { |
||
132 | return false; |
||
133 | } |
||
134 | if (!$xoopsObject->isDirty()) { |
||
135 | return true; |
||
136 | } |
||
137 | if (!$xoopsObject->cleanVars()) { |
||
138 | return false; |
||
139 | } |
||
140 | $image_id = ''; |
||
141 | foreach ($xoopsObject->cleanVars as $k => $v) { |
||
142 | ${$k} = $v; |
||
143 | } |
||
144 | // $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)'; |
||
145 | if ($xoopsObject->isNew()) { |
||
146 | // ajout/modification d'un Image |
||
147 | $xoopsObject = new Image(); |
||
148 | $format = 'INSERT INTO %s (image_id, title, caption, date_created, date_updated, uid_owner, filename, private)'; |
||
149 | $format .= 'VALUES (%u, %s, %s, %s, %s, %s, %s, 0)'; |
||
150 | $sql = \sprintf( |
||
151 | $format, |
||
152 | $this->db->prefix('suico_images'), |
||
153 | $image_id, |
||
154 | $this->db->quoteString($title), |
||
155 | $this->db->quoteString($caption), |
||
156 | \time(),//$now, |
||
157 | \time(),//$now, |
||
158 | $this->db->quoteString($uid_owner), |
||
159 | $this->db->quoteString($filename) |
||
160 | ); |
||
161 | $force = true; |
||
162 | } else { |
||
163 | $format = 'UPDATE %s SET '; |
||
164 | $format .= 'image_id=%u, title=%s, caption=%s, date_created=%s, date_updated=%s, uid_owner=%s, filename=%s, private=%s'; |
||
165 | $format .= ' WHERE image_id = %u'; |
||
166 | $sql = \sprintf( |
||
167 | $format, |
||
168 | $this->db->prefix('suico_images'), |
||
169 | $image_id, |
||
170 | $this->db->quoteString($title), |
||
171 | $this->db->quoteString($caption), |
||
172 | $xoopsObject->getVar('date_created'), // $now, |
||
173 | $xoopsObject->getVar('date_updated'), // $now, |
||
174 | $this->db->quoteString($uid_owner), |
||
175 | $this->db->quoteString($filename), |
||
176 | $this->db->quoteString($private), |
||
177 | $image_id |
||
178 | ); |
||
179 | } |
||
180 | if ($force) { |
||
181 | $result = $this->db->queryF($sql); |
||
182 | } else { |
||
183 | $result = $this->db->query($sql); |
||
184 | } |
||
185 | if (!$result) { |
||
186 | return false; |
||
187 | } |
||
188 | if (empty($image_id)) { |
||
189 | $image_id = $this->db->getInsertId(); |
||
190 | } |
||
191 | $xoopsObject->assignVar('image_id', $image_id); |
||
192 | return true; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * delete a Image from the database |
||
197 | * |
||
198 | * @param \XoopsObject $xoopsObject reference to the Image to delete |
||
199 | * @param bool $force |
||
200 | * @return bool FALSE if failed. |
||
201 | */ |
||
202 | public function delete( |
||
203 | XoopsObject $xoopsObject, |
||
204 | $force = false |
||
205 | ) { |
||
206 | if (!$xoopsObject instanceof Image) { |
||
207 | return false; |
||
208 | } |
||
209 | $sql = \sprintf( |
||
210 | 'DELETE FROM %s WHERE image_id = %u', |
||
211 | $this->db->prefix('suico_images'), |
||
212 | $xoopsObject->getVar('image_id') |
||
213 | ); |
||
214 | if ($force) { |
||
215 | $result = $this->db->queryF($sql); |
||
216 | } else { |
||
217 | $result = $this->db->query($sql); |
||
218 | } |
||
219 | if (!$result) { |
||
220 | return false; |
||
221 | } |
||
222 | return true; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * retrieve suico_imagess from the database |
||
227 | * |
||
228 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} conditions to be met |
||
229 | * @param bool $id_as_key use the UID as key for the array? |
||
230 | * @param bool $as_object |
||
231 | * @return array array of {@link Image} objects |
||
232 | */ |
||
233 | public function &getObjects( |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * count suico_imagess matching a condition |
||
268 | * |
||
269 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} to match |
||
270 | * @return int count of suico_imagess |
||
271 | */ |
||
272 | public function getCount( |
||
273 | ?CriteriaElement $criteriaElement = null |
||
274 | ) { |
||
275 | $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('suico_images'); |
||
276 | if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) { |
||
277 | $sql .= ' ' . $criteriaElement->renderWhere(); |
||
278 | } |
||
279 | $result = $this->db->query($sql); |
||
280 | if (!$result) { |
||
281 | return 0; |
||
282 | } |
||
283 | [$count] = $this->db->fetchRow($result); |
||
284 | return (int)$count; |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * delete suico_imagess matching a set of conditions |
||
289 | * |
||
290 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} |
||
291 | * @param bool $force |
||
292 | * @param bool $asObject |
||
293 | * @return bool FALSE if deletion failed |
||
294 | */ |
||
295 | public function deleteAll( |
||
296 | ?CriteriaElement $criteriaElement = null, |
||
297 | $force = true, |
||
298 | $asObject = false |
||
299 | ) { |
||
300 | $sql = 'DELETE FROM ' . $this->db->prefix('suico_images'); |
||
301 | if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) { |
||
302 | $sql .= ' ' . $criteriaElement->renderWhere(); |
||
303 | } |
||
304 | if (!$result = $this->db->query($sql)) { |
||
305 | return false; |
||
306 | } |
||
307 | return true; |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Render a form to send pictures |
||
312 | * |
||
313 | * @param int $maxbytes the maximum size of a picture |
||
314 | * @param \XoopsTpl $xoopsTpl the one in which the form will be rendered |
||
315 | * @return bool TRUE |
||
316 | * |
||
317 | * obs: Some functions wont work on php 4 so edit lines down under acording to your version |
||
318 | */ |
||
319 | public function renderFormSubmit( |
||
320 | $maxbytes, |
||
321 | $xoopsTpl |
||
322 | ) { |
||
323 | $form = new XoopsThemeForm(\_MD_SUICO_SUBMIT_PIC_TITLE, 'form_picture', 'submitImage.php', 'post', true); |
||
324 | $field_url = new XoopsFormFile(\_MD_SUICO_SELECT_PHOTO, 'sel_photo', 2000000); |
||
325 | $field_title = new XoopsFormText(\_MD_SUICO_PHOTOTITLE, 'title', 35, 55); |
||
326 | $field_caption = new XoopsFormText(\_MD_SUICO_CAPTION, 'caption', 35, 55); |
||
327 | $form->setExtra('enctype="multipart/form-data"'); |
||
328 | $buttonSend = new XoopsFormButton('', 'submit_button', \_MD_SUICO_UPLOADPICTURE, 'submit'); |
||
329 | $field_warning = new XoopsFormLabel(\sprintf(\_MD_SUICO_YOU_CAN_UPLOAD, $maxbytes / 1024)); |
||
330 | $form->addElement($field_warning); |
||
331 | $form->addElement($field_url, true); |
||
332 | $form->addElement($field_title); |
||
333 | $form->addElement($field_caption); |
||
334 | $form->addElement($buttonSend); |
||
335 | $form->assign($xoopsTpl); //If your server is php 5 |
||
336 | return true; |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * Render a form to edit the description of the pictures |
||
341 | * |
||
342 | * @param $title |
||
343 | * @param string $caption The description of the picture |
||
344 | * @param int $image_id the id of the image in database |
||
345 | * @param string $filename the url to the thumb of the image so it can be displayed |
||
346 | * @return bool TRUE |
||
347 | */ |
||
348 | public function renderFormEdit( |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * Upload the file and Save into database |
||
374 | * |
||
375 | * @param string $title A litle title of the file |
||
376 | * @param string $caption A litle description of the file |
||
377 | * @param string $pathUpload The path to where the file should be uploaded |
||
378 | * @param int $thumbwidth the width in pixels that the thumbnail will have |
||
379 | * @param int $thumbheight the height in pixels that the thumbnail will have |
||
380 | * @param int $pictwidth the width in pixels that the pic will have |
||
381 | * @param int $pictheight the height in pixels that the pic will have |
||
382 | * @param int $maxfilebytes the maximum size a file can have to be uploaded in bytes |
||
383 | * @param int $maxfilewidth the maximum width in pixels that a pic can have |
||
384 | * @param int $maxfileheight the maximum height in pixels that a pic can have |
||
385 | * @return bool FALSE if upload fails or database fails |
||
386 | */ |
||
387 | public function receivePicture( |
||
388 | $title, |
||
389 | $caption, |
||
390 | $pathUpload, |
||
391 | $thumbwidth, |
||
392 | $thumbheight, |
||
393 | $pictwidth, |
||
394 | $pictheight, |
||
395 | $maxfilebytes, |
||
396 | $maxfilewidth, |
||
397 | $maxfileheight |
||
398 | ) { |
||
399 | global $xoopsUser, $xoopsDB; |
||
400 | //search logged user id |
||
401 | $uid = $xoopsUser->getVar('uid'); |
||
402 | //create a hash so it does not erase another file |
||
403 | //$hash1 = date(); |
||
404 | //$hash = substr($hash1,0,4); |
||
405 | // mimetypes and settings put this in admin part later |
||
406 | $allowed_mimetypes = Helper::getInstance()->getConfig( |
||
407 | 'mimetypes' |
||
408 | ); |
||
409 | $maxfilesize = $maxfilebytes; |
||
410 | // $uploadDir = \XOOPS_UPLOAD_PATH . '/suico/images/'; |
||
411 | // create the object to upload |
||
412 | $uploader = new XoopsMediaUploader( |
||
413 | $pathUpload, $allowed_mimetypes, $maxfilesize, $maxfilewidth, $maxfileheight |
||
414 | ); |
||
415 | // fetch the media |
||
416 | if ($uploader->fetchMedia(Request::getArray('xoops_upload_file', '', 'POST')[0])) { |
||
417 | //lets create a name for it |
||
418 | $uploader->setPrefix('pic_' . $uid . '_'); |
||
419 | //now let s upload the file |
||
420 | if (!$uploader->upload()) { |
||
421 | // if there are errors lets return them |
||
422 | echo '<div style="color:#FF0000; background-color:#FFEAF4; border-color:#FF0000; border-width:thick; border-style:solid; text-align:center"><p>' . $uploader->getErrors() . '</p></div>'; |
||
423 | return false; |
||
424 | } |
||
425 | // now let s create a new object picture and set its variables |
||
426 | $picture = $this->create(); |
||
427 | $filename = $uploader->getSavedFileName(); |
||
428 | $picture->setVar('filename', $filename); |
||
429 | $picture->setVar('title', $title); |
||
430 | $picture->setVar('caption', $caption); |
||
431 | $picture->setVar('date_created', \time()); |
||
432 | $picture->setVar('date_updated', \time()); |
||
433 | $picture->setVar('private', 0); |
||
434 | $uid = $xoopsUser->getVar('uid'); |
||
435 | $picture->setVar('uid_owner', $uid); |
||
436 | $this->insert($picture); |
||
437 | $saved_destination = $uploader->getSavedDestination(); |
||
438 | //print_r($_FILES); |
||
439 | //$this->resizeImage($saved_destination,false, $thumbwidth, $thumbheight, $pictwidth, $pictheight,$pathUpload); |
||
440 | //$this->resizeImage($saved_destination,true, $thumbwidth, $thumbheight, $pictwidth, $pictheight,$pathUpload); |
||
441 | $this->resizeImage( |
||
442 | $saved_destination, |
||
443 | $thumbwidth, |
||
444 | $thumbheight, |
||
445 | $pictwidth, |
||
446 | $pictheight, |
||
447 | $pathUpload |
||
448 | ); |
||
449 | } else { |
||
450 | echo '<div style="color:#FF0000; background-color:#FFEAF4; border-color:#FF0000; border-width:thick; border-style:solid; text-align:center"><p>' . $uploader->getErrors() . '</p></div>'; |
||
451 | return false; |
||
452 | } |
||
453 | return true; |
||
454 | } |
||
455 | |||
456 | /** |
||
457 | * Resize a picture and save it to $pathUpload |
||
458 | * |
||
459 | * @param string $img the path to the file |
||
460 | * @param int $thumbwidth the width in pixels that the thumbnail will have |
||
461 | * @param int $thumbheight the height in pixels that the thumbnail will have |
||
462 | * @param int $pictwidth the width in pixels that the pic will have |
||
463 | * @param int $pictheight the height in pixels that the pic will have |
||
464 | * @param string $pathUpload The path to where the files should be saved after resizing |
||
465 | */ |
||
466 | public function resizeImage( |
||
531 | } |
||
532 | |||
533 | /** |
||
534 | * @param $limit |
||
535 | * @return array |
||
536 | */ |
||
537 | public function getLastPictures($limit) |
||
556 | } |
||
557 | |||
558 | /** |
||
559 | * @param $limit |
||
560 | * @return array |
||
561 | */ |
||
562 | public function getLastPicturesForBlock($limit) |
||
609 | } |
||
610 | |||
611 | /** |
||
612 | * Resize a picture and save it to $pathUpload |
||
613 | * |
||
614 | * @param string $img the path to the file |
||
615 | * @param $width |
||
616 | * @param $height |
||
617 | * @param string $pathUpload The path to where the files should be saved after resizing |
||
618 | */ |
||
619 | public function makeAvatar( |
||
682 | } |
||
683 | } |
||
684 | |||
685 |