Files

1022 lines
28 KiB
PHP
Raw Permalink Normal View History

2025-02-04 23:06:08 +01:00
<?php
Route::add('/client/login', function () {
include_once __DIR__ . '/client-login.php';
}, 'get');
Route::add('/client/logout', function () {
unset($_SESSION["is_logged_in"]);
unset($_SESSION['role']);
unset( $_SESSION['user']);
header('Location: /client/login');
}, 'get');
Route::add('/client', function () {
header('Location: /client/login');
}, 'get');
Route::add('/client/login', function () {
$error = false;
$data = [];
2025-08-07 16:34:00 +01:00
if (empty($_POST['password']) || empty($_POST['email'])) {
2025-02-04 23:06:08 +01:00
$error = true;
// include_once __DIR__ . '/layout/header/Clientleft_sidebar.php';
include_once __DIR__ . '/client-login.php';
// include_once __DIR__ . '/layout/footer/Clientnone_footer.php';
exit;
} else {
// Collect form data
$raw_password = $_POST['password'];
$email = $_POST['email'];
// Prepare data array
$data = [
'password' => password_hash($raw_password, PASSWORD_BCRYPT),
'email' => $email,
];
// Insert data into the database using LicenseModel
$userModel = new UserModel();
2025-08-07 16:34:00 +01:00
$result = $userModel->get_by_field('email', $email);
2025-02-04 23:06:08 +01:00
// var_dump($result);exit;
if ($result) {
if (password_verify($raw_password, $result['password']) &&
$result['status'] == 'active' &&
$result['role'] == 'client') {
$_SESSION['is_logged_in'] = true;
$_SESSION['role'] = $result['role'];
$_SESSION['user'] = $result['id'];
header('Location: /client/report');
} else {
$error = true;
// include_once __DIR__ . '/layout/header/Clientleft_sidebar.php';
include_once __DIR__ . '/client-login.php';
// include_once __DIR__ . '/layout/footer/Clientnone_footer.php';
exit;
}
}
$error = true;
include_once __DIR__ . '/client-login.php';
}
}, 'post');
Route::add('/client/report', function () {
check_login();
$format = isset($_GET['format']) ? $_GET['format'] : 'json';
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$per_page = isset($_GET['size']) ? intval($_GET['size']) : 10;
$sort = isset($_GET['sort']) ? $_GET['sort'] : 'id';
$direction = isset($_GET['direction']) ? $_GET['direction'] : 'ASC';
$date = isset($_GET['date']) ? $_GET['date'] : '';
$reportModel = new ReportModel();
$data = [
'page_title' => 'Report',
'date' => $date
];
$user = $_SESSION['user'];
$where = [];
$where[] = "location_id IN (SELECT id from location WHERE user_id = {$user} )";
if ($date != '') {
$where['date'] = '"' . $date . '"';
}
$result = $reportModel->get_paginated($page, $per_page, $where, 'id', 'DESC');
// echo json_encode($result);
if ($result) {
if ($format == 'json') {
$data = array_merge($data, $result);
}
}
include_once __DIR__ . '/layout/header/Clientleft_sidebar.php';
include_once __DIR__ . '/reportListing.php';
include_once __DIR__ . '/layout/footer/Clientnone_footer.php';
}, 'get');
Route::add('/client/location', function () {
check_login();
$format = isset($_GET['format']) ? $_GET['format'] : 'json';
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$per_page = isset($_GET['size']) ? intval($_GET['size']) : 10;
$sort = isset($_GET['sort']) ? $_GET['sort'] : 'id';
$direction = isset($_GET['direction']) ? $_GET['direction'] : 'ASC';
$name = isset($_GET['name']) ? $_GET['name'] : '';
$locationModel = new LocationModel();
$data = [
'page_title' => 'Location',
'name' => $name
];
$where = [
'user_id' => $_SESSION['user']
];
if ($name != '') {
$where['name'] = '"' . $name . '"';
}
$result = $locationModel->get_paginated($page, $per_page, $where, $sort, $direction);
if ($result) {
if ($format == 'json') {
$data = array_merge($data, $result);
}
}
include_once __DIR__ . '/layout/header/Clientleft_sidebar.php';
include_once __DIR__ . '/locationListing.php';
include_once __DIR__ . '/layout/footer/Clientnone_footer.php';
}, 'get');
Route::add('/client/location/add', function () {
check_login();
$error = false;
$data = [
'page_title' => 'Location'
];
include_once __DIR__ . '/layout/header/Clientleft_sidebar.php';
include_once __DIR__ . '/locationAdd.php';
include_once __DIR__ . '/layout/footer/Clientnone_footer.php';
}, 'get');
Route::add('/client/location/add', function () {
check_login();
$error = false;
$data = [
'page_title' => 'Location'
];
if (empty($_POST['name']) || empty($_POST['apikey']) || empty($_POST['location_id'])) {
$error = true;
include_once __DIR__ . '/layout/header/Clientleft_sidebar.php';
include_once __DIR__ . '/locationAdd.php';
include_once __DIR__ . '/layout/footer/Clientnone_footer.php';
} else {
// Collect form data
$name = $_POST['name'];
$apikey = $_POST['apikey'];
$webhook = $_POST['webhook'];
$location_id = $_POST['location_id'];
// Prepare data array
$data = [
'name' => $name,
'apikey' => $apikey,
'webhook' => $webhook,
'location_id' => $location_id,
'created_at' => $current_date,
'user_id' => $_SESSION['user']
];
// Insert data into the database using LicenseModel
$locationModel = new LocationModel();
$locationModel->create($data);
header('Location: /client/location');
}
}, 'post');
Route::add('/client/location/edit/([0-9]+)', function ($id) {
check_login();
$error = false;
$locationModel = new LocationModel();
$model = $locationModel->get($id);
if (!$model) {
header('Location: /admin/location');
exit;
}
$data = [
'page_title' => 'Location',
'id' => $id
];
if (empty($_POST['name']) || empty($_POST['apikey']) || empty($_POST['location_id']) ) {
$error = true;
include_once __DIR__ . '/layout/header/Clientleft_sidebar.php';
include_once __DIR__ . '/locationEdit.php';
include_once __DIR__ . '/layout/footer/Clientnone_footer.php';
} else {
// Collect form data
$name = $_POST['name'];
$apikey = $_POST['apikey'];
$webhook = $_POST['webhook'];
$location_id = $_POST['location_id'];
// Generate apikey
$current_date = date('Y-m-d H:i:s');
// Prepare data array
$data = [
'name' => $name,
'apikey' => $apikey,
'webhook' => $webhook,
'location_id' => $location_id,
];
$locationModel = new LocationModel();
$locationModel->edit($data, $id);
header('Location: /client/location');
}
}, 'post');
Route::add('/client/location/edit/([0-9]+)', function ($id) {
check_login();
$error = false;
$locationModel = new LocationModel();
$model = $locationModel->get($id);
if (!$model) {
header('Location: /client/location');
exit;
}
$data = [
'page_title' => 'Location',
'model' => $model
];
include_once __DIR__ . '/layout/header/Clientleft_sidebar.php';
include_once __DIR__ . '/locationEdit.php';
include_once __DIR__ . '/layout/footer/Clientnone_footer.php';
}, 'get');
Route::add('/client/location/delete/([0-9]+)', function ($id) {
check_login();
$locationModel = new LocationModel();
$locationModel->real_delete($id);
header('Location: /client/location');
}, 'get');
Route::add('/client/profile', function () {
check_login();
$error = false;
$userModel = new UserModel();
$model = $userModel->get($_SESSION['user']);
$data = [
'page_title' => 'Profile',
'model' => $model
];
include_once __DIR__ . '/layout/header/Clientleft_sidebar.php';
include_once __DIR__ . '/client-profile.php';
include_once __DIR__ . '/layout/footer/Clientnone_footer.php';
}, 'get');
Route::add('/client/profile/edit/([0-9]+)', function ($id) {
check_login();
$error = false;
$userModel = new UserModel();
$model = $userModel->get($id);
if (!$model) {
header('Location: /client/profile');
exit;
}
$data = [
'page_title' => 'Profile',
'id' => $id
];
if (empty($_POST['email']) || empty($_POST['status'])) {
$error = true;
include_once __DIR__ . '/layout/header/Clientleft_sidebar.php';
include_once __DIR__ . '/client-profile.php';
include_once __DIR__ . '/layout/footer/Clientnone_footer.php';
} else {
// Collect form data
$password = isset($_POST['password']) ? $_POST['password'] : '';
$email = $_POST['email'];
$status = isset($_POST['status']) ? $_POST['status'] : 'active';
// Prepare data array
$data = [
'email' => $email,
'status' => $status
];
if (strlen($password) > 0) {
$data['password'] = password_hash($password, PASSWORD_BCRYPT);
}
// Insert data into the database using LicenseModel
$userModel = new UserModel();
$userModel->edit($data, $id);
header('Location: /client/profile');
}
}, 'post');
Route::add('/client/project', function () {
check_login();
$format = isset($_GET['format']) ? $_GET['format'] : 'json';
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$per_page = isset($_GET['size']) ? intval($_GET['size']) : 10;
$sort = isset($_GET['sort']) ? $_GET['sort'] : 'id';
$direction = isset($_GET['direction']) ? $_GET['direction'] : 'ASC';
// $relationship_num = isset($_GET['relationship_num']) ? $_GET['relationship_num'] : '';
$projectModel = new ProjectModel();
$data = [
'page_title' => 'Project',
];
$where = [
"user_id" => $_SESSION['user']
];
// if ($relationship_num != '') {
// $where['relationship_num'] = '"' . $relationship_num . '"';
// }
$result = $projectModel->get_paginated($page, $per_page, $where, 'id', 'DESC');
// echo json_encode($result);
if ($result) {
if ($format == 'json') {
$data = array_merge($data, $result);
}
}
include_once __DIR__ . '/layout/header/Clientleft_sidebar.php';
include_once __DIR__ . '/projectListing.php';
include_once __DIR__ . '/layout/footer/Clientnone_footer.php';
}, 'get');
Route::add('/client/project/add', function () {
check_login();
$error = false;
$data = [
'page_title' => 'Project'
];
include_once __DIR__ . '/layout/header/Clientleft_sidebar.php';
include_once __DIR__ . '/projectAdd.php';
include_once __DIR__ . '/layout/footer/Clientnone_footer.php';
}, 'get');
Route::add('/client/project/edit/([0-9]+)', function ($id) {
check_login();
$error = false;
$projectModel = new ProjectModel();
$model = $projectModel->get($id);
if (!$model) {
header('Location: /client/project');
exit;
}
$data = [
'page_title' => 'Project',
'model' => $model
];
include_once __DIR__ . '/layout/header/Clientleft_sidebar.php';
include_once __DIR__ . '/projectEdit.php';
include_once __DIR__ . '/layout/footer/Clientnone_footer.php';
}, 'get');
Route::add('/client/project/add', function () {
check_login();
$error = false;
$data = [
'page_title' => 'Project'
];
if (empty($_POST['project_name']) || empty($_POST['slot']) || empty($_POST['days']) || empty($_POST['score_threshold']) || empty($_POST['actual_score']) || empty($_POST['webhook']) || empty($_POST['calendar_id']) || empty($_POST['location'])) {
$error = true;
include_once __DIR__ . '/layout/header/Clientleft_sidebar.php';
include_once __DIR__ . '/projectAdd.php';
include_once __DIR__ . '/layout/footer/Clientnone_footer.php';
} else {
// Collect form data
$project_name = $_POST['project_name'];
$slot = $_POST['slot'];
$days = $_POST['days'];
// $alert = $_POST['alert'];
$score_threshold = $_POST['score_threshold'];
$actual_score = $_POST['actual_score'];
$webhook = $_POST['webhook'];
// $webhook_payload = $_POST['webhook_payload'];
$calendar_id = $_POST['calendar_id'];
$location = $_POST['location'];
$current_date = date('Y-m-d H:i:s');
$webhook_payload = array(
"project_name" => $project_name,
);
$webhook_payload = json_encode($webhook_payload);
// echo $webhook_payload;
// exit;
// function create_calendar_id()
// {
// $dt = microtime(true) * 1000; // Get current time in milliseconds
// $uuid = preg_replace_callback('/[xy]/', function ($matches) use ($dt) {
// $r = ($dt + mt_rand() * 16) % 16 | 0;
// $dt = floor($dt / 16);
// return ($matches[0] == 'x' ? dechex($r) : (dechex($r & 0x3 | 0x8)));
// }, 'xxxxxxxxxx');
// return $uuid;
// }
// function create_calendar_id()
// {
// $base = uniqid(); // Use uniqid as a base
// $uuid = preg_replace_callback('/[a-f0-9]/', function ($matches) {
// return dechex(mt_rand(0, 15));
// }, $base);
// return $uuid;
// }
// $config = MkdConfig::get_instance()->get_config();
// $calendar = $config['domain-name'] . "/admin/calendar/";
// $calendars = create_calendar_id();
// Prepare data array
// $calendar_data = [
// 'slot' => $slot,
// 'days' => $days,
// 'calendar' => $calendars,
// 'created_at' => $current_date
// ];
// $calendarModel = new CalendarModel();
// $calendarModel->create($calendar_data);
// echo $test;
// exit;
// if ($score_threshold < $actual_score) {
// $alert = "Yes";
// } else {
$alert = "Off";
// }
$data = [
'project_name' => $project_name,
'slot' => $slot,
'days' => $days,
'alert' => $alert,
'score_threshold' => $score_threshold,
'actual_score' => $actual_score,
'webhook' => $webhook,
'payload' => $webhook_payload,
'calendar' => $calendar_id,
'location' => $location,
'created_at' => $current_date,
'user_id' => $_SESSION["user"]
];
// Insert data into the database using LicenseModel
$projectModel = new ProjectModel();
$projectModel->create($data);
echo 'Project Added';
// header('Location: /admin/project');
}
}, 'post');
Route::add('/client/project/edit/([0-9]+)', function ($id) {
check_login();
$error = false;
$projectModel = new ProjectModel();
$model = $projectModel->get($id);
if (!$model) {
header('Location: /client/project');
exit;
}
$data = [
'page_title' => 'Project',
'id' => $id
];
if (empty($_POST['project_name']) || empty($_POST['slot']) || empty($_POST['days']) || empty($_POST['score_threshold']) || empty($_POST['actual_score']) || empty($_POST['webhook']) || empty($_POST['webhook_payload']) || empty($_POST['calendar_id']) || empty($_POST['location'])) {
$error = true;
include_once __DIR__ . '/layout/header/Clientleft_sidebar.php';
include_once __DIR__ . '/projectEdit.php';
include_once __DIR__ . '/layout/footer/Clientnone_footer.php';
} else {
// Collect form data
$project_name = $_POST['project_name'];
$slot = $_POST['slot'];
$days = $_POST['days'];
// $alert = $_POST['alert'];
$score_threshold = $_POST['score_threshold'];
$actual_score = $_POST['actual_score'];
$webhook = $_POST['webhook'];
$webhook_payload = $_POST['webhook_payload'];
$calendar_id = $_POST['calendar_id'];
$location = $_POST['location'];
// $calendar_id = $_POST['calendar_id'];
$current_date = date('Y-m-d H:i:s');
$data = [
'project_name' => $project_name,
'slot' => $slot,
'days' => $days,
// 'alert' => $alert,
'score_threshold' => $score_threshold,
'actual_score' => $actual_score,
'webhook' => $webhook,
'calendar' => $calendar_id,
'location' => $location,
'payload' => $webhook_payload,
];
// Insert data into the database using LicenseModel
$projectModel = new ProjectModel();
$projectModel->edit($data, $id);
// header('Location: /admin/project');
// echo 'done';
}
}, 'post');
Route::add('/client/duplicate', function () {
check_login();
$error = false;
// $data = [
// 'page_title' => 'Calendar'
// ];
// if (empty($_POST['project_name']) || empty($_POST['slot'])) {
// $error = true;
// include_once __DIR__ . '/layout/header/Adminleft_sidebar.php';
// include_once __DIR__ . '/projectAdd.php';
// include_once __DIR__ . '/layout/footer/Adminnone_footer.php';
// } else {
// Collect form data
$id = $_POST['project_id'];
$calendar_id = $_POST['calendar_id'];
$current_date = date('Y-m-d H:i:s');
$projectModel = new ProjectModel();
$model = $projectModel->get($id);
// echo $model;
// exit;
// Use regular expression to check if the variable ends with a number within brackets
if (preg_match('/\((\d+)\)$/', $model->project_name, $matches)) {
// Extract the number and increment it
$number = $matches[1] + 1;
// Replace the old number with the incremented number
$modifiedVariable = preg_replace('/\(\d+\)$/', "($number)", $model->project_name);
// echo $modifiedVariable;
// Remove content within parentheses
$modifiedVariable2 = preg_replace('/\(\d+\)/', '', $model->project_name);
} else {
// If no number within brackets at the end, append "(1)"
$modifiedVariable2 = $model->project_name;
// echo $modifiedVariable;
}
$model2 = $projectModel->get_like('project_name', $modifiedVariable2);
if (!empty($model2)) {
foreach ($model2 as $mod) {
// Use regular expression to check if the variable ends with a number within brackets
if (preg_match('/\((\d+)\)$/', $mod->project_name, $matches)) {
// Extract the number and increment it
$number = $matches[1] + 1;
// Replace the old number with the incremented number
$modifiedVariable = preg_replace('/\(\d+\)$/', "($number)", $mod->project_name);
// echo $modifiedVariable;
} else {
// If no number within brackets at the end, append "(1)"
$modifiedVariable = $mod->project_name . "(1)";
// echo $modifiedVariable;
}
// $modifiedVariable = $mod->project_name;
}
}
// echo json_encode($model2);
// exit;
$data = [
'project_name' => $modifiedVariable,
'slot' => $model->slot,
'days' => $model->days,
'alert' => $model->alert,
'score_threshold' => $model->score_threshold,
'actual_score' => $model->actual_score,
'webhook' => $model->webhook,
'location' => $model->location,
'payload' => $model->payload,
'calendar' => $calendar_id,
'created_at' => $current_date,
'user_id' => $_SESSION["user"]
];
// Insert data into the database using LicenseModel
$projectModel->create($data);
echo 'Project Duplicated';
// }
}, 'post');
Route::add('/client/project/delete/([0-9]+)', function ($id) {
check_login();
$projectModel = new ProjectModel();
$projectModel->real_delete($id);
header('Location: /client/project');
}, 'get');
Route::add('/client/project/list/multiselect', function () {
check_login();
$error = false;
$projectModel = new ProjectModel();
if (isset($_POST['delete'])) {
$ids = implode(', ', array_map('intval', $_POST['selected']));
$projectModel->real_delete_by_fields([
"id IN ($ids)"
]);
header('Location: /client/project');
}
if (isset($_POST['edit'])) {
$ids = implode(',', array_map('intval', $_POST['selected']));
$data = [
'page_title' => 'Project',
'ids' => "$ids"
];
include_once __DIR__ . '/layout/header/Clientleft_sidebar.php';
include_once __DIR__ . '/projectEditMulti.php';
include_once __DIR__ . '/layout/footer/Clientnone_footer.php';
exit;
}
if (isset($_POST['multiedit'])) {
if (empty($_POST['project_name']) || empty($_POST['slot']) || empty($_POST['days']) || empty($_POST['score_threshold']) || empty($_POST['actual_score']) || empty($_POST['webhook']) || empty($_POST['webhook_payload']) || empty($_POST['calendar_id']) || empty($_POST['location'])) {
$error = true;
$ids = implode(',', array_map('intval', $_POST['selected']));
$data = [
'page_title' => 'Project',
'ids' => "$ids"
];
include_once __DIR__ . '/layout/header/Clientleft_sidebar.php';
include_once __DIR__ . '/projectEditMulti.php';
include_once __DIR__ . '/layout/footer/Clientnone_footer.php';
exit;
} else {
// Collect form data
$project_name = $_POST['project_name'];
$slot = $_POST['slot'];
$days = $_POST['days'];
// $alert = $_POST['alert'];
$score_threshold = $_POST['score_threshold'];
$actual_score = $_POST['actual_score'];
$webhook = $_POST['webhook'];
$webhook_payload = $_POST['webhook_payload'];
$calendar_id = $_POST['calendar_id'];
$location = $_POST['location'];
// $calendar_id = $_POST['calendar_id'];
$current_date = date('Y-m-d H:i:s');
$data = [
'project_name' => $project_name,
'slot' => $slot,
'days' => $days,
// 'alert' => $alert,
'score_threshold' => $score_threshold,
'actual_score' => $actual_score,
'webhook' => $webhook,
'calendar' => $calendar_id,
'location' => $location,
'payload' => $webhook_payload,
];
$edit_ids = explode(",", $_POST['ids']);
foreach($edit_ids as $id) {
$projectModel = new ProjectModel();
$projectModel->edit($data, $id);
}
header('Location: /client/project');
exit;
}
}
}, 'post');
Route::add('/client/campaign', function () {
check_login();
$format = isset($_GET['format']) ? $_GET['format'] : 'json';
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$per_page = isset($_GET['size']) ? intval($_GET['size']) : 10;
$sort = isset($_GET['sort']) ? $_GET['sort'] : 'id';
$direction = isset($_GET['direction']) ? $_GET['direction'] : 'ASC';
// $relationship_num = isset($_GET['relationship_num']) ? $_GET['relationship_num'] : '';
$campaignModel = new CampaignModel();
$data = [
'page_title' => 'Campaign',
];
$where = [
"user_id" => $_SESSION['user']
];
// if ($relationship_num != '') {
// $where['relationship_num'] = '"' . $relationship_num . '"';
// }
$result = $campaignModel->get_paginated($page, $per_page, $where, 'id', 'DESC');
// echo json_encode($result);
if ($result) {
if ($format == 'json') {
$data = array_merge($data, $result);
}
}
include_once __DIR__ . '/layout/header/Clientleft_sidebar.php';
include_once __DIR__ . '/campaignListing.php';
include_once __DIR__ . '/layout/footer/Clientnone_footer.php';
}, 'get');
Route::add('/client/campaign/add', function () {
check_login();
$error = false;
$data = [
'page_title' => 'Campaign'
];
include_once __DIR__ . '/layout/header/Clientleft_sidebar.php';
include_once __DIR__ . '/campaignAdd.php';
include_once __DIR__ . '/layout/footer/Clientnone_footer.php';
}, 'get');
Route::add('/client/campaign/add', function () {
check_login();
$error = false;
$data = [
'page_title' => 'Campaign'
];
if (empty($_POST['name']) || empty($_POST['file_id'])) {
$error = true;
include_once __DIR__ . '/layout/header/Clientleft_sidebar.php';
include_once __DIR__ . '/campaignAdd.php';
include_once __DIR__ . '/layout/footer/Clientnone_footer.php';
} else {
// Collect form data
$name = $_POST['name'];
$file_id = $_POST['file_id'];
$current_date = date('Y-m-d H:i:s');
// }
$data = [
'name' => $name,
'file_id' => $file_id,
'user_id' => $_SESSION["user"],
'created_at' => $current_date,
];
// Insert data into the database using LicenseModel
$campaignModel = new CampaignModel();
$campaignModel->create($data);
// echo 'Campaign Added';
header('Location: /client/campaign');
}
}, 'post');
Route::add('/client/campaign/view/([0-9]+)', function ($id) {
check_login();
$campaignModel = new CampaignModel();
$campaign = $campaignModel->get($id);
if (!$campaign) {
header('Location: /client/campaign');
exit;
}
$config = MkdConfig::get_instance()->get_config();
$userModel = new UserModel();
$user = $userModel->get($_SESSION['user']);
if (!$user->drive_refresh_token) {
header('Location: /client/campaign?error=drive_not_connected');
exit;
}
$oauth = new \Lib\Google\GoogleOAuth2(
$config['google_client_id'],
$config['google_client_secret'],
$config['google_redirect_uri']
);
$oauth->setRefreshToken($user->drive_refresh_token);
$oauth->refreshAccessToken();
$drive = new \Lib\Google\GoogleDrive($oauth);
try {
// Download as CSV
$content = $drive->downloadFile(
$campaign->file_id,
'text/csv'
);
// Convert TSV/CSV to array of objects
$rows = array_map('str_getcsv', explode("\n", $content));
$headers = array_map(function($header) {
return str_replace(' ', '_', trim(strtolower($header)));
}, array_shift($rows));
$campaignData = array_map(function($row) use ($headers) {
return array_combine($headers, $row);
}, array_filter($rows));
$data = [
'page_title' => 'View Campaign',
'campaign' => $campaign,
'campaign_data' => $campaignData
];
include_once __DIR__ . '/layout/header/Clientleft_sidebar.php';
include_once __DIR__ . '/campaignView.php';
include_once __DIR__ . '/layout/footer/Clientnone_footer.php';
} catch (\Exception $e) {
print_r($e);
exit;
header('Location: /client/campaign?error=file_load_failed');
exit;
}
}, 'get');
// Add route to handle filter updates via AJAX
Route::add('/client/campaign/filter', function() {
check_login();
if (!isset($_POST['campaign_id'])) {
http_response_code(400);
echo json_encode(['error' => 'Missing campaign ID']);
exit;
}
$campaignModel = new CampaignModel();
$campaign = $campaignModel->get($_POST['campaign_id']);
if (!$campaign) {
http_response_code(404);
echo json_encode(['error' => 'Campaign not found']);
exit;
}
// Get the current filters
$filters = [
'campaign_name' => $_POST['campaign_name'] ?? null,
'ad_set_name' => $_POST['ad_set_name'] ?? null,
'ad_name' => $_POST['ad_name'] ?? null
];
// Get filtered data
$filteredData = $campaignModel->getFilteredData($campaign, $filters);
echo json_encode([
'data' => $filteredData
]);
}, 'post');
// Add edit route
Route::add('/client/campaign/edit/([0-9]+)', function ($id) {
check_login();
$campaignModel = new CampaignModel();
$campaign = $campaignModel->get($id);
if (!$campaign) {
header('Location: /client/campaign');
exit;
}
$data = [
'page_title' => 'Edit Campaign',
'campaign' => $campaign
];
include_once __DIR__ . '/layout/header/Clientleft_sidebar.php';
include_once __DIR__ . '/campaignEdit.php';
include_once __DIR__ . '/layout/footer/Clientnone_footer.php';
}, 'get');
Route::add('/client/campaign/edit/([0-9]+)', function ($id) {
check_login();
if (empty($_POST['name']) || empty($_POST['file_id'])) {
header('Location: /client/campaign/edit/' . $id);
exit;
}
$campaignModel = new CampaignModel();
$campaign = $campaignModel->get($id);
if (!$campaign) {
header('Location: /client/campaign');
exit;
}
$data = [
'name' => $_POST['name'],
'file_id' => $_POST['file_id']
];
$campaignModel->edit($data, $id);
header('Location: /client/campaign');
}, 'post');
// Add delete route
Route::add('/client/campaign/delete/([0-9]+)', function ($id) {
check_login();
$campaignModel = new CampaignModel();
$campaignModel->real_delete($id);
header('Location: /client/campaign');
}, 'get');
Route::add('/drive/disconnect', function() {
check_login();
$userModel = new UserModel();
$userModel->edit([
'drive_refresh_token' => null,
'drive_access_token' => null
], $_SESSION['user']);
header('Location: /' . $_SESSION['role'] . '/campaign');
exit;
}, 'post');