1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
<?php
/**
* @author Lukas Reschke
* @author Thomas Müller
* @copyright 2014 Lukas Reschke lukas@owncloud.com, 2014 Thomas Müller deepdiver@owncloud.com
*
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace OC\Settings\Controller;
use OC\App\DependencyAnalyzer;
use OC\App\Platform;
use OC\OCSClient;
use \OCP\AppFramework\Controller;
use OCP\ICacheFactory;
use OCP\IRequest;
use OCP\IL10N;
use OCP\IConfig;
/**
* @package OC\Settings\Controller
*/
class AppSettingsController extends Controller {
/** @var \OCP\IL10N */
private $l10n;
/** @var IConfig */
private $config;
/** @var \OCP\ICache */
private $cache;
/**
* @param string $appName
* @param IRequest $request
* @param IL10N $l10n
* @param IConfig $config
* @param ICacheFactory $cache
*/
public function __construct($appName,
IRequest $request,
IL10N $l10n,
IConfig $config,
ICacheFactory $cache) {
parent::__construct($appName, $request);
$this->l10n = $l10n;
$this->config = $config;
$this->cache = $cache->create($appName);
}
/**
* Get all available categories
* @return array
*/
public function listCategories() {
if(!is_null($this->cache->get('listCategories'))) {
return $this->cache->get('listCategories');
}
$categories = [
['id' => 0, 'displayName' => (string)$this->l10n->t('Enabled')],
['id' => 1, 'displayName' => (string)$this->l10n->t('Not enabled')],
];
if(OCSClient::isAppStoreEnabled()) {
$categories[] = ['id' => 2, 'displayName' => (string)$this->l10n->t('Recommended')];
// apps from external repo via OCS
$ocs = OCSClient::getCategories();
if ($ocs) {
foreach($ocs as $k => $v) {
$categories[] = array(
'id' => $k,
'displayName' => str_replace('ownCloud ', '', $v)
);
}
}
}
$this->cache->set('listCategories', $categories, 3600);
return $categories;
}
/**
* Get all available categories
* @param int $category
* @return array
*/
public function listApps($category = 0) {
if(!is_null($this->cache->get('listApps-'.$category))) {
$apps = $this->cache->get('listApps-'.$category);
} else {
switch ($category) {
// installed apps
case 0:
$apps = $this->getInstalledApps();
usort($apps, function ($a, $b) {
$a = (string)$a['name'];
$b = (string)$b['name'];
if ($a === $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
});
break;
// not-installed apps
case 1:
$apps = \OC_App::listAllApps(true);
$apps = array_filter($apps, function ($app) {
return !$app['active'];
});
usort($apps, function ($a, $b) {
$a = (string)$a['name'];
$b = (string)$b['name'];
if ($a === $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
});
break;
default:
if ($category === 2) {
$apps = \OC_App::getAppstoreApps('approved');
if ($apps) {
$apps = array_filter($apps, function ($app) {
return isset($app['internalclass']) && $app['internalclass'] === 'recommendedapp';
});
}
} else {
$apps = \OC_App::getAppstoreApps('approved', $category);
}
if (!$apps) {
$apps = array();
} else {
// don't list installed apps
$installedApps = $this->getInstalledApps();
$installedApps = array_map(function ($app) {
if (isset($app['ocsid'])) {
return $app['ocsid'];
}
return $app['id'];
}, $installedApps);
$apps = array_filter($apps, function ($app) use ($installedApps) {
return !in_array($app['id'], $installedApps);
});
}
// sort by score
usort($apps, function ($a, $b) {
$a = (int)$a['score'];
$b = (int)$b['score'];
if ($a === $b) {
return 0;
}
return ($a > $b) ? -1 : 1;
});
break;
}
}
// fix groups to be an array
$dependencyAnalyzer = new DependencyAnalyzer(new Platform($this->config), $this->l10n);
$apps = array_map(function($app) use ($dependencyAnalyzer) {
// fix groups
$groups = array();
if (is_string($app['groups'])) {
$groups = json_decode($app['groups']);
}
$app['groups'] = $groups;
$app['canUnInstall'] = !$app['active'] && $app['removable'];
// fix licence vs license
if (isset($app['license']) && !isset($app['licence'])) {
$app['licence'] = $app['license'];
}
// analyse dependencies
$missing = $dependencyAnalyzer->analyze($app);
$app['canInstall'] = empty($missing);
$app['missingDependencies'] = $missing;
return $app;
}, $apps);
$this->cache->set('listApps-'.$category, $apps, 300);
return ['apps' => $apps, 'status' => 'success'];
}
/**
* @return array
*/
private function getInstalledApps() {
$apps = \OC_App::listAllApps(true);
$apps = array_filter($apps, function ($app) {
return $app['active'];
});
return $apps;
}
}
|