/**
  * Adds the custom properties of an object into the database.
  * 
  * @param $object
  * @return unknown_type
  */
 function add_custom_properties($object)
 {
     if (logged_user()->isGuest()) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     $obj_custom_properties = array_var($_POST, 'object_custom_properties');
     $customProps = CustomProperties::getAllCustomPropertiesByObjectType($object->getObjectTypeId());
     //Sets all boolean custom properties to 0. If any boolean properties are returned, they are subsequently set to 1.
     foreach ($customProps as $cp) {
         if ($cp->getType() == 'boolean') {
             $custom_property_value = CustomPropertyValues::getCustomPropertyValue($object->getId(), $cp->getId());
             if (!$custom_property_value instanceof CustomPropertyValue) {
                 $custom_property_value = new CustomPropertyValue();
             }
             $custom_property_value->setObjectId($object->getId());
             $custom_property_value->setCustomPropertyId($cp->getId());
             $custom_property_value->setValue(0);
             $custom_property_value->save();
         }
     }
     if (is_array($obj_custom_properties)) {
         foreach ($obj_custom_properties as $id => $value) {
             //Get the custom property
             $custom_property = null;
             foreach ($customProps as $cp) {
                 if ($cp->getId() == $id) {
                     $custom_property = $cp;
                     break;
                 }
             }
             if ($custom_property instanceof CustomProperty) {
                 // save dates in standard format "Y-m-d H:i:s", because the column type is string
                 if ($custom_property->getType() == 'date') {
                     if (is_array($value)) {
                         $newValues = array();
                         foreach ($value as $val) {
                             $dtv = DateTimeValueLib::dateFromFormatAndString(user_config_option('date_format'), $val);
                             $newValues[] = $dtv->format("Y-m-d H:i:s");
                         }
                         $value = $newValues;
                     } else {
                         $dtv = DateTimeValueLib::dateFromFormatAndString(user_config_option('date_format'), $value);
                         $value = $dtv->format("Y-m-d H:i:s");
                     }
                 }
                 //Save multiple values
                 if (is_array($value)) {
                     CustomPropertyValues::deleteCustomPropertyValues($object->getId(), $id);
                     foreach ($value as &$val) {
                         if (is_array($val)) {
                             // CP type == table
                             $str_val = '';
                             foreach ($val as $col_val) {
                                 $col_val = str_replace("|", "\\|", $col_val);
                                 $str_val .= ($str_val == '' ? '' : '|') . $col_val;
                             }
                             $val = $str_val;
                         }
                         if ($val != '') {
                             if (strpos($val, ',')) {
                                 $val = str_replace(',', '|', $val);
                             }
                             $custom_property_value = new CustomPropertyValue();
                             $custom_property_value->setObjectId($object->getId());
                             $custom_property_value->setCustomPropertyId($id);
                             $custom_property_value->setValue($val);
                             $custom_property_value->save();
                         }
                     }
                 } else {
                     if ($custom_property->getType() == 'boolean') {
                         $value = isset($value);
                     }
                     $cpv = CustomPropertyValues::getCustomPropertyValue($object->getId(), $id);
                     if ($cpv instanceof CustomPropertyValue) {
                         $custom_property_value = $cpv;
                     } else {
                         $custom_property_value = new CustomPropertyValue();
                     }
                     $custom_property_value->setObjectId($object->getId());
                     $custom_property_value->setCustomPropertyId($id);
                     $custom_property_value->setValue($value);
                     $custom_property_value->save();
                 }
                 //Add to searchable objects
                 if ($object->isSearchable() && ($custom_property->getType() == 'text' || $custom_property->getType() == 'list' || $custom_property->getType() == 'numeric')) {
                     $name = $custom_property->getName();
                     $searchable_object = SearchableObjects::findOne(array("conditions" => "`rel_object_id` = " . $object->getId() . " AND `column_name` = '{$name}'"));
                     if (!$searchable_object) {
                         $searchable_object = new SearchableObject();
                     }
                     if (is_array($value)) {
                         $value = implode(', ', $value);
                     }
                     $searchable_object->setRelObjectId($object->getId());
                     $searchable_object->setColumnName($name);
                     $searchable_object->setContent($value);
                     $searchable_object->save();
                 }
             }
         }
     }
 }