<?php # vim: set filetype=php expandtab tabstop=2 shiftwidth=2 autoindent smartindent:

/*
bug in _form_builder_handle_input_element() function in form.inc at line when doing "$form['#attributes']['disabled'] = 'disable';"
Fatal error: Cannot use string offset as an array in /data/projects/voip_support/trunk/control_interface/includes/form.inc on line 965

*/


function user_queue_member_admin($form, $form_state = NULL) {
  if(module_exists("profile")) {
    $default_value = FALSE;
    if($field = _user_queue_member_get_profile_field()) {
      $default_value = $field->name." [".$field->fid."]";
    }
    $form["profile_field"] = array(
      "#type" => "textfield"
      ,"#title" => t("Phone number field")
      ,"#autocomplete_path" => "admin/user/user_queue_member/autocomplete"
      ,"#required" => FALSE
      ,"#default_value" => $default_value
    );
  } else {
    $form["profile_field"] = array(
      "#value" => t("Enable profile module to send calls to user phone number.")
      ,"#title" => t("Phone number field")
    );
  }
  $form["save"] = array(
    "#type" => "submit"
    ,"#value" => "Save"
  );
  return $form;
}

function user_queue_member_admin_validate($form, &$form_state) {
  if($form_state["values"]["profile_field"]) {
    /* parse the profile field */
    if(!preg_match("^.*\[([0-9]+)\]$", $form_state["profile_field"], $matches)) {
      form_error($form["profile_field"], "Profile field could not be identified, you can keep it blank if you do not want to use phone number.");
      return;
    }
    /* see if the fid exists */
    if(!$field = db_fetch_object(db_query('SELECT f.name, f.fid FROM {profile_fields} f WHERE f.fid = %d', $matches[1]))) {
      form_error($form["profile_field"], "Profile field does not exist, you can keep it blank if you do not want to use phone number.");
      return;
    }
    $form_state["fid"] = $field->fid;
  }
}

function user_queue_member_admin_submit($form, &$form_state) {
  if($form_submit["fid"]) {
      variable_set(USER_QUEUE_MEMBER_PHONE_NUMBER, $form_submit["fid"]);
  }
}

function user_queue_member_admin_autocomplete($string) {
  $matches = array();
  $result = db_query_range("SELECT f.name, f.fid FROM {profile_fields} AS f WHERE LOWER(f.name) LIKE LOWER('%s%%')", $string, 0, 10);
  while ($data = db_fetch_object($result)) {
    $matches[$data->name." [".$data->fid."]"] = check_plain($data->name);
  }
  drupal_json($matches);
}
