Skip to content

Commit 292d0ae

Browse files
authored
Merge pull request #12 from sl0wik/feature/images
Component images
2 parents dbafc81 + b1fd78e commit 292d0ae

File tree

6 files changed

+79
-2
lines changed

6 files changed

+79
-2
lines changed
File renamed without changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Sl0wik\Webset\Http\Controllers;
4+
5+
use Sl0wik\Webset\Models\Image;
6+
7+
class ImageController extends Controller
8+
{
9+
/**
10+
* Show image for public access.
11+
*
12+
* @param string $hash
13+
* @param string $size
14+
*
15+
* @return Illuminate\Http\Response
16+
*/
17+
public function showByHash($hash, $size = null)
18+
{
19+
$image = Image::where('hash', $hash)->firstOrFail();
20+
if ($size) {
21+
return $image->size($size)->response();
22+
}
23+
24+
return $image->response();
25+
}
26+
}

src/Models/Component.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,27 @@
88
class Component extends Model
99
{
1010
use SoftDeletes;
11+
12+
/**
13+
* Json objects.
14+
*
15+
* @var array
16+
*/
17+
protected $casts = [
18+
'custom' => 'object',
19+
];
20+
21+
/**
22+
* Get custom values.
23+
*
24+
* @param string $key
25+
*
26+
* @return string|null
27+
*/
28+
public function custom($key)
29+
{
30+
if (isset($this->custom->$key)) {
31+
return $this->custom->$key;
32+
}
33+
}
1134
}

src/Models/ComponentPayload.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,14 @@ public function component()
2727
{
2828
return $this->belongsTo(Component::class);
2929
}
30+
31+
/**
32+
* Relation with images.
33+
*
34+
* @return type
35+
*/
36+
public function images()
37+
{
38+
return $this->morphMany(Image::class, 'parent');
39+
}
3040
}

src/Models/Content.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,26 @@ public function getComponents($name = null)
126126
});
127127
}
128128

129-
return $query->get()->map(function ($component) {
130-
return $component->payload;
129+
return $query->get()->map(function ($componentPayload) {
130+
$output = $componentPayload->payload;
131+
132+
if ($componentPayload->component->custom('gallery')) {
133+
$images = Image::where([['parent_type', 'component-payloads'], ['parent_id', $componentPayload->id]])->get();
134+
$output->images = $images->map(function ($image) {
135+
$outputImage = (object) $image->toArray();
136+
$outputImage->href = $image->href();
137+
138+
return $outputImage;
139+
});
140+
}
141+
if ($componentPayload->component->custom('image')) {
142+
if ($image = Image::find($componentPayload->image_id)) {
143+
$output->image = (object) $image->toArray();
144+
$output->image->href = $image->href();
145+
}
146+
}
147+
148+
return $output;
131149
});
132150
}
133151
}

0 commit comments

Comments
 (0)