Example #1
0
 /**
  * @return ActiveQuery
  */
 public function getPriceList()
 {
     return $this->hasMany(EventPrice::className(), ['event_id' => 'id'])->orderBy('cost');
 }
Example #2
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;
 }