Skip to content

Commit

Permalink
Merge pull request #12 from sl0wik/feature/images
Browse files Browse the repository at this point in the history
Component images
  • Loading branch information
sl0wik authored Mar 17, 2018
2 parents dbafc81 + b1fd78e commit 292d0ae
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 2 deletions.
File renamed without changes.
File renamed without changes.
26 changes: 26 additions & 0 deletions src/Http/Controllers/ImageController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Sl0wik\Webset\Http\Controllers;

use Sl0wik\Webset\Models\Image;

class ImageController extends Controller
{
/**
* Show image for public access.
*
* @param string $hash
* @param string $size
*
* @return Illuminate\Http\Response
*/
public function showByHash($hash, $size = null)
{
$image = Image::where('hash', $hash)->firstOrFail();
if ($size) {
return $image->size($size)->response();
}

return $image->response();
}
}
23 changes: 23 additions & 0 deletions src/Models/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,27 @@
class Component extends Model
{
use SoftDeletes;

/**
* Json objects.
*
* @var array
*/
protected $casts = [
'custom' => 'object',
];

/**
* Get custom values.
*
* @param string $key
*
* @return string|null
*/
public function custom($key)
{
if (isset($this->custom->$key)) {
return $this->custom->$key;
}
}
}
10 changes: 10 additions & 0 deletions src/Models/ComponentPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,14 @@ public function component()
{
return $this->belongsTo(Component::class);
}

/**
* Relation with images.
*
* @return type
*/
public function images()
{
return $this->morphMany(Image::class, 'parent');
}
}
22 changes: 20 additions & 2 deletions src/Models/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,26 @@ public function getComponents($name = null)
});
}

return $query->get()->map(function ($component) {
return $component->payload;
return $query->get()->map(function ($componentPayload) {
$output = $componentPayload->payload;

if ($componentPayload->component->custom('gallery')) {
$images = Image::where([['parent_type', 'component-payloads'], ['parent_id', $componentPayload->id]])->get();
$output->images = $images->map(function ($image) {
$outputImage = (object) $image->toArray();
$outputImage->href = $image->href();

return $outputImage;
});
}
if ($componentPayload->component->custom('image')) {
if ($image = Image::find($componentPayload->image_id)) {
$output->image = (object) $image->toArray();
$output->image->href = $image->href();
}
}

return $output;
});
}
}

0 comments on commit 292d0ae

Please sign in to comment.