SettingService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace common\services;
  3. use backend\models\form\SettingSMTPForm;
  4. use backend\models\form\SettingWebsiteForm;
  5. use common\models\Options;
  6. use Yii;
  7. use yii\base\Exception;
  8. use yii\base\Model;
  9. use yii\swiftmailer\Mailer;
  10. use yii\web\BadRequestHttpException;
  11. use yii\web\Response;
  12. class SettingService extends Service implements SettingServiceInterface
  13. {
  14. public function getSearchModel(array $options = [])
  15. {
  16. throw new Exception("not need implement");
  17. }
  18. public function getModel($id, array $options = [])
  19. {
  20. if($id == "website") {
  21. return new SettingWebsiteForm();
  22. }else if($id == "custom"){
  23. return $this->getCustomSettings();
  24. }else if($id == "smtp"){
  25. return new SettingSMTPForm();
  26. } else if( is_numeric($id) ){
  27. return Options::findOne($id);
  28. }
  29. }
  30. public function newModel(array $options = [])
  31. {
  32. $model = new Options();
  33. $model->loadDefaultValues();
  34. if( isset($options['type']) && in_array($options['type'], [Options::TYPE_SYSTEM, Options::TYPE_CUSTOM, Options::TYPE_BANNER, Options::TYPE_AD]) ){
  35. $model->type = $options['type'];
  36. }
  37. return $model;
  38. }
  39. public function getCustomSettings(){
  40. return Options::find()->where(['type' => Options::TYPE_CUSTOM])->orderBy("sort")->indexBy('id')->all();
  41. }
  42. public function updateWebsiteSetting(array $postData = [])
  43. {
  44. $model = $this->getModel("website");
  45. if ( $model->load($postData) && $model->setWebsiteConfig() ){
  46. return true;
  47. }
  48. return $model;
  49. }
  50. public function updateCustomSetting(array $postData=[]){
  51. $errors = [];
  52. $settings = $this->getCustomSettings();
  53. if (Model::loadMultiple($settings, $postData) && Model::validateMultiple($settings)) {
  54. foreach ($settings as $setting) {
  55. $result = $setting->save(false);
  56. if (!$result){
  57. $errors[] = $setting;
  58. }
  59. }
  60. }
  61. if(count($errors) === 0 ){
  62. return true;
  63. }
  64. return $errors;
  65. }
  66. public function updateSMTPSetting(array $postData = [])
  67. {
  68. $model = $this->getModel("smtp");
  69. if ( $model->load($postData) && $model->setSMTPSettingConfig() ){
  70. return true;
  71. }
  72. return $model;
  73. }
  74. public function testSMTPSetting(array $postData = []){
  75. $model = $this->getModel("smtp");
  76. if ($model->load($postData) && $model->validate()) {
  77. /** @var Mailer $mailer */
  78. $mailer = Yii::createObject([
  79. 'class' => Mailer::className(),
  80. 'useFileTransport' => false,
  81. 'transport' => [
  82. 'class' => 'Swift_SmtpTransport',
  83. 'host' => $model->smtp_host,
  84. 'username' => $model->smtp_username,
  85. 'password' => $model->smtp_password,
  86. 'port' => $model->smtp_port,
  87. 'encryption' => $model->smtp_encryption,
  88. ],
  89. 'messageConfig' => [
  90. 'charset' => 'UTF-8',
  91. 'from' => [$model->smtp_username => $model->smtp_nickname]
  92. ],
  93. ]);
  94. $result = $mailer->compose()
  95. ->setFrom($model->smtp_username)
  96. ->setTo($model->smtp_username)
  97. ->setSubject('Email SMTP test ' . Yii::$app->name)
  98. ->setTextBody('Email SMTP config works successful')
  99. ->send();
  100. if( $result === false ){
  101. return $result;
  102. }
  103. return true;
  104. } else {
  105. return $model;
  106. }
  107. }
  108. }