diff --git a/config/sync/core.entity_form_display.paragraph.attendee.default.yml b/config/sync/core.entity_form_display.paragraph.attendee.default.yml index fef23729..960afb16 100644 --- a/config/sync/core.entity_form_display.paragraph.attendee.default.yml +++ b/config/sync/core.entity_form_display.paragraph.attendee.default.yml @@ -5,6 +5,7 @@ dependencies: config: - field.field.paragraph.attendee.field_attended - field.field.paragraph.attendee.field_attendee_email + - field.field.paragraph.attendee.field_attendee_email_sent - field.field.paragraph.attendee.field_attendee_firstname - field.field.paragraph.attendee.field_attendee_secondname - field.field.paragraph.attendee.field_attendee_status @@ -23,6 +24,12 @@ content: third_party_settings: { } type: email_default region: content + field_attendee_email_sent: + weight: 5 + settings: { } + third_party_settings: { } + type: datetime_timestamp + region: content field_attendee_firstname: weight: 1 settings: diff --git a/config/sync/core.entity_view_display.paragraph.attendee.default.yml b/config/sync/core.entity_view_display.paragraph.attendee.default.yml index 46c22911..e03e76f8 100644 --- a/config/sync/core.entity_view_display.paragraph.attendee.default.yml +++ b/config/sync/core.entity_view_display.paragraph.attendee.default.yml @@ -5,6 +5,7 @@ dependencies: config: - field.field.paragraph.attendee.field_attended - field.field.paragraph.attendee.field_attendee_email + - field.field.paragraph.attendee.field_attendee_email_sent - field.field.paragraph.attendee.field_attendee_firstname - field.field.paragraph.attendee.field_attendee_secondname - field.field.paragraph.attendee.field_attendee_status @@ -48,4 +49,5 @@ content: region: content hidden: field_attended: true + field_attendee_email_sent: true field_attendee_status: true diff --git a/config/sync/field.field.paragraph.attendee.field_attendee_email_sent.yml b/config/sync/field.field.paragraph.attendee.field_attendee_email_sent.yml new file mode 100644 index 00000000..9bbcdce5 --- /dev/null +++ b/config/sync/field.field.paragraph.attendee.field_attendee_email_sent.yml @@ -0,0 +1,19 @@ +uuid: 2261c366-a02e-4ebe-bbce-a0ffcedc6a79 +langcode: en +status: true +dependencies: + config: + - field.storage.paragraph.field_attendee_email_sent + - paragraphs.paragraphs_type.attendee +id: paragraph.attendee.field_attendee_email_sent +field_name: field_attendee_email_sent +entity_type: paragraph +bundle: attendee +label: 'Attendee email sent' +description: '' +required: false +translatable: false +default_value: { } +default_value_callback: '' +settings: { } +field_type: timestamp diff --git a/config/sync/field.storage.paragraph.field_attendee_email_sent.yml b/config/sync/field.storage.paragraph.field_attendee_email_sent.yml new file mode 100644 index 00000000..721c0f89 --- /dev/null +++ b/config/sync/field.storage.paragraph.field_attendee_email_sent.yml @@ -0,0 +1,18 @@ +uuid: 91f1677d-4c23-490b-a713-9d37648979b7 +langcode: en +status: true +dependencies: + module: + - paragraphs +id: paragraph.field_attendee_email_sent +field_name: field_attendee_email_sent +entity_type: paragraph +type: timestamp +settings: { } +module: core +locked: false +cardinality: 1 +translatable: true +indexes: { } +persist_with_no_fields: false +custom_storage: false diff --git a/docroot/modules/custom/dckyiv_commerce/dckyiv_commerce.routing.yml b/docroot/modules/custom/dckyiv_commerce/dckyiv_commerce.routing.yml index 8b81a400..3654bc7c 100644 --- a/docroot/modules/custom/dckyiv_commerce/dckyiv_commerce.routing.yml +++ b/docroot/modules/custom/dckyiv_commerce/dckyiv_commerce.routing.yml @@ -14,6 +14,22 @@ dckyiv_commerce.attendee_edit: requirements: _permission: 'access content' +dckyiv_commerce.attendee_send_ticket: + path: '/user/{user}/my-tickets/{commerce_order_item}/attendee/{attendee_paragraph}/send-ticket' + defaults: + _controller: '\Drupal\dckyiv_commerce\Controller\DckyivCommerceController::sendTicket' + _title: 'Attendee information' + options: + parameters: + user: + type: entity:user + commerce_order_item: + type: entity:commerce_order_item + attendee_paragraph: + type: entity:paragraph + requirements: + _permission: 'access content' + dckyiv_commerce.attendees_report: path: '/admin/dc/attendees_report' defaults: diff --git a/docroot/modules/custom/dckyiv_commerce/src/Controller/DckyivCommerceController.php b/docroot/modules/custom/dckyiv_commerce/src/Controller/DckyivCommerceController.php index ee43292e..27154454 100644 --- a/docroot/modules/custom/dckyiv_commerce/src/Controller/DckyivCommerceController.php +++ b/docroot/modules/custom/dckyiv_commerce/src/Controller/DckyivCommerceController.php @@ -6,6 +6,9 @@ use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityFormBuilderInterface; +use Drupal\Core\Mail\MailManagerInterface; +use Drupal\Core\Render\RenderContext; +use Drupal\Core\Render\Renderer; use Drupal\paragraphs\ParagraphInterface; use Drupal\user\UserInterface; use Drupal\views\Views; @@ -31,6 +34,20 @@ class DckyivCommerceController extends ControllerBase { */ protected $entityFieldManager; + /** + * The mail manager service. + * + * @var \Drupal\Core\Mail\MailManagerInterface + */ + protected $mailManager; + + /** + * The renderer. + * + * @var \Drupal\Core\Render\RendererInterface + */ + protected $renderer; + /** * Constructs a DckyivCommerceController object. * @@ -38,11 +55,14 @@ class DckyivCommerceController extends ControllerBase { * The entity form builder. * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager * The entity field manager. + * @param \Drupal\Core\Mail\MailManagerInterface $mail_manager + * The mail manager service. */ - public function __construct(EntityFormBuilderInterface $entity_form_builder, EntityFieldManagerInterface $entity_field_manager) { + public function __construct(EntityFormBuilderInterface $entity_form_builder, EntityFieldManagerInterface $entity_field_manager, MailManagerInterface $mail_manager, Renderer $renderer) { $this->entityFormBuilder = $entity_form_builder; $this->entityFieldManager = $entity_field_manager; - + $this->mailManager = $mail_manager; + $this->renderer = $renderer; } /** @@ -51,7 +71,9 @@ public function __construct(EntityFormBuilderInterface $entity_form_builder, Ent public static function create(ContainerInterface $container) { return new static( $container->get('entity.form_builder'), - $container->get('entity_field.manager') + $container->get('entity_field.manager'), + $container->get('plugin.manager.mail'), + $container->get('renderer') ); } @@ -178,4 +200,53 @@ public function attendeesReport() { return $build; } + /** + * Attendee send ticket callback page. + * + * @param UserInterface $user + * @param OrderItemInterface $commerce_order_item + * @param ParagraphInterface $attendee_paragraph + * + * @return array + */ + public function sendTicket(UserInterface $user, OrderItemInterface $commerce_order_item, ParagraphInterface $attendee_paragraph) { + $to = $attendee_paragraph->get('field_attendee_email')->value; + if (empty($to)) { + // @TODO show order info + $message = $this->t('Mail was not sent to attendee. Empty recipient address'); + $this->getLogger('dckyiv_commerce')->warning($message); + $this->messenger()->addWarning($message); + return [ + '#markup' => $message, + ]; + } + $order = $commerce_order_item->getOrder(); + $params = [ + 'headers' => [ + 'Content-Type' => 'text/html; charset=UTF-8;', + 'Content-Transfer-Encoding' => '8Bit', + ], + 'from' => $order->getStore()->getEmail(), + 'subject' => 'Ticket info', + 'attendee_paragraph' => $attendee_paragraph, + ]; + + $viewBuilder = $this->entityTypeManager()->getViewBuilder('paragraph'); + $build = $viewBuilder->view($attendee_paragraph, 'mail'); + $params['body'] =$this->renderer->executeInRenderContext(new RenderContext(), function () use ($build) { + return $this->renderer->render($build); + }); + $langcode = $this->languageManager()->getDefaultLanguage()->getId(); + + $result = $this->mailManager->mail('commerce_order', 'receipt', $to, $langcode, $params); + if ($result['send']) { + $attendee_paragraph->set('field_attendee_email_sent', time()); + $attendee_paragraph->save(); + } + // @TODO redirect with message. + return [ + '#markup' => $this->t('Ticket has been sent'), + ]; + } + } diff --git a/docroot/modules/custom/dckyiv_commerce/src/Form/EditAttendeeForm.php b/docroot/modules/custom/dckyiv_commerce/src/Form/EditAttendeeForm.php index ffc0c069..226d2c83 100644 --- a/docroot/modules/custom/dckyiv_commerce/src/Form/EditAttendeeForm.php +++ b/docroot/modules/custom/dckyiv_commerce/src/Form/EditAttendeeForm.php @@ -14,7 +14,7 @@ * Form controller for profile forms. */ class EditAttendeeForm extends ContentEntityForm { - + /** * Constructs a ContentEntityForm object. * @@ -35,8 +35,8 @@ public function __construct( parent::__construct($entity_repository, $entity_type_bundle_info, $time); $this->routeMatch = $route_match; } - - + + /** * {@inheritdoc} */ @@ -48,7 +48,7 @@ public static function create(ContainerInterface $container) { $container->get('datetime.time') ); } - + /** * {@inheritdoc} */ @@ -63,7 +63,7 @@ public function form(array $form, FormStateInterface $form_state) { if (!$paragraph->field_attendee_email->isEmpty()) { $attendee_info_default = 'email'; } - $form['attende_info'] = [ +/* $form['attende_info'] = [ '#type' => 'radios', '#options' => [ 'name' => t('Name'), @@ -75,33 +75,33 @@ public function form(array $form, FormStateInterface $form_state) { '#prefix' => '
', '#suffix' => '
', ]; - $form['field_attendee_email']['#states'] = [ 'visible' => [ ':input[name="attende_info"]' => ['value' => 'email'], ], ]; - + $form['field_site_user']['#states'] = [ 'visible' => [ ':input[name="attende_info"]' => ['value' => 'user'], ], ]; - - + + $form['field_attendee_firstname']['#states'] = [ 'visible' => [ ':input[name="attende_info"]' => ['value' => 'name'], ], ]; - - + + $form['field_attendee_secondname']['#states'] = [ 'visible' => [ ':input[name="attende_info"]' => ['value' => 'name'], ], ]; - + */ + $form['field_attendee_status']['#access'] = FALSE; $form_state->set('commerce_order_item', $this->routeMatch->getParameter('commerce_order_item')); return $form; diff --git a/docroot/modules/custom/dckyiv_commerce/src/Plugin/Field/FieldFormatter/AttendeeFormatter.php b/docroot/modules/custom/dckyiv_commerce/src/Plugin/Field/FieldFormatter/AttendeeFormatter.php index 00c67b0b..d7ed9cd2 100644 --- a/docroot/modules/custom/dckyiv_commerce/src/Plugin/Field/FieldFormatter/AttendeeFormatter.php +++ b/docroot/modules/custom/dckyiv_commerce/src/Plugin/Field/FieldFormatter/AttendeeFormatter.php @@ -64,23 +64,63 @@ public function viewElements(FieldItemListInterface $items, $langcode) { } $elements[$delta]['edit-attendee'] = [ - '#type' => 'link', + '#type' => 'container', '#weight' => 10, - '#title' => $empty ? $this->t('Add info') : $this->t('edit'), - '#url' => Url::fromRoute('dckyiv_commerce.attendee_edit', [ - 'user' => $commerce_order_item->getOrder()->getCustomerId(), - 'commerce_order_item' => $commerce_order_item->id(), - 'attendee_paragraph' => $entity->id(), - ], [ - 'query' => [ - 'destination' => \Drupal::service('path.current')->getPath(), - ], - 'attributes' => [ - 'class' => ['use-ajax'], - 'data-dialog-type'=> 'modal', - ], - ]), + 'link' => [ + '#type' => 'link', + '#weight' => 10, + '#title' => $empty ? $this->t('Add info') : $this->t('edit'), + '#url' => Url::fromRoute('dckyiv_commerce.attendee_edit', [ + 'user' => $commerce_order_item->getOrder()->getCustomerId(), + 'commerce_order_item' => $commerce_order_item->id(), + 'attendee_paragraph' => $entity->id(), + ], [ + 'query' => [ + 'destination' => \Drupal::service('path.current')->getPath(), + ], + 'attributes' => [ + 'class' => ['use-ajax'], + 'data-dialog-type'=> 'modal', + ], + ]), + ], ]; + + if (!$entity->get('field_attendee_email')->isEmpty()) { + $elements[$delta]['send-email-attendee'] = [ + '#type' => 'container', + '#weight' => 10, + ]; + if (!$entity->get('field_attendee_email_sent')->isEmpty() + && $ticket_sent = $entity->get('field_attendee_email_sent')->value) { + $elements[$delta]['send-email-attendee']['sent-info'] = [ + '#type' => 'container', + 'markup' => [ + '#markup' => $this->t('Ticket sent @time', [ + '@time' => \Drupal::service('date.formatter')->format($ticket_sent, 'short'), + ]), + ], + ]; + } + $elements[$delta]['send-email-attendee']['link'] = [ + '#type' => 'link', + '#weight' => 10, + '#title' => isset($ticket_sent) ? $this->t('resend ticket') : $this->t('send ticket'), + '#url' => Url::fromRoute('dckyiv_commerce.attendee_send_ticket', [ + 'user' => $commerce_order_item->getOrder()->getCustomerId(), + 'commerce_order_item' => $commerce_order_item->id(), + 'attendee_paragraph' => $entity->id(), + ], [ + 'query' => [ + 'destination' => \Drupal::service('path.current')->getPath(), + ], + 'attributes' => [ + 'class' => ['use-ajax'], + 'data-dialog-type'=> 'modal', + ], + ]), + ]; + } } } diff --git a/docroot/themes/custom/dckyiv/templates/paragraph/paragraph--attendee--mail.html.twig b/docroot/themes/custom/dckyiv/templates/paragraph/paragraph--attendee--mail.html.twig index d9364620..e0f63b8d 100644 --- a/docroot/themes/custom/dckyiv/templates/paragraph/paragraph--attendee--mail.html.twig +++ b/docroot/themes/custom/dckyiv/templates/paragraph/paragraph--attendee--mail.html.twig @@ -56,7 +56,7 @@ {{ content }} - +