public function actionEditContact()
 {
     if (!($patient = Patient::model()->findByPk(@$_POST['patient_id']))) {
         throw new Exception("Patient not found: " . @$_POST['patient_id']);
     }
     if (!($contact = Contact::model()->findByPk(@$_POST['contact_id']))) {
         throw new Exception("Contact not found: " . @$_POST['contact_id']);
     }
     if (@$_POST['site_id']) {
         if (!($site = Site::model()->findByPk(@$_POST['site_id']))) {
             throw new Exception("Site not found: " . @$_POST['site_id']);
         }
         if (!($cl = ContactLocation::model()->find('contact_id=? and site_id=?', array($contact->id, $site->id)))) {
             $cl = new ContactLocation();
             $cl->contact_id = $contact->id;
             $cl->site_id = $site->id;
             if (!$cl->save()) {
                 throw new Exception("Unable to save contact location: " . print_r($cl->getErrors(), true));
             }
         }
     } else {
         if (!($institution = Institution::model()->findByPk(@$_POST['institution_id']))) {
             throw new Exception("Institution not found: " . @$_POST['institution_id']);
         }
         if (!($cl = ContactLocation::model()->find('contact_id=? and institution_id=?', array($contact->id, $institution->id)))) {
             $cl = new ContactLocation();
             $cl->contact_id = $contact->id;
             $cl->institution_id = $institution->id;
             if (!$cl->save()) {
                 throw new Exception("Unable to save contact location: " . print_r($cl->getErrors(), true));
             }
         }
     }
     if (!($pca = PatientContactAssignment::model()->findByPk(@$_POST['pca_id']))) {
         throw new Exception("PCA not found: " . @$_POST['pca_id']);
     }
     $pca->location_id = $cl->id;
     if (!$pca->save()) {
         throw new Exception("Unable to save patient contact assignment: " . print_r($pca->getErrors(), true));
     }
     $this->redirect(array('/patient/view/' . $patient->id));
 }
 public function actionAddContactLocation()
 {
     if (!($contact = Contact::model()->findByPk(@$_GET['contact_id']))) {
         throw new Exception("Contact not found: " . @$_GET['contact_id']);
     }
     $errors = array();
     $sites = array();
     if (!empty($_POST)) {
         if (!($institution = Institution::model()->findByPk(@$_POST['institution_id']))) {
             $errors['institution_id'] = array("Please select an institution");
         } else {
             $sites = $institution->sites;
         }
         if (empty($errors)) {
             $cl = new ContactLocation();
             $cl->contact_id = $contact->id;
             if ($site = Site::model()->findByPk(@$_POST['site_id'])) {
                 $cl->site_id = $site->id;
             } else {
                 $cl->institution_id = $institution->id;
             }
             if (!$cl->save()) {
                 $errors = array_merge($errors, $cl->getErrors());
             } else {
                 Audit::add('admin-ContactLocation', 'add', $cl->id);
                 $this->redirect(array('/admin/editContact?contact_id=' . $contact->id));
             }
         }
     }
     $this->render('/admin/addcontactlocation', array('contact' => $contact, 'errors' => $errors, 'sites' => $sites));
 }