Position.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * @link http://quartsoft.com/
  4. * @copyright Copyright &copy; 2015 QuartSoft ltd.
  5. * @license http://www.quartsoft.com/license/
  6. */
  7. namespace backend\actions;
  8. use Yii;
  9. use yii\web\BadRequestHttpException;
  10. use yii\web\MethodNotAllowedHttpException;
  11. use yii\web\Response;
  12. /**
  13. * Position action switches custom sorting position of the existing record.
  14. * This action supports [yii2tech/ar-position](https://github.com/yii2tech/ar-position) extension.
  15. *
  16. * @see https://github.com/yii2tech/ar-position
  17. *
  18. * @author Paul Klimov <klimov.paul@gmail.com>
  19. * @since 1.0
  20. */
  21. class Position extends Action
  22. {
  23. /**
  24. * @var string name of the query param, which is used for new model position specification.
  25. */
  26. public $positionParam = 'at';
  27. /**
  28. * Updates existing record specified by id.
  29. * @param mixed $id id of the model to be deleted.
  30. * @return mixed response.
  31. * @throws BadRequestHttpException on invalid request.
  32. * @throws MethodNotAllowedHttpException on invalid request.
  33. */
  34. public function run($id)
  35. {
  36. if (!Yii::$app->request->isPost) {
  37. throw new MethodNotAllowedHttpException('Method Not Allowed. This url can only handle post');
  38. }
  39. $position = Yii::$app->request->getQueryParam($this->positionParam, null);
  40. if (empty($position)) {
  41. throw new BadRequestHttpException(Yii::t('yii', '{attribute} cannot be blank.', ['attribute' => $this->positionParam]));
  42. }
  43. $model = $this->findModel($id);
  44. $this->positionModel($model, $position);
  45. return $this->respondSuccess($model);
  46. }
  47. /**
  48. * @param \yii\db\ActiveRecordInterface|\backend\behaviors\PositionBehavior $model
  49. * @param $position
  50. * @throws BadRequestHttpException
  51. */
  52. protected function positionModel($model, $position)
  53. {
  54. switch (strtolower($position)) {
  55. case 'up':
  56. case 'prev':
  57. $model->movePrev();
  58. break;
  59. case 'down':
  60. case 'next':
  61. $model->moveNext();
  62. break;
  63. case 'top':
  64. case 'first':
  65. $model->moveFirst();
  66. break;
  67. case 'bottom':
  68. case 'last':
  69. $model->moveLast();
  70. break;
  71. default:
  72. if (is_numeric($position)) {
  73. $model->moveToPosition($position);
  74. } else {
  75. throw new BadRequestHttpException(Yii::t('yii', '{attribute} is invalid.', ['attribute' => $this->positionParam]));
  76. }
  77. }
  78. }
  79. /**
  80. * Composes success response.
  81. * @param \yii\db\ActiveRecordInterface $model
  82. * @return mixed response.
  83. */
  84. protected function respondSuccess($model)
  85. {
  86. if (Yii::$app->request->isAjax) {
  87. Yii::$app->response->format = Response::FORMAT_JSON;
  88. return [
  89. 'success' => true
  90. ];
  91. }
  92. return $this->controller->redirect($this->createReturnUrl('view', $model));
  93. }
  94. /**
  95. * @inheritdoc
  96. */
  97. public function createReturnUrl($defaultActionId = 'index', $model = null)
  98. {
  99. if ($this->returnUrl !== null) {
  100. return parent::createReturnUrl($defaultActionId, $model);
  101. }
  102. $url = parent::createReturnUrl($defaultActionId, $model);
  103. unset($url[$this->positionParam]);
  104. return $url;
  105. }
  106. }