@@ -7,58 +7,58 @@ |
||
| 7 | 7 | |
| 8 | 8 | class EloquentDataProvider extends BaseDataProvider |
| 9 | 9 | { |
| 10 | - protected $query; |
|
| 10 | + protected $query; |
|
| 11 | 11 | |
| 12 | - /** |
|
| 13 | - * EloquentDataProvider constructor. |
|
| 14 | - * @param Builder $query |
|
| 15 | - */ |
|
| 16 | - public function __construct(Builder $query) |
|
| 17 | - { |
|
| 18 | - $this->query = clone $query; |
|
| 19 | - } |
|
| 12 | + /** |
|
| 13 | + * EloquentDataProvider constructor. |
|
| 14 | + * @param Builder $query |
|
| 15 | + */ |
|
| 16 | + public function __construct(Builder $query) |
|
| 17 | + { |
|
| 18 | + $this->query = clone $query; |
|
| 19 | + } |
|
| 20 | 20 | |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * @param GridViewRequest $request |
|
| 24 | - * @return Builder |
|
| 25 | - */ |
|
| 26 | - protected function baseQuery(GridViewRequest $request) |
|
| 27 | - { |
|
| 28 | - $query = clone $this->query; |
|
| 22 | + /** |
|
| 23 | + * @param GridViewRequest $request |
|
| 24 | + * @return Builder |
|
| 25 | + */ |
|
| 26 | + protected function baseQuery(GridViewRequest $request) |
|
| 27 | + { |
|
| 28 | + $query = clone $this->query; |
|
| 29 | 29 | |
| 30 | - foreach ($request->filters as $field => $value) { |
|
| 31 | - $query->where($field, 'LIKE', '%' . $value . '%'); |
|
| 32 | - } |
|
| 30 | + foreach ($request->filters as $field => $value) { |
|
| 31 | + $query->where($field, 'LIKE', '%' . $value . '%'); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - if ($request->sortColumn) { |
|
| 35 | - $query->orderBy($request->sortColumn, $request->sortOrder); |
|
| 36 | - } |
|
| 34 | + if ($request->sortColumn) { |
|
| 35 | + $query->orderBy($request->sortColumn, $request->sortOrder); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - return $query; |
|
| 39 | - } |
|
| 38 | + return $query; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * @inheritdoc |
|
| 43 | - */ |
|
| 44 | - public function getCount(GridViewRequest $request) : int |
|
| 45 | - { |
|
| 46 | - return $this->baseQuery($request)->count(); |
|
| 47 | - } |
|
| 41 | + /** |
|
| 42 | + * @inheritdoc |
|
| 43 | + */ |
|
| 44 | + public function getCount(GridViewRequest $request) : int |
|
| 45 | + { |
|
| 46 | + return $this->baseQuery($request)->count(); |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * @inheritdoc |
|
| 51 | - */ |
|
| 52 | - public function getData(GridViewRequest $request) |
|
| 53 | - { |
|
| 54 | - $query = $this->baseQuery($request); |
|
| 49 | + /** |
|
| 50 | + * @inheritdoc |
|
| 51 | + */ |
|
| 52 | + public function getData(GridViewRequest $request) |
|
| 53 | + { |
|
| 54 | + $query = $this->baseQuery($request); |
|
| 55 | 55 | |
| 56 | - if ($request->perPage == 0) { |
|
| 57 | - return $query->get(); |
|
| 58 | - } |
|
| 56 | + if ($request->perPage == 0) { |
|
| 57 | + return $query->get(); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - return $query->offset(($request->page - 1) * $request->perPage) |
|
| 61 | - ->limit($request->perPage) |
|
| 62 | - ->get(); |
|
| 63 | - } |
|
| 60 | + return $query->offset(($request->page - 1) * $request->perPage) |
|
| 61 | + ->limit($request->perPage) |
|
| 62 | + ->get(); |
|
| 63 | + } |
|
| 64 | 64 | } |
@@ -9,166 +9,166 @@ |
||
| 9 | 9 | |
| 10 | 10 | abstract class BaseColumn |
| 11 | 11 | { |
| 12 | - use Configurable; |
|
| 13 | - |
|
| 14 | - /** |
|
| 15 | - * Column title |
|
| 16 | - * @var string |
|
| 17 | - */ |
|
| 18 | - public $title = ''; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * Column value. Could be an attribute, |
|
| 22 | - * @var string|mixed |
|
| 23 | - */ |
|
| 24 | - public $value = ''; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @var BaseFilter |
|
| 28 | - */ |
|
| 29 | - public $filter; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var boolean|string |
|
| 33 | - */ |
|
| 34 | - public $sortable = true; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @var array |
|
| 38 | - */ |
|
| 39 | - public $headerHtmlOptions = []; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @var array |
|
| 43 | - */ |
|
| 44 | - public $contentHtmlOptions = []; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @var array - allowed: raw, url, email, text, image |
|
| 48 | - */ |
|
| 49 | - public $formatters = ['text']; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Value when column is empty |
|
| 53 | - * @var string |
|
| 54 | - */ |
|
| 55 | - public $emptyValue = '-'; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * BaseColumn constructor. |
|
| 59 | - * @param array $config |
|
| 60 | - * @throws \Woo\GridView\Exceptions\GridViewConfigException |
|
| 61 | - */ |
|
| 62 | - public function __construct(array $config) |
|
| 63 | - { |
|
| 64 | - $this->loadConfig($config); |
|
| 65 | - |
|
| 66 | - $this->buildFilter(); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Allows to get sortable column's name |
|
| 71 | - */ |
|
| 72 | - public function getSortableName() |
|
| 73 | - { |
|
| 74 | - if ($this->sortable === false) { |
|
| 75 | - return false; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - if (is_scalar($this->sortable)) { |
|
| 79 | - return $this->sortable; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - if (is_scalar($this->value)) { |
|
| 83 | - return $this->value; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - return false; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - protected function buildFilter() |
|
| 90 | - { |
|
| 91 | - if (is_null($this->filter) || is_object($this->filter)) { |
|
| 92 | - return; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - if (is_string($this->filter)) { |
|
| 96 | - $this->filter = [ |
|
| 97 | - 'class' => $this->filter, |
|
| 98 | - 'name' => $this->value, |
|
| 99 | - ]; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - if (empty($this->filter['class'])) { |
|
| 103 | - $this->filter['class'] = TextFilter::class; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - if (empty($this->filter['name'])) { |
|
| 107 | - $this->filter['name'] = $this->value; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - $className = GridViewHelper::resolveAlias('filter', $this->filter['class']); |
|
| 111 | - $this->filter = new $className($this->filter); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * @return array |
|
| 116 | - */ |
|
| 117 | - protected function configTests(): array |
|
| 118 | - { |
|
| 119 | - return [ |
|
| 120 | - 'title' => 'string', |
|
| 121 | - 'value' => 'any', |
|
| 122 | - 'headerHtmlOptions' => 'array', |
|
| 123 | - 'contentHtmlOptions' => 'array', |
|
| 124 | - 'formatters' => 'array', |
|
| 125 | - 'emptyValue' => 'string', |
|
| 126 | - 'sortable' => 'any', |
|
| 127 | - 'filter' => BaseFilter::class . '|null', |
|
| 128 | - ]; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Formatted header html options |
|
| 133 | - * @return string |
|
| 134 | - */ |
|
| 135 | - public function compileHeaderHtmlOptions() : string |
|
| 136 | - { |
|
| 137 | - return GridViewHelper::htmlOptionsToString($this->headerHtmlOptions); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Formatted content html options |
|
| 142 | - * @param array $context |
|
| 143 | - * @return string |
|
| 144 | - */ |
|
| 145 | - public function compileContentHtmlOptions(array $context) : string |
|
| 146 | - { |
|
| 147 | - return GridViewHelper::htmlOptionsToString($this->contentHtmlOptions, $context); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Render column value for row |
|
| 152 | - * @param array|object $row |
|
| 153 | - * @return string|mixed |
|
| 154 | - */ |
|
| 155 | - protected abstract function _renderValue($row); |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Renders column content |
|
| 159 | - * @param $row |
|
| 160 | - * @return string |
|
| 161 | - */ |
|
| 162 | - public function renderValue($row) |
|
| 163 | - { |
|
| 164 | - $value = $this->_renderValue($row); |
|
| 165 | - |
|
| 166 | - foreach ($this->formatters as $formatter) { |
|
| 167 | - $className = GridViewHelper::resolveAlias('formatter', $formatter); |
|
| 168 | - $value = (new $className)->format($value); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - return $value; |
|
| 172 | - } |
|
| 12 | + use Configurable; |
|
| 13 | + |
|
| 14 | + /** |
|
| 15 | + * Column title |
|
| 16 | + * @var string |
|
| 17 | + */ |
|
| 18 | + public $title = ''; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * Column value. Could be an attribute, |
|
| 22 | + * @var string|mixed |
|
| 23 | + */ |
|
| 24 | + public $value = ''; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @var BaseFilter |
|
| 28 | + */ |
|
| 29 | + public $filter; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var boolean|string |
|
| 33 | + */ |
|
| 34 | + public $sortable = true; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @var array |
|
| 38 | + */ |
|
| 39 | + public $headerHtmlOptions = []; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @var array |
|
| 43 | + */ |
|
| 44 | + public $contentHtmlOptions = []; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @var array - allowed: raw, url, email, text, image |
|
| 48 | + */ |
|
| 49 | + public $formatters = ['text']; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Value when column is empty |
|
| 53 | + * @var string |
|
| 54 | + */ |
|
| 55 | + public $emptyValue = '-'; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * BaseColumn constructor. |
|
| 59 | + * @param array $config |
|
| 60 | + * @throws \Woo\GridView\Exceptions\GridViewConfigException |
|
| 61 | + */ |
|
| 62 | + public function __construct(array $config) |
|
| 63 | + { |
|
| 64 | + $this->loadConfig($config); |
|
| 65 | + |
|
| 66 | + $this->buildFilter(); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Allows to get sortable column's name |
|
| 71 | + */ |
|
| 72 | + public function getSortableName() |
|
| 73 | + { |
|
| 74 | + if ($this->sortable === false) { |
|
| 75 | + return false; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + if (is_scalar($this->sortable)) { |
|
| 79 | + return $this->sortable; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + if (is_scalar($this->value)) { |
|
| 83 | + return $this->value; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + return false; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + protected function buildFilter() |
|
| 90 | + { |
|
| 91 | + if (is_null($this->filter) || is_object($this->filter)) { |
|
| 92 | + return; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + if (is_string($this->filter)) { |
|
| 96 | + $this->filter = [ |
|
| 97 | + 'class' => $this->filter, |
|
| 98 | + 'name' => $this->value, |
|
| 99 | + ]; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + if (empty($this->filter['class'])) { |
|
| 103 | + $this->filter['class'] = TextFilter::class; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + if (empty($this->filter['name'])) { |
|
| 107 | + $this->filter['name'] = $this->value; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + $className = GridViewHelper::resolveAlias('filter', $this->filter['class']); |
|
| 111 | + $this->filter = new $className($this->filter); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * @return array |
|
| 116 | + */ |
|
| 117 | + protected function configTests(): array |
|
| 118 | + { |
|
| 119 | + return [ |
|
| 120 | + 'title' => 'string', |
|
| 121 | + 'value' => 'any', |
|
| 122 | + 'headerHtmlOptions' => 'array', |
|
| 123 | + 'contentHtmlOptions' => 'array', |
|
| 124 | + 'formatters' => 'array', |
|
| 125 | + 'emptyValue' => 'string', |
|
| 126 | + 'sortable' => 'any', |
|
| 127 | + 'filter' => BaseFilter::class . '|null', |
|
| 128 | + ]; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Formatted header html options |
|
| 133 | + * @return string |
|
| 134 | + */ |
|
| 135 | + public function compileHeaderHtmlOptions() : string |
|
| 136 | + { |
|
| 137 | + return GridViewHelper::htmlOptionsToString($this->headerHtmlOptions); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Formatted content html options |
|
| 142 | + * @param array $context |
|
| 143 | + * @return string |
|
| 144 | + */ |
|
| 145 | + public function compileContentHtmlOptions(array $context) : string |
|
| 146 | + { |
|
| 147 | + return GridViewHelper::htmlOptionsToString($this->contentHtmlOptions, $context); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Render column value for row |
|
| 152 | + * @param array|object $row |
|
| 153 | + * @return string|mixed |
|
| 154 | + */ |
|
| 155 | + protected abstract function _renderValue($row); |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Renders column content |
|
| 159 | + * @param $row |
|
| 160 | + * @return string |
|
| 161 | + */ |
|
| 162 | + public function renderValue($row) |
|
| 163 | + { |
|
| 164 | + $value = $this->_renderValue($row); |
|
| 165 | + |
|
| 166 | + foreach ($this->formatters as $formatter) { |
|
| 167 | + $className = GridViewHelper::resolveAlias('formatter', $formatter); |
|
| 168 | + $value = (new $className)->format($value); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + return $value; |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | 174 | } |