Nav.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace common\models;
  3. use common\helpers\Util;
  4. use Yii;
  5. /**
  6. * This is the model class for table "{{%nav}}".
  7. *
  8. * @property integer $id
  9. * @property string $key
  10. */
  11. class Nav extends \yii\db\ActiveRecord
  12. {
  13. /**
  14. * @inheritdoc
  15. */
  16. public static function tableName()
  17. {
  18. return '{{%nav}}';
  19. }
  20. /**
  21. * @inheritdoc
  22. */
  23. public function rules()
  24. {
  25. return [
  26. [['key', 'title'], 'required'],
  27. [['key', 'title'], 'string', 'max' => 128],
  28. ];
  29. }
  30. /**
  31. * @inheritdoc
  32. */
  33. public function attributeLabels()
  34. {
  35. return [
  36. 'id' => Yii::t('common', 'ID'),
  37. 'key' => Yii::t('common', 'Key'),
  38. 'title' => Yii::t('common', 'Title'),
  39. ];
  40. }
  41. public function getActiveItem()
  42. {
  43. return $this->hasMany(NavItem::className(), ['nav_id' => 'id'])->where(['status' => 1]);
  44. }
  45. public static function getItems($key)
  46. {
  47. $nav = self::find()->where(['key' => $key])->one();
  48. if ($nav == null) {
  49. return [];
  50. }
  51. $items = NavItem::find()->select('title label, url, target')
  52. ->where(['nav_id' => $nav->id, 'status' => 1])
  53. ->orderBy(['order' => SORT_ASC])
  54. ->asArray()->all();
  55. return array_map(function($value){
  56. $value['url'] = Util::parseUrl($value['url']);
  57. if ($value['target'] == 1) {
  58. $value['linkOptions'] = ['target' => '_blank'];
  59. }
  60. return $value;
  61. }, $items);
  62. }
  63. }