/**
  * Function to return singleton object
  * 
  * @return object $_singleton
  * @access public
  * @static
  */
 public static function &singleton($contact_id)
 {
     if (self::$_singleton === NULL) {
         self::$_singleton = new CRM_Lidmaatschapwijziging_ConfigRelationship($contact_id);
     }
     return self::$_singleton;
 }
 function postProcess()
 {
     $values = $this->exportValues();
     $this->_configRelationship = CRM_Lidmaatschapwijziging_ConfigRelationship::singleton($this->_contactId);
     // empty
     if ('empty' == $this->_request) {
         // set message
         $session = CRM_Core_Session::singleton();
         $session->setStatus(ts('Voor %1 bestaat geen lidmaatschap !', $this->_display_name), ts('Lidmaatschap Wijziging - Relatie'), 'warning');
         // redirect user
         $url = CRM_Utils_System::url('civicrm/lidmaatschapwijziging/group', 'reset=1&cid=' . $this->_contactId);
         CRM_Utils_System::redirect($url);
     }
     // choose
     if ('choose' == $this->_request) {
         // set message
         $session = CRM_Core_Session::singleton();
         // redirect user
         $url = CRM_Utils_System::url('civicrm/lidmaatschapwijziging/relationship', 'reset=1&request=update&cid=' . $this->_contactId . '&relationship_id=' . $values['relationship_id']);
         CRM_Utils_System::redirect($url);
     }
     // update
     if ('update' == $this->_request) {
         /**
          * Note works diffrent from the other, if the
          * is empty delete it, if the note not empty create 
          * it or update
          */
         if (!isset($values['note']) or empty($values['note'])) {
             // delete if empty
             // only delete if there is a id
             if (isset($values['note_id']) and !empty($values['note_id'])) {
                 // just delete all the notes, there is only one note with the relationship
                 try {
                     $params = array('version' => 3, 'sequential' => 1, 'entity_id' => $this->_relationshipId, 'contact_id' => $this->_contactId, 'entity_table' => 'civicrm_relationship', 'note' => NULL, 'id' => $values['note_id']);
                     $result = civicrm_api('Note', 'delete', $params);
                     // check no error
                     if (isset($result['is_error']) and !$result['is_error']) {
                         // if there is no error
                         // set message
                         //$session = CRM_Core_Session::singleton();
                         //$session->setStatus(ts('%1 is opgeslagen !', $this->_display_name), ts('Lidmaatschap Wijziging - Relatie'), 'success');
                     } else {
                         // if there is a error
                         // set message
                         $session = CRM_Core_Session::singleton();
                         $session->setStatus(ts('Er is een error: %1, De notitie van %2 is niet verwijdert !', array($result['error_message'], $this->_display_name)), ts('Lidmaatschap Wijziging - Relatie'), 'error');
                     }
                 } catch (CiviCRM_API3_Exception $ex) {
                     throw new Exception('Could not delete note, ' . 'error from Api Note create: ' . $ex->getMessage());
                 }
             }
         } else {
             // create if not empty
             try {
                 $params = array('version' => 3, 'sequential' => 1, 'entity_id' => $this->_relationshipId, 'contact_id' => $this->_contactId, 'entity_table' => 'civicrm_relationship', 'note' => $values['note']);
                 if (isset($values['note_id']) and !empty($values['note_id'])) {
                     $params['id'] = $values['note_id'];
                 }
                 $result = civicrm_api('Note', 'create', $params);
                 // check no error
                 if (isset($result['is_error']) and !$result['is_error']) {
                     // if there is no error
                     // set message
                     //$session = CRM_Core_Session::singleton();
                     //$session->setStatus(ts('%1 is opgeslagen !', $this->_display_name), ts('Lidmaatschap Wijziging - Relatie'), 'success');
                 } else {
                     // if there is a error
                     // set message
                     $session = CRM_Core_Session::singleton();
                     $session->setStatus(ts('Er is een error: %1, De notitie van %2 is niet opgeslagen !', array($result['error_message'], $this->_display_name)), ts('Lidmaatschap Wijziging - Relatie'), 'error');
                 }
             } catch (CiviCRM_API3_Exception $ex) {
                 throw new Exception('Could not create note, ' . 'error from Api Note create: ' . $ex->getMessage());
             }
         }
         // set checkboxes
         // set if not exist
         if (!isset($values['is_permission_a_b'])) {
             $values['is_permission_a_b'] = '0';
         }
         // set if not exist
         if (!isset($values['is_permission_b_a'])) {
             $values['is_permission_b_a'] = '0';
         }
         // set if not exist
         if (!isset($values['is_active'])) {
             $values['is_active'] = '0';
         }
         // api create relatiosnhip
         try {
             $params = array('version' => 3, 'sequential' => 1, 'id' => $this->_relationshipId, 'contact_id_a' => $values['contact_id_a'], 'contact_id_b' => $values['contact_id_b'], 'relationship_type_id' => $values['relationship_type_id'], 'start_date' => $values['start_date'], 'end_date' => $values['end_date'], 'description' => $values['description'], 'is_permission_a_b' => $values['is_permission_a_b'], 'is_permission_b_a' => $values['is_permission_b_a'], 'is_active' => $values['is_active']);
             $result = civicrm_api('Relationship', 'create', $params);
             // check no error
             if (isset($result['is_error']) and !$result['is_error']) {
                 // if there is no error
                 // set message
                 $session = CRM_Core_Session::singleton();
                 $session->setStatus(ts('%1 is opgeslagen !', $this->_display_name), ts('Lidmaatschap Wijziging - Relatie'), 'success');
                 // redirect user
                 $url = CRM_Utils_System::url('civicrm/lidmaatschapwijziging/group', 'reset=1&cid=' . $this->_contactId);
                 CRM_Utils_System::redirect($url);
             } else {
                 // if there is a error
                 // set message
                 $session = CRM_Core_Session::singleton();
                 $session->setStatus(ts('Er is een error: %1, %2 is niet opgeslagen !', array($result['error_message'], $this->_display_name)), ts('Lidmaatschap Wijziging - Relatie'), 'error');
                 // redirect user
                 $url = CRM_Utils_System::url('civicrm/lidmaatschapwijziging/relationship', 'reset=1&cid=' . $this->_contactId);
                 CRM_Utils_System::redirect($url);
             }
         } catch (CiviCRM_API3_Exception $ex) {
             throw new Exception('Could not create relationship, ' . 'error from Api Relationship create: ' . $ex->getMessage());
         }
     }
     //parent::postProcess();
 }