| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Http\Requests; |
| 4 |
|
| 5 |
use FluentSupport\Framework\Foundation\RequestGuard; |
| 6 |
|
| 7 |
class TicketRequest extends RequestGuard |
| 8 |
{ |
| 9 |
/** |
| 10 |
* @return Array |
| 11 |
*/ |
| 12 |
public function rules() |
| 13 |
{ |
| 14 |
$rules = [ |
| 15 |
'ticket.title' => 'required', |
| 16 |
'ticket.content' => 'required' |
| 17 |
]; |
| 18 |
|
| 19 |
//If create customer checkbox is checked |
| 20 |
if($this->get('ticket.create_customer') == 'yes'){ |
| 21 |
$rules['newCustomer.email'] = 'required'; |
| 22 |
//If Create WP user checkbox is checked |
| 23 |
if($this->get('ticket.create_wp_user') == 'yes'){ |
| 24 |
$rules['newCustomer.username'] = 'required'; |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
return apply_filters('fluent_support/ticket_create_validation_rules', $rules); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* @return Array |
| 33 |
*/ |
| 34 |
public function messages() |
| 35 |
{ |
| 36 |
$messages = [ |
| 37 |
'ticket.title.required' => 'Ticket title is required', |
| 38 |
'ticket.content.required' => 'Ticket content is required', |
| 39 |
'newCustomer.email.required' => 'Customer email is required', |
| 40 |
'newCustomer.username.required' => 'Customer username is required wp user', |
| 41 |
]; |
| 42 |
|
| 43 |
return apply_filters('fluent_support/ticket_create_validation_messages', $messages); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
public function sanitize() |
| 48 |
{ |
| 49 |
$data = $this->all(); |
| 50 |
$ticketData = $data["ticket"]; |
| 51 |
|
| 52 |
$sanitizeRules = [ |
| 53 |
'customer_id' => 'intval', |
| 54 |
'mailbox_id' => 'intval', |
| 55 |
'title' => 'sanitize_text_field', |
| 56 |
'content' => 'wp_kses_post', |
| 57 |
'product_id' => 'intval', |
| 58 |
'client_priority' => 'sanitize_text_field', |
| 59 |
'create_customer' => 'sanitize_text_field', |
| 60 |
'create_wp_user' => 'sanitize_text_field', |
| 61 |
'first_name' => 'sanitize_text_field', |
| 62 |
'last_name' => 'sanitize_text_field', |
| 63 |
'email' => 'sanitize_email', |
| 64 |
'username' => 'sanitize_text_field', |
| 65 |
'password' => 'sanitize_text_field', |
| 66 |
]; |
| 67 |
|
| 68 |
if( $ticketData && is_array($ticketData) ) { |
| 69 |
foreach ($ticketData as $dataKey => $dataItem) { |
| 70 |
$sanitizeFunc = isset($sanitizeRules[$dataKey]) ? $sanitizeRules[$dataKey]: 'sanitize_text_field'; |
| 71 |
if(is_array($dataItem)) { |
| 72 |
$ticketData[$dataKey] = map_deep($dataItem, $sanitizeFunc); |
| 73 |
} else { |
| 74 |
$ticketData[$dataKey] = $sanitizeFunc($dataItem); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
$data["ticket"] = $ticketData; |
| 79 |
} |
| 80 |
|
| 81 |
$newCustomer = isset($data['newCustomer'])? $data['newCustomer']: []; |
| 82 |
|
| 83 |
if(is_array($newCustomer) && !empty($newCustomer)) { |
| 84 |
foreach ($newCustomer as $dataKey => $dataItem) { |
| 85 |
$sanitizeFunc = isset($sanitizeRules[$dataKey]) ? $sanitizeRules[$dataKey]: 'sanitize_text_field'; |
| 86 |
|
| 87 |
if(is_array($dataItem)) { |
| 88 |
$newCustomer[$dataKey] = map_deep($dataItem, $sanitizeFunc); |
| 89 |
} else { |
| 90 |
$newCustomer[$dataKey] = $sanitizeFunc($dataItem); |
| 91 |
} |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
$data['newCustomer'] = $newCustomer; |
| 96 |
} |
| 97 |
|
| 98 |
return $data; |
| 99 |
} |
| 100 |
} |
| 101 |
|