1 00:00:00,511 --> 00:00:03,464 Before we add and edit the books themselves, 2 00:00:03,464 --> 00:00:07,902 we're going to want to allow administrators to edit all the books. 3 00:00:07,902 --> 00:00:10,954 So we need a way to set administrators. 4 00:00:10,954 --> 00:00:14,668 We'll want to make sure that an administrator is able to promote 5 00:00:14,668 --> 00:00:17,840 other users to an administrator as well. 6 00:00:17,840 --> 00:00:21,590 We'll need to add this functionality in two steps. 7 00:00:21,590 --> 00:00:27,401 First, we need to create a user list with promote and demote buttons. 8 00:00:27,401 --> 00:00:31,874 When we have those wired up, we'll promote ourselves to an administrator. 9 00:00:31,874 --> 00:00:32,833 After that, 10 00:00:32,833 --> 00:00:37,807 we'll lock down the administrator panel to only administrators. 11 00:00:37,807 --> 00:00:41,755 Let's create a new file, 12 00:00:41,755 --> 00:00:46,425 we'll name this admin.php. 13 00:00:46,425 --> 00:00:50,974 We're going to copy everything from add.php as our base. 14 00:00:53,947 --> 00:00:55,942 Now we can make our changes. 15 00:00:55,942 --> 00:01:01,754 We'll change our header to be Admin, and we'll remove the rest of the content. 16 00:01:04,384 --> 00:01:08,531 We're going to add a new panel with a user table. 17 00:01:08,531 --> 00:01:12,278 You can grab this code from the notes attached to this video. 18 00:01:18,971 --> 00:01:23,723 Within our table body, we want to loop through each user and 19 00:01:23,723 --> 00:01:26,576 display the correct information, 20 00:01:26,576 --> 00:01:31,249 along with a button to either promote or demote that user. 21 00:01:31,249 --> 00:01:36,172 There are two functions we'll be using in the functions_users file, 22 00:01:36,172 --> 00:01:37,737 so let's take a look. 23 00:01:37,737 --> 00:01:41,039 The first function is named getAllUsers, and 24 00:01:41,039 --> 00:01:45,282 it simply returns an array of all the users in the database. 25 00:01:45,282 --> 00:01:50,792 The other function we'll be using is named changeRole, 26 00:01:50,792 --> 00:01:54,246 it accepts the userId and a roleId. 27 00:01:54,246 --> 00:02:00,836 It then updates the database with these new values and returns an updated user. 28 00:02:00,836 --> 00:02:03,494 Let's go back to admin. 29 00:02:03,494 --> 00:02:04,614 We'll start with our loop. 30 00:02:06,667 --> 00:02:15,090 Foreach, getAllUsers() as $user. 31 00:02:20,712 --> 00:02:27,417 We're going to add a row, And 32 00:02:27,417 --> 00:02:31,443 then add our table cells, 33 00:02:31,443 --> 00:02:36,567 echo $user['username']. 34 00:02:42,316 --> 00:02:44,738 And then our created_at date. 35 00:02:48,291 --> 00:02:50,424 And then we're going to add our button. 36 00:02:55,959 --> 00:02:58,716 And then we end our foreach. 37 00:03:00,928 --> 00:03:03,401 Now we're ready to set up our buttons. 38 00:03:03,401 --> 00:03:06,770 I want our button to show the action that can be done, so 39 00:03:06,770 --> 00:03:09,427 we will add a little bit of logic around it. 40 00:03:12,780 --> 00:03:17,235 First for our conditional, 41 00:03:17,235 --> 00:03:23,737 if the $user['role_id'] == 1, 42 00:03:27,845 --> 00:03:30,387 And then we'll add our endif. 43 00:03:33,620 --> 00:03:36,624 If our role_id == 1, 44 00:03:36,624 --> 00:03:42,500 then we're going to add a link to /procedures, 45 00:03:44,922 --> 00:03:50,139 /adjustRole.php. 46 00:03:50,139 --> 00:03:54,803 And we're going to pass the roleId, 47 00:03:54,803 --> 00:03:59,617 which will equal 2, and the userId, 48 00:03:59,617 --> 00:04:05,791 which is going to equal echo $user['id']. 49 00:04:07,792 --> 00:04:13,332 And then we add a class = 50 00:04:13,332 --> 00:04:21,091 "btn btn-xs btn-warning". 51 00:04:22,801 --> 00:04:30,648 And this will be Demote to General User. 52 00:04:30,648 --> 00:04:35,247 Let's copy this line, and then we're going to add an else. 53 00:04:37,383 --> 00:04:47,000 Elseif our $user['role_id'] == 2, 54 00:04:53,745 --> 00:04:56,375 Then we're going to change our roleId to 1. 55 00:04:58,812 --> 00:05:06,388 And we're going to be promoting, Promote to Admin. 56 00:05:06,388 --> 00:05:10,478 We'll also change this styling on the button. 57 00:05:10,478 --> 00:05:13,640 This is going to be success. 58 00:05:13,640 --> 00:05:17,860 Now to make these buttons work, we're going to need a new procedure.