123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- <?php
- namespace Drupal\statusmessage\Form;
- use Drupal\Core\Form\FormBase;
- use Drupal\Core\Form\FormStateInterface;
- use Drupal\statusmessage\Entity\Status;
- use Drupal\statusmessage\ClientGeneratorService;
- use Drupal\statusmessage\MarkupGenerator;
- use Drupal\statusmessage\StatusService;
- use Drupal\statusmessage\StatusTypeService;
- use Drupal\statusmessage\Ajax\ClientCommand;
- use Drupal\statusmessage\StatusHeartPost;
- use Drupal\statusmessage\StatusTwitter;
- use Drupal\statusmessage\StatusYoutube;
- use Drupal\Core\Ajax\AjaxResponse;
- use Drupal\heartbeat\Ajax\SelectFeedCommand;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- class StatusForm extends FormBase {
- protected $statusTypeService;
- protected $statusService;
- protected $markupgenerator;
- private $mediaTabs;
-
- public static function create(ContainerInterface $container) {
- return new static(
- $container->get('status_type_service'),
- $container->get('statusservice'),
- $container->get('markupgenerator'));
- }
-
-
- public function __construct(StatusTypeService $status_type_service, StatusService $status_service, MarkupGenerator $markupgenerator) {
- $this->statusTypeService = $status_type_service;
- $this->statusService = $status_service;
- $this->markupgenerator = $markupgenerator;
- $this->mediaTabs = ['Photo', 'Video'];
- }
-
- public function buildForm(array $form, FormStateInterface $form_state) {
-
- $form['#attached']['library'][] = 'statusmessage/status';
- if (\Drupal::moduleHandler()->moduleExists('heartbeat')) {
- $friendData = \Drupal::config('heartbeat_friendship.settings')->get('data');
- $form['#attached']['library'][] = 'heartbeat/heartbeat';
- $form['#attached']['drupalSettings']['friendData'] = $friendData;
- }
- $form['message'] = array(
- '#type' => 'textarea',
- '#description' => 'Status Message',
- '#attributes' => array(
- 'placeholder' => t('Post a status update'),
- ),
- '#ajax' => [
- 'event' => 'change, paste, keyup',
- 'callback' => '::generatePreview',
- 'progress' => array(
- 'type' => 'throbber',
- 'message' => t('Generating preview'),
- ),
- ],
- );
- $form['post'] = array(
- '#type' => 'submit',
- '#description' => 'Post',
- '#value' => t('Post'),
- '#ajax' => [
- 'callback' => '::statusAjaxSubmit',
- 'progress' => array(
- 'type' => 'throbber',
- 'message' => t('Posting Message'),
- ),
- ]
- );
- $stophere = null;
- return $form;
- }
-
- public function save(array $form, FormStateInterface $form_state) {
- $entity = $this->entity;
- $status = parent::save($form, $form_state);
- switch ($status) {
- case SAVED_NEW:
- drupal_set_message($this->t('Created the %label Status.', [
- '%label' => $entity->label(),
- ]));
- break;
- default:
- drupal_set_message($this->t('Saved the %label Status.', [
- '%label' => $entity->label(),
- ]));
- }
- $form_state->setRedirect('entity.status.canonical', ['status' => $entity->id()]);
- }
-
- public function getFormId() {
- return 'status_form';
- }
-
- public function generatePreview(array &$form, FormStateInterface $form_state) {
- $message = $form_state->getValue('message');
- preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $message, $match);
- if ($this->markupgenerator !== null && !empty($match) && array_values($match)[0] !== null) {
- $url = array_values($match)[0];
- $response = new AjaxResponse();
- $response->addCommand(new ClientCommand($url[0]));
- return $response;
- }
- }
- public function statusAjaxSubmit(array &$form, FormStateInterface $form_state) {
- $message = $form_state->getValue('message');
- 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);
- $nid = $statusYoutube->generateNode();
- } else {
- $statusHeartPost = new StatusHeartPost($url);
- $statusHeartPost->sendRequest();
- }
- }
- if ($nid === NULL && !empty($this->statusTypeService)) {
- $statusCreated = false;
- foreach ($this->statusTypeService->loadAll() as $type) {
- if (!$statusCreated && !$type->getMedia()) {
- $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
- ]);
- $statusEntity->setMessage($message);
- if ($statusEntity->save()) {
- $statusCreated = TRUE;
- }
- }
- }
- }
- }
- if (\Drupal::service('module_handler')
- ->moduleExists('heartbeat') && ($nid !== NULL || $statusEntity !== NULL)
- ) {
- $feedConfig = \Drupal::config('heartbeat_feed.settings');
- $response = new AjaxResponse();
- $response->addCommand(new SelectFeedCommand($feedConfig->get('message')));
- return $response;
- }
- }
- return null;
- }
-
- public function submitForm(array &$form, FormStateInterface $form_state) {
- }
- }
|