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 defineFields() { parent::defineFields(); // add custom fields definitions (as virtual fields) $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; } 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 LoadPersistantVars() { $sql = 'SELECT VariableValue, VariableName FROM '.TABLE_PREFIX.'PersistantSessionData WHERE PortalUserId = '.$this->GetID(); $this->persistantVars = $this->Conn->GetCol($sql, 'VariableName'); } function setPersistantVar($var_name, $var_value) { $this->persistantVars[$var_name] = $var_value; $replace_hash = Array( 'PortalUserId' => $this->GetID(), 'VariableName' => $var_name, 'VariableValue' => $var_value ); $this->Conn->doInsert($replace_hash, TABLE_PREFIX.'PersistantSessionData', 'REPLACE'); } function getPersistantVar($var_name) { return getArrayValue($this->persistantVars, $var_name); } 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(); $this->LoadPersistantVars(); } return $ret; } /** * Returns IDs of groups to which user belongs and membership is not expired * * @return Array * @access public */ function getMembershipGroups($force_reload = false) { $user_groups = $this->Application->RecallVar('UserGroups'); if($user_groups === false || $force_reload) { $sql = 'SELECT GroupId FROM %s WHERE (PortalUserId = %s) AND ( (MembershipExpires IS NULL) OR ( MembershipExpires >= UNIX_TIMESTAMP() ) )'; $sql = sprintf($sql, TABLE_PREFIX.'UserGroup', $this->GetID() ); return $this->Conn->GetCol($sql); } else { return explode(',', $user_groups); } } /** * Set's Login from Email if required by configuration settings * */ function setLogin() { if( $this->Application->ConfigValue('Email_As_Login') ) { $this->SetDBField('Login', $this->GetDBField('Email') ); } } function SendEmailEvents() { switch( $this->GetDBField('Status') ) { case 1: $this->Application->EmailEventAdmin('USER.ADD', $this->GetID() ); $this->Application->EmailEventUser('USER.ADD', $this->GetID() ); break; case 2: $this->Application->EmailEventAdmin('USER.ADD.PENDING', $this->GetID() ); $this->Application->EmailEventUser('USER.ADD.PENDING', $this->GetID() ); break; } } function isSubscriberOnly() { $subscribers_group_id = $this->Application->ConfigValue('User_SubscriberGroup'); $sql = 'SELECT PortalUserId FROM '.TABLE_PREFIX.'UserGroup WHERE GroupId = '.$subscribers_group_id.' AND PortalUserId = '.$this->GetDBField('PortalUserId').' AND PrimaryGroup = 1'; 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(); // find out how to syncronize user only when it's copied to live table $sync_manager =& $this->Application->recallObject('UsersSyncronizeManager', null, Array(), 'InPortalSyncronize'); $sync_manager->performAction('createUser', $this->FieldValues); } return $ret; } function Update($id=null, $system_update=false) { $ret = parent::Update($id, $system_update); if ($ret) { $this->UpdateCustomFields(); // find out how to syncronize user only when it's copied to live table $sync_manager =& $this->Application->recallObject('UsersSyncronizeManager', null, Array(), 'InPortalSyncronize'); $sync_manager->performAction('updateUser', $this->FieldValues); } return $ret; } /** * Deletes the record from databse * * @access public * @return bool */ function Delete($id = null) { $ret = parent::Delete($id); if ($ret) { $sync_manager =& $this->Application->recallObject('UsersSyncronizeManager', null, Array(), 'InPortalSyncronize'); $sync_manager->performAction('deleteUser', $this->FieldValues); } return $ret; } function setName($full_name) { $full_name = explode(' ', $full_name); if (count($full_name) > 2) { $last_name = array_pop($full_name); $first_name = implode(' ', $full_name); } else { $last_name = $full_name[1]; $first_name = $full_name[0]; } $this->SetDBField('FirstName', $first_name); $this->SetDBField('LastName', $last_name); } } ?>