You are here

public function MaestroTaskTrait::getAssignmentsAndNotificationsForm in Maestro 8.2

Same name and namespace in other branches
  1. 3.x src/MaestroTaskTrait.php \Drupal\maestro\MaestroTaskTrait::getAssignmentsAndNotificationsForm()

Retrieve the core Maestro form edit elements for Assignments and Notifications.

Parameters

array $task: The task loaded from the template.

string $templateMachineName: The Maestro template's machine name.

File

src/MaestroTaskTrait.php, line 143

Class

MaestroTaskTrait
MaestroTaskTrait.

Namespace

Drupal\maestro

Code

public function getAssignmentsAndNotificationsForm(array $task, $templateMachineName) {
  $variables = MaestroEngine::getTemplateVariables($templateMachineName);
  $options = [];
  foreach ($variables as $variableName => $arr) {
    $options[$variableName] = $variableName;
  }

  // Assignments section.
  $form['assignments'] = [
    '#title' => $this
      ->t('Assignments'),
  ];
  $form['edit_task_assignments'] = [
    '#tree' => TRUE,
    '#type' => 'details',
    '#group' => 'assignments',
    '#title' => 'Assignment Details',
  ];

  // The following are the assignment mechanisms.
  $form['edit_task_assignments']['select_method'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Assign by'),
    '#options' => [
      'fixed' => $this
        ->t('Fixed Value'),
      'variable' => $this
        ->t('Variable'),
    ],
    '#default_value' => 'fixed',
    '#attributes' => [
      'onchange' => 'maestro_task_editor_assignments_assignby(this.value);',
    ],
  ];

  /*
   * Developers:  You can add to the onchange for this as you see fit to allow for other types
   */
  $form['edit_task_assignments']['select_assign_to'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Assign to'),
    '#options' => [
      'user' => $this
        ->t('User'),
      'role' => $this
        ->t('Role'),
    ],
    '#default_value' => 'user',
    '#attributes' => [
      'onchange' => 'maestro_task_editor_assignments_assignto(this.value);',
    ],
  ];
  $form['edit_task_assignments']['select_assigned_user'] = [
    '#id' => 'select_assigned_user',
    '#type' => 'entity_autocomplete',
    '#target_type' => 'user',
    '#default_value' => '',
    '#selection_settings' => [
      'include_anonymous' => FALSE,
    ],
    '#title' => $this
      ->t('User'),
    '#required' => FALSE,
    '#prefix' => '<div class="maestro-engine-user-and-role"><div class="maestro-engine-assignments-hidden-user">',
    '#suffix' => '</div>',
  ];
  $form['edit_task_assignments']['select_assigned_role'] = [
    '#id' => 'select_assigned_role',
    '#type' => 'textfield',
    '#default_value' => '',
    '#title' => $this
      ->t('Role'),
    '#autocomplete_route_name' => 'maestro.autocomplete.roles',
    '#required' => FALSE,
    '#prefix' => '<div class="maestro-engine-assignments-hidden-role">',
    '#suffix' => '</div></div>',
  ];
  $form['edit_task_assignments']['variable'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Choose the variable'),
    '#required' => FALSE,
    '#default_value' => '',
    '#options' => $options,
    '#prefix' => '<div class="maestro-engine-assignments-hidden-variable">',
    '#suffix' => '</div>',
  ];

  // Now to list the existing assignments here:
  $form['edit_task_assignments']['task_assignment_table'] = [
    '#type' => 'table',
    '#tree' => TRUE,
    '#header' => [
      $this
        ->t('Delete'),
      $this
        ->t('To What'),
      $this
        ->t('By'),
      $this
        ->t('Assignee'),
    ],
    '#empty' => t('There are no assignments.'),
  ];
  isset($task['assigned']) ? $assignments = explode(',', $task['assigned']) : ($assignments = []);
  $cntr = 0;
  foreach ($assignments as $assignment) {
    if ($assignment != '') {

      // [0]=to what, [1]=by fixed or variable, [2]=who or varname
      $howAssigned = explode(':', $assignment);
      $form['edit_task_assignments']['task_assignment_table'][$cntr]['delete'] = [
        '#type' => 'checkbox',
        '#default_value' => 0,
      ];
      $form['edit_task_assignments']['task_assignment_table'][$cntr]['to_what'] = [
        '#plain_text' => $howAssigned[0],
      ];
      $form['edit_task_assignments']['task_assignment_table'][$cntr]['by'] = [
        '#plain_text' => $howAssigned[1],
      ];
      $form['edit_task_assignments']['task_assignment_table'][$cntr]['asignee'] = [
        '#plain_text' => $howAssigned[2],
      ];
      $cntr++;
    }
  }

  // End of assignments section.
  // Notifications section.
  $form['notifications'] = [
    '#title' => $this
      ->t('Notifications'),
  ];
  $form['edit_task_notifications'] = [
    '#tree' => TRUE,
    '#type' => 'details',
    '#group' => 'notifications',
    '#title' => 'Notification Details',
  ];
  $form['edit_task_notifications']['select_notification_method'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Notify by'),
    '#options' => [
      'fixed' => $this
        ->t('Fixed Value'),
      'variable' => $this
        ->t('Variable'),
    ],
    '#default_value' => 'fixed',
    '#attributes' => [
      'onchange' => 'maestro_task_editor_notifications_assignby(this.value);',
    ],
  ];
  $form['edit_task_notifications']['select_notification_to'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Notification to'),
    '#options' => [
      'user' => $this
        ->t('User'),
      'role' => $this
        ->t('Role'),
    ],
    '#default_value' => 'user',
    '#attributes' => [
      'onchange' => 'maestro_task_editor_notifications_assignto(this.value);',
    ],
  ];
  $form['edit_task_notifications']['select_notification_user'] = [
    '#id' => 'select_notification_user',
    '#type' => 'entity_autocomplete',
    '#target_type' => 'user',
    '#default_value' => '',
    '#selection_settings' => [
      'include_anonymous' => FALSE,
    ],
    '#title' => $this
      ->t('User'),
    '#required' => FALSE,
    '#prefix' => '<div class="maestro-engine-user-and-role-notifications"><div class="maestro-engine-notifications-hidden-user">',
    '#suffix' => '</div>',
  ];
  $form['edit_task_notifications']['select_notification_role'] = [
    '#id' => 'select_notification_role',
    '#type' => 'textfield',
    '#default_value' => '',
    '#title' => $this
      ->t('Role'),
    '#autocomplete_route_name' => 'maestro.autocomplete.roles',
    '#required' => FALSE,
    '#prefix' => '<div class="maestro-engine-notifications-hidden-role">',
    '#suffix' => '</div></div>',
  ];
  $form['edit_task_notifications']['variable'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Choose the variable'),
    '#required' => FALSE,
    '#default_value' => '',
    '#options' => $options,
    '#prefix' => '<div class="maestro-engine-notifications-hidden-variable">',
    '#suffix' => '</div>',
  ];
  $whichNotification = [
    'assignment' => $this
      ->t('Assignment'),
    'reminder' => $this
      ->t('Reminder'),
    'escalation' => $this
      ->t('Escalation'),
  ];
  $form['edit_task_notifications']['which_notification'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Which notification'),
    '#required' => FALSE,
    '#default_value' => 'assignment',
    '#options' => $whichNotification,
    '#prefix' => '<div class="">',
    '#suffix' => '</div>',
  ];
  $form['edit_task_notifications']['reminder_after'] = [
    '#type' => 'textfield',
    '#default_value' => '0',
    '#title' => $this
      ->t('Reminder After (days)'),
    '#required' => FALSE,
    '#size' => 2,
    '#prefix' => '<div class="maestro-engine-reminder-escalation-values"><div class="maestro-reminder-wrapper">',
    '#suffix' => '</div>',
  ];
  $form['edit_task_notifications']['escalation_after'] = [
    '#type' => 'textfield',
    '#default_value' => '0',
    '#title' => $this
      ->t('Escalation After (days)'),
    '#required' => FALSE,
    '#size' => 2,
    '#prefix' => '<div class="maestro-escalation-wrapper">',
    '#suffix' => '</div></div>',
  ];

  // Now to list the existing assignments here:
  $form['edit_task_notifications']['task_notifications_table'] = [
    '#type' => 'table',
    '#tree' => TRUE,
    '#header' => [
      $this
        ->t('Delete'),
      $this
        ->t('To What'),
      $this
        ->t('By'),
      $this
        ->t('Assignee'),
      $this
        ->t('Notification Type'),
    ],
    '#empty' => t('There are no notifications.'),
  ];
  if (array_key_exists('notifications', $task) && array_key_exists('notification_assignments', $task['notifications'])) {
    $notifications = explode(',', $task['notifications']['notification_assignments']);
    $cntr = 0;
    foreach ($notifications as $notification) {
      if ($notification != '') {

        // [0]=to what, [1]=by fixed or variable, [2]=who or varname, [3] which notification
        $howAssigned = explode(':', $notification);
        $form['edit_task_notifications']['task_notifications_table'][$cntr]['delete'] = [
          '#type' => 'checkbox',
          '#default_value' => 0,
        ];
        $form['edit_task_notifications']['task_notifications_table'][$cntr]['to_what'] = [
          '#plain_text' => $howAssigned[0],
        ];
        $form['edit_task_notifications']['task_notifications_table'][$cntr]['by'] = [
          '#plain_text' => $howAssigned[1],
        ];
        $form['edit_task_notifications']['task_notifications_table'][$cntr]['asignee'] = [
          '#plain_text' => $howAssigned[2],
        ];
        $form['edit_task_notifications']['task_notifications_table'][$cntr]['type'] = [
          '#plain_text' => $howAssigned[3],
        ];
        $cntr++;
      }
    }
  }
  if (\Drupal::moduleHandler()
    ->moduleExists('token')) {
    $form['edit_task_notifications']['token_tree'] = [
      '#theme' => 'token_tree_link',
      '#token_types' => [
        'maestro',
      ],
    ];
  }
  else {
    $form['edit_task_notifications']['token_tree'] = [
      '#plain_text' => $this
        ->t('Enabling the Token module will reveal the replacable tokens available for custom notifications.'),
    ];
  }
  $form['edit_task_notifications']['notification_assignment_subject'] = [
    '#type' => 'textarea',
    '#default_value' => isset($task['notifications']['notification_assignment_subject']) ? $task['notifications']['notification_assignment_subject'] : '',
    '#title' => $this
      ->t('Custom Assignment Subject'),
    '#required' => FALSE,
    '#rows' => 1,
    '#prefix' => '<div class="maestro-engine-assignments-hidden-notification">',
    '#suffix' => '</div>',
  ];
  $form['edit_task_notifications']['notification_assignment'] = [
    '#id' => 'notification_assignment',
    '#type' => 'textarea',
    '#default_value' => isset($task['notifications']['notification_assignment']) ? $task['notifications']['notification_assignment'] : '',
    '#title' => $this
      ->t('Custom Assignment Message'),
    '#required' => FALSE,
    '#rows' => 2,
    '#prefix' => '<div class="maestro-engine-assignments-hidden-notification">',
    '#suffix' => '</div>',
  ];
  $form['edit_task_notifications']['notification_reminder_subject'] = [
    '#type' => 'textarea',
    '#default_value' => isset($task['notifications']['notification_reminder_subject']) ? $task['notifications']['notification_reminder_subject'] : '',
    '#title' => $this
      ->t('Custom Reminder Subject'),
    '#required' => FALSE,
    '#rows' => 1,
    '#prefix' => '<div class="maestro-engine-assignments-hidden-notification">',
    '#suffix' => '</div>',
  ];
  $form['edit_task_notifications']['notification_reminder'] = [
    '#id' => 'notification_reminder',
    '#type' => 'textarea',
    '#default_value' => isset($task['notifications']['notification_reminder']) ? $task['notifications']['notification_reminder'] : '',
    '#title' => $this
      ->t('Custom Reminder Message'),
    '#required' => FALSE,
    '#rows' => 2,
    '#prefix' => '<div class="maestro-engine-assignments-hidden-escalation">',
    '#suffix' => '</div>',
  ];
  $form['edit_task_notifications']['notification_escalation_subject'] = [
    '#type' => 'textarea',
    '#default_value' => isset($task['notifications']['notification_escalation_subject']) ? $task['notifications']['notification_escalation_subject'] : '',
    '#title' => $this
      ->t('Custom Escalation Subject'),
    '#required' => FALSE,
    '#rows' => 1,
    '#prefix' => '<div class="maestro-engine-assignments-hidden-notification">',
    '#suffix' => '</div>',
  ];
  $form['edit_task_notifications']['notification_escalation'] = [
    '#id' => 'notification_escalation',
    '#type' => 'textarea',
    '#default_value' => isset($task['notifications']['notification_escalation']) ? $task['notifications']['notification_escalation'] : '',
    '#title' => $this
      ->t('Custom Escalation Message'),
    '#required' => FALSE,
    '#rows' => 2,
    '#prefix' => '<div class="maestro-engine-assignments-hidden-escalation">',
    '#suffix' => '</div>',
  ];
  $form['#attached']['library'][] = 'maestro/maestro-engine-css';
  $form['#attached']['library'][] = 'maestro/maestro-engine-task-edit';
  return $form;
}