1: <?php
2:
3: App::uses('ArrayUtil', 'Base.Lib');
4: App::uses('AppHelper', 'View/Helper');
5:
6: 7: 8: 9: 10: 11: 12: 13: 14:
15: class ActionListHelper extends AppHelper {
16:
17: const LAYOUT_LIST = 'list';
18: const LAYOUT_TABLE = 'table';
19:
20: public $helpers = array(
21: 'AccessControl.AccessControl',
22: );
23:
24: 25: 26:
27: public $settings = array(
28: 'listLayoutBeforeAll' => '',
29: 'listLayoutAfterAll' => '',
30: 'listLayoutBeforeEach' => '',
31: 'listLayoutAfterEach' => '',
32: 'tableLayoutBeforeAll' => '',
33: 'tableLayoutAfterAll' => '',
34: 'tableLayoutBeforeEach' => '',
35: 'tableLayoutAfterEach' => '',
36: 'layout' => self::LAYOUT_LIST
37: );
38:
39: 40: 41: 42: 43: 44:
45: public function actionList($actions, $options = array()) {
46: $options = array_merge($this->settings, $options);
47: switch ($options['layout']) {
48: case self::LAYOUT_LIST:
49: return $this->_outputActionsList($this->_parseActions($actions), $options);
50:
51: case self::LAYOUT_TABLE:
52: return $this->_outputActionsTable($this->_parseActions($actions), $options);
53:
54: default:
55: throw new Exception("Layout not mapped: \"{$options['layout']}\".");
56: }
57: }
58:
59: private function _parseActions($actions) {
60: return array_map(function($value) {
61: return array_merge(
62: array('post' => false, 'caption' => '?', 'linkOptions' => array())
63: , $value
64: );
65: }, $actions);
66: }
67:
68: 69: 70: 71: 72: 73:
74: private function _outputActionsList($actions, $options) {
75: $buffer = $options['listLayoutBeforeAll'];
76: foreach ($actions as $action) {
77: $buffer .= $options['listLayoutBeforeEach'] . $this->_buildActionLink($action) . $options['listLayoutAfterEach'];
78: }
79: $buffer .= $options['listLayoutAfterAll'];
80: return $buffer;
81: }
82:
83: 84: 85: 86: 87:
88: private function _outputActionsTable($actions, $options) {
89: if (count($actions) >= 4) {
90: $rows = floor(count($actions) / sqrt(count($actions)));
91: } else {
92: $rows = 1;
93: }
94:
95: $columns = ceil(count($actions) / $rows);
96: $cellWidth = ($columns == 0 ? '100%' : floor(100 / $columns) . '%');
97:
98: $b = $options['tableLayoutBeforeAll'];
99: $b .= '<table class="actionListHelperTableLayout">';
100:
101: for ($row = 0; $row < $rows; $row++) {
102: $b .= '<tr>';
103: for ($column = 0; $column < $columns; $column++) {
104: $index = $row * $columns + $column;
105: $b .= "<td style='width: $cellWidth'>";
106: if (!empty($actions[$index])) {
107: $b .= $options['tableLayoutBeforeEach'];
108: $b .= $this->_buildActionLink($actions[$index]);
109: $b .= $options['tableLayoutAfterEach'];
110: } else {
111: $b .= ' ';
112: }
113:
114: $b .= '</td>';
115: }
116: $b .= '</tr>';
117: }
118:
119: $b .= '</table>';
120: $b .= $options['tableLayoutAfterAll'];
121: return $b;
122: }
123:
124: 125: 126: 127: 128:
129: private function _buildActionLink($action) {
130: $linkOptions = empty($action['linkOptions']) ? array() : $action['linkOptions'];
131: $linkOptions['method'] = $action['post'] ? 'post' : 'get';
132: $question = isset($action['question']) ? __d('widgets',$action['question']) : false;
133: return $this->AccessControl->link(
134: $action['caption']
135: , $action['url']
136: , $linkOptions
137: , $question
138: );
139: }
140:
141: }
142: