commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / CRM / Utils / Migrate / ImportJSON.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 * $Id$
33 *
34 */
35 class CRM_Utils_Migrate_ImportJSON {
36
37 protected $_lookupCache;
38
39 protected $_saveMapping;
40
41 /**
42 */
43 public function __construct() {
44 $this->_lookupCache = array();
45 $this->_saveMapping = array();
46 }
47
48 /**
49 * @param $file
50 */
51 public function run($file) {
52 $json = file_get_contents($file);
53
54 $decodedContacts = json_decode($json);
55
56 //migrate contact data
57 $this->contact($decodedContacts->civicrm_contact);
58 $this->email($decodedContacts->civicrm_email);
59 $this->phone($decodedContacts->civicrm_phone);
60 $this->address($decodedContacts->civicrm_address);
61 $this->note($decodedContacts->civicrm_note);
62 $this->relationship($decodedContacts->civicrm_relationship);
63 $this->activity($decodedContacts->civicrm_activity,
64 $decodedContacts->civicrm_activity_contact
65 );
66 $this->group($decodedContacts->civicrm_group,
67 $decodedContacts->civicrm_group_contact
68 );
69 $this->tag($decodedContacts->civicrm_tag,
70 $decodedContacts->civicrm_entity_tag
71 );
72
73 // clean up all caches etc
74 CRM_Core_Config::clearDBCache();
75 }
76
77 /**
78 * @param $contact
79 */
80 public function contact(&$contact) {
81 $this->restore($contact,
82 'CRM_Contact_DAO_Contact',
83 array('id' => 'civicrm_contact'),
84 array('birth_date', 'deceased_date', 'created_date', 'modified_date')
85 );
86 }
87
88 /**
89 * @param $email
90 */
91 public function email(&$email) {
92 $this->restore($email,
93 'CRM_Core_DAO_Email',
94 array('contact_id' => 'civicrm_contact')
95 );
96 }
97
98 /**
99 * @param $phone
100 */
101 public function phone(&$phone) {
102 $this->restore($phone,
103 'CRM_Core_DAO_Phone',
104 array('contact_id' => 'civicrm_contact')
105 );
106 }
107
108 /**
109 * @param $address
110 */
111 public function address(&$address) {
112 $this->restore($address,
113 'CRM_Core_DAO_Address',
114 array('contact_id' => 'civicrm_contact')
115 );
116 }
117
118 /**
119 * @param $note
120 */
121 public function note(&$note) {
122 $this->restore($note,
123 'CRM_Core_DAO_Note',
124 array('contact_id' => 'civicrm_contact'),
125 array('modified_date')
126 );
127 }
128
129 /**
130 * @param $relationship
131 */
132 public function relationship(&$relationship) {
133 $this->restore($relationship,
134 'CRM_Contact_DAO_Relationship',
135 array(
136 'contact_id_a' => 'civicrm_contact',
137 'contact_id_b' => 'civicrm_contact',
138 )
139 );
140 }
141
142 /**
143 * @param $activity
144 * @param $activityContacts
145 */
146 public function activity($activity, $activityContacts) {
147 $this->restore($activity,
148 'CRM_Activity_DAO_Activity',
149 NULL,
150 array('activity_date_time')
151 );
152
153 $this->restore($activityContacts,
154 'CRM_Activity_DAO_ActivityContact',
155 array(
156 'contact_id' => 'civicrm_contact',
157 'activity_id' => 'civicrm_activity',
158 )
159 );
160 }
161
162 /**
163 * @param $group
164 * @param $groupContact
165 */
166 public function group($group, $groupContact) {
167 $this->restore($group,
168 'CRM_Contact_DAO_Group',
169 NULL,
170 array('cache_date', 'refresh_date')
171 );
172
173 $this->restore($groupContact,
174 'CRM_Contact_DAO_GroupContact',
175 array(
176 'group_id' => 'civicrm_group',
177 'contact_id' => 'civicrm_contact',
178 )
179 );
180 }
181
182 /**
183 * @param $tag
184 * @param $entityTag
185 */
186 public function tag($tag, $entityTag) {
187 $this->restore($tag,
188 'CRM_Core_DAO_Tag',
189 array(
190 'created_id' => 'civicrm_contact',
191 'parent_id' => 'civicrm_tag',
192 )
193 );
194
195 $this->restore($entityTag,
196 'CRM_Core_DAO_EntityTag',
197 array(
198 'entity_id' => 'civicrm_contact',
199 'tag_id' => 'civicrm_tag',
200 )
201 );
202 }
203
204 /**
205 * @param $chunk
206 * @param string $daoName
207 * @param null $lookUpMapping
208 * @param null $dateFields
209 */
210 public function restore(&$chunk, $daoName, $lookUpMapping = NULL, $dateFields = NULL) {
211 $object = new $daoName();
212 $tableName = $object->__table;
213
214 if (is_array($lookUpMapping)) {
215 $lookUpMapping['id'] = $tableName;
216 }
217 else {
218 $lookUpMapping = array('id' => $tableName);
219 }
220
221 foreach ($lookUpMapping as $columnName => $tableName) {
222 $this->populateCache($tableName);
223 }
224
225 $saveMapping = FALSE;
226 $columns = $chunk[0];
227 foreach ($chunk as $key => $value) {
228 if ($key) {
229 $object = new $daoName();
230 foreach ($columns as $k => $column) {
231 if ($column == 'id') {
232 $childID = $value[$k];
233 $masterID = CRM_Utils_Array::value($value[$k],
234 $this->_lookupCache[$tableName],
235 NULL
236 );
237 if ($masterID) {
238 $object->id = $masterID;
239 }
240 }
241 else {
242 if (array_key_exists($column, $lookUpMapping)) {
243 $object->$column = $this->_lookupCache[$lookUpMapping[$column]][$value[$k]];
244 }
245 elseif (!empty($dateFields) && in_array($column, $dateFields)) {
246 $object->$column = CRM_Utils_Date::isoToMysql($value[$k]);
247 }
248 else {
249 $object->$column = $value[$k];
250 }
251 }
252 }
253
254 $object->save();
255 if (!$masterID) {
256 $this->_lookupCache[$tableName][$childID] = $object->id;
257 $this->_saveMapping[$tableName] = TRUE;
258 }
259 }
260 }
261 }
262
263 public function saveCache() {
264 $sql = "INSERT INTO civicrm_migration_mapping (master_id, slave_id, entity_table ) VALUES ";
265
266 foreach ($this->_lookupCache as $tableName => & $values) {
267 if (!$this->_saveMapping[$tableName]) {
268 continue;
269 }
270
271 $mapValues = array();
272 CRM_Core_DAO::executeQuery("DELETE FROM civicrm_migration_mapping where entity_table = '$tableName'");
273 foreach ($values as $childID => $masterID) {
274 $mapValues[] = "($masterID,$childID,'$tableName' )";
275 }
276 $insertSQL = $sql . implode(",\n", $mapValues);
277 CRM_Core_DAO::executeQuery($insertSQL);
278 }
279 }
280
281 /**
282 * @param string $tableName
283 */
284 public function populateCache($tableName) {
285 if (isset($this->_lookupCache[$tableName])) {
286 return;
287 }
288
289 $this->_lookupCache[$tableName] = array();
290 $this->_saveMapping[$tableName] = FALSE;
291
292 $query = "SELECT master_id, slave_id
293 FROM civicrm_migration_mapping
294 WHERE entity_table = '{$tableName}'
295 ";
296
297 $dao = CRM_Core_DAO::executeQuery($query);
298 while ($dao->fetch()) {
299 $this->_lookupCache[$dao->slave_id] = $dao->master_id;
300 }
301 }
302
303 }