1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HDNET\Focuspoint\Service\WizardHandler; |
4
|
|
|
|
5
|
|
|
use HDNET\Focuspoint\Domain\Repository\SysFileMetadataRepository; |
6
|
|
|
use HDNET\Focuspoint\Domain\Repository\SysFileReferenceRepository; |
7
|
|
|
use TYPO3\CMS\Core\Resource\ResourceFactory; |
8
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
9
|
|
|
use TYPO3\CMS\Core\Utility\MathUtility; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* FileReference. |
13
|
|
|
*/ |
14
|
|
|
class FileReference extends AbstractWizardHandler |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Check if the handler can handle the current request. |
18
|
|
|
* |
19
|
|
|
* @return bool |
20
|
|
|
*/ |
21
|
|
|
public function canHandle() |
22
|
|
|
{ |
23
|
|
|
return null !== $this->getReferenceUid(); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* get the arguments for same request call. |
28
|
|
|
* |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
|
|
public function getArguments() |
32
|
|
|
{ |
33
|
|
|
return [ |
34
|
|
|
'P' => [ |
35
|
|
|
'referenceUid' => $this->getReferenceUid(), |
36
|
|
|
], |
37
|
|
|
]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Return the current point. |
42
|
|
|
* |
43
|
|
|
* @return int[] |
44
|
|
|
*/ |
45
|
|
|
public function getCurrentPoint() |
46
|
|
|
{ |
47
|
|
|
$reference = ResourceFactory::getInstance()->getFileReferenceObject($this->getReferenceUid()); |
48
|
|
|
$properties = $reference->getProperties(); |
49
|
|
|
|
50
|
|
|
return $this->cleanupPosition([ |
51
|
|
|
$properties['focus_point_x'], |
52
|
|
|
$properties['focus_point_y'], |
53
|
|
|
]); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the public URL for the current handler. |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function getPublicUrl() |
62
|
|
|
{ |
63
|
|
|
$reference = ResourceFactory::getInstance()->getFileReferenceObject($this->getReferenceUid()); |
64
|
|
|
|
65
|
|
|
return $this->displayableImageUrl($reference->getPublicUrl()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set the point (between -100 and 100). |
70
|
|
|
* |
71
|
|
|
* @param int $x |
72
|
|
|
* @param int $y |
73
|
|
|
*/ |
74
|
|
|
public function setCurrentPoint($x, $y) |
75
|
|
|
{ |
76
|
|
|
$values = [ |
77
|
|
|
'focus_point_x' => MathUtility::forceIntegerInRange($x, -100, 100, 0), |
78
|
|
|
'focus_point_y' => MathUtility::forceIntegerInRange($y, -100, 100, 0), |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
GeneralUtility::makeInstance(SysFileReferenceRepository::class)->update((int) $this->getReferenceUid(), $values); |
82
|
|
|
|
83
|
|
|
// save also to the file |
84
|
|
|
$reference = ResourceFactory::getInstance()->getFileReferenceObject($this->getReferenceUid()); |
85
|
|
|
$fileUid = $reference->getOriginalFile()->getUid(); |
86
|
|
|
|
87
|
|
|
$sysFileMatadataRepository = GeneralUtility::makeInstance(SysFileMetadataRepository::class); |
88
|
|
|
$row = $sysFileMatadataRepository->findOneByFileUid((int) $fileUid); |
89
|
|
|
if ($row) { |
90
|
|
|
$sysFileMatadataRepository->update((int) $row['uid'], $values); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Fetch the meta data UID. |
96
|
|
|
* |
97
|
|
|
* @return int|null |
98
|
|
|
*/ |
99
|
|
|
protected function getReferenceUid() |
100
|
|
|
{ |
101
|
|
|
$parameter = GeneralUtility::_GET(); |
102
|
|
|
if (!isset($parameter['P'])) { |
103
|
|
|
return; |
104
|
|
|
} |
105
|
|
|
$p = $parameter['P']; |
106
|
|
|
if (isset($p['referenceUid']) && MathUtility::canBeInterpretedAsInteger($p['referenceUid'])) { |
107
|
|
|
return (int) $p['referenceUid']; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.