Article.php 4.1 KB

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