BusinessRule.php in Business Rules 8
File
src/Entity/BusinessRule.php
View source
<?php
namespace Drupal\business_rules\Entity;
use Drupal\business_rules\BusinessRuleInterface;
use Drupal\business_rules\BusinessRulesItemObject;
use Drupal\Core\Config\Entity\ConfigEntityBase;
class BusinessRule extends ConfigEntityBase implements BusinessRuleInterface {
protected static $reactsOnManager;
protected $configFactory;
protected $description;
protected $id;
protected $items = [];
protected $label;
protected $reacts_on;
protected $status;
protected $tags = [];
protected $target_bundle;
protected $target_entity_type;
protected $util;
public function __construct(array $values, $entity_type = 'business_rule') {
parent::__construct($values, $entity_type);
$this->util = \Drupal::getContainer()
->get('business_rules.util');
$this->configFactory = \Drupal::getContainer()
->get('config.factory');
self::$reactsOnManager = \Drupal::getContainer()
->get('plugin.manager.business_rules.reacts_on');
}
public function getActions() {
return is_array($this->actions) ? $this->actions : [];
}
public function getConditions() {
return is_array($this->conditions) ? $this->conditions : [];
}
public function getVariables() {
return is_array($this->variables) ? $this->variables : [];
}
public function save() {
$context_items = [];
if (count($this->items)) {
foreach ($this->items as $key => $item) {
if (!$item instanceof BusinessRulesItemObject) {
$item = new BusinessRulesItemObject($item['id'], $item['type'], $item['weight']);
}
if ($this
->checkItemContext($item)) {
$context_items[$key] = $item
->toArray()[$key];
}
}
}
$this->items = $context_items;
return parent::save();
}
public function checkItemContext(BusinessRulesItemObject $itemObject) {
if ($itemObject
->getType() == 'condition') {
$item = Condition::load($itemObject
->getId());
}
elseif ($itemObject
->getType() == 'action') {
$item = Action::load($itemObject
->getId());
}
if (empty($item)) {
return FALSE;
}
$entity_type = $this
->getTargetEntityType();
$bundle = $this
->getTargetBundle();
if (($item
->getTargetEntityType() == $entity_type || empty($item
->getTargetEntityType())) && ($item
->getTargetBundle() == $bundle || empty($item
->getTargetBundle())) && (in_array($this
->getReactsOn(), $item
->getReactOnEvents()) || count($item
->getReactOnEvents()) === 0) || !$item
->isContextDependent()) {
return TRUE;
}
else {
return FALSE;
}
}
public function getTargetEntityType() {
return $this->target_entity_type;
}
public function getTargetBundle() {
return $this->target_bundle;
}
public function getReactsOn() {
return $this->reacts_on;
}
public function getItemMaxWeight() {
$items = $this
->getItems();
$max = -10;
if (is_array($items)) {
foreach ($items as $item) {
if ($max < $item
->getWeight()) {
$max = $item
->getWeight();
}
}
}
return $max;
}
public function getItems() {
$obj_items = BusinessRulesItemObject::itemsArrayToItemsObject($this->items);
return $obj_items;
}
public function getItem($item_id) {
if (isset($this->items[$item_id])) {
$item = $this->items[$item_id];
$itemObj = new BusinessRulesItemObject($item['id'], $item['type'], $item['weight']);
return $itemObj;
}
else {
return NULL;
}
}
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 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 addItem(BusinessRulesItemObject $item) {
$item_array = $item
->toArray();
$this->items[$item
->getId()] = $item_array[$item
->getId()];
}
public function removeItem(BusinessRulesItemObject $item) {
unset($this->items[$item
->getId()]);
}
public function getTargetBundleLabel() {
$bundles = $this->util
->getBundles($this
->getTargetEntityType());
foreach ($bundles as $key => $value) {
if ($key == $this
->getTargetBundle()) {
if ($key === '') {
return t('All');
}
return $value;
}
}
return '';
}
public function getDescription() {
return $this->description;
}
public function isEnabled() {
return $this->status;
}
public function setEnabled($status) {
$this->status = $status;
}
public function getReactsOnLabel() {
$reacts = self::getEventTypes();
foreach ($reacts as $react) {
foreach ($react as $key => $value) {
if ($key == $this
->getReactsOn()) {
return $value;
}
}
}
return '';
}
public static function getEventTypes() {
$types = [];
$events = self::$reactsOnManager
->getDefinitions();
uasort($events, function ($a, $b) {
return $a['label']
->render() > $b['label']
->render() ? 1 : -1;
});
foreach ($events as $event) {
if (isset($types[$event['group']
->render()])) {
$types[$event['group']
->render()] += [
$event['id'] => $event['label'],
];
}
else {
$types[$event['group']
->render()] = [
$event['id'] => $event['label'],
];
}
}
ksort($types);
return $types;
}
public function getTargetEntityTypeLabel() {
$entities = $this->util
->getEntityTypes();
foreach ($entities as $key => $value) {
if ($key == $this
->getTargetEntityType()) {
return $value;
}
}
return '';
}
public function filterContextAvailableItems(array $items) {
$entity_type = $this
->getTargetEntityType();
$bundle = $this
->getTargetBundle();
$available_items = [];
foreach ($items as $key => $value) {
if (($value
->getTargetEntityType() == $entity_type || empty($value
->getTargetEntityType())) && ($value
->getTargetBundle() == $bundle || empty($value
->getTargetBundle())) && (in_array($this
->getReactsOn(), $value
->getReactOnEvents()) || count($value
->getReactOnEvents()) === 0) || !$value
->isContextDependent()) {
$available_items[$key] = $value;
}
}
return $available_items;
}
public function calculateDependencies() {
parent::calculateDependencies();
foreach ($this
->getItems() as $item) {
$this
->addDependency('config', $item
->loadEntity()
->getConfigDependencyName());
}
return $this;
}
}