VideoFilterForm::attributeLabels()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
/**
4
 * Base class for filter forms
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
abstract class VideoFilterForm extends CFormModel
11
{
12
	
13
	const WATCHED_STATUS_WATCHED = 'watched';
14
	const WATCHED_STATUS_UNWATCHED = 'unwatched';
15
16
	/**
17
	 * @var string the movie title
18
	 */
19
	public $name;
20
21
	/**
22
	 * @var string the movie genre
23
	 */
24
	public $genre;
25
	
26
	/**
27
	 * @var string name of actor
28
	 */
29
	public $actor;
30
	
31
	/**
32
	 * @var string watched status
33
	 */
34
	public $watchedStatus;
35
36
	/**
37
	 * @var array list of all genres (key same as value)
38
	 */
39
	protected $_genres = array();
40
41
	/**
42
	 * @return array the attribute labels for this model
43
	 */
44
	public function attributeLabels()
45
	{
46
		return array(
47
			'name'=>Yii::t('FilterForm', 'Name'),
48
			'genre'=>Yii::t('FilterForm', 'Genre'),
49
			'actor'=>Yii::t('FilterForm', 'Actor'),
50
			'watchedStatus'=>Yii::t('FilterForm', 'Watched status'),
51
		);
52
	}
53
54
	/**
55
	 * @return array the validation rules for this model
56
	 */
57
	public function rules()
58
	{
59
		return array(
60
			array('name', 'safe'),
61
			array('genre', 'in', 'range'=>$this->getGenres()),
62
			array('actor', 'safe'),
63
			array('watchedStatus', 'in', 'range'=>array_keys(self::getWatchedStatuses())),
64
		);
65
	}
66
	
67
	/**
68
	 * Populates and returns the list of genres
69
	 * @return array
70
	 */
71
	public function getGenres()
72
	{
73
		if (empty($this->_genres))
74
		{
75
			$genres = VideoLibrary::getGenres($this->getGenreType());
76
77
			foreach ($genres as $genre)
78
				$this->_genres[$genre->label] = $genre->label;
79
		}
80
81
		return $this->_genres;
82
	}
83
84
	/**
85
	 * @return boolean whether the filter is empty or not
86
	 */
87
	public function isEmpty()
88
	{
89
		foreach ($this->attributes as $attribute)
90
			if ($attribute)
91
				return false;
92
93
		return true;
94
	}
95
	
96
	/**
97
	 * Returns the request parameters that represent the current filter.
98
	 * @return array the request parameters
99
	 */
100
	public function buildRequestParameters()
101
	{
102
		$modelClass = get_class($this);
103
		$parameters = array();
104
105
		if (isset($_GET[$modelClass]))
106
		{
107
			$this->attributes = $_GET[$modelClass];
108
109
			if (!$this->isEmpty() && $this->validate())
110
				$parameters['filter'] = $this->getFilter();
111
		}
112
113
		return $parameters;
114
	}
115
116
	/**
117
	 * Returns a filter object which can be used when quering for media using 
118
	 * the methods in VideoLibrary
119
	 * @return \stdClass
120
	 */
121
	public function getFilter()
122
	{
123
		$filters = new stdClass();
124
125
		foreach ($this->getFilterDefinitions() as $field=> $options)
126
		{
127
			// '0' is a valid value so we can't use empty()
128
			if ($options['value'] === '' || $options['value'] === null)
129
				continue;
130
131
			$filter = new stdClass();
132
			$filter->field = $field;
133
			$filter->operator = $options['operator'];
134
			$filter->value = $options['value'];
135
136
			$filters->and[] = $filter;
137
		}
138
139
		return $filters;
140
	}
141
	
142
	/**
143
	 * Returns a list of possible watched statuses. Used for validation and 
144
	 * population of dropdown lists.
145
	 * @return array
146
	 */
147
	public static function getWatchedStatuses()
148
	{
149
		return array(
150
			self::WATCHED_STATUS_WATCHED=>Yii::t('WatchedStatus', 'Watched'),
151
			self::WATCHED_STATUS_UNWATCHED=>Yii::t('WatchedStatus', 'Unwatched'),
152
		);
153
	}
154
	
155
	/**
156
	 * Returns the definitions for the common filters
157
	 * @return array the filter definitions
158
	 */
159
	protected function getCommonFilterDefinitions()
160
	{
161
		$filter = array();
162
163
		// only do a partial match on the title
164
		$filter['title'] = array(
165
			'operator'=>'contains',
166
			'value'=>$this->name);
167
168
		$filter['genre'] = array(
169
			'operator'=>'is',
170
			'value'=>$this->genre);
171
		
172
		$filter['actor'] = array(
173
			'operator'=>'is',
174
			'value'=>$this->actor,
175
		);
176
		
177
		if ($this->watchedStatus)
178
		{
179
			switch ($this->watchedStatus)
180
			{
181
				case self::WATCHED_STATUS_WATCHED:
182
					$operator = 'greaterthan';
183
					break;
184
				case self::WATCHED_STATUS_UNWATCHED:
185
					$operator = 'is';
186
					break;
187
			}
188
189
			$filter['playcount'] = array(
190
				'operator'=>$operator,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $operator does not seem to be defined for all execution paths leading up to this point.
Loading history...
191
				'value'=>'0',
192
			);
193
		}
194
		
195
		return $filter;
196
	}
197
198
	/**
199
	 * @return string the type of genres to fetch (movies, TV shows, etc.)
200
	 */
201
	abstract public function getGenreType();
202
203
	/**
204
	 * Should return an array containing the individual filter definitions
205
	 */
206
	abstract public function getFilterDefinitions();
207
}