Skip to content

Commit

Permalink
UPD: padrs
Browse files Browse the repository at this point in the history
  • Loading branch information
Itskiprotich committed Oct 28, 2024
1 parent 5494f31 commit 5d2b402
Show file tree
Hide file tree
Showing 15 changed files with 806 additions and 272 deletions.
19 changes: 19 additions & 0 deletions collection/padr.rest
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

#API:::: Create a PADR report
POST http://localhost:8765/api/padrs/add
Accept: application/json
Content-Type: application/json

{
"reporter_name":"Japheth Kiprotich",
"county_id":27,
"sub_county_id":89,
"patient_name":"Patient 254",
"reporter_email":"[email protected]",
"reporter_phone":"",
"report_sadr":"Side Effects",
"outcome":"recovering/resolving",
"consent":"No"

}

22 changes: 19 additions & 3 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ public function bootstrap(): void
*/
public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
{

$csrf = new CsrfProtectionMiddleware([
'httpOnly' => true,
]);

// Disable CSRF for API routes
$csrf->skipCheckCallback(function ($request) {
return $request->getParam('prefix') === 'Api';
});



$middlewareQueue
// Catch any exceptions in the lower layers,
// and make an error page/response
Expand All @@ -110,9 +122,13 @@ public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue

// Cross Site Request Forgery (CSRF) Protection Middleware
// https://book.cakephp.org/4/en/security/csrf.html#cross-site-request-forgery-csrf-middleware
->add(new CsrfProtectionMiddleware([
'httponly' => true,
]));
// ->add(new CsrfProtectionMiddleware([
// 'httponly' => true,
// ]));
->add($csrf);




return $middlewareQueue;
}
Expand Down
179 changes: 179 additions & 0 deletions src/Controller/Api/PadrsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
<?php

declare(strict_types=1);

namespace App\Controller\Api;

use App\Controller\AppController;
use Cake\Event\EventInterface;
use Cake\I18n\FrozenTime;
use Cake\Utility\Security;

/**
* Padrs Controller
*
* @property \App\Model\Table\PadrsTable $Padrs
* @method \App\Model\Entity\Padr[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
*/
class PadrsController extends AppController
{

public function initialize(): void
{
parent::initialize();
$this->loadComponent('Paginator');
$this->Auth->allow('add');
}
public function beforeFilter(EventInterface $event): void
{
parent::beforeFilter($event);
$this->Auth->allow([
'add'
]);
}
/**
* Index method
*
* @return \Cake\Http\Response|null|void Renders view
*/
public function index()
{
$this->paginate = [
'contain' => ['Users', 'Counties', 'SubCounties', 'Designations'],
];
$padrs = $this->paginate($this->Padrs);

$this->set(compact('padrs'));
}

/**
* View method
*
* @param string|null $id Padr id.
* @return \Cake\Http\Response|null|void Renders view
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function view($id = null)
{
$padr = $this->Padrs->get($id, [
'contain' => ['Users', 'Counties', 'SubCounties', 'Designations', 'Padrs', 'PadrListOfMedicines', 'Attachments'],
]);

$this->set(compact('padr'));
}

/**
* Add method
*
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
*/
public function add()
{
$padr = $this->Padrs->newEmptyEntity();
if ($this->request->is('post')) {
$padr = $this->Padrs->patchEntity($padr, $this->request->getData(), [
'validate'=>true,
'associated' => ['PadrListOfMedicines', 'Attachments']
]);
if ($this->Padrs->save($padr,['validate'=>true])) {


$token = Security::hash(strval($padr['id']));
$startDate = new FrozenTime(date("Y-01-01 00:00:00"));
$endDate = new FrozenTime(date("Y-m-d H:i:s"));

$count = $this->Padrs->find()
->where([
'Padrs.submitted_date BETWEEN :start AND :end'
])
->bind(':start', $startDate->format('Y-m-d H:i:s'), 'datetime')
->bind(':end', $endDate->format('Y-m-d H:i:s'), 'datetime')
->count();

$count++;
$count = ($count < 10) ? "0$count" : $count;

$token = Security::hash(strval($padr['id']));
$dataTable = $this->getTableLocator()->get('padrs');
// Update the field using the query builder
$dataTable->query()
->update()
->set([
'token' => $token,
'submitted_date' => date("Y-m-d H:i:s"),
'reference_no' => 'PADR/' . date('Y') . '/' . $count
])
->where(['id' => $padr['id']])
->execute();
$response = [
'status' => 'success',
'message' => __('The padr has been saved.'),
'data' => $padr
];
} else {
$response = [
'status' => 'error',
'message' => __('The padr could not be saved. Please, try again.'),
'errors' => $padr->getErrors()
];
}
} else {
$response = [
'status' => 'error',
'message' => __('Invalid request method.')
];
}
$this->set([
'response' => $response,
'_serialize' => ['response']
]);
}

/**
* Edit method
*
* @param string|null $id Padr id.
* @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function edit($id = null)
{
$padr = $this->Padrs->get($id, [
'contain' => [],
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$padr = $this->Padrs->patchEntity($padr, $this->request->getData());
if ($this->Padrs->save($padr)) {
$this->Flash->success(__('The padr has been saved.'));

return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The padr could not be saved. Please, try again.'));
}
$users = $this->Padrs->Users->find('list', ['limit' => 200])->all();
$counties = $this->Padrs->Counties->find('list', ['limit' => 200])->all();
$subCounties = $this->Padrs->SubCounties->find('list', ['limit' => 200])->all();
$designations = $this->Padrs->Designations->find('list', ['limit' => 200])->all();
$this->set(compact('padr', 'users', 'counties', 'subCounties', 'designations'));
}

/**
* Delete method
*
* @param string|null $id Padr id.
* @return \Cake\Http\Response|null|void Redirects to index.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$padr = $this->Padrs->get($id);
if ($this->Padrs->delete($padr)) {
$this->Flash->success(__('The padr has been deleted.'));
} else {
$this->Flash->error(__('The padr could not be deleted. Please, try again.'));
}

return $this->redirect(['action' => 'index']);
}
}
13 changes: 4 additions & 9 deletions src/Controller/Manager/ReportsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ class ReportsController extends AppController
*/
public function index()
{

$reports =[];

$this->set(compact('reports'));
debug("Sample");
exit;
}

/**
Expand All @@ -33,11 +31,8 @@ public function index()
*/
public function view($id = null)
{
$report = $this->Reports->get($id, [
'contain' => [],
]);

$this->set(compact('report'));
debug("Sample");
exit;
}

/**
Expand Down
11 changes: 9 additions & 2 deletions src/Controller/PadrsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function index()
$this->set(compact('padrs'));
}

/**
/**
* View method
*
* @param string|null $id Padr id.
Expand Down Expand Up @@ -140,14 +140,21 @@ public function followup($token = null)
*/
public function add()
{


$this->Padrs->addBehavior('Captcha.Captcha');
$padr = $this->Padrs->newEmptyEntity();
if ($this->request->is('post')) {


$padr = $this->Padrs->patchEntity($padr, $this->request->getData(), [
'validate'=>true,
'associated' => ['PadrListOfMedicines', 'Attachments']
]);
if ($this->Padrs->save($padr)) {

// debug($padr);
// exit;
if ($this->Padrs->save($padr,['validate'=>true])) {


$startDate = new FrozenTime(date("Y-01-01 00:00:00"));
Expand Down
Loading

0 comments on commit 5d2b402

Please sign in to comment.