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 { $data = $this->request->getPost(); $sanitized_post_data = sanitizeInputArrayAdvanced($data); $id = ((int) $sanitized_post_data['add_image_id']) ?? null; $rules = [ 'client_id' => [ 'rules' => 'required|integer', 'errors' => [ 'required' => 'Client is required', 'integer' => 'Invalid client selected' ] ], ]; $rules['advertise_image'] = [ 'rules' => ($id === 0 ? 'uploaded[advertise_image]|' : '') // required only for ADD . 'is_image[advertise_image]' . '|mime_in[advertise_image,image/jpg,image/jpeg,image/png]' . '|max_size[advertise_image,200]' . '|min_dims[advertise_image,1640,664]' . '|max_dims[advertise_image,1640,664]', 'errors' => [ 'uploaded' => 'Image is required', 'is_image' => 'File must be an image', 'mime_in' => 'Only JPG, JPEG, PNG allowed', 'max_size' => 'Image size must not exceed 200 KB', 'min_dims' => 'Image dimensions must be exactly 1640x664 pixels', 'max_dims' => 'Image dimensions must be exactly 1640x664 pixels', ] ]; if (!$this->validate($rules)) { return $this->response->setStatusCode(400)->setJSON([ 'status' => false, 'message' => 'Input validation failed', 'code' => 400, 'errors' => $this->validator->getErrors() ]); } $file = $this->request->getFile('advertise_image'); $client_id = $sanitized_post_data['client_id'] ?? null; //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 = $sanitized_post_data['add_image_id'] ?? null; $details = ['name' => $fileName,'client_id'=>$client_id]; if ($id == 0) { $this->addImgModel->insert($details); } else { $this->addImgModel->update($id, $details); } 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') { $rules = [ 'type' => [ 'rules' => 'required|max_length[255]', 'errors' => [ 'required' => 'Type is required', 'max_length' => 'Type cannot exceed 255 characters' ] ], 'content_section' => [ 'rules' => 'required|max_length[255]', 'errors' => [ 'required' => 'Content Section is required', 'max_length' => 'Content Section cannot exceed 255 characters' ] ], 'heading' => [ 'rules' => 'required|max_length[255]', 'errors' => [ 'required' => 'Heading is required', 'max_length' => 'Heading cannot exceed 255 characters' ] ], 'content' => [ 'rules' => 'required|max_length[5000]', 'errors' => [ 'required' => 'Content is required', 'max_length' => 'Content cannot exceed 5000 characters' ] ], 'notes' => [ 'rules' => 'required|max_length[1500]', 'errors' => [ 'required' => 'Notes are required', 'max_length' => 'Notes cannot exceed 1500 characters' ] ] ]; if (!$this->validate($rules)) { return $this->response->setStatusCode(400)->setJSON([ 'status' => false, 'message' => 'Input validation failed', 'code' => 400, 'errors' => $this->validator->getErrors() ]); } $request_post_data = $this->request->getPost(); $data = sanitizeInputArrayAdvanced($request_post_data); $id = $data['fe_id']; 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') { $rules = [ 'category' => [ 'rules' => 'required', 'errors' => [ 'required' => 'Category is required' ] ], 'question' => [ 'rules' => 'required', 'errors' => [ 'required' => 'Question is required' ] ], 'answer' => [ 'rules' => 'required', 'errors' => [ 'required' => 'Answer is required' ] ] ]; $request_post_data = $this->request->getPost(); $sanitized_post_data = sanitizeInputArrayAdvanced($request_post_data); $data = array_filter($sanitized_post_data, fn($v) => $v !== '' && $v !== null); $id = $data['faq_id']; 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]); // } // } // } }