diff --git a/neo/services/NeoService.php b/neo/services/NeoService.php index 2da10a6b..c8998bc7 100644 --- a/neo/services/NeoService.php +++ b/neo/services/NeoService.php @@ -14,6 +14,7 @@ class NeoService extends BaseApplicationComponent private $_blockRecordsById; private $_uniqueBlockTypeAndFieldHandles; private $_parentNeoFields; + private $_blocks = []; public $currentSavingBlockType; @@ -682,6 +683,40 @@ public function getParentNeoField(FieldModel $neoField) return $this->_parentNeoFields[$neoField->id]; } + public function getChildBlocks(Neo_BlockModel $block) + { + $owner = $block->getOwner(); + $type = $block->getType(); + $field = craft()->fields->getFieldById($type->fieldId); + + $blocks = $this->_getBlocksByOwnerAndField($owner, $field); + + $childLevel = ((int) $block->level) + 1; + $children = array(); + $foundBlock = false; + + foreach($blocks as $testBlock) + { + if($foundBlock) + { + if($testBlock->level == $childLevel) + { + $children[] = $testBlock; + } + else if($testBlock->level < $childLevel) + { + break; + } + } + if($testBlock->id == $block->id) + { + $foundBlock = true; + } + } + + return $children; + } + public function requirePlugin($plugin) { if(!craft()->plugins->getPlugin($plugin)) @@ -885,4 +920,28 @@ private function _applyFieldTranslationSetting($owner, $field, $blocks) } } } + + private function _getBlocksByOwnerAndField(BaseElementModel $owner, FieldModel $field) + { + $key = $owner->id . ':' . $field->id; + + if(!isset($this->_blocks[$key])) + { + $result = Neo_BlockRecord::model()->findAllByAttributes([ + 'ownerId' => $owner->id, + 'fieldId' => $field->id, + ]); + + $models = Neo_BlockModel::populateModels($result); + + usort($models, function(Neo_BlockModel $a, Neo_BlockModel $b) + { + return $a->sortOrder - $b->sortOrder; + }); + + $this->_blocks[$key] = $models; + } + + return $this->_blocks[$key]; + } } diff --git a/neo/variables/NeoVariable.php b/neo/variables/NeoVariable.php new file mode 100644 index 00000000..2bbbbe5c --- /dev/null +++ b/neo/variables/NeoVariable.php @@ -0,0 +1,15 @@ +neo->getChildBlocks($block); + } + + public function hasChildren(Neo_BlockModel $block) + { + return !empty($this->children($block)); + } +}