ListController.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace rss\controllers;
  3. use Yii;
  4. use yii\data\ArrayDataProvider;
  5. use yii\web\Controller;
  6. use yii\filters\VerbFilter;
  7. use yii\filters\AccessControl;
  8. /**
  9. * ListController implements the CRUD actions
  10. */
  11. class ListController extends Controller
  12. {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. public function behaviors()
  17. {
  18. $behaviors = [
  19. 'verbs' => [
  20. 'class' => VerbFilter::class,
  21. 'actions' => [
  22. 'index' => ['get'],
  23. ],
  24. ],
  25. 'access' => [
  26. 'class' => AccessControl::class,
  27. 'rules' => [
  28. [
  29. 'roles' => ['admin'],
  30. 'allow' => true
  31. ],
  32. ],
  33. ],
  34. ];
  35. // If auth manager not configured use default access control
  36. if(!Yii::$app->authManager) {
  37. $behaviors['access'] = [
  38. 'class' => AccessControl::class,
  39. 'rules' => [
  40. [
  41. 'roles' => ['@'],
  42. 'allow' => true
  43. ],
  44. ]
  45. ];
  46. }
  47. return $behaviors;
  48. }
  49. /**
  50. * Lists all turbo-pages.
  51. * @return mixed
  52. */
  53. public function actionIndex()
  54. {
  55. $module = $this->module;
  56. $dataProvider = new ArrayDataProvider([
  57. 'allModels' => $module->getTurboItems()
  58. ]);
  59. return $this->render('index', [
  60. 'module' => $module,
  61. 'dataProvider' => $dataProvider
  62. ]);
  63. }
  64. /**
  65. * Clear turbo-pages cache
  66. *
  67. * @return mixed
  68. */
  69. public function actionClear()
  70. {
  71. if ($cache = Yii::$app->getCache()) {
  72. if ($cache->delete(md5('yandex-turbo'))) {
  73. Yii::$app->getSession()->setFlash(
  74. 'success',
  75. Yii::t('app/modules/turbo', 'Turbo-pages cache has been successfully flushing!')
  76. );
  77. } else {
  78. Yii::$app->getSession()->setFlash(
  79. 'danger',
  80. Yii::t('app/modules/turbo', 'An error occurred while flushing the turbo-pages cache.')
  81. );
  82. }
  83. } else {
  84. Yii::$app->getSession()->setFlash(
  85. 'warning',
  86. Yii::t('app/modules/turbo', 'Error! Cache component not configured in the application.')
  87. );
  88. }
  89. return $this->redirect(['list/index']);
  90. }
  91. }