206 lines
14 KiB
PHP
206 lines
14 KiB
PHP
<?php
|
|
/** 工单处理弹窗:左侧会话与回复输入,右侧处理信息与内部备注。@package AKSRT */
|
|
|
|
namespace AKSRT\Admin\Native;
|
|
|
|
use AKSRT\Ticket\TicketNoteService;
|
|
use AKSRT\Ticket\TicketPrereplyService;
|
|
use AKSRT\Ticket\TicketRepository;
|
|
use AKSRT\Ticket\TicketSettings;
|
|
use AKSRT\Ticket\TicketStatusService;
|
|
use AKSRT\Ticket\TicketTypeService;
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
trait UsersMenusTicketsDetail {
|
|
|
|
private function render_ticket_detail_panel(array $ticket): string {
|
|
$id = (int) $ticket['id'];
|
|
$html = '<div class="aksrt-ticket-detail" data-aksrt-ticket-detail="' . $id . '"><div class="aksrt-ticket-detail__main">';
|
|
$html .= $this->ticket_conversation($ticket, TicketRepository::get_instance()->replies($id));
|
|
$html .= !empty($ticket['is_closed'])
|
|
? '<p class="aksrt-ticket-detail__closed"><span class="dashicons dashicons-lock" aria-hidden="true"></span>' . esc_html__('工单已关闭,不能继续回复或变更处理信息;仍可添加内部备注。', 'aksrt') . '</p>'
|
|
: $this->ticket_reply_form($id);
|
|
$html .= '</div><aside class="aksrt-ticket-detail__side" aria-label="' . esc_attr__('工单信息与内部备注', 'aksrt') . '">';
|
|
return $html . $this->ticket_info_card($ticket) . $this->ticket_notes_card($id) . '</aside></div>';
|
|
}
|
|
|
|
/** 原始问题与回复同属一条时间线:用户在左、客服在右、系统事件居中。 */
|
|
private function ticket_conversation(array $ticket, array $replies): string {
|
|
$html = '<section class="aksrt-ticket-conversation" aria-label="' . esc_attr__('问题与沟通记录', 'aksrt') . '">';
|
|
$html .= '<div class="aksrt-ticket-section-head"><h3>' . esc_html__('沟通记录', 'aksrt') . ' <span class="aksrt-ticket-pill">' . esc_html(sprintf(__('%d 条记录', 'aksrt'), count($replies))) . '</span></h3>';
|
|
if ($replies !== []) {
|
|
$html .= '<button type="button" class="button-link" data-aksrt-ticket-latest hidden>' . esc_html__('查看最新记录', 'aksrt') . '</button>';
|
|
}
|
|
$html .= '</div><div class="aksrt-ticket-conversation__scroll" tabindex="0" role="region" aria-label="' . esc_attr__('可滚动的沟通记录', 'aksrt') . '"><ol class="aksrt-ticket-detail__thread">';
|
|
$html .= $this->ticket_message('origin', (int) $ticket['user_id'], (string) $ticket['user_name'], !empty($ticket['post_admin_id']) ? __('代客提交', 'aksrt') : __('提交了工单', 'aksrt'), (string) $ticket['post_time'], (string) $ticket['content'], (array) $ticket['attachment']);
|
|
if ($replies === []) {
|
|
$html .= '<li class="aksrt-ticket-detail__empty">' . esc_html__('暂无回复,请核对问题后联系用户。', 'aksrt') . '</li>';
|
|
}
|
|
foreach ($replies as $reply) {
|
|
if ((string) $reply['author_type'] === 'system') {
|
|
$html .= '<li class="aksrt-ticket-detail__event"><span>' . esc_html((string) $reply['content']) . '</span><time>' . esc_html((string) $reply['created_at']) . '</time></li>';
|
|
continue;
|
|
}
|
|
$admin = (string) $reply['author_type'] === 'admin';
|
|
$quote_html = '';
|
|
if (is_array($reply['quote'] ?? null)) {
|
|
$quote = $reply['quote'];
|
|
$quote_html = '<blockquote class="aksrt-ticket-detail__quote">' . esc_html(!empty($quote['is_deleted']) ? __('原回复已删除', 'aksrt') : (string) $quote['content']) . '</blockquote>';
|
|
}
|
|
$html .= $this->ticket_message($admin ? 'admin' : 'user', (int) $reply['author_id'], (string) $reply['author_name'], $admin ? __('客服', 'aksrt') : __('用户', 'aksrt'), (string) $reply['created_at'], (string) $reply['content'], (array) $reply['attachment'], $quote_html);
|
|
}
|
|
return $html . '</ol></div></section>';
|
|
}
|
|
|
|
private function ticket_message(string $variant, int $user_id, string $name, string $role, string $time, string $content, array $attachments, string $quote_html = ''): string {
|
|
$avatar = function_exists('aksrt_get_avatar_url') ? aksrt_get_avatar_url($user_id, 64) : '';
|
|
return '<li class="aksrt-ticket-detail__message is-' . esc_attr($variant) . '">'
|
|
. ($avatar !== '' ? '<img class="aksrt-ticket-detail__avatar" src="' . esc_url($avatar) . '" alt="" width="32" height="32">' : '<span class="aksrt-ticket-detail__avatar" aria-hidden="true"></span>')
|
|
. '<div class="aksrt-ticket-detail__bubble"><header class="aksrt-ticket-detail__meta"><strong>' . esc_html($name !== '' ? $name : '#' . $user_id) . '</strong>'
|
|
. '<span class="aksrt-ticket-detail__role">' . esc_html($role) . '</span><time>' . esc_html($time) . '</time></header>'
|
|
. $quote_html . '<div class="aksrt-ticket-detail__body">' . wpautop(esc_html($content)) . '</div>'
|
|
. $this->ticket_attachments_html($attachments) . '</div></li>';
|
|
}
|
|
|
|
private function ticket_reply_form(int $id): string {
|
|
$field = 'aksrt-ticket-reply-' . $id;
|
|
$tools = '';
|
|
$prereplies = TicketPrereplyService::all();
|
|
if ($prereplies !== []) {
|
|
$tools = '<label class="screen-reader-text" for="' . $field . '-pre">' . esc_html__('插入预设回复', 'aksrt') . '</label><select id="' . $field . '-pre" data-aksrt-ticket-prereply="' . $field . '"><option value="">' . esc_html__('插入预设回复…', 'aksrt') . '</option>';
|
|
foreach ($prereplies as $row) {
|
|
$tools .= '<option value="' . esc_attr((string) $row['content']) . '">' . esc_html(mb_strimwidth((string) $row['content'], 0, 40, '…')) . '</option>';
|
|
}
|
|
$tools .= '</select>';
|
|
}
|
|
return $this->ticket_composer('reply', $id, $field, __('回复用户', 'aksrt'), __('发送后用户将收到通知', 'aksrt'), __('填写回复内容…', 'aksrt'), 4, $tools, __('发送回复', 'aksrt'), true);
|
|
}
|
|
|
|
/** 输入框与工具栏合为一个整体:预设回复、字数与提交按钮同排。 */
|
|
private function ticket_composer(string $action, int $id, string $field, string $label, string $hint, string $placeholder, int $rows, string $tools, string $submit, bool $primary): string {
|
|
$max = TicketSettings::reply_max();
|
|
return $this->ticket_form_open($action, $id, 'aksrt-ticket-composer aksrt-ticket-composer--' . $action)
|
|
. '<div class="aksrt-ticket-composer__head"><label for="' . esc_attr($field) . '">' . esc_html($label) . '</label>'
|
|
. ($hint !== '' ? '<span class="aksrt-ticket-hint">' . esc_html($hint) . '</span>' : '') . '</div>'
|
|
. '<div class="aksrt-ticket-composer__box"><textarea id="' . esc_attr($field) . '" name="content" rows="' . $rows . '" maxlength="' . $max . '" required data-aksrt-ticket-editor aria-describedby="' . esc_attr($field) . '-count" placeholder="' . esc_attr($placeholder) . '"></textarea>'
|
|
. '<div class="aksrt-ticket-composer__bar">' . $tools
|
|
. '<span id="' . esc_attr($field) . '-count" class="aksrt-ticket-counter" data-aksrt-ticket-counter="' . esc_attr($field) . '">0 / ' . $max . '</span>'
|
|
. '<button type="submit" class="button' . ($primary ? ' button-primary' : '') . '">' . esc_html($submit) . '</button></div></div></form>';
|
|
}
|
|
|
|
private function ticket_info_card(array $ticket): string {
|
|
$id = (int) $ticket['id'];
|
|
$editable = empty($ticket['is_closed']);
|
|
$controls = $editable ? $this->ticket_info_controls($ticket) : [];
|
|
$rows = [
|
|
[__('工单号', 'aksrt'), esc_html((string) $ticket['ticket_num'])],
|
|
[__('状态', 'aksrt'), $controls['status'] ?? $this->ticket_status_badge($ticket), 'status'],
|
|
[__('用户', 'aksrt'), $this->ticket_user_chip((int) $ticket['user_id'], (string) $ticket['user_name'])],
|
|
[__('部门', 'aksrt'), $controls['ticket_type_id'] ?? esc_html((string) $ticket['ticket_type_name'] !== '' ? (string) $ticket['ticket_type_name'] : '—'), 'ticket_type_id'],
|
|
[__('跟进人', 'aksrt'), $controls['admin_id'] ?? esc_html((int) $ticket['admin_id'] > 0 ? (string) $ticket['admin_name'] : __('未指派', 'aksrt')), 'admin_id'],
|
|
];
|
|
if ((string) $ticket['order_number'] !== '') {
|
|
$rows[] = [__('关联订单', 'aksrt'), esc_html((string) $ticket['order_number'])];
|
|
}
|
|
$rows[] = [__('提交时间', 'aksrt'), esc_html((string) $ticket['post_time'])];
|
|
if ((int) $ticket['urge_count'] > 0) {
|
|
$rows[] = [__('催单', 'aksrt'), '<span class="aksrt-ticket-urge">' . esc_html(sprintf(__('%1$d 次,最近 %2$s', 'aksrt'), (int) $ticket['urge_count'], (string) $ticket['last_urge_time'])) . '</span>'];
|
|
}
|
|
$html = '<section class="aksrt-ticket-detail__card"><div class="aksrt-ticket-section-head"><h3>' . esc_html__('工单信息', 'aksrt') . '</h3>';
|
|
if ($editable) {
|
|
$html .= '<button type="button" class="button-link" data-aksrt-ticket-assign>' . esc_html__('由我跟进', 'aksrt') . '</button>';
|
|
}
|
|
$html .= '</div>';
|
|
if ($editable) {
|
|
$html .= $this->ticket_form_open('status', $id, 'aksrt-ticket-detail__status-form');
|
|
}
|
|
$html .= '<dl class="aksrt-ticket-detail__info">';
|
|
foreach ($rows as $row) {
|
|
[$label, $value] = $row;
|
|
$label_html = esc_html($label);
|
|
if ($editable && isset($row[2])) {
|
|
$label_html = '<label for="aksrt-ticket-' . esc_attr($row[2]) . '-' . $id . '">' . $label_html . '</label>';
|
|
}
|
|
$html .= '<dt>' . $label_html . '</dt><dd>' . $value . '</dd>';
|
|
}
|
|
$html .= '</dl>';
|
|
if ($editable) {
|
|
$html .= '<div class="aksrt-ticket-close-warning" data-aksrt-ticket-close-warning hidden><label><input type="checkbox" data-aksrt-ticket-close-confirm> <span>' . esc_html__('确认关闭工单:关闭后无法再次回复,尚未发送的回复不会保存。', 'aksrt') . '</span></label></div>';
|
|
$html .= '<button type="submit" class="button">' . esc_html__('保存工单信息', 'aksrt') . '</button></form>';
|
|
}
|
|
return $html . '</section>';
|
|
}
|
|
|
|
/** 工单信息中的可编辑值;不再重复渲染只读值或独立处理表单。 */
|
|
private function ticket_info_controls(array $ticket): array {
|
|
$id = (int) $ticket['id'];
|
|
$from = (string) $ticket['status'];
|
|
$controls = ['status' => '<select id="aksrt-ticket-status-' . $id . '" name="status" data-aksrt-ticket-status>'];
|
|
foreach (TicketStatusService::map() as $row) {
|
|
$key = (string) $row['status_key'];
|
|
if (TicketStatusService::can_transition($from, $key)) {
|
|
$controls['status'] .= '<option value="' . esc_attr($key) . '" data-closed="' . (empty($row['is_closed']) ? '0' : '1') . '"' . selected($from, $key, false) . '>' . esc_html((string) $row['name']) . '</option>';
|
|
}
|
|
}
|
|
$controls['status'] .= '</select>';
|
|
$controls['ticket_type_id'] = '<select id="aksrt-ticket-ticket_type_id-' . $id . '" name="ticket_type_id" required>';
|
|
foreach (TicketTypeService::all() as $row) {
|
|
$controls['ticket_type_id'] .= '<option value="' . (int) $row['id'] . '"' . selected((int) $ticket['ticket_type_id'], (int) $row['id'], false) . '>' . esc_html((string) $row['name']) . '</option>';
|
|
}
|
|
$controls['ticket_type_id'] .= '</select>';
|
|
$controls['admin_id'] = '<select id="aksrt-ticket-admin_id-' . $id . '" name="admin_id"><option value="0">' . esc_html__('未指派', 'aksrt') . '</option>';
|
|
$current = (int) $ticket['admin_id'];
|
|
$admins = $this->ticket_admin_options();
|
|
if ($current > 0 && !isset($admins[$current])) {
|
|
$admins[$current] = (string) $ticket['admin_name'];
|
|
}
|
|
foreach ($admins as $admin_id => $name) {
|
|
$controls['admin_id'] .= '<option value="' . $admin_id . '"' . selected($current, $admin_id, false) . '>' . esc_html($admin_id === get_current_user_id() ? sprintf(__('%s(我)', 'aksrt'), $name) : $name) . '</option>';
|
|
}
|
|
$controls['admin_id'] .= '</select>';
|
|
return $controls;
|
|
}
|
|
|
|
private function ticket_admin_options(): array {
|
|
static $options = null;
|
|
if ($options === null) {
|
|
$options = [];
|
|
foreach (get_users(['capability' => self::CAPABILITY, 'fields' => ['ID', 'display_name'], 'orderby' => 'display_name', 'number' => 200]) as $user) {
|
|
$options[(int) $user->ID] = (string) $user->display_name;
|
|
}
|
|
$me = wp_get_current_user();
|
|
$options[(int) $me->ID] = (string) $me->display_name;
|
|
}
|
|
return $options;
|
|
}
|
|
|
|
private function ticket_notes_card(int $id): string {
|
|
$notes = TicketNoteService::for_ticket($id);
|
|
$html = '<section class="aksrt-ticket-detail__card aksrt-ticket-detail__private"><div class="aksrt-ticket-section-head"><h3>' . esc_html__('内部备注', 'aksrt');
|
|
if ($notes !== []) {
|
|
$html .= ' <span class="aksrt-ticket-pill">' . count($notes) . '</span>';
|
|
}
|
|
$html .= '</h3></div><p class="aksrt-ticket-hint">' . esc_html__('仅管理员可见,不会发送给用户。', 'aksrt') . '</p>';
|
|
if ($notes !== []) {
|
|
$html .= '<ol class="aksrt-ticket-detail__notes">';
|
|
foreach ($notes as $note) {
|
|
$html .= '<li><div class="aksrt-ticket-detail__note-head"><strong>' . esc_html((string) $note['admin_name']) . '</strong><time>' . esc_html((string) $note['created_at']) . '</time></div><div>' . nl2br(esc_html((string) $note['content'])) . '</div></li>';
|
|
}
|
|
$html .= '</ol>';
|
|
}
|
|
return $html . $this->ticket_composer('note', $id, 'aksrt-ticket-note-' . $id, __('添加备注', 'aksrt'), '', __('记录处理进展、排查结论…', 'aksrt'), 2, '', __('添加内部备注', 'aksrt'), false) . '</section>';
|
|
}
|
|
|
|
private function ticket_attachments_html(array $urls): string {
|
|
if ($urls === []) {
|
|
return '';
|
|
}
|
|
$html = '<ul class="aksrt-ticket-detail__attachments">';
|
|
foreach ($urls as $index => $url) {
|
|
$html .= '<li><a href="' . esc_url((string) $url) . '" target="_blank" rel="noopener noreferrer"><img src="' . esc_url((string) $url) . '" alt="' . esc_attr(sprintf(__('查看附件 %d(新窗口)', 'aksrt'), $index + 1)) . '"></a></li>';
|
|
}
|
|
return $html . '</ul>';
|
|
}
|
|
}
|