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