Article.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace common\models;
  3. use backendApi\modules\v1\models\Admin;
  4. use backendApi\modules\v1\models\AdminCountry;
  5. use common\helpers\Tool;
  6. use Yii;
  7. /**
  8. * This is the model class for table "{{%ARTICLE}}".
  9. *
  10. * @property string $ID
  11. * @property string $COUNTRY_ID 国家ID
  12. * @property string $TITLE 标题
  13. * @property string $CID 分类ID
  14. * @property string $CONTENT 内容
  15. * @property int $STATUS 状态
  16. * @property int $SORT 排序值
  17. * @property int $CREATED_AT 创建时间
  18. */
  19. class Article extends \common\components\ActiveRecord
  20. {
  21. /**
  22. * @inheritdoc
  23. */
  24. public static function tableName()
  25. {
  26. return '{{%ARTICLE}}';
  27. }
  28. public function dataHandle()
  29. {
  30. $this->condition .= '';
  31. $this->listData = Article::lists($this->condition, $this->params, [
  32. 'select' => 'ART.ID,ART.TITLE,ART.CID,ART.COUNTRY_ID,ART.STATUS,ART.SORT,ART.CREATED_AT,,ADC.NAME AS COUNTRY_NAME, ADC.CODE AS COUNTRY_CODE',
  33. 'from' => Article::tableName().' AS ART',
  34. 'join' => [
  35. ['INNER JOIN', Countries::tableName() . ' AS ADC', 'ADC.ID=ART.COUNTRY_ID'],
  36. ],
  37. 'orderBy' => 'ART.SORT ASC,ART.CREATED_AT DESC',
  38. ]);
  39. }
  40. /**
  41. * @inheritdoc
  42. */
  43. public function rules()
  44. {
  45. return [
  46. [['CID', 'CREATED_AT', 'COUNTRY_ID'], 'required'],
  47. [['COUNTRY_ID'], 'string'],
  48. [['STATUS', 'CREATED_AT', 'SORT'], 'integer'],
  49. [['ID', 'CID'], 'string', 'max' => 32],
  50. [['TITLE'], 'string', 'max' => 255],
  51. [['CONTENT'], 'string', 'max' => 4000],
  52. [['TITLE'], 'unique'],
  53. [['ID'], 'unique'],
  54. ];
  55. }
  56. /**
  57. * @inheritdoc
  58. */
  59. public function attributeLabels()
  60. {
  61. return [
  62. 'ID' => 'ID',
  63. 'TITLE' => Yii::t('app', 'title'),
  64. 'COUNTRY_ID' => Yii::t('app', 'country'),
  65. 'CID' => Yii::t('app', 'category'),
  66. 'CONTENT' => Yii::t('app', 'content'),
  67. 'STATUS' => Yii::t('app', 'state'),
  68. 'Order' => Yii::t('app', 'sort'),
  69. 'CREATED_AT' => Yii::t('app', 'createAt'),
  70. ];
  71. }
  72. /**
  73. * 前台用于筛选的类型集合
  74. * @return mixed
  75. */
  76. public function getFilterTypes()
  77. {
  78. if (!$this->filterTypes) {
  79. $this->filterTypes = [
  80. 'TITLE'=> ['name'=> Yii::t('ctx', 'title')],
  81. 'COUNTRY'=> [
  82. 'name'=> \Yii::t('ctx', 'country'),
  83. 'other'=> 'select',
  84. 'selectData'=> self::getCountry()
  85. ],
  86. 'STATUS'=> [
  87. 'name'=> Yii::t('ctx', 'activeStatus'),
  88. 'other'=> 'select',
  89. 'selectData'=> [
  90. ['id'=> 0, 'name'=> Yii::t('ctx', 'Hide')],
  91. ['id'=> 1, 'name'=> Yii::t('ctx', 'Show')]
  92. ]
  93. ],
  94. ];
  95. }
  96. return $this->filterTypes;
  97. }
  98. public function getCountry()
  99. {
  100. $admin = Admin::findOne(Yii::$app->user->id);
  101. $roleId = $admin->ROLE_ID;
  102. if ($roleId == \Yii::$app->params['superAdminRoleId']) {
  103. $countries = Countries::find()->asArray()->all();
  104. } else {
  105. $countries = Countries::find()
  106. ->select('COU.ID, COU.CODE, COU.NAME')
  107. ->from(['COU' => Countries::tableName()])
  108. ->join('INNER JOIN', AdminCountry::tableName() . ' AS ADL', 'ADL.COUNTRY_ID = COU.ID')
  109. ->where(['ADL.ADMIN_ID' => $admin->ID])
  110. ->asArray()
  111. ->all();
  112. }
  113. $data = [];
  114. foreach ($countries as $country) {
  115. $data[] = [
  116. 'id' => $country['ID'],
  117. 'name' => $country['NAME'],
  118. ];
  119. }
  120. return $data;
  121. }
  122. }