Commit 1f1154db authored by Jerome Mariette's avatar Jerome Mariette
Browse files

better handle of add users

parent a729cfd7
......@@ -76,7 +76,8 @@ class tx_nG6_pi1 extends tslib_pibase {
<script type="text/javascript" src="'.t3lib_extMgm::siteRelPath($this->extKey).'res/js/DT_bootstrap.js"></script>
<script type="text/javascript" src="'.t3lib_extMgm::siteRelPath($this->extKey).'res/js/bootstrap-editable.min.js"></script>
<script type="text/javascript" src="'.t3lib_extMgm::siteRelPath($this->extKey).'res/js/bootstrap-datepicker.js"></script>
<script type="text/javascript" src="'.t3lib_extMgm::siteRelPath($this->extKey).'res/js/bootstrap-typeahead.js"></script>
<script type="text/javascript" src="'.t3lib_extMgm::siteRelPath($this->extKey).'res/js/bootstrap-typeahead.js"></script>
<script type="text/javascript" src="'.t3lib_extMgm::siteRelPath($this->extKey).'res/js/jquery.validate.js"></script>
<script type="text/javascript" src="'.t3lib_extMgm::siteRelPath($this->extKey).'res/js/jquery.url.packed.js"></script>
......
......@@ -172,12 +172,6 @@ table tr.tx-nG6-pi1-line-emphasis td.sorting_1 {background: #d2d2d2;}
list-style-type: none;
overflow: auto;
}
.new_user_error {
color: red;
}
.new_user_valid {
color: green;
}
.ui-menu {
width : 210px;
}
......
This diff is collapsed.
......@@ -26,6 +26,51 @@
* @author PF bioinformatique de Toulouse <>
*/
$.validator.setDefaults({
highlight: function (element, errorClass, validClass) {
var $element;
if ( element.type === "radio" ) {
$element = this.findByName(element.name);
} else {
$element = $(element);
}
$element.addClass(errorClass).removeClass(validClass);
// add the bootstrap error class
$element.parents("div.control-group").addClass("error");
if ($element.parent("div.input-append").find(".btn").length > 0) {
$element.parent("div.input-append").find(".btn").addClass("btn-danger");
$element.parent("div.input-append").find(".icon-wrench").addClass("icon-white");
}
},
unhighlight: function (element, errorClass, validClass) {
var $element;
if (element.type === 'radio') {
$element = this.findByName(element.name);
} else {
$element = $(element);
}
$element.removeClass(errorClass).addClass(validClass);
// remove the bootstrap error class
if ($element.parents("div.control-group").find("." + errorClass).length == 0) {
// Only remove the class if there are no other errors
$element.parents("div.control-group").removeClass(errorClass).addClass(validClass);
}
if ($element.parent("div.input-append").find(".btn").length > 0) {
$element.parent("div.input-append").find(".btn").removeClass("btn-danger");
$element.parent("div.input-append").find(".icon-wrench").removeClass("icon-white");
}
},
errorPlacement: function(error, element) {
if (element.parent("div.input-append").length > 0) {
error.insertAfter(element.parent("div.input-append"));
} else {
error.insertAfter(element);
}
},
errorElement: "span",
errorClass: "help-block error"
});
$(function () {
/* Project table init */
......@@ -583,6 +628,57 @@ $(function () {
$("#ng6modal-DTnoraw").modal();
}
});
/* add user to project option */
$(":button[id=add_user]").click(function(){
......@@ -596,7 +692,7 @@ $(function () {
add_user_html += ' <div class="control-group">';
add_user_html += ' <label class="control-label">Last name</label>';
add_user_html += ' <div class="controls">';
add_user_html += ' <input type="text" id="last_name_val" class="typeahead" data-provide="typeahead" placeholder="Last name" data-items="4" /> ';
add_user_html += ' <input type="text" id="last_name_val" class="typeahead" name="last_name_val" placeholder="Last name"/> ';
add_user_html += ' </div>';
add_user_html += ' </div>';
......@@ -659,6 +755,8 @@ $(function () {
add_user_html += ' </label>';
add_user_html += ' </div>';
add_user_html += ' </div>';
add_user_html += '</form>';
$("#modal-body-tmpl").html(add_user_html);
$("#modal-foot-tmpl").html('<button class="btn" id="raz_form"><i class="icon-repeat"></i> Clear form</a>' +
......@@ -722,11 +820,11 @@ $(function () {
$("#user_password_pwd_val").val(pwd);
$("#user_password_text_val").show();
$("#user_password_text_val").val(pwd);
$("#user_password_text_val").removeAttr('disabled');
});
// empty the form
$("#raz_form").click(function(){
clear_error_msg();
$("#last_name_val").val("");
$("#first_name_val").val("");
$("#username_val").val("");
......@@ -740,12 +838,6 @@ $(function () {
lock_fields(0); // unlock fields
});
// clear error and valid messages
function clear_error_msg(){
$(".new_user_error").remove();
$(".new_user_valid").remove();
}
// lock/unlock fields when autocomplete
function lock_fields(order){
// lock fields
......@@ -768,159 +860,92 @@ $(function () {
$("#user_password_pwd_val").removeAttr('disabled');
}
}
});
$("#modal-btn-adduser").click( function() {
function fields_locked(){
var locked = false;
if($("#username_val").attr('disabled') == 'disabled'){
locked = true;
}
return locked
}
var hasError = false;
var email_regex_pattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
// clean "error" tags
$(".new_user_error").remove();
$(".new_user_valid").remove();
// control fields
if(!fields_locked()){
if($("#first_name_val").val() == ''){
$("#first_name_val").after('<span class="new_user_error">Please enter the first name</span>');
hasError = true;
}
}
if(!fields_locked()){
if($("#last_name_val").val() == ''){
$("#last_name_val").after('<span class="new_user_error">Please enter the last name</span>');
hasError = true;
}
}
if($("#username_val").val() == ''){
$("#username_val").after('<span class="new_user_error">Please enter the login</span>');
hasError = true;
}
if($("#user_group_val").val() == ''){
$("#user_group_val").after('<span class="new_user_error">Please enter the group</span>');
hasError = true;
}
if(!fields_locked()){
if($("#email_val").val() == ''){
$("#email_val").after('<span class="new_user_error">Please enter a correct email</span>');
hasError = true;
} else if( !email_regex_pattern.test($("#email_val").val()) ){ // test email regex
$("#email_val").after('<span class="new_user_error">Please enter a correct email</span>');
hasError = true;
}
}
if($("#user_password_pwd_val").val() == '' && $("#user_password_text_val").val() == ''){
$("#key_gen_btn").after('<span class="new_user_error">Please generate a password</span>');
hasError = true;
} else {
// new user
$("#user_password_text_val").val($("#user_password_pwd_val").val());
}
// send datas
if(!hasError) {
var val_url = "index.php?eID=tx_nG6&type=add_user";
val_url += "&username=" + $("#username_val").val();
val_url += "&first_name=" + $("#first_name_val").val();
val_url += "&last_name=" + $("#last_name_val").val();
val_url += "&password=" + MD5($("#user_password_pwd_val").val());
val_url += "&clearpassword=" + $("#user_password_pwd_val").val();
val_url += "&email=" + $("#email_val").val();
val_url += "&creator=" + $("#user_id").val();
val_url += "&group=" + $("#user_group_val").val();
val_url += "&project_id=" + $("#current_project_id").val();
val_url += "&right=" + $("#cbb_user_right").val();
val_url += "&pid=" + $("#pid").val();
val_url += "&add_user_email=" + escape("Dear ###USER_FIRST_NAME### ###USER_LAST_NAME###,\nYou have now acccess to the ###PROJECT_NAME### project (###PROJECT_DESCRIPTION###) accessible once logged in by the following url: ###PROJECT_LINK###\nThe NG6 team\nPS: this is an automatic e-mail message generated by the NG6 system. Please DO NOT RESPOND to this e-mail because the mail box is unattended.");
val_url += "&add_user_title=[NG6] You have been added to a project";
val_url += "&create_user_email=" + escape("Dear ###USER_FIRST_NAME### ###USER_LAST_NAME###,\nPlease find bellow your login/password required to log into the NG6 system (###PROJECT_LINK###): \n - login: ###USER_LOGIN###\n - password: ###USER_PASSWORD###\nThe NG6 team\nPS: this is an automatic e-mail message generated by the NG6 system. Please DO NOT RESPOND to this e-mail because the mail box is unattended.");
val_url += "&create_user_title=[NG6] Account creation";
val_url += "&from_email=" + $("#from_email").val();
val_url += "&project_url=" + escape(location.href);
val_url += "&send_an_email=" + ($("#send_email_chk").attr("checked") == "checked");
if($("#username_val").attr('disabled') != 'disabled'){
$("#new_member").validate({
rules: {
first_name_val: { required: true },
email_val: { required: true, email: true },
last_name_val: { required: true },
username_val: { required: true },
user_group_val: { required: true },
user_password_pwd_val: { required: true }
},
messages: {
first_name_val: "Enter your firstname",
email_val: "Enter a valid email",
last_name_val: "Enter your lastname",
username_val: "Enter a username",
user_group_val: "Enter a laboratory",
user_password_pwd_val: "Enter a password"
},
submitHandler: function(form) {
var val_url = "index.php?eID=tx_nG6&type=add_user";
val_url += "&username=" + $("#username_val").val();
val_url += "&first_name=" + $("#first_name_val").val();
val_url += "&last_name=" + $("#last_name_val").val();
val_url += "&password=" + MD5($("#user_password_pwd_val").val());
val_url += "&clearpassword=" + $("#user_password_pwd_val").val();
val_url += "&email=" + $("#email_val").val();
val_url += "&creator=" + $("#user_id").val();
val_url += "&group=" + $("#user_group_val").val();
val_url += "&project_id=" + $("#current_project_id").val();
val_url += "&right=" + $("#cbb_user_right").val();
val_url += "&pid=" + $("#pid").val();
val_url += "&add_user_email=" + escape("Dear ###USER_FIRST_NAME### ###USER_LAST_NAME###,\nYou have now acccess to the ###PROJECT_NAME### project (###PROJECT_DESCRIPTION###) accessible once logged in by the following url: ###PROJECT_LINK###\nThe NG6 team\nPS: this is an automatic e-mail message generated by the NG6 system. Please DO NOT RESPOND to this e-mail because the mail box is unattended.");
val_url += "&add_user_title=[NG6] You have been added to a project";
val_url += "&create_user_email=" + escape("Dear ###USER_FIRST_NAME### ###USER_LAST_NAME###,\nPlease find bellow your login/password required to log into the NG6 system (###PROJECT_LINK###): \n - login: ###USER_LOGIN###\n - password: ###USER_PASSWORD###\nThe NG6 team\nPS: this is an automatic e-mail message generated by the NG6 system. Please DO NOT RESPOND to this e-mail because the mail box is unattended.");
val_url += "&create_user_title=[NG6] Account creation";
val_url += "&from_email=" + $("#from_email").val();
val_url += "&project_url=" + escape(location.href);
val_url += "&send_an_email=" + ($("#send_email_chk").attr("checked") == "checked");
$.ajax({
url: val_url,
success: function(val, status, xhr) {
if (val == '1') {
// user has already access to this project
var res_access = '<tr><td id="error_td" colspan="2" class="new_user_error">';
res_access += $("#username_val").val() + ' has already access to this project';
res_access += '</td></tr>';
$("#new_member tbody").append(res_access);
hasError = true;
} else if (val == '4' || val == '3') {
// username exists in database cannot create
var res_access = '<tr><td id="error_td" colspan="2" class="new_user_error">';
res_access += 'Cannot create this user account, ' + $("#username_val").val() + ' already exists';
res_access += '</td></tr>';
$("#new_member tbody").append(res_access);
hasError = true;
} else if (val == '5') {
// email exists in database cannot create
var res_access = '<tr><td id="error_td" colspan="2" class="new_user_error">';
res_access += 'Cannot create this user account, ' + $("#email_val").val() + ' already exists';
res_access += '</td></tr>';
$("#new_member tbody").append(res_access);
hasError = true;
} else {
$("#ng6modal").modal('hide');
if (!endsWith(location.href, "active_tab=users")) {
location.assign(location.href+"&active_tab=users");
} else {
location.assign(location.href);
$.ajax({
url: val_url,
success: function(val, status, xhr) {
if (val == '1') {
// user has already access to this project
var res_access = '<tr><td id="error_td" colspan="2" class="new_user_error">';
res_access += $("#username_val").val() + ' has already access to this project';
res_access += '</td></tr>';
$("#new_member tbody").append(res_access);
hasError = true;
} else if (val == '4' || val == '3') {
// username exists in database cannot create
var res_access = '<tr><td id="error_td" colspan="2" class="new_user_error">';
res_access += 'Cannot create this user account, ' + $("#username_val").val() + ' already exists';
res_access += '</td></tr>';
$("#new_member tbody").append(res_access);
hasError = true;
} else if (val == '5') {
// email exists in database cannot create
var res_access = '<tr><td id="error_td" colspan="2" class="new_user_error">';
res_access += 'Cannot create this user account, ' + $("#email_val").val() + ' already exists';
res_access += '</td></tr>';
$("#new_member tbody").append(res_access);
hasError = true;
} else {
$("#ng6modal").modal('hide');
if (!endsWith(location.href, "active_tab=users")) {
location.assign(location.href+"&active_tab=users");
} else {
location.assign(location.href);
}
}
}
}
}
});
}
});
}
}
});
$("#modal-btn-adduser").click( function() {
$("#new_member").submit();
});
});
});
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment