Merge pull request #13915 from colemanw/shortCRM
[civicrm-core.git] / CRM / Contact / Form / Inline / Lock.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * Auxiliary class to provide support for locking (and ignoring locks on) contact records.
36 */
37 class CRM_Contact_Form_Inline_Lock {
38
39 /**
40 * This function provides the HTML form elements.
41 *
42 * @param CRM_Core_Form $form
43 * Form object.
44 * @param int $contactID
45 */
46 public static function buildQuickForm(&$form, $contactID) {
47 // We provide a value for oplock_ts to client, but JS uses it carefully
48 // -- i.e. when loading the first inline form, JS copies oplock_ts to a
49 // global value, and that global value is used for future form submissions.
50 // Any time a form is submitted, the value will be updated. This
51 // handles cases like:
52 // - V1:open V1.phone:open V1.email:open V1.email:submit V1.phone:submit
53 // - V1:open E1:open E1:submit V1.email:open V1.email:submit
54 // - V1:open V1.email:open E1:open E1:submit V1.email:submit V1:lock
55 $timestamps = CRM_Contact_BAO_Contact::getTimestamps($contactID);
56 $form->addElement('hidden', 'oplock_ts', $timestamps['modified_date'], ['id' => 'oplock_ts']);
57 $form->addFormRule(['CRM_Contact_Form_Inline_Lock', 'formRule'], $contactID);
58 }
59
60 /**
61 * Ensure that oplock_ts hasn't changed in the underlying DB.
62 *
63 * @param array $fields
64 * The input form values.
65 * @param array $files
66 * The uploaded files if any.
67 * @param int $contactID
68 *
69 * @return bool|array
70 * true if no errors, else array of errors
71 */
72 public static function formRule($fields, $files, $contactID = NULL) {
73 $errors = [];
74
75 $timestamps = CRM_Contact_BAO_Contact::getTimestamps($contactID);
76 if ($fields['oplock_ts'] != $timestamps['modified_date']) {
77 // Inline buttons generated via JS
78 $open = sprintf("<div class='update_oplock_ts' data:update_oplock_ts='%s'>", $timestamps['modified_date']);
79 $close = "</div>";
80 $errors['oplock_ts'] = $open . ts('This record was modified by another user!') . $close;
81 }
82
83 return empty($errors) ? TRUE : $errors;
84 }
85
86 /**
87 * Return any post-save data.
88 *
89 * @param int $contactID
90 *
91 * @return array
92 * extra options to return in JSON
93 */
94 public static function getResponse($contactID) {
95 $timestamps = CRM_Contact_BAO_Contact::getTimestamps($contactID);
96 return ['oplock_ts' => $timestamps['modified_date']];
97 }
98
99 }