/** * 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(); } } } } }
/** * 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'); if (is_array($obj_custom_properties)) { foreach ($obj_custom_properties as $id => &$val) { $val = remove_scripts($val); } } $date_format = user_config_option('date_format'); $date_format_tip = date_format_tip($date_format); $required_custom_props = array(); $object_type_id = $object instanceof TemplateTask ? ProjectTasks::instance()->getObjectTypeId() : $object->getObjectTypeId(); $customProps = CustomProperties::getAllCustomPropertiesByObjectType($object_type_id); //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 ($cp->getIsRequired()) { $required_custom_props[] = $cp; } } foreach ($required_custom_props as $req_cp) { if (!isset($obj_custom_properties[$req_cp->getId()])) { throw new Exception(lang('custom property value required', $req_cp->getName())); } } if (is_array($obj_custom_properties)) { // check required 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) { if (trim($val) != '' && trim($val) != $date_format_tip) { $dtv = DateTimeValueLib::dateFromFormatAndString($date_format, $val); $newValues[] = $dtv->format("Y-m-d H:i:s"); } } $value = $newValues; } else { if (trim($value) != '' && trim($val) != $date_format_tip) { $dtv = DateTimeValueLib::dateFromFormatAndString($date_format, $value); $value = $dtv->format("Y-m-d H:i:s"); } else { $value = ''; } } } foreach (array_var($_REQUEST, 'remove_custom_properties', array()) as $cpropid => $remove) { if ($remove) { CustomPropertyValues::deleteCustomPropertyValues($object->getId(), $cpropid); } } Hook::fire('before_save_custom_property_value', array('custom_prop' => $custom_property), $value); if (is_array($value)) { if ($custom_property->getType() == 'address') { if ($custom_property->getIsRequired()) { if (array_var($value, 'street') == '' && array_var($value, 'city') == '' && array_var($value, 'state') == '' && array_var($value, 'country') == '' && array_var($value, 'zip_code') == '') { throw new Exception(lang('custom property value required', $custom_property->getName())); } $errors = array(lang('error form validation')); Env::useHelper('form'); $ok = checkAddressInputMandatoryFields($value, $custom_property->getName(), $errors); if (!$ok) { throw new Exception(implode("\n - ", $errors)); } } // Address custom property $val = array_var($value, 'type') . '|' . array_var($value, 'street') . '|' . array_var($value, 'city') . '|' . array_var($value, 'state') . '|' . array_var($value, 'country') . '|' . array_var($value, 'zip_code'); CustomPropertyValues::deleteCustomPropertyValues($object->getId(), $id); $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 { //Save multiple values 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 != '') { $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 = str_replace("'", "\\'", $custom_property->getName()); if (is_array($value)) { $value = implode(', ', $value); } $value = str_replace("'", "\\'", $value); $sql = "INSERT INTO " . TABLE_PREFIX . "searchable_objects (rel_object_id, column_name, content)\r\n\t\t\t\t\t\tVALUES ('" . $object->getId() . "', '" . $name . "', '" . $value . "')\r\n\t\t\t\t\t\tON DUPLICATE KEY UPDATE content='" . $value . "'"; DB::execute($sql); } } } } //Save the key - value pair custom properties (object_properties table) $object->clearObjectProperties(); $names = array_var($_POST, 'custom_prop_names'); $values = array_var($_POST, 'custom_prop_values'); if (!is_array($names)) { return; } for ($i = 0; $i < count($names); $i++) { $name = trim($names[$i]); $value = trim($values[$i]); if ($name != '' && $value != '') { $property = new ObjectProperty(); $property->setObject($object); $property->setPropertyName($name); $property->setPropertyValue($value); $property->save(); if ($object->isSearchable()) { $object->addPropertyToSearchableObject($property); } } } }