67 lines
1.6 KiB
PHP
67 lines
1.6 KiB
PHP
<?php namespace App\Controllers;
|
|
|
|
use App\Libraries\GoogleSheetLib;
|
|
use CodeIgniter\Controller;
|
|
|
|
class GoogleSheetController extends Controller
|
|
{
|
|
protected GoogleSheetLib $sheetLib;
|
|
|
|
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';
|
|
}
|
|
|
|
/* ---------- 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);
|
|
}
|
|
}
|