public function afterSave($insert, $attr)
 {
     parent::afterSave($insert, $attr);
     $wall = new Wall();
     $wall->setData(new SubscribeUser(['to' => $this->user_id, 'from' => $this->subscriber_id, 'status' => SubscribeUser::STATUS_SUBSCRIBE]));
     $wall->publishTo(new WallPost(['target_type' => WallPost::TARGET_TYPE_USER, 'target_id' => $this->user_id, 'personal' => true]));
     $wall->publishTo(new WallPost(['target_type' => WallPost::TARGET_TYPE_USER, 'target_id' => $this->subscriber_id, 'personal' => false]));
     $wall->save();
 }
 public function afterSave($insert, $changedAttributes)
 {
     parent::afterSave($insert, $changedAttributes);
     if ($insert) {
         $wall = new Wall();
         $wall->setData(new SubscribeEvent(['userId' => $this->user_id, 'eventId' => $this->event_id, 'status' => SubscribeEvent::STATUS_SUBSCRIBE]));
         $wall->publishTo(new WallPost(['target_id' => $this->user_id, 'personal' => false]));
         foreach ($this->user->subscribers as $subscriber) {
             $wall->publishTo(new WallPost(['target_id' => $subscriber->id, 'personal' => true]));
         }
         $wall->save(false);
     }
 }
Example #3
0
 protected function prepareSubscribeEvent()
 {
     /**
      * @var  $data \common\models\Wall\SubscribeEvent
      */
     $data = $this->model->getData();
     $event = \Yii::$app->db->cache(function () use($data) {
         return Event::findOne($data->eventId);
     }, 60);
     $user = \Yii::$app->db->cache(function () use($data) {
         return User::findOne($data->userId);
     }, 60);
     return array_merge($data->toArray(), ['event' => $event, 'user' => $user]);
 }
 /**
  * Уведомления при подписке на человека
  */
 public function actionSubscriptionUserNotifications()
 {
     $date_minus_six_hours = new \DateTime();
     $date_minus_six_hours = $date_minus_six_hours->modify('-2 hours');
     $email_queue = [];
     $wall_events = Wall::find()->where('created >= ' . $date_minus_six_hours->getTimestamp() . ' AND created <= ' . time())->all();
     foreach ($wall_events as $event) {
         if ($event->type == 2) {
             $event_data = json_decode($event->mem);
             $User = User::findOne(['id' => $event_data->to]);
             $Subscriber = User::find()->where(['id' => $event_data->from])->asArray()->one();
             if ($Subscriber) {
                 $email_queue[$User->email][$event_data->from] = ['subscriber' => User::find()->where(['id' => $event_data->from])->asArray()->one(), 'data' => $event->created, 'status' => $event_data->status];
             }
         }
     }
     foreach ($email_queue as $email => $recipient) {
         $subscribed = [];
         $unsubscribed = [];
         foreach ($recipient as $person) {
             if ($person['status']) {
                 $subscribed[] = $person;
             } else {
                 $unsubscribed[] = $person;
             }
         }
         $messages[] = \Yii::$app->mailer->compose('user-subscribe', ['subscribed' => $subscribed, 'unsubscribed' => $unsubscribed])->setFrom('*****@*****.**')->setTo($email)->setSubject('Для Вас есть новые уведомления на сайте http://findspree.ru');
     }
     if (isset($messages) && !empty($messages)) {
         \Yii::$app->mailer->sendMultiple($messages);
     }
 }
Example #5
0
 public function save()
 {
     /**
      * @var User $user
      */
     $user = \Yii::$app->user->identity;
     $event = $this->_eventModel ?: new Event();
     if (preg_match_all('/[^\\s,]+/iu', $this->tag, $tags)) {
         $tags = array_slice($tags[0], 0, self::TAG_MAX_COUNT);
     } else {
         $tags = [];
     }
     $historyData = ['before' => '', 'after' => '', 'event_id' => '', 'action' => EventHistory::ACTION_CREATE];
     $before = [];
     if (!$event->isNewRecord) {
         $historyData['action'] = EventHistory::ACTION_EDIT;
         $before = $event->getAttributes();
         unset($before['updated']);
         $tagsOld = ArrayHelper::getColumn($event->tags, 'name', false);
         $before['tags'] = $tagsOld;
         $before['priceList'] = $event->getPriceList()->asArray()->select(['cost', 'description'])->all();
         Tag::unbind($event, $tagsOld);
         EventPrice::deleteAll(['event_id' => $event->id]);
         FileHelper::removeDirectory($event->getImageDir());
     }
     $event->geo_description = $this->geoDescription;
     $event->geo_title = $this->geoTitle;
     list($event->geo_longitude, $event->geo_latitude) = explode(',', $this->geoCoordinates);
     //сохраняем картиночку
     $uploadedFile = $this->img;
     if ($uploadedFile) {
         $basePath = \Yii::getAlias('@webroot') . \Yii::$app->params['DIR_EVENT_IMG'];
         $folderName = sha1(uniqid(mt_rand(), true) . ':' . $user->id . ':' . $uploadedFile->name);
         $dir = $basePath . DIRECTORY_SEPARATOR . $folderName;
         $main = Image::getImagine()->open($uploadedFile->tempName);
         FileHelper::createDirectory($dir);
         $main->copy()->thumbnail(new Box(200, 200), ImageInterface::THUMBNAIL_OUTBOUND)->save($dir . '/' . Event::IMAGE_THUMB_MD . '.jpeg');
         $main->copy()->thumbnail(new Box(60, 60), ImageInterface::THUMBNAIL_OUTBOUND)->save($dir . '/' . Event::IMAGE_THUMB_SM . '.jpeg');
         if ($main->getSize()->getHeight() / $main->getSize()->getWidth() >= 1.9) {
             $main->thumbnail(new Box(600, 600), ImageInterface::THUMBNAIL_OUTBOUND)->save($dir . '/' . Event::IMAGE_MAIN . '.jpeg');
         } else {
             $main->resize($main->getSize()->widen(600))->save($dir . '/' . Event::IMAGE_MAIN . '.jpeg');
         }
         unlink($uploadedFile->tempName);
         $event->img = $folderName;
     }
     $event->event_type_id = $this->type;
     $event->user_id = $user->id;
     $event->name = $this->name;
     $event->description = $this->description;
     $event->begin = (new \DateTime($this->begin))->getTimestamp();
     //todo приводим дату к нормальному виду
     if ($this->end) {
         $event->end = (new \DateTime($this->end))->getTimestamp();
         //todo приводим дату к нормальному виду
     }
     $event->site = $this->site;
     if ($event->save()) {
         $after = $event->getAttributes();
         unset($after['updated']);
         $wall = new Wall();
         $eventWallParams = ['userId' => $user->id, 'eventId' => $event->id];
         if ($this->getScenario() != self::SCENARIO_EDIT) {
             $wall->setData(new EventCreate($eventWallParams));
         } else {
             $wall->setData(new EventEdit($eventWallParams));
         }
         $wall->publishTo(new WallPost(['target_type' => WallPost::TARGET_TYPE_USER, 'target_id' => $user->id, 'personal' => false]));
         $subscribers = $user->getSubscribers()->select('subscriber_id')->column();
         if ($subscribers) {
             foreach ($subscribers as $userSub) {
                 $wall->publishTo(new WallPost(['target_type' => WallPost::TARGET_TYPE_USER, 'target_id' => $userSub, 'personal' => true]));
             }
         }
         $wall->save();
         $i = 1;
         $priceCheck = [];
         foreach ($this->getPrice() as $ep) {
             $ep->event_id = $event->id;
             if ($ep->validate() && is_numeric($ep->cost) && !in_array((int) $ep->cost, $priceCheck)) {
                 $priceCheck[] = (int) $ep->cost;
                 $ep->insert(false);
                 $after['priceList'][] = ['cost' => $ep->cost, 'description' => $ep->description];
             }
             if ($i == 10) {
                 break;
             }
             $i++;
         }
         Tag::bind($event, $tags);
         $historyData['event_id'] = $event->id;
         $after['tags'] = $tags;
         $diff = Arr::diff($before, $after);
         $historyData = array_merge($historyData, $diff);
         $historyData['before'] = isset($historyData['before']) ? json_encode($historyData['before']) : '';
         $historyData['after'] = isset($historyData['after']) ? json_encode($historyData['after']) : '';
         $history = new EventHistory($historyData);
         $history->insert(false);
         return $event->id;
     }
     return false;
 }
 public function actionWall($id = null)
 {
     /**
      * @var $userView User
      */
     $userView = $id && \Yii::$app->user->identity != $id ? User::findOne($id) : \Yii::$app->user->identity;
     if (!$userView) {
         throw new NotFoundHttpException();
     }
     $collection = new ActiveDataProvider();
     $collection->pagination->pageSize = 12;
     $criteria = ['and', 'wall.id=wall_post.wall_id', ['=', 'target_type', WallPost::TARGET_TYPE_USER], ['=', 'target_id', $userView->id]];
     if (!$userView->isMy()) {
         $criteria[] = ['=', 'personal', 0];
     }
     $collection->query = Wall::find()->select('wall.*')->innerJoin(WallPost::tableName(), $criteria)->orderBy('wall.id DESC');
     return $this->renderCollection($collection);
 }