DefaultController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: NODELOG
  5. * Date: 2016/12/10
  6. * Time: 下午7:03
  7. */
  8. namespace common\modules\attachment\backend\controllers;
  9. use Yii;
  10. use common\modules\attachment\models\Attachment;
  11. use yii\imagine\Image;
  12. use common\components\Controller;
  13. use yii\web\NotFoundHttpException;
  14. use yii\data\ActiveDataProvider;
  15. class DefaultController extends Controller
  16. {
  17. /**
  18. * Lists all Gather models.
  19. * @return mixed
  20. */
  21. public function actionIndex()
  22. {
  23. $dataProvider = new ActiveDataProvider([
  24. 'query' => Attachment::find(),
  25. 'sort' => [
  26. 'defaultOrder' => [
  27. 'updated_at' => SORT_DESC,
  28. ]
  29. ]
  30. ]);
  31. return $this->render('index', [
  32. 'dataProvider' => $dataProvider,
  33. ]);
  34. }
  35. public function actionView($id)
  36. {
  37. $model = $this->findModel($id);
  38. if ($model->load(Yii::$app->request->post())) {
  39. if (!$model->save()) {
  40. $error = current($model->getFirstErrors());
  41. return $this->renderJson(0, $error);
  42. }
  43. }
  44. $content = $this->renderPartial('view', [
  45. 'model' => $model
  46. ]);
  47. return $this->renderJson(1, $content);
  48. }
  49. public function actionUpdate($id)
  50. {
  51. $model = $this->findModel($id);
  52. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  53. return $this->refresh();
  54. } else {
  55. return $this->render('update', [
  56. 'model' => $model,
  57. ]);
  58. }
  59. }
  60. public function actionUploader()
  61. {
  62. return $this->render("uploader");
  63. }
  64. public function actionCrop($id)
  65. {
  66. $model = $this->findModel($id);
  67. $type = \Yii::$app->getRequest()->post("type");
  68. $x = \Yii::$app->getRequest()->post("x");
  69. $y = \Yii::$app->getRequest()->post("y");
  70. $w = \Yii::$app->getRequest()->post("w");
  71. $h = \Yii::$app->getRequest()->post("h");
  72. $newModel = $model->makeCropStorage($w, $h, $x, $y);
  73. //删除原图
  74. $model->delete();
  75. $newModel->updateAttributes(['id' => $model->id]);
  76. return $this->renderJson(1, "裁剪成功");
  77. }
  78. public function actionDelete($id)
  79. {
  80. $this->findModel($id)->delete();
  81. if (Yii::$app->request->isAjax) {
  82. return $this->renderJson(1, '删除成功');
  83. }
  84. return $this->redirect(['index']);
  85. }
  86. /**
  87. * Finds the Attachment model based on its primary key value.
  88. * If the model is not found, a 404 HTTP exception will be thrown.
  89. *
  90. * @param integer $id
  91. * @return Attachment the loaded model
  92. * @throws NotFoundHttpException if the model cannot be found
  93. */
  94. protected function findModel($id)
  95. {
  96. if (($model = Attachment::findOne($id)) !== null) {
  97. return $model;
  98. } else {
  99. throw new NotFoundHttpException('The requested page does not exist.');
  100. }
  101. }
  102. }