Completed
Push — master ( 68735c...ea470f )
by Björn
11:25
created

TemplateLayout::generateCssAsset()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 33
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 24
nc 2
nop 1
dl 0
loc 33
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Adam Williamson <[email protected]>
4
 * @author Bart Visscher <[email protected]>
5
 * @author Christopher Schäpers <[email protected]>
6
 * @author Clark Tomlinson <[email protected]>
7
 * @author Hendrik Leppelsack <[email protected]>
8
 * @author Joas Schilling <[email protected]>
9
 * @author Jörn Friedrich Dreyer <[email protected]>
10
 * @author Lukas Reschke <[email protected]>
11
 * @author Michael Gapczynski <[email protected]>
12
 * @author Morris Jobke <[email protected]>
13
 * @author Remco Brenninkmeijer <[email protected]>
14
 * @author Robin Appelman <[email protected]>
15
 * @author Robin McCorkell <[email protected]>
16
 * @author Roeland Jago Douma <[email protected]>
17
 * @author Thomas Müller <[email protected]>
18
 * @author Victor Dubiniuk <[email protected]>
19
 *
20
 * @copyright Copyright (c) 2016, ownCloud, Inc.
21
 * @license AGPL-3.0
22
 *
23
 * This code is free software: you can redistribute it and/or modify
24
 * it under the terms of the GNU Affero General Public License, version 3,
25
 * as published by the Free Software Foundation.
26
 *
27
 * This program is distributed in the hope that it will be useful,
28
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30
 * GNU Affero General Public License for more details.
31
 *
32
 * You should have received a copy of the GNU Affero General Public License, version 3,
33
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
34
 *
35
 */
36
namespace OC;
37
38
use Assetic\Asset\AssetCollection;
39
use Assetic\Asset\FileAsset;
40
use Assetic\AssetWriter;
41
use Assetic\Filter\CssImportFilter;
42
use Assetic\Filter\CssMinFilter;
43
use Assetic\Filter\CssRewriteFilter;
44
use Assetic\Filter\JSqueezeFilter;
45
use Assetic\Filter\SeparatorFilter;
46
47
class TemplateLayout extends \OC_Template {
48
49
	private static $versionHash = '';
50
51
	/**
52
	 * @var \OCP\IConfig
53
	 */
54
	private $config;
55
56
	/**
57
	 * @param string $renderAs
58
	 * @param string $appId application id
59
	 */
60
	public function __construct( $renderAs, $appId = '' ) {
61
62
		// yes - should be injected ....
63
		$this->config = \OC::$server->getConfig();
64
65
		// Decide which page we show
66
		if($renderAs == 'user') {
67
			parent::__construct( 'core', 'layout.user' );
68
			if(in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) {
69
				$this->assign('bodyid', 'body-settings');
70
			}else{
71
				$this->assign('bodyid', 'body-user');
72
			}
73
74
			// Code integrity notification
75
			$integrityChecker = \OC::$server->getIntegrityCodeChecker();
76
			if(\OC_User::isAdminUser(\OC_User::getUser()) && $integrityChecker->isCodeCheckEnforced() && !$integrityChecker->hasPassedCheck()) {
77
				\OCP\Util::addScript('core', 'integritycheck-failed-notification');
78
			}
79
80
			// Add navigation entry
81
			$this->assign( 'application', '');
82
			$this->assign( 'appid', $appId );
83
			$navigation = \OC_App::getNavigation();
84
			$this->assign( 'navigation', $navigation);
85
			$settingsNavigation = \OC_App::getSettingsNavigation();
86
			$this->assign( 'settingsnavigation', $settingsNavigation);
87
			foreach($navigation as $entry) {
88
				if ($entry['active']) {
89
					$this->assign( 'application', $entry['name'] );
90
					break;
91
				}
92
			}
93
			
94
			foreach($settingsNavigation as $entry) {
95
				if ($entry['active']) {
96
					$this->assign( 'application', $entry['name'] );
97
					break;
98
				}
99
			}
100
			$userDisplayName = \OC_User::getDisplayName();
101
			$appsMgmtActive = strpos(\OC::$server->getRequest()->getRequestUri(), \OC::$server->getURLGenerator()->linkToRoute('settings.AppSettings.viewApps')) === 0;
102
			if ($appsMgmtActive) {
103
				$l = \OC::$server->getL10N('lib');
104
				$this->assign('application', $l->t('Apps'));
105
			}
106
			$this->assign('user_displayname', $userDisplayName);
107
			$this->assign('user_uid', \OC_User::getUser());
108
			$this->assign('appsmanagement_active', $appsMgmtActive);
109
			$this->assign('enableAvatars', $this->config->getSystemValue('enable_avatars', true) === true);
110
111
			if (\OC_User::getUser() === false) {
112
				$this->assign('userAvatarSet', false);
113
			} else {
114
				$this->assign('userAvatarSet', \OC::$server->getAvatarManager()->getAvatar(\OC_User::getUser())->exists());
115
			}
116
117
		} else if ($renderAs == 'error') {
118
			parent::__construct('core', 'layout.guest', '', false);
119
			$this->assign('bodyid', 'body-login');
120
		} else if ($renderAs == 'guest') {
121
			parent::__construct('core', 'layout.guest');
122
			$this->assign('bodyid', 'body-login');
123
		} else {
124
			parent::__construct('core', 'layout.base');
125
126
		}
127
		// Send the language to our layouts
128
		$this->assign('language', \OC_L10N::findLanguage());
0 ignored issues
show
Deprecated Code introduced by
The method OC_L10N::findLanguage() has been deprecated with message: 9.0.0 Use \OC::$server->getL10NFactory()->findLanguage() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
129
130
		if(\OC::$server->getSystemConfig()->getValue('installed', false)) {
131
			if (empty(self::$versionHash)) {
132
				$v = \OC_App::getAppVersions();
133
				$v['core'] = implode('.', \OCP\Util::getVersion());
134
				self::$versionHash = md5(implode(',', $v));
135
			}
136
		} else {
137
			self::$versionHash = md5('not installed');
138
		}
139
140
		// Add the js files
141
		$jsFiles = self::findJavascriptFiles(\OC_Util::$scripts);
142
		$this->assign('jsfiles', array());
143
		if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') {
144
			$this->append( 'jsfiles', \OC::$server->getURLGenerator()->linkToRoute('js_config', ['v' => self::$versionHash]));
145
		}
146
		foreach($jsFiles as $info) {
147
			$web = $info[1];
148
			$file = $info[2];
149
			$this->append( 'jsfiles', $web.'/'.$file . '?v=' . self::$versionHash);
150
		}
151
152
		// Add the css files
153
		$cssFiles = self::findStylesheetFiles(\OC_Util::$styles);
154
		$this->assign('cssfiles', array());
155
		$this->assign('printcssfiles', []);
156
		foreach($cssFiles as $info) {
157
			$web = $info[1];
158
			$file = $info[2];
159
160
			if (substr($file, -strlen('print.css')) === 'print.css') {
161
				$this->append( 'printcssfiles', $web.'/'.$file . '?v=' . self::$versionHash);
162
			} else {
163
				$this->append( 'cssfiles', $web.'/'.$file . '?v=' . self::$versionHash);
164
			}
165
		}
166
	}
167
168
	/**
169
	 * @param array $styles
170
	 * @return array
171
	 */
172 View Code Duplication
	static public function findStylesheetFiles($styles) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
173
		// Read the selected theme from the config file
174
		$theme = \OC_Util::getTheme();
175
176
		$locator = new \OC\Template\CSSResourceLocator(
177
			\OC::$server->getLogger(),
178
			$theme,
179
			array( \OC::$SERVERROOT => \OC::$WEBROOT ),
180
			array( \OC::$SERVERROOT => \OC::$WEBROOT ));
181
		$locator->find($styles);
182
		return $locator->getResources();
183
	}
184
185
	/**
186
	 * @param array $scripts
187
	 * @return array
188
	 */
189 View Code Duplication
	static public function findJavascriptFiles($scripts) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
190
		// Read the selected theme from the config file
191
		$theme = \OC_Util::getTheme();
192
193
		$locator = new \OC\Template\JSResourceLocator(
194
			\OC::$server->getLogger(),
195
			$theme,
196
			array( \OC::$SERVERROOT => \OC::$WEBROOT ),
197
			array( \OC::$SERVERROOT => \OC::$WEBROOT ));
198
		$locator->find($scripts);
199
		return $locator->getResources();
200
	}
201
202
	/**
203
	 * Converts the absolute file path to a relative path from \OC::$SERVERROOT
204
	 * @param string $filePath Absolute path
205
	 * @return string Relative path
206
	 * @throws \Exception If $filePath is not under \OC::$SERVERROOT
207
	 */
208
	public static function convertToRelativePath($filePath) {
209
		$relativePath = explode(\OC::$SERVERROOT, $filePath);
210
		if(count($relativePath) !== 2) {
211
			throw new \Exception('$filePath is not under the \OC::$SERVERROOT');
212
		}
213
214
		return $relativePath[1];
215
	}
216
}
217