J3.x

J3.x:Developing an MVC Component/Adding Checkout

From Joomla! Documentation

Revision as of 14:49, 13 September 2018 by Robbiej (talk | contribs) (first few sections)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Joomla! 
3.x
Tutorial
Developing an MVC Component



This is a multiple-article series of tutorials on how to develop a Model-View-Controller Component for Joomla! VersionJoomla 3.x.

Begin with the Introduction, and navigate the articles in this series by using the navigation button at the bottom or the box to the right (the Articles in this series).



This tutorial is part of the Developing an MVC Component for Joomla! 3.2 tutorial. You are encouraged to read the previous parts of the tutorial before reading this.

In this step we add checkout capability to our component.

Introduction

Checkout capability avoids the unexpected results which can arise when 2 users edit the same record simultaneously. It provides a record-locking capability which means that only 1 user can edit the record at the one time. You might want to have this capability in your component if you have many users updating your component's database records and have a significant probably of 2 users updating the same record simultaneously. This might arise if you allow updates from the front end of your site, although in this tutorial step we just consider the back end; the work required on the front end to support checkout would be similar.

A number of core Joomla components, including com_content, support checkout, so we will mirror that functionality in our helloworld component.

Functionality

We want to support the following aspects

  1. checking out a record when a user accesses it for editing
  2. checking in a record whenever the user has finished editing (either by typing Save or Cancel)
  3. preventing other users from editing the record when it's checked out
  4. displaying on our helloworlds view the records which are locked (using the little padlock symbol)
  5. allowing an authorised user to override the checkout and checkin a record manually

Approach

As usual, Joomla provides a lot of core functionality which we can reuse, provide we align with how the standard Joomla approach.

To our database record we need to add 2 fields

  1. checked_out – which holds the id of the user who has checked out the record
  2. checked_out_time – a date/timestamp field which records when the record was checked out

The very presence of these fields in our database record switches on the checkout functionality in the JTable class, and as checkin and checkout functionality is already in the Admin and Form Controllers/Model (which ours inherit from), there's no need to do anything else to enable the checkout functionality working for editing helloworld records.

(If you're adding this sort of control you may wish to add also "modified" and "modified_by" fields to capture the user who last modified the record, and when that occurred. Joomla automatically populates these fields for us as well.)

All that we are left to handle by ourselves is the display in our helloworlds view, and for that we need to do the following:

  1. include the checked_out and checked_out_time fields in our select query in the model
  2. display the little padlock symbol against records which are checked out, and enable clicking on it based on whether the user is authorised to checkin records which are checked out.
  3. currently a user can click on a record to edit it – we'll disable this if the record is checked out, and the user doesn't have checkin capability
  4. include a Checkin button in the toolbar.