AdForm.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace common\models\forms;
  3. use common\helpers\Date;
  4. use common\components\Model;
  5. use common\helpers\Form;
  6. use common\helpers\Validator;
  7. use common\libs\logging\operate\AdminOperate;
  8. use common\models\Ad;
  9. use common\models\AdLocation;
  10. use common\models\Article;
  11. use yii\base\Exception;
  12. use yii\validators\UrlValidator;
  13. /**
  14. * Login form
  15. */
  16. class AdForm extends Model
  17. {
  18. public $id;
  19. public $title;
  20. public $image;
  21. public $type;
  22. public $lid;
  23. public $content;
  24. public $sort;
  25. public $status;
  26. public $countryId;
  27. private $_adModel;
  28. public function init() {
  29. parent::init();
  30. $this->adminOperateLogger = new AdminOperate([
  31. 'fetchClass' => Ad::class,
  32. ]);
  33. }
  34. /**
  35. * @inheritdoc
  36. */
  37. public function rules()
  38. {
  39. return [
  40. [['id', 'title', 'image', 'type', 'lid', 'content', 'sort', 'status', 'countryId'], 'trim'],
  41. [['id', 'title', 'image', 'type', 'lid', 'content', 'sort', 'status', 'countryId'], 'required'],
  42. // [['image'], 'url'],
  43. [['id'], 'exist', 'targetClass'=>Ad::class, 'targetAttribute'=>'ID'],
  44. [['id'], 'initModel'],
  45. [['lid'], 'exist', 'targetClass'=>AdLocation::class, 'targetAttribute'=>'ID'],
  46. [['type'], 'isType'],
  47. [['content'], 'isContent'],
  48. ];
  49. }
  50. public function attributeLabels()
  51. {
  52. return [
  53. 'id' => 'ID',
  54. 'title' => 'Title',// 标题
  55. 'image' => 'Image', // 图片
  56. 'type' => 'Type', // 类型
  57. 'lid' => 'Ad', // 广告位
  58. 'content' => 'External link or article ID', // 链接地址或文章ID
  59. 'sort' => 'Order', // 排序
  60. 'status' => 'State', // 状态
  61. 'countryId' => 'Country', // 国家
  62. ];
  63. }
  64. /**
  65. * 指定校验场景
  66. * @return array
  67. */
  68. public function scenarios()
  69. {
  70. $parentScenarios = parent::scenarios();
  71. $customScenarios = [
  72. 'add' => ['title', 'image', 'type', 'lid', 'content', 'countryId'],
  73. 'edit' => ['id', 'title', 'image', 'type', 'lid', 'content', 'countryId'],
  74. 'sort' => ['id','sort'],
  75. 'status' => ['id','status'],
  76. ];
  77. return array_merge($parentScenarios, $customScenarios);
  78. }
  79. /**
  80. * 初始化广告model类
  81. * @param $attribute
  82. */
  83. public function initModel($attribute){
  84. $this->_adModel = Ad::findOne(['ID'=>$this->id]);
  85. }
  86. /**
  87. * 校验类型
  88. * @param $attribute
  89. */
  90. public function isType($attribute){
  91. if(!in_array($this->type, [Ad::TYPE_LINK, Ad::TYPE_ARTICLE])){
  92. $this->addError($attribute, \Yii::t('ctx', 'typeError')); // 类型错误
  93. }
  94. }
  95. /**
  96. * 校验内容
  97. * @param $attribute
  98. */
  99. public function isContent($attribute){
  100. if($this->content == ""){
  101. $this->addError($attribute, \Yii::t('ctx', 'contentError')); // 内容错误
  102. }
  103. }
  104. /**
  105. * 添加编辑广告
  106. * @return Ad|null
  107. * @throws \yii\db\Exception
  108. */
  109. public function edit(){
  110. if(!$this->validate()){
  111. return null;
  112. }
  113. $db = \Yii::$app->db;
  114. $transaction = $db->beginTransaction();
  115. try {
  116. if($this->scenario == 'add'){
  117. $model = new Ad();
  118. $model->CREATE_ADMIN = \Yii::$app->user->id;
  119. $model->CREATED_AT = Date::nowTime();
  120. } elseif($this->scenario == 'edit') {
  121. $this->adminOperateLogger->beforeUpdate($this->_adModel);
  122. $model = $this->_adModel;
  123. $model->UPDATE_ADMIN = \Yii::$app->user->id;
  124. $model->UPDATED_AT = Date::nowTime();
  125. } else {
  126. $this->addError('edit', \Yii::t('ctx', 'submissionDoesNotExist'));
  127. return null;
  128. }
  129. $model->TITLE = $this->title;
  130. $model->IMAGE = $this->image;
  131. $model->TYPE = $this->type;
  132. $model->LID = $this->lid;
  133. $model->CONTENT = $this->content;
  134. $model->COUNTRY_ID = $this->countryId;
  135. $model->STATUS = 1;
  136. if(!$model->save()){
  137. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  138. }
  139. $transaction->commit();
  140. } catch (Exception $e) {
  141. $transaction->rollBack();
  142. $this->addError('edit', $e->getMessage());
  143. return null;
  144. }
  145. if($this->scenario == 'add'){
  146. $this->adminOperateLogger->afterInsert($model)->clean()->save([
  147. 'optType' => 'Add',
  148. ]);
  149. }else{
  150. $this->adminOperateLogger->afterUpdate($model)->clean()->save([
  151. 'optType' => 'Edit',
  152. ]);
  153. }
  154. return $model;
  155. }
  156. /**
  157. * 排序
  158. * @return null
  159. */
  160. public function sortTo(){
  161. if(!$this->validate()){
  162. return null;
  163. }
  164. $this->adminOperateLogger->beforeUpdate($this->_adModel);
  165. $model = $this->_adModel;
  166. $model->SORT = $this->sort;
  167. if(!$model->save()){
  168. $this->addError('sortTo', Form::formatErrorsForApi($model->getErrors()));
  169. return null;
  170. }
  171. $this->adminOperateLogger->afterUpdate($model)->clean()->save([
  172. 'optType' => 'Ad Order', // 广告排序
  173. ]);
  174. return $model;
  175. }
  176. /**
  177. * 改变状态
  178. * @return null
  179. */
  180. public function statusTo(){
  181. if(!$this->validate()){
  182. return null;
  183. }
  184. $model = $this->_adModel;
  185. $model->STATUS = $this->status;
  186. if(!$model->save()){
  187. $this->addError('statusTo', Form::formatErrorsForApi($model->getErrors()));
  188. return null;
  189. }
  190. return $model;
  191. }
  192. /**
  193. * 删除前
  194. * @param $selected
  195. */
  196. public function beforeDelete($selected) {
  197. $this->adminOperateLogger->fetchClass = Ad::class;
  198. $this->adminOperateLogger->setIsBatch(true)->beforeDelete($selected, 'ID');
  199. }
  200. /**
  201. * 删除
  202. * @param $selected
  203. * @throws Exception
  204. */
  205. public function delete($selected) {
  206. $this->adminOperateLogger->clean()->save([
  207. 'optType' => 'Delete Ad', // 删除广告
  208. ]);
  209. }
  210. }