3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Auxiliary class to provide support for locking (and ignoring locks on) contact records.
21 class CRM_Contact_Form_Inline_Lock
{
24 * This function provides the HTML form elements.
26 * @param CRM_Core_Form $form
28 * @param int $contactID
30 public static function buildQuickForm(&$form, $contactID) {
31 // We provide a value for oplock_ts to client, but JS uses it carefully
32 // -- i.e. when loading the first inline form, JS copies oplock_ts to a
33 // global value, and that global value is used for future form submissions.
34 // Any time a form is submitted, the value will be updated. This
35 // handles cases like:
36 // - V1:open V1.phone:open V1.email:open V1.email:submit V1.phone:submit
37 // - V1:open E1:open E1:submit V1.email:open V1.email:submit
38 // - V1:open V1.email:open E1:open E1:submit V1.email:submit V1:lock
39 $timestamps = CRM_Contact_BAO_Contact
::getTimestamps($contactID);
40 $form->addElement('hidden', 'oplock_ts', $timestamps['modified_date'], ['id' => 'oplock_ts']);
41 $form->addFormRule(['CRM_Contact_Form_Inline_Lock', 'formRule'], $contactID);
45 * Ensure that oplock_ts hasn't changed in the underlying DB.
47 * @param array $fields
48 * The input form values.
50 * The uploaded files if any.
51 * @param int $contactID
54 * true if no errors, else array of errors
56 public static function formRule($fields, $files, $contactID = NULL) {
59 $timestamps = CRM_Contact_BAO_Contact
::getTimestamps($contactID);
60 if ($fields['oplock_ts'] != $timestamps['modified_date']) {
61 // Inline buttons generated via JS
62 $open = sprintf("<div class='update_oplock_ts' data:update_oplock_ts='%s'>", $timestamps['modified_date']);
64 $errors['oplock_ts'] = $open . ts('This record was modified by another user!') . $close;
67 return empty($errors) ?
TRUE : $errors;
71 * Return any post-save data.
73 * @param int $contactID
76 * extra options to return in JSON
78 public static function getResponse($contactID) {
79 $timestamps = CRM_Contact_BAO_Contact
::getTimestamps($contactID);
80 return ['oplock_ts' => $timestamps['modified_date']];