Skip to content

Commit

Permalink
snippet params: renderTo, giveTo
Browse files Browse the repository at this point in the history
  • Loading branch information
mnoskov committed Dec 20, 2017
1 parent 38dcaca commit 03f8f16
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 28 deletions.
82 changes: 56 additions & 26 deletions assets/plugins/pagebuilder/pagebuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class PageBuilder {

const version = '1.1.7';
const version = '1.2.0';

private $modx;
private $data;
Expand Down Expand Up @@ -154,6 +154,7 @@ public function render($params) {
'templates' => '',
'offset' => 0,
'limit' => 0,
'renderTo' => 'templates',
], $params);

if ($params['blocks'] != '*') {
Expand All @@ -165,6 +166,8 @@ public function render($params) {

$this->fetch($params['docid'], $params['container'], false);

$data = [];

foreach ($this->data as $row) {
$idx++;

Expand All @@ -188,42 +191,62 @@ public function render($params) {
}

$conf = $this->conf[ $row['config'] ];
$templates = $conf['templates'];

if (!empty($params['templates'])) {
if (!isset($templates[ $params['templates'] ])) {
$out .= "<div>Templates set '" . $params['templates'] . "' not defined</div>";
continue;
$values = $this->prepareData($conf, $row['values']);

if ($params['renderTo'] != 'templates') {
$data[] = $values;
continue;
} else {
$templates = $conf['templates'];

if (!empty($params['templates'])) {
if (!isset($templates[ $params['templates'] ])) {
$out .= "<div>Templates set '" . $params['templates'] . "' not defined</div>";
continue;
}

$templates = $templates[ $params['templates'] ];
}

$templates = $templates[ $params['templates'] ];
}
if (!isset($templates['owner'])) {
$out .= "<div>Template 'owner' not defined</div>";
continue;
}

if (!isset($templates['owner'])) {
$out .= "<div>Template 'owner' not defined</div>";
continue;
$out .= $this->renderFieldsList($templates, $templates['owner'], $conf, $values);
}
}

$values = $this->prepareData($conf, $row['values']);

$out .= $this->renderFieldsList($templates, $templates['owner'], $conf, $values);
if ($params['renderTo'] == 'json') {
$data = json_encode($data, JSON_UNESCAPED_UNICODE);
}

$wrapper = '[+wrap+]';
if ($params['renderTo'] == 'templates') {
$wrapper = '[+wrap+]';

if (!empty($out)) {
if (isset($params['wrapTpl'])) {
$wrapper = $this->modx->getChunk($params['wrapTpl']);
} else if (isset($this->containers[ $params['container'] ])) {
$container = $this->containers[ $params['container'] ];
if (!empty($out)) {
if (isset($params['wrapTpl'])) {
$wrapper = $this->modx->getChunk($params['wrapTpl']);
} else if (isset($this->containers[ $params['container'] ])) {
$container = $this->containers[ $params['container'] ];

if (!empty($container['templates']['owner'])) {
$wrapper = $container['templates']['owner'];
if (!empty($container['templates']['owner'])) {
$wrapper = $container['templates']['owner'];
}
}
}

$out = $this->parseTemplate($wrapper, ['wrap' => $out]);
} else {
$out = $data;
}

return $this->parseTemplate($wrapper, ['wrap' => $out]);
if (!empty($params['giveTo'])) {
return $this->modx->runSnippet($params['giveTo'], ['data' => $out]);
}

return $out;
}

/**
Expand Down Expand Up @@ -311,8 +334,6 @@ private function prepareData($options, $values) {
* @return boolean
*/
private function canIncludeBlock($block, $docid) {
$templateid = isset($this->params['template']) ? $this->params['template'] : $this->modx->documentObject['template'];

if (isset($block['placement'])) {
if (isset($this->params['tv']) && $block['placement'] != 'tv') {
return false;
Expand All @@ -329,7 +350,7 @@ private function canIncludeBlock($block, $docid) {
}
}

if (isset($block['show_in_templates']) && !in_array($templateid, $block['show_in_templates'])) {
if (isset($block['show_in_templates']) && !in_array($this->params['template'], $block['show_in_templates'])) {
return false;
}

Expand Down Expand Up @@ -364,6 +385,15 @@ private function fetch($docid, $containerName = null, $notpl = true) {

$this->conf = [];

if (!isset($this->params['template'])) {
if ($docid == $modx->documentIdentifier) {
$this->params['template'] = $this->modx->documentObject['template'];
} else {
$doc = $this->modx->getDocument($docid, 'template');
$this->params['template'] = $doc['template'];
}
}

// Loading all config files, that complied with filters
foreach (scandir($this->path) as $entry) {
if (pathinfo($entry, PATHINFO_EXTENSION) == 'php') {
Expand Down
2 changes: 1 addition & 1 deletion install/assets/plugins/PageBuilder.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Creates form for manage content sections
*
* @category plugin
* @version 1.1.7
* @version 1.2.0
* @author mnoskov
* @internal @properties &tabName=Tab name;text;Page Builder &addType=Add type;menu;dropdown,icons,images;dropdown &placement=Placement;menu;content,tab;tab
* @internal @events OnWebPageInit,OnManagerPageInit,OnDocFormRender,OnDocFormSave,OnBeforeEmptyTrash,OnDocDuplicate
Expand Down
2 changes: 1 addition & 1 deletion install/assets/snippets/PageBuilder.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* output content sections for current page
*
* @version 1.1.7
* @version 1.2.0
* @author mnoskov
* @category snippet
* @internal @properties
Expand Down

0 comments on commit 03f8f16

Please sign in to comment.