View source
<?php
namespace Drupal\activity\Form;
use Drupal\activity\QueryActivity;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\user\Entity\Role;
use Drupal\token\TokenEntityMapperInterface;
use Drupal\token\TreeBuilderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\SessionManagerInterface;
use Drupal\Core\Url;
use Drupal\user\PrivateTempStoreFactory;
class ConfigureActivityForm extends MultiStepFormBase {
protected $treeBuilder;
protected $entityMapper;
protected $database;
protected $activityService;
protected $entityTypeManager;
protected $moduleHandler;
protected $label;
protected $hook;
protected $eventId;
public function __construct(TreeBuilderInterface $tree_builder, TokenEntityMapperInterface $entity_mapper, Connection $database, AccountInterface $currentUser, SessionManagerInterface $sessionManager, PrivateTempStoreFactory $tempStoreFactory, QueryActivity $activityService, EntityTypeManagerInterface $entityTypeManager, ModuleHandlerInterface $moduleHandler) {
parent::__construct($currentUser, $sessionManager, $tempStoreFactory);
$this->treeBuilder = $tree_builder;
$this->entityMapper = $entity_mapper;
$this->database = $database;
$this->activityService = $activityService;
$this->entityTypeManager = $entityTypeManager;
$this->moduleHandler = $moduleHandler;
$this->label = $this->store
->get('label');
$this->hook = $this->store
->get('hook');
}
public static function create(ContainerInterface $container) {
return new static($container
->get('token.tree_builder'), $container
->get('token.entity_mapper'), $container
->get('database'), $container
->get('current_user'), $container
->get('session_manager'), $container
->get('user.private_tempstore'), $container
->get('query_activity'), $container
->get('entity_type.manager'), $container
->get('module_handler'));
}
public function getFormId() {
return 'configure_activities_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$windowDefault = '';
$rolesDefault = '';
$typesDefault = '';
$messageDefault = '';
$form = parent::buildForm($form, $form_state);
$current_path = \Drupal::service('path.current')
->getPath();
$result = \Drupal::service('path.alias_manager')
->getAliasByPath($current_path);
$path_args = explode('/', $result);
$this->eventId = $path_args[4];
if ($this->eventId != 'new') {
$query = $this->activityService
->getActivityEventField($this->eventId, 'label');
$this->label = $query[0]->label;
$query = $this->activityService
->getActivityEventField($this->eventId, 'hook');
$this->hook = $query[0]->hook;
}
$messageJson = $this->activityService
->getActivityEventField($this->eventId, 'message');
if (!empty($messageJson)) {
$activityMessage = json_decode($messageJson[0]->message);
$windowDefault = $activityMessage->window;
$rolesDefault = $activityMessage->roles;
$typesDefault = $activityMessage->types;
$messageDefault = $activityMessage->message;
}
$form['activity_label'] = [
'#type' => 'textfield',
'#title' => t('Label'),
'#default_value' => $this->label,
'#required' => TRUE,
'#size' => 30,
'#attributes' => [
'class' => [
'activity_label',
],
],
];
$timeIntervals = range(0, 7200, 300);
$options = $this
->buildOptions($timeIntervals);
$options[0] = 'Unlimited';
$form['activity_window'] = [
'#type' => 'select',
'#title' => t('Activity Window'),
'#description' => t('Prevent repeat Activity from the same user for the same entity within this interval'),
'#options' => $options,
'#default_value' => $windowDefault == '' ? $this->options['window'] : $windowDefault,
];
if (strpos($this->hook, 'user') !== FALSE) {
$roles = Role::loadMultiple();
$roleOptions = [];
foreach ($roles as $role => $value) {
$roleOptions[$role] = $role;
}
$form['activity_roles'] = [
'#type' => 'checkboxes',
'#title' => t('Roles'),
'#default_value' => $rolesDefault,
'#options' => $roleOptions,
];
}
else {
$bundles = $this->entityTypeManager
->getStorage('node_type')
->loadMultiple();
$contentTypes = array_keys($bundles);
$types = array_combine($contentTypes, $contentTypes);
$form['activity_node_types'] = [
'#type' => 'checkboxes',
'#title' => t('Allowed Node Types'),
'#default_value' => $typesDefault,
'#options' => $types,
];
}
$form['activity_message'] = [
'#type' => 'textarea',
'#title' => t('Public Message'),
'#description' => t('Message displayed to everyone who is not part of this Activity.'),
'#default_value' => $messageDefault,
];
$moduleHandler = $this->moduleHandler;
if ($moduleHandler
->moduleExists('token')) {
$token_tree = $this->treeBuilder
->buildAllRenderable([
'click_insert' => TRUE,
'show_restricted' => TRUE,
'show_nested' => FALSE,
]);
$tokenFor = [
'current-date' => 'current-date',
'current-page' => 'current-page',
'current-user' => 'current-user',
'node' => 'node',
'user' => 'user',
'random' => 'random',
'site' => 'site',
];
if (strpos($this->hook, 'user') !== FALSE) {
unset($tokenFor['node']);
}
elseif (strpos($this->hook, 'comment') !== FALSE) {
$tokenFor['comment'] = 'comment';
}
foreach ($token_tree['#token_tree'] as $key => $value) {
if (!in_array($key, $tokenFor)) {
unset($token_tree['#token_tree'][$key]);
}
}
$form['token_help'] = [
'#type' => 'markup',
'#markup' => \Drupal::service('renderer')
->render($token_tree),
];
}
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Save'),
];
return $form;
}
public function buildOptions(array $timeIntervals) {
$callback = [
$this,
'callbackOptions',
];
return array_combine($timeIntervals, array_map($callback, $timeIntervals));
}
public function callbackOptions($value, $langcode, $granularity = 2) {
return \Drupal::service('date.formatter')
->formatInterval($value, $granularity, $langcode);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($this->hook == NULL) {
$query = $this->activityService
->getActivityEventField($this->eventId, 'hook');
$this->hook = $query[0]->hook;
}
$userId = \Drupal::currentUser()
->id();
$this->label = $form_state
->getValue('activity_label');
$windowOption = $form_state
->getValue('activity_window');
$roleOptions = $form_state
->getValue('activity_roles') == NULL ? NULL : array_filter(array_values($form_state
->getValue('activity_roles')));
$contentTypes = $form_state
->getValue('activity_node_types') == NULL ? NULL : array_filter(array_values($form_state
->getValue('activity_node_types')));
$message = $form_state
->getValue('activity_message');
$messageArray = [
'window' => $windowOption,
'roles' => $roleOptions,
'types' => $contentTypes,
'message' => $message,
];
if ($this->eventId != 'new') {
$this->database
->update('activity_events')
->fields([
'label' => $this->label,
'hook' => $this->hook,
'userId' => $userId,
'message' => json_encode($messageArray),
])
->condition('event_id', $this->eventId)
->execute();
}
else {
$this->database
->insert('activity_events')
->fields([
'label' => $this->label,
'hook' => $this->hook,
'userId' => $userId,
'created' => \Drupal::time()
->getCurrentTime(),
'message' => json_encode($messageArray),
])
->execute();
}
$this
->deleteStore();
$url = Url::fromUri('internal:/admin/activity');
$form_state
->setRedirectUrl($url);
}
}