nhance/app/Controllers/GoogleSheetController.php
2026-02-26 12:28:23 +05:30

137 lines
3.8 KiB
PHP

<?php namespace App\Controllers;
use App\Libraries\GoogleSheetLib;
use CodeIgniter\Controller;
class GoogleSheetController extends Controller
{
protected GoogleSheetLib $sheetLib;
public $config = [
'permissions' => [
'editors' => [
'vitvelz@gmail.com', 'velz1990@gmail.com','venkateshraman786@gmail.com'
// 'group:rfq-editors@company.com'
],
'viewers' => [],
],
'protections' => [
[
'range' => 'RFQ Page!B12:C12',
'users' => [ //allowed users to edit this cell range
'velz1990@gmail.com','vitvelz@gmail.com','firebase-adminsdk-mcdfe@nhance-ee8d1.iam.gserviceaccount.com'
],
'groups' => []
],
[
'range' => 'Claims Page!A1',
'users' => [
'velz1990@gmail.com','venkateshraman786@gmail.com','firebase-adminsdk-mcdfe@nhance-ee8d1.iam.gserviceaccount.com'
],
'groups' => []
]
],
'parentFolderId' => '19uySI-PSFQfFpZvMSnBtirXbwfCTXFBL', // RFQ folder
];
public function __construct()
{
$this->sheetLib = new GoogleSheetLib();
}
/* ---------- UI ---------- */
public function editor(string $sheetId)
{
return view('gsheet_editor', [
'sheetId' => $sheetId
]);
}
/* ---------- FETCH ---------- */
public function fetch(string $sheetId)
{
$data = $this->sheetLib->read($sheetId);
// print_rr($data);die;
return $this->response->setJSON($data);
// echo 'Hi';
}
public function sample(string $sheetId)
{
echo $sheetId;
}
/* ---------- SAVE ---------- */
public function save(string $sheetId)
{
$rows = $this->request->getJSON(true);
if (!is_array($rows)) {
return $this->response
->setStatusCode(400)
->setJSON(['error' => 'Invalid data']);
}
$this->sheetLib->write($sheetId, $rows);
return $this->response->setJSON([
'status' => 'success'
]);
}
/* ---------- DOWNLOAD ---------- */
public function download(string $sheetId)
{
$content = $this->sheetLib->downloadExcel($sheetId);
return $this->response
->setHeader(
'Content-Type',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
)
->setHeader(
'Content-Disposition',
'attachment; filename="sheet.xlsx"'
)
->setBody($content);
}
public function create()
{
try {
$config = config('RfqConfig');
// #$lib = new RfqGoogleSheetLib();
// $templateId = $this->request->getPost('templateId');
// if (!$templateId) {
// throw new \Exception('Template ID missing');
// }
// $newSheetId = $this->sheetLib->copyTemplate(
// $templateId,
// 'RFQ_' . date('Ymd_His'),
// $this->config['parentFolderId']
// );
$newSheetId = '1GXNDNXoWriClb5HCqPYd2GAY1T8aie_Hos0yAOoTaC0';
// $this->sheetLib->applyPermissions($newSheetId, $this->config['permissions']);
$this->sheetLib->applyProtections($newSheetId, $this->config['protections']);
return $this->response->setJSON([
'status' => 'success',
'url' => $this->sheetLib->sheetUrl($newSheetId)
]);
} catch (\Throwable $e) {
return $this->response
->setStatusCode(500)
->setJSON([
'status' => 'error',
'message' => $e->getMessage()
]);
}
}
}