View source
<?php
namespace Drupal\business_rules\Controller;
use Drupal\business_rules\Ajax\UpdateFlowchartCommand;
use Drupal\business_rules\BusinessRulesItemObject;
use Drupal\business_rules\ConditionInterface;
use Drupal\business_rules\Entity\Condition;
use Drupal\business_rules\Plugin\BusinessRulesCondition\ConditionSet;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\Core\Ajax\RemoveCommand;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
class ConditionSetController extends ControllerBase {
protected $entityTypeManager;
protected $item;
protected $itemType;
protected $items;
protected $itemsName;
protected $label;
protected $labelPlural;
protected $savedItems = [];
protected $util;
public function __construct(ContainerInterface $container) {
$this->entityTypeManager = $container
->get('entity_type.manager');
$this->util = $container
->get('business_rules.util');
}
public static function create(ContainerInterface $container) {
return new static($container);
}
public function addItem($condition_id, $item_id) {
$condition = Condition::load($condition_id);
$weight = $this
->getMaxItemWeight($condition) + 1;
$itemObj = new BusinessRulesItemObject($item_id, 'condition', $weight);
$items = $condition
->getSettings('items');
$item_array = $itemObj
->toArray();
$items[$itemObj
->getId()] = $item_array[$itemObj
->getId()];
$condition
->setSetting('items', $items);
$condition
->save();
$url = new Url('entity.business_rules_condition.edit_form', [
'business_rules_condition' => $condition_id,
], [
'fragment' => $item_id,
]);
return new RedirectResponse($url
->toString());
}
public function getMaxItemWeight(ConditionInterface $condition) {
$items = $condition
->getSettings('items');
$max = -100;
if (is_array($items)) {
foreach ($items as $item) {
if ($max < $item['weight']) {
$max = $item['weight'];
}
}
}
return $max;
}
public function itemsTable($condition_id, $method) {
$condition = Condition::load($condition_id);
$this
->init($condition);
$table['#title'] = $this
->t('Add @label_plural on %condition', [
'%condition' => $condition
->label(),
'@label_plural' => $this->labelPlural,
]);
$table['#attached']['library'][] = 'system/drupal.system.modules';
$table['filters'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'table-filter',
'js-show',
],
],
];
$table['filters']['text'] = [
'#type' => 'search',
'#title' => $this
->t('Search'),
'#size' => 30,
'#placeholder' => $this
->t('Search for a @label key', [
'@label' => $this->label,
]),
'#attributes' => [
'class' => [
'table-filter-text',
],
'data-table' => '.searchable-list',
'autocomplete' => 'off',
'title' => $this
->t('Enter a part of the @label key to filter by.', [
'@label' => $this->label,
]),
],
];
$header = [
'label' => $this->label,
'id' => $this
->t('Machine Name'),
'type' => $this
->t('Type'),
'description' => $this
->t('Description'),
'operations' => $this
->t('Operations'),
'filter' => [
'data' => [
'#markup' => 'filter',
],
'style' => 'display: none',
],
];
$rows = [];
foreach ($this->items as $item) {
if (!in_array($item
->id(), array_keys($this->savedItems)) && $item
->id() != $condition
->id()) {
$listBuilder = $this->entityTypeManager
->getListBuilder($item
->getEntityTypeId());
$operations = $listBuilder
->buildOperations($item);
$search_string = $item
->label() . ' ' . $item
->id() . ' ' . $item
->getTypeLabel() . ' ' . $item
->getDescription();
$link = Link::createFromRoute($item
->label(), 'business_rules.condition_set.items.add', [
'condition_id' => $condition_id,
'item_id' => $item
->id(),
]);
$rows[$item
->id()] = [
'label' => [
'data' => $link,
],
'id' => [
'data' => [
'#markup' => $item
->id(),
],
],
'type' => [
'data' => [
'#markup' => $item
->getTypeLabel(),
],
],
'description' => [
'data' => [
'#markup' => $item
->getDescription(),
],
],
'operations' => [
'data' => $operations,
],
'filter' => [
'data' => [
[
'#markup' => '<span class="table-filter-text-source">' . $search_string . '</span>',
],
],
'style' => [
'display: none',
],
],
];
}
}
$table['business_rule_items'] = [
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
'#attributes' => [
'class' => [
'searchable-list',
],
],
];
if ($method == 'ajax') {
$table['#attached']['library'][] = 'core/drupal.dialog.ajax';
$options = [
'width' => '75%',
];
$title = $this
->t('Add @item', [
'@item' => $this->label,
]);
$response = new AjaxResponse();
$response
->addCommand(new OpenModalDialogCommand($title, $table, $options));
return $response;
}
else {
return $table;
}
}
public function init(Condition $condition) {
$this->label = $this
->t('Condition');
$this->labelPlural = $this
->t('Conditions');
$this->items = ConditionSet::getAvailableItems($condition);
$this->itemsName = 'conditions';
$this->itemType = 'condition';
}
public function removeItem($condition_id, $item_id, $method) {
$condition = Condition::load($condition_id);
$items = $condition
->getSettings('items');
unset($items[$item_id]);
$items = is_null($items) ? [] : $items;
$condition
->setSetting('items', $items);
$condition
->save();
if ($method == 'ajax') {
$chart_definition = $this->util->flowchart
->getGraphDefinition($condition);
$textarea = '<textarea id="graph_definition" style="display: none;">' . $chart_definition . '</textarea>';
$response = new AjaxResponse();
$response
->addCommand(new RemoveCommand('#' . $item_id));
$response
->addCommand(new ReplaceCommand('#graph_definition', $textarea));
$response
->addCommand(new UpdateFlowchartCommand());
return $response;
}
else {
$url = new Url('entity.business_rules_condition.edit_form', [
'business_rule_condition' => $condition_id,
]);
$string_url = $url
->toString() . '#business_rule-add_buttons';
return new RedirectResponse($string_url);
}
}
}