You are here

public function MeetingForm::save in Opigno Moxtra 3.x

Same name and namespace in other branches
  1. 8 src/Form/MeetingForm.php \Drupal\opigno_moxtra\Form\MeetingForm::save()

Form submission handler for the 'save' action.

Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides EntityForm::save

File

src/Form/MeetingForm.php, line 256

Class

MeetingForm
Provides a form for creating/editing a opigno_moxtra_meeting entity.

Namespace

Drupal\opigno_moxtra\Form

Code

public function save(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\opigno_moxtra\MeetingInterface $entity */
  $entity = $this->entity;
  $current_members_ids = [];
  $current_members = !empty($form['members']['#default_value']) ? $form['members']['#default_value'] : [];
  foreach ($current_members as $current_member) {
    list($type, $id) = explode('_', $current_member);
    $current_members_ids[] = $id;
  }
  $date = $form_state
    ->getValue('date');
  if (isset($date[0]['value'])) {
    $start_date = $date[0]['value'];
  }
  if (isset($date[0]['end_value'])) {
    $end_date = $date[0]['end_value'];
  }
  $start_date_value = isset($start_date) ? $start_date
    ->setTimezone(new \DateTimeZone(date_default_timezone_get()))
    ->format(DrupalDateTime::FORMAT) : NULL;
  $end_date_value = isset($end_date) ? $end_date
    ->setTimezone(new \DateTimeZone(date_default_timezone_get()))
    ->format(DrupalDateTime::FORMAT) : NULL;
  $date_range = [
    'value' => $start_date_value,
    'end_value' => $end_date_value,
  ];
  $entity
    ->setDate($date_range);

  // Load added users & classes from the form_state.
  $users_ids = [];
  $classes_ids = [];
  $invite_users = [];
  $owner_id = $entity
    ->getOwnerId();
  $options = $form_state
    ->getValue('members');
  if ($options) {
    if (count($options)) {
      $options['user_' . $owner_id] = 'user_' . $owner_id;
    }
    if (!empty($options)) {
      foreach ($options as $option) {
        list($type, $id) = explode('_', $option);
        if ($type === 'user') {
          $users_ids[] = $id;
        }
        elseif ($type === 'class') {
          $classes_ids[] = $id;
        }
      }
      $classes = Group::loadMultiple($classes_ids);
      foreach ($classes as $class) {

        // Add class members to the users.

        /** @var \Drupal\group\Entity\Group $class */
        $members = $class
          ->getMembers();
        foreach ($members as $member) {

          /** @var \Drupal\group\GroupMembership $member */
          $user = $member
            ->getUser();
          $users_ids[] = $user
            ->id();
        }
      }
      $entity
        ->setMembersIds($users_ids);
    }
  }
  $users = User::loadMultiple($users_ids);
  foreach ($users as $account) {
    $prefix = $this->moxtraService
      ->prefix($account);
    $invite_users[] = [
      'user' => [
        'unique_id' => $prefix . $account
          ->id(),
      ],
    ];
  }
  $this->moxtraService
    ->AddUsersToMeeting($entity
    ->getOwnerId(), $entity
    ->getSessionKey(), $invite_users);

  // Save entity.
  $status = parent::save($form, $form_state);

  // Prepare email notifications.
  $mail_service = \Drupal::service('plugin.manager.mail');
  $params = [];
  $params['subject'] = $params['message'] = t('Created new Live Meeting %meeting', [
    '%meeting' => $entity
      ->getTitle(),
  ]);
  if (\Drupal::hasService('opigno_calendar_event.iCal')) {
    $params['attachments'] = opigno_moxtra_ical_prepare($entity);
  }
  $module = 'opigno_moxtra';
  $key = 'upcoming_meeting_notify';

  // Set status message.
  $meeting_link = $entity
    ->toLink()
    ->toString();
  if ($status == SAVED_UPDATED) {
    $message = $this
      ->t('The Live Meeting %meeting has been updated.', [
      '%meeting' => $meeting_link,
    ]);

    // Send email notifications about meeting for added users.
    foreach ($users as $user) {
      if (!in_array($user
        ->id(), $current_members_ids)) {
        $to = $user
          ->getEmail();
        $langcode = $user
          ->getPreferredLangcode();
        $mail_service
          ->mail($module, $key, $to, $langcode, $params, NULL, TRUE);
      }
    }
  }
  else {
    $message = $this
      ->t('The Live Meeting %meeting has been created.', [
      '%meeting' => $meeting_link,
    ]);
    if (empty($options) && ($training = $entity
      ->getTraining())) {
      $memberships = $training
        ->getMembers();
      if ($memberships) {
        foreach ($memberships as $membership) {
          $uid = $membership
            ->getUser()
            ->id();
          if ($uid != $entity
            ->getOwner()
            ->id()) {
            $users_ids[] = $uid;
          }
        }
      }
    }

    // Send email notifications about new meeting for users.
    foreach ($users as $user) {
      $to = $user
        ->getEmail();
      $langcode = $user
        ->getPreferredLangcode();
      $mail_service
        ->mail($module, $key, $to, $langcode, $params, NULL, TRUE);
    }
  }
  $this
    ->messenger()
    ->addMessage($message);

  // Set redirect.
  $form_state
    ->setRedirect('opigno_moxtra.meeting', [
    'opigno_moxtra_meeting' => $entity
      ->id(),
  ]);
  return $status;
}