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