BusinessRulesItemBase.php in Business Rules 8
File
src/Entity/BusinessRulesItemBase.php
View source
<?php
namespace Drupal\business_rules\Entity;
use Drupal\business_rules\Events\BusinessRulesEvent;
use Drupal\business_rules\ItemInterface;
use Drupal\Core\Config\Entity\ConfigEntityBase;
abstract class BusinessRulesItemBase extends ConfigEntityBase implements ItemInterface {
protected $description;
protected $eventDispatcher;
protected $id;
protected $itemManager;
protected $label;
protected $settings = [];
protected $tags = [];
protected $target_bundle;
protected $target_entity_type;
protected $type;
protected $util;
public function __construct(array $values, $entity_type) {
parent::__construct($values, $entity_type);
$this->itemManager = $this
->getItemManager();
$this->eventDispatcher = \Drupal::getContainer()
->get('event_dispatcher');
$this->util = \Drupal::getContainer()
->get('business_rules.util');
}
public abstract function getItemManager();
public function getDescription() {
return $this->description;
}
public function getReactOnEvents() {
$definition = $this->itemManager
->getDefinition($this
->getType());
if (array_key_exists('reactsOnIds', $definition)) {
return $definition['reactsOnIds'];
}
else {
return [];
}
}
public function getType() {
return $this->type;
}
public function getSettings($settingId = '') {
if ($settingId == '') {
return $this->settings;
}
elseif (empty($this->settings[$settingId])) {
if (array_key_exists($settingId, $this->settings)) {
if ($this->settings[$settingId] === 0 || $this->settings[$settingId] === "0") {
$value = 0;
}
else {
$value = NULL;
}
}
else {
$value = NULL;
}
}
else {
$value = $this->settings[$settingId];
}
return $value;
}
public function setSetting($settingId, $value) {
if (!empty($settingId)) {
$this->settings[$settingId] = $value;
}
else {
throw new \Exception('You must enter a value to the settingId');
}
}
public function getTags() {
return $this->tags;
}
public function setTags(array $tags) {
$formatted_tags = [];
foreach ($tags as $tag) {
if ($tag != '') {
$this->util
->toSafeLowerString($tag);
$formatted_tags[$tag] = $tag;
}
}
ksort($formatted_tags);
$this->tags = $formatted_tags;
}
public function getTargetBundle() {
return $this->target_bundle;
}
public function getTargetEntityType() {
return $this->target_entity_type;
}
public function getTypeLabel() {
$types = $this
->getTypes();
foreach ($types as $type) {
foreach ($type as $key => $value) {
if ($key == $this
->getType()) {
return $value;
}
}
}
return '';
}
public function getTypes() {
$types = [];
$items = $this->itemManager
->getDefinitions();
uasort($items, function ($a, $b) {
return $a['label']
->render() > $b['label']
->render() ? 1 : -1;
});
foreach ($items as $item) {
if (isset($types[$item['group']
->render()])) {
$types[$item['group']
->render()] += [
$item['id'] => $item['label'],
];
}
else {
$types[$item['group']
->render()] = [
$item['id'] => $item['label'],
];
}
}
ksort($types);
return $types;
}
public function isContextDependent() {
$type = $this
->getType();
$definition = $this
->getItemManager()
->getDefinition($type);
return $definition['isContextDependent'];
}
public static function loadMultipleByType($type, array $ids = NULL) {
$items = self::loadMultiple($ids);
$result = [];
foreach ($items as $item) {
if ($item
->getType() == $type) {
$result[] = $item;
}
}
return $result;
}
public function getVariables() {
$item_type = $this->itemManager
->getDefinition($this
->getType());
$reflection = new \ReflectionClass($item_type['class']);
$defined_item = $reflection
->newInstance($item_type, $item_type['id'], $item_type);
$variables = $defined_item
->getVariables($this);
return $variables;
}
public static function loadAllTags() {
$business_rules = self::loadMultiple();
$tags = [];
foreach ($business_rules as $business_rule) {
if (count($business_rule
->getTags())) {
foreach ($business_rule
->getTags() as $key => $value) {
if ($key != '' || $value != '') {
$tags[$key] = $value;
}
}
}
}
ksort($tags);
return $tags;
}
public function delete() {
parent::delete();
if (!$this
->isNew()) {
$event = new BusinessRulesEvent($this);
$this->eventDispatcher
->dispatch('business_rules.item_pos_delete', $event);
}
}
public function calculateDependencies() {
parent::calculateDependencies();
$plugin_type = $this->itemManager
->getDefinition($this
->getType());
$reflection = new \ReflectionClass($plugin_type['class']);
$defined_item = $reflection
->newInstance($plugin_type, $plugin_type['id'], $plugin_type);
foreach ($defined_item
->getVariables($this)
->getVariablesIds() as $id) {
if ($variable = Variable::load($id)) {
$this
->addDependency('config', $variable
->getConfigDependencyName());
}
}
return $this;
}
}