Skip to content

#2820 [User/Evaluator] add: creation of user when multientity and creation of user inside evaluator model #4464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion js/digiriskdolibarr.min.js

Large diffs are not rendered by default.

66 changes: 66 additions & 0 deletions js/modules/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ window.digiriskdolibarr.evaluator.init = function() {
window.digiriskdolibarr.evaluator.event = function() {
$( document ).on( 'click', '.evaluator-create', window.digiriskdolibarr.evaluator.createEvaluator );
$( document ).on( 'change', '#fk_user_employer', window.digiriskdolibarr.evaluator.selectUser );

$( document ).on( 'click', '#openCreateUser', window.digiriskdolibarr.evaluator.openCreateUser );
$( document ).on( 'click', '.user-create .wpeo-button', window.digiriskdolibarr.evaluator.createUser );
};

/**
Expand Down Expand Up @@ -143,3 +146,66 @@ window.digiriskdolibarr.evaluator.createEvaluator = function ( event ) {
});

};

/**
* Action open create user.
* @since 21.0.0
* @version 21.0.0
*
* @param {ClickEvent} event L'état du clic.
* @return {void}
*/
window.digiriskdolibarr.evaluator.openCreateUser = function( event ) {
event.preventDefault();
event.stopPropagation();

let $box = $(document).find('.user-create');

$box.fadeIn();

$box.find('input:not([type="hidden"])').first().focus();
}

/**
* Action create user.
* @since 21.0.0
* @version 21.0.0
* @param {ClickEvent} event L'état du clic.
* @return {void}
* */
window.digiriskdolibarr.evaluator.createUser = function( event ) {
$this = $(this);
let values = {};

$this.closest('.user-create').find('input, select').each(function() {
let name = $(this).attr('name');
if (name) {
values[name] = $(this).val();
}
});

let token = window.saturne.toolbox.getToken()
$.ajax({
url: document.URL + '&action=create_user&token='+token,
type: "POST",
data: JSON.stringify(values),
contentType: "application/json",
success: function (resp) {
$.jnotify($(resp).find('#actionMessage').val(), {
type: $(resp).find('#actionNewUserId').val() > 0 ? 'success' : 'error',
sticky: false,
position: 'top-right',
})
if ($(resp).find('#actionNewUserId').val() <= 0) {
return;
}
let newOption = new Option(values.lastname + ' ' + values.firstname, $(resp).find('#actionNewUserId').val(), true, true);
let $select = $('#fk_user_employer');

$select.append(newOption);
$select.trigger('change');

$this.closest('.user-create').fadeOut();
},
});
}
69 changes: 68 additions & 1 deletion view/digiriskelement/digiriskelement_evaluator.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,43 @@
$userID = $data['userID'];
$usertmp->fetch($userID);
}

if ( ! $error && $action == 'create_user' && $permissiontoadd) {

$data = json_decode(file_get_contents('php://input'), true);

$userTmp = new User($db);

$userTmp->lastname = $data['lastname'];
$userTmp->firstname = $data['firstname'];
$userTmp->login = $userTmp->firstname . $userTmp->lastname;
$userTmp->email = preg_replace('/\s+/', '', $data['email']);
$userTmp->job = $data['job'] ?? '';

// Fill array 'array_options' with data from add form
$ret = $extrafields->setOptionalsFromPost(null, $userTmp);
if ($ret < 0) {
$error++;
}

// Set entity property
$userTmp->entity = getDolGlobalInt('MULTICOMPANY_TRANSVERSE_MODE') ? 1 : $conf->entity;

$db->begin();

$actionNewUserId = $user->create($userTmp);
if ($actionNewUserId > 0) {
$userTmp->SetInGroup($data['groupid'], $conf->entity);

$actionMessage = $langs->trans("UserCreated");
$db->commit();
} else {
$langs->load("errors");
$db->rollback();
$actionMessage = $langs->trans($object->error);
}
}
$action = '';
}

/*
Expand All @@ -261,6 +298,11 @@

print '<div id="cardContent" value="">';

if (!empty($actionNewUserId)) {
print '<input type="hidden" id="actionNewUserId" value="' . $actionNewUserId . '">';
print '<input type="hidden" id="actionMessage" value="' . $actionMessage . '">';
}

if ($object->id > 0 || $fromid > 0) {
$res = $object->fetch_optionals();

Expand Down Expand Up @@ -460,7 +502,7 @@
print '<table><tr>';
print '<td>';
print $form->selectarray('fk_user_employer', $userlist, (GETPOST('userid') ? GETPOST('userid') : $usertmp->id), $langs->trans('SelectUser'), null, null, null, "40%", 0, 0, '', 'minwidth300', 1);
print ' <a href="' . dol_buildpath('custom/digiriskdolibarr/view/digiriskusers.php?backtopage=' . urlencode($_SERVER["PHP_SELF"] . '?id=' . $id . '&userid=USERID&modalactive=1&job=JOB') . '#addUser', 1) . '"><span class="fa fa-plus-circle valignmiddle paddingleft" title="' . $langs->trans("AddUser") . '"></span></a>';
print '<span class="fa fa-plus-circle valignmiddle paddingleft" id="openCreateUser" title="' . $langs->trans("AddUser") . '"></span>';
print '</td></tr></table>';
?>
</div>
Expand All @@ -479,6 +521,31 @@
</div>
</div>
</div>

<div class="user-create wpeo-gridlayout grid-6" style="display: none;">
<input type="hidden" name="action" value="create_user" />
<div class="table-cell table-150">
<input type="text" id="lastname" placeholder="<?php echo $langs->trans('LastName'); ?>" name="lastname" value="<?php echo dol_escape_htmltag(GETPOST('lastname')); ?>" />
</div>
<div class="table-cell table-150">
<input type="text" id="firstname" placeholder="<?php echo $langs->trans('FirstName'); ?>" name="firstname" value="<?php echo dol_escape_htmltag(GETPOST('firstname')); ?>" />
</div>
<div class="table-cell table-300">
<input style="width:100%" type="email" id="email" class="email" placeholder="<?php echo $langs->trans('Email') ; ?>" name="email" value="<?php echo GETPOST('email'); ?>" />
</div>
<div class="table-cell table-150">
<input type="text" id="job" placeholder="<?php echo $langs->trans('PostOrFunction'); ?>" name="job" value="<?php echo dol_escape_htmltag(GETPOST('job')); ?>" />
</div>
<div class="table-cell table-300">
<?php echo $form->select_dolgroups(getDolGlobalInt('DIGIRISKDOLIBARR_READERGROUP_SET'), 'groupid', 1, '', 0, '', array(), (string) $conf->entity, false, 'minwidth100imp widthcentpercentminusxx groupselectcontact'); ?>
</div>
<div class="table-cell">
<button type="button" class="wpeo-button button-square-50 button-blue wpeo-tooltip-event" aria-label="<?php echo $langs->trans('CreateUser'); ?>">
<i class="button-icon fas fa-plus"></i>
</button>
</div>
</div>

</div>
<!-- Modal-Footer -->
<div class="modal-footer">
Expand Down
18 changes: 8 additions & 10 deletions view/digiriskusers.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@
}

// Set entity property
$object->entity = $conf->entity;
$object->entity = getDolGlobalInt('MULTICOMPANY_TRANSVERSE_MODE') ? 1 : $conf->entity;

$db->begin();

Expand All @@ -254,7 +254,8 @@
if (GETPOST('password')) {
$newpassword = $object->setPassword($user, GETPOST('password'));
}
$object->SetInGroup($group, $conf->entity);

$object->SetInGroup(GETPOSTINT('groupid'), $conf->entity);

if ($newpassword < 0) {
// Echec
Expand Down Expand Up @@ -785,7 +786,7 @@

print "</form>\n";

if ($permissiontoadd && (empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE) || $conf->entity == 1)) {
if ($permissiontoadd) {
print '<form action="' . $_SERVER['PHP_SELF'] . '" method="POST" name="createuser">';
print '<input type="hidden" name="token" value="' . newToken() . '">';
print '<input type="hidden" name="action" value="add">';
Expand Down Expand Up @@ -818,6 +819,9 @@
<div class="table-cell table-150">
<input type="text" id="job" placeholder="<?php echo $langs->trans('PostOrFunction'); ?>" name="job" value="<?php echo dol_escape_htmltag(GETPOST('job')); ?>" />
</div>
<div class="table-cell table-300">
<?php echo $form->select_dolgroups($group, 'groupid', 1, '', 0, '', array(), (string) $conf->entity, false, 'minwidth100imp widthcentpercentminusxx groupselectcontact'); ?>
</div>
<div class="table-cell">
<input type="submit" id="createuseraction" name="createuseraction" style="display : none">
<label for="createuseraction">
Expand All @@ -837,13 +841,7 @@
$action = '';
print '</form>';
print '</table></tr>';
} else { ?>
<div class="wpeo-notice notice-info">
<div class="notice-content">
<div class="notice-subtitle"><?php echo $langs->trans("MulticompanyTransverseModeEnabled"); ?></div>
</div>
</div>
<?php }
}

print '</div>';

Expand Down