AdForm.php 6.1 KB

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