/revisions/Revisions.php

https://gitlab.com/nitm/yii2-widgets · PHP · 140 lines · 116 code · 12 blank · 12 comment · 8 complexity · d0e7cbd0442c9d28fd99c2076e8074b8 MD5 · raw file

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace nitm\widgets\revisions;
  8. use Yii;
  9. use yii\base\InvalidConfigException;
  10. use nitm\helpers\Html;
  11. use yii\grid\GridView;
  12. use yii\data\ArrayDataProvider;
  13. use nitm\widgets\models\User;
  14. use nitm\widgets\models\Revisions as RevisionsModel;
  15. use nitm\widgets\BaseWidget;
  16. use nitm\helpers\ArrayHelper;
  17. use kartik\icons\Icon;
  18. class Revisions extends BaseWidget
  19. {
  20. /*
  21. * HTML options for generevision the widget
  22. */
  23. public $options = [
  24. 'class' => 'revision',
  25. 'role' => 'entityRevisions',
  26. 'id' => 'revision',
  27. ];
  28. /**
  29. * The actions that are supported
  30. */
  31. private $_actions = [
  32. 'view' => [
  33. 'tag' => 'span',
  34. 'action' => '/revisions/view',
  35. 'text' => 'eye',
  36. 'options' => [
  37. 'class' => 'col-md-4 col-lg-4',
  38. 'role' => 'viewRevision',
  39. 'id' => 'revision-view',
  40. 'title' => 'View this version',
  41. 'data-toggle' => 'modal',
  42. 'data-target' => '#revisions-view-modal'
  43. ]
  44. ],
  45. 'restore' => [
  46. 'tag' => 'span',
  47. 'action' => '/revisions/restore',
  48. 'text' => 'reply',
  49. 'options' => [
  50. 'class' => 'col-md-4 col-lg-4',
  51. 'role' => 'dynamicAction',
  52. 'id' => 'revision-restore',
  53. 'title' => 'Restore this version'
  54. ]
  55. ],
  56. 'delete' => [
  57. 'tag' => 'span',
  58. 'action' => '/revisions/delete',
  59. 'text' => 'trash-o',
  60. 'options' => [
  61. 'class' => 'col-lg-4 col-md-4',
  62. 'role' => 'dynamicAction',
  63. 'id' => 'revision-delete',
  64. 'title' => 'Delete this version'
  65. ]
  66. ],
  67. ];
  68. public function init()
  69. {
  70. switch(1)
  71. {
  72. case !($this->model instanceof RevisionsModel) && (($this->parentType == null) || ($this->parentId == null)):
  73. $this->model = null;
  74. break;
  75. default:
  76. $this->model = ($this->model instanceof RevisionsModel) ? $this->model : (new RevisionsModel(['initSearchClass' => false]))->findModel([$this->parentId, $this->parentType]);
  77. break;
  78. }
  79. parent::init();
  80. Asset::register($this->getView());
  81. }
  82. public function run()
  83. {
  84. $this->model->queryOptions['orderBy'] = ['id' => SORT_DESC];
  85. //$this->model->queryOptions['andWhere'] = array_merge(ArrayHelper::getValue($this->model->queryOptions, 'andWhere', []), ['disabled' => false]);
  86. $dataProvider = new \yii\data\ArrayDataProvider([
  87. "allModels" => (is_array($this->items) && !empty($this->items)) ? $this->items : $this->model->getModels(),
  88. 'pagination' => false,
  89. ]);
  90. if(!\Yii::$app->getUser()->getIdentity()->isAdmin())
  91. $this->model->queryOptions['andWhere']['disabled'] = false;
  92. $revisions = $this->render('@nitm/widgets/views/revisions/index', [
  93. 'dataProvider' => $dataProvider
  94. ]);
  95. $this->options['id'] .= $this->parentId;
  96. return Html::tag('div', $revisions, $this->options);
  97. }
  98. public function getActions()
  99. {
  100. $actions = is_null($this->actions) ? $this->_actions : array_intersect_key($this->_actions, $this->actions);
  101. $ret_val = '';
  102. foreach($actions as $name=>$action)
  103. {
  104. switch(isset($action['adminOnly']) && ($action['adminOnly'] == true))
  105. {
  106. case true:
  107. switch(\Yii::$app->userMeta->isAdmin())
  108. {
  109. case true:
  110. $action['options']['id'] = $action['options']['id'].$this->parentId;
  111. $ret_val[$name] = function ($url, $model) use($action) {
  112. return Html::a(Icon::show($action['text']), $action['action'].'/'.$model->getId(), $action['options']);
  113. };
  114. break;
  115. }
  116. break;
  117. default:
  118. $action['options']['id'] = $action['options']['id'].$this->parentId;
  119. $ret_val[$name] = function ($url, $model) use($action) {
  120. return Html::a(Icon::show($action['text']), $action['action'].'/'.$model->getId(), $action['options']);
  121. };
  122. break;
  123. }
  124. }
  125. return $ret_val;
  126. }
  127. }
  128. ?>