Completed
Push — master ( aec9f9...84751c )
by Sam
02:36 queued 01:13
created

MediaController::getDefaultDisplayMode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * Base class for controllers that serve some kind of media to the user
5
 *
6
 * @author Sam Stenvall <[email protected]>
7
 * @copyright Copyright &copy; Sam Stenvall 2013-
8
 * @license https://www.gnu.org/licenses/gpl.html The GNU General Public License v3.0
9
 * 
10
 * Virtual properties in this class
11
 * @property string $displayMode
12
 */
13
abstract class MediaController extends Controller
14
{
15
16
	/**
17
	 * @return array list of actions that a spectator should be prohibited from 
18
	 * performing
19
	 */
20
	abstract protected function getSpectatorProhibitedActions();
21
	
22
	/**
23
	 * Some parts of the code assume we're in either Movie or TvShowController 
24
	 * so make sure the common actions are actually available
25
	 */
26
	abstract public function actionDetails($id);
27
28
	/**
29
	 * Override parent implementation to add access control filter
30
	 * @return array the filters for this controller
31
	 */
32
	public function filters()
33
	{
34
		return array_merge(parent::filters(), array(
35
			'accessControl',
36
			array('application.filters.CheckBackendConnectivityFilter'),
37
		));
38
	}
39
40
	/**
41
	 * @return array the access rules for this controller
42
	 */
43
	public function accessRules()
44
	{
45
		return array(
46
			// Prevent spectators from performing certain actions
47
			array('deny',
48
				'actions'=>$this->getSpectatorProhibitedActions(),
49
				'expression'=>function() {
50
					return Yii::app()->user->role === User::ROLE_SPECTATOR;
51
				}
52
			),
53
			array('deny',
54
				'actions'=>array('playOnBackend'),
55
				'expression'=>function() {
56
					return Yii::app()->user->role !== User::ROLE_ADMIN;
57
				}
58
			),
59
		);
60
	}
61
62
	/**
63
	 * Sets the display mode for the specified context and redirects the user 
64
	 * back to where he came from.
65
	 * @param string $mode the desired display mode
66
	 * @param string $context the context
67
	 */
68
	public function actionSetDisplayMode($mode, $context)
69
	{
70
		// See if the user has a stored display mode for this context
71
		$displayMode = DisplayMode::model()->findByContext($context);
72
73
		if ($displayMode === null)
74
			$displayMode = new DisplayMode();
75
76
		$displayMode->mode = $mode;
77
		$displayMode->context = $context;
78
79
		if (!$displayMode->save())
80
			throw new InvalidRequestException();
81
82
		$this->redirectToPrevious(Yii::app()->homeUrl);
83
	}
84
85
	/**
86
	 * Returns the current display mode for the specified context
87
	 * @param string $context the context
88
	 * @return string the display mode
89
	 */
90
	public function getDisplayMode($context)
91
	{
92
		$model = DisplayMode::model()->findByContext($context);
93
94
		// Use default display mode if no specific one is stored
95
		if ($model === null)
96
			$displayMode = $this->getDefaultDisplayMode($context);
97
		else
98
			$displayMode = $model->mode;
99
		
100
		return $displayMode;
101
	}
102
	
103
	/**
104
	 * Returns the default display mode for the specified context
105
	 * @param string $context the context
106
	 * @return string the display mode
107
	 */
108
	private function getDefaultDisplayMode($context)
109
	{
110
		// Use list view for seasons
111
		if ($context === DisplayMode::CONTEXT_SEASONS)
112
			return DisplayMode::MODE_LIST;
113
114
		return DisplayMode::MODE_GRID;
115
	}
116
	
117
	/**
118
	 * Generates a playlist from the specified media item
119
	 * @param IStreamable $media the media item
120
	 * @param string $format the desired playlist format. Defaults to null, 
121
	 * meaning the configured default format will be used
122
	 * @return Playlist the playlist
123
	 */
124
	private function createPlaylist($media, $format = null)
125
	{
126
		// Create the playlist
127
		$name = $media->getDisplayName();
128
		$playlist = PlaylistFactory::create($name, $format);
129
130
		// Add the playlist items
131
		foreach ($media->getItemLinks() as $itemLink)
132
		{
133
			// The item used in the playlist is not necessarily the same item 
134
			// as $media
135
			$item = new PlaylistItem($itemLink);
136
			$playlist->addItem($item);
137
		}
138
139
		return $playlist;
140
	}
141
142
	/**
143
	 * Creates and serves a playlist based on the specified media item
144
	 * @param IStreamable $media a media item
145
	 * @param string $format the desired playlist format. Defaults to null, 
146
	 * meaning the configured default format will be used
147
	 */
148
	protected function servePlaylist($media, $format = null)
149
	{
150
		$playlist = $this->createPlaylist($media, $format);
151
		
152
		header('Content-Type: '.$playlist->getMIMEType());
153
		header('Content-Disposition: attachment; filename="'.$playlist->getSanitizedFileName().'"');
154
155
		echo $playlist;
156
	}
157
	
158
	/**
159
	 * Tells the current backend to play the specified file, then redirects 
160
	 * to the previous page
161
	 * @param string $file the file path
162
	 */
163
	public function actionPlayOnBackend($file)
164
	{
165
		Yii::app()->xbmc->performRequestUncached('Player.Open', array(
166
			'item'=>array('file'=>$file)));
167
168
		// Go back to the previous page and inform the user
169
		Yii::app()->user->setFlash('info', Yii::t('RetrieveMediaWidget', 'The item should now be playing on {backend}', array(
170
			'{backend}'=>Yii::app()->backendManager->getCurrent()->name)));
171
172
		$this->redirectToPrevious('index');
173
	}
174
	
175
	/**
176
	 * Plays the specified URL in the in-browser player
177
	 * @param string $url the URL to the media
178
	 */
179
	public function actionWatchInBrowser($url)
180
	{
181
		// Create a tuple containing the URL and the MIME type of the file
182
		$item = new stdClass();
183
		$item->url = $url;
184
		$item->mimeType = MediaInfoHelper::getMIMEType($url);
185
186
		$this->render('//videoLibrary/browserPlayer', array(
187
			'items'=>array($item)));
188
	}
189
}
190