Merge pull request #5513 from mallezie/contact-select-file-16178
[civicrm-core.git] / CRM / Contact / BAO / Contact / Optimizer.php
CommitLineData
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 */
35class CRM_Contact_BAO_Contact_Optimizer {
86538308
EM
36 /**
37 * @param $newValues
38 * @param $oldValues
39 */
481a74f4 40 public static function edit(&$newValues, &$oldValues) {
6a488035
TO
41 // still need to do more work on this
42 // CRM-10192
43 return;
44
481a74f4 45 self::website($newValues, $oldValues);
6a488035
TO
46 }
47
86538308
EM
48 /**
49 * @param $newValues
50 * @param $oldValues
51 */
481a74f4
TO
52 public static function website(&$newValues, &$oldValues) {
53 $oldWebsiteValues = CRM_Utils_Array::value('website', $oldValues);
54 $newWebsiteValues = CRM_Utils_Array::value('website', $newValues);
6a488035 55
e60f24eb 56 if ($oldWebsiteValues == NULL || $newWebsiteValues == NULL) {
6a488035
TO
57 return;
58 }
59
60 // check if we had a value in the old
4eeb9a5b 61 $oldEmpty = $newEmpty = TRUE;
481a74f4 62 $old = $new = array();
6a488035 63
481a74f4 64 foreach ($oldWebsiteValues as $idx => $value) {
353ffa53 65 if (!empty($value['url'])) {
ab8a593e 66 $oldEmpty = FALSE;
481a74f4 67 $old[] = array('website_type_id' => $value['website_type_id'], 'url' => $value['url']);
6a488035
TO
68 }
69 }
70
481a74f4 71 foreach ($newWebsiteValues as $idx => $value) {
353ffa53 72 if (!empty($value['url'])) {
ab8a593e 73 $newEmpty = FALSE;
481a74f4 74 $new[] = array('website_type_id' => $value['website_type_id'], 'url' => $value['url']);
6a488035
TO
75 }
76 }
77
78 // if both old and new are empty, we can delete new and avoid a write
481a74f4
TO
79 if ($oldEmpty && $newEmpty) {
80 unset($newValues['website']);
6a488035
TO
81 }
82
83 // if different number of non-empty entries, return
481a74f4 84 if (count($new) != count($old)) {
6a488035
TO
85 return;
86 }
87
88 // same number of entries, check if they are exactly the same
481a74f4 89 foreach ($old as $oldID => $oldValues) {
ab8a593e 90 $found = FALSE;
481a74f4 91 foreach ($new as $newID => $newValues) {
6a488035
TO
92 if (
93 $old['website_type_id'] == $new['website_type_id'] &&
94 $old['url'] == $new['url']
95 ) {
4eeb9a5b 96 $found = TRUE;
481a74f4 97 unset($new[$newID]);
6a488035
TO
98 break;
99 }
353ffa53 100 if (!$found) {
6a488035
TO
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
481a74f4 108 unset($newValues['website']);
6a488035
TO
109 }
110
86538308
EM
111 /**
112 * @param $newValues
113 * @param $oldValues
114 */
481a74f4
TO
115 public static function email(&$newValues, &$oldValues) {
116 $oldEmailValues = CRM_Utils_Array::value('email', $oldValues);
117 $newEmailValues = CRM_Utils_Array::value('email', $newValues);
6a488035 118
e60f24eb 119 if ($oldEmailValues == NULL || $newEmailValues == NULL) {
6a488035
TO
120 return;
121 }
122
123 // check if we had a value in the old
4eeb9a5b 124 $oldEmpty = $newEmpty = TRUE;
481a74f4 125 $old = $new = array();
6a488035 126
481a74f4 127 foreach ($oldEmailValues as $idx => $value) {
353ffa53 128 if (!empty($value['email'])) {
ab8a593e 129 $oldEmpty = FALSE;
6a488035 130 $old[] = array(
353ffa53 131 'email' => $value['email'],
6a488035 132 'location_type_id' => $value['location_type_id'],
353ffa53
TO
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'] : '',
6a488035
TO
138 );
139 }
140 }
141
481a74f4 142 foreach ($newEmailValues as $idx => $value) {
353ffa53 143 if (!empty($value['email'])) {
ab8a593e 144 $newEmpty = FALSE;
6a488035 145 $new[] = array(
353ffa53 146 'email' => $value['email'],
6a488035 147 'location_type_id' => $value['location_type_id'],
353ffa53
TO
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'] : '',
6a488035
TO
153 );
154 }
155 }
156
157 // if both old and new are empty, we can delete new and avoid a write
481a74f4
TO
158 if ($oldEmpty && $newEmpty) {
159 unset($newValues['email']);
6a488035
TO
160 }
161
162 // if different number of non-empty entries, return
481a74f4 163 if (count($new) != count($old)) {
6a488035
TO
164 return;
165 }
166
167 // same number of entries, check if they are exactly the same
481a74f4 168 foreach ($old as $oldID => $oldValues) {
ab8a593e 169 $found = FALSE;
481a74f4 170 foreach ($new as $newID => $newValues) {
6a488035
TO
171 if (
172 $old['email_type_id'] == $new['email_type_id'] &&
173 $old['url'] == $new['url']
174 ) {
4eeb9a5b 175 $found = TRUE;
481a74f4 176 unset($new[$newID]);
6a488035
TO
177 break;
178 }
353ffa53 179 if (!$found) {
6a488035
TO
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
481a74f4 187 unset($newValues['email']);
6a488035 188 }
96025800 189
6a488035 190}