/**
  * Updates a particular model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id the ID of the model to be updated
  */
 public function actionUpdate($id)
 {
     $criteria = new CDbCriteria();
     $criteria->condition = 'customer_id=' . $id;
     $model = $this->loadModel($id);
     $model->is_allow_api = 1;
     $contact = Contact::model()->find('parent_model=:pm and parent_id=:pid', array(':pm' => 'Customer', ':pid' => $id));
     if (!$contact instanceof Contact) {
         $contact = new Contact();
     }
     $rekening = new CActiveDataProvider('CustomerRekening', array('criteria' => $criteria));
     $customer_contacts = new CArrayDataProvider($model->contacts, array('id' => 'contact', 'pagination' => array('pageSize' => 10)));
     $customer_tokens = new CActiveDataProvider('Token', array('criteria' => $criteria));
     $customer_shipping_profile = new CActiveDataProvider('CustomerShippingProfile', array('criteria' => $criteria));
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Customer']) && isset($_POST['Contact'])) {
         $trans = Yii::app()->db->beginTransaction();
         try {
             $model->attributes = $_POST['Customer'];
             $contact->attributes = $_POST['Contact'];
             if ($model->save()) {
                 $contact->parent_id = $model->id;
                 $contact->parent_model = 'Customer';
                 if ($contact->save()) {
                     UserLogs::createLog('create new ' . get_class($model) . ' ' . $model->id);
                     Yii::app()->user->setFlash('success', 'Customer ' . $model->name . '\'s information has been updated');
                     $trans->commit();
                     $this->redirect(array('view', 'id' => $model->id));
                 } else {
                     throw new CException(var_export($contact->getErrors(), true));
                 }
             }
         } catch (CException $e) {
             $trans->rollback();
             throw $e;
         }
     }
     $contact->dob = date('m/d/Y', strtotime($contact->dob));
     $this->render('update', array('model' => $model, 'contact' => $contact, 'customer_contacs' => $customer_contacts, 'rekening' => $rekening, 'customer_tokens' => $customer_tokens, 'customer_shipping_profile' => $customer_shipping_profile));
 }
 public function actionAddContact()
 {
     if (@$_POST['site_id']) {
         if (!($site = Site::model()->findByPk($_POST['site_id']))) {
             throw new Exception("Site not found: " . $_POST['site_id']);
         }
     } else {
         if (!($institution = Institution::model()->findByPk(@$_POST['institution_id']))) {
             throw new Exception("Institution not found: " . @$_POST['institution_id']);
         }
     }
     if (!($patient = Patient::model()->findByPk(@$_POST['patient_id']))) {
         throw new Exception("patient required for contact assignment");
     }
     // Attempt to de-dupe by looking for an existing record that matches the user's input
     $criteria = new CDbCriteria();
     $criteria->compare('lower(title)', strtolower($_POST['title']));
     $criteria->compare('lower(first_name)', strtolower($_POST['first_name']));
     $criteria->compare('lower(last_name)', strtolower($_POST['last_name']));
     if (isset($site)) {
         $criteria->compare('site_id', $site->id);
     } else {
         $criteria->compare('institution_id', $institution->id);
     }
     if ($contact = Contact::model()->with('locations')->find($criteria)) {
         foreach ($contact->locations as $location) {
             $pca = new PatientContactAssignment();
             $pca->patient_id = $patient->id;
             $pca->location_id = $location->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));
         }
     }
     $contact = new Contact();
     $contact->attributes = $_POST;
     if (@$_POST['contact_label_id'] == 'nonspecialty') {
         if (!($label = ContactLabel::model()->findByPk(@$_POST['label_id']))) {
             throw new Exception("Contact label not found: " . @$_POST['label_id']);
         }
     } else {
         if (!($label = ContactLabel::model()->find('name=?', array(@$_POST['contact_label_id'])))) {
             throw new Exception("Contact label not found: " . @$_POST['contact_label_id']);
         }
     }
     $contact->contact_label_id = $label->id;
     if (!$contact->save()) {
         throw new Exception("Unable to save contact: " . print_r($contact->getErrors(), true));
     }
     $cl = new ContactLocation();
     $cl->contact_id = $contact->id;
     if (isset($site)) {
         $cl->site_id = $site->id;
     } else {
         $cl->institution_id = $institution->id;
     }
     if (!$cl->save()) {
         throw new Exception("Unable to save contact location: " . print_r($cl->getErrors(), true));
     }
     $pca = new PatientContactAssignment();
     $pca->patient_id = $patient->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 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));
 }
 public function beforeSave($event)
 {
     if ($this->owner->isNewRecord) {
         // create a base contact object
         if (!$this->owner->contact_id) {
             $contact = new Contact();
             if (!$contact->save()) {
                 throw new Exception('cannot save institution contact: ' . print_r($contact->getErrors(), true));
             }
             $this->owner->contact_id = $contact->id;
         }
     }
 }
 public function actionRegister()
 {
     if (!isset($_POST['contact'])) {
         echo CJSON::encode($this->statusError('form not available'));
         Yii::app()->end();
     }
     $checkUsername = Customer::model()->findByAttributes(array('username' => $_POST['contact']['email']));
     $checkEmail = Contact::model()->findByAttributes(array('email' => $_POST['contact']['email']));
     if ($checkUsername !== null) {
         echo CJSON::encode($this->statusError(array('field' => 'username', 'message' => 'username already exist')));
         Yii::app()->end();
     } else {
         if ($checkEmail !== null) {
             echo CJSON::encode($this->statusError(array('field' => 'email', 'message' => 'email already exist')));
             Yii::app()->end();
         }
     }
     $customer = new Customer('api');
     $customer->created = time();
     $customer->name = $_POST['contact']['name'];
     $customer->type = $_POST['contact']['type'];
     $customer->username = $_POST['contact']['email'];
     $customer->password = Yii::app()->hasher->hashPassword($_POST['contact']['password']);
     $customer->accountnr = 'REG' . time();
     $customer->save(false);
     $contact = new Contact();
     $contact->attributes = $_POST['contact'];
     $contact->parent_id = $customer->id;
     $contact->parent_model = 'Customer';
     if ($contact->save()) {
         echo CJSON::encode($this->statusSuccess(array('customer' => $customer, 'contact' => $contact)));
     } else {
         var_dump($contact->getErrors());
         exit;
     }
 }