1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Livewire; |
4
|
|
|
|
5
|
|
|
use App\Models\Contact; |
6
|
|
|
use App\Models\Setting; |
7
|
|
|
use Illuminate\Support\Facades\Http; |
8
|
|
|
use Jantinnerezo\LivewireAlert\LivewireAlert; |
9
|
|
|
use Livewire\Component; |
10
|
|
|
use Livewire\WithPagination; |
11
|
|
|
|
12
|
|
|
class ContactTable extends Component |
13
|
|
|
{ |
14
|
|
|
use WithPagination, LivewireAlert; |
|
|
|
|
15
|
|
|
|
16
|
|
|
public $perPage = 10; |
17
|
|
|
public $search = ''; |
18
|
|
|
public $orderBy = 'id'; |
19
|
|
|
public $orderAsc = true; |
20
|
|
|
|
21
|
|
|
public $contact = null; |
22
|
|
|
|
23
|
|
|
public function render() |
24
|
|
|
{ |
25
|
|
|
return view('components.contact-table', [ |
26
|
|
|
'contacts' => Contact::search($this->search) |
27
|
|
|
->orderBy($this->orderBy, $this->orderAsc ? 'asc' : 'desc') |
28
|
|
|
->paginate($this->perPage), |
29
|
|
|
]); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function view($id) |
33
|
|
|
{ |
34
|
|
|
$this->contact = Contact::findOrFail($id); |
35
|
|
|
$this->openViewModal(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param $id |
40
|
|
|
*/ |
41
|
|
|
public function createWPAccount($id) |
42
|
|
|
{ |
43
|
|
|
$settings = Setting::pluck('value', 'key')->toArray(); |
44
|
|
|
|
45
|
|
|
if (!isset($settings['site_url']) || !isset($settings['username']) || !isset($settings['password'])) { |
46
|
|
|
return $this->alert('warning', 'Missing WP site settings'); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$contact = Contact::findOrFail($id); |
50
|
|
|
|
51
|
|
|
$response = $this->sendHttpRequest($settings, $contact); |
52
|
|
|
|
53
|
|
|
if ($response->successful()) { |
54
|
|
|
$contact->update(['is_wp_synced' => 1]); |
55
|
|
|
$this->alert('success', 'Data sync successfull'); |
56
|
|
|
} else { |
57
|
|
|
$body = json_decode($response->body(), true); |
58
|
|
|
if(isset($body['message'])) { |
59
|
|
|
$detail = $body['message']; |
60
|
|
|
} else { |
61
|
|
|
$detail = 'Unknown'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->alert('warning', 'Something wrong happened. Please try again. DETAIL: ' . $detail); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function openViewModal() |
69
|
|
|
{ |
70
|
|
|
$this->dispatchBrowserEvent('openContactViewModal'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function closeViewModal() |
74
|
|
|
{ |
75
|
|
|
$this->dispatchBrowserEvent('closeContactViewModal'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function getBaseUrl($fullUrl) |
79
|
|
|
{ |
80
|
|
|
$url_info = parse_url($fullUrl); |
81
|
|
|
return $url_info['scheme'] . '://' . $url_info['host'] . '/wp-json/wlu/v1/'; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function sendHttpRequest($settings, $contact) |
85
|
|
|
{ |
86
|
|
|
return Http::withBasicAuth($settings['username'], $settings['password'])->post($this->getBaseUrl($settings['site_url']) . 'customers', [ |
87
|
|
|
'name' => $contact->name, |
88
|
|
|
'phone' => $contact->phone_number, |
89
|
|
|
'email' => $contact->email, |
90
|
|
|
'budget' => $contact->budget, |
91
|
|
|
'message' => $contact->message, |
92
|
|
|
'source' => route('contacts.show', ['id' => $contact->id]) |
93
|
|
|
]); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|