Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved attendee orders managing. Added ability to send ticket per e… #141

Merged
merged 2 commits into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -48,4 +49,5 @@ content:
region: content
hidden:
field_attended: true
field_attendee_email_sent: true
field_attendee_status: true
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions config/sync/field.storage.paragraph.field_attendee_email_sent.yml
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions docroot/modules/custom/dckyiv_commerce/dckyiv_commerce.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,18 +34,35 @@ 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.
*
* @param \Drupal\Core\Entity\EntityFormBuilderInterface $entity_form_builder
* 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;
}

/**
Expand All @@ -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')
);
}

Expand Down Expand Up @@ -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'] = \Drupal::service('renderer')->executeInRenderContext(new RenderContext(), function () use ($build) {
anpolimus marked this conversation as resolved.
Show resolved Hide resolved
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'),
];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* Form controller for profile forms.
*/
class EditAttendeeForm extends ContentEntityForm {

/**
* Constructs a ContentEntityForm object.
*
Expand All @@ -35,8 +35,8 @@ public function __construct(
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->routeMatch = $route_match;
}


/**
* {@inheritdoc}
*/
Expand All @@ -48,7 +48,7 @@ public static function create(ContainerInterface $container) {
$container->get('datetime.time')
);
}

/**
* {@inheritdoc}
*/
Expand All @@ -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'),
Expand All @@ -75,33 +75,33 @@ public function form(array $form, FormStateInterface $form_state) {
'#prefix' => '<div class="form-item--inline attendee-info-radio">',
'#suffix' => '</div>',
];

$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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
]),
];
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
{{ content }}
</td>
<td>
<img src="{{ qrSrc }}" />
<img src="{{ qrSrc | raw }}" />
</td>
</tr>
</tbody>
Expand Down