157 lines
4.4 KiB
PHP
157 lines
4.4 KiB
PHP
<?php namespace App\Controllers;
|
|
|
|
use App\Libraries\GoogleSheetLib;
|
|
use CodeIgniter\CLI\CLI;
|
|
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()
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* CLI: php public/index.php cli/list-sheet-folder-files [folderId]
|
|
*/
|
|
public function listFolderSheetFilesCli(string $folderId = '')
|
|
{
|
|
if (! is_cli()) {
|
|
return $this->response
|
|
->setStatusCode(405)
|
|
->setJSON(['status' => 'error', 'message' => 'CLI only route']);
|
|
}
|
|
|
|
$folderId = CLI::getOption('folderId') ?: $folderId;
|
|
$json = $this->sheetLib->listFolderSheetIdsAsJson($folderId);
|
|
file_put_contents('sheetid.json', $json);
|
|
CLI::write($json);
|
|
|
|
return;
|
|
}
|
|
}
|