$smarty->assign('purchased_products', isset($search['purchased_products']) ? $search['purchased_products'] : array());
/*
 * require $app_main_dir.'/include/users/register.php'; {
 */
$fill_error =& cw_session_register('fill_error', array());
$filled_profile =& cw_session_register('filled_profile', array());
$fields_area = cw_profile_fields_get_area($user, $salesman_membership, $self_modification, AREA_TYPE == 'A' ? $usertype : null);
list($profile_sections, $profile_fields, $additional_fields) = cw_profile_fields_get_sections('U', true, $fields_area);
$userphoto = array();
if ($user) {
    $userinfo = cw_call('cw_user_get_info', array($user, 65535));
    $userphoto = cw_call('cw_user_get_avatar', array($user));
}
if ($action == 'update') {
    $fill_error = array();
    $update_fields['address'] = cw_user_address_array($update_fields['address']);
    // make sure we processes array of addresses
    // password is not required for existing user
    if ($mode != 'add') {
        $profile_fields['basic']['password']['is_required'] = 0;
    }
    // handle flags "as_new" and "is_same_address"
    foreach ($update_fields['address'] as $type => $address) {
        if ($address['as_new']) {
            $update_fields['address'][$type]['address_id'] = 0;
        }
        if (($type == 'current' || $address['address_id'] == 'current') && $update_fields['is_same_address']) {
            unset($update_fields['address'][$type]);
        }
        // Do not check shipping address if it is the same as billing
    }
function cw_user_update($userinfo, $customer_id, $by_customer_id)
{
    global $tables, $addons;
    foreach (array('email', 'status', 'membership_id', 'language', 'change_password') as $fld) {
        if (isset($userinfo[$fld])) {
            $customer[$fld] = $userinfo[$fld];
        }
    }
    if (AREA_TYPE == 'A' && $userinfo['usertype']) {
        $customer['usertype'] = $userinfo['usertype'];
    }
    if (AREA_TYPE == 'A' && $userinfo['customer_id']) {
        $customer['customer_id'] = $userinfo['customer_id'];
    }
    $current = cw_user_get_info($customer_id, 0);
    //allow password update only by the profile owner or by admin
    if (isset($userinfo['password'])) {
        if (!empty($userinfo['password']) && ($customer_id == $by_customer_id || AREA_TYPE == 'A')) {
            $customer['password'] = cw_call('cw_user_get_hashed_password', array($userinfo['password']));
        }
    }
    cw_event('on_user_update', array($customer_id, $by_customer_id, $customer, $userinfo));
    cw_array2update('customers', $customer, "customer_id='{$customer_id}'");
    $additional_info = $userinfo['additional_info'];
    $customer_info_fields = array('ssn', 'tax_number', 'birthday', 'birthday_place', 'sex', 'married', 'nationality', 'company', 'employees', 'foundation', 'foundation_place', 'company_type', 'company_id', 'can_change_company_id', 'contact_date', 'tax_id', 'payment_id', 'payment_note', 'tax_exempt', 'separate_invoices', 'shipping_operated', 'shipment_paid', 'shipping_company_to_carrier_id', 'shipping_company_from_carrier_id', 'cod_delivery_type_id', 'leaving_type', 'department_id', 'division_id', 'doc_prefix', 'order_entering_format', 'status_note');
    if (!cw_query_first_cell("select count(*) from {$tables['customers_customer_info']} where customer_id='{$customer_id}'")) {
        cw_array2insert('customers_customer_info', array('customer_id' => $customer_id));
    }
    cw_array2update('customers_customer_info', $additional_info, "customer_id='{$customer_id}'", $customer_info_fields);
    /*
          db_query("update $tables[customers_addresses] set main=0, current=0 where customer_id = '$customer_id'");
        foreach(array('main_address', 'current_address') as $addr)  {
            if (!$userinfo[$addr]) continue;
    
            $userinfo[$addr]['main'] = $addr == 'main_address';
            $userinfo[$addr]['current'] = $addr == 'current_address';
    
            $address_id = $userinfo[$addr]['address_id'];
            if (!$address_id) $address_id = cw_array2insert('customers_addresses', array('customer_id' => $customer_id, 'main' => $userinfo[$addr]['main'], 'current' => $userinfo[$addr]['current']));
    
            cw_user_update_address($customer_id, $address_id, $userinfo[$addr]);
        }
    */
    foreach (cw_user_address_array($userinfo['addresses']) as $address_id => $address) {
        cw_user_update_address($customer_id, $address['address_id'], $address);
    }
    cw_user_check_addresses($customer_id);
    $relations = $userinfo['relations'];
    $relations_info = array('salesman_customer_id' => $relations['salesman_customer_id'], 'employee_customer_id' => $relations['employee_customer_id'], 'warehouse_customer_id' => $relations['warehouse_customer_id']);
    if (!cw_query_first_cell("select count(*) from {$tables['customers_relations']} where customer_id='{$customer_id}'")) {
        db_query("insert into {$tables['customers_relations']}(customer_id) values('{$customer_id}')");
    }
    cw_array2update('customers_relations', $relations_info, "customer_id='{$customer_id}'");
    if ($userinfo['chamber_certificate_uploaded']['file_path']) {
        db_query("delete from {$tables['customers_chamber_certificates']} where customer_id='{$customer_id}'");
        cw_file_area_save('customers_chamber_certificates', $customer_id, $userinfo['chamber_certificate_uploaded']);
    }
    if (is_array($userinfo['custom_fields'])) {
        foreach ($userinfo['custom_fields'] as $field_id => $value) {
            db_query("delete from {$tables['register_fields_values']} where customer_id='{$customer_id}' AND field_id='{$field_id}'");
            cw_array2insert('register_fields_values', array('field_id' => $field_id, 'value' => $value, 'customer_id' => $customer_id));
        }
    }
    $customer_system_info = cw_query_first("select * from {$tables['customers_system_info']} where customer_id='{$customer_id}'");
    $customer_system_info['customer_id'] = $customer_id;
    if (!$customer_system_info['creation_customer_id']) {
        $customer_system_info['creation_customer_id'] = $by_customer_id;
        $customer_system_info['creation_date'] = cw_core_get_time();
    }
    $customer_system_info['modification_customer_id'] = $by_customer_id;
    $customer_system_info['modification_date'] = cw_core_get_time();
    cw_array2insert('customers_system_info', $customer_system_info, true);
    $salesman_info = cw_query_first("select * from {$tables['customers_salesman_info']} where customer_id = '{$customer_id}'");
    $salesman_info['parent_customer_id'] = $userinfo['salesman_info']['parent_customer_id'];
    cw_array2insert('customers_salesman_info', $salesman_info, true);
}
function cw_check_user_field_validate($customer_id, $update_fields, $profile_fields)
{
    $fill_error = array();
    // Transform one array of addresses to multiple addresses
    // $update_fields['address']['main'] and $update_fields['address']['current'] become $update_fields['address_main'] and $update_fields['address_current']
    foreach (cw_user_address_array($update_fields['address']) as $address_id => $address) {
        $update_fields['address_' . $address_id] = $address;
    }
    unset($update_fields['address']);
    // Check sections which presented in updated fields
    foreach ($update_fields as $section => $fields) {
        $_section = $section;
        if (strpos($section, 'address_') !== false) {
            $_section = 'address';
        }
        // Check address_xxxxx sections according to 'address' rules
        if (!empty($profile_fields[$_section]) && is_array($profile_fields[$_section])) {
            foreach ($profile_fields[$_section] as $field => $val) {
                $cw_for_check = 'cw_check_user_field_' . $val['field'];
                if (function_exists($cw_for_check)) {
                    if ($ret = $cw_for_check($customer_id, $update_fields[$section][$field], $update_fields[$section], $update_fields)) {
                        $fill_error[$_section][$field] = $ret;
                    }
                }
                if ($val['is_required'] && empty($update_fields[$section][$field])) {
                    $fill_error[$_section][$field] = 'a';
                }
            }
        }
    }
    return $fill_error;
    // TODO: should return true or new Error
}