diff --git a/include/ajax.users.php b/include/ajax.users.php index b17d742a478af318cc7127161b56172aef9d8157..8e31f3922a3be4407b094e1f84685fd667b41505 100644 --- a/include/ajax.users.php +++ b/include/ajax.users.php @@ -87,8 +87,10 @@ class UsersAjaxAPI extends AjaxController { elseif(!($user = User::lookup($id))) Http::response(404, 'Unknown user'); - $info = array('title' => ''); - + $info = array( + 'title' => '', + 'useredit' => sprintf('#users/%d/edit', $user->getId()), + ); ob_start(); echo sprintf('<div style="width:650px; padding: 2px 2px 0 5px;" id="u%d">', $user->getId()); @@ -377,7 +379,7 @@ class UsersAjaxAPI extends AjaxController { } elseif ($orgId) $org = Organization::lookup($orgId); elseif ($org = $user->getOrganization()) { - $info['title'] = $org->getName(); + $info['title'] = sprintf('%s — %s', $user->getName(), 'Organization'); $info['action'] = $info['onselect'] = ''; $tmpl = 'org.tmpl.php'; } diff --git a/include/class.organization.php b/include/class.organization.php index 78332b1e60067de88efac03bd3306e7da0a69dcf..a68060d0f6721e2770e65f2975794bce798ae68b 100644 --- a/include/class.organization.php +++ b/include/class.organization.php @@ -207,6 +207,20 @@ class Organization extends OrganizationModel { return $vars; } + function removeUser($user) { + + if (!$user instanceof User) + return false; + + if (!$user->setOrganization(null, false)) + return false; + + // House cleaning - remove user from org contact..etc + $user->setPrimaryContact(false); + + return $user->save(); + } + function to_json() { $info = array( diff --git a/include/class.orm.php b/include/class.orm.php index b403617b2e71a0b5211d0274eb0942c1fc2e948a..b4f65e551122d6261e80c26effe8f5ce297d3288 100644 --- a/include/class.orm.php +++ b/include/class.orm.php @@ -73,15 +73,23 @@ class VerySimpleModel { $this->ht[$field] = $value; return; } - // XXX: Ensure $value instanceof $j['fkey'][0] - if ($value->__new__) - $value->save(); - // Capture the object under the object's field name - $this->ht[$field] = $value; + if ($value === null) { + // Pass. Set local field to NULL in logic below + } + elseif ($value instanceof $j['fkey'][0]) { + if ($value->__new__) + $value->save(); + // Capture the object under the object's field name + $this->ht[$field] = $value; + $value = $value->get($j['fkey'][1]); + // Fall through to the standard logic below + } + else + throw new InvalidArgumentException( + 'Expecting NULL or instance of ' . $j['fkey'][0]); + // Capture the foreign key id value $field = $j['local']; - $value = $value->get($j['fkey'][1]); - // Fall through to the standard logic below } // XXX: Fully support or die if updating pk // XXX: The contents of $this->dirty should be the value after the diff --git a/include/class.user.php b/include/class.user.php index 21e7d99c0e0955346de6fa043f38b4785ae25a4d..c169c48cca060cfc019c781e45de1e6620d0f134 100644 --- a/include/class.user.php +++ b/include/class.user.php @@ -110,10 +110,9 @@ class UserModel extends VerySimpleModel { } function setOrganization($org, $save=true) { - if (!$org instanceof Organization) - return false; $this->set('org', $org); + if ($save) $this->save(); diff --git a/include/staff/templates/user.tmpl.php b/include/staff/templates/user.tmpl.php index 4776bf4a3946f42a6a7e59f3d240c5cb6b0cd2b1..52f5f9fc7fc44b455649cd35bdccf7354b4406ea 100644 --- a/include/staff/templates/user.tmpl.php +++ b/include/staff/templates/user.tmpl.php @@ -48,7 +48,7 @@ if ($info['error']) { <div class="tab_content" id="info-tab"> <div class="floating-options"> - <a href="#" id="edituser" class="action" title="Edit"><i class="icon-edit"></i></a> + <a href="<?php echo $info['useredit'] ?: '#'; ?>" id="edituser" class="action" title="Edit"><i class="icon-edit"></i></a> <a href="users.php?id=<?php echo $user->getId(); ?>" title="Manage User" class="action"><i class="icon-share"></i></a> </div> @@ -141,8 +141,18 @@ if ($ticket && $ticket->getOwnerId() == $user->getId()) $(function() { $('a#edituser').click( function(e) { e.preventDefault(); - $('div#user-profile').hide(); - $('div#user-form').fadeIn(); + if ($(this).attr('href').length > 1) { + var url = 'ajax.php/'+$(this).attr('href').substr(1); + $.dialog(url, [201, 204], function (xhr) { + window.location.href = window.location.href; + }, { + onshow: function() { $('#user-search').focus(); } + }); + } else { + $('div#user-profile').hide(); + $('div#user-form').fadeIn(); + } + return false; }); diff --git a/include/staff/templates/users.tmpl.php b/include/staff/templates/users.tmpl.php index d5beaf2c77fdcd50d8dde8bd189a84e7acf4e0e9..42ad7e6f1d1317df697f492cb7a9bb5a0949cfee 100644 --- a/include/staff/templates/users.tmpl.php +++ b/include/staff/templates/users.tmpl.php @@ -64,13 +64,15 @@ else <br/> <?php if ($num) { ?> -<form action="users.php" method="POST" name="staff" > +<form action="orgs.php?id=<?php echo $org->getId(); ?>" method="POST" name="users" > <?php csrf_token(); ?> <input type="hidden" name="do" value="mass_process" > + <input type="hidden" id="id" name="id" value="<?php echo $org->getId(); ?>" > <input type="hidden" id="action" name="a" value="" > <table class="list" border="0" cellspacing="1" cellpadding="0" width="940"> <thead> <tr> + <th width="7px"> </th> <th width="350"> Name</th> <th width="300"> Email</th> <th width="100"> Status</th> @@ -90,6 +92,10 @@ if ($num) { ?> $sel=true; ?> <tr id="<?php echo $row['id']; ?>"> + <td width=7px> + <input type="checkbox" class="ckb" name="ids[]" + value="<?php echo $row['id']; ?>" <?php echo $sel?'checked="checked"':''; ?> > + </td> <td> <a class="userPreview" href="users.php?id=<?php echo $row['id']; ?>"><?php echo $name; ?></a> @@ -107,16 +113,61 @@ if ($num) { ?> } //end of while. endif; ?> </tbody> + <tfoot> + <tr> + <td colspan="5"> + <?php + if ($res && $num) { + ?> + Select: + <a id="selectAll" href="#ckb">All</a> + <a id="selectNone" href="#ckb">None</a> + <a id="selectToggle" href="#ckb">Toggle</a> + <?php + } else { + echo 'No users found'; + } + ?> + </td> + </tr> + </tfoot> </table> <?php -if($res && $num): //Show options.. +if ($res && $num) { //Show options.. echo '<div> Page:'.$pageNav->getPageLinks().' </div>'; -endif; + + ?> + <p class="centered" id="actions"> + <input class="button" type="submit" name="remove-users" value="Remove" > + </p> +<?php +} ?> </form> <?php } ?> +<div style="display:none;" class="dialog" id="confirm-action"> + <h3>Please Confirm</h3> + <a class="close" href=""><i class="icon-remove-circle"></i></a> + <hr/> + <p class="confirm-action" style="display:none;" id="remove-users-confirm"> + Are you sure want to <b>REMOVE</b> selected user from <strong><?php + echo $org->getName(); ?></strong> organization? + </p> + <div>Please confirm to continue.</div> + <hr style="margin-top:1em"/> + <p class="full-width"> + <span class="buttons" style="float:left"> + <input type="button" value="No, Cancel" class="close"> + </span> + <span class="buttons" style="float:right"> + <input type="button" value="Yes, Do it!" class="confirm"> + </span> + </p> + <div class="clear"></div> +</div> + <script type="text/javascript"> $(function() { $(document).on('click', 'a.add-user', function(e) { diff --git a/scp/orgs.php b/scp/orgs.php index 83a13fb0ba712365295341d5510ad37d2c51822c..373864afd09b30f321af48d20890ea197d32538b 100644 --- a/scp/orgs.php +++ b/scp/orgs.php @@ -33,6 +33,27 @@ if ($_POST) { else $errors['err'] = $status; break; + case 'remove-users': + if (!$org) + $errors['err'] = ' Trying to remove users from unknown + organization'; + elseif (!$_POST['ids'] || !is_array($_POST['ids']) || !count($_POST['ids'])) { + $errors['err'] = 'You must select at least one user to remove'; + } else { + $i = 0; + foreach ($_POST['ids'] as $k=>$v) { + if (($u=User::lookup($v)) && $org->removeUser($u)) + $i++; + } + $num = count($_POST['ids']); + if ($i && $i == $num) + $msg = 'Selected users removed successfully'; + elseif ($i > 0) + $warn = "$i of $num selected users removed"; + elseif (!$errors['err']) + $errors['err'] = 'Unable to remove selected users'; + } + break; default: $errors['err'] = 'Unknown action'; }