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