itstructure /
yii2-admin-module
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Itstructure\AdminModule\components; |
||
| 4 | |||
| 5 | use yii\web\View; |
||
| 6 | use yii\base\InvalidConfigException; |
||
| 7 | use Itstructure\AdminModule\assets\AdminAsset; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Class AdminView |
||
| 11 | * Default view for the Admin View |
||
| 12 | * |
||
| 13 | * @property array $mainMenuConfig Sidebar menu config. |
||
| 14 | * @property string $bodyLayout Admin layout type. |
||
| 15 | * @property string $skin Admin layout theme. By default are used blue skin. |
||
| 16 | * @property null|array|string $assetBundleConfig AssetBundle config. |
||
| 17 | * @property string $homeUrl Home URL. |
||
| 18 | * @property array $extraAssets An array of extra asset. Each asset can be specified one of the following format: |
||
| 19 | * - a string thar represent a class name of extra asset; |
||
| 20 | * - an array that must contain a class key and may contain other settings of asset bundle. |
||
| 21 | * @see BaseYii::createObject() |
||
| 22 | * @property string[] $userBody This array contain a key->value pairs where key - is link name and value is link |
||
| 23 | * that will be rendered in "user-body" section of menu. |
||
| 24 | * @property string $defaultAssetBundleClass Asset bundle class-name using by default for admin view. |
||
| 25 | * @property string $companyName Company name. |
||
| 26 | * @property string $shotCompanyName Short company name. |
||
| 27 | * @property string $profileLink Link to user profile. |
||
| 28 | * @property string $signOutLink Link to sign-out action. |
||
| 29 | * |
||
| 30 | * @package Itstructure\AdminModule\components |
||
| 31 | * |
||
| 32 | * @author Andrey Girnik <[email protected]> |
||
| 33 | */ |
||
| 34 | class AdminView extends View |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * Layouts. |
||
| 38 | */ |
||
| 39 | const LAYOUT_FIXED = 'fixed'; |
||
| 40 | const LAYOUT_SIDEBAR_MINI = 'sidebar-mini'; |
||
| 41 | const LAYOUT_SIDEBAR_COLLAPSE = 'sidebar-collapse'; |
||
| 42 | const LAYOUT_BOXED = 'layout-boxed'; |
||
| 43 | const LAYOUT_TOP_NAV = 'layout-top-nav'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Skins. |
||
| 47 | */ |
||
| 48 | const SKIN_BLUE = 'skin-blue'; |
||
| 49 | const SKIN_BLUE_LIGHT = 'skin-blue-light'; |
||
| 50 | const SKIN_YELLOW = 'skin-yellow'; |
||
| 51 | const SKIN_YELLOW_LIGHT = 'skin-yellow-light'; |
||
| 52 | const SKIN_GREEN = 'skin-green'; |
||
| 53 | const SKIN_GREEN_LIGHT = 'skin-green-light'; |
||
| 54 | const SKIN_PURPLE = 'skin-purple'; |
||
| 55 | const SKIN_PURPLE_LIGHT = 'skin-purple-light'; |
||
| 56 | const SKIN_RED = 'skin-red'; |
||
| 57 | const SKIN_RED_LIGHT = 'skin-red-light'; |
||
| 58 | const SKIN_BLACK = 'skin-black'; |
||
| 59 | const SKIN_BLACK_LIGHT = 'skin-black-light'; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Sidebar menu config. |
||
| 63 | * |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | public $mainMenuConfig = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Admin layout type. |
||
| 70 | * |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | public $bodyLayout = self::LAYOUT_SIDEBAR_MINI; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Admin layout theme. By default are used blue skin. |
||
| 77 | * |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | public $skin = self::SKIN_BLUE; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * AssetBundle config. |
||
| 84 | * |
||
| 85 | * @var null|array|string |
||
| 86 | */ |
||
| 87 | public $assetBundleConfig = null; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Home URL. |
||
| 91 | * |
||
| 92 | * @var string |
||
| 93 | */ |
||
| 94 | public $homeUrl = '/admin'; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * An array of extra asset. Each asset can be specified one of the following format: |
||
| 98 | * - a string thar represent a class name of extra asset; |
||
| 99 | * - an array that must contain a class key and may contain other settings of asset bundle. |
||
| 100 | * |
||
| 101 | * @see BaseYii::createObject() |
||
| 102 | * |
||
| 103 | * @var array |
||
| 104 | */ |
||
| 105 | public $extraAssets = []; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * This array contain a key->value pairs where key - is link name and value is link |
||
| 109 | * that will be rendered in "user-body" section of menu. |
||
| 110 | * |
||
| 111 | * @var string[] |
||
| 112 | */ |
||
| 113 | public $userBody = []; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Asset bundle class-name using by default for admin view. |
||
| 117 | * |
||
| 118 | * @var string |
||
| 119 | */ |
||
| 120 | private $defaultAssetBundleClass = AdminAsset::class; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Company name. |
||
| 124 | * |
||
| 125 | * @var string |
||
| 126 | */ |
||
| 127 | public $companyName = 'Company'; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Short company name. |
||
| 131 | * |
||
| 132 | * @var string |
||
| 133 | */ |
||
| 134 | public $shotCompanyName = ''; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Link to user profile. |
||
| 138 | * |
||
| 139 | * @var string |
||
| 140 | */ |
||
| 141 | public $profileLink = '/profile'; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Link to sign-out action. |
||
| 145 | * |
||
| 146 | * @var string |
||
| 147 | */ |
||
| 148 | public $signOutLink = '/site/logout'; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Initializes the object. |
||
| 152 | * |
||
| 153 | * @return void |
||
| 154 | */ |
||
| 155 | public function init(): void |
||
| 156 | { |
||
| 157 | $this->registerAdminAsset(); |
||
| 158 | $this->registerExtraAssets(); |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Register a main admin asset. |
||
| 163 | * |
||
| 164 | * @return void |
||
| 165 | */ |
||
| 166 | private function registerAdminAsset(): void |
||
| 167 | { |
||
| 168 | $assetConfig = $this->getAdminAssetConfig(); |
||
| 169 | $this->registerAsset($assetConfig); |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Register an extra assets. |
||
| 174 | * |
||
| 175 | * @return void |
||
| 176 | */ |
||
| 177 | private function registerExtraAssets(): void |
||
| 178 | { |
||
| 179 | foreach ($this->extraAssets as $asset) { |
||
| 180 | $assetConfig = $this->getExtraAssetConfig($asset); |
||
| 181 | $this->registerAsset($assetConfig); |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Register an asset bundle in view. |
||
| 187 | * |
||
| 188 | * @param array $assetConfig config of asset bundle. |
||
| 189 | * |
||
| 190 | * @return void |
||
| 191 | */ |
||
| 192 | private function registerAsset(array $assetConfig): void |
||
| 193 | { |
||
| 194 | $assetClassName = $assetConfig['class']; |
||
| 195 | $this->assetManager->bundles[$assetClassName] = $assetConfig; |
||
| 196 | if (method_exists($assetClassName, 'register')) { |
||
| 197 | call_user_func([ |
||
| 198 | $assetClassName, |
||
| 199 | 'register', |
||
| 200 | ], $this); |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Return config array for create instance of AssetBundle. |
||
| 206 | * |
||
| 207 | * @return array |
||
| 208 | */ |
||
| 209 | private function getAdminAssetConfig(): array |
||
| 210 | { |
||
| 211 | $config = $this->assetBundleConfig; |
||
| 212 | if (null === $config) { |
||
| 213 | $config = []; |
||
| 214 | } |
||
| 215 | if (is_string($config)) { |
||
| 216 | $config['class'] = $config; |
||
| 217 | } |
||
| 218 | if (is_array($config) && !array_key_exists('class', $config)) { |
||
| 219 | $config['class'] = $this->defaultAssetBundleClass; |
||
| 220 | if (array_key_exists('skin', $config)) { |
||
| 221 | $config['skin'] = $this->skin; |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | return $config; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Prepare and return an extra asset config. |
||
| 230 | * |
||
| 231 | * @param string|array $asset extra asset config. |
||
| 232 | * |
||
| 233 | * @throws InvalidConfigException if the configuration is invalid. |
||
| 234 | * |
||
| 235 | * @return array |
||
| 236 | */ |
||
| 237 | private function getExtraAssetConfig($asset): array |
||
| 238 | { |
||
| 239 | $config = []; |
||
| 240 | if (is_string($asset)) { |
||
| 241 | $config['class'] = $asset; |
||
| 242 | } else { |
||
| 243 | $config = $asset; |
||
| 244 | } |
||
| 245 | if (false === array_key_exists('class', $config)) { |
||
| 246 | throw new InvalidConfigException('Object configuration must be an array containing a "class" element.'); |
||
| 247 | } |
||
| 248 | |||
| 249 | return $config; |
||
| 250 | } |
||
| 251 | } |
||
| 252 |