Skip to content
Snippets Groups Projects
NbrGenObservations.php 3.44 KiB
Newer Older
aly2191's avatar
aly2191 committed
<?php

use CRM_Nbrgeneralobservation_ExtensionUtil as E;
aly2191's avatar
aly2191 committed

class CRM_Nbrgeneralobservation_Page_NbrGenObservations extends CRM_Core_Page {
  private $_currentData = [];
aly2191's avatar
aly2191 committed

  public function run() {
    CRM_Utils_System::setTitle(E::ts('NbrGenObservations'));
ErikHommel's avatar
ErikHommel committed
    // todo I do not think you want to go back to barcodeprint :-) And you do not need a push user context here, it will go back to contact summary
    $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));
aly2191's avatar
aly2191 committed
    parent::run();
  }

ErikHommel's avatar
ErikHommel committed
  private function getUserGenObs(int $contactId): array {
aly2191's avatar
aly2191 committed
    $userObservation = [];
ErikHommel's avatar
ErikHommel committed
    // 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();
      $userObservation = $this->assembleObsRow($observations);
    }
    catch (API_Exception $ex) {
    }
aly2191's avatar
aly2191 committed
    return $userObservation;
  }
ErikHommel's avatar
ErikHommel committed
  // 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;
  }
aly2191's avatar
aly2191 committed

  // Convert to readable format, might not even need to change most
ErikHommel's avatar
ErikHommel committed
  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['show_on_portal_1015'] = $this->yesOrNo($observations['show_on_portal_1015']);
    $observations['nvgo_bmi'] = round($observations['nvgo_bmi'], 2);
    return ($observations);
aly2191's avatar
aly2191 committed
  }

  protected function createEditButton($entityId) {
    $updateUrl = CRM_Utils_System::url('civicrm/nbrgeneralobservation/form/nbrgenobservations', 'reset=1&action=update&cid=' . $entityId, TRUE);
    $editButton = '<a class="action-item button" title="Update" href="' . $updateUrl . '">' . E::ts('Edit general observations') . '</a>';
aly2191's avatar
aly2191 committed
    return $editButton;
  }
  protected function createDeleteButton($entityId) {
    $deleteButton = '<a class="action-item button" id="deleteBtn" entity_id="' . $entityId . '" title="Delete"">' . E::ts('Delete general observations') . '</a>';
aly2191's avatar
aly2191 committed

}