Posting.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. declare(strict_types = 1);
  3. namespace Component\Posting\Form;
  4. use App\Core\ActorLocalRoles;
  5. use App\Core\Event;
  6. use App\Core\Form;
  7. use function App\Core\I18n\_m;
  8. use App\Core\Router\Router;
  9. use App\Core\VisibilityScope;
  10. use App\Entity\Actor;
  11. use App\Util\Common;
  12. use App\Util\Form\FormFields;
  13. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  14. use Symfony\Component\Form\Extension\Core\Type\FileType;
  15. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  16. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  17. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\Validator\Constraints\Length;
  20. class Posting
  21. {
  22. public static function create(Request $request)
  23. {
  24. $actor = Common::actor();
  25. $placeholder_strings = ['How are you feeling?', 'Have something to share?', 'How was your day?'];
  26. Event::handle('PostingPlaceHolderString', [&$placeholder_strings]);
  27. $placeholder = $placeholder_strings[array_rand($placeholder_strings)];
  28. $initial_content = '';
  29. Event::handle('PostingInitialContent', [&$initial_content]);
  30. $available_content_types = [
  31. _m('Plain Text') => 'text/plain',
  32. ];
  33. Event::handle('PostingAvailableContentTypes', [&$available_content_types]);
  34. $in_targets = [];
  35. Event::handle('PostingFillTargetChoices', [$request, $actor, &$in_targets]);
  36. $context_actor = null;
  37. Event::handle('PostingGetContextActor', [$request, $actor, &$context_actor]);
  38. $form_params = [];
  39. if (!empty($in_targets)) { // @phpstan-ignore-line
  40. // Add "none" option to the first of choices
  41. $in_targets = array_merge([_m('Public') => 'public'], $in_targets);
  42. // Make the context actor the first In target option
  43. if (!\is_null($context_actor)) {
  44. foreach ($in_targets as $it_nick => $it_id) {
  45. if ($it_id === $context_actor->getId()) {
  46. unset($in_targets[$it_nick]);
  47. $in_targets = array_merge([$it_nick => $it_id], $in_targets);
  48. break;
  49. }
  50. }
  51. }
  52. $form_params[] = ['in', ChoiceType::class, ['label' => _m('In:'), 'multiple' => false, 'expanded' => false, 'choices' => $in_targets]];
  53. }
  54. $visibility_options = [
  55. _m('Public') => VisibilityScope::EVERYWHERE->value,
  56. _m('Local') => VisibilityScope::LOCAL->value,
  57. _m('Addressee') => VisibilityScope::ADDRESSEE->value,
  58. ];
  59. if (!\is_null($context_actor) && $context_actor->isGroup()) { // @phpstan-ignore-line currently an open bug. See https://web.archive.org/web/20220226131651/https://github.com/phpstan/phpstan/issues/6234
  60. if ($actor->canModerate($context_actor)) {
  61. if ($context_actor->getRoles() & ActorLocalRoles::PRIVATE_GROUP) {
  62. $visibility_options = array_merge([_m('Group') => VisibilityScope::GROUP->value], $visibility_options);
  63. } else {
  64. $visibility_options[_m('Group')] = VisibilityScope::GROUP->value;
  65. }
  66. }
  67. }
  68. $form_params[] = ['visibility', ChoiceType::class, ['label' => _m('Visibility:'), 'multiple' => false, 'expanded' => false, 'choices' => $visibility_options]];
  69. $form_params[] = ['content', TextareaType::class, ['label' => _m('Content:'), 'data' => $initial_content, 'attr' => ['placeholder' => _m($placeholder)], 'constraints' => [new Length(['max' => Common::config('site', 'text_limit')])]]];
  70. $form_params[] = ['attachments', FileType::class, ['label' => _m('Attachments:'), 'multiple' => true, 'required' => false, 'invalid_message' => _m('Attachment not valid.')]];
  71. $form_params[] = FormFields::language($actor, $context_actor, label: _m('Note language'), help: _m('The selected language will be federated and added as a lang attribute, preferred language can be set up in settings'));
  72. if (\count($available_content_types) > 1) {
  73. $form_params[] = ['content_type', ChoiceType::class,
  74. [
  75. 'label' => _m('Text format:'), 'multiple' => false, 'expanded' => false,
  76. 'data' => $available_content_types[array_key_first($available_content_types)],
  77. 'choices' => $available_content_types,
  78. ],
  79. ];
  80. } else {
  81. $form_params[] = ['content_type', HiddenType::class, ['data' => $available_content_types[array_key_first($available_content_types)]]];
  82. }
  83. Event::handle('PostingAddFormEntries', [$request, $actor, &$form_params]);
  84. $form_params[] = ['post_note', SubmitType::class, ['label' => _m('Post')]];
  85. return Form::create($form_params, form_options: ['action' => Router::url(\Component\Posting\Posting::route)]);
  86. }
  87. }