1 | <?php |
||
5 | class LinkToFunction |
||
6 | { |
||
7 | /** |
||
8 | * Get a link literal to a Model |
||
9 | * |
||
10 | * @param $context |
||
11 | * @param \Model $model The model we want to link to |
||
12 | * @param string $icon A font awesome icon identifier to show instead of text |
||
13 | * @param string $action The action to link to (e.g show or edit) |
||
14 | * @param bool $linkAll Whether to link to inactive or deleted models |
||
15 | * @param string $class The CSS class(es) to apply to the link |
||
16 | * @param bool $forceText Whether to show both the icon and text |
||
17 | * @param string $content Override the content that will automatically be used |
||
18 | * |
||
19 | * @return string The HTML link |
||
20 | */ |
||
21 | 1 | public function __invoke( |
|
22 | $context, |
||
23 | $model, |
||
24 | $icon = null, |
||
25 | $action = 'show', |
||
26 | $linkAll = false, |
||
27 | $class = '', |
||
28 | $forceText = false, |
||
29 | $content = '' |
||
30 | ) { |
||
31 | 1 | if ($content === '' || $content === null) { |
|
32 | 1 | $content = $this->getContent($model, $icon, $forceText); |
|
33 | 1 | } elseif ($icon) { |
|
|
|||
34 | $content = "<i class=\"fa fa-$icon\"></i> " . $content; |
||
35 | } |
||
36 | |||
37 | 1 | if ($this->isLinkable($model, $linkAll, $context)) { |
|
38 | 1 | $params = array(); |
|
39 | 1 | if ($linkAll) { |
|
40 | $params['showDeleted'] = true; |
||
41 | } |
||
42 | |||
43 | 1 | $url = $model->getURL($action, false, $params); |
|
44 | |||
45 | 1 | return '<a' . $this->getClass($class) . ' href="' . $url . '">' . $content . '</a>'; |
|
46 | } |
||
47 | |||
48 | 1 | return '<span' . $this->getClass("$class disabled-link") . '>' . $content . '</span>'; |
|
49 | } |
||
50 | |||
51 | /** |
||
52 | * Get the content of the link to show |
||
53 | * |
||
54 | * @param \Model $model The model we want to link to |
||
55 | * @param string $icon A font awesome icon identifier to show instead of text |
||
56 | * @param bool $forceText Whether to show both the icon and text |
||
57 | * @return string The link's content |
||
58 | */ |
||
59 | 1 | private function getContent($model, $icon, $forceText) |
|
77 | |||
78 | /** |
||
79 | * Get the name of any model |
||
80 | * |
||
81 | * @param \Model $model |
||
82 | * @return string The name of the model |
||
83 | */ |
||
84 | 1 | private function getModelName($model) |
|
95 | |||
96 | /** |
||
97 | * Create a CSS class string |
||
98 | * |
||
99 | * @param $class string The CSS class(es), without `class=".."` |
||
100 | * @return string |
||
101 | */ |
||
102 | 1 | private function getClass($class) |
|
110 | |||
111 | /** |
||
112 | * Find out if a link should be provided to an object, instead of just a |
||
113 | * reference to its name |
||
114 | * |
||
115 | * @param \Model $model The model to test |
||
116 | * @param bool $linkAll Whether to link deleted and inactive models |
||
117 | * @param array $context Twig's context |
||
118 | * @return bool |
||
119 | */ |
||
120 | 1 | private function isLinkable($model, $linkAll, &$context) |
|
121 | { |
||
122 | // Models that don't have a URL can't be linked |
||
123 | 1 | if (!$model instanceof \UrlModel) { |
|
124 | 1 | return false; |
|
125 | } |
||
126 | |||
127 | 1 | if ($linkAll) { |
|
128 | return true; |
||
129 | } |
||
130 | |||
131 | 1 | if (!$context['app']) { |
|
132 | // Only link active models by default |
||
133 | return $model->isActive(); |
||
134 | } |
||
135 | |||
136 | 1 | return $context['app']->getController()->canSee($model); |
|
137 | } |
||
138 | |||
139 | 1 | public static function get() |
|
146 | } |
||
147 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: