public function savepagedata() { // Get JSON POST data $json =…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
public function save_page_data() { // Get JSON POST data data = json_decode($json, true);
// Set response headers
header('Content-Type: application/json');
// Validate input data - now including new required fields
if (
!isset($data['app_slug']) ||
!isset($data['pageData']) ||
!isset($data['configFile']) ||
!isset($data['globalCss'])
) {
echo json_encode([
'status' => 'error',
'message' => 'Invalid input data format. Missing required fields.'
]);
return;
}
$app_slug = $data['app_slug'];
$response = [
'status' => 'success',
'message' => 'Pages saved successfully',
'app_slug' => $app_slug,
'details' => []
];
// Create app directory if it doesn't exist
$dir = FCPATH . 'uploads/apps/' . $this->active_app->id;
if (!file_exists($dir)) {
mkdir($dir, 0777, true);
}
// Save config.js
$config_path = $dir . '/config.js';
try {
if (file_put_contents($config_path, $data['configFile']) !== false) {
$response['details'][] = [
'file' => 'config.js',
'status' => 'success',
'message' => 'Config file saved'
];
} else {
throw new Exception('Failed to write config file');
}
} catch (Exception $e) {
$response['details'][] = [
'file' => 'config.js',
'status' => 'error',
'message' => $e->getMessage()
];
}
// Save global.css
$css_path = $dir . '/global.css';
try {
if (file_put_contents($css_path, $data['globalCss']) !== false) {
$response['details'][] = [
'file' => 'global.css',
'status' => 'success',
'message' => 'Global CSS saved'
];
} else {
throw new Exception('Failed to write CSS file');
}
} catch (Exception $e) {
$response['details'][] = [
'file' => 'global.css',
'status' => 'error',
'message' => $e->getMessage()
];
}
// Process each page
$project_data = [];
foreach ($data['pageData'] as $key => $page) {
if (!isset($page['page_name'])) {
$response['details'][] = [
'page_name' => 'unknown',
'status' => 'error',
'message' => 'Missing page name'
];
continue;
}
$project_data[$key]['page_name'] = $page['page_name'];
// Save HTML file
if (isset($page['page_html'])) {
$html_filepath = $dir . '/' . $page['page_name'] . '.php';
try {
if (file_put_contents($html_filepath, $page['page_html']) !== false) {
$project_data[$key]['page_html'] = $html_filepath;
$response['details'][] = [
'page_name' => $page['page_name'],
'status' => 'success',
];
} else {
throw new Exception('Failed to write HTML file');
}
} catch (Exception $e) {
$response['details'][] = [
'page_name' => $page['page_name'],
'status' => 'error',
'message' => $e->getMessage()
];
}
}
}
// Save the root-level projectData as a single file
$project_data_filepath = null;
if (isset($data['projectData'])) {
$project_data_filepath = $dir . '/project_data.json';
try {
if (file_put_contents($project_data_filepath, json_encode($data['projectData'], JSON_PRETTY_PRINT)) !== false) {
$response['project_data_file'] = 'uploads/apps/' . $this->active_app->id . '/project_data.json';
} else {
throw new Exception('Failed to write project data file');
}
} catch (Exception $e) {
$response['status'] = 'error';
$response['message'] = 'Project data save failed: ' . $e->getMessage();
}
}
// Check for any errors in the entire process
$hasErrors = false;
foreach ($response['details'] as $detail) {
if ($detail['status'] === 'error') {
$hasErrors = true;
break;
}
}
if ($hasErrors) {
$response['status'] = 'partial';
$response['message'] = 'Some files failed to save';
}
// Save homepage to database (default to 'home' if not specified)
$this->db->set('site_url', base_url() . 'uploads/apps/' . $this->active_app->id . '/index.php');
$this->db->set('project_data_file', $project_data_filepath ? 'uploads/apps/' . $this->active_app->id . '/project_data.json' : null);
$this->db->set('project_data', json_encode($project_data));
$this->db->where('ids', $this->active_app->ids);
if (!$this->db->update('apps')) {
$response['status'] = 'error';
$response['message'] = 'Database update failed: ' . $this->db->error()['message'];
}
echo json_encode($response);
}
some pages failed to save when this error comes
Looking at your code, I can see why you're getting the "some pages failed to save" error. The issue is likely in the file writing process. Let me identify the key problems:
The function sets $response['status'] = 'partial' if any file fails to save, which triggers your "some pages failed to save" error message.
The most likely causes of failure are:
There's no explicit error handling for the project_data.json file save operation that updates the response details array.
The database update at the end could also be failing, but the error message would be different.
To fix this, I recommend:
// Add better error logging
try {
if (file_put_contents($html_filepath, $page['page_html']) !== false) {
$project_data[$key]['page_html'] = $html_filepath;
$response['details'][] = [
'page_name' => $page['page_name'],
'status' => 'success',
'message' => 'HTML file saved successfully'
];
} else {
throw new Exception('Failed to write HTML file: ' . error_get_last()['message']);
}
} catch (Exception $e) {
$response['details'][] = [
'page_name' => $page['page_name'],
'status' => 'error',
'message' => $e->getMessage()
];
}
Also check directory permissions and make sure the web server user has write access to the uploads directory.
Would you like me to explain more about any specific part of this code or suggest additional debugging steps?
access_token null
app_slug "gourmetgri"
config \n \n const $ = (selector) => document.querySelector(selector);\n const $all = (selector) => Array.from(document.querySelectorAll(selector));\n\n function showToast(type, text) {\n let gradient =\n type === "success"\n ? "linear-gradient(to right, #00b09b, #96c93d)"\n : type === "error"\n ? "linear-gradient(to right, #ff0000, #… console.warn('Could not override window.open:', error);\n }\n \n // Since we can't override location.assign/replace, we'll monitor for navigation\n }\n\n // Initialize URL modification when page loads\n if (document.readyState === 'loading') {\n document.addEventListener('DOMContentLoaded', setupRuntimeUrlModification);\n } else {\n setupRuntimeUrlModification();\n }\n
configFile const $ = (selector) => document.querySelector(selector);\n const $all = (selector) => Array.from(document.querySelectorAll(selector));\n\n function showToast(type, text) {\n let gradient =\n type === "success"\n ? "linear-gradient(to right, #00b09b, #96c93d)"\n : type === "error"\n ? "linear-gradient(to right, #ff0000, #ff5f6d)"\n …r) {\n console.warn('Could not override window.open:', error);\n }\n \n // Since we can't override location.assign/replace, we'll monitor for navigation\n }\n\n // Initialize URL modification when page loads\n if (document.readyState === 'loading') {\n document.addEventListener('DOMContentLoaded', setupRuntimeUrlModification);\n } else {\n setupRuntimeUrlModification();\n }
css '\n .cursor-pointer {\n cursor: pointer;\n }\n\n .toast-close {\n color: #fff !important;\n margin-left: 5px;\n opacity: 0.7;\n }\n\n body {\n font-family: "Roboto", sans-serif;\n min-width: 360px;\n max-width: 480px;\n margin: 0 auto;\n box-sizing: border-box;\n background-color: aliceblue;\n }\n\n .logou…translateX(-50%);\n }\n }\n\n @media (max-width: 576px) {\n .signup-container,\n .login-container {\n margin: auto;\n border-radius: 8px;\n box-shadow: 0 4px 8px rgba(0, 0, 0, 0.05);\n }\n \n .otp-input {\n width: 50px !important;\n height: 50px;\n font-size: 20px !important;\n }\n }\n '
globalCss '\n .cursor-pointer {\n cursor: pointer;\n }\n\n .toast-close {\n color: #fff !important;\n margin-left: 5px;\n opacity: 0.7;\n }\n\n body {\n font-family: "Roboto", sans-serif;\n min-width: 360px;\n max-width: 480px;\n margin: 0 auto;\n box-sizing: border-box;\n background-color: aliceblue;\n }\n\n .logou…translateX(-50%);\n }\n }\n\n @media (max-width: 576px) {\n .signup-container,\n .login-container {\n margin: auto;\n border-radius: 8px;\n box-shadow: 0 4px 8px rgba(0, 0, 0, 0.05);\n }\n \n .otp-input {\n width: 50px !important;\n height: 50px;\n font-size: 20px !important;\n }\n }\n '
pageData (10)[…]
projectData {…}
why this reqest data failed the requet
Looking at your request data, I can see a few potential issues that might be causing the save operation to fail:
The access_token is null, which might be causing authentication issues if your backend requires a valid token.
Your configFile and globalCss appear to be truncated in the middle (indicated by the "…" in the content), suggesting they might be too large or contain characters that are being improperly encoded.
The pageData is shown as an array with 10 items but the actual content isn't visible in the snippet you provided, so there might be issues with the structure or content of those pages.
To troubleshoot this:
Check if authentication is required - the null access_token might be causing permission issues.
Verify that your JSON isn't being truncated during transmission. Large files might need to be chunked or compressed.
Look for special characters or invalid syntax in your CSS and JS files that might be causing parsing errors.
Check your server logs for more detailed error messages that might not be returned in the response.
Try saving a minimal version with just one simple page to see if that works, then gradually add more content to identify what's causing the issue.
The most likely culprit is either an authentication issue or malformed/truncated data in one of your page files that's causing the server-side validation to fail.