Subscriptions.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace app\models;
  3. use Yii;
  4. use yii\base\Model;
  5. use yii\helpers\Url;
  6. use yii\helpers\Html;
  7. use \yii\db\ActiveRecord;
  8. use yii\behaviors\TimestampBehavior;
  9. /**
  10. * This is the model class for table "subscriptions".
  11. *
  12. * @property User $user
  13. * @property Category $category
  14. * @property integer $id
  15. * @property integer $user_id
  16. * @property integer $category_id
  17. * @property integer $created_at
  18. */
  19. class Subscriptions extends ActiveRecord
  20. {
  21. /**
  22. * @inheritdoc
  23. */
  24. public static function tableName()
  25. {
  26. return 'subscriptions';
  27. }
  28. public function behaviors(){
  29. return [
  30. 'timestamp' => [
  31. 'class' => TimestampBehavior::className(),
  32. 'attributes' => [
  33. ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
  34. ]
  35. ]
  36. ];
  37. }
  38. /**
  39. * @inheritdoc
  40. */
  41. public function rules()
  42. {
  43. return [
  44. [['user_id', 'category_id'], 'required'],
  45. [['user_id', 'category_id', 'created_at'], 'integer'],
  46. [['category_id'], 'exist', 'targetClass' => Category::className(), 'targetAttribute' => 'id'],
  47. [['user_id'], 'exist', 'targetClass' => User::className(), 'targetAttribute' => 'id'],
  48. ];
  49. }
  50. /**
  51. * @inheritdoc
  52. */
  53. public function attributeLabels()
  54. {
  55. return [
  56. 'id' => Yii::t('app', 'ID'),
  57. 'user_id' => Yii::t('app', 'User ID'),
  58. 'category_id' => Yii::t('app', 'Category Name'),
  59. 'created_at' => Yii::t('app', 'Created At'),
  60. 'fio' => Yii::t('app', 'FIO'),
  61. 'email' => Yii::t('app', 'Email'),
  62. ];
  63. }
  64. public function getUser(){
  65. return $this->hasOne(User::className(), ['id' => 'user_id']);
  66. }
  67. public function getCategory(){
  68. return $this->hasOne(Category::className(), ['id' => 'category_id']);
  69. }
  70. public static function subscribe($category_id, $user_id){
  71. if(false != (Subscriptions::findOne(['category_id' => $category_id, 'user_id' => $user_id]))){
  72. return false;
  73. }
  74. $subscription = new Subscriptions(['user_id' => $user_id, 'category_id' => $category_id]);
  75. return $subscription->save();
  76. }
  77. public static function createNewsletter($auction){
  78. $category = $auction->category;
  79. if(!$category){
  80. return false;
  81. }
  82. foreach(Subscriptions::find()->where(['category_id' => $category->id])->all() as $subscription){
  83. $mail = new Mails([
  84. 'email' => $subscription->user->email,
  85. 'title' => Yii::t('app', 'New Lot in category: "{category}"', ['category' => $category->name]),
  86. 'text' => Yii::t('app', 'View: {link}', ['link' => Url::to(['/publishing/view', 'id' => $auction->id], true)]),
  87. ]);
  88. $mail->save(false);
  89. }
  90. return true;
  91. }
  92. }