public function actionEditCommissioningBodyService()
 {
     $address = new Address();
     $address->country_id = 1;
     if (isset($_GET['commissioning_body_service_id'])) {
         if (!($cbs = CommissioningBodyService::model()->findByPk(@$_GET['commissioning_body_service_id']))) {
             throw new Exception("CommissioningBody not found: " . @$_GET['commissioning_body_service_id']);
         }
         if ($cbs->contact && $cbs->contact->address) {
             $address = $cbs->contact->address;
         }
     } else {
         $cbs = new CommissioningBodyService();
     }
     $errors = array();
     if (!empty($_POST)) {
         $cbs->attributes = $_POST['CommissioningBodyService'];
         if (!$cbs->validate()) {
             $errors = $cbs->getErrors();
         }
         $address->attributes = $_POST['Address'];
         if (!$address->validate()) {
             $errors = array_merge($errors, $address->getErrors());
         }
         if (empty($errors)) {
             if (!$address->id) {
                 $contact = new Contact();
                 if (!$contact->save()) {
                     throw new Exception("Unable to save contact: " . print_r($contact->getErrors(), true));
                 }
                 $cbs->contact_id = $contact->id;
                 $address->contact_id = $contact->id;
             }
             $method = $cbs->id ? 'edit' : 'add';
             $audit = $_POST;
             if ($method == 'edit') {
                 $audit['id'] = $cbs->id;
             }
             if (!$cbs->save()) {
                 throw new Exception("Unable to save CommissioningBodyService: " . print_r($cbs->getErrors(), true));
             }
             if (!$address->save()) {
                 throw new Exception("Unable to save CommissioningBodyService address: " . print_r($address->getErrors(), true));
             }
             Audit::add('admin-CommissioningBodyService', $method, $cbs->id);
             $this->redirect('/admin/commissioning_body_services');
         }
     }
     $this->render('/admin/editCommissioningBodyService', array('cbs' => $cbs, 'address' => $address, 'errors' => $errors));
 }
示例#2
0
 public function actionEditCommissioningBodyService()
 {
     $address = new Address();
     $contact = new Contact();
     $address->country_id = 1;
     // to allow the commissioning body type list to be filtered
     $commissioning_bt = null;
     $commissioning_bst = null;
     if ($cbs_id = $this->getApp()->request->getQuery('commissioning_body_service_id')) {
         if (!($cbs = CommissioningBodyService::model()->findByPk($cbs_id))) {
             throw new Exception('CommissioningBody not found: ' . $cbs_id);
         }
         if ($cbs->contact) {
             $contact = $cbs->contact;
             if ($cbs->contact->address) {
                 $address = $cbs->contact->address;
             }
         }
     } else {
         $cbs = new CommissioningBodyService();
         if ($commissioning_bt_id = Yii::app()->request->getQuery('commissioning_body_type_id')) {
             if (!($commissioning_bt = CommissioningBodyType::model()->findByPk($commissioning_bt_id))) {
                 throw new CHttpException(404, 'Unrecognised Commissioning Body Type ID');
             }
         }
         if ($service_type_id = Yii::app()->request->getQuery('service_type_id')) {
             if (!($commissioning_bst = CommissioningBodyServiceType::model()->findByPk($service_type_id))) {
                 throw new CHttpException(404, 'Unrecognised Service Type ID');
             }
             $cbs->setAttribute('commissioning_body_service_type_id', $service_type_id);
         }
     }
     $errors = array();
     if (!($return_url = Yii::app()->request->getQuery('return_url'))) {
         $return_url = '/admin/commissioning_body_services';
     }
     if (!empty($_POST)) {
         $cbs->attributes = $_POST['CommissioningBodyService'];
         if (!$cbs->validate()) {
             $errors = $cbs->getErrors();
         }
         $contact->attributes = $_POST['Contact'];
         if (!$contact->validate()) {
             $errors = array_merge($errors, $contact->getErrors());
         }
         $address->attributes = $_POST['Address'];
         if (!$address->validate()) {
             $errors = array_merge($errors, $address->getErrors());
         }
         if (empty($errors)) {
             $transaction = Yii::app()->db->beginInternalTransaction();
             try {
                 if (!$contact->save()) {
                     throw new Exception('Unable to save contact: ' . print_r($contact->getErrors(), true));
                 }
                 if (!$address->id) {
                     $cbs->contact_id = $contact->id;
                     $address->contact_id = $contact->id;
                 }
                 $method = $cbs->id ? 'edit' : 'add';
                 if (!$cbs->save()) {
                     throw new Exception('Unable to save CommissioningBodyService: ' . print_r($cbs->getErrors(), true));
                 }
                 if (!$address->save()) {
                     throw new Exception('Unable to save CommissioningBodyService address: ' . print_r($address->getErrors(), true));
                 }
                 Audit::add('admin-CommissioningBodyService', $method, $cbs->id);
                 $transaction->commit();
             } catch (Exception $e) {
                 $transaction->rollback();
                 throw $e;
             }
             $this->redirect($return_url);
         }
     }
     $this->render('//admin/editCommissioningBodyService', array('commissioning_bt' => $commissioning_bt, 'commissioning_bst' => $commissioning_bst, 'cbs' => $cbs, 'address' => $address, 'errors' => $errors, 'return_url' => $return_url));
 }