/**
 * Returns a list of hidden inputs which are extracted from the form attributes which can be extracted
 * from the user's profile information in Drupal. The attributes which are used are marked as handled
 * so they don't need to be output elsewhere on the form. This function also handles non-profile based 
 * CMS User ID, Username, and Email; and also special processing for names.
 * @param array $attributes List of form attributes.
 * @param array $args List of form arguments. Can include values called:
 *   copyFromProfile - boolean indicating if values should be copied from the profile when the names match
 *   nameShow - boolean, if true then name values should be displayed rather than hidden.
 *     In fact this extends to all profile fields whose names match attribute 
 *     captions. E.g. in D7, field_age would populate an attribute with caption 
 *     age.
 *   emailShow - boolean, if true then email values should be displayed rather than hidden.
 * @param boolean $exists Pass true for an existing record. If the record exists, then the attributes
 *   are marked as handled but are not output to avoid overwriting metadata about the original creator of the record.
 * @param array $readAuth Read authorisation tokens.
 * @return string HTML for the hidden inputs.
 */
function get_user_profile_hidden_inputs(&$attributes, $args, $exists, $readAuth)
{
    // This is Drupal specific code
    global $user;
    $logged_in = $user->uid > 0;
    // If the user is not logged in there is no profile so return early.
    if (!$logged_in) {
        return '';
    }
    $hiddens = '';
    $version6 = substr(VERSION, 0, 1) == '6';
    if ($version6) {
        // In version 6 the profile module holds user setttings.
        // but may not be using profile module, but still need to proceed to handle CMS User ID etc.
        if (function_exists('profile_load_all_profile')) {
            // D6 specific
            profile_load_all_profile($user);
        }
    } else {
        // In version 7, the field module holds user settings.
        $user = user_load($user->uid);
    }
    foreach ($attributes as &$attribute) {
        // Constuct the name of the user property (which varies between versions) to match against the attribute caption.
        $attrPropName = ($version6 ? 'profile_' : 'field_') . strtolower(str_replace(' ', '_', $attribute['caption']));
        if (isset($user->{$attrPropName}) && isset($args['copyFromProfile']) && $args['copyFromProfile'] == true) {
            // Obtain the property value which is stored differently between versions.
            if ($version6) {
                $attrPropValue = $user->{$attrPropName};
            } else {
                $attrPropValues = field_get_items('user', $user, $attrPropName);
                $attrPropValue = isset($attrPropValues[0]['safe value']) ? $attrPropValues[0]['safe value'] : $attrPropValues[0]['value'];
            }
            // lookups need to be translated to the termlist_term_id, unless they are already IDs
            if ($attribute['data_type'] === 'L' && !preg_match('/^[\\d]+$/', $attrPropValue)) {
                $terms = data_entry_helper::get_population_data(array('table' => 'termlists_term', 'extraParams' => $readAuth + array('termlist_id' => $attribute['termlist_id'], 'term' => $attrPropValue)));
                $value = count($terms) > 0 ? $terms[0]['id'] : '';
            } else {
                $value = $attrPropValue;
            }
            if (isset($args['nameShow']) && $args['nameShow'] == true) {
                // Show the attribute with default value.
                $attribute['default'] = $value;
            } else {
                // Hide the attribute value
                $attribute['handled'] = true;
                $attribute['value'] = $value;
            }
        } elseif (strcasecmp($attribute['caption'], 'cms user id') == 0) {
            $attribute['value'] = $user->uid;
            $attribute['handled'] = true;
            // user id attribute is never displayed
        } elseif (strcasecmp($attribute['caption'], 'cms username') == 0) {
            $attribute['value'] = $user->name;
            $attribute['handled'] = true;
            // username attribute is never displayed
        } elseif (strcasecmp($attribute['caption'], 'email') == 0) {
            if (isset($args['emailShow']) && $args['emailShow'] == true) {
                // Show the email attribute with default value.
                $attribute['default'] = $user->mail;
            } else {
                // Hide the email value
                $attribute['value'] = $user->mail;
                $attribute['handled'] = true;
            }
        } elseif (strcasecmp($attribute['caption'], 'first name') == 0 || strcasecmp($attribute['caption'], 'last name') == 0 || strcasecmp($attribute['caption'], 'surname') == 0) {
            // This would be the case where the warehouse is configured to store these
            // values but there are no matching profile fields
            if (!isset($args['nameShow']) || $args['nameShow'] != true) {
                // Name attributes are not displayed because we have the users login.
                $attribute['handled'] = true;
            }
        }
        // If we have a value for one of the user login attributes then we need to
        // output this value. BUT, for existing data we must not overwrite the user
        // who created the record. Note that we don't do this at the beginning of
        // the method as we still wanted to mark the attributes as handled.
        if (isset($attribute['value']) && !$exists) {
            $hiddens .= '<input type="hidden" name="' . $attribute['fieldname'] . '" value="' . $attribute['value'] . '" />' . "\n";
        }
    }
    return $hiddens;
}
 /**
  * Get the block of sample custom attributes for the recorder
  */
 private static function get_control_recorderdetails($auth, $args, $tabalias, $options)
 {
     // get the sample attributes
     $attrOpts = array('id' => data_entry_helper::$entity_to_load['sample:id'], 'valuetable' => 'sample_attribute_value', 'attrtable' => 'sample_attribute', 'key' => 'sample_id', 'fieldprefix' => 'smpAttr', 'extraParams' => $auth['read'], 'survey_id' => $args['survey_id']);
     // select only the custom attributes that are for this sample method or all sample methods, if this
     // form is for a specific sample method.
     if (!empty($args['sample_method_id'])) {
         $attrOpts['sample_method_id'] = $args['sample_method_id'];
     }
     $attributes = data_entry_helper::getAttributes($attrOpts, false);
     // load values from profile. This is Drupal specific code, so degrade gracefully.
     if (function_exists('profile_load_profile')) {
         global $user;
         profile_load_all_profile($user);
         foreach ($attributes as &$attribute) {
             if (!isset($attribute['default'])) {
                 $attrPropName = 'profile_' . strtolower(str_replace(' ', '_', $attribute['caption']));
                 if (isset($user->{$attrPropName})) {
                     $attribute['default'] = $user->{$attrPropName};
                 } elseif (strcasecmp($attribute['caption'], 'email') === 0 && isset($user->mail)) {
                     $attribute['default'] = $user->mail;
                 }
             }
         }
     }
     $defAttrOptions = array('extraParams' => $auth['read'], 'class' => "required");
     $attrHtml = '';
     // Drupal specific code
     if (!user_access('IForm n' . self::$node->nid . ' enter data by proxy')) {
         if (isset($options['lockable'])) {
             unset($options['lockable']);
         }
         $defAttrOptions += array('readonly' => 'readonly="readonly"');
         $attrHtml .= '<div class="readonlyFieldset">';
     }
     $defAttrOptions += $options;
     $blockOptions = array();
     $attrHtml .= get_attribute_html($attributes, $args, $defAttrOptions, 'Enter data by proxy', $blockOptions);
     if (!user_access('IForm n' . self::$node->nid . ' enter data by proxy')) {
         $attrHtml .= '</div>';
     }
     return $attrHtml;
 }