ElementLetter::setDefaultOptions PHP Method

setDefaultOptions() public method

public setDefaultOptions ( )
    public function setDefaultOptions()
    {
        if (Yii::app()->getController()->getAction()->id == 'create') {
            $this->site_id = Yii::app()->session['selected_site_id'];
            if (!($patient = Patient::model()->with(array('contact' => array('with' => array('address'))))->findByPk(@$_GET['patient_id']))) {
                throw new Exception('Patient not found: ' . @$_GET['patient_id']);
            }
            $this->re = $patient->first_name . ' ' . $patient->last_name;
            foreach (array('address1', 'address2', 'city', 'postcode') as $field) {
                if ($patient->contact->address && $patient->contact->address->{$field}) {
                    $this->re .= ', ' . $patient->contact->address->{$field};
                }
            }
            $this->re .= ', DOB: ' . $patient->NHSDate('dob') . ', Hosp No: ' . $patient->hos_num . ', NHS No: ' . $patient->nhsnum;
            $user = Yii::app()->session['user'];
            $firm = Firm::model()->with('serviceSubspecialtyAssignment')->findByPk(Yii::app()->session['selected_firm_id']);
            if ($contact = $user->contact) {
                $consultant = null;
                // only want to get consultant for medical firms
                if ($specialty = $firm->getSpecialty()) {
                    if ($specialty->medical) {
                        $consultant = $firm->consultant;
                    }
                }
                $this->footer = "Yours sincerely\n\n\n\n\n" . trim($contact->title . ' ' . $contact->first_name . ' ' . $contact->last_name . ' ' . $contact->qualifications) . "\n" . $user->role;
                if ($consultant && $consultant->id != $user->id) {
                    $this->footer .= "\nConsultant: {$consultant->contact->title} {$consultant->contact->first_name} {$consultant->contact->last_name}";
                }
                $ssa = $firm->serviceSubspecialtyAssignment;
            }
            // Look for a macro based on the episode_status
            if ($episode = $patient->getEpisodeForCurrentSubspecialty()) {
                if (!($this->macro = LetterMacro::model()->find('firm_id=? and episode_status_id=?', array($firm->id, $episode->episode_status_id)))) {
                    if ($firm->service_subspecialty_assignment_id) {
                        $subspecialty_id = $firm->serviceSubspecialtyAssignment->subspecialty_id;
                        if (!($this->macro = LetterMacro::model()->find('subspecialty_id=? and episode_status_id=?', array($subspecialty_id, $episode->episode_status_id)))) {
                            $this->macro = LetterMacro::model()->find('site_id=? and episode_status_id=?', array(Yii::app()->session['selected_site_id'], $episode->episode_status_id));
                        }
                    }
                }
            }
            if ($this->macro) {
                $this->populate_from_macro($patient);
            }
            if (Yii::app()->params['populate_clinic_date_from_last_examination'] && Yii::app()->findModule('OphCiExamination')) {
                if ($episode = $patient->getEpisodeForCurrentSubspecialty()) {
                    if ($event_type = EventType::model()->find('class_name=?', array('OphCiExamination'))) {
                        $criteria = new CDbCriteria();
                        $criteria->addCondition('event_type_id = ' . $event_type->id);
                        $criteria->addCondition('episode_id = ' . $episode->id);
                        $criteria->order = 'created_date desc';
                        $criteria->limit = 1;
                        if ($event = Event::model()->find($criteria)) {
                            $this->clinic_date = $event->created_date;
                        }
                    }
                }
            }
            if ($dl = FirmSiteSecretary::model()->find('firm_id=? and site_id=?', array(Yii::app()->session['selected_firm_id'], $this->site_id))) {
                $this->direct_line = $dl->direct_line;
                $this->fax = $dl->fax;
            }
        }
    }