Skip to content

Commit

Permalink
UPD: SADR validatinons
Browse files Browse the repository at this point in the history
  • Loading branch information
Itskiprotich committed Sep 27, 2024
1 parent e7bad3f commit c457df9
Show file tree
Hide file tree
Showing 35 changed files with 866 additions and 76 deletions.
1 change: 1 addition & 0 deletions config/cakepdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
'dpi' => 96,
'no-stop-slow-scripts' => true,
'enable-local-file-access' => true,
'--javascript-delay' =>5000
],
],
'margin' => [
Expand Down
2 changes: 1 addition & 1 deletion config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
*/
Router::defaultRouteClass(DashedRoute::class);

Router::extensions(['json', 'xml', 'csv']);
Router::extensions(['json', 'xml', 'csv','pdf']);
Router::prefix('api', function ($routes) {
$routes->setExtensions(['json', 'xml','pdf']);
$routes->resources('Sadrs', [
Expand Down
33 changes: 33 additions & 0 deletions src/Controller/Manager/CommentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace App\Controller\Manager;

use App\Controller\AppController;
use Cake\Filesystem\File;
use Cake\ORM\TableRegistry;
use Cake\Utility\Text;
use Cake\View\Helper\HtmlHelper;
Expand All @@ -16,6 +17,7 @@
*/
class CommentsController extends AppController
{

/**
* Index method
*
Expand Down Expand Up @@ -204,4 +206,35 @@ public function delete($id = null)

return $this->redirect(['action' => 'index']);
}

public function commentFileDownload($id = null)
{
$attachment = $this->Attachments->get($id);

if (!$attachment) {
$this->Flash->error(__('The requested file does not exist!'));
return $this->redirect($this->referer());
}

// Get the directory name and file name from the attachment entity
$directory = $attachment->dirname;
$fileName = $attachment->basename;

// Construct the full file path
$filePath = ROOT . DS . $directory . $fileName;

// Create a File object for the file
$file = new File($filePath);
if (!$file->exists()) {
$this->Flash->error(__('The requested file does not exist!'));
return $this->redirect($this->referer());
}

$response = $this->response->withFile(
$file->path,
['download' => true, 'name' => $attachment->basename]
);

return $response;
}
}
71 changes: 63 additions & 8 deletions src/Controller/Manager/SadrsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class SadrsController extends AppController
{


public $page_options = array('25' => '25', '50' => '50', '100' => '100');
/**
* Index method
Expand All @@ -24,26 +24,26 @@ class SadrsController extends AppController
*/

public function index()
{
{
$criteria = array();
$criteria['Sadrs.deleted'] = false;
$criteria['Sadrs.archived'] = false;
$criteria['Sadrs.submitted'] = 2;
$criteria['Sadrs.copied !='] = '1';
$limit = $this->request->getQuery('pages', 1000); // Default to 10 if 'pages' is not set

$this->paginate = [
'contain' => array('Users', 'Pqmps', 'Medications', 'Counties', 'SubCounties', 'Designations'),
'conditions' => $criteria
];
];
$this->paginate = [
'contain' => ['Users', 'Pqmps', 'Medications', 'Counties', 'SubCounties', 'Designations'],
'conditions' => $criteria,
'order' => ['Sadrs.created' => 'DESC'],
'limit'=>$limit
];
'limit' => $limit
];
$sadrs = $this->paginate($this->Sadrs->find('search', ['search' => $this->request->getQuery()]));

$this->set('sadrs', $sadrs);
$this->set('page_options', $this->page_options);
}
Expand All @@ -56,12 +56,67 @@ public function index()
* @return \Cake\Http\Response|null|void Renders view
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function download($id = null)
{
// Retrieve the Sadr record with all related data
$sadr = $this->Sadrs->get($id, [
'contain' => ['Users', 'Pqmps', 'ExternalComment' => ['Attachments'], 'Medications', 'Counties', 'Attachments', 'SubCounties', 'Designations', 'SadrDescriptions', 'SadrFollowups', 'SadrListOfDrugs' => ['Routes', 'Frequencies', 'Doses'], 'SadrListOfMedicines', 'SadrReaction'],
]);
// debug($sadr);
// exit;
// Set the Sadr data to be used in the template
$this->set(compact('sadr'));

// Define the filename for download
$filename = 'SADR_' . $sadr['id'] . '.xml';

// Render the template and set the response for download
$this->viewBuilder()->enableAutoLayout(false); // Disable the layout
$this->response = $this->response
->withType('xml') // Set the response content type to XML
->withDownload($filename); // Force the file download

return $this->render('download');
// $filename = 'SADR_' . $sadr['id'] . '.xml';
// $sadr = json_encode($sadr);

// // Set the response headers for file download
// $response = $this->response
// ->withType('xml') // Set the response content type to XML
// ->withStringBody($sadr) // Set the XML content as the body
// ->withDownload($filename); // Force the file download

// return $response;
}



public function view($id = null)
{
$sadr = $this->Sadrs->get($id, [
'contain' => ['Users', 'Pqmps', 'ExternalComment'=>['Attachments'], 'Medications', 'Counties', 'Attachments', 'SubCounties', 'Designations', 'SadrDescriptions', 'SadrFollowups', 'SadrListOfDrugs', 'SadrListOfMedicines', 'SadrReaction'],
'contain' => ['Users', 'Pqmps', 'ExternalComment' => ['Attachments'], 'Medications', 'Counties', 'Attachments', 'SubCounties', 'Designations', 'SadrDescriptions', 'SadrFollowups', 'SadrListOfDrugs' => [
'Routes',
'Frequencies',
'Doses'
], 'SadrListOfMedicines', 'SadrReaction'],
]);

if ($this->request->getParam('_ext') === 'pdf') {
// debug($sadr);
// exit;
$reference_no = $sadr['reference_no'];
// debug($reference_no);
$reference_no = str_replace('/', '_', $reference_no);
// debug($reference_no);
// exit;
$this->viewBuilder()->enableAutoLayout(false);
$this->viewBuilder()->setClassName('CakePdf.Pdf');
$this->viewBuilder()->setOptions([
'pdfConfig' => [
'filename' => $reference_no . '' . '.pdf'
]
]);
}
$this->set(compact('sadr'));
}

Expand Down
4 changes: 3 additions & 1 deletion src/Controller/Reporter/SadrsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public function index()
public function view($id = null)
{
$sadr = $this->Sadrs->get($id, [
'contain' => ['Users', 'Pqmps', 'ExternalComment' => ['Attachments'], 'Medications', 'Counties', 'Attachments', 'SubCounties', 'Designations', 'SadrDescriptions', 'SadrFollowups', 'SadrListOfDrugs', 'SadrListOfMedicines', 'SadrReaction'],
'contain' => ['Users', 'Pqmps', 'ExternalComment' => ['Attachments'], 'Medications', 'Counties', 'Attachments', 'SubCounties', 'Designations', 'SadrDescriptions', 'SadrFollowups', 'SadrListOfDrugs'=>[
'Routes','Frequencies','Doses'
], 'SadrListOfMedicines', 'SadrReaction'],
]);
// debug($sadr);
// exit;
Expand Down
61 changes: 9 additions & 52 deletions src/Model/Table/SadrListOfDrugsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,76 +78,33 @@ public function initialize(array $config): void
*/
public function validationDefault(Validator $validator): Validator
{
$validator
->integer('sadr_id')
->allowEmptyString('sadr_id');

$validator
->integer('sadr_followup_id')
->allowEmptyString('sadr_followup_id');

$validator
->integer('dose_id')
->allowEmptyString('dose_id');

$validator
->integer('route_id')
->allowEmptyString('route_id');

$validator
->integer('frequency_id')
->allowEmptyString('frequency_id');
->notEmptyString('drug_name', 'Please provide drug name');

$validator
->scalar('frequency_id_other')
->maxLength('frequency_id_other', 255)
->allowEmptyString('frequency_id_other');
->notEmptyString('brand_name', 'Please specify the brand name of the suspected drug');

$validator
->scalar('drug_name')
->maxLength('drug_name', 255)
->allowEmptyString('drug_name');
->notEmptyString('dose', 'Please specify the dosage')
->numeric('dose', 'dose must be a numeric value.'); // Enforce numeric validation if input is provided

$validator
->scalar('brand_name')
->maxLength('brand_name', 255)
->allowEmptyString('brand_name');
->notEmptyString('dose_id', 'Please specify the dose units');

$validator
->scalar('batch_no')
->maxLength('batch_no', 255)
->allowEmptyString('batch_no');
->notEmptyString('route_id', 'Please specify the route');

$validator
->scalar('manufacturer')
->maxLength('manufacturer', 255)
->allowEmptyString('manufacturer');
->notEmptyString('frequency_id', 'Please specify the frequency');

$validator
->scalar('dose')
->maxLength('dose', 100)
->allowEmptyString('dose');
->notEmptyString('start_date', 'Please specify the start date for the suspected drug');

$validator
->date('start_date')
->allowEmptyDate('start_date');

$validator
->date('stop_date')
->allowEmptyDate('stop_date');

$validator
->scalar('indication')
->maxLength('indication', 100)
->allowEmptyString('indication');

$validator
->scalar('suspected_drug')
->maxLength('suspected_drug', 100)
->allowEmptyString('suspected_drug');

return $validator;
}


/**
* Returns a rules checker object that will be used for validating
Expand Down
42 changes: 39 additions & 3 deletions src/Model/Table/SadrsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,6 @@ public function initialize(array $config): void
public function validationDefault(Validator $validator): Validator
{



// Set validation rules here *****

$validator
Expand Down Expand Up @@ -283,8 +281,41 @@ public function validationDefault(Validator $validator): Validator
return true; // If no date_of_birth, skip validation
},
'message' => 'Date of onset of reaction must be on or after date of birth.',
])
->add('date_of_onset_of_reaction', 'dateAfterStartDates', [
'rule' => function ($value, $context) {
// Check if sadr_list_of_drugs is present and loop through it
if (!empty($context['data']['sadr_list_of_drugs'])) {
foreach ($context['data']['sadr_list_of_drugs'] as $drug) {
// Check if the drug is suspected and has a start_date
if (!empty($drug['start_date']) && !empty($drug['suspected_drug']) && $drug['suspected_drug'] == 1) {
// Compare the date_of_onset_of_reaction with start_date
if (strtotime($value) <= strtotime($drug['start_date'])) {
return false; // Validation fails if date is not after start_date
}
}
}
}
return true; // Validation passes if all checks are satisfied
},
'message' => 'The date of onset of the reaction must come after the start date of the suspected drug(s).',
]);

$validator ->add('sadr_list_of_drugs', 'atLeastOneSuspectedDrug', [
'rule' => function ($value, $context) {
// Check if sadr_list_of_drugs is present and loop through it
if (!empty($context['data']['sadr_list_of_drugs'])) {
foreach ($context['data']['sadr_list_of_drugs'] as $drug) {
// Check if at least one drug is suspected
if (!empty($drug['suspected_drug']) && $drug['suspected_drug'] == 1) {
return true; // Validation passes if at least one suspected drug is found
}
}
}
return false; // Validation fails if no suspected drugs are found
},
'message' => 'At least one drug must be marked as suspected.',
]);


$validator
->scalar('description_of_reaction')
Expand All @@ -306,6 +337,8 @@ public function validationDefault(Validator $validator): Validator
$validator
->scalar('outcome')
->notEmptyString('outcome', 'Please specify outcome');


$validator
->scalar('reporter_name')
->notEmptyString('reporter_name', 'Please provide reporter name');
Expand All @@ -324,6 +357,9 @@ public function validationDefault(Validator $validator): Validator
->scalar('weight')
->allowEmptyString('weight') // Allow empty weight
->numeric('weight', 'Weight must be a numeric value.'); // Enforce numeric validation if input is provided



$validator
->scalar('height')
->allowEmptyString('height') // Allow empty height
Expand Down
Loading

0 comments on commit c457df9

Please sign in to comment.