myLogger = \Config\Services::mylogger(); $this->addImgModel = new AddImgModel(); $this->feContentModel = new FEContentModel(); $this->clientModel = new ClientModel(); $this->faqModel = new FAQModel(); } //listing public function add_image_index() { $headerData['tab_name'] = 'Advertisement Images'; $headerData['page_name'] = 'Advertisement Images'; // $data['addImageList'] = $this->addImgModel->findAll(); $data['addImageList'] = $this->addImgModel->select('advertisement_images.*, clients.client_name, clients.short_name, CASE WHEN advertisement_images.is_active = 1 THEN "Active" ELSE "Inactive" END AS status', false) ->join('clients', 'advertisement_images.client_id = clients.id', 'left') ->where('advertisement_images.is_active', 1) ->findAll(); $data['client'] = $this->clientModel->where('is_active', 1)->where('client_type', 1)->findAll(); // dd($data); echo view('layout/header', $headerData); echo view('add_image_list', $data); echo view('layout/footer'); // $this->loadLayout('client_onboarding', $data); } // add and edit public function add_advertise_image() { try { $file = $this->request->getFile('advertise_image'); $client_id = $this->request->getPost('client_id'); //1) original file name for vaildations $fileName = $file->getClientName(); //original file name for vaildations $existing = $this->addImgModel->where('name', $fileName)->where('client_id', $fileName)->where('is_active', 1)->first(); if($existing){ return $this->respond(['status' => false, 'message' => 'This file has already been uploaded in active state.'], 400); } //skip 1) and use this // $fileName = $file->getRandomName(); // Same Name Multiple time upload means different name if (!$file || !$file->isValid()) { return $this->respond(['status' => false, 'message' => 'No file uploaded or invalid file.'], 400); } // $uploadPath = WRITEPATH . 'uploads/advertiseImage/'; $uploadPath = ROOTPATH . 'public/uploads/add_image_upload/'; if (!is_dir($uploadPath)) mkdir($uploadPath, 0755, true); $file->move($uploadPath, $fileName); $id = $this->request->getPost('add_image_id'); $data = ['name' => $fileName,'client_id'=>$client_id]; if ($id == 0) { $this->addImgModel->insert($data); } else { $this->addImgModel->update($id, $data); } return $this->respond(['status' => true, 'message' => 'Image saved successfully.']); } catch (\Exception $e) { return $this->respond(['status' => false, 'message' => $e->getMessage()], 500); } } // soft delete public function remove_advertise_image() { try { $id = $this->request->getPost('add_image_id'); if (!$id) { return $this->respond(['status' => false, 'message' => 'ID missing'], 400); } $data = ['is_active' => 0]; $this->addImgModel->update($id, $data); return $this->respond(['status' => true, 'message' => 'Deleted successfully']); } catch (\Exception $e) { return $this->respond(['status' => false, 'message' => $e->getMessage()], 500); } } // Preview image Went Edit. public function showAdvertiseImage($filename) { // $path = WRITEPATH . 'uploads/advertiseImage/' . $filename; $path = ROOTPATH . 'public/uploads/add_image_upload/' . $filename; if (!file_exists($path)) { throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound(); // return $this->response->setStatusCode(404, 'File not found'); } $mime = mime_content_type($path); // header('Content-Type: ' . $mimeType); // readfile($path); // exit; return $this->response->setHeader('Content-Type', $mime)->setBody(file_get_contents($path)); } public function frontend_content() { $method = $this->request->getMethod(); if ($this->request->getMethod() === 'post') { $id = $this->request->getPost('fe_id'); $data = $this->request->getPost(); unset($data['fe_id']); foreach ($data as $k => $v) { if ($v === '' || $v === null) {unset($data[$k]);} } // INSERT / UPDATE if (empty($id)) { $status = $this->feContentModel->insert($data); $text = "Created"; } else { $status = $this->feContentModel->update($id, $data); $text = "Updated"; } return $this->respond([ 'status' => $status ? true : false, 'message' => "Frontend Content $text " . ($status ? 'successfully' : 'failed'), 'code' => $status ? 200 : 400, 'data' => $data, ] , $status ? 200 : 400 ); } elseif ($method === 'get') { $id = $this->request->getGet('fe_id') ?? null; if (!empty($id)) { $data['fe_list'] = $this->feContentModel->where('id', $id)->orderBy('id', 'DESC')->findAll(); if (!empty($data)) { return $this->respond(['status' => true, 'code' => 200, 'data' => $data], 200); } else { return $this->respond(['status' => false, 'code' => 400, 'message' => 'No data found'], 200); } } $data['fe_list'] = $this->feContentModel->orderBy('id', 'DESC')->findAll(); return $this->loadLayout('frontend_content_list', ['data' => $data,'tab_name' => 'Front-End Content','page_name' => 'Front-End Content']); } elseif ($method === 'delete') { // $input = $this->request->getRawInput(); $id = $this->request->getGet('fe_id'); // ✅ THIS $id = $id ?? null; if (empty($id)) { return $this->respond([ 'status' => false, 'code' => 404, 'message' => 'No ID provided for deletion' ], 200); } $update_status = $this->feContentModel->where('id', $id)->set(['is_active' => 0])->update(); if ($update_status) { return $this->respond([ 'status' => true, 'code' => 200, 'message' => 'Data removed successfully', 'fe_id' => $id ], 200); } else { return $this->respond([ 'status' => false, 'code' => 400, 'message' => 'Failed to remove data', 'fe_id' => $id ], 200); } } } public function FAQ() { $method = strtolower($this->request->getMethod()); $returnType = strtolower($this->request->getGet('return_type') ?? 'api'); $ref = ['timestamp' => date('Y-m-d H:i:s')]; try { // --- 1. POST: CREATE OR UPDATE --- if ($method === 'post') { $id = $this->request->getPost('faq_id'); $data = array_filter($this->request->getPost(), fn($v) => $v !== '' && $v !== null); if (empty($id)) { $status = $this->faqModel->insert($data); $msg = "Created"; } else { $status = $this->faqModel->update($id, $data); $msg = "Updated"; } // if ($returnType === 'web') { // return redirect()->back()->with($status ? 'success' : 'error', "FAQ $msg " . ($status ? 'successfully' : 'failed')); // } // return $this->response->setJSON([ // ])->setStatusCode($result ? 200 : 400); return $this->response->setJSON([ 'status' => $status ? 'success' : 'error', 'message' => "FAQ $msg " . ($status ? 'successfully' : 'failed'), 'code' => $status ? 200 : 400, 'data' => $data, 'ref' => $ref ])->setStatusCode($status ? 200 : 400); } // --- 2. GET: FETCH LIST OR SINGLE --- elseif ($method === 'get') { $id = $this->request->getGet('faq_id'); if (!empty($id)) { if($returnType === 'web'){ $row = $this->faqModel->find((int)$id); }else{ $row = $this->faqModel->where('is_active', 1)->find((int)$id); } $data['faq_list'] = $row ? [$row] : []; } else { if($returnType === 'web'){ $data['faq_list'] = $this->faqModel->orderBy('id', 'desc')->findAll(); }else{ $data['faq_list'] = $this->faqModel->where('is_active', 1)->orderBy('id', 'desc')->findAll(); } } if ($returnType === 'web') { $data['tab_name'] = "FAQ"; $data['page_name'] = "FAQ"; // print_r($data);die; return $this->loadLayout('faq_list', $data); } // API Response Logic if (empty($data['faq_list'])) { return $this->response->setJSON([ 'status' => $returnType === 'web' ? false : 'error', 'message' => 'No data found', 'code' => 404, 'data' => [], 'ref' => $ref ])->setStatusCode(200); // Using 200 with error status is common for mobile apps to prevent crashes } return $this->response->setJSON([ 'status' => $returnType === 'web' ? true : 'success', 'message' => 'Data retrieved', 'code' => 200, 'data' => $data, 'ref' => $ref ])->setStatusCode(200); } // --- 3. DELETE: SOFT DELETE --- elseif ($method === 'delete') { $id = $this->request->getGet('faq_id'); $status = (!empty($id)) ? $this->faqModel->update($id, ['is_active' => 0]) : false; return $this->response->setJSON([ 'status' => $status ? ($returnType === 'web' ? true : 'success') : ($returnType === 'web' ? false : 'error'), 'message' => $status ? 'Data removed successfully' : 'Failed to remove or ID missing', 'code' => $status ? 200 : 400, 'data' => [], 'ref' => $ref ])->setStatusCode( 200 ); } } catch (\Throwable $e) { $msg = $e->getMessage(); return $this->response->setJSON([ 'status' => $returnType === 'web' ? false : 'error', 'code' => 500, 'message' => $msg, 'ref' => ['file' => $e->getFile(), 'line' => $e->getLine()] ])->setStatusCode(500); } } // public function frontend_content_files() // { // // 1. Define your physical system path // define('UPLOAD_PATH', ROOTPATH . 'public/uploads/add_image_upload/'); // // 2. Define the public URL so the editor can display the image later // // This is what the browser uses to see the image (e.g., https://site.com/uploads/...) // // $publicUrl = "https://yourdomain.com/public/uploads/add_image_upload/"; // $publicUrl = ROOTPATH . 'public/uploads/add_image_upload/'; // // if (!is_dir($uploadPath)) mkdir($uploadPath, 0755, true); // header('Content-Type: application/json'); // if ($_FILES['files']) { // // Ensure directory exists // if (!is_dir(UPLOAD_PATH)) { // mkdir(UPLOAD_PATH, 0775, true); // } // $fileName = time() . '_' . basename($_FILES['files']['name'][0]); // $fullPath = UPLOAD_PATH . $fileName; // if (move_uploaded_file($_FILES['files']['tmp_name'][0], $fullPath)) { // echo json_encode([ // "success" => true, // "data" => [ // "baseurl" => $publicUrl, // "files" => [$fileName] // ] // ]); // } else { // echo json_encode(["success" => false, "error" => "Failed to move file to " . UPLOAD_PATH]); // } // } // } }