Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 110 |
|
0.00% |
0 / 11 |
CRAP | |
0.00% |
0 / 1 |
| ContactController | |
0.00% |
0 / 110 |
|
0.00% |
0 / 11 |
600 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| index | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| create | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| store | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
12 | |||
| edit | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| update | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
20 | |||
| delete | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| search | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| show | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| toggleStatus | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| getWithCompleteness | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Modules\ContactModule\Controllers; |
| 4 | |
| 5 | use App\Controllers\BaseController; |
| 6 | use App\Modules\ContactModule\Models\ContactModel; |
| 7 | |
| 8 | class ContactController extends BaseController |
| 9 | { |
| 10 | protected ContactModel $contactModel; |
| 11 | |
| 12 | public function __construct() |
| 13 | { |
| 14 | $this->contactModel = new ContactModel(); |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * ========================= |
| 19 | * LISTE CONTACTS |
| 20 | * ========================= |
| 21 | */ |
| 22 | public function index() |
| 23 | { |
| 24 | $data = [ |
| 25 | 'title' => 'Contacts', |
| 26 | 'contacts' => $this->contactModel |
| 27 | ->orderBy('id', 'DESC') |
| 28 | ->paginate(20), |
| 29 | 'pager' => $this->contactModel->pager, |
| 30 | ]; |
| 31 | |
| 32 | return view('App\Modules\ContactModule\Views\contacts\index', $data); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * ========================= |
| 37 | * FORM CREATE |
| 38 | * ========================= |
| 39 | */ |
| 40 | public function create() |
| 41 | { |
| 42 | return view('App\Modules\ContactModule\Views\contacts\create', [ |
| 43 | 'title' => 'Créer un contact' |
| 44 | ]); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * ========================= |
| 49 | * STORE CONTACT |
| 50 | * ========================= |
| 51 | */ |
| 52 | public function store() |
| 53 | { |
| 54 | $data = [ |
| 55 | 'type' => $this->request->getPost('type') ?? 'customer', |
| 56 | 'name' => $this->request->getPost('name'), |
| 57 | 'email' => $this->request->getPost('email'), |
| 58 | 'phone' => $this->request->getPost('phone'), |
| 59 | 'whatsapp' => $this->request->getPost('whatsapp'), |
| 60 | 'company' => $this->request->getPost('company'), |
| 61 | 'job_title' => $this->request->getPost('job_title'), |
| 62 | 'status' => $this->request->getPost('status') ?? 'active', |
| 63 | |
| 64 | 'sms_enabled' => (int) $this->request->getPost('sms_enabled'), |
| 65 | 'email_enabled' => (int) $this->request->getPost('email_enabled'), |
| 66 | 'whatsapp_enabled' => (int) $this->request->getPost('whatsapp_enabled'), |
| 67 | |
| 68 | 'metadata' => json_encode( |
| 69 | $this->request->getPost('metadata') |
| 70 | ? json_decode($this->request->getPost('metadata'), true) |
| 71 | : [] |
| 72 | ), |
| 73 | ]; |
| 74 | |
| 75 | if (!$this->contactModel->insert($data)) { |
| 76 | return redirect()->back() |
| 77 | ->with('error', 'Erreur lors de la création du contact'); |
| 78 | } |
| 79 | |
| 80 | return redirect()->to('/dashboard/contacts') |
| 81 | ->with('success', 'Contact créé avec succès'); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * ========================= |
| 86 | * EDIT FORM |
| 87 | * ========================= |
| 88 | */ |
| 89 | public function edit($id) |
| 90 | { |
| 91 | $contact = $this->contactModel->find($id); |
| 92 | |
| 93 | if (!$contact) { |
| 94 | throw new \CodeIgniter\Exceptions\PageNotFoundException('Contact introuvable'); |
| 95 | } |
| 96 | |
| 97 | return view('App\Modules\ContactModule\Views\contacts\edit', [ |
| 98 | 'title' => 'Modifier contact', |
| 99 | 'contact' => $contact |
| 100 | ]); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * ========================= |
| 105 | * UPDATE CONTACT |
| 106 | * ========================= |
| 107 | */ |
| 108 | public function update($id) |
| 109 | { |
| 110 | $contact = $this->contactModel->find($id); |
| 111 | |
| 112 | if (!$contact) { |
| 113 | return redirect()->back()->with('error', 'Contact introuvable'); |
| 114 | } |
| 115 | |
| 116 | $data = [ |
| 117 | 'type' => $this->request->getPost('type'), |
| 118 | 'name' => $this->request->getPost('name'), |
| 119 | 'email' => $this->request->getPost('email'), |
| 120 | 'phone' => $this->request->getPost('phone'), |
| 121 | 'whatsapp' => $this->request->getPost('whatsapp'), |
| 122 | 'company' => $this->request->getPost('company'), |
| 123 | 'job_title' => $this->request->getPost('job_title'), |
| 124 | 'status' => $this->request->getPost('status'), |
| 125 | |
| 126 | 'sms_enabled' => (int) $this->request->getPost('sms_enabled'), |
| 127 | 'email_enabled' => (int) $this->request->getPost('email_enabled'), |
| 128 | 'whatsapp_enabled' => (int) $this->request->getPost('whatsapp_enabled'), |
| 129 | |
| 130 | 'metadata' => json_encode( |
| 131 | $this->request->getPost('metadata') |
| 132 | ? json_decode($this->request->getPost('metadata'), true) |
| 133 | : [] |
| 134 | ), |
| 135 | ]; |
| 136 | |
| 137 | if (!$this->contactModel->update($id, $data)) { |
| 138 | return redirect()->back() |
| 139 | ->with('error', 'Erreur lors de la mise à jour'); |
| 140 | } |
| 141 | |
| 142 | return redirect()->to('/dashboard/contacts') |
| 143 | ->with('success', 'Contact mis à jour'); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * ========================= |
| 148 | * DELETE CONTACT |
| 149 | * ========================= |
| 150 | */ |
| 151 | public function delete($id) |
| 152 | { |
| 153 | if (!$this->contactModel->delete($id)) { |
| 154 | return redirect()->back()->with('error', 'Suppression impossible'); |
| 155 | } |
| 156 | |
| 157 | return redirect()->to('/dashboard/contacts') |
| 158 | ->with('success', 'Contact supprimé'); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * ========================= |
| 163 | * SEARCH CONTACTS |
| 164 | * ========================= |
| 165 | */ |
| 166 | public function search() |
| 167 | { |
| 168 | $keyword = $this->request->getGet('q'); |
| 169 | |
| 170 | $results = $this->contactModel->search($keyword); |
| 171 | |
| 172 | return $this->response->setJSON($results); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * ========================= |
| 177 | * CONTACT DETAILS (API READY) |
| 178 | * ========================= |
| 179 | */ |
| 180 | public function show($id) |
| 181 | { |
| 182 | $contact = $this->contactModel->find($id); |
| 183 | |
| 184 | if (!$contact) { |
| 185 | return $this->response->setStatusCode(404) |
| 186 | ->setJSON(['error' => 'Contact introuvable']); |
| 187 | } |
| 188 | |
| 189 | return $this->response->setJSON([ |
| 190 | 'contact' => $contact, |
| 191 | 'channels' => $this->contactModel->getAvailableChannels($contact) |
| 192 | ]); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * ========================= |
| 197 | * TOGGLE STATUS |
| 198 | * ========================= |
| 199 | */ |
| 200 | public function toggleStatus($id) |
| 201 | { |
| 202 | $contact = $this->contactModel->find($id); |
| 203 | |
| 204 | if (!$contact) { |
| 205 | return $this->response->setJSON(['error' => 'Contact introuvable']); |
| 206 | } |
| 207 | |
| 208 | $newStatus = $contact->status === 'active' ? 'inactive' : 'active'; |
| 209 | |
| 210 | $this->contactModel->update($id, [ |
| 211 | 'status' => $newStatus |
| 212 | ]); |
| 213 | |
| 214 | return $this->response->setJSON([ |
| 215 | 'status' => $newStatus |
| 216 | ]); |
| 217 | } |
| 218 | |
| 219 | |
| 220 | |
| 221 | public function getWithCompleteness(): array |
| 222 | { |
| 223 | $contacts = $this->findAll(); |
| 224 | |
| 225 | foreach ($contacts as $c) { |
| 226 | |
| 227 | $fields = [ |
| 228 | $c->name, |
| 229 | $c->email, |
| 230 | $c->phone, |
| 231 | $c->whatsapp, |
| 232 | $c->company, |
| 233 | $c->job_title, |
| 234 | ]; |
| 235 | |
| 236 | $score = 0; |
| 237 | |
| 238 | foreach ($fields as $f) { |
| 239 | if (!empty($f)) { |
| 240 | $score++; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | $c->completeness_score = $score; |
| 245 | } |
| 246 | |
| 247 | usort($contacts, fn($a, $b) => $b->completeness_score <=> $a->completeness_score); |
| 248 | |
| 249 | return $contacts; |
| 250 | } |
| 251 | } |