ElementLetter::getAddress_targets PHP Method

getAddress_targets() public method

public getAddress_targets ( )
    public function getAddress_targets()
    {
        if (Yii::app()->getController()->getAction()->id == 'create') {
            if (!($patient = Patient::model()->with(array('gp', 'practice'))->findByPk(@$_GET['patient_id']))) {
                throw new Exception('patient not found: ' . @$_GET['patient_id']);
            }
        } else {
            $patient = $this->event->episode->patient;
        }
        $options = array('Patient' . $patient->id => $patient->fullname . ' (Patient)');
        if (!isset($patient->contact->address)) {
            $options['Patient' . $patient->id] .= ' - NO ADDRESS';
        }
        if ($patient->gp) {
            if (@$patient->gp->contact) {
                $options['Gp' . $patient->gp_id] = $patient->gp->contact->fullname . ' (GP)';
            } else {
                $options['Gp' . $patient->gp_id] = Gp::UNKNOWN_NAME . ' (GP)';
            }
            if (!$patient->practice || !@$patient->practice->contact->address) {
                $options['Gp' . $patient->gp_id] .= ' - NO ADDRESS';
            }
        } else {
            if ($patient->practice) {
                $options['Practice' . $patient->practice_id] = Gp::UNKNOWN_NAME . ' (GP)';
                if (@$patient->practice->contact && !@$patient->practice->contact->address) {
                    $options['Practice' . $patient->practice_id] .= ' - NO ADDRESS';
                }
            }
        }
        // get the ids of the commissioning body types that should be shown as potential recipients to filter against
        $cbt_ids = array();
        foreach (OphCoCorrespondence_CommissioningBodyType_Recipient::model()->getCommissioningBodyTypes() as $cbt) {
            $cbt_ids[] = $cbt->id;
        }
        if ($cbs = $patient->getDistinctCommissioningBodiesByType()) {
            $criteria = new CDbCriteria();
            $criteria->addInCondition('id', array_keys($cbs));
            $cbtype_lookup = CHtml::listData(CommissioningBodyType::model()->findAll($criteria), 'id', 'name');
            foreach ($cbs as $cb_type_id => $cb_list) {
                foreach ($cb_list as $cb) {
                    if (in_array($cb_type_id, $cbt_ids)) {
                        $options['CommissioningBody' . $cb->id] = $cb->name . ' (' . $cbtype_lookup[$cb_type_id] . ')';
                        if (!$cb->getAddress()) {
                            $options['CommissioningBody' . $cb->id] .= ' - NO ADDRESS';
                        }
                    }
                    // include all services at the moment, regardless of whether the commissioning body type is filtered
                    if ($services = $cb->services) {
                        foreach ($services as $svc) {
                            $options['CommissioningBodyService' . $svc->id] = $svc->name . ' (' . $svc->getTypeShortName() . ')';
                        }
                    }
                }
            }
        }
        foreach (PatientContactAssignment::model()->with(array('contact' => array('with' => array('address')), 'location' => array('with' => array('contact' => array('alias' => 'contact2', 'with' => array('label'))))))->findAll('patient_id=?', array($patient->id)) as $pca) {
            if ($pca->location) {
                $options['ContactLocation' . $pca->location_id] = $pca->location->contact->fullName . ' (' . $pca->location->contact->label->name . ', ' . $pca->location . ')';
            } else {
                // Note that this index will always be the basis for a Person model search - if PCA has a wider use case than this,
                // this will need to be revisited
                $options['Contact' . $pca->contact_id] = $pca->contact->fullName . ' (' . $pca->contact->label->name;
                if ($pca->contact->address) {
                    $options['Contact' . $pca->contact_id] .= ', ' . $pca->contact->address->address1 . ')';
                } else {
                    $options['Contact' . $pca->contact_id] .= ') - NO ADDRESS';
                }
            }
        }
        asort($options);
        return $options;
    }