StatusForm.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. namespace Drupal\statusmessage\Form;
  3. use Drupal\Core\Form\FormBase;
  4. use Drupal\Core\Form\FormStateInterface;
  5. use Drupal\statusmessage\Entity\Status;
  6. use Drupal\statusmessage\ClientGeneratorService;
  7. use Drupal\statusmessage\MarkupGenerator;
  8. use Drupal\statusmessage\StatusService;
  9. use Drupal\statusmessage\StatusTypeService;
  10. use Drupal\statusmessage\Ajax\ClientCommand;
  11. use Drupal\statusmessage\StatusHeartPost;
  12. use Drupal\statusmessage\StatusTwitter;
  13. use Drupal\statusmessage\StatusYoutube;
  14. use Drupal\Core\Ajax\AjaxResponse;
  15. use Drupal\heartbeat\Ajax\SelectFeedCommand;
  16. use Symfony\Component\DependencyInjection\ContainerInterface;
  17. /**
  18. * Form controller for Status edit forms.
  19. *
  20. * @ingroup statusmessage
  21. */
  22. class StatusForm extends FormBase {
  23. protected $statusTypeService;
  24. protected $statusService;
  25. protected $markupgenerator;
  26. private $mediaTabs;
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public static function create(ContainerInterface $container) {
  31. return new static(
  32. $container->get('status_type_service'),
  33. $container->get('statusservice'),
  34. $container->get('markupgenerator'));
  35. }
  36. //TODO remove markup generator from this class
  37. /**
  38. * StatusForm constructor.
  39. * @param StatusTypeService $status_type_service
  40. * @param StatusService $status_service
  41. */
  42. public function __construct(StatusTypeService $status_type_service, StatusService $status_service, MarkupGenerator $markupgenerator) {
  43. $this->statusTypeService = $status_type_service;
  44. $this->statusService = $status_service;
  45. $this->markupgenerator = $markupgenerator;
  46. $this->mediaTabs = ['Photo', 'Video'];
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function buildForm(array $form, FormStateInterface $form_state) {
  52. /* @var $entity \Drupal\statusmessage\Entity\Status */
  53. $form['#attached']['library'][] = 'statusmessage/status';
  54. if (\Drupal::moduleHandler()->moduleExists('heartbeat')) {
  55. $friendData = \Drupal::config('heartbeat_friendship.settings')->get('data');
  56. $form['#attached']['library'][] = 'heartbeat/heartbeat';
  57. $form['#attached']['drupalSettings']['friendData'] = $friendData;
  58. }
  59. $form['message'] = array(
  60. '#type' => 'textarea',
  61. '#description' => 'Status Message',
  62. '#attributes' => array(
  63. 'placeholder' => t('Post a status update'),
  64. ),
  65. '#ajax' => [
  66. 'event' => 'change, paste, keyup',
  67. 'callback' => '::generatePreview',
  68. 'progress' => array(
  69. 'type' => 'throbber',
  70. 'message' => t('Generating preview'),
  71. ),
  72. ],
  73. );
  74. // $form['mediatabs'] = [
  75. // '#type' => 'radios',
  76. //// '#description' => $this->t('User selectable feeds'),
  77. // '#options' => $this->mediaTabs,
  78. //// '#ajax' => [
  79. //// 'callback' => '::updateFeed',
  80. ////// 'event' => 'onclick',
  81. //// 'progress' => array(
  82. //// 'type' => 'none',
  83. ////// 'message' => t('Fetching feed'),
  84. //// ),
  85. // ];
  86. $form['post'] = array(
  87. '#type' => 'submit',
  88. '#description' => 'Post',
  89. '#value' => t('Post'),
  90. '#ajax' => [
  91. 'callback' => '::statusAjaxSubmit',
  92. 'progress' => array(
  93. 'type' => 'throbber',
  94. 'message' => t('Posting Message'),
  95. ),
  96. ]
  97. );
  98. $stophere = null;
  99. return $form;
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function save(array $form, FormStateInterface $form_state) {
  105. $entity = $this->entity;
  106. $status = parent::save($form, $form_state);
  107. switch ($status) {
  108. case SAVED_NEW:
  109. drupal_set_message($this->t('Created the %label Status.', [
  110. '%label' => $entity->label(),
  111. ]));
  112. break;
  113. default:
  114. drupal_set_message($this->t('Saved the %label Status.', [
  115. '%label' => $entity->label(),
  116. ]));
  117. }
  118. $form_state->setRedirect('entity.status.canonical', ['status' => $entity->id()]);
  119. }
  120. /**
  121. * Returns a unique string identifying the form.
  122. *
  123. * @return string
  124. * The unique string identifying the form.
  125. */
  126. public function getFormId() {
  127. return 'status_form';
  128. }
  129. /**
  130. * Form submission handler.
  131. *
  132. * @param array $form
  133. * An associative array containing the structure of the form.
  134. * @param \Drupal\Core\Form\FormStateInterface $form_state
  135. * The current state of the form.
  136. * @throws \Drupal\Core\Entity\EntityStorageException
  137. * @throws \InvalidArgumentException
  138. */
  139. public function generatePreview(array &$form, FormStateInterface $form_state) {
  140. $message = $form_state->getValue('message');
  141. preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $message, $match);
  142. if ($this->markupgenerator !== null && !empty($match) && array_values($match)[0] !== null) {
  143. $url = array_values($match)[0];
  144. // $this->previewGenerator->generatePreview($url);
  145. $response = new AjaxResponse();
  146. $response->addCommand(new ClientCommand($url[0]));
  147. return $response;
  148. }
  149. // if (!empty($this->statusTypeService)) {
  150. // foreach ($this->statusTypeService->loadAll() as $type) {
  151. // if (!$type->getMedia()) {
  152. //
  153. // $userViewed = \Drupal::routeMatch()->getParameters()->get('user') === null ? \Drupal::currentUser()->id() : \Drupal::routeMatch()->getParameters()->get('user')->id();
  154. //
  155. // if ($userViewed !== null) {
  156. //
  157. // $statusEntity = Status::create([
  158. // 'type' => $type->id(),
  159. // 'uid' => \Drupal::currentUser()->id(),
  160. // 'recipient' => $userViewed
  161. // ]);
  162. //
  163. // $statusEntity->setMessage($form_state->getValue('message'));
  164. // $statusEntity->save();
  165. //
  166. // if (\Drupal::service('module_handler')->moduleExists('heartbeat')) {
  167. //
  168. //// $configManager = \Drupal::service('config.manager');
  169. // $feedConfig = \Drupal::config('heartbeat_feed.settings');
  170. //// $feedConfig = $feedConfig = $configManager->get('heartbeat_feed.settings');
  171. // $response = new AjaxResponse();
  172. // $response->addCommand(new SelectFeedCommand($feedConfig->get('message')));
  173. //
  174. // return $response;
  175. // }
  176. // break;
  177. // }
  178. // }
  179. // }
  180. // }
  181. }
  182. public function statusAjaxSubmit(array &$form, FormStateInterface $form_state) {
  183. $message = $form_state->getValue('message');
  184. if (strlen(trim($message)) > 1) {
  185. preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $message, $match);
  186. if ($this->markupgenerator !== NULL && !empty($match) && array_values($match)[0] !== NULL) {
  187. $url = is_array(array_values($match)[0]) ? array_values(array_values($match)[0])[0] : array_values($match)[0];
  188. if (strpos($message, 'twitter')) {
  189. $statusTwitter = new StatusTwitter($url);
  190. $nid = $statusTwitter->sendRequest();
  191. } else if (strpos($message, 'youtube') || strpos($message, 'youtu.be')) {
  192. $statusYoutube = new StatusYoutube($url);
  193. $nid = $statusYoutube->generateNode();
  194. } else {
  195. $statusHeartPost = new StatusHeartPost($url);
  196. $statusHeartPost->sendRequest();
  197. }
  198. }
  199. if ($nid === NULL && !empty($this->statusTypeService)) {
  200. $statusCreated = false;
  201. foreach ($this->statusTypeService->loadAll() as $type) {
  202. if (!$statusCreated && !$type->getMedia()) {
  203. $userViewed = \Drupal::routeMatch()
  204. ->getParameters()
  205. ->get('user') === NULL ? \Drupal::currentUser()
  206. ->id() : \Drupal::routeMatch()
  207. ->getParameters()
  208. ->get('user')
  209. ->id();
  210. if ($userViewed !== NULL) {
  211. $statusEntity = Status::create([
  212. 'type' => $type->id(),
  213. 'uid' => \Drupal::currentUser()->id(),
  214. 'recipient' => $userViewed
  215. ]);
  216. $statusEntity->setMessage($message);
  217. if ($statusEntity->save()) {
  218. $statusCreated = TRUE;
  219. }
  220. }
  221. }
  222. }
  223. }
  224. if (\Drupal::service('module_handler')
  225. ->moduleExists('heartbeat') && ($nid !== NULL || $statusEntity !== NULL)
  226. ) {
  227. // $configManager = \Drupal::service('config.manager');
  228. $feedConfig = \Drupal::config('heartbeat_feed.settings');
  229. // $feedConfig = $feedConfig = $configManager->get('heartbeat_feed.settings');
  230. $response = new AjaxResponse();
  231. $response->addCommand(new SelectFeedCommand($feedConfig->get('message')));
  232. return $response;
  233. }
  234. }
  235. return null;
  236. }
  237. /**
  238. * Form submission handler.
  239. *
  240. * @param array $form
  241. * An associative array containing the structure of the form.
  242. * @param \Drupal\Core\Form\FormStateInterface $form_state
  243. * The current state of the form.
  244. */
  245. public function submitForm(array &$form, FormStateInterface $form_state) {
  246. }
  247. }