|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
|
7
|
|
|
* @license MIT |
|
8
|
|
|
* @author [email protected] |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Jobs\View\Helper; |
|
12
|
|
|
|
|
13
|
|
|
use Zend\View\Helper\AbstractHelper; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* View helper to display apply buttons |
|
17
|
|
|
*/ |
|
18
|
|
|
class ApplyButtons extends AbstractHelper |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $partial = 'partials/buttons'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Renders apply buttons according to passed $options |
|
28
|
|
|
* Following optional options are recognized: |
|
29
|
|
|
* partial: custom partial script for rendering the buttons (relative to current template script), default: 'partials/buttons' |
|
30
|
|
|
* oneClickOnly: display only one click apply buttons, default: false |
|
31
|
|
|
* defaultLabel: label of default apply button, default: 'Apply now' |
|
32
|
|
|
* oneClickLabel: label of one click apply buttons, default: 'Apply with %s' |
|
33
|
|
|
* sendImmediately: flag whether to sent application immediately, default: false |
|
34
|
|
|
* |
|
35
|
|
|
* Usage example with defaults: |
|
36
|
|
|
* <code> |
|
37
|
|
|
* <?=$this->jobApplyButtons($this->applyButtons)?> |
|
38
|
|
|
* </code> |
|
39
|
|
|
* |
|
40
|
|
|
* Usage example with options set explicitly: |
|
41
|
|
|
* <code> |
|
42
|
|
|
* <?=$this->jobApplyButtons($this->applyButtons , [ |
|
43
|
|
|
* 'partial' => 'partials/mybuttons', |
|
44
|
|
|
* 'oneClickOnly' => true, |
|
45
|
|
|
* 'defaultLabel' => $this->translate('Send application'), |
|
46
|
|
|
* 'oneClickLabel' => $this->translate('Send application with my %s social profile'), |
|
47
|
|
|
* 'sendImmediately' => true, |
|
48
|
|
|
* ])?> |
|
49
|
|
|
* </code> |
|
50
|
|
|
* @param array $data |
|
51
|
|
|
* @param array $options |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __invoke(array $data, array $options = []) |
|
55
|
|
|
{ |
|
56
|
|
|
// check data |
|
57
|
|
|
if (!isset($data['uri']) || !isset($data['oneClickProfiles']) || !isset($data['applyId'])) |
|
58
|
|
|
{ |
|
59
|
|
|
throw new \InvalidArgumentException('Invalid data passed'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$variables = [ |
|
63
|
|
|
'default' => null, |
|
64
|
|
|
'oneClick' => [], |
|
65
|
|
|
]; |
|
66
|
|
|
$options = array_merge([ |
|
67
|
|
|
'partial' => $this->partial, |
|
68
|
|
|
'oneClickOnly' => false, |
|
69
|
|
|
'defaultLabel' => null, |
|
70
|
|
|
'oneClickLabel' => null, |
|
71
|
|
|
'sendImmediately' => false |
|
72
|
|
|
], $options); |
|
73
|
|
|
$view = $this->view; |
|
74
|
|
|
$currentTemplate = $view->viewModel() |
|
|
|
|
|
|
75
|
|
|
->getCurrent() |
|
76
|
|
|
->getTemplate(); |
|
77
|
|
|
$partial = dirname($currentTemplate) . '/' . $options['partial']; |
|
78
|
|
|
|
|
79
|
|
|
if (!$options['oneClickOnly'] && $data['uri']) { |
|
80
|
|
|
$variables['default'] = [ |
|
81
|
|
|
'label' => $options['defaultLabel'] ?: $view->translate('Apply now'), |
|
|
|
|
|
|
82
|
|
|
'url' => $data['uri'] |
|
83
|
|
|
]; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if ($data['oneClickProfiles']) { |
|
87
|
|
|
$label = $options['oneClickLabel'] ?: $view->translate('Apply with %s'); |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
foreach ($data['oneClickProfiles'] as $network) { |
|
90
|
|
|
$variables['oneClick'][] = [ |
|
91
|
|
|
'label' => sprintf($label, $network), |
|
92
|
|
|
'url' => $view->url('lang/apply-one-click', ['applyId' => $data['applyId'], 'network' => $network, 'immediately' => $options['sendImmediately'] ?: null], ['force_canonical' => true]), |
|
|
|
|
|
|
93
|
|
|
'network' => $network |
|
94
|
|
|
]; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $view->partial($partial, $variables); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getPartial() |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->partial; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param string $partial |
|
111
|
|
|
* @return ApplyButtons |
|
112
|
|
|
*/ |
|
113
|
|
|
public function setPartial($partial) |
|
114
|
|
|
{ |
|
115
|
|
|
$this->partial = $partial; |
|
116
|
|
|
|
|
117
|
|
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: