You are here

public function StatusForm::statusAjaxSubmit in Heartbeat 8

File

modules/statusmessage/src/Form/StatusForm.php, line 204

Class

StatusForm
Form controller for Status edit forms.

Namespace

Drupal\statusmessage\Form

Code

public function statusAjaxSubmit(array &$form, FormStateInterface $form_state) {
  $message = $form_state
    ->getValue('message');
  $file = $form_state
    ->getValue('media');
  if (strlen(trim($message)) > 1) {
    preg_match_all('#\\bhttps?://[^,\\s()<>]+(?:\\([\\w\\d]+\\)|([^,[:punct:]\\s]|/))#', $message, $match);
    if ($this->markupgenerator !== NULL && !empty($match) && array_values($match)[0] !== NULL) {
      $url = is_array(array_values($match)[0]) ? array_values(array_values($match)[0])[0] : array_values($match)[0];
      if (strpos($message, 'twitter')) {
        $statusTwitter = new StatusTwitter($url);
        $nid = $statusTwitter
          ->sendRequest();
      }
      else {
        if (strpos($message, 'youtube') || strpos($message, 'youtu.be')) {
          $statusYoutube = new StatusYoutube($url, $message);
          $nid = $statusYoutube
            ->generateNode();
        }
        else {
          if ($url !== null) {
            $statusHeartPost = new StatusHeartPost($url, $message);
            $nid = $statusHeartPost
              ->sendRequest();
          }
        }
      }
    }
    if (null === $nid && !empty($this->statusTypeService)) {
      $sTypes = $this->statusTypeService
        ->loadAll();
      foreach ($this->statusTypeService
        ->loadAll() as $type) {
        $userViewed = \Drupal::routeMatch()
          ->getParameters()
          ->get('user') === NULL ? \Drupal::currentUser()
          ->id() : \Drupal::routeMatch()
          ->getParameters()
          ->get('user')
          ->id();
        if ($userViewed !== NULL) {
          $statusEntity = Status::create([
            'type' => $type
              ->id(),
            'uid' => \Drupal::currentUser()
              ->id(),
            'recipient' => $userViewed,
            'format' => 'basic_html',
          ]);
          StatusHeartPost::parseHashtags($message);
          if ($type
            ->getMedia() && $file !== null) {
            $statusEntity
              ->set('field_image', array_values($file)[0]);
          }
          $statusEntity
            ->setMessage($message);
        }
        if (!empty($statusEntity) && $statusEntity
          ->save()) {

          //TODO Log or error report
          $statusCreated = TRUE;
        }
      }
    }
    if (\Drupal::service('module_handler')
      ->moduleExists('heartbeat') && ($nid !== NULL || $statusEntity !== NULL)) {

      //these config settings provide the chosen "Feed" with which to reload the stream

      //earlier in development, the implementation was centered around selectable feed

      //types rather than filtering a single feed

      //TODO decide on the use of feed selections

      //TODO this implementation causes a complete reloading of the feed. This should be changed such as to

      // only prepend the new post to the top of the feed
      $feedConfig = \Drupal::config('heartbeat_feed.settings');
      $response = new AjaxResponse();

      //        $response->addCommand(new SelectFeedCommand($feedConfig->get('message')));
      $response
        ->addCommand(new PrependCommand('.heartbeat-stream', HeartbeatBlock::renderOneHeartbeat(\Drupal::service('heartbeat')
        ->loadByUid(\Drupal::currentUser()
        ->id()))));
      $response
        ->addCommand(new ClearPreviewCommand(true));

      //        $this->clearFormInput($form_state);
      //        $form['message']['#default'] = '';
      //        $form['message']['#value'] = '';
      return $response;

      //        return [
      //          '#type' => 'markup',
      //          '#markup' => 'Status submitted'
      //        ];
    }
  }
  return null;
}