Last active
September 7, 2021 23:06
pageblocks.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @var modX $modx | |
* @var array $scriptProperties | |
* @var PageBlocks $PageBlocks | |
* @var PageBlockTools $tools | |
*/ | |
// Return Parameters Snippet | |
$id = $modx->getOption('id', $scriptProperties, ''); | |
$rid = $modx->getOption('rid', $scriptProperties, ''); | |
$up = $modx->getOption('up', $scriptProperties, 0); | |
$return = $modx->getOption('return', $scriptProperties, 'chunk'); | |
$criteria = $modx->getOption('where', $scriptProperties, ['active' => 1]); | |
$outputSeparator = $modx->getOption('outputSeparator', $scriptProperties, "\n"); | |
$additionalPlaceholders = $modx->getOption('additionalPlaceholders', $scriptProperties, []); | |
$PageBlocks = $modx->getService('PageBlocks', 'PageBlocks', MODX_CORE_PATH . 'components/pageblocks/model/', $scriptProperties); | |
if (!$PageBlocks) { | |
return 'Could not load PageBlocks class!'; | |
} | |
$default = [ | |
'id' => $id, | |
'return' => $return, | |
'where' => $criteria, | |
'up' => $up, | |
'rid' => (!empty($rid)) ? $rid : $modx->resource->get('id'), | |
'outputSeparator' => $outputSeparator, | |
'additionalPlaceholders' => $additionalPlaceholders | |
]; | |
$scriptProperties = array_merge($default, $scriptProperties); | |
$scriptProperties['where'] = $PageBlocks->tools->buildCriteriaSelection($scriptProperties['rid'], $scriptProperties['up'], $scriptProperties['id'], $scriptProperties['where']); | |
$data = $PageBlocks->tools->buildPageBlocksData('pageBlock', $scriptProperties['where'] , $scriptProperties['return'],$scriptProperties['additionalPlaceholders']); | |
$output = $PageBlocks->tools->buildPageBlocksReturn($scriptProperties['return'], $scriptProperties['outputSeparator'], $data); | |
return $output; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class PageBlockTools | |
{ | |
/** @var modX $modx */ | |
public $modx; | |
/** @var pdoTools $pdoTools */ | |
public $pdoTools; | |
/** | |
* @param modX $modx | |
*/ | |
function __construct(modX &$modx) | |
{ | |
$this->modx =& $modx; | |
$this->pdoTools = $this->modx->getService('pdoTools'); | |
} | |
/** | |
* @param $query | |
* @param $criteria | |
* @param string $sortby | |
* @param string $sortdir | |
* @return mixed | |
*/ | |
public function getIterator($query, $criteria, $sortby = 'rank', $sortdir = 'ASC') | |
{ | |
$c = $this->modx->newQuery($query); | |
$c->where($criteria); | |
$c->sortby($sortby, $sortdir); | |
$data = $this->modx->getIterator($query, $c); | |
return $data; | |
} | |
/** | |
* @param $query | |
* @param $criteria | |
* @param $return | |
* @param $additionalPlaceholders | |
* @param string $sortby | |
* @param string $sortdir | |
* @return array | |
*/ | |
public function buildPageBlocksData($query, $criteria, $return, $additionalPlaceholders, $sortby = "rank", $sortdir = 'ASC') | |
{ | |
$data = $this->getIterator($query, $criteria, $sortby, $sortdir); | |
$rows = []; | |
foreach ($data as $row) { | |
$rows[] = $this->buildPageBlocksDataReturn($row->get('values'), $return, $row->get('chunk'),$additionalPlaceholders); | |
} | |
return $rows; | |
} | |
/** | |
* @param $query | |
* @param $criteria | |
* @param string $sortby | |
* @param string $sortdir | |
* @return array | |
*/ | |
public function getArrayPageBlockResourceId($query, $criteria, $sortby = "rank", $sortdir = 'ASC') | |
{ | |
$data = $this->getIterator($query, $criteria, $sortby, $sortdir); | |
$rows = []; | |
foreach ($data as $row) { | |
$rows[] = $row->get('resource_id'); | |
} | |
return $rows; | |
} | |
/** | |
* @param $rid | |
* @param $up | |
* @param $id | |
* @param $where | |
* @return array|false[]|mixed | |
*/ | |
public function buildCriteriaSelection($rid, $up, $id, $where) | |
{ | |
if ($id) { | |
$where = array_merge(['constructor_id' => $id], $where); | |
} | |
if ($up != 1) { | |
if (!empty($rid)) { | |
$where = array_merge(['resource_id' => $rid], $where); | |
} | |
} else { | |
$rid = $this->modx->resource->get('id'); | |
$getArrayPageBlockIds = $this->getArrayPageBlockResourceId('pageBlock', ['active' => 1]); | |
$getParentIds = array_merge(array_reverse($this->modx->getParentIds($rid, 10, ['context' => 'web'])), [$rid]); | |
$intersectPageBlockIds = array_intersect($getParentIds, $getArrayPageBlockIds); | |
$sid = end($intersectPageBlockIds); | |
$where = array_merge(['resource_id' => $sid], $where); | |
} | |
return $where; | |
} | |
/** | |
* @param $name | |
* @param $data | |
* @param $additionalPlaceholders | |
* @return mixed | |
*/ | |
public function parseChunk($name, $data, $additionalPlaceholders) { | |
if ($additionalPlaceholders) { | |
$this->modx->setPlaceholders($additionalPlaceholders, 'pb_pls_'); | |
} | |
$output = $this->pdoTools->getChunk($name, $data); | |
return $output; | |
} | |
/** | |
* @param $data | |
* @param $return | |
* @param $chunk | |
* @param $additionalPlaceholders | |
* @return mixed|string | |
*/ | |
public function buildPageBlocksDataReturn($data, $return, $chunk, $additionalPlaceholders) | |
{ | |
$output = ''; | |
switch ($return) { | |
case 'chunk': | |
$output = $this->parseChunk($chunk, json_decode($data, true),$additionalPlaceholders); | |
break; | |
case 'file': | |
$output = $this->parseChunk('@FILE chunks/' . $chunk . '.tpl', json_decode($data, true),$additionalPlaceholders); | |
break; | |
case 'data': | |
$output = json_decode($data, true); | |
break; | |
case 'json': | |
$output = $data; | |
break; | |
} | |
return $output; | |
} | |
/** | |
* @param $return | |
* @param $outputSeparator | |
* @param $data | |
* @return array|false|mixed|string | |
*/ | |
public function buildPageBlocksReturn($return, $outputSeparator, $data) | |
{ | |
$output = []; | |
switch ($return) { | |
case ('data'): | |
$output = $data; | |
break; | |
case ('chunk'): | |
case ('file'): | |
foreach ($data as $row) { | |
$rows[] = $row; | |
} | |
$output = implode($outputSeparator, $rows); | |
break; | |
case ('json'): | |
$outputSeparator = ','; | |
foreach ($data as $row) { | |
$rows[] = $row; | |
} | |
$output = implode($outputSeparator, $rows); | |
$output = json_encode($output); | |
} | |
return $output; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment