1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Http/Livewire/RequiredClientInfo.php

114 lines
2.7 KiB
PHP
Raw Normal View History

2021-01-12 15:43:07 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Http\Livewire;
use Illuminate\Support\Facades\Validator;
2021-01-12 16:23:39 +01:00
use Illuminate\Support\Str;
2021-01-12 15:43:07 +01:00
use Livewire\Component;
class RequiredClientInfo extends Component
{
public $fields = [];
2021-01-12 16:23:39 +01:00
public $contact;
2021-01-12 15:43:07 +01:00
private $gateway;
2021-01-12 16:23:39 +01:00
private $mappings = [
'client_name' => 'name',
'contact_phone' => 'phone',
];
2021-01-12 15:43:07 +01:00
public function handleSubmit(array $data): bool
{
$rules = [];
collect($this->fields)->map(function ($field) use (&$rules) {
2021-01-12 16:23:39 +01:00
$rules[$field['name']] = array_key_exists('validation_rules', $field)
? $field['validation_rules']
: 'required';
2021-01-12 15:43:07 +01:00
});
$validator = Validator::make($data, $rules);
if ($validator->fails()) {
session()->flash('validation_errors', $validator->getMessageBag()->getMessages());
return false;
}
2021-01-12 16:23:39 +01:00
if ($this->updateClientDetails($data)) {
$this->emit('passed-required-fields-check');
return true;
}
// TODO: Throw an exception about not being able to update the profile.
return false;
}
private function updateClientDetails(array $data): bool
{
$client = [];
$contact = [];
foreach ($data as $field => $value) {
if (Str::startsWith($field, 'client_', )) {
$client[$this->mappings[$field]] = $value;
}
if (Str::startsWith($field, 'contact_')) {
$contact[$this->mappings[$field]] = $value;
}
}
$contact_update = $this->contact
->fill($contact)
->push();
$client_update = $this->contact->client
->fill($client)
->push();
if ($contact_update && $client_update) {
return true;
}
2021-01-12 15:43:07 +01:00
2021-01-12 16:23:39 +01:00
return false;
2021-01-12 15:43:07 +01:00
}
public function render()
{
// This will be coming from the gateway itself. Something like $gateway->getRequiredRules();
$this->fields = [
[
2021-01-12 16:23:39 +01:00
'name' => 'client_name',
'label' => ctrans('texts.name'),
2021-01-12 15:43:07 +01:00
'type' => 'text',
'validation_rules' => 'required|min:3'
],
[
2021-01-12 16:23:39 +01:00
'name' => 'contact_phone',
'label' => ctrans('texts.phone'),
2021-01-12 15:43:07 +01:00
'type' => 'number',
'validation_rules' => 'required|min:2',
],
];
return render('components.livewire.required-client-info');
}
}