Created
July 9, 2018 10:16
-
-
Save hmic/2fd01903d64c3c3d769aa85aa9ed0d08 to your computer and use it in GitHub Desktop.
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 | |
namespace App\Listener; | |
use Cake\Utility\Inflector; | |
use Crud\Listener\BaseListener; | |
use Crud\Event\Subject; | |
class ContainedModelsListener extends BaseListener { | |
public function beforeFind(\Cake\Event\Event $event) { | |
$this->containModels($event); | |
} | |
public function beforePaginate(\Cake\Event\Event $event) { | |
$this->paginateModels($event); | |
} | |
/** | |
* Gets the list of associated model lists to be fetched for an action | |
* | |
* @param string $action name of the action | |
* @return array | |
*/ | |
public function models($action = null) | |
{ | |
$settings = $this->containedModels(null, $action); | |
if (is_int($settings)) { | |
return $this->getContainedRecursive($settings); | |
} | |
if (empty($settings)) { | |
return []; | |
} | |
if (is_string($settings)) { | |
$settings = [$settings]; | |
} | |
return $settings; | |
} | |
/** | |
* Set or get the contained models that should be found | |
* for the action | |
* | |
* @param mixed $contained Everything but `null` will change the configuration | |
* @param string $action The action to configure | |
* @return mixed | |
*/ | |
public function containedModels($contained = null, $action = null) | |
{ | |
if ($contained === null) { | |
return $this->_action($action)->config('containedModels'); | |
} | |
debug($this->_action($action)->config('containedModels', $contained, false)); exit; | |
return $this->_action($action)->config('containedModels', $contained, false); | |
} | |
public function getContainedRecursive($level, $table = null, $path = '', $deep = 1) | |
{ | |
$names = []; | |
if($table === null) { | |
$table = $this->_table(); | |
} | |
if(!empty($path)) { | |
$path .= '.'; | |
} | |
foreach ($table->associations()->keys() as $association) { | |
$associationClass = $table->associations()->get($association); | |
$prefix = $associationClass->name(); | |
$prefix = $path . $prefix; | |
$names[] = $prefix; | |
if($level > 0) { | |
foreach($this->getContainedRecursive($level - 1, $associationClass->target(), $prefix, $deep + 3) as $recursive) { | |
$names[] = $recursive; | |
} | |
} | |
} | |
return $names; | |
} | |
public function containModels(\Cake\Event\Event $event, $action = null) { | |
$models = $this->models($action); | |
if(empty($models)) { | |
return; | |
} | |
$fetch = $this->_trigger('containModels', new Subject(compact('models'))); | |
$models = $fetch->subject->models; | |
$event->subject->query->contain($models); | |
} | |
public function paginateModels(\Cake\Event\Event $event, $action = null) { | |
$models = $this->models($action); | |
if(empty($models)) { | |
return; | |
} | |
$fetch = $this->_trigger('paginateModels', new Subject(compact('models'))); | |
$models = $fetch->subject->models; | |
$controller = $this->_controller(); | |
if(array_key_exists('contain', $controller->paginate)) { | |
$models = array_merge($controller->paginate['contain'], $models); | |
} | |
$controller->paginate['contain'] = $models; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment