Skip to content
Snippets Groups Projects
NbrGenObservations.php 3.93 KiB
<?php

use CRM_Nbrgeneralobservation_ExtensionUtil as E;

class CRM_Nbrgeneralobservation_Page_NbrGenObservations extends CRM_Core_Page {

  private $_currentData = [];

  public function run() {
    CRM_Utils_System::setTitle(E::ts('NbrGenObservations'));

    CRM_Core_Resources::singleton()->addScriptFile('nbrgeneralobservation', 'templates/CRM/Nbrgeneralobservation/utils.js');

    $tableName = CRM_Nihrbackbone_BackboneConfig::singleton()->getVolunteerGeneralObservationsCustomGroup('table_name');
    $columnName = CRM_Nihrbackbone_BackboneConfig::singleton()->getGeneralObservationCustomField('nvgo_bmi', 'column_name');

    Civi::log()->info("Table name is ".$tableName);
    Civi::log()->info("Column name is ".$columnName);
    $contactId = (int) CRM_Utils_Request::retrieveValue('cid', 'Integer');
    $this->assign('contactId', $contactId);
    $this->assign('editButton', $this->createEditButton($contactId));
    $this->assign('deleteButton', $this->createDeleteButton($contactId));
    $this->assign('observations', $this->getUserGenObs($contactId));
    parent::run();
  }

  private function getUserGenObs(int $contactId): array {
    $userObservation = [];
    // todo you could do this with API4, no need for additional methods?
    $observationData = CRM_Nbrgeneralobservation_BAO_NbrGenObservations::getWithId($contactId);
//    $userObservation[] = $this->assembleObsRow($observationData);

//    try {
//      $nbrGenObservationses = \Civi\Api4\NbrGenObservations::get()
//        ->addSelect('*')
//        ->addWhere('entity_id', '=', $contactId)
//        ->setLimit(1)
//        ->execute();
//      $observations = $nbrGenObservationses->first();
      // If observation matches assemble row, otherwise continue
      if($observationData){
        $userObservation = $this->assembleObsRow($observationData);
      }

//    }
//    catch (API_Exception $ex) {
//    }
    return $userObservation;
  }

  // todo Move this to utils -> in nihrbackbone, either in Utils or in service (nbrConfig)
  public static function yesOrNo($value) {
    if ($value !== NULL) {
      $value = ($value) ? "Yes" : "No";
    }
    return $value;
  }

  // Convert to readable format, might not even need to change most
  private function assembleObsRow(array $observations): array {

    $observations['nvgo_hand_preference'] = CRM_Nihrbackbone_Utils::getOptionValueLabel($observations['nvgo_hand_preference'], 'nbr_hand_preference');
    $observations['nvgo_ethnicity_id'] = CRM_Nihrbackbone_Utils::getOptionValueLabel($observations['nvgo_ethnicity_id'], 'nihr_ethnicity');
    $observations['nvgo_abo_group'] = CRM_Nihrbackbone_Utils::getOptionValueLabel($observations['nvgo_abo_group'], 'nbr_abo_group');
    $observations['nvgo_rhesus_factor'] = CRM_Nihrbackbone_Utils::getOptionValueLabel($observations['nvgo_rhesus_factor'], 'nbr_rhesus_factor');
    $observations['nvgo_probably_consanguineous'] = $this->yesOrNo($observations['nvgo_probably_consanguineous']);
    $observations['nvgo_proband'] = ($observations['nvgo_proband']) ? "Proband" : NULL;
    $observations['nvgo_family_history'] = $this->yesOrNo($observations['nvgo_family_history']);
    $observations['nvgo_school_id'] = CRM_Nbrgeneralobservation_BAO_NbrGenObservations::getSchoolLabel($observations['nvgo_school_id']);
    $observations['nvgo_bmi'] = round($observations['nvgo_bmi'], 2);
    return ($observations);
  }

  protected function createEditButton($contactId) {
    $updateUrl = CRM_Utils_System::url('civicrm/nbrgeneralobservation/form/nbrgenobservations', 'reset=1&action=update&cid=' . $contactId, TRUE);
    $editButton = '<a class="action-item button" title="Update" href="' . $updateUrl . '">' . E::ts('Edit general observations') . '</a>';
    return $editButton;
  }

  protected function createDeleteButton($contactId) {
    $deleteButton = '<a class="action-item button" id="deleteBtn" entity_id="' . $contactId . '" title="Delete"">' . E::ts('Delete general observations') . '</a>';
    return $deleteButton;
  }


}