accounts.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <html>
  2. <?php include("header.php") ?>
  3. <div class="sidebar">
  4. <button class="add-account-modal button"><i class="fa fa-plus"></i> Add New Account</button>
  5. </div>
  6. <div class="content">
  7. <div id="message-center"></div>
  8. <table>
  9. <tr>
  10. <th>Id</th>
  11. <th style="width: 160px;">Display Name</th>
  12. <th style="width: 160px;">Subdomain</th>
  13. <th style="width: 120px;">Actions</th>
  14. </tr>
  15. <?php foreach($accounts as $account) : ?>
  16. <tr class="display-row">
  17. <td><?=$account['client_id']?></td>
  18. <td><?=$account['display_name']?></td>
  19. <td><?=$account['subdomain']?></td>
  20. <td><button class="edit button" data-client="<?=$account['client_id']?>">Edit</button></td>
  21. </tr>
  22. <tr class="edit-row">
  23. <td><?=$account['client_id']?></td>
  24. <td><input type="text" name="display_name" value="<?=$account['display_name']?>" /></td>
  25. <td><input type="text" name="subdomain" value="<?=$account['subdomain']?>" /></td>
  26. <td>
  27. <button class="save button" data-client="<?=$account['client_id']?>">Save</button>
  28. <button class="cancel button">Cancel</button>
  29. </td>
  30. </tr>
  31. <?php endforeach; ?>
  32. </table>
  33. </div>
  34. <div class="modal-overlay"></div>
  35. <div class="modal-popup"><button class="modal-close">X</button>
  36. <h1>Add Account</h1>
  37. <hr />
  38. <div class="modal-message"></div>
  39. <form autocomplete="off">
  40. <div class="input-row centered"><input name="display_name" type="text" placeholder="display name" autocomplete="off" /></div>
  41. <div class="input-row centered"><input name="subdomain" type="text" placeholder="subdomain" autocomplete="off" /></div>
  42. <!--<div class="input-row centered"><input name="password_confirm" type="password" placeholder="password (confirm)" /></div>-->
  43. <div class="input-row centered"><button class="add-account">Add Account</button></div>
  44. </form>
  45. </div>
  46. </body>
  47. <script type="text/javascript">
  48. document.addEventListener("DOMContentLoaded", function(event) {
  49. bindButtons();
  50. <?php if($superadmin): ?>
  51. superadminNotice();
  52. <?php endif; ?>
  53. });
  54. function bindButtons() {
  55. var addUserModalButton = document.getElementsByClassName("add-account-modal")[0];
  56. addUserModalButton.addEventListener("click", addAccountModalButton_click);
  57. var addAccountButton = document.getElementsByClassName("add-account")[0];
  58. addAccountButton.addEventListener("click", addAccountButton_click);
  59. var editButtons = document.getElementsByClassName("edit");
  60. for(var i = 0; i < editButtons.length; i++) {
  61. editButtons[i].addEventListener("click", editButton_click);
  62. }
  63. var cancelButtons = document.getElementsByClassName("cancel");
  64. for(var i = 0; i < cancelButtons.length; i++) {
  65. cancelButtons[i].addEventListener("click", cancelButton_click);
  66. }
  67. var saveButtons = document.getElementsByClassName("save");
  68. for(var i = 0; i < saveButtons.length; i++) {
  69. saveButtons[i].addEventListener("click", saveButton_click);
  70. }
  71. var overlay = document.getElementsByClassName("modal-overlay")[0];
  72. overlay.addEventListener("click", closeModal_click);
  73. var closeModal = document.getElementsByClassName("modal-close")[0];
  74. closeModal.addEventListener("click", closeModal_click);
  75. }
  76. function addAccountModalButton_click(event) {
  77. var overlay = document.getElementsByClassName("modal-overlay")[0];
  78. var popup = document.getElementsByClassName("modal-popup")[0];
  79. //TODO: clear the modal form
  80. overlay.classList.add("make-visible");
  81. popup.classList.add("make-visible");
  82. }
  83. function closeModal_click(event) {
  84. var overlay = document.getElementsByClassName("modal-overlay")[0];
  85. var popup = document.getElementsByClassName("modal-popup")[0];
  86. overlay.classList.remove("make-visible");
  87. popup.classList.remove("make-visible");
  88. }
  89. function addAccountButton_click(event) {
  90. event.preventDefault();
  91. var form = this.parentNode.parentNode;
  92. var inputFields = form.getElementsByTagName("input");
  93. var subdomain = inputFields.subdomain.value;
  94. var displayName = inputFields.display_name.value;
  95. if(!validateAddAccountFields(subdomain, displayName)) {
  96. return;
  97. }
  98. var postData = {};
  99. postData.subdomain = subdomain;
  100. postData.display_name = displayName;
  101. var promise = ajaxPost("/api/addaccount", postData);
  102. promise.success = function(response) {
  103. var message = document.getElementsByClassName("modal-message")[0];
  104. if(response.hasOwnProperty("error")) {
  105. message.innerHTML = response.error;
  106. if(response.hasOwnProperty("exception")) {
  107. console.warn(response.exception);
  108. }
  109. } else {
  110. location.reload();
  111. }
  112. };
  113. promise.failure = function(response) {
  114. console.warn("Communication failure", response);
  115. };
  116. }
  117. function validateAddAccountFields(subdomain, displayName) {
  118. var message = document.getElementsByClassName("modal-message")[0];
  119. re = new RegExp(/^((?:(?:(?:\w[\-\+]?)*)\w)+)((?:(?:(?:\w[\-\+]?){0,62})\w)+)$/);
  120. if(subdomain.trim() != subdomain || encodeURIComponent(subdomain) != subdomain || !subdomain.match(re)) {
  121. message.innerHTML = "subdomain contains invalid characters";
  122. return false;
  123. }
  124. if(subdomain == "admin") {
  125. message.innerHTML = "reserved subdomain";
  126. return false;
  127. }
  128. if(displayName.length < 3) {
  129. message.innerHTML = "display name is too short";
  130. return false;
  131. }
  132. if(displayName.trim() != displayName) {
  133. message.innerHTML = "display name contains invalid characters";
  134. return false;
  135. }
  136. return true;
  137. }
  138. function editButton_click(event) {
  139. this.parentNode.parentNode.style="display: none";
  140. this.parentNode.parentNode.nextElementSibling.style="display: table-row";
  141. }
  142. function cancelButton_click(event) {
  143. this.parentNode.parentNode.style="display: none";
  144. this.parentNode.parentNode.previousElementSibling.style="display: table-row";
  145. }
  146. function saveButton_click(event) {
  147. // this.parentNode.parentNode.style="display: none";
  148. // this.parentNode.parentNode.previousElementSibling.style="display: table-row";
  149. var clientId = this.getAttribute("data-client");
  150. var inputFields = this.parentNode.parentNode.getElementsByTagName("input");
  151. console.log(this.parentNode.parentNode);
  152. var subdomain = inputFields.subdomain.value;
  153. var displayName = inputFields.display_name.value;
  154. var postData = {};
  155. postData.client_id = clientId;
  156. postData.subdomain = subdomain;
  157. postData.display_name = displayName;
  158. var promise = ajaxPost("/api/editaccount", postData);
  159. promise.success = function(response) {
  160. //TODO: instead of reload, update display section
  161. location.reload();
  162. };
  163. promise.failure = function(response) {
  164. console.warn("Communication failure", response);
  165. };
  166. }
  167. </script>
  168. </html>