Skip to content
Snippets Groups Projects
Commit 591c37a5 authored by Peter Rotich's avatar Peter Rotich
Browse files

Add users to organization

Support adding existing or new users while on organization page.
parent 9ec863cf
No related branches found
No related tags found
No related merge requests found
...@@ -101,6 +101,57 @@ class OrgsAjaxAPI extends AjaxController { ...@@ -101,6 +101,57 @@ class OrgsAjaxAPI extends AjaxController {
include(STAFFINC_DIR . 'templates/org-delete.tmpl.php'); include(STAFFINC_DIR . 'templates/org-delete.tmpl.php');
} }
function addUser($id, $userId=0) {
global $thisstaff;
if (!$thisstaff)
Http::response(403, 'Login Required');
elseif (!($org = Organization::lookup($id)))
Http::response(404, 'Unknown organization');
$info = array();
$info['title'] = 'Add User';
$info['action'] = '#orgs/'.$org->getId().'/add-user';
$info['onselect'] = 'ajax.php/orgs/'.$org->getId().'/add-user/';
$info['lookup'] = false;
if (AuthenticationBackend::getSearchDirectories())
$info['lookup'] = 'remote';
if ($_POST) {
if ($_POST['id']) { //Existing useer
if (!($user = User::lookup($_POST['id'])))
$info['error'] = 'Unknown user selected';
elseif ($user->getOrgId() == $org->getId())
$info['error'] = sprintf('%s already belongs to the organization',
Format::htmlchars($user->getName()));
} else { //Creating new user
$form = UserForm::getUserForm()->getForm($_POST);
if (!($user = User::fromForm($form)))
$info['error'] = 'Error adding user - try again!';
}
if (!$info['error'] && $user && $user->setOrganization($org))
Http::response(201, $user->to_json());
elseif (!$info['error'])
$info['error'] = 'Unable to add user to the organization - try again';
} elseif ($userId) //Selected local user
$user = User::lookup($userId);
if ($user && $user->getOrgId()) {
if ($user->getOrgId() == $org->getId())
$info['warn'] = 'User already belongs to this organization!';
else
$info['warn'] = "Are you sure you want to change the user's organization?";
}
ob_start();
include(STAFFINC_DIR . 'templates/user-lookup.tmpl.php');
$resp = ob_get_contents();
ob_end_clean();
return $resp;
}
function addOrg() { function addOrg() {
......
...@@ -15,6 +15,8 @@ if (!isset($info['lookup']) || $info['lookup'] !== false) { ?> ...@@ -15,6 +15,8 @@ if (!isset($info['lookup']) || $info['lookup'] !== false) { ?>
if ($info['error']) { if ($info['error']) {
echo sprintf('<p id="msg_error">%s</p>', $info['error']); echo sprintf('<p id="msg_error">%s</p>', $info['error']);
} elseif ($info['warn']) {
echo sprintf('<p id="msg_warning">%s</p>', $info['warn']);
} elseif ($info['msg']) { } elseif ($info['msg']) {
echo sprintf('<p id="msg_notice">%s</p>', $info['msg']); echo sprintf('<p id="msg_notice">%s</p>', $info['msg']);
} ?> } ?>
...@@ -24,9 +26,14 @@ if ($info['error']) { ...@@ -24,9 +26,14 @@ if ($info['error']) {
<i class="icon-user icon-4x pull-left icon-border"></i> <i class="icon-user icon-4x pull-left icon-border"></i>
<a class="action-button pull-right" style="overflow:inherit" <a class="action-button pull-right" style="overflow:inherit"
id="unselect-user" href="#"><i class="icon-remove"></i> Add New User</a> id="unselect-user" href="#"><i class="icon-remove"></i> Add New User</a>
<div><strong id="user-name"><?php echo $user ? Format::htmlchars($user->getName()->getOriginal()) : ''; ?></strong></div>
<div>&lt;<span id="user-email"><?php echo $user ? $user->getEmail() : ''; ?></span>&gt;</div>
<?php if ($user) { ?> <?php if ($user) { ?>
<div><strong id="user-name"><?php echo Format::htmlchars($user->getName()->getOriginal()); ?></strong></div>
<div>&lt;<span id="user-email"><?php echo $user->getEmail(); ?></span>&gt;</div>
<?php
if ($org=$user->getOrganization()) { ?>
<div><span id="user-org"><?php echo $org->getName(); ?></span></div>
<?php
} ?>
<table style="margin-top: 1em;"> <table style="margin-top: 1em;">
<?php foreach ($user->getDynamicData() as $entry) { ?> <?php foreach ($user->getDynamicData() as $entry) { ?>
<tr><td colspan="2" style="border-bottom: 1px dotted black"><strong><?php <tr><td colspan="2" style="border-bottom: 1px dotted black"><strong><?php
...@@ -99,6 +106,7 @@ $(function() { ...@@ -99,6 +106,7 @@ $(function() {
$('a#unselect-user').click( function(e) { $('a#unselect-user').click( function(e) {
e.preventDefault(); e.preventDefault();
$("#msg_error, #msg_notice, #msg_warning").fadeOut();
$('div#selected-user-info').hide(); $('div#selected-user-info').hide();
$('div#new-user-form').fadeIn({start: function(){ $('#user-search').focus(); }}); $('div#new-user-form').fadeIn({start: function(){ $('#user-search').focus(); }});
return false; return false;
......
...@@ -22,6 +22,11 @@ if ($info['error']) { ...@@ -22,6 +22,11 @@ if ($info['error']) {
<div><b><a href="#" id="edituser"><i class="icon-edit"></i>&nbsp;<?php <div><b><a href="#" id="edituser"><i class="icon-edit"></i>&nbsp;<?php
echo Format::htmlchars($user->getName()->getOriginal()); ?></a></b></div> echo Format::htmlchars($user->getName()->getOriginal()); ?></a></b></div>
<div>&lt;<?php echo $user->getEmail(); ?>&gt;</div> <div>&lt;<?php echo $user->getEmail(); ?>&gt;</div>
<?php
if (($org=$user->getOrganization())) { ?>
<div><?php echo $org->getName(); ?></div>
<?php
} ?>
<table style="margin-top: 1em;"> <table style="margin-top: 1em;">
<?php foreach ($user->getDynamicData() as $entry) { <?php foreach ($user->getDynamicData() as $entry) {
?> ?>
......
...@@ -117,8 +117,8 @@ endif; ...@@ -117,8 +117,8 @@ endif;
$(function() { $(function() {
$(document).on('click', 'a.add-user', function(e) { $(document).on('click', 'a.add-user', function(e) {
e.preventDefault(); e.preventDefault();
$.userLookup('ajax.php/users/add', function (user) { $.userLookup('ajax.php/orgs/<?php echo $org->getId(); ?>/add-user', function (user) {
window.location.href = 'users.php?id='+user.id; window.location.href = 'orgs.php?id=<?php echo $org->getId(); ?>'
}); });
return false; return false;
......
...@@ -103,6 +103,8 @@ $dispatcher = patterns('', ...@@ -103,6 +103,8 @@ $dispatcher = patterns('',
url_post('^/add$', 'addOrg'), url_post('^/add$', 'addOrg'),
url_get('^/select$', 'selectOrg'), url_get('^/select$', 'selectOrg'),
url_get('^/select/(?P<id>\d+)$', 'selectOrg'), url_get('^/select/(?P<id>\d+)$', 'selectOrg'),
url_get('^/(?P<id>\d+)/add-user(?:/(?P<userid>\d+))?$', 'addUser'),
url_post('^/(?P<id>\d+)/add-user$', 'addUser'),
url_get('^/(?P<id>\d+)/delete$', 'delete'), url_get('^/(?P<id>\d+)/delete$', 'delete'),
url_delete('^/(?P<id>\d+)/delete$', 'delete') url_delete('^/(?P<id>\d+)/delete$', 'delete')
)), )),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment