Merge pull request #17942 from jitendrapurohit/configure-link
[civicrm-core.git] / CRM / Contact / Form / Inline / Lock.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Auxiliary class to provide support for locking (and ignoring locks on) contact records.
20 */
21 class CRM_Contact_Form_Inline_Lock {
22
23 /**
24 * This function provides the HTML form elements.
25 *
26 * @param CRM_Core_Form $form
27 * Form object.
28 * @param int $contactID
29 */
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);
42 }
43
44 /**
45 * Ensure that oplock_ts hasn't changed in the underlying DB.
46 *
47 * @param array $fields
48 * The input form values.
49 * @param array $files
50 * The uploaded files if any.
51 * @param int $contactID
52 *
53 * @return bool|array
54 * true if no errors, else array of errors
55 */
56 public static function formRule($fields, $files, $contactID = NULL) {
57 $errors = [];
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 /**
71 * Return any post-save data.
72 *
73 * @param int $contactID
74 *
75 * @return array
76 * extra options to return in JSON
77 */
78 public static function getResponse($contactID) {
79 $timestamps = CRM_Contact_BAO_Contact::getTimestamps($contactID);
80 return ['oplock_ts' => $timestamps['modified_date']];
81 }
82
83 }