NbrGenObservations.php 7.04 KiB
<?php
use CRM_Nbrgeneralobservation_ExtensionUtil as E;
/**
* Form controller class
*
* @see https://docs.civicrm.org/dev/en/latest/framework/quickform/
*/
class CRM_Nbrgeneralobservation_Form_NbrGenObservations extends CRM_Core_Form {
public $_contactId;
private $_currentData = [];
public function preProcess() {
// Perform any setup tasks you may need
// Import utils.js
CRM_Core_Resources::singleton()->addScriptFile('nbrgeneralobservation', 'templates/CRM/Nbrgeneralobservation/utils.js');
// Get contact id
$contactId = CRM_Utils_Request::retrieve('cid', 'Integer');
$this->_contactId = $contactId;
if ($contactId) {
$this->_currentData = CRM_Nbrgeneralobservation_BAO_NbrGenObservations::getWithId($contactId);
}
$url = CRM_Utils_System::url("civicrm/nbrgeneralobservation/page/nbrgenobservations", ['cid' => $contactId], TRUE);
CRM_Core_Session::singleton()->pushUserContext($url);
$title = ts("Edit General Observations");
CRM_Utils_System::setTitle($title);
$this->assign('contactId', $contactId);
}
public function buildQuickForm() {
$this->add('hidden', 'entity_id');
$this->add('select', 'nvgo_school_id', E::ts('School list'),
CRM_Nbrschools_BAO_NbrSchool::getNbrSchoolValues(), FALSE,
['class' => 'crm-select2', 'placeholder' => ' - select school']);
$this->add('select', 'nvgo_ethnicity_id', E::ts('Ethnicity'),
CRM_Nihrbackbone_Utils::getOptionValueList('nihr_ethnicity'), FALSE,
['class' => 'crm-select2', 'placeholder' => ' - select ethnicity',]);
$this->add('number', 'nvgo_weight_kg', 'Weight (kg)', ["min"=>0], FALSE);
$this->add('number', 'nvgo_height_m', 'Height (m)', ["min"=>0, "step"=>0.1], FALSE);
$this->add('number', 'nvgo_bmi', 'BMI', ['disabled' => TRUE], FALSE);
$this->add('select', 'nvgo_hand_preference', E::ts('Hand Preference'),
CRM_Nihrbackbone_Utils::getOptionValueList('nbr_hand_preference'), FALSE,
['class' => 'crm-select2', 'placeholder' => ' - select hand']);
$this->add('select', 'nvgo_abo_group', E::ts('ABO group'),
CRM_Nihrbackbone_Utils::getOptionValueList('nbr_abo_group'), FALSE,
['class' => 'crm-select2', 'placeholder' => ' - select ABO group']);
$this->add('select', 'nvgo_rhesus_factor', E::ts('Rhesus factor'),
CRM_Nihrbackbone_Utils::getOptionValueList('nbr_rhesus_factor'), FALSE,
['class' => 'crm-select2', 'placeholder' => ' - select rhesus factor']);
$this->addYesNo(
'nvgo_probably_consanguineous',
'Probably consanguineous',
TRUE,
FALSE,
[]
);
$this->addYesNo(
'nvgo_family_history',
'Family history (close family members with same or similar conditions)',
TRUE,
FALSE,
[]
);
$this->add(
'checkbox', // field type
'nvgo_proband', // field name
'Proband', // field label(),
[1 => 'proband']
);
$this->addButtons([
[
'type' => 'submit',
'name' => E::ts("Submit"),
'isDefault' => TRUE,
],
]);
$this->add('number', 'weight_stone', "", ["min"=>0], FALSE);
$this->add('number', 'weight_lbs', "", ["min"=>0 ,"max"=>14], FALSE);
$this->add('number', 'height_ft', "", ["min"=>0,], FALSE);
$this->add('number', 'height_in', "", ["min"=>0,"max"=>13], FALSE);
// export form elements
$this->assign('elementNames', $this->getRenderableElementNames());
parent::buildQuickForm();
}
/** Form valiation rules */
public function addRules() {
$this->addFormRule([
'CRM_Nbrgeneralobservation_Form_NbrGenObservations',
'validateHeightAndWeight',
]);
}
public function validateHeightAndWeight($values) {
$errors = [];
// Greater than zero
$heightMin = 0;
$heightMax = 4;
$weightMin = 0;
$weightMax = 641;
if ($values['nvgo_height_m'] < $heightMin) {
$errors['nvgo_height_m'] = ts("Height must not be negative");
}
elseif ($values['nvgo_height_m'] > $heightMax) {
$errors['nvgo_height_m'] = ts("Height must not be higher than " . $heightMax . ".");
}
if ($values['nvgo_weight_kg'] < $weightMin) {
$errors['nvgo_weight_kg'] = ts("Weight must not be negative");
}
elseif ($values['nvgo_weight_kg'] > $weightMax) {
$errors['nvgo_weight_kg'] = ts("Weight must not be higher than " . $weightMax . " .");
}
if(is_nan($values['nvgo_bmi'])){
$errors['nvgo_bmi'] = ts("Bmi must be a number");
}
return empty($errors) ? TRUE : $errors;
}
/** Method to set defaults for edit - @return array|NULL|void */
public function setDefaultValues() {
$defaults = ['entity_id' => $this->_contactId];
if ($this->_action == CRM_Core_Action::UPDATE) {
foreach ($this->_currentData as $key => $value) {
$defaults[$key] = $value;
}
}
$weightHeightArr= self::calcWeightHeightArray($defaults['nvgo_weight_kg'], $defaults['nvgo_height_m']);
$defaults['height_ft']= $weightHeightArr['height_ft'];
$defaults['height_in']= $weightHeightArr['height_in'];
$defaults['weight_stone']= $weightHeightArr['weight_stone'];
$defaults['weight_lbs']= $weightHeightArr['weight_lbs'];
return $defaults;
}
public function postProcess() {
// I would use API4 calls here? And base it on the form action?
if ($this->_action == CRM_Core_Action::UPDATE) {
$values = $this->exportValues();
$contactId = $values['entity_id'];
$contactExists = CRM_Nbrgeneralobservation_BAO_NbrGenObservations::contactExists($contactId);
Civi::log()->info("contact exists is " . $contactExists);
if ($contactExists) {
CRM_Nbrgeneralobservation_BAO_NbrGenObservations::add($values);
CRM_Core_Session::setStatus(E::ts("Successfully updated observations."));
}
else {
CRM_Core_Session::setStatus(E::ts("No contact matching this id found"));
}
parent::postProcess();
}
}
function calcWeightHeightArray($weight_kg, $height_m){
$weight_tolbs = intval($weight_kg / 0.453592);
$weight_stone = intval($weight_tolbs / 14);
$weight_lbs = ($weight_tolbs / 14 - $weight_stone) * 14;
$height_toFt = $height_m * 3.28084;
$height_ft = intval($height_toFt);
$height_in = round(12 * ($height_toFt - $height_ft));
$weightHeightArr= [
"weight_stone"=> $weight_stone,
"weight_lbs"=>$weight_lbs,
"height_ft"=> $height_ft,
"height_in"=>$height_in
];
return $weightHeightArr;
}
/**
* Get the fields/elements defined in this form.
*
* @return array (string)
*/
public function getRenderableElementNames() {
// The _elements list includes some items which should not be
// auto-rendered in the loop -- such as "qfKey" and "buttons". These
// items don't have labels. We'll identify renderable by filtering on
// the 'label'.
$elementNames = [];
foreach ($this->_elements as $element) {
/** @var HTML_QuickForm_Element $element */
$label = $element->getLabel();
if (!empty($label)) {
$elementNames[] = $element->getName();
}
}
return $elementNames;
}
}