Beispiel #1
0
 public function updateLocation()
 {
     $form = $this->input->post('form');
     $related_object_name = urldecode(trim($this->input->post('related_object_name')));
     $related_object_id = urldecode(trim($this->input->post('related_object_id')));
     if (!$form || !is_array($form)) {
         $this->returnError('The form can not be processed.');
     }
     if (!$related_object_name || is_array($related_object_name)) {
         $this->returnError('Missing or wrong related object name.');
     }
     if (!$related_object_id || is_array($related_object_id)) {
         $this->returnError('Missing or wrong related object id.');
     }
     $possible_object_names = array('person', 'organization', 'location');
     if (!in_array($related_object_name, $possible_object_names)) {
         $this->returnError('The specified object ' . $related_object_name . ' is not a valid object.');
     }
     switch ($related_object_name) {
         case 'person':
             $contact = new Mdl_Person();
             $contact->uid = $related_object_id;
             break;
         case 'organization':
             $contact = new Mdl_Organization();
             $contact->oid = $related_object_id;
             break;
         default:
             $this->returnError('The specified object' . $related_object_name . ' can not be a related object.');
             break;
     }
     $result = $contact->get(null, true);
     if ($result['status']['status_code'] != 200) {
         $this->returnError('The specified related contact with id ' . $related_object_id . ' can not be found.');
     }
     $contact_result = $result['data']['0'];
     $location = new Mdl_Location();
     $input = array();
     foreach ($form as $key => $item) {
         if (!empty($item['field']) && isset($item['value'])) {
             $input[$item['field']] = $item['value'];
         }
     }
     //let's check if the user set one of the two reserved descriptions
     if (strtolower($input['locDescription']) == 'home' || strtolower($input['locDescription']) == 'registered address') {
         $this->returnError('The description "' . $input['locDescription'] . '" is reserved. Please choose another description.');
     }
     $creation = $input['locId'] == '' ? true : false;
     $return = $location->save($creation, false, $input);
     if ($return) {
         if (empty($location->locId)) {
             $this->returnError('Something went wrong during the location save process.');
         }
         if ($creation) {
             //associate the contact with the new location
             if (!empty($contact_result['locRDN'])) {
                 $locs = implode(',', $contact_result['locRDN']);
                 $locs .= ',' . $location->locId;
             } else {
                 $locs = $location->locId;
             }
             $contact_result['locRDN'] = explode(',', $locs);
             if ($contact->arrayToObject($contact_result)) {
                 //TODO add cases
                 if ($contact->save(false)) {
                     $message = 'The location has been created.';
                 } else {
                     $message = 'The location has been created but it has not been associated to the contact.';
                 }
             }
         } else {
             $message = 'The location has been updated.';
         }
     } else {
         //do something
         $this->returnError('The location has not been created');
     }
     if (isset($message)) {
         $to_js = array();
         $to_js['message'] = $message;
         $to_js['focus_tab'] = '#tab_locations';
         $this->output($to_js);
     }
 }