DefaultController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: NODELOG
  5. * Date: 16/7/19
  6. * Time: 上午12:12
  7. */
  8. namespace common\modules\theme\backend\controllers;
  9. use backend\models\ThemezipForm;
  10. use Distill\Distill;
  11. use Yii;
  12. use yii\data\ArrayDataProvider;
  13. use yii\filters\VerbFilter;
  14. use yii\web\Controller;
  15. use yii\web\UploadedFile;
  16. class DefaultController extends Controller
  17. {
  18. public function behaviors()
  19. {
  20. return [
  21. 'verbs' => [
  22. 'class' => VerbFilter::className(),
  23. 'actions' => [
  24. 'open' => ['post'],
  25. ],
  26. ],
  27. ];
  28. }
  29. public function actionIndex()
  30. {
  31. $packages = Yii::$app->themeManager->findAll();
  32. $dataProvider = new ArrayDataProvider([
  33. "allModels" => $packages,
  34. ]);
  35. return $this->render('index', [
  36. 'dataProvider' => $dataProvider
  37. ]);
  38. }
  39. public function actionView($id)
  40. {
  41. $theme = \Yii::$app->themeManager->findOne($id);
  42. return $this->render("view", [
  43. "model" => $theme
  44. ]);
  45. }
  46. public function actionOpen($id)
  47. {
  48. $themeManager = \Yii::$app->themeManager;
  49. $theme = $themeManager->findOne($id);
  50. if ($theme != null) {
  51. if ($themeManager->setDefaultTheme($id) == true) {
  52. Yii::$app->session->setFlash("success", "设置主题成功,当前主题为" . $id);
  53. } else {
  54. Yii::$app->session->setFlash("error", "设置主题失败");
  55. }
  56. }
  57. return $this->redirect([
  58. "index"
  59. ]);
  60. }
  61. public function actionDemo($id)
  62. {
  63. $url = Yii::$app->config->get('site_url') . '?theme=' . $id;
  64. return $this->redirect($url);
  65. }
  66. public function actionCustom()
  67. {
  68. Yii::$app->session->setFlash("error", "暂未开放");
  69. return $this->redirect([
  70. "index"
  71. ]);
  72. }
  73. public function actionUpload()
  74. {
  75. $model = new ThemezipForm();
  76. if (\Yii::$app->getRequest()->getIsPost() == true && ($uploaded = UploadedFile::getInstance($model, "themezip")) != null) {
  77. $distill = new Distill();
  78. $themePath = Yii::getAlias(\Yii::$app->get("themeManager")->getThemePath());
  79. $extractFileName = $themePath . DIRECTORY_SEPARATOR . $uploaded->name;
  80. $pathinfo = pathinfo($extractFileName);
  81. $target = $pathinfo['dirname']. DIRECTORY_SEPARATOR . $pathinfo['filename'];
  82. if(is_dir($target)) {
  83. Yii::$app->session->setFlash("error", "主题路径已经存在该主题ID");
  84. } else {
  85. if (move_uploaded_file($uploaded->tempName, $extractFileName) == true) {
  86. try {
  87. if ($distill->extract($extractFileName, $themePath)) {
  88. $newTheme = \Yii::$app->get("themeManager")->findByPath($target . DIRECTORY_SEPARATOR . $uploaded->getBaseName());
  89. if (count($newTheme) === 1) {
  90. Yii::$app->session->setFlash("success", "上传主题文件成功");
  91. } else {
  92. Yii::$app->session->setFlash("error", "上传主题配置文件不存在或者上传主题有错误");
  93. }
  94. } else {
  95. throw new \Exception('解压文件失败');
  96. }
  97. }catch (\Exception $e) {
  98. Yii::$app->session->setFlash("error", "解压文件失败");
  99. }
  100. } else {
  101. Yii::$app->session->setFlash("error", "移动文件失败,请确定你的临时目录是可写的");
  102. }
  103. }
  104. if (file_exists($uploaded->tempName)) {
  105. unlink($uploaded->tempName);
  106. }
  107. if (file_exists($extractFileName)) {
  108. unlink($extractFileName);
  109. }
  110. return $this->refresh();
  111. }
  112. return $this->render('upload', [
  113. "model" => $model
  114. ]);
  115. }
  116. }