/**
* Method to check-out a row for editing.
*
* @param integer $pk The numeric id of the primary key.
*
* @return boolean False on failure or error, true otherwise.
*
* @since 1.6
*/
public function checkout($pk = null)
{
// Only attempt to check the row in if it exists.
if ($pk) {
// Get an instance of the row to checkout.
$table = $this->getTable();
if (!$table->load($pk)) {
if ($table->getError() === false) {
// There was no error returned, but false indicates that the row did not exist in the db, so probably previously deleted.
$this->setError(Text::_('JLIB_APPLICATION_ERROR_NOT_EXIST'));
} else {
$this->setError($table->getError());
}
return false;
}
// If there is no checked_out or checked_out_time field, just return true.
if (!$table->hasField('checked_out') || !$table->hasField('checked_out_time')) {
return true;
}
$user = Factory::getUser();
$checkedOutField = $table->getColumnAlias('checked_out');
// Check if this is the user having previously checked out the row.
if ($table->{$checkedOutField} > 0 && $table->{$checkedOutField} != $user->get('id')) {
$this->setError(Text::_('JLIB_APPLICATION_ERROR_CHECKOUT_USER_MISMATCH'));
return false;
}
// Attempt to check the row out.
if (!$table->checkOut($user->get('id'), $pk)) {
$this->setError($table->getError());
return false;
}
}
return true;
}