1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* ownCloud - Activity App |
5
|
|
|
* |
6
|
|
|
* @author Joas Schilling |
7
|
|
|
* @copyright 2014 Joas Schilling [email protected] |
8
|
|
|
* |
9
|
|
|
* This library is free software; you can redistribute it and/or |
10
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE |
11
|
|
|
* License as published by the Free Software Foundation; either |
12
|
|
|
* version 3 of the License, or any later version. |
13
|
|
|
* |
14
|
|
|
* This library is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU Lesser General Public |
20
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace OCA\Activity\Parameter; |
25
|
|
|
|
26
|
|
|
use OCP\IL10N; |
27
|
|
|
use OCP\Util; |
28
|
|
|
|
29
|
|
|
class Collection implements IParameter { |
30
|
|
|
/** @var IL10N */ |
31
|
|
|
protected $l; |
32
|
|
|
|
33
|
|
|
/** @var Parameter[] */ |
34
|
|
|
protected $parameters; |
35
|
|
|
|
36
|
|
|
/** @var string */ |
37
|
|
|
protected $random; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param IL10N $l |
41
|
|
|
* @param string $random |
42
|
|
|
*/ |
43
|
22 |
|
public function __construct(IL10N $l, $random) { |
44
|
22 |
|
$this->l = $l; |
45
|
22 |
|
$this->random = $random; |
46
|
22 |
|
$this->parameters = []; |
47
|
22 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param IParameter $parameter |
51
|
|
|
*/ |
52
|
1 |
|
public function addParameter(IParameter $parameter) { |
53
|
1 |
|
foreach ($this->parameters as $existingParameter) { |
54
|
1 |
|
if ($existingParameter->getParameter() === $parameter->getParameter()) { |
55
|
1 |
|
return; |
56
|
|
|
} |
57
|
1 |
|
} |
58
|
1 |
|
$this->parameters[] = $parameter; |
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
3 |
|
public function getParameter() { |
65
|
3 |
|
return $this->random; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return array With two entries: value and type |
70
|
|
|
*/ |
71
|
2 |
|
public function getParameterInfo() { |
72
|
2 |
|
$parameters = []; |
73
|
2 |
|
foreach ($this->parameters as $parameter) { |
74
|
1 |
|
$parameters[] = $parameter->getParameterInfo(); |
75
|
2 |
|
} |
76
|
|
|
|
77
|
|
|
return [ |
78
|
2 |
|
'value' => $parameters, |
79
|
2 |
|
'type' => 'collection', |
80
|
2 |
|
]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param bool $allowHtml Should HTML be used to format the parameter? |
85
|
|
|
* @param bool $verbose Should paths, names, etc be shortened or full length |
86
|
|
|
* @return string The formatted parameter |
87
|
|
|
*/ |
88
|
5 |
|
public function format($allowHtml, $verbose = false) { |
89
|
5 |
|
$parameterList = $plainParameterList = []; |
90
|
|
|
|
91
|
5 |
|
foreach ($this->parameters as $parameter) { |
92
|
5 |
|
$parameterList[] = $parameter->format($allowHtml, $verbose); |
93
|
5 |
|
$plainParameterList[] = $parameter->format(false, false); |
94
|
5 |
|
} |
95
|
|
|
|
96
|
5 |
|
if ($allowHtml === null) { |
97
|
1 |
|
return '<collection>' . $this->joinParameterList($parameterList, $plainParameterList, $allowHtml) . '</collection>'; |
98
|
|
|
} |
99
|
4 |
|
return $this->joinParameterList($parameterList, $plainParameterList, $allowHtml); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Returns a list of grouped parameters |
104
|
|
|
* |
105
|
|
|
* 2 parameters are joined by "and": |
106
|
|
|
* => A and B |
107
|
|
|
* Up to 5 parameters are joined by "," and "and": |
108
|
|
|
* => A, B, C, D and E |
109
|
|
|
* More than 5 parameters are joined by "," and trimmed: |
110
|
|
|
* => A, B, C and #n more |
111
|
|
|
* |
112
|
|
|
* @param array $parameterList |
113
|
|
|
* @param array $plainParameterList |
114
|
|
|
* @param bool $allowHtml |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
11 |
|
protected function joinParameterList($parameterList, $plainParameterList, $allowHtml) { |
118
|
11 |
|
if (empty($parameterList)) { |
119
|
2 |
|
return ''; |
120
|
|
|
} |
121
|
9 |
|
if ($allowHtml === null) { |
122
|
1 |
|
return implode('', $parameterList); |
123
|
|
|
} |
124
|
|
|
|
125
|
8 |
|
$count = sizeof($parameterList); |
126
|
8 |
|
$lastItem = array_pop($parameterList); |
127
|
|
|
|
128
|
8 |
|
if ($count === 1) { |
129
|
2 |
|
return $lastItem; |
130
|
6 |
|
} else if ($count === 2) { |
131
|
2 |
|
$firstItem = array_pop($parameterList); |
132
|
2 |
|
return (string) $this->l->t('%s and %s', array($firstItem, $lastItem)); |
133
|
4 |
|
} else if ($count <= 5) { |
134
|
2 |
|
$list = implode($this->l->t(', '), $parameterList); |
135
|
2 |
|
return (string) $this->l->t('%s and %s', array($list, $lastItem)); |
136
|
|
|
} |
137
|
|
|
|
138
|
2 |
|
$firstParams = array_slice($parameterList, 0, 3); |
139
|
2 |
|
$firstList = implode($this->l->t(', '), $firstParams); |
140
|
2 |
|
$trimmedParams = array_slice($plainParameterList, 3); |
141
|
2 |
|
$trimmedList = implode($this->l->t(', '), $trimmedParams); |
142
|
|
|
|
143
|
2 |
|
if ($allowHtml) { |
144
|
1 |
|
return (string) $this->l->n( |
145
|
1 |
|
'%s and <strong %s>%n more</strong>', |
146
|
1 |
|
'%s and <strong %s>%n more</strong>', |
147
|
1 |
|
$count - 3, |
148
|
1 |
|
array($firstList, 'class="has-tooltip" title="' . Util::sanitizeHTML($trimmedList) . '"')); |
149
|
|
|
} |
150
|
1 |
|
return (string) $this->l->n('%s and %n more', '%s and %n more', $count - 3, array($firstList)); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|