/** * * Returns a collection of random photos * * @return Collection */ protected function getPhotos() { $count = $this->property('photosCount'); $photos = PhotoModel::orderBy(DB::raw('RAND()'))->with('image')->take($count)->get(); foreach ($photos as $photo) { $photo->url = $photo->setUrl($this->property('photoPage'), $this->controller); $photo->thumb = $photo->image->getThumb($this->property('thumbWidth'), $this->property('thumbHeight'), ['mode' => $this->property('thumbMode')]); } $this->cachePhotos($photos); return $photos; }
/** * * Returns replacement for text * (replaces [photo:id:width:height:mode] with resulting photo's image path) * * @param $entry * @param $placeholder * @return string */ protected function getReplacement($entry, $placeholder) { list($id, $width, $height, $mode) = $this->getPhotoParams($placeholder); $photo = Photo::where('id', $id)->with('image')->first(); if (!$photo) { return $placeholder; } else { if ($width && $height) { $path = $photo->image->getThumb($width, $height, ['mode' => $mode]); } else { $path = $photo->image->path; } return str_replace($placeholder, $path, $entry); } }
/** * * Loads photo to be displayed in this component * * @return PhotoModel */ protected function loadPhoto() { $id = $this->property('id'); $photo = PhotoModel::where('id', $id)->with('image')->with('album')->first(); if ($photo) { // set url so we can have back link to the parent album $photo->album->url = $photo->album->setUrl($this->property('albumPage'), $this->controller); //set next and previous photos $photo->next = $photo->nextPhoto(); if ($photo->next) { $photo->next->url = $photo->next->setUrl($this->property('photoPage'), $this->controller); } $photo->previous = $photo->previousPhoto(); if ($photo->previous) { $photo->previous->url = $photo->previous->setUrl($this->property('photoPage'), $this->controller); } } return $photo; }
/** * * Saves photos with files attached from $file_ids and attaches them to album * * @param AlbumModel $album * @param array $file_ids * @param string[] $file_titles arrray of titles */ protected function savePhotos($album, $file_ids, $file_titles) { $files = File::whereIn('id', $file_ids)->get(); $photos = array(); foreach ($files as $file) { $photo = new PhotoModel(); $photo->title = isset($file_titles[$file->id]) ? $file_titles[$file->id] : ''; $photo->save(); $photo->image()->save($file); $photos[] = $photo; } $album->photos()->saveMany($photos); }