Skip to content

Commit

Permalink
better image size handling and other improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
saibotd committed May 5, 2017
1 parent 704cb77 commit e8d7d24
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
9 changes: 1 addition & 8 deletions src/ContentElementHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ public static function module($content, $column = 'main')
{
$contentModuleClass = ContentElement::findClass($content->type);
$element = new $contentModuleClass($content, $column);
$moduleModel = ModuleModel::findByPk($element->module);
$moduleClass = Module::findClass($moduleModel->type);
$module = new $moduleClass($moduleModel, $column);

$_module = Helper::toObj($moduleModel);
$_module->compiledHTML = Helper::replaceHTML(@$module->generate());

return $_module;
return ModuleHelper::get($element->module);
}
}
19 changes: 18 additions & 1 deletion src/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,33 @@
namespace DieSchittigs\ContaoContentApi;

use Contao\FilesModel;
use Contao\ImageSizeModel;
use Contao\ImageSizeItemModel;

class FileHelper
{
public static function file($uuid)
public static function file($uuid, $size = null)
{
$model = FilesModel::findOneBy('uuid', $uuid);
if(!$model) return null;
$result = Helper::toObj($model);
unset($result->pid);
unset($result->uuid);
$result->path = '/' + $result->path;
if($size && count($size) == 3){
if(is_numeric($size[2])){
$result->size = Helper::toObj(ImageSizeModel::findOneById($size[2]));
if($result->size){
$result->size->subSizes = Helper::toObj(ImageSizeItemModel::findVisibleByPid($result->size->id));
}
} else {
$result->size = [
'width' => $size[0],
'height' => $size[1],
'resizeMode' => $size[2]
];
}
}
return $result;
}
}
5 changes: 5 additions & 0 deletions src/FrontendApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public function handle(Request $request)
];
}
break;
case '/module':
$result = ModuleHelper::get(
$request->query->get('id', null)
);
break;
case '/':
$result = new \stdClass;
$result->page = PageHelper::getPage(
Expand Down
27 changes: 19 additions & 8 deletions src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public static function toObj($instance, $fields = null)
} else {
$obj = (object) $instance;
}
$imageSize = null;
$images = [];
foreach ($obj as $key => &$val) {
$originalVal = $val;
if ($fields && !in_array($key, $fields)) {
Expand All @@ -44,16 +46,22 @@ public static function toObj($instance, $fields = null)
if ($val == '') {
$val = null;
}
if($key == 'size'){
$imageSize = $val;
}
if (strpos($key, 'SRC') !== false && $val) {
if (is_array($val)) {
$__val = [];
foreach ($val as $_key => $_val) {
$__val[] = FileHelper::file($_val);
}
$val = $__val;
} else {
$val = FileHelper::file($val);
$images[$key] = $val;
}
}
foreach($images as $key => $image){
if (is_array($image)) {
$val = [];
foreach ($image as $_key => $_val) {
$val[] = FileHelper::file($_val, $imageSize);
}
$obj[$key] = $val;
} else {
$obj[$key] = FileHelper::file($image, $imageSize);
}
}
return (object) $obj;
Expand All @@ -67,6 +75,9 @@ public static function replaceHTML($html)
$html = str_replace('"'.$apiScript, '"', $html);
$html = str_replace('src="files', 'src="/files', $html);
$html = str_replace('href="files', 'href="/files', $html);
$html = str_replace('src="assets', 'src="/assets', $html);
$html = str_replace('href="assets', 'href="/assets', $html);
$html = str_replace('srcset="assets', 'href="/assets', $html);
return $html;
}

Expand Down

0 comments on commit e8d7d24

Please sign in to comment.