PositionColumn.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /**
  3. * @link https://github.com/yii2tech
  4. * @copyright Copyright (c) 2015 Yii2tech
  5. * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
  6. */
  7. namespace backend\widgets\grid;
  8. use Yii;
  9. use yii\base\InvalidConfigException;
  10. use yii\bootstrap\Html;
  11. use yii\grid\DataColumn;
  12. use yii\helpers\ArrayHelper;
  13. use yii\helpers\Url;
  14. /**
  15. * PositionColumn renders controls for the custom sorting position switching, provided by [yii2tech/ar-position](https://github.com/yii2tech/ar-position) extension.
  16. *
  17. * @see https://github.com/yii2tech/ar-position
  18. *
  19. * @author Paul Klimov <klimov.paul@gmail.com>
  20. * @since 1.0
  21. */
  22. class PositionColumn extends DataColumn
  23. {
  24. /**
  25. * @inheritdoc
  26. */
  27. public $headerOptions = ['class' => 'position-column'];
  28. /**
  29. * @var string the template that is used to render the content in each cell.
  30. * These default tokens are recognized: {first}, {prev}, {next}, {last} and {value}.
  31. */
  32. public $template = '{first}&nbsp;{prev}&nbsp;{value}&nbsp;{next}&nbsp;{last}';
  33. /**
  34. * @var array configuration for the switch position buttons.
  35. */
  36. public $buttons = [];
  37. /**
  38. * @var array html options to be applied to the [[initDefaultButtons()|default buttons]].
  39. */
  40. public $buttonOptions = ["class"=>"btn btn-default btn-xs"];
  41. /**
  42. * @var string route to the action, which should process position switching, for example: 'item/position'.
  43. */
  44. public $route = 'position';
  45. /**
  46. * @var string name of the query param, which is used for new position specification.
  47. */
  48. public $positionParam = 'at';
  49. /**
  50. * @var callable a callback that creates a button URL using the specified model information.
  51. * The signature of the callback should be the same as that of [[createUrl()]].
  52. * If this property is not set, button URLs will be created using [[createUrl()]].
  53. */
  54. public $urlCreator;
  55. /**
  56. * @inheritdoc
  57. */
  58. public function init()
  59. {
  60. parent::init();
  61. $this->initDefaultButtons();
  62. }
  63. /**
  64. * Initializes the default buttons.
  65. */
  66. protected function initDefaultButtons()
  67. {
  68. $this->buttons = ArrayHelper::merge(
  69. [
  70. 'first' => [
  71. 'icon' => 'triangle-top',
  72. 'visible' => function ($model) {
  73. /* @var $model \yii\db\BaseActiveRecord */
  74. if ($this->attribute !== null && isset($model[$this->attribute])) {
  75. return $model[$this->attribute] > 1;
  76. }
  77. return true;
  78. },
  79. 'options' => [
  80. 'title' => 'Move top',
  81. 'aria-label' => 'Move top',
  82. 'data-method' => 'post'
  83. ],
  84. ],
  85. 'last' => [
  86. 'icon' => 'triangle-bottom',
  87. 'visible' => function ($model) {
  88. /* @var $model \yii\db\BaseActiveRecord */
  89. if ($this->attribute !== null && isset($model[$this->attribute])) {
  90. return $model[$this->attribute] < $this->grid->dataProvider->getTotalCount();
  91. }
  92. return true;
  93. },
  94. 'options' => [
  95. 'title' => 'Move bottom',
  96. 'aria-label' => 'Move bottom',
  97. 'data-method' => 'post'
  98. ],
  99. ],
  100. 'prev' => [
  101. 'icon' => 'arrow-up',
  102. 'visible' => function ($model) {
  103. /* @var $model \yii\db\BaseActiveRecord */
  104. if ($this->attribute !== null && isset($model[$this->attribute])) {
  105. return $model[$this->attribute] > 1;
  106. }
  107. return true;
  108. },
  109. 'options' => [
  110. 'title' => 'Move up',
  111. 'aria-label' => 'Move up',
  112. 'data-method' => 'post'
  113. ],
  114. ],
  115. 'next' => [
  116. 'icon' => 'arrow-down',
  117. 'visible' => function ($model) {
  118. /* @var $model \yii\db\BaseActiveRecord */
  119. if ($this->attribute !== null && isset($model[$this->attribute])) {
  120. return $model[$this->attribute] < $this->grid->dataProvider->getTotalCount();
  121. }
  122. return true;
  123. },
  124. 'options' => [
  125. 'title' => 'Move down',
  126. 'aria-label' => 'Move down',
  127. 'data-method' => 'post'
  128. ],
  129. ],
  130. ],
  131. $this->buttons
  132. );
  133. }
  134. /**
  135. * @inheritdoc
  136. */
  137. protected function renderDataCellContent($model, $key, $index)
  138. {
  139. if ($this->content === null) {
  140. return preg_replace_callback('/\\{([\w\-\/]+)\\}/', function ($matches) use ($model, $key, $index) {
  141. $name = $matches[1];
  142. if ($name === 'value') {
  143. return $this->grid->formatter->format($this->getDataCellValue($model, $key, $index), $this->format);
  144. }
  145. return $this->renderButton($name, $model, $key, $index);
  146. }, $this->template);
  147. } else {
  148. return parent::renderDataCellContent($model, $key, $index);
  149. }
  150. }
  151. /**
  152. * Renders button.
  153. * @param string $name button name.
  154. * @param mixed $model
  155. * @param string $key
  156. * @param integer $index
  157. * @return string rendered HTML
  158. * @throws InvalidConfigException on invalid button format.
  159. */
  160. protected function renderButton($name, $model, $key, $index)
  161. {
  162. if (!isset($this->buttons[$name])) {
  163. return '';
  164. }
  165. $button = $this->buttons[$name];
  166. if ($button instanceof \Closure) {
  167. $url = $this->createUrl($name, $model, $key, $index);
  168. return call_user_func($button, $url, $model, $key);
  169. }
  170. if (!is_array($button)) {
  171. throw new InvalidConfigException("Button should be either a Closure or array configuration.");
  172. }
  173. // Visibility :
  174. if (isset($button['visible'])) {
  175. if ($button['visible'] instanceof \Closure) {
  176. if (!call_user_func($button['visible'], $model, $key, $index)) {
  177. return '';
  178. }
  179. } elseif (!$button['visible']) {
  180. return '';
  181. }
  182. }
  183. // URL :
  184. if (isset($button['url'])) {
  185. $url = call_user_func($button['url'], $name, $model, $key, $index);
  186. } else {
  187. $url = $this->createUrl($name, $model, $key, $index);
  188. }
  189. // label :
  190. if (isset($button['label'])) {
  191. $label = $button['label'];
  192. if (isset($button['encode'])) {
  193. $encodeLabel = $button['encode'];
  194. unset($button['encode']);
  195. } else {
  196. $encodeLabel = true;
  197. }
  198. if ($encodeLabel) {
  199. $label = Html::encode($label);
  200. }
  201. } else {
  202. $label = '';
  203. }
  204. // icon :
  205. if (isset($button['icon'])) {
  206. $icon = $button['icon'];
  207. $label = Html::icon($icon) . (empty($label) ? '' : ' ' . $label);
  208. }
  209. $options = array_merge(ArrayHelper::getValue($button, 'options', []), $this->buttonOptions);
  210. return Html::a($label, $url, $options);
  211. }
  212. /**
  213. * Creates a URL for the given position and model.
  214. * This method is called for each button and each row.
  215. * @param string $position the position name
  216. * @param \yii\db\BaseActiveRecord $model the data model
  217. * @param mixed $key the key associated with the data model
  218. * @param integer $index the current row index
  219. * @return string the created URL
  220. */
  221. public function createUrl($position, $model, $key, $index)
  222. {
  223. if (is_callable($this->urlCreator)) {
  224. return call_user_func($this->urlCreator, $position, $model, $key, $index);
  225. } else {
  226. $params = array_merge(
  227. Yii::$app->getRequest()->getQueryParams(),
  228. is_array($key) ? $key : ['id' => (string) $key]
  229. );
  230. $params[$this->positionParam] = $position;
  231. $params[0] = $this->route;
  232. return Url::toRoute($params);
  233. }
  234. }
  235. }