Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
b44e3f84 | 37 | * Auxiliary class to provide support for locking (and ignoring locks on) |
6a488035 TO |
38 | * contact records. |
39 | */ | |
40 | class CRM_Contact_Form_Inline_Lock { | |
41 | ||
42 | /** | |
fe482240 | 43 | * This function provides the HTML form elements. |
6a488035 | 44 | * |
77c5b619 TO |
45 | * @param CRM_Core_Form $form |
46 | * Form object. | |
c490a46a | 47 | * @param int $contactID |
6a488035 | 48 | * |
6a488035 TO |
49 | * @return void |
50 | */ | |
00be9182 | 51 | public static function buildQuickForm(&$form, $contactID) { |
6a488035 TO |
52 | // We provide a value for oplock_ts to client, but JS uses it carefully |
53 | // -- i.e. when loading the first inline form, JS copies oplock_ts to a | |
54 | // global value, and that global value is used for future form submissions. | |
55 | // Any time a form is submitted, the value will be updated. This | |
56 | // handles cases like: | |
57 | // - V1:open V1.phone:open V1.email:open V1.email:submit V1.phone:submit | |
58 | // - V1:open E1:open E1:submit V1.email:open V1.email:submit | |
59 | // - V1:open V1.email:open E1:open E1:submit V1.email:submit V1:lock | |
60 | $timestamps = CRM_Contact_BAO_Contact::getTimestamps($contactID); | |
61 | $form->addElement('hidden', 'oplock_ts', $timestamps['modified_date'], array('id' => 'oplock_ts')); | |
62 | $form->addFormRule(array('CRM_Contact_Form_Inline_Lock', 'formRule'), $contactID); | |
63 | } | |
64 | ||
65 | /** | |
66 | * Ensure that oplock_ts hasn't changed in the underlying DB | |
67 | * | |
77c5b619 TO |
68 | * @param array $fields |
69 | * The input form values. | |
70 | * @param array $files | |
71 | * The uploaded files if any. | |
100fef9d | 72 | * @param int $contactID |
dd244018 | 73 | * |
72b3a70c CW |
74 | * @return bool|array |
75 | * true if no errors, else array of errors | |
6a488035 | 76 | */ |
00be9182 | 77 | public static function formRule($fields, $files, $contactID = NULL) { |
6a488035 TO |
78 | $errors = array(); |
79 | ||
80 | $timestamps = CRM_Contact_BAO_Contact::getTimestamps($contactID); | |
81 | if ($fields['oplock_ts'] != $timestamps['modified_date']) { | |
82 | // Inline buttons generated via JS | |
83 | $open = sprintf("<div class='update_oplock_ts' data:update_oplock_ts='%s'>", $timestamps['modified_date']); | |
84 | $close = "</div>"; | |
85 | $errors['oplock_ts'] = $open . ts('This record was modified by another user!') . $close; | |
86 | } | |
87 | ||
88 | return empty($errors) ? TRUE : $errors; | |
89 | } | |
90 | ||
91 | /** | |
92 | * Return any post-save data | |
93 | * | |
100fef9d | 94 | * @param int $contactID |
77b97be7 | 95 | * |
a6c01b45 CW |
96 | * @return array |
97 | * extra options to return in JSON | |
6a488035 | 98 | */ |
00be9182 | 99 | public static function getResponse($contactID) { |
6a488035 TO |
100 | $timestamps = CRM_Contact_BAO_Contact::getTimestamps($contactID); |
101 | return array('oplock_ts' => $timestamps['modified_date']); | |
102 | } | |
96025800 | 103 | |
6a488035 | 104 | } |