Merge pull request #15881 from civicrm/5.20
[civicrm-core.git] / CRM / Contact / Form / Inline / Lock.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
5a409b50 19 * Auxiliary class to provide support for locking (and ignoring locks on) contact records.
6a488035
TO
20 */
21class CRM_Contact_Form_Inline_Lock {
22
23 /**
fe482240 24 * This function provides the HTML form elements.
6a488035 25 *
77c5b619
TO
26 * @param CRM_Core_Form $form
27 * Form object.
c490a46a 28 * @param int $contactID
6a488035 29 */
00be9182 30 public static function buildQuickForm(&$form, $contactID) {
6a488035
TO
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);
be2fb01f
CW
40 $form->addElement('hidden', 'oplock_ts', $timestamps['modified_date'], ['id' => 'oplock_ts']);
41 $form->addFormRule(['CRM_Contact_Form_Inline_Lock', 'formRule'], $contactID);
6a488035
TO
42 }
43
44 /**
5a409b50 45 * Ensure that oplock_ts hasn't changed in the underlying DB.
6a488035 46 *
77c5b619
TO
47 * @param array $fields
48 * The input form values.
49 * @param array $files
50 * The uploaded files if any.
100fef9d 51 * @param int $contactID
dd244018 52 *
72b3a70c
CW
53 * @return bool|array
54 * true if no errors, else array of errors
6a488035 55 */
00be9182 56 public static function formRule($fields, $files, $contactID = NULL) {
be2fb01f 57 $errors = [];
6a488035
TO
58
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']);
63 $close = "</div>";
64 $errors['oplock_ts'] = $open . ts('This record was modified by another user!') . $close;
65 }
66
67 return empty($errors) ? TRUE : $errors;
68 }
69
70 /**
5a409b50 71 * Return any post-save data.
6a488035 72 *
100fef9d 73 * @param int $contactID
77b97be7 74 *
a6c01b45
CW
75 * @return array
76 * extra options to return in JSON
6a488035 77 */
00be9182 78 public static function getResponse($contactID) {
6a488035 79 $timestamps = CRM_Contact_BAO_Contact::getTimestamps($contactID);
be2fb01f 80 return ['oplock_ts' => $timestamps['modified_date']];
6a488035 81 }
96025800 82
6a488035 83}