Skip to content

Commit

Permalink
working to unify the resource processing / rendering method
Browse files Browse the repository at this point in the history
  • Loading branch information
matdave committed Jul 3, 2023
1 parent b77c871 commit 033a24a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 89 deletions.
89 changes: 8 additions & 81 deletions core/components/fred/src/Endpoint/Ajax/SaveContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,40 +73,6 @@ public function process(): string

$this->loadTagger();

$parser = $this->modx->getParser();

if (isset($this->body['content'])) {
$content = $this->body['content'];
$content = Utils::htmlDecodeTags($content, $parser);

$this->object->setContent($content);
}

$c = $this->modx->newQuery(modTemplateVar::class);
$c->leftJoin(modTemplateVarTemplate::class, 'TemplateVarTemplates');

$c->where([
'TemplateVarTemplates.templateid' => $this->object->get('template')
]);

/** @var modTemplateVar[] $tvs */
$tvs = $this->modx->getIterator(modTemplateVar::class, $c);
$mTypes = $this->modx->getOption('manipulatable_url_tv_output_types', null, 'image,file');
$mTypes = explode(',', $mTypes);
foreach ($tvs as $tv) {
$tvName = $tv->get('name');

if (isset($this->body[$tvName])) {
$tvContent = $this->body[$tvName];
$tvContent = Utils::htmlDecodeTags($tvContent, $parser);
if (in_array($tv->type, $mTypes, true)) {
$this->object->setTVValue($tvName, $this->reversePreparedOutput($tv, $tvContent, $this->object));
} else {
$this->object->setTVValue($tvName, $tvContent);
}
}
}

if (isset($this->body['pageSettings']['introtext'])) {
$this->object->set('introtext', $this->body['pageSettings']['introtext']);
}
Expand Down Expand Up @@ -241,7 +207,6 @@ public function process(): string

$this->handleTagger($this->object);

$this->body['data']['fingerprint'] = Utils::resourceFingerprint($this->object);
$this->object->setProperty('data', $this->body['data'], 'fred');

$beforeSave = $this->modx->invokeEvent('FredOnBeforeFredResourceSave', [
Expand All @@ -265,28 +230,19 @@ public function process(): string
return $this->failure($preventSave);
}

if (isset($this->body['pageSettings']['tvs']) && is_array($this->body['pageSettings']['tvs'])) {
foreach ($tvs as $tv) {
$tvName = $tv->get('name');
if (isset($this->body['pageSettings']['tvs'][$tvName])) {
$tvContent = $this->body['pageSettings']['tvs'][$tvName];
$tvContent = Utils::htmlDecodeTags($tvContent, $parser);
if (in_array($tv->type, $mTypes, true)) {
$this->object->setTVValue($tvName, $this->reversePreparedOutput($tv, $tvContent, $this->object));
} else {
$this->object->setTVValue($tvName, $tvContent);
}
}
}
}

$saved = $this->object->save();

if (!$saved) {
return $this->failure($this->modx->lexicon('fred.fe.err.resource_save'));
}
// unify resource rendering
$renderResource = new \Fred\RenderResource($this->object, $this->modx, $this->body['data']);
if (!$renderResource->render()) {
return $this->failure($this->modx->lexicon('fred.fe.err.resource_save'));
}

$this->object = $renderResource->resource;

$this->modx->getCacheManager()->refresh();
$this->modx->setOption('cache_alias_map', false);

$this->modx->invokeEvent('FredOnFredResourceSave', [
Expand All @@ -296,7 +252,7 @@ public function process(): string

$response = [
'message' => $this->modx->lexicon('fred.fe.pages.updated'),
'fingerprint' => $this->body['data']['fingerprint'],
'fingerprint' => $renderResource->data['fingerprint'],
'publishedon' => $this->object->publishedon,
'alias' => $this->object->alias,
];
Expand Down Expand Up @@ -447,33 +403,4 @@ protected function setAlias($alias, $pageTitle): bool
$this->object->set('alias', $alias);
return true;
}

public function reversePreparedOutput($tv, $value, $resource): string
{
if (!empty($value)) {
$context = !empty($resource) ? $resource->get('context_key') : $this->modx->context->get('key');
$sourceCache = $tv->getSourceCache($context);
$classKey = $sourceCache['class_key'];
if (!empty($sourceCache) && !empty($classKey)) {
if ($this->modx->loadClass($classKey)) {
/** @var modMediaSource $source */
$source = $this->modx->newObject($classKey);
if ($source) {
$source->fromArray($sourceCache, '', true, true);
$source->initialize();
$properties = $source->getPropertyList();
if (!empty($properties['baseUrl'])) {
return ltrim($value, rtrim($properties['baseUrl'], DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR);
}
//S3 Objects
if (!empty($properties['url'])) {
return ltrim($value, rtrim($properties['url'], DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR);
}
}
}
}
}

return $value;
}
}
23 changes: 15 additions & 8 deletions core/components/fred/src/RenderResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
use MODX\Revolution\modResource;
use MODX\Revolution\modRequest;
use MODX\Revolution\modTemplateVar;
use MODX\Revolution\modTemplateVarTemplate;
use MODX\Revolution\modX;
use Wa72\HtmlPageDom\HtmlPageCrawler;

final class RenderResource
{
/** @var modResource */
private $resource;
public $resource;

/** @var FredTheme */
private $theme;
Expand All @@ -36,30 +37,33 @@ final class RenderResource
private $fred;

/** @var array */
private $data = [];
public $data = [];

/** @var array */
private $elementOptions = [];

/** @var array */
private $elementCache = [];

public function __construct(modResource $resource, modX $modx)
public function __construct(modResource $resource, modX $modx, $data = [])
{
$this->resource = $resource;
$this->modx = $modx;
$this->fred = $modx->services->get('fred');

$this->theme = $this->fred->getTheme($this->resource->template);

$this->data = $this->resource->getProperty('data', 'fred');
if (empty($data)) {
$this->data = $this->resource->getProperty('data', 'fred');
} else {
$this->data = $data;
}
if (empty($this->data) && !empty($this->resource->content)) {
$this->setDefaults();
}
$elements = [];
$this->gatherElements($elements, $this->data);

$loader = new \Twig\Loader\ArrayLoader($elements);
$loader = new \Twig\Loader\ArrayLoader($elements);
$this->twig = new \Twig\Environment($loader, []);
$this->twig->setCache(false);
}
Expand Down Expand Up @@ -184,6 +188,7 @@ public function render(): bool
}
$html .= $elementContent;
} catch (\Exception $e) {
$this->modx->log(modX::LOG_LEVEL_ERROR, "[Fred] Error rendering element {$item['widget']}: {$e->getMessage()}");
}
}
}
Expand All @@ -198,11 +203,13 @@ public function render(): bool
try {
$this->resource->set('content', $twig->render('content', $this->mergeSetting('')));
} catch (\Exception $e) {
$this->modx->log(modX::LOG_LEVEL_ERROR, "[Fred] Error rendering resource {$this->resource->id}: {$e->getMessage()}");
$this->modx->log(modX::LOG_LEVEL_ERROR, "[Fred] HTML \n {$html}");
$this->resource->set('content', '');
}

$c = $this->modx->newQuery('modTemplateVar');
$c->leftJoin('modTemplateVarTemplate', 'TemplateVarTemplates');
$c = $this->modx->newQuery(modTemplateVar::class);
$c->leftJoin(modTemplateVarTemplate::class, 'TemplateVarTemplates');

$c->where(
[
Expand Down

0 comments on commit 033a24a

Please sign in to comment.