forked from NetCommons3/NetCommons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplayNumberHelper.php
More file actions
124 lines (109 loc) · 3.13 KB
/
DisplayNumberHelper.php
File metadata and controls
124 lines (109 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
* DisplayNumber Helper
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('AppHelper', 'View/Helper');
/**
* DisplayNumber Helper
*
* @package NetCommons\NetCommons\View\Helper
*/
class DisplayNumberHelper extends AppHelper {
/**
* Other helpers used by FormHelper
*
* @var array
*/
public $helpers = array(
'Form',
'Html',
'NetCommons.Button',
'NetCommons.NetCommonsForm'
);
/**
* listStyle
*
* @var array
*/
public $displayNumberOptions = array(1, 5, 10, 20, 50, 100);
/**
* Get display number options
*
* @param array $attributes Array of HTML attributes, and special attributes above.
* @return string Completed radio widget set.
*/
private function __getOptions($attributes = array()) {
$options = array();
if (! isset($attributes['options'])) {
if (! isset($attributes['unit'])) {
$attributes['unit']['single'] = __d('net_commons', '%s item');
$attributes['unit']['multiple'] = __d('net_commons', '%s items');
} elseif (is_string($attributes['unit'])) {
$unit = $attributes['unit'];
$attributes['unit'] = array();
$attributes['unit']['single'] = $unit;
$attributes['unit']['multiple'] = $unit;
}
foreach ($this->displayNumberOptions as $value) {
if ($value === 1) {
$unitLabel = $attributes['unit']['single'];
} else {
$unitLabel = $attributes['unit']['multiple'];
}
$options[$value] = sprintf($unitLabel, $value);
}
} else {
$options = $attributes['options'];
}
return $options;
}
/**
* Setting display number
*
* @param string $fieldName Name of a field, like this "Modelname.fieldname"
* @param array $attributes Array of HTML attributes, and special attributes above.
* @return string Completed radio widget set.
*/
public function select($fieldName, $attributes = array()) {
$attributes['options'] = $this->__getOptions($attributes);
if (isset($attributes['unit'])) {
unset($attributes['unit']);
}
$defaultAttributes = array(
'type' => 'select',
);
$attributes = Hash::merge($defaultAttributes, $attributes);
return $this->NetCommonsForm->input($fieldName, $attributes);
}
/**
* Output display number drop down toggle
*
* @param array $attributes Array of options and HTML arguments.
* @return string HTML tags
*/
public function dropDownToggle($attributes = array()) {
$attributes['options'] = $this->__getOptions($attributes);
if (isset($attributes['unit'])) {
unset($attributes['unit']);
}
if (! isset($attributes['currentLimit'])) {
$attributes['currentLimit'] = $this->_View->Paginator->param('limit');
}
if (! isset($attributes['url'])) {
$named = $this->_View->Paginator->params['named'];
$named['page'] = '1';
$attributes['url'] = $named;
}
return $this->_View->element('NetCommons.limit_dropdown_toggle', array(
'displayNumberOptions' => $attributes['options'],
'currentLimit' => $attributes['currentLimit'],
'url' => $attributes['url'],
));
}
}