public function getFileList() { /* * Use deferred bindings */ if ($sessionKey = $this->getSessionKey()) { $list = $deferredQuery = $this->model->{$this->attribute}()->withDeferred($sessionKey)->orderBy('id', 'desc')->get(); } else { $list = $this->model->{$this->attribute}()->orderBy('id', 'desc')->get(); } if (!$list) { $list = new Collection(); } /* * Decorate each file with thumb */ $list->each(function ($file) { $this->decorateFileAttributes($file); }); return $list; }
/** * Import events from Facebook. * * @param bool $save * @return array */ public function import($save = false) { $added = []; $errors = []; $imported = []; $skipped = []; $updated = []; try { $vevents = VCalendar::getFromUrl($this->importUrl, true); if (!$vevents) { throw new SystemException('No events found.'); } $imported = Collection::make(VCalendar::parseEvents($vevents))->keyBy('facebook_id'); // We are interested only in upcoming events $upcoming = $imported->filter(function (EventModel $event) { return $event->ends_at > Carbon::create(); }); // Get existing Facebook events /** @var Collection $existing */ $existing = EventModel::upcoming()->where(function ($query) use($upcoming) { $query->facebook($upcoming->keys()->toArray())->orWhere(DB::raw('LOWER(url)'), 'LIKE', '%facebook.com/events%'); })->get(); /** @var EventModel $upcomingEvent */ foreach ($upcoming as $upcomingEvent) { /** @var EventModel $existingEvent */ $existingEvent = $existing->first(function ($key, EventModel $event) use($upcomingEvent) { return $event->facebook_id == $upcomingEvent->facebook_id || $event->url && strpos($event->url, $upcomingEvent->facebook_id); }); if ($existingEvent) { if ($upcomingEvent->updated_at > $existingEvent->updated_at) { $existingEvent->fill(['name' => $upcomingEvent->name, 'url' => $upcomingEvent->url, 'begins_at' => $upcomingEvent->begins_at, 'ends_at' => $upcomingEvent->ends_at, 'facebook_id' => $upcomingEvent->facebook_id, 'facebook_organizer' => $upcomingEvent->facebook_organizer]); $error = $this->updateEvent($existingEvent, $save); if ($error) { $errors[] = $error; } $updated[$existingEvent->facebook_id] = $existingEvent; if ($save && $existingEvent->isDirty()) { $existingEvent->save(); // @TODO: Move to newsfeed plugin $this->createEventNewsfeedItem($existingEvent->id, false); } } else { $skipped[$existingEvent->facebook_id] = $existingEvent; } } else { $upcomingEvent->author_id = $this->importUserId; $error = $this->updateEvent($upcomingEvent, $save); if ($error) { $errors[] = $error; } $added[$upcomingEvent->facebook_id] = $upcomingEvent; if ($save) { $upcomingEvent->save(); // @TODO: Move to newsfeed plugin $this->createEventNewsfeedItem($upcomingEvent->id, true); } } } } catch (SystemException $e) { $errors[] = $e->getMessage(); } return compact('added', 'errors', 'imported', 'skipped', 'updated'); }
/** * Saves a collectino of selections * * @param \October\Rain\Support\Collection * @return void */ public function saveSelections(Collection $collection) { // Remove selections not present in the collection $this->selections->each(function ($selection) use($collection) { if (!$collection->contains('id', $selection->id)) { $selection->delete(); } }); // Save the collection $collection->each(function ($selection) { $selection->option_id = $this->id; $selection->save(); }); }