AdForm.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 $countries;
  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', 'countries'], 'trim'],
  41. [['id', 'title', 'image', 'type', 'lid', 'content', 'sort', 'status', 'countries'], '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. ];
  62. }
  63. /**
  64. * 指定校验场景
  65. * @return array
  66. */
  67. public function scenarios()
  68. {
  69. $parentScenarios = parent::scenarios();
  70. $customScenarios = [
  71. 'add' => ['title', 'image', 'type', 'lid', 'content'],
  72. 'edit' => ['id', 'title', 'image', 'type', 'lid', 'content'],
  73. 'sort' => ['id','sort'],
  74. 'status' => ['id','status'],
  75. ];
  76. return array_merge($parentScenarios, $customScenarios);
  77. }
  78. /**
  79. * 初始化广告model类
  80. * @param $attribute
  81. */
  82. public function initModel($attribute){
  83. $this->_adModel = Ad::findOne(['ID'=>$this->id]);
  84. }
  85. /**
  86. * 校验类型
  87. * @param $attribute
  88. */
  89. public function isType($attribute){
  90. if(!in_array($this->type, [Ad::TYPE_LINK, Ad::TYPE_ARTICLE])){
  91. $this->addError($attribute, \Yii::t('ctx', 'typeError')); // 类型错误
  92. }
  93. }
  94. /**
  95. * 校验内容
  96. * @param $attribute
  97. */
  98. public function isContent($attribute){
  99. if($this->content == ""){
  100. $this->addError($attribute, \Yii::t('ctx', 'contentError')); // 内容错误
  101. }
  102. }
  103. /**
  104. * 添加编辑广告
  105. * @return Ad|null
  106. * @throws \yii\db\Exception
  107. */
  108. public function edit(){
  109. if(!$this->validate()){
  110. return null;
  111. }
  112. $db = \Yii::$app->db;
  113. $transaction = $db->beginTransaction();
  114. try {
  115. if($this->scenario == 'add'){
  116. $model = new Ad();
  117. $model->CREATE_ADMIN = \Yii::$app->user->id;
  118. $model->CREATED_AT = Date::nowTime();
  119. } elseif($this->scenario == 'edit') {
  120. $this->adminOperateLogger->beforeUpdate($this->_adModel);
  121. $model = $this->_adModel;
  122. $model->UPDATE_ADMIN = \Yii::$app->user->id;
  123. $model->UPDATED_AT = Date::nowTime();
  124. } else {
  125. $this->addError('edit', \Yii::t('ctx', 'submissionDoesNotExist'));
  126. return null;
  127. }
  128. $model->TITLE = $this->title;
  129. $model->IMAGE = $this->image;
  130. $model->TYPE = $this->type;
  131. $model->LID = $this->lid;
  132. $model->CONTENT = $this->content;
  133. $model->COUNTRIES = $this->countries;
  134. $model->STATUS = 1;
  135. if(!$model->save()){
  136. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  137. }
  138. $transaction->commit();
  139. } catch (Exception $e) {
  140. $transaction->rollBack();
  141. $this->addError('edit', $e->getMessage());
  142. return null;
  143. }
  144. if($this->scenario == 'add'){
  145. $this->adminOperateLogger->afterInsert($model)->clean()->save([
  146. 'optType' => 'Add',
  147. ]);
  148. }else{
  149. $this->adminOperateLogger->afterUpdate($model)->clean()->save([
  150. 'optType' => 'Edit',
  151. ]);
  152. }
  153. return $model;
  154. }
  155. /**
  156. * 排序
  157. * @return null
  158. */
  159. public function sortTo(){
  160. if(!$this->validate()){
  161. return null;
  162. }
  163. $this->adminOperateLogger->beforeUpdate($this->_adModel);
  164. $model = $this->_adModel;
  165. $model->SORT = $this->sort;
  166. if(!$model->save()){
  167. $this->addError('sortTo', Form::formatErrorsForApi($model->getErrors()));
  168. return null;
  169. }
  170. $this->adminOperateLogger->afterUpdate($model)->clean()->save([
  171. 'optType' => 'Ad Order', // 广告排序
  172. ]);
  173. return $model;
  174. }
  175. /**
  176. * 改变状态
  177. * @return null
  178. */
  179. public function statusTo(){
  180. if(!$this->validate()){
  181. return null;
  182. }
  183. $model = $this->_adModel;
  184. $model->STATUS = $this->status;
  185. if(!$model->save()){
  186. $this->addError('statusTo', Form::formatErrorsForApi($model->getErrors()));
  187. return null;
  188. }
  189. return $model;
  190. }
  191. /**
  192. * 删除前
  193. * @param $selected
  194. */
  195. public function beforeDelete($selected) {
  196. $this->adminOperateLogger->fetchClass = Ad::class;
  197. $this->adminOperateLogger->setIsBatch(true)->beforeDelete($selected, 'ID');
  198. }
  199. /**
  200. * 删除
  201. * @param $selected
  202. * @throws Exception
  203. */
  204. public function delete($selected) {
  205. $this->adminOperateLogger->clean()->save([
  206. 'optType' => 'Delete Ad', // 删除广告
  207. ]);
  208. }
  209. }