@@ -24,145 +24,145 @@ |
||
24 | 24 | |
25 | 25 | class GridViewHelper |
26 | 26 | { |
27 | - /** |
|
28 | - * A list of grid aliases |
|
29 | - * @var array |
|
30 | - */ |
|
31 | - private static $aliases = [ |
|
32 | - 'column' => [ |
|
33 | - 'attribute' => AttributeColumn::class, |
|
34 | - 'raw' => CallbackColumn::class, |
|
35 | - 'callback' => CallbackColumn::class, |
|
36 | - 'actions' => ActionsColumn::class, |
|
37 | - 'view' => ViewColumn::class, |
|
38 | - ], |
|
39 | - 'formatter' => [ |
|
40 | - 'email' => EmailFormatter::class, |
|
41 | - 'image' => ImageFormatter::class, |
|
42 | - 'text' => TextFormatter::class, |
|
43 | - 'url' => UrlFormatter::class, |
|
44 | - 'raw' => RawFormatter::class, |
|
45 | - 'currency' => CurrencyFormatter::class, |
|
46 | - ], |
|
47 | - 'filter' => [ |
|
48 | - 'text' => TextFilter::class, |
|
49 | - 'dropdown' => DropdownFilter::class, |
|
50 | - ], |
|
51 | - 'renderer' => [ |
|
52 | - 'default' => DefaultRenderer::class, |
|
53 | - ], |
|
54 | - 'action' => [ |
|
55 | - 'delete' => DeleteAction::class, |
|
56 | - 'update' => EditAction::class, |
|
57 | - 'edit' => EditAction::class, |
|
58 | - 'show' => ShowAction::class, |
|
59 | - 'view' => ShowAction::class, |
|
60 | - 'action' => Action::class, |
|
61 | - ] |
|
62 | - ]; |
|
63 | - |
|
64 | - private function __construct() {} |
|
65 | - |
|
66 | - /** |
|
67 | - * Useful in case you want to register a new alias for your project |
|
68 | - * @param string $context |
|
69 | - * @param string $alias |
|
70 | - * @param string $aliasTo |
|
71 | - */ |
|
72 | - public static function registerAlias(string $context, string $alias, string $aliasTo) |
|
73 | - { |
|
74 | - self::$aliases[$context][$alias] = $aliasTo; |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Allows to resolve class name by its alias |
|
79 | - * @param string $context |
|
80 | - * @param string $alias |
|
81 | - * @return mixed |
|
82 | - */ |
|
83 | - public static function resolveAlias(string $context, string $alias) |
|
84 | - { |
|
85 | - return self::$aliases[$context][$alias] ?? $alias; |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Allows to convert options array to html string |
|
90 | - * @param array $htmlOptions |
|
91 | - * @param array $context - context is variables, which are allowed to use when property value calculated dynamically |
|
92 | - * @return string |
|
93 | - */ |
|
94 | - public static function htmlOptionsToString(array $htmlOptions, array $context = []) : string |
|
95 | - { |
|
96 | - if (empty($htmlOptions)) { |
|
97 | - return ''; |
|
98 | - } |
|
99 | - |
|
100 | - $out = []; |
|
101 | - |
|
102 | - foreach ($htmlOptions as $k => $v) { |
|
103 | - |
|
104 | - if ($v instanceof \Closure) { |
|
105 | - $v = call_user_func_array($v, $context); |
|
106 | - } |
|
107 | - |
|
108 | - $out[] = htmlentities($k) . '="' . htmlentities($v, ENT_COMPAT) . '"'; |
|
109 | - } |
|
110 | - |
|
111 | - return implode(' ', $out); |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * Allows to make column title by it key or attribute name |
|
116 | - * @param string|int $key |
|
117 | - * @return string |
|
118 | - */ |
|
119 | - public static function columnTitle($key) : string |
|
120 | - { |
|
121 | - if (is_numeric($key)) { |
|
122 | - return 'Column'; |
|
123 | - } |
|
124 | - |
|
125 | - return ucwords( |
|
126 | - trim( |
|
127 | - preg_replace_callback( |
|
128 | - '/([A-Z]|_|\.)/', |
|
129 | - function($word) { |
|
130 | - $word = $word[0]; |
|
131 | - |
|
132 | - if ($word == '_' || $word == '.') { |
|
133 | - return ' '; |
|
134 | - } |
|
135 | - |
|
136 | - return ' ' . strtolower($word); |
|
137 | - }, |
|
138 | - $key |
|
139 | - ) |
|
140 | - ) |
|
141 | - ); |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Helper for internal purposes |
|
146 | - * @param $id |
|
147 | - * @param $component |
|
148 | - * @return string |
|
149 | - */ |
|
150 | - public static function gridIdFormatter($id, $component) |
|
151 | - { |
|
152 | - if ($id == 0) { |
|
153 | - return $component; |
|
154 | - } |
|
155 | - |
|
156 | - return 'grid[' . $id . '][' . $component . ']'; |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Generates page url with all requested params from request |
|
161 | - * @param $gridId |
|
162 | - * @param $page |
|
163 | - */ |
|
164 | - public static function pageUrl($gridId, $page) |
|
165 | - { |
|
166 | - return url()->current() . '?' . Arr::query([\Woo\GridView\GridViewHelper::gridIdFormatter($gridId, 'page') => $page] + request()->query()); |
|
167 | - } |
|
27 | + /** |
|
28 | + * A list of grid aliases |
|
29 | + * @var array |
|
30 | + */ |
|
31 | + private static $aliases = [ |
|
32 | + 'column' => [ |
|
33 | + 'attribute' => AttributeColumn::class, |
|
34 | + 'raw' => CallbackColumn::class, |
|
35 | + 'callback' => CallbackColumn::class, |
|
36 | + 'actions' => ActionsColumn::class, |
|
37 | + 'view' => ViewColumn::class, |
|
38 | + ], |
|
39 | + 'formatter' => [ |
|
40 | + 'email' => EmailFormatter::class, |
|
41 | + 'image' => ImageFormatter::class, |
|
42 | + 'text' => TextFormatter::class, |
|
43 | + 'url' => UrlFormatter::class, |
|
44 | + 'raw' => RawFormatter::class, |
|
45 | + 'currency' => CurrencyFormatter::class, |
|
46 | + ], |
|
47 | + 'filter' => [ |
|
48 | + 'text' => TextFilter::class, |
|
49 | + 'dropdown' => DropdownFilter::class, |
|
50 | + ], |
|
51 | + 'renderer' => [ |
|
52 | + 'default' => DefaultRenderer::class, |
|
53 | + ], |
|
54 | + 'action' => [ |
|
55 | + 'delete' => DeleteAction::class, |
|
56 | + 'update' => EditAction::class, |
|
57 | + 'edit' => EditAction::class, |
|
58 | + 'show' => ShowAction::class, |
|
59 | + 'view' => ShowAction::class, |
|
60 | + 'action' => Action::class, |
|
61 | + ] |
|
62 | + ]; |
|
63 | + |
|
64 | + private function __construct() {} |
|
65 | + |
|
66 | + /** |
|
67 | + * Useful in case you want to register a new alias for your project |
|
68 | + * @param string $context |
|
69 | + * @param string $alias |
|
70 | + * @param string $aliasTo |
|
71 | + */ |
|
72 | + public static function registerAlias(string $context, string $alias, string $aliasTo) |
|
73 | + { |
|
74 | + self::$aliases[$context][$alias] = $aliasTo; |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Allows to resolve class name by its alias |
|
79 | + * @param string $context |
|
80 | + * @param string $alias |
|
81 | + * @return mixed |
|
82 | + */ |
|
83 | + public static function resolveAlias(string $context, string $alias) |
|
84 | + { |
|
85 | + return self::$aliases[$context][$alias] ?? $alias; |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Allows to convert options array to html string |
|
90 | + * @param array $htmlOptions |
|
91 | + * @param array $context - context is variables, which are allowed to use when property value calculated dynamically |
|
92 | + * @return string |
|
93 | + */ |
|
94 | + public static function htmlOptionsToString(array $htmlOptions, array $context = []) : string |
|
95 | + { |
|
96 | + if (empty($htmlOptions)) { |
|
97 | + return ''; |
|
98 | + } |
|
99 | + |
|
100 | + $out = []; |
|
101 | + |
|
102 | + foreach ($htmlOptions as $k => $v) { |
|
103 | + |
|
104 | + if ($v instanceof \Closure) { |
|
105 | + $v = call_user_func_array($v, $context); |
|
106 | + } |
|
107 | + |
|
108 | + $out[] = htmlentities($k) . '="' . htmlentities($v, ENT_COMPAT) . '"'; |
|
109 | + } |
|
110 | + |
|
111 | + return implode(' ', $out); |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * Allows to make column title by it key or attribute name |
|
116 | + * @param string|int $key |
|
117 | + * @return string |
|
118 | + */ |
|
119 | + public static function columnTitle($key) : string |
|
120 | + { |
|
121 | + if (is_numeric($key)) { |
|
122 | + return 'Column'; |
|
123 | + } |
|
124 | + |
|
125 | + return ucwords( |
|
126 | + trim( |
|
127 | + preg_replace_callback( |
|
128 | + '/([A-Z]|_|\.)/', |
|
129 | + function($word) { |
|
130 | + $word = $word[0]; |
|
131 | + |
|
132 | + if ($word == '_' || $word == '.') { |
|
133 | + return ' '; |
|
134 | + } |
|
135 | + |
|
136 | + return ' ' . strtolower($word); |
|
137 | + }, |
|
138 | + $key |
|
139 | + ) |
|
140 | + ) |
|
141 | + ); |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Helper for internal purposes |
|
146 | + * @param $id |
|
147 | + * @param $component |
|
148 | + * @return string |
|
149 | + */ |
|
150 | + public static function gridIdFormatter($id, $component) |
|
151 | + { |
|
152 | + if ($id == 0) { |
|
153 | + return $component; |
|
154 | + } |
|
155 | + |
|
156 | + return 'grid[' . $id . '][' . $component . ']'; |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Generates page url with all requested params from request |
|
161 | + * @param $gridId |
|
162 | + * @param $page |
|
163 | + */ |
|
164 | + public static function pageUrl($gridId, $page) |
|
165 | + { |
|
166 | + return url()->current() . '?' . Arr::query([\Woo\GridView\GridViewHelper::gridIdFormatter($gridId, 'page') => $page] + request()->query()); |
|
167 | + } |
|
168 | 168 | } |