<?php
namespace App\Controller;
use App\Controller\AppController;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;
/**
* Stores Controller
*
* @property \App\Model\Table\StoresTable $Stores
*
* @method \App\Model\Entity\Store[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
*/
class StoresController extends AppController
{
/**
* Index method
*
* @return \Cake\Http\Response|void
*/
public function index()
{
$stores = $this->paginate($this->Stores);
$this->set(compact('stores'));
}
/**
* View method
*
* @param string|null $id Store id.
* @return \Cake\Http\Response|void
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function view($id = null)
{
$store = $this->Stores->get($id, [
'contain' => ['Surveys', 'Users']
]);
$this->set('store', $store);
}
/**
* Add method
*
* @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise.
*/
public function add()
{
$store = $this->Stores->newEntity();
if ($this->request->is('post')) {
$store = $this->Stores->patchEntity($store, $this->request->getData());
if ($this->Stores->save($store)) {
$this->Flash->success(__('The store has been saved.'));
// $this->createExcelDoc($this->request->getData());
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The store could not be saved. Please, try again.'));
}
$surveys = $this->Stores->Surveys->find('list', ['limit' => 200]);
$users = $this->Stores->Users->find('list', ['limit' => 200]);
$this->set(compact('store', 'surveys', 'users'));
}
public function createExcelDoc($requestData)
{
$data = $requestData;
$inputFileName = WWW_ROOT . 'resources/survey.xlsx';
$spreadsheet = IOFactory::load($inputFileName);
$spreadsheet->setActiveSheetIndex(0)
->setCellValue('B10', $data['name'])
->setCellValue('B12', $data['address'] . ', ' . $data['city'] . ', ' . $data['state'] . ', ' . $data['zip'])
->setCellValue('B14', $data['rc_number'])
->setCellValue('B20', date("F d, Y") );
$writer = new Xlsx($spreadsheet);
$writer->save('survey.xlsx');
}
/**
* Edit method
*
* @param string|null $id Store id.
* @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise.
* @throws \Cake\Network\Exception\NotFoundException When record not found.
*/
public function edit($id = null)
{
$store = $this->Stores->get($id, [
'contain' => ['Surveys', 'Users']
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$store = $this->Stores->patchEntity($store, $this->request->getData());
if ($this->Stores->save($store)) {
$this->Flash->success(__('The store has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The store could not be saved. Please, try again.'));
}
$surveys = $this->Stores->Surveys->find('list', ['limit' => 200]);
$users = $this->Stores->Users->find('list', ['limit' => 200]);
$this->set(compact('store', 'surveys', 'users'));
}
/**
* Delete method
*
* @param string|null $id Store id.
* @return \Cake\Http\Response|null Redirects to index.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$store = $this->Stores->get($id);
if ($this->Stores->delete($store)) {
$this->Flash->success(__('The store has been deleted.'));
} else {
$this->Flash->error(__('The store could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
}