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 | class CRM_Contact_BAO_Contact_Optimizer { | |
86538308 EM |
36 | /** |
37 | * @param $newValues | |
38 | * @param $oldValues | |
39 | */ | |
6a488035 TO |
40 | static function edit( &$newValues, &$oldValues ) { |
41 | // still need to do more work on this | |
42 | // CRM-10192 | |
43 | return; | |
44 | ||
45 | self::website( $newValues, $oldValues ); | |
46 | } | |
47 | ||
86538308 EM |
48 | /** |
49 | * @param $newValues | |
50 | * @param $oldValues | |
51 | */ | |
6a488035 TO |
52 | static function website( &$newValues, &$oldValues ) { |
53 | $oldWebsiteValues = CRM_Utils_Array::value( 'website', $oldValues ); | |
54 | $newWebsiteValues = CRM_Utils_Array::value( 'website', $newValues ); | |
55 | ||
56 | if ( $oldWebsiteValues == null || $newWebsiteValues == null ) { | |
57 | return; | |
58 | } | |
59 | ||
60 | // check if we had a value in the old | |
61 | $oldEmpty = $newEmpty = true; | |
62 | $old = $new = array( ); | |
63 | ||
64 | foreach ( $oldWebsiteValues as $idx => $value ) { | |
65 | if ( ! empty( $value['url'] ) ) { | |
66 | $oldEmpty = false; | |
67 | $old[] = array( 'website_type_id' => $value['website_type_id'], 'url' => $value['url'] ); | |
68 | } | |
69 | } | |
70 | ||
71 | foreach ( $newWebsiteValues as $idx => $value ) { | |
72 | if ( ! empty( $value['url'] ) ) { | |
73 | $newEmpty = false; | |
74 | $new[] = array( 'website_type_id' => $value['website_type_id'], 'url' => $value['url'] ); | |
75 | } | |
76 | } | |
77 | ||
78 | // if both old and new are empty, we can delete new and avoid a write | |
79 | if ( $oldEmpty && $newEmpty ) { | |
80 | unset( $newValues['website'] ); | |
81 | } | |
82 | ||
83 | // if different number of non-empty entries, return | |
84 | if ( count( $new ) != count( $old ) ) { | |
85 | return; | |
86 | } | |
87 | ||
88 | // same number of entries, check if they are exactly the same | |
89 | foreach ( $old as $oldID => $oldValues ) { | |
90 | $found = false; | |
91 | foreach ( $new as $newID => $newValues ) { | |
92 | if ( | |
93 | $old['website_type_id'] == $new['website_type_id'] && | |
94 | $old['url'] == $new['url'] | |
95 | ) { | |
96 | $found = true; | |
97 | unset( $new[$newID] ); | |
98 | break; | |
99 | } | |
100 | if ( ! $found ) { | |
101 | return; | |
102 | } | |
103 | } | |
104 | } | |
105 | ||
106 | // if we've come here, this means old and new are the same | |
107 | // we can skip saving new and return | |
108 | unset( $newValues['website'] ); | |
109 | } | |
110 | ||
86538308 EM |
111 | /** |
112 | * @param $newValues | |
113 | * @param $oldValues | |
114 | */ | |
6a488035 TO |
115 | static function email( &$newValues, &$oldValues ) { |
116 | $oldEmailValues = CRM_Utils_Array::value( 'email', $oldValues ); | |
117 | $newEmailValues = CRM_Utils_Array::value( 'email', $newValues ); | |
118 | ||
119 | if ( $oldEmailValues == null || $newEmailValues == null ) { | |
120 | return; | |
121 | } | |
122 | ||
123 | // check if we had a value in the old | |
124 | $oldEmpty = $newEmpty = true; | |
125 | $old = $new = array( ); | |
126 | ||
127 | foreach ( $oldEmailValues as $idx => $value ) { | |
128 | if ( ! empty( $value['email'] ) ) { | |
129 | $oldEmpty = false; | |
130 | $old[] = array( | |
131 | 'email' => $value['email'], | |
132 | 'location_type_id' => $value['location_type_id'], | |
133 | 'on_hold' => $value['on_hold'] ? 1 : 0, | |
134 | 'is_primary' => $value['is_primary'] ? 1 : 0, | |
135 | 'is_bulkmail' => $value['is_bulkmail'] ? 1 : 0, | |
136 | 'signature_text' => $value['signature_text'] ? $value['signature_text'] : '', | |
137 | 'signature_html' => $value['signature_html'] ? $value['signature_html'] : '', | |
138 | ); | |
139 | } | |
140 | } | |
141 | ||
142 | foreach ( $newEmailValues as $idx => $value ) { | |
143 | if ( ! empty( $value['email'] ) ) { | |
144 | $newEmpty = false; | |
145 | $new[] = array( | |
146 | 'email' => $value['email'], | |
147 | 'location_type_id' => $value['location_type_id'], | |
148 | 'on_hold' => $value['on_hold'] ? 1 : 0, | |
149 | 'is_primary' => $value['is_primary'] ? 1 : 0, | |
150 | 'is_bulkmail' => $value['is_bulkmail'] ? 1 : 0, | |
151 | 'signature_text' => $value['signature_text'] ? $value['signature_text'] : '', | |
152 | 'signature_html' => $value['signature_html'] ? $value['signature_html'] : '', | |
153 | ); | |
154 | } | |
155 | } | |
156 | ||
157 | // if both old and new are empty, we can delete new and avoid a write | |
158 | if ( $oldEmpty && $newEmpty ) { | |
159 | unset( $newValues['email'] ); | |
160 | } | |
161 | ||
162 | // if different number of non-empty entries, return | |
163 | if ( count( $new ) != count( $old ) ) { | |
164 | return; | |
165 | } | |
166 | ||
167 | // same number of entries, check if they are exactly the same | |
168 | foreach ( $old as $oldID => $oldValues ) { | |
169 | $found = false; | |
170 | foreach ( $new as $newID => $newValues ) { | |
171 | if ( | |
172 | $old['email_type_id'] == $new['email_type_id'] && | |
173 | $old['url'] == $new['url'] | |
174 | ) { | |
175 | $found = true; | |
176 | unset( $new[$newID] ); | |
177 | break; | |
178 | } | |
179 | if ( ! $found ) { | |
180 | return; | |
181 | } | |
182 | } | |
183 | } | |
184 | ||
185 | // if we've come here, this means old and new are the same | |
186 | // we can skip saving new and return | |
187 | unset( $newValues['email'] ); | |
188 | } | |
189 | } | |
190 |