Passed
Push — master ( d021a5...a13070 )
by Sam
03:47 queued 12s
created

DisplayMode::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 1
eloc 4
c 2
b 0
f 2
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
/**
4
 * Represents a display mode. A display mode determines how something is 
5
 * rendered on a page. The style determines how it is displayed and the context 
6
 * determines where the style applies.
7
 * 
8
 * Each user has his own display mode persisted in the database (as long as he 
9
 * has changed it once, otherwise defaults are always used).
10
 *
11
 * @author Sam Stenvall <[email protected]>
12
 * @copyright Copyright &copy; Sam Stenvall 2013-
13
 * @license https://www.gnu.org/licenses/gpl.html The GNU General Public License v3.0
14
 *
15
 * The followings are the available columns in table 'backends':
16
 * @property int $id
17
 * @property int $user_id
18
 * @property string $context
19
 * @property string $mode
20
 */
21
class DisplayMode extends CActiveRecord
22
{
23
24
	const MODE_GRID = 'grid';
25
	const MODE_LIST = 'list';
26
	const CONTEXT_RESULTS = 'results';
27
	const CONTEXT_SEASONS = 'seasons';
28
29
	/**
30
	 * Returns the static model of the specified AR class.
31
	 * @param string $className active record class name.
32
	 * @return static the static model class
33
	 */
34
	public static function model($className = __CLASS__)
35
	{
36
		return parent::model($className);
37
	}
38
39
	/**
40
	 * @return string the associated database table name
41
	 */
42
	public function tableName()
43
	{
44
		return 'display_mode';
45
	}
46
47
	/**
48
	 * @return array validation rules for model attributes.
49
	 */
50
	public function rules()
51
	{
52
		return array(
53
			array('user_id, mode, context', 'required'),
54
			array('mode', 'in', 'range'=>array(self::MODE_GRID, self::MODE_LIST)),
55
			array('context', 'in', 'range'=>array(self::CONTEXT_RESULTS, self::CONTEXT_SEASONS)),
56
		);
57
	}
58
59
	/**
60
	 * @return array the relations for this model
61
	 */
62
	public function relations()
63
	{
64
		return array(
65
			'user'=>array(self::BELONGS_TO, 'User', 'user_id'),
66
		);
67
	}
68
69
	/**
70
	 * Sets the user_id to the current user before validating
71
	 * @return boolean whether validation should be executed
72
	 */
73
	protected function beforeValidate()
74
	{
75
		$this->user_id = Yii::app()->user->id;
76
77
		return parent::beforeValidate();
78
	}
79
80
	/**
81
	 * Returns the display mode for the specified context
82
	 * @param string $context the context
83
	 * @return DisplayMode the model, or null if no mode is defined
84
	 */
85
	public function findByContext($context)
86
	{
87
		return $this->findByAttributes(array(
88
					'user_id'=>Yii::app()->user->id,
89
					'context'=>$context));
90
	}
91
92
}
93