2018-10-17 14:26:27 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2018-10-17 14:26:27 +02:00
|
|
|
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
|
2020-10-22 08:46:02 +02:00
|
|
|
use App\Http\ValidationRules\User\RelatedUserRule;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
2018-10-17 14:26:27 +02:00
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
|
|
|
|
class Request extends FormRequest
|
|
|
|
{
|
2020-11-25 15:19:52 +01:00
|
|
|
use MakesHash;
|
2020-10-22 08:46:02 +02:00
|
|
|
|
2018-10-17 14:26:27 +02:00
|
|
|
/**
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function rules()
|
|
|
|
{
|
2018-11-03 02:01:40 +01:00
|
|
|
return [];
|
2018-10-17 14:26:27 +02:00
|
|
|
}
|
2020-10-22 08:46:02 +02:00
|
|
|
|
|
|
|
public function globalRules($rules)
|
|
|
|
{
|
2020-11-25 15:19:52 +01:00
|
|
|
$merge_rules = [];
|
2020-10-22 08:46:02 +02:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
foreach ($this->all() as $key => $value) {
|
|
|
|
if (method_exists($this, $key)) {
|
|
|
|
$merge_rules = $this->{$key}($rules);
|
|
|
|
}
|
|
|
|
}
|
2020-10-22 08:46:02 +02:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
return array_merge($merge_rules, $rules);
|
2020-10-22 08:46:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function assigned_user_id($rules)
|
2020-11-25 15:19:52 +01:00
|
|
|
{
|
|
|
|
$rules['assigned_user_id'] = [
|
|
|
|
'bail' ,
|
|
|
|
'sometimes',
|
2020-10-22 08:46:02 +02:00
|
|
|
'nullable',
|
|
|
|
new RelatedUserRule($this->all())
|
|
|
|
];
|
|
|
|
|
|
|
|
return $rules;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function invoice_id($rules)
|
|
|
|
{
|
|
|
|
$rules['invoice_id'] = 'bail|nullable|sometimes|exists:invoices,id,company_id,'.auth()->user()->company()->id.',client_id,'.$this['client_id'];
|
|
|
|
|
|
|
|
return $rules;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function vendor_id($rules)
|
|
|
|
{
|
2020-10-22 23:00:49 +02:00
|
|
|
$rules['vendor_id'] = 'bail|nullable|sometimes|exists:vendors,id,company_id,'.auth()->user()->company()->id;
|
2020-10-22 08:46:02 +02:00
|
|
|
|
|
|
|
return $rules;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function decodePrimaryKeys($input)
|
|
|
|
{
|
|
|
|
if (array_key_exists('assigned_user_id', $input) && is_string($input['assigned_user_id'])) {
|
|
|
|
$input['assigned_user_id'] = $this->decodePrimaryKey($input['assigned_user_id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('user_id', $input) && is_string($input['user_id'])) {
|
|
|
|
$input['user_id'] = $this->decodePrimaryKey($input['user_id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('vendor_id', $input) && is_string($input['vendor_id'])) {
|
|
|
|
$input['vendor_id'] = $this->decodePrimaryKey($input['vendor_id']);
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-10-22 08:46:02 +02:00
|
|
|
|
|
|
|
if (array_key_exists('client_id', $input) && is_string($input['client_id'])) {
|
|
|
|
$input['client_id'] = $this->decodePrimaryKey($input['client_id']);
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-10-22 08:46:02 +02:00
|
|
|
|
|
|
|
if (array_key_exists('invoice_id', $input) && is_string($input['invoice_id'])) {
|
|
|
|
$input['invoice_id'] = $this->decodePrimaryKey($input['invoice_id']);
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-10-22 08:46:02 +02:00
|
|
|
|
|
|
|
if (array_key_exists('design_id', $input) && is_string($input['design_id'])) {
|
|
|
|
$input['design_id'] = $this->decodePrimaryKey($input['design_id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('project_id', $input) && is_string($input['project_id'])) {
|
|
|
|
$input['project_id'] = $this->decodePrimaryKey($input['project_id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($input['client_contacts'])) {
|
|
|
|
foreach ($input['client_contacts'] as $key => $contact) {
|
|
|
|
if (! array_key_exists('send_email', $contact) || ! array_key_exists('id', $contact)) {
|
|
|
|
unset($input['client_contacts'][$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($input['invitations'])) {
|
|
|
|
foreach ($input['invitations'] as $key => $value) {
|
|
|
|
if (isset($input['invitations'][$key]['id']) && is_numeric($input['invitations'][$key]['id'])) {
|
|
|
|
unset($input['invitations'][$key]['id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($input['invitations'][$key]['id']) && is_string($input['invitations'][$key]['id'])) {
|
|
|
|
$input['invitations'][$key]['id'] = $this->decodePrimaryKey($input['invitations'][$key]['id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_string($input['invitations'][$key]['client_contact_id'])) {
|
|
|
|
$input['invitations'][$key]['client_contact_id'] = $this->decodePrimaryKey($input['invitations'][$key]['client_contact_id']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($input['contacts'])) {
|
|
|
|
foreach ($input['contacts'] as $key => $contact) {
|
|
|
|
if (array_key_exists('id', $contact) && is_numeric($contact['id'])) {
|
|
|
|
unset($input['contacts'][$key]['id']);
|
|
|
|
} elseif (array_key_exists('id', $contact) && is_string($contact['id'])) {
|
|
|
|
$input['contacts'][$key]['id'] = $this->decodePrimaryKey($contact['id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Filter the client contact password - if it is sent with ***** we should ignore it!
|
|
|
|
if (isset($contact['password'])) {
|
|
|
|
if (strlen($contact['password']) == 0) {
|
|
|
|
$input['contacts'][$key]['password'] = '';
|
|
|
|
} else {
|
|
|
|
$contact['password'] = str_replace('*', '', $contact['password']);
|
|
|
|
|
|
|
|
if (strlen($contact['password']) == 0) {
|
|
|
|
unset($input['contacts'][$key]['password']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
return $input;
|
2020-10-22 08:46:02 +02:00
|
|
|
}
|
2018-10-17 14:26:27 +02:00
|
|
|
}
|