12.10. Ticket-Editor Component
The ticket-editor component provides a form that is used to edit tickets. It allows you to update the ticket's title and description, assign it to a user, and change its status. It also shows some details, including who opened the ticket and when it was last changed. The ticket-editing interface is created by TicketEditor.php (see Listing 12-17) and is shown in Figure 12-13.
Figure 12-13. Ticket editor
Listing 12-17. TicketEditor.php
Listing 12-18. TicketEditor.js
This component doesn't require setup methods, but it does have to set up its AJAX class (line 12). We will be using two of the server's methodsgetTicket and updateTicketso callback functions are added for each. The getTicket callback calls the updateTicketForm method when it is called, and the updateTicket method calls that same method. We will also be providing a user feedback message and highlighting the form (lines 79).
Lines 2023 define the viewTicket method; this is used by other parts of the application, such as the add ticket component, to select a ticket to view. This method sets the current section to ticket and does an AJAX call to getTicket. The viewTicketForm method (lines 2023) calls viewTicket, getting the value of ticket_id from the form that is passed in.
The updateTicket function (lines 2546) processes a submission of the ticket form. It builds an object that contains the fields to update and then makes an AJAX call to updateTicket (line 42) with that list. The fields object is defined on line 26, and then the easy fields (title and description) are added to it. Next the assigned value is processed (lines 3037); assigned is set using a select box, and its first option needs a special value. We support this value mapping by checking the select box's selected index (line 31), and if the first option is not selected, using its value. Otherwise, the mapping code uses the value of false. Setting the assigned property to false lets the back end know that the ticket is unassigned and allows it to clean up the database accordingly. Finishing the process, the status property is added to the fields object (line 40), and the AJAX update is performed.
The updateTicketForm function updates the form that is used to edit the data. It starts with some basic error checking (lines 4954). If the server isn't able to find a ticket for a given ID, it returns false. Thus, the function checks for a false value, sets an error message if it finds one, and then sets the application to the front page. If the ticket exists, we start updating the form. We start with the input boxes, text area, and spans (lines 5864) and then move on to the select boxes. Status contains a hard-coded set of options, so it can be selected by looping over the list, matching the status value against the options text, and then setting the element's selectedIndex property (lines 6672).
The users array is dynamic, so the process has to start by updating the list of users from the data that is returned from the server. This is done by creating a new Option instance for each user and adding it to the select element options array. With the list updated, we loop over the list as we did in the status case. The only difference is that this time, we compare the option's value because we have a user_id. If no user matches, the selected index is set to 0, which is the "not assigned" option.
The function finishes by doing a basic security check. If the user is not logged in, the Submission button is disabled, and a message telling the user to log in to update the ticket is shown. This check allows the same page to be used for authenticated and nonauthenticated users. Like any other security check, this code on the client is there just to provide a good user experience; the back end also enforces security and would prevent any not-logged-in user from updating a ticket, no matter what he or she did to the client's code.