DocsController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. namespace manager\controllers;
  3. use manager\models\Docs;
  4. use yii\data\ActiveDataProvider;
  5. use yii\web\Controller;
  6. use yii\web\NotFoundHttpException;
  7. use yii\filters\VerbFilter;
  8. use Yii;
  9. use dosamigos\fileupload\FileUpload;
  10. use yii\web\UploadedFile;
  11. use yii\helpers\FileHelper;
  12. use yii\helpers\BaseFileHelper;
  13. use yii\helpers\Json;
  14. /**
  15. * PageController implements the CRUD actions for Page model.
  16. */
  17. class DocsController extends BaseController
  18. {
  19. /**
  20. * @inheritDoc
  21. */
  22. public function behaviors()
  23. {
  24. return array_merge(
  25. parent::behaviors(),
  26. [
  27. 'verbs' => [
  28. 'class' => VerbFilter::className(),
  29. 'actions' => [
  30. // 'delete' => ['POST'],
  31. ],
  32. ],
  33. ]
  34. );
  35. }
  36. /**
  37. * Lists all Page models.
  38. *
  39. * @return string
  40. */
  41. public function actionIndex()
  42. {
  43. $model = new Docs();
  44. return $this->render('index', [
  45. 'model' => $model
  46. ]);
  47. }
  48. /**
  49. * Displays a single Page model.
  50. * @param int $id ID
  51. * @return string
  52. * @throws NotFoundHttpException if the model cannot be found
  53. */
  54. public function actionView($id)
  55. {
  56. return $this->render('view', [
  57. 'model' => $this->findModel($id),
  58. ]);
  59. }
  60. public function actionUploads()
  61. {
  62. return $this->render('uploads', [
  63. 'model' => new Docs(),
  64. ]);
  65. }
  66. /**
  67. * Creates a new Page model.
  68. * If creation is successful, the browser will be redirected to the 'view' page.
  69. * @return string|\yii\web\Response
  70. */
  71. public function actionCreate()
  72. {
  73. $model = new Docs();
  74. if ($this->request->isPost) {
  75. if ($model->load($this->request->post()) && $model->save()) {
  76. return $this->redirect(['view', 'id' => $model->id]);
  77. }else{
  78. // return json_encode( $model->getErrors() );
  79. }
  80. } else {
  81. $model->loadDefaultValues();
  82. }
  83. return $this->render('create', [
  84. 'model' => $model,
  85. ]);
  86. }
  87. /**
  88. * Updates an existing Page model.
  89. * If update is successful, the browser will be redirected to the 'view' page.
  90. * @param int $id ID
  91. * @return string|\yii\web\Response
  92. * @throws NotFoundHttpException if the model cannot be found
  93. */
  94. public function actionUpdate($id)
  95. {
  96. $model = $this->findModel($id);
  97. if ($this->request->isPost && $model->load($this->request->post()) && $model->save()) {
  98. Yii::$app->cache->delete("static_pagesx");
  99. return $this->redirect(['view', 'id' => $model->id]);
  100. }
  101. return $this->render('update', [
  102. 'model' => $model,
  103. ]);
  104. }
  105. /**
  106. * Deletes an existing Page model.
  107. * If deletion is successful, the browser will be redirected to the 'index' page.
  108. * @param int $id ID
  109. * @return \yii\web\Response
  110. * @throws NotFoundHttpException if the model cannot be found
  111. */
  112. public function actionDelete($id)
  113. {
  114. if( is_numeric($id) ){
  115. $p = $this->findModel($id);
  116. if( $p ){
  117. $uid = $p->uid;
  118. $cat = $p->GetCat();
  119. $p->delete();
  120. $f = $cat. DIRECTORY_SEPARATOR .$uid;
  121. if( $uid && file_exists( $f ) ){
  122. unlink($f);
  123. }
  124. $ext = pathinfo($f, PATHINFO_EXTENSION);
  125. $ft = str_replace( '.'.$ext, '.webp', $f );
  126. if( $uid && file_exists( $ft ) ){
  127. unlink($ft);
  128. }
  129. }
  130. }else{
  131. $m = new Docs();
  132. $cat = $m->GetCat();
  133. $f = $cat. DIRECTORY_SEPARATOR .$id;
  134. if( file_exists( $f ) ){
  135. unlink( $f );
  136. }
  137. $ext = pathinfo($f, PATHINFO_EXTENSION);
  138. $ft = str_replace( '.'.$ext, '.webp', $f );
  139. if( file_exists( $ft ) ){
  140. unlink($ft);
  141. }
  142. }
  143. $cat = Yii::$app->request->get('cat', '');
  144. if( $cat ) return $this->redirect(['index', 'cat'=>$cat]);
  145. return $this->redirect(['index']);
  146. }
  147. public function actionUpdatetitle()
  148. {
  149. if ($this->request->isPost ) {
  150. // $this->request->enableCsrfValidation = false;
  151. if( $post && isset($post['uid']) && isset($post['title']) ){
  152. $uid = str_replace( '_', '.', $post['uid'] );
  153. $model = Docs::findOne(['uid' => $uid]);
  154. if( !$model ){
  155. $model = new Docs();
  156. $model->text = '';
  157. $model->uid = $uid;
  158. }
  159. $model->title = $post['title'];
  160. if( $model->save() ){
  161. return json_encode(['status'=>'ok','item'=>$uid]);
  162. }else{
  163. return json_encode(['status'=>'err','text'=>'Ошибка сохранения']);
  164. }
  165. }else{
  166. return json_encode(['status'=>'err','text'=>'Ошибка данных']);
  167. }
  168. }
  169. return json_encode(['status'=>'err', 'text'=>'Ошибка передачи данных']);
  170. }
  171. public function actionDocsupload()
  172. {
  173. $model = new Docs();
  174. $imageFile = UploadedFile::getInstance($model, 'file');
  175. $directory = Yii::getAlias('@webroot/images/tmp') . DIRECTORY_SEPARATOR . Yii::$app->session->id . DIRECTORY_SEPARATOR;
  176. if (!is_dir($directory)) {
  177. FileHelper::createDirectory($directory);
  178. }
  179. if ($imageFile) {
  180. $cat = Yii::$app->request->get('cat', '');
  181. $uid = uniqid(time(), true);
  182. $fileName = $uid . '.' . $imageFile->extension;
  183. $filePath = $directory . $fileName;
  184. if ($imageFile->saveAs($filePath)) {
  185. $path = '/images/tmp/' . Yii::$app->session->id . DIRECTORY_SEPARATOR . $fileName;
  186. rename( $filePath, $model->GetCat() . DIRECTORY_SEPARATOR . $fileName );
  187. $model = new Docs();
  188. $model->title = $imageFile->baseName. '.' . $imageFile->extension;
  189. $model->uid = $fileName;
  190. $model->text = '';
  191. $model->cat = $cat;
  192. $model->save();
  193. return Json::encode([
  194. 'files' => [
  195. [
  196. 'name' => $model->GetUrl() . DIRECTORY_SEPARATOR . $fileName,
  197. 'oname' => $imageFile->baseName. '.' . $imageFile->extension,
  198. 'size' => $imageFile->size,
  199. 'url' => $path,
  200. 'thumbnailUrl' => $path,
  201. 'deleteUrl' => 'image-delete?name=' . $fileName,
  202. 'deleteType' => 'POST',
  203. ],
  204. ],
  205. ]);
  206. }
  207. }
  208. return Json::encode( [ 'error' => 'ok' ] );
  209. }
  210. public function actionCatcreate()
  211. {
  212. $model = new Docs();
  213. if ($this->request->isPost && $cat = Yii::$app->request->post('Docs') ) {
  214. if ( isset($cat['cat']) && $model->CatCreate( $cat['cat'] ) ) {
  215. return $this->redirect(['index']);
  216. }
  217. }
  218. return $this->render('catcreate', [
  219. 'model' => $model,
  220. ]);
  221. }
  222. /**
  223. * Finds the Page model based on its primary key value.
  224. * If the model is not found, a 404 HTTP exception will be thrown.
  225. * @param int $id ID
  226. * @return Page the loaded model
  227. * @throws NotFoundHttpException if the model cannot be found
  228. */
  229. protected function findModel($id)
  230. {
  231. if (($model = Docs::findOne(['id' => $id])) !== null) {
  232. return $model;
  233. }
  234. throw new NotFoundHttpException('The requested page does not exist.');
  235. }
  236. }