function user_edit_user_attributes_form

6.x user.module user_edit_user_attributes_form()

File

modules/user/user.module, line 300

Code

function user_edit_user_attributes_form() {

  $form = array();

  $user_id = intval($_REQUEST ['user_id']);
  $student_cwid = @strip_tags($_REQUEST ["student_cwid"]); // now getting it from argument
  $faculty_cwid = @strip_tags($_REQUEST ["faculty_cwid"]); // now getting it from argument
  $de_catalog_year = @strip_tags($_REQUEST ["de_catalog_year"]);
  $user_type = strip_tags($_REQUEST ['user_type']);


  $attributes = user_get_registered_attributes();

  $account = fp_load_user($user_id);

  fp_set_title(t("Edit User Attributes @name", array("@name" => "$account->f_name $account->l_name ($account->cwid)")));


  $form ['user_id'] = array(
    'type' => 'hidden',
    'value' => $user_id,
  );

  // Set up sub tabs.
  $tab_array = array();
  // Figure out what the page's sub-tabs should be, and set them.  

  if ($student_cwid != '') {
    $tab_array [0]["title"] = t("Edit Student");
    $tab_array [0]["active"] = FALSE;
    $tab_array [0]["on_click"] = "window.location=\"" . fp_url("admin/users/edit-student-user", "student_cwid=$student_cwid&de_catalog_year=$de_catalog_year") . "\"";

    $tab_array [1]["title"] = t("Edit Student Courses");
    $tab_array [1]["active"] = FALSE;
    $tab_array [1]["on_click"] = "window.location=\"" . fp_url("admin/users/edit-student-user/courses", "student_cwid=$student_cwid&de_catalog_year=$de_catalog_year") . "\"";

    $tab_array [2]["title"] = t("Edit User Attributes");
    $tab_array [2]["active"] = TRUE;
    $tab_array [2]["on_click"] = "window.location=\"" . fp_url("admin/users/edit-user/attributes", "user_id=$user_id&user_type=$user_type&student_cwid=$student_cwid&de_catalog_year=$de_catalog_year") . "\"";
  }
  else if ($faculty_cwid != '') {

    $tab_array = array();
    $tab_array [0]["title"] = t("Edit Faculty/Staff User");
    $tab_array [0]["active"] = TRUE;
    $tab_array [0]["on_click"] = "window.location=\"" . fp_url("admin/users/edit-user", "faculty_cwid=$faculty_cwid&de_catalog_year=$de_catalog_year") . "\"";

    $tab_array [1]["title"] = t("Edit Faculty Advisees");
    $tab_array [1]["active"] = FALSE;
    $tab_array [1]["on_click"] = "window.location=\"" . fp_url("admin/users/edit-user/advisees", "faculty_cwid=$faculty_cwid&de_catalog_year=$de_catalog_year") . "\"";

    $tab_array [2]["title"] = t("Edit User Attributes");
    $tab_array [2]["active"] = TRUE;
    $tab_array [2]["on_click"] = "window.location=\"" . fp_url("admin/users/edit-user/attributes", "user_id=$user_id&user_type=$user_type&faculty_cwid=$faculty_cwid&de_catalog_year=$de_catalog_year") . "\"";

  }
  fp_set_page_sub_tabs($tab_array);




  $form ['mark_top'] = array(
    'value' => '<p>' . t("The following attributes are available to view/edit, depending on your permission level.") . "</p>",
  );

  $bool_something_to_edit = FALSE;

  foreach ($attributes as $name => $definition) {

    if (!isset($attributes [$name]['fields'])) {
      // Create a "default" field of "value" that's a simple textfield.
      $attributes [$name]['fields']['value'] = array(
        'label' => $attributes [$name]['title'],
        'type' => 'textfield',
      );
    }


    foreach ($attributes [$name]["fields"] as $field_name => $field_details) {
      $ofield_name = $field_name;
      $field_name = $name . "__" . $field_name;


      if (isset($attributes [$name]['settings']['#validate_handlers'])) {
        $form ['#validate_handlers'] += $attributes [$name]['settings']['#validate_handlers'];
      }

      if (isset($attributes [$name]['settings']['#submit_handlers'])) {
        $form ['#submit_handlers'] += $attributes [$name]['settings']['#submit_handlers'];
      }

      // Any js or css to add for this form, defined in the settings?
      if (isset($attributes [$name]['settings']['js'])) {
        fp_add_js($attributes [$name]['settings']['js']);
      }
      if (isset($attributes [$name]['settings']['css'])) {
        fp_add_css($attributes [$name]['settings']['css']);
      }




      if (user_has_permission("edit_attribute_$name")) {
        // We may edit
        $form [$field_name] = $field_details;
        $form [$field_name]['prefix'] = "<h3>" . $definition ['title'] . "</h3>" . $form [$field_name]['prefix'];
        $form [$field_name]['suffix'] .= "<hr>";

        // Set the value properly...
        if ($field_details ['type'] != 'cfieldset') {
          $value = user_get_attribute($user_id, $field_name, '');
          $form [$field_name]['value'] = $value;
          // If this is a datetime-local field, then the value needs to be adjusted for it to work correctly. (it was stored as UTC in database)             
          if ($form [$field_name]['type'] == 'datetime-local') {
            if (trim($value) != '') {
              $form [$field_name]["value"] = date('Y-m-d\TH:i', convert_time(strtotime($value)));
            }
          }
          // Similar to datetime-local, if this is a "time" field, then it has been stored as UTC in the database, and now
          // needs to be converted to local timezone.
          if ($form [$field_name]['type'] == 'time') {
            if (trim($value) != '') {
              $form [$field_name]["value"] = date('H:i', convert_time(strtotime($value)));
            }
          }
        } // is not cfieldset
        else {
          // If this is a fieldset, then we need to assign values to its ELEMENTS.  If not empty, then OPEN the cfieldset!
          foreach ($form [$field_name]['elements'] as $c => $efields) {
            foreach ($efields as $efield_name => $efield_details) {
              $value = user_get_attribute($user_id, $field_name, '');
              $efield_details ['value'] = $value;
              // If this is a datetime-local field, then the value needs to be adjusted for it to work correctly.
              if ($efield_details ['type'] == 'datetime-local') {
                if (trim($value) != '') {
                  $value = date('Y-m-d\TH:i', convert_time(strtotime($value)));
                }
              }
              // Similar to datetime-local, if this is a "time" field, then it has been stored as UTC in the database, and now
              // needs to be converted to local timezone.
              if ($efield_details ['type'] == 'time') {
                if (trim($value) != '') {
                  $value = date('H:i', convert_time(strtotime($value)));
                }
              }
              $form [$field_name]['elements'][$c][$efield_name]["value"] = $value;
              if (trim($value)) {
                $form [$field_name]['start_closed'] = FALSE;
              }
            }
          }

        } // else is fieldset        


        $bool_something_to_edit = TRUE;
      }
      else if (user_has_permission("view_attribute_$name")) {
        // We may only view

        $value = user_get_attribute($user_id, $field_name, '');
        $display_value = filter_markup($value, 'plain');

        // Is there any special way this value is meant to be displayed?
        // Ex: if its a date field, should we change how that gets displayed?
        invoke_hook("alter_user_attribute_display", array($field_name, &$display_value));


        if (isset($definition ['fields'][$field_name]['options'][$value])) {
          $display_value = $definition ['fields'][$field_name]['options'][$value];
        }

        $html = "";
        $html_val = "";
        $details = array();
        if (isset($definition ['display'][$ofield_name])) {
          $details = $definition ['display'][$ofield_name];
        }
        $html .= "<h3>" . $definition ['title'] . "</h3>";
        $html .= "<strong>" . @$details ['label'] . "</strong> ";
        $html .= "<span class='attrib-val'>";

        $html_val .= @$details ['value'];

        $html_val = str_replace("@value", $display_value, $html_val);
        $html_val = str_replace("@key", $value, $html_val);
        if (trim($html_val) == "") {
          $html_val .= $display_value; // meaning, it's empty.
        }

        $html .= "$html_val</span><hr>";
        $form ["mark_$field_name"] = array(
          'value' => $html,
        );

      }

    }

  } // foreach attributes


  if ($bool_something_to_edit) {
    $form ['submit_btn'] = array(
      'type' => 'submit',
      'value' => 'Submit',
      'weight' => 9990,
    );
  }
  return $form;
}