|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* HiPanel core package |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://hipanel.com/ |
|
6
|
|
|
* @package hipanel-core |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hipanel\models; |
|
12
|
|
|
|
|
13
|
|
|
use Yii; |
|
14
|
|
|
use yii\base\Model; |
|
15
|
|
|
|
|
16
|
|
|
class IndexPageUiOptions extends Model |
|
17
|
|
|
{ |
|
18
|
|
|
const ORIENTATION_VERTICAL = 'vertical'; |
|
19
|
|
|
|
|
20
|
|
|
const ORIENTATION_HORIZONTAL = 'horizontal'; |
|
21
|
|
|
|
|
22
|
|
|
public $sort; |
|
23
|
|
|
|
|
24
|
|
|
public $per_page; |
|
25
|
|
|
|
|
26
|
|
|
public $orientation; |
|
27
|
|
|
|
|
28
|
|
|
public $representation; |
|
29
|
|
|
|
|
30
|
|
|
public $availableRepresentations = []; |
|
31
|
|
|
|
|
32
|
|
|
public function fields() |
|
33
|
|
|
{ |
|
34
|
|
|
return ['sort', 'per_page', 'orientation', 'representation']; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function rules() |
|
38
|
|
|
{ |
|
39
|
|
|
return [ |
|
40
|
|
|
['per_page', 'default', 'value' => 25], |
|
41
|
|
|
['per_page', 'number', 'skipOnEmpty' => true], |
|
42
|
|
|
|
|
43
|
|
|
['sort', 'default', 'value' => null], |
|
44
|
|
|
['sort', 'string', 'skipOnEmpty' => true], |
|
45
|
|
|
|
|
46
|
|
|
['orientation', 'default', 'value' => $this->getDefaultOrientation()], |
|
47
|
|
|
['orientation', 'in', 'range' => array_keys($this->getOrientationOptions())], |
|
48
|
|
|
|
|
49
|
|
|
['representation', 'default', 'value' => null], |
|
50
|
|
|
['representation', function ($attribute, $params, $validator) { |
|
|
|
|
|
|
51
|
|
|
if (!empty($this->availableRepresentations) && in_array($this->{$attribute}, $this->availableRepresentations, true)) { |
|
52
|
|
|
$this->addError($attribute, 'The token must contain letters or digits.'); |
|
53
|
|
|
} |
|
54
|
|
|
}], |
|
55
|
|
|
]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return array |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getOrientationOptions() |
|
62
|
|
|
{ |
|
63
|
|
|
return [ |
|
64
|
|
|
self::ORIENTATION_HORIZONTAL => Yii::t('hipanel', 'Horizontal'), |
|
65
|
|
|
self::ORIENTATION_VERTICAL => Yii::t('hipanel', 'Vertical'), |
|
66
|
|
|
]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getDefaultOrientation() |
|
73
|
|
|
{ |
|
74
|
|
|
$settings = isset(Yii::$app->themeManager) ? Yii::$app->themeManager->getSettings() : null; |
|
75
|
|
|
$orientationOptions = array_keys($this->getOrientationOptions()); |
|
76
|
|
|
|
|
77
|
|
|
if (isset($settings->filterOrientation) && in_array($settings->filterOrientation, $orientationOptions, true)) { |
|
78
|
|
|
return $settings->filterOrientation; |
|
79
|
|
|
} else { |
|
80
|
|
|
return reset($orientationOptions); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function getSortDirection() |
|
85
|
|
|
{ |
|
86
|
|
|
return (strncmp($this->sort, '-', 1) === 0) ? SORT_DESC : SORT_ASC; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function getSortAttribute() |
|
90
|
|
|
{ |
|
91
|
|
|
$attribute = $this->sort; |
|
92
|
|
|
if (strncmp($attribute, '-', 1) === 0) { |
|
93
|
|
|
$attribute = substr($attribute, 1); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $attribute; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.