Index: trunk/kernel/units/users/users_event_handler.php =================================================================== diff -u -N -r3282 -r3306 --- trunk/kernel/units/users/users_event_handler.php (.../users_event_handler.php) (revision 3282) +++ trunk/kernel/units/users/users_event_handler.php (.../users_event_handler.php) (revision 3306) @@ -343,7 +343,11 @@ function OnAfterItemValidate(&$event) { $object =& $event->getObject(); - $object->SetDBField('ResourceId', $this->Application->NextResourceId() ); + $resource_id = $object->GetDBField('ResourceId'); + if (!$resource_id) + { + $object->SetDBField('ResourceId', $this->Application->NextResourceId() ); + } } Index: trunk/core/units/users/users_config.php =================================================================== diff -u -N -r3282 -r3306 --- trunk/core/units/users/users_config.php (.../users_config.php) (revision 3282) +++ trunk/core/units/users/users_config.php (.../users_config.php) (revision 3306) @@ -43,6 +43,7 @@ ), 'IDField' => 'PortalUserId', + 'ItemType' => 6, // used for custom fields only (on user's case) 'TableName' => TABLE_PREFIX.'PortalUser', 'ListSQLs' => Array( ''=>'SELECT * FROM %s', Index: trunk/core/units/users/users_event_handler.php =================================================================== diff -u -N -r3282 -r3306 --- trunk/core/units/users/users_event_handler.php (.../users_event_handler.php) (revision 3282) +++ trunk/core/units/users/users_event_handler.php (.../users_event_handler.php) (revision 3306) @@ -343,7 +343,11 @@ function OnAfterItemValidate(&$event) { $object =& $event->getObject(); - $object->SetDBField('ResourceId', $this->Application->NextResourceId() ); + $resource_id = $object->GetDBField('ResourceId'); + if (!$resource_id) + { + $object->SetDBField('ResourceId', $this->Application->NextResourceId() ); + } } Index: trunk/core/units/users/users_item.php =================================================================== diff -u -N -r2818 -r3306 --- trunk/core/units/users/users_item.php (.../users_item.php) (revision 2818) +++ trunk/core/units/users/users_item.php (.../users_item.php) (revision 3306) @@ -1,7 +1,101 @@ Application->getUnitOption($this->Prefix, 'ItemType'); + $sql = 'SELECT CustomFieldId, FieldName FROM '.TABLE_PREFIX.'CustomField WHERE Type = %s'; + $this->CustomFields = $this->Conn->GetCol( sprintf($sql, $item_type), 'FieldName' ); + } + + + function Configure() + { + parent::Configure(); + + $add_fields = Array(); + $custom_field_options = Array('default' => '', 'not_null' => 1); + foreach ($this->CustomFields as $custom_field => $custom_id) + { + $add_fields[$custom_field] = $custom_field_options; + $this->SetDBField($custom_field, ''); + } + + if($add_fields) + { + $add_fields = array_merge_recursive2($add_fields, $this->VirtualFields); + $this->setVirtualFields($add_fields); + } + } + /** + * Load custom fields values as virtual fields for dbitem + * + * @return bool + */ + function LoadCustomFields() + { + if (!$this->CustomFields) return false; + + $custom_ids = implode(',', $this->CustomFields); + $sql = 'SELECT Value, CustomFieldId + FROM '.TABLE_PREFIX.'CustomMetaData + WHERE ResourceId = '.$this->GetDBField('ResourceId').' AND CustomFieldId IN ('.$custom_ids.')'; + $custom_values = $this->Conn->GetCol($sql, 'CustomFieldId'); + if (!$custom_values) return false; + + $custom_fields = array_flip($this->CustomFields); + foreach ($custom_values as $custom_id => $custom_value) + { + $this->SetDBField($custom_fields[$custom_id], $custom_value); + } + return true; + } + + function UpdateCustomFields() + { + $data_table = TABLE_PREFIX.'CustomMetaData'; + + // get values already written, to find their ids + $sql = 'SELECT CustomDataId, CustomFieldId + FROM '.$data_table.' + WHERE ResourceId = '.$this->GetDBField('ResourceId'); + $custom_value_ids = $this->Conn->GetCol($sql, 'CustomFieldId'); + $sql = 'REPLACE INTO '.$data_table.'(CustomDataId,ResourceId,CustomFieldId,Value) VALUES (%1$s,%2$s,%3$s,%4$s)'; + + foreach ($this->CustomFields as $custom_field => $custom_id) + { + $data_id = isset($custom_value_ids[$custom_id]) ? $custom_value_ids[$custom_id] : 0; + $custom_value = $this->GetDBField($custom_field); + + if (!$custom_value && $data_id) + { + $temp_sql = 'DELETE FROM '.$data_table.' WHERE CustomDataId = '.$data_id; + $this->Conn->Query($temp_sql); + } + elseif($custom_value) + { + $temp_sql = sprintf($sql, $data_id, $this->GetDBField('ResourceId'), $custom_id, $this->Conn->qstr( $this->GetDBField($custom_field) ) ); + $this->Conn->Query($temp_sql); + } + } + } + + function Load($id, $id_field_name = null) + { + $ret = parent::Load($id, $id_field_name); + if($ret) + { + $this->LoadCustomFields(); + } + return $ret; + } + + /** * Returns IDs of groups to which user belongs and membership is not expired * * @return Array @@ -61,6 +155,27 @@ return $this->Conn->GetOne($sql) == $this->GetDBField('PortalUserId'); } + function Create($force_id=false, $system_create=false) + { + $ret = parent::Create($force_id, $system_create); + if ($ret) + { + $this->UpdateCustomFields(); + } + return $ret; + } + + + function Update($id=null, $system_update=false) + { + $ret = parent::Update($id, $system_update); + if ($ret) + { + $this->UpdateCustomFields(); + } + return $ret; + } + } ?> \ No newline at end of file Index: trunk/kernel/units/users/users_config.php =================================================================== diff -u -N -r3282 -r3306 --- trunk/kernel/units/users/users_config.php (.../users_config.php) (revision 3282) +++ trunk/kernel/units/users/users_config.php (.../users_config.php) (revision 3306) @@ -43,6 +43,7 @@ ), 'IDField' => 'PortalUserId', + 'ItemType' => 6, // used for custom fields only (on user's case) 'TableName' => TABLE_PREFIX.'PortalUser', 'ListSQLs' => Array( ''=>'SELECT * FROM %s', Index: trunk/kernel/units/users/users_item.php =================================================================== diff -u -N -r2818 -r3306 --- trunk/kernel/units/users/users_item.php (.../users_item.php) (revision 2818) +++ trunk/kernel/units/users/users_item.php (.../users_item.php) (revision 3306) @@ -1,7 +1,101 @@ Application->getUnitOption($this->Prefix, 'ItemType'); + $sql = 'SELECT CustomFieldId, FieldName FROM '.TABLE_PREFIX.'CustomField WHERE Type = %s'; + $this->CustomFields = $this->Conn->GetCol( sprintf($sql, $item_type), 'FieldName' ); + } + + + function Configure() + { + parent::Configure(); + + $add_fields = Array(); + $custom_field_options = Array('default' => '', 'not_null' => 1); + foreach ($this->CustomFields as $custom_field => $custom_id) + { + $add_fields[$custom_field] = $custom_field_options; + $this->SetDBField($custom_field, ''); + } + + if($add_fields) + { + $add_fields = array_merge_recursive2($add_fields, $this->VirtualFields); + $this->setVirtualFields($add_fields); + } + } + /** + * Load custom fields values as virtual fields for dbitem + * + * @return bool + */ + function LoadCustomFields() + { + if (!$this->CustomFields) return false; + + $custom_ids = implode(',', $this->CustomFields); + $sql = 'SELECT Value, CustomFieldId + FROM '.TABLE_PREFIX.'CustomMetaData + WHERE ResourceId = '.$this->GetDBField('ResourceId').' AND CustomFieldId IN ('.$custom_ids.')'; + $custom_values = $this->Conn->GetCol($sql, 'CustomFieldId'); + if (!$custom_values) return false; + + $custom_fields = array_flip($this->CustomFields); + foreach ($custom_values as $custom_id => $custom_value) + { + $this->SetDBField($custom_fields[$custom_id], $custom_value); + } + return true; + } + + function UpdateCustomFields() + { + $data_table = TABLE_PREFIX.'CustomMetaData'; + + // get values already written, to find their ids + $sql = 'SELECT CustomDataId, CustomFieldId + FROM '.$data_table.' + WHERE ResourceId = '.$this->GetDBField('ResourceId'); + $custom_value_ids = $this->Conn->GetCol($sql, 'CustomFieldId'); + $sql = 'REPLACE INTO '.$data_table.'(CustomDataId,ResourceId,CustomFieldId,Value) VALUES (%1$s,%2$s,%3$s,%4$s)'; + + foreach ($this->CustomFields as $custom_field => $custom_id) + { + $data_id = isset($custom_value_ids[$custom_id]) ? $custom_value_ids[$custom_id] : 0; + $custom_value = $this->GetDBField($custom_field); + + if (!$custom_value && $data_id) + { + $temp_sql = 'DELETE FROM '.$data_table.' WHERE CustomDataId = '.$data_id; + $this->Conn->Query($temp_sql); + } + elseif($custom_value) + { + $temp_sql = sprintf($sql, $data_id, $this->GetDBField('ResourceId'), $custom_id, $this->Conn->qstr( $this->GetDBField($custom_field) ) ); + $this->Conn->Query($temp_sql); + } + } + } + + function Load($id, $id_field_name = null) + { + $ret = parent::Load($id, $id_field_name); + if($ret) + { + $this->LoadCustomFields(); + } + return $ret; + } + + /** * Returns IDs of groups to which user belongs and membership is not expired * * @return Array @@ -61,6 +155,27 @@ return $this->Conn->GetOne($sql) == $this->GetDBField('PortalUserId'); } + function Create($force_id=false, $system_create=false) + { + $ret = parent::Create($force_id, $system_create); + if ($ret) + { + $this->UpdateCustomFields(); + } + return $ret; + } + + + function Update($id=null, $system_update=false) + { + $ret = parent::Update($id, $system_update); + if ($ret) + { + $this->UpdateCustomFields(); + } + return $ret; + } + } ?> \ No newline at end of file