Merge remote-tracking branch 'upstream/4.6' into 4.6-master-2015-09-05-17-42-45
[civicrm-core.git] / sql / GenerateData.php
CommitLineData
6a488035 1<?php
7bc930af 2/**
906a298c
KJ
3 * +--------------------------------------------------------------------+
4 * | CiviCRM version 4.7 |
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 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class generates data for the schema located in Contact.sql
38 *
39 * each public method generates data for the concerned table.
40 * so for example the addContactDomain method generates and adds
41 * data to the contact_domain table
42 *
43 * Data generation is a bit tricky since the data generated
44 * randomly in one table could be used as a FKEY in another
45 * table.
46 *
47 * In order to ensure that a randomly generated FKEY matches
48 * a field in the referened table, the field in the referenced
49 * table is always generated linearly.
50 *
51 *
52 *
53 *
54 * Some numbers
55 *
56 * Domain ID's - 1 to NUM_DOMAIN
57 *
58 * Context - 3/domain
59 *
60 * Contact - 1 to NUM_CONTACT
375fffa8 61 * 80% - Individual
6a488035
TO
62 * 10% - Household
63 * 10% - Organization
64 *
65 * Contact to Domain distribution should be equal.
66 *
67 *
68 * Contact Individual = 1 to 0.8*NUM_CONTACT
69 *
70 * Contact Household = 0.8*NUM_CONTACT to 0.9*NUM_CONTACT
71 *
72 * Contact Organization = 0.9*NUM_CONTACT to NUM_CONTACT
73 *
74 * Assumption is that each household contains 4 individuals
75 *
76 */
77
78/**
79 *
80 * Note: implication of using of mt_srand(1) in constructor
81 * The data generated will be done in a consistent manner
82 * so as to give the same data during each run (but this
83 * would involve populating the entire db at one go - since
84 * mt_srand(1) is in the constructor, if one needs to be able
85 * to get consistent random numbers then the mt_srand(1) shld
86 * be in each function that adds data to each table.
87 *
88 */
89
90
91require_once '../civicrm.config.php';
92
93// autoload
94require_once 'CRM/Core/ClassLoader.php';
95CRM_Core_ClassLoader::singleton()->register();
627456b5
EM
96
97/**
98 * Class CRM_GCD
99 */
6a488035
TO
100class CRM_GCD {
101
102 /**
103 * Constants
104 */
105
106 // Set ADD_TO_DB = FALSE to do a dry run
7da04cde 107 const ADD_TO_DB = TRUE;
6a488035 108
7da04cde
TO
109 const DATA_FILENAME = "sample_data.xml";
110 const NUM_DOMAIN = 1;
111 const NUM_CONTACT = 200;
112 const INDIVIDUAL_PERCENT = 80;
113 const HOUSEHOLD_PERCENT = 10;
114 const ORGANIZATION_PERCENT = 10;
115 const NUM_INDIVIDUAL_PER_HOUSEHOLD = 4;
116 const NUM_ACTIVITY = 150;
6a488035 117
6a488035 118 // Location types from the table crm_location_type
7da04cde
TO
119 const HOME = 1;
120 const WORK = 2;
121 const MAIN = 3;
122 const OTHER = 4;
6a488035
TO
123
124 /**
125 * Class constructor
126 */
906a298c 127 public function __construct() {
6a488035
TO
128 // initialize all the vars
129 $this->numIndividual = self::INDIVIDUAL_PERCENT * self::NUM_CONTACT / 100;
130 $this->numHousehold = self::HOUSEHOLD_PERCENT * self::NUM_CONTACT / 100;
131 $this->numOrganization = self::ORGANIZATION_PERCENT * self::NUM_CONTACT / 100;
132 $this->numStrictIndividual = $this->numIndividual - ($this->numHousehold * self::NUM_INDIVIDUAL_PER_HOUSEHOLD);
133
134 // Parse data file
7bc930af 135 foreach ((array) simplexml_load_file(self::DATA_FILENAME) as $key => $val) {
6a488035
TO
136 $val = (array) $val;
137 $this->sampleData[$key] = (array) $val['item'];
138 }
139 // Init DB
140 $config = CRM_Core_Config::singleton();
51fa20cb 141
05802b8e
DG
142 // Relationship types indexed by name_a_b from the table civicrm_relationship_type
143 $this->relTypes = CRM_Utils_Array::index(array('name_a_b'), CRM_Core_PseudoConstant::relationshipType('name'));
51fa20cb 144
6a488035
TO
145 }
146
147 /**
148 * Public wrapper for calling private "add" functions
149 * Provides user feedback
257e7666 150 * @param $itemName
6a488035
TO
151 */
152 public function generate($itemName) {
375fffa8 153 echo "Generating $itemName\n";
6a488035
TO
154 $fn = "add$itemName";
155 $this->$fn();
156 }
157
158 /**
159 * this function creates arrays for the following
160 *
161 * domain id
162 * contact id
163 * contact_location id
164 * contact_contact_location id
165 * contact_email uuid
166 * contact_phone_uuid
167 * contact_instant_message uuid
168 * contact_relationship uuid
169 * contact_task uuid
170 * contact_note uuid
171 */
172 public function initID() {
6a488035
TO
173 // get the domain and contact id arrays
174 $this->domain = range(1, self::NUM_DOMAIN);
175 shuffle($this->domain);
375fffa8
CW
176
177 // Get first contact id
178 $this->startCid = $cid = CRM_Core_DAO::singleValueQuery("SELECT MAX(id) FROM civicrm_contact");
179 $this->contact = range($cid + 1, $cid + self::NUM_CONTACT);
6a488035
TO
180 shuffle($this->contact);
181
182 // get the individual, household and organizaton contacts
183 $offset = 0;
184 $this->Individual = array_slice($this->contact, $offset, $this->numIndividual);
185 $offset += $this->numIndividual;
186 $this->Household = array_slice($this->contact, $offset, $this->numHousehold);
187 $offset += $this->numHousehold;
188 $this->Organization = array_slice($this->contact, $offset, $this->numOrganization);
189
190 // get the strict individual contacts (i.e individual contacts not belonging to any household)
191 $this->strictIndividual = array_slice($this->Individual, 0, $this->numStrictIndividual);
192
193 // get the household to individual mapping array
194 $this->householdIndividual = array_slice($this->Individual, $this->numStrictIndividual);
195 $this->householdIndividual = array_chunk($this->householdIndividual, self::NUM_INDIVIDUAL_PER_HOUSEHOLD);
196 $this->householdIndividual = array_combine($this->Household, $this->householdIndividual);
197 }
198
199 /*********************************
200 * private members
201 *********************************/
202
203 // enum's from database
204 private $preferredCommunicationMethod = array('1', '2', '3', '4', '5');
205 private $contactType = array('Individual', 'Household', 'Organization');
206 private $phoneType = array('1', '2', '3', '4');
207
208 // customizable enums (foreign keys)
209 private $prefix = array(
210 // Female
211 1 => array(
212 1 => 'Mrs.',
213 2 => 'Ms.',
906a298c 214 4 => 'Dr.',
6a488035
TO
215 ),
216 // Male
217 2 => array(
218 3 => 'Mr.',
219 4 => 'Dr.',
906a298c 220 ),
6a488035
TO
221 );
222 private $suffix = array(1 => 'Jr.', 2 => 'Sr.', 3 => 'II', 4 => 'III');
223 private $gender = array(1 => 'female', 2 => 'male');
224
225 // store domain id's
226 private $domain = array();
227
228 // store contact id's
229 private $contact = array();
230 private $Individual = array();
231 private $Household = array();
232 private $Organization = array();
233
234 // store which contacts have a location entity
235 // for automatic management of is_primary field
236 private $location = array(
237 'Email' => array(),
238 'Phone' => array(),
239 'Address' => array(),
240 );
241
242 // stores the strict individual id and household id to individual id mapping
243 private $strictIndividual = array();
244 private $householdIndividual = array();
245 private $householdName = array();
246
247 // sample data in xml format
248 private $sampleData = array();
249
250 // private vars
375fffa8 251 private $startCid;
6a488035
TO
252 private $numIndividual = 0;
253 private $numHousehold = 0;
254 private $numOrganization = 0;
255 private $numStrictIndividual = 0;
256 private $stateMap = array();
257 private $states = array();
258
259 private $groupMembershipStatus = array('Added', 'Removed', 'Pending');
260 private $subscriptionHistoryMethod = array('Admin', 'Email');
261
262 /*********************************
263 * private methods
257e7666
EM
264 ********************************
265 * @param int $size
266 * @return string
267 */
6a488035 268
906a298c
KJ
269 /**
270 * get a randomly generated string
271 */
6a488035
TO
272 private function randomString($size = 32) {
273 $string = "";
274
275 // get an ascii code for each character
276 for ($i = 0; $i < $size; $i++) {
277 $random_int = mt_rand(65, 122);
278 if (($random_int < 97) && ($random_int > 90)) {
279 // if ascii code between 90 and 97 substitute with space
280 $random_int = 32;
281 }
282 $random_char = chr($random_int);
283 $string .= $random_char;
284 }
285 return $string;
286 }
287
627456b5
EM
288 /**
289 * @return string
290 */
6a488035
TO
291 private function randomChar() {
292 return chr(mt_rand(65, 90));
293 }
294
295 /**
296 * Get a random item from the sample data or any other array
297 *
298 * @param $items (array or string) - if string, used as key for sample data, if array, used as data source
299 *
300 * @return mixed (element from array)
301 *
302 * @private
303 */
304 private function randomItem($items) {
305 if (!is_array($items)) {
306 $key = $items;
307 $items = $this->sampleData[$key];
308 }
309 if (!$items) {
310 echo "Error: no items found for '$key'\n";
906a298c 311 return FALSE;
6a488035
TO
312 }
313 return $items[mt_rand(0, count($items) - 1)];
314 }
315
627456b5
EM
316 /**
317 * @param $items
318 *
319 * @return mixed
320 */
6a488035
TO
321 private function randomIndex($items) {
322 return $this->randomItem(array_keys($items));
323 }
324
627456b5
EM
325 /**
326 * @param $items
327 *
328 * @return array
329 */
6a488035
TO
330 private function randomKeyValue($items) {
331 $key = $this->randomIndex($items);
332 return array($key, $items[$key]);
333 }
334
627456b5
EM
335 /**
336 * @param $chance
337 *
338 * @return int
339 */
6a488035
TO
340 private function probability($chance) {
341 if (mt_rand(0, 100) < ($chance * 100)) {
342 return 1;
343 }
344 return 0;
345 }
346
347 /**
348 * Generate a random date.
349 *
350 * If both $startDate and $endDate are defined generate
351 * date between them.
352 *
353 * If only startDate is specified then date generated is
354 * between startDate + 1 year.
355 *
356 * if only endDate is specified then date generated is
357 * between endDate - 1 year.
358 *
359 * if none are specified - date is between today - 1year
360 * and today
361 *
362 * @param int $startDate Start Date in Unix timestamp
7bc930af 363 * @param int $endDate End Date in Unix timestamp
6a488035
TO
364 * @access private
365 *
366 * @return string randomly generated date in the format "Ymd"
367 *
368 */
369 private function randomDate($startDate = 0, $endDate = 0) {
370
371 // number of seconds per year
372 $numSecond = 31536000;
373 $dateFormat = "Ymdhis";
374 $today = time();
375
376 // both are defined
377 if ($startDate && $endDate) {
378 return date($dateFormat, mt_rand($startDate, $endDate));
379 }
380
381 // only startDate is defined
382 if ($startDate) {
383 return date($dateFormat, mt_rand($startDate, $startDate + $numSecond));
384 }
385
386 // only endDate is defined
387 if ($startDate) {
388 return date($dateFormat, mt_rand($endDate - $numSecond, $endDate));
389 }
390
391 // none are defined
392 return date($dateFormat, mt_rand($today - $numSecond, $today));
393 }
394
395 /**
396 * Automatically manage the is_primary field by tracking which contacts have each item
257e7666
EM
397 * @param $cid
398 * @param $type
399 * @return int
6a488035
TO
400 */
401 private function isPrimary($cid, $type) {
402 if (empty($this->location[$type][$cid])) {
403 $this->location[$type][$cid] = TRUE;
404 return 1;
405 }
406 return 0;
407 }
408
409 /**
410 * Execute a query unless we are doing a dry run
411 * Note: this wrapper should not be used for SELECT queries
257e7666
EM
412 * @param $query
413 * @param array $params
414 * @return \CRM_Core_DAO
6a488035
TO
415 */
416 private function _query($query, $params = array()) {
417 if (self::ADD_TO_DB) {
418 return CRM_Core_DAO::executeQuery($query, $params);
419 }
420 }
421
422 /**
423 * Call dao insert method unless we are doing a dry run
257e7666 424 * @param $dao
6a488035
TO
425 */
426 private function _insert(&$dao) {
427 if (self::ADD_TO_DB) {
428 if (!$dao->insert()) {
429 echo "ERROR INSERT: " . mysql_error() . "\n";
430 print_r($dao);
431 exit(1);
432 }
433 }
434 }
435
436 /**
437 * Call dao update method unless we are doing a dry run
257e7666 438 * @param $dao
6a488035
TO
439 */
440 private function _update(&$dao) {
441 if (self::ADD_TO_DB) {
442 if (!$dao->update()) {
443 echo "ERROR UPDATE: " . mysql_error() . "\n";
444 print_r($dao);
445 exit(1);
446 }
447 }
448 }
449
450 /**
451 * Add core DAO object
257e7666
EM
452 * @param $type
453 * @param $params
6a488035
TO
454 */
455 private function _addDAO($type, $params) {
456 $daoName = "CRM_Core_DAO_$type";
457 $obj = new $daoName();
458 foreach ($params as $key => $value) {
459 $obj->$key = $value;
460 }
461 if (isset($this->location[$type])) {
462 $obj->is_primary = $this->isPrimary($params['contact_id'], $type);
463 }
464 $this->_insert($obj);
465 }
466
467 /**
468 * Fetch contact type based on stored mapping
257e7666 469 * @param $id
906a298c 470 * @return string $type
6a488035
TO
471 */
472 private function getContactType($id) {
473 foreach (array('Individual', 'Household', 'Organization') as $type) {
474 if (in_array($id, $this->$type)) {
475 return $type;
476 }
477 }
478 }
479
480 /**
481 * This method adds NUM_DOMAIN domains and then adds NUM_REVISION
482 * revisions for each domain with the latest revision being the last one..
483 */
484 private function addDomain() {
485
486 /* Add a location for domain 1 */
487
488 $domain = new CRM_Core_DAO_Domain();
489 for ($id = 2; $id <= self::NUM_DOMAIN; $id++) {
490 // domain name is pretty simple. it is "Domain $id"
491 $domain->name = "Domain $id";
492 $domain->description = "Description $id";
493 $domain->contact_name = $this->randomName();
494
495 // insert domain
496 $this->_insert($domain);
497 }
498 }
499
627456b5
EM
500 /**
501 * @return string
502 */
6a488035
TO
503 public function randomName() {
504 $first_name = $this->randomItem(($this->probability(.5) ? 'fe' : '') . 'male_name');
505 $middle_name = ucfirst($this->randomChar());
506 $last_name = $this->randomItem('last_name');
507 return "$first_name $middle_name. $last_name";
508 }
509
510 /**
511 * This method adds data to the contact table
512 *
513 * id - from $contact
514 * contact_type 'Individual' 'Household' 'Organization'
515 * preferred_communication (random 1 to 3)
516 */
517 private function addContact() {
518 $contact = new CRM_Contact_DAO_Contact();
375fffa8 519 $cid = $this->startCid;
6a488035 520
375fffa8
CW
521 for ($id = $cid + 1; $id <= $cid + self::NUM_CONTACT; $id++) {
522 $contact->contact_type = $this->getContactType($id);
6a488035
TO
523 $contact->do_not_phone = $this->probability(.2);
524 $contact->do_not_email = $this->probability(.2);
525 $contact->do_not_post = $this->probability(.2);
526 $contact->do_not_trade = $this->probability(.2);
527 $contact->preferred_communication_method = NULL;
528 if ($this->probability(.5)) {
529 $contact->preferred_communication_method = CRM_Core_DAO::VALUE_SEPARATOR . $this->randomItem($this->preferredCommunicationMethod) . CRM_Core_DAO::VALUE_SEPARATOR;
530 }
557ceeb7 531 $contact->source = 'Sample Data';
6a488035
TO
532 $this->_insert($contact);
533 }
534 }
535
536 /**
537 * addIndividual()
538 *
539 * This method adds individual's data to the contact table
540 *
541 * The following fields are generated and added.
542 *
543 * contact_uuid - individual
544 * contact_rid - latest one
545 * first_name 'First Name $contact_uuid'
546 * middle_name 'Middle Name $contact_uuid'
547 * last_name 'Last Name $contact_uuid'
548 * job_title 'Job Title $contact_uuid'
549 *
550 */
551 private function addIndividual() {
552
553 $contact = new CRM_Contact_DAO_Contact();
554 $year = 60 * 60 * 24 * 365.25;
555 $now = time();
556
557 foreach ($this->Individual as $cid) {
558 $contact->is_deceased = $contact->gender_id = $contact->birth_date = $contact->deceased_date = $email = NULL;
559 list($gender_id, $gender) = $this->randomKeyValue($this->gender);
560 $birth_date = mt_rand($now - 90 * $year, $now - 10 * $year);
561
562 $contact->last_name = $this->randomItem('last_name');
563
564 // Manage household names
565 if (!in_array($contact->id, $this->strictIndividual)) {
566 // Find position in household
567 foreach ($this->householdIndividual as $householdId => $house) {
568 foreach ($house as $position => $memberId) {
569 if ($memberId == $cid) {
570 break 2;
571 }
572 }
573 }
574 // Head of household: set name
575 if (empty($this->householdName[$householdId])) {
576 $this->householdName[$householdId] = $contact->last_name;
577 }
578 // Kids get household name, spouse might get it
579 if ($position > 1 || $this->probability(.5)) {
580 $contact->last_name = $this->householdName[$householdId];
581 }
582 elseif ($this->householdName[$householdId] != $contact->last_name) {
583 // Spouse might hyphenate name
584 if ($this->probability(.5)) {
585 $contact->last_name .= '-' . $this->householdName[$householdId];
586 }
587 // Kids might hyphenate name
588 else {
589 $this->householdName[$householdId] .= '-' . $contact->last_name;
590 }
591 }
592 // Sensible ages and genders
593 $offset = mt_rand($now - 40 * $year, $now);
594 // Parents
595 if ($position < 2) {
596 $birth_date = mt_rand($offset - 35 * $year, $offset - 20 * $year);
597 if ($this->probability(.8)) {
598 $gender_id = 2 - $position;
599 $gender = $this->gender[$gender_id];
600 }
601 }
602 // Kids
603 else {
604 $birth_date = mt_rand($offset - 10 * $year, $offset);
605 }
606 }
607 // Non household people
608 else {
609 if ($this->probability(.6)) {
610 $this->_addAddress($cid);
611 }
612 }
613
614 $contact->first_name = $this->randomItem($gender . '_name');
615 $contact->middle_name = $this->probability(.5) ? '' : ucfirst($this->randomChar());
616 $age = intval(($now - $birth_date) / $year);
617
618 // Prefix and suffix by gender and age
619 $contact->prefix_id = $contact->suffix_id = $prefix = $suffix = NULL;
620 if ($this->probability(.5) && $age > 20) {
621 list($contact->prefix_id, $prefix) = $this->randomKeyValue($this->prefix[$gender_id]);
622 $prefix .= ' ';
623 }
624 if ($gender == 'male' && $this->probability(.50)) {
625 list($contact->suffix_id, $suffix) = $this->randomKeyValue($this->suffix);
626 $suffix = ' ' . $suffix;
627 }
628 if ($this->probability(.7)) {
629 $contact->gender_id = $gender_id;
630 }
631 if ($this->probability(.7)) {
632 $contact->birth_date = date("Ymd", $birth_date);
633 }
634
635 // Deceased probability based on age
636 if ($age > 40) {
637 $contact->is_deceased = $this->probability(($age - 30) / 100);
638 if ($contact->is_deceased && $this->probability(.7)) {
639 $contact->deceased_date = $this->randomDate();
640 }
641 }
642
643 // Add 0, 1 or 2 email address
644 $count = mt_rand(0, 2);
645 for ($i = 0; $i < $count; ++$i) {
646 $email = $this->_individualEmail($contact);
647 $this->_addEmail($cid, $email, self::HOME);
648 }
649
650 // Add 0, 1 or 2 phones
651 $count = mt_rand(0, 2);
652 for ($i = 0; $i < $count; ++$i) {
653 $this->_addPhone($cid);
654 }
655
656 // Occasionally you get contacts with just an email in the db
657 if ($this->probability(.2) && $email) {
658 $contact->first_name = $contact->last_name = $contact->middle_name = NULL;
659 $contact->is_deceased = $contact->gender_id = $contact->birth_date = $contact->deceased_date = NULL;
660 $contact->display_name = $contact->sort_name = $email;
661 $contact->postal_greeting_display = $contact->email_greeting_display = "Dear $email";
662 }
663 else {
664 $contact->display_name = $prefix . $contact->first_name . ' ' . $contact->last_name . $suffix;
665 $contact->sort_name = $contact->last_name . ', ' . $contact->first_name;
666 $contact->postal_greeting_display = $contact->email_greeting_display = 'Dear ' . $contact->first_name;
667 }
668 $contact->addressee_id = $contact->postal_greeting_id = $contact->email_greeting_id = 1;
669 $contact->addressee_display = $contact->display_name;
670 $contact->hash = crc32($contact->sort_name);
671 $contact->id = $cid;
672 $this->_update($contact);
673 }
674 }
675
676 /**
677 * This method adds household's data to the contact table
678 *
679 * The following fields are generated and added.
680 *
681 * contact_uuid - household_individual
682 * contact_rid - latest one
683 * household_name 'household $contact_uuid primary contact $primary_contact_uuid'
684 * nick_name 'nick $contact_uuid'
685 * primary_contact_uuid = $household_individual[$contact_uuid][0];
686 *
687 */
688 private function addHousehold() {
689
690 $contact = new CRM_Contact_DAO_Contact();
691 foreach ($this->Household as $cid) {
692 // Add address
693 $this->_addAddress($cid);
694
695 $contact->id = $cid;
696 $contact->household_name = $this->householdName[$cid] . " family";
697 // need to update the sort name for the main contact table
698 $contact->display_name = $contact->sort_name = $contact->household_name;
699 $contact->postal_greeting_id = $contact->email_greeting_id = 5;
700 $contact->postal_greeting_display = $contact->email_greeting_display = 'Dear ' . $contact->household_name;
701 $contact->addressee_id = 2;
702 $contact->addressee_display = $contact->display_name;
703 $contact->hash = crc32($contact->sort_name);
704 $this->_update($contact);
705 }
706 }
707
708 /**
709 * This method adds organization data to the contact table
710 *
711 * The following fields are generated and added.
712 *
713 * contact_uuid - organization
714 * contact_rid - latest one
715 * organization_name 'organization $contact_uuid'
716 * legal_name 'legal $contact_uuid'
717 * nick_name 'nick $contact_uuid'
718 * sic_code 'sic $contact_uuid'
719 * primary_contact_id - random individual contact uuid
720 *
721 */
722 private function addOrganization() {
723
724 $org = new CRM_Contact_DAO_Contact();
725 $employees = $this->Individual;
726 shuffle($employees);
727
728 foreach ($this->Organization as $key => $id) {
729 $org->primary_contact_id = $website = $email = NULL;
730 $org->id = $id;
731 $address = $this->_addAddress($id);
732
733 $namePre = $this->randomItem('organization_prefix');
734 $nameMid = $this->randomItem('organization_name');
735 $namePost = $this->randomItem('organization_suffix');
736
737 // Some orgs are named after their location
738 if ($this->probability(.7)) {
739 $place = $this->randomItem(array('city', 'street_name', 'state'));
740 $namePre = $address[$place];
741 }
742 $org->organization_name = "$namePre $nameMid $namePost";
743
744 // Most orgs have a website and email
745 if ($this->probability(.8)) {
746 $website = $this->_addWebsite($id, $org->organization_name);
747 $url = str_replace('http://', '', $website['url']);
748 $email = $this->randomItem('email_address') . '@' . $url;
749 $this->_addEmail($id, $email, self::MAIN);
750 }
751
752 // current employee
753 if ($this->probability(.8)) {
754 $indiv = new CRM_Contact_DAO_Contact();
755 $org->primary_contact_id = $indiv->id = $employees[$key];
756 $indiv->organization_name = $org->organization_name;
757 $indiv->employer_id = $id;
758 $this->_update($indiv);
759 // Share address with employee
760 if ($this->probability(.8)) {
761 $this->_addAddress($indiv->id, $id);
762 }
763 // Add work email for employee
764 if ($website) {
765 $indiv->find(TRUE);
766 $email = $this->_individualEmail($indiv, $url);
767 $this->_addEmail($indiv->id, $email, self::WORK);
768 }
769 }
770
771 // need to update the sort name for the main contact table
772 $org->display_name = $org->sort_name = $org->organization_name;
773 $org->addressee_id = 3;
774 $org->addressee_display = $org->display_name;
775 $org->hash = crc32($org->sort_name);
776 $this->_update($org);
777 }
778 }
779
780 /**
781 * This method adds data to the contact_relationship table
782 */
783 private function addRelationship() {
784
785 $relationship = new CRM_Contact_DAO_Relationship();
786
787 // Household relationships
788 foreach ($this->householdIndividual as $household_id => $household_member) {
789 // Default active
790 $relationship->is_active = 1;
791
792 // add child_of relationship for each child
05802b8e 793 $relationship->relationship_type_id = $this->relTypes['Child of']['id'];
6a488035
TO
794 foreach (array(0, 1) as $parent) {
795 foreach (array(2, 3) as $child) {
796 $relationship->contact_id_a = $household_member[$child];
797 $relationship->contact_id_b = $household_member[$parent];
798 $this->_insert($relationship);
799 }
800 }
801
802 // add sibling_of relationship
05802b8e 803 $relationship->relationship_type_id = $this->relTypes['Sibling of']['id'];
6a488035
TO
804 $relationship->contact_id_a = $household_member[3];
805 $relationship->contact_id_b = $household_member[2];
806 $this->_insert($relationship);
807
808 // add member_of_household relationships and shared address
05802b8e 809 $relationship->relationship_type_id = $this->relTypes['Household Member of']['id'];
6a488035
TO
810 $relationship->contact_id_b = $household_id;
811 for ($i = 1; $i < 4; ++$i) {
812 $relationship->contact_id_a = $household_member[$i];
813 $this->_insert($relationship);
814 $this->_addAddress($household_member[$i], $household_id);
815 }
816
817 // Divorced/separated couples - end relationship and different address
818 if ($this->probability(.4)) {
819 $relationship->is_active = 0;
820 $this->_addAddress($household_member[0]);
821 }
822 else {
823 $this->_addAddress($household_member[0], $household_id);
824 }
825
826 // add head_of_household relationship 1 for head of house
05802b8e 827 $relationship->relationship_type_id = $this->relTypes['Head of Household for']['id'];
6a488035
TO
828 $relationship->contact_id_a = $household_member[0];
829 $relationship->contact_id_b = $household_id;
830 $this->_insert($relationship);
831
832 // add spouse_of relationship 1 for both the spouses
05802b8e 833 $relationship->relationship_type_id = $this->relTypes['Spouse of']['id'];
6a488035
TO
834 $relationship->contact_id_a = $household_member[1];
835 $relationship->contact_id_b = $household_member[0];
836 $this->_insert($relationship);
837 }
838
839 // Add current employer relationships
840 $this->_query("INSERT INTO civicrm_relationship
841 (contact_id_a, contact_id_b, relationship_type_id, is_active)
05802b8e 842 (SELECT id, employer_id, " . $this->relTypes['Employee of']['id'] . ", 1 FROM civicrm_contact WHERE employer_id IN (" . implode(',', $this->Organization) . "))"
6a488035
TO
843 );
844 }
845
846 /**
847 * Create an address for a contact
848 *
849 * @param $cid int: contact id
850 * @param $masterContactId int: set if this is a shared address
77b97be7
EM
851 *
852 * @return array
6a488035
TO
853 */
854 private function _addAddress($cid, $masterContactId = NULL) {
855
856 // Share existing address
857 if ($masterContactId) {
858 $dao = new CRM_Core_DAO_Address();
859 $dao->is_primary = 1;
860 $dao->contact_id = $masterContactId;
861 $dao->find(TRUE);
862 $dao->master_id = $dao->id;
863 $dao->id = NULL;
864 $dao->contact_id = $cid;
865 $dao->is_primary = $this->isPrimary($cid, 'Address');
866 $dao->location_type_id = $this->getContactType($masterContactId) == 'Organization' ? self::WORK : self::HOME;
867 $this->_insert($dao);
868 }
869
870 // Generate new address
871 else {
872 $params = array(
873 'contact_id' => $cid,
874 'location_type_id' => $this->getContactType($cid) == 'Organization' ? self::MAIN : self::HOME,
875 'street_number' => mt_rand(1, 1000),
876 'street_number_suffix' => ucfirst($this->randomChar()),
877 'street_name' => $this->randomItem('street_name'),
878 'street_type' => $this->randomItem('street_type'),
879 'street_number_postdirectional' => $this->randomItem('address_direction'),
880 'county_id' => 1,
881 );
882
883 $params['street_address'] = $params['street_number'] . $params['street_number_suffix'] . " " . $params['street_name'] . " " . $params['street_type'] . " " . $params['street_number_postdirectional'];
884
6a488035
TO
885 if ($params['location_type_id'] == self::MAIN) {
886 $params['supplemental_address_1'] = $this->randomItem('supplemental_addresses_1');
887 }
888
889 // Hack to add lat/long (limited to USA based addresses)
890 list(
891 $params['country_id'],
892 $params['state_province_id'],
893 $params['city'],
894 $params['postal_code'],
895 $params['geo_code_1'],
896 $params['geo_code_2'],
7bc930af 897 ) = $this->getZipCodeInfo();
6a488035
TO
898
899 $this->_addDAO('Address', $params);
900 $params['state'] = $this->states[$params['state_province_id']];
901 return $params;
902 }
903 }
904
905 /**
906 * Add a phone number for a contact
907 *
908 * @param $cid int: contact id
77b97be7
EM
909 *
910 * @return array
6a488035
TO
911 */
912 private function _addPhone($cid) {
913 $area = $this->probability(.5) ? '' : mt_rand(201, 899);
914 $pre = mt_rand(201, 899);
915 $post = mt_rand(1000, 9999);
916 $params = array(
917 'location_type_id' => $this->getContactType($cid) == 'Organization' ? self::MAIN : self::HOME,
918 'contact_id' => $cid,
919 'phone' => ($area ? "($area) " : '') . "$pre-$post",
920 'phone_numeric' => $area . $pre . $post,
921 'phone_type_id' => mt_rand(1, 2),
922 );
923 $this->_addDAO('Phone', $params);
924 return $params;
925 }
926
927 /**
928 * Add an email for a contact
929 *
930 * @param $cid int: contact id
77b97be7
EM
931 * @param $email
932 * @param $locationType
933 *
934 * @return array
6a488035
TO
935 */
936 private function _addEmail($cid, $email, $locationType) {
937 $params = array(
938 'location_type_id' => $locationType,
939 'contact_id' => $cid,
940 'email' => $email,
941 );
942 $this->_addDAO('Email', $params);
943 return $params;
944 }
945
946 /**
947 * Add a website based on organization name
948 * Using common naming patterns
949 *
950 * @param $cid int: contact id
951 * @param $name str: contact name
77b97be7
EM
952 *
953 * @return array
6a488035
TO
954 */
955 private function _addWebsite($cid, $name) {
906a298c 956 $part = array_pad(explode(' ', strtolower($name)), 3, '');
6a488035
TO
957 if (count($part) > 3) {
958 // Abbreviate the place name if it's two words
959 $domain = $part[0][0] . $part[1][0] . $part[2] . $part[3];
960 }
961 else {
962 // Common naming patterns
963 switch (mt_rand(1, 3)) {
964 case 1:
965 $domain = $part[0] . $part[1] . $part[2];
966 break;
906a298c 967
6a488035
TO
968 case 2:
969 $domain = $part[0] . $part[1];
970 break;
906a298c 971
6a488035
TO
972 case 3:
973 $domain = $part[0] . $part[2];
974 break;
975 }
976 }
977 $params = array(
978 'website_type_id' => 1,
979 'location_type_id' => self::MAIN,
980 'contact_id' => $cid,
981 'url' => "http://$domain.org",
982 );
983 $this->_addDAO('Website', $params);
984 return $params;
985 }
986
987 /**
988 * Create an email address based on a person's name
989 * Using common naming patterns
77b97be7 990 *
6a488035
TO
991 * @param $contact obj: individual contact record
992 * @param $domain str: supply a domain (i.e. for a work address)
77b97be7
EM
993 *
994 * @return string
6a488035
TO
995 */
996 private function _individualEmail($contact, $domain = NULL) {
997 $first = $contact->first_name;
998 $last = $contact->last_name;
999 $f = $first[0];
1000 $l = $last[0];
1001 $m = $contact->middle_name ? $contact->middle_name[0] . '.' : '';
1002 // Common naming patterns
1003 switch (mt_rand(1, 6)) {
1004 case 1:
1005 $email = $first . $last;
1006 break;
906a298c 1007
6a488035
TO
1008 case 2:
1009 $email = "$last.$first";
1010 break;
906a298c 1011
6a488035
TO
1012 case 3:
1013 $email = $last . $f;
1014 break;
906a298c 1015
6a488035
TO
1016 case 4:
1017 $email = $first . $l;
1018 break;
906a298c 1019
6a488035
TO
1020 case 5:
1021 $email = "$last.$m$first";
1022 break;
721e8594 1023
6a488035
TO
1024 case 6:
1025 $email = "$f$m$last";
1026 break;
1027 }
91da6cd5 1028 //to ensure we dont insert
6a488035
TO
1029 //invalid characters in email
1030 $email = preg_replace("([^a-zA-Z0-9_\.-]*)", "", $email);
91da6cd5 1031
6a488035
TO
1032 // Some people have numbers in their address
1033 if ($this->probability(.4)) {
1034 $email .= mt_rand(1, 99);
1035 }
1036 // Generate random domain if not specified
1037 if (!$domain) {
1038 $domain = $this->randomItem('email_domain') . '.' . $this->randomItem('email_tld');
1039 }
1040 return strtolower($email) . '@' . $domain;
1041 }
1042
1043 /**
375fffa8 1044 * This method populates the civicrm_entity_tag table
6a488035
TO
1045 */
1046 private function addEntityTag() {
1047
1048 $entity_tag = new CRM_Core_DAO_EntityTag();
1049
1050 // add categories 1,2,3 for Organizations.
1051 for ($i = 0; $i < $this->numOrganization; $i += 2) {
1052 $org_id = $this->Organization[$i];
1053 // echo "org_id = $org_id\n";
1054 $entity_tag->entity_id = $this->Organization[$i];
1055 $entity_tag->entity_table = 'civicrm_contact';
1056 $entity_tag->tag_id = mt_rand(1, 3);
1057 $this->_insert($entity_tag);
1058 }
1059
1060 // add categories 4,5 for Individuals.
1061 for ($i = 0; $i < $this->numIndividual; $i += 2) {
1062 $entity_tag->entity_table = 'civicrm_contact';
1063 $entity_tag->entity_id = $this->Individual[$i];
1064 if (($entity_tag->entity_id) % 3) {
1065 $entity_tag->tag_id = mt_rand(4, 5);
1066 $this->_insert($entity_tag);
1067 }
1068 else {
1069 // some of the individuals are in both categories (4 and 5).
1070 $entity_tag->tag_id = 4;
1071 $this->_insert($entity_tag);
1072 $entity_tag->tag_id = 5;
1073 $this->_insert($entity_tag);
1074 }
1075 }
1076 }
1077
1078 /**
375fffa8 1079 * This method populates the civicrm_group_contact table
6a488035
TO
1080 */
1081 private function addGroup() {
1082 // add the 3 groups first
1083 foreach ($this->sampleData['group'] as $groupName) {
1084 $group = new CRM_Contact_BAO_Group();
1085 $group->name = $group->title = $groupName;
1086 $group->group_type = "\ 11\ 12\ 1";
1087 $group->visibility = 'Public Pages';
1088 $group->is_active = 1;
1089 $group->save();
1090 $group->buildClause();
1091 $group->save();
1092 }
1093
1094 // 60 are for newsletter
1095 for ($i = 0; $i < 60; $i++) {
1096 $groupContact = new CRM_Contact_DAO_GroupContact();
1097 // newsletter subscribers
1098 $groupContact->group_id = 2;
1099 $groupContact->contact_id = $this->Individual[$i];
1100 // always add members
1101 $groupContact->status = 'Added';
1102
6a488035
TO
1103 $subscriptionHistory = new CRM_Contact_DAO_SubscriptionHistory();
1104 $subscriptionHistory->contact_id = $groupContact->contact_id;
1105
1106 $subscriptionHistory->group_id = $groupContact->group_id;
1107 $subscriptionHistory->status = $groupContact->status;
1108 // method
1109 $subscriptionHistory->method = $this->randomItem($this->subscriptionHistoryMethod);
1110 $subscriptionHistory->date = $this->randomDate();
1111 if ($groupContact->status != 'Pending') {
1112 $this->_insert($groupContact);
1113 }
1114 $this->_insert($subscriptionHistory);
1115 }
1116
1117 // 15 volunteers
1118 for ($i = 0; $i < 15; $i++) {
1119 $groupContact = new CRM_Contact_DAO_GroupContact();
1120 // Volunteers
1121 $groupContact->group_id = 3;
1122 $groupContact->contact_id = $this->Individual[$i + 60];
1123 // membership status
1124 $groupContact->status = 'Added';
1125
1126 $subscriptionHistory = new CRM_Contact_DAO_SubscriptionHistory();
1127 $subscriptionHistory->contact_id = $groupContact->contact_id;
1128 $subscriptionHistory->group_id = $groupContact->group_id;
1129 $subscriptionHistory->status = $groupContact->status;
1130 // method
1131 $subscriptionHistory->method = $this->randomItem($this->subscriptionHistoryMethod);
1132 $subscriptionHistory->date = $this->randomDate();
1133
1134 if ($groupContact->status != 'Pending') {
1135 $this->_insert($groupContact);
1136 }
1137 $this->_insert($subscriptionHistory);
1138 }
1139
1140 // 8 advisory board group
1141 for ($i = 0; $i < 8; $i++) {
1142 $groupContact = new CRM_Contact_DAO_GroupContact();
1143 // advisory board group
1144 $groupContact->group_id = 4;
1145 $groupContact->contact_id = $this->Individual[$i * 7];
1146 // membership status
1147 $groupContact->status = 'Added';
1148
1149 $subscriptionHistory = new CRM_Contact_DAO_SubscriptionHistory();
1150 $subscriptionHistory->contact_id = $groupContact->contact_id;
1151 $subscriptionHistory->group_id = $groupContact->group_id;
1152 $subscriptionHistory->status = $groupContact->status;
1153 // method
1154 $subscriptionHistory->method = $this->randomItem($this->subscriptionHistoryMethod);
1155 $subscriptionHistory->date = $this->randomDate();
1156
1157 if ($groupContact->status != 'Pending') {
1158 $this->_insert($groupContact);
1159 }
1160 $this->_insert($subscriptionHistory);
1161 }
1162
1163 //In this function when we add groups that time we are cache the contact fields
1164 //But at the end of setup we are appending sample custom data, so for consistency
1165 //reset the cache.
1166 CRM_Core_BAO_Cache::deleteGroup('contact fields');
1167 }
1168
1169 /**
375fffa8 1170 * This method populates the civicrm_note table
6a488035
TO
1171 */
1172 private function addNote() {
1173 $params = array(
1174 'entity_table' => 'civicrm_contact',
1175 'contact_id' => 1,
1176 'privacy' => 0,
1177 );
1178 for ($i = 0; $i < self::NUM_CONTACT; $i += 10) {
1179 $params['entity_id'] = $this->randomItem($this->contact);
1180 $params['note'] = $this->randomItem('note');
1181 $params['modified_date'] = $this->randomDate();
1182 $this->_addDAO('Note', $params);
1183 }
1184 }
1185
1186 /**
375fffa8 1187 * This method populates the civicrm_activity_history table
6a488035
TO
1188 */
1189 private function addActivity() {
1190 $contactDAO = new CRM_Contact_DAO_Contact();
1191 $contactDAO->contact_type = 'Individual';
1192 $contactDAO->selectAdd();
1193 $contactDAO->selectAdd('id');
1194 $contactDAO->orderBy('sort_name');
1195 $contactDAO->find();
1196
1197 $count = 0;
e7e657f0 1198 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
6a488035
TO
1199 while ($contactDAO->fetch()) {
1200 if ($count++ > 2) {
1201 break;
1202 }
1203 for ($i = 0; $i < self::NUM_ACTIVITY; $i++) {
1204 $activityDAO = new CRM_Activity_DAO_Activity();
ed232f0d 1205 $activityId = CRM_Core_OptionGroup::values('activity_type', NULL, NULL, NULL, ' AND v.name IN ("Tell A Friend", "Pledge Acknowledgment")');
1206 $activityTypeID = array_rand($activityId);
6a488035
TO
1207 $activity = CRM_Core_PseudoConstant::activityType();
1208 $activityDAO->activity_type_id = $activityTypeID;
1209 $activityDAO->subject = "Subject for $activity[$activityTypeID]";
1210 $activityDAO->activity_date_time = $this->randomDate();
6a488035
TO
1211 $activityDAO->status_id = 2;
1212 $this->_insert($activityDAO);
1213
91da6cd5
DL
1214 $activityContactDAO = new CRM_Activity_DAO_ActivityContact();
1215 $activityContactDAO->activity_id = $activityDAO->id;
1216 $activityContactDAO->contact_id = $contactDAO->id;
a24b3694 1217 $activityContactDAO->record_type_id = CRM_Utils_Array::key('Activity Source', $activityContacts);
91da6cd5
DL
1218 $this->_insert($activityContactDAO);
1219
ed232f0d 1220 if ($activityTypeID == 9) {
91da6cd5
DL
1221 $activityContactDAO = new CRM_Activity_DAO_ActivityContact();
1222 $activityContactDAO->activity_id = $activityDAO->id;
1223 $activityContactDAO->contact_id = mt_rand(1, 101);
a24b3694 1224 $activityContactDAO->record_type_id = CRM_Utils_Array::key('Activity Targets', $activityContacts);
91da6cd5 1225 $this->_insert($activityContactDAO);
6a488035 1226 }
6a488035
TO
1227 }
1228 }
1229 }
1230
627456b5
EM
1231 /**
1232 * @return array
1233 */
906a298c 1234 public function getZipCodeInfo() {
6a488035
TO
1235
1236 if (!$this->stateMap) {
1237 $query = 'SELECT id, name, abbreviation from civicrm_state_province where country_id = 1228';
1238 $dao = new CRM_Core_DAO();
1239 $dao->query($query);
1240 $this->stateMap = array();
1241 while ($dao->fetch()) {
1242 $this->stateMap[$dao->abbreviation] = $dao->id;
1243 $this->states[$dao->id] = $dao->name;
1244 }
1245 $dao->free();
1246 }
1247
1248 $offset = mt_rand(1, 43000);
1249 $query = "SELECT city, state, zip, latitude, longitude FROM zipcodes LIMIT $offset, 1";
1250 $dao = new CRM_Core_DAO();
1251 $dao->query($query);
1252 while ($dao->fetch()) {
1253 if ($this->stateMap[$dao->state]) {
1254 $stateID = $this->stateMap[$dao->state];
1255 }
1256 else {
1257 $stateID = 1004;
1258 }
1259
1260 $zip = str_pad($dao->zip, 5, '0', STR_PAD_LEFT);
1261 return array(1228, $stateID, $dao->city, $zip, $dao->latitude, $dao->longitude);
1262 }
1263 }
1264
627456b5
EM
1265 /**
1266 * @param $zipCode
1267 *
1268 * @return array
1269 */
906a298c 1270 public static function getLatLong($zipCode) {
6a488035
TO
1271 $query = "http://maps.google.com/maps?q=$zipCode&output=js";
1272 $userAgent = "Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0";
1273
1274 $ch = curl_init();
1275 curl_setopt($ch, CURLOPT_URL, $query);
1276 curl_setopt($ch, CURLOPT_HEADER, FALSE);
1277 curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
1278 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
1279
1280 // grab URL and pass it to the browser
1281 $outstr = curl_exec($ch);
1282
1283 // close CURL resource, and free up system resources
1284 curl_close($ch);
1285
1286 $preg = "/'(<\?xml.+?)',/s";
1287 preg_match($preg, $outstr, $matches);
1288 if ($matches[1]) {
1289 $xml = simplexml_load_string($matches[1]);
1290 $attributes = $xml->center->attributes();
1291 if (!empty($attributes)) {
1292 return array((float ) $attributes['lat'], (float ) $attributes['lng']);
1293 }
1294 }
1295 return array(NULL, NULL);
1296 }
1297
1298 private function addMembershipType() {
1299 $organizationDAO = new CRM_Contact_DAO_Contact();
1300 $organizationDAO->id = 5;
1301 $organizationDAO->find(TRUE);
1302 $contact_id = $organizationDAO->contact_id;
1303
1304 $membershipType = "INSERT INTO civicrm_membership_type
1305 (name, description, member_of_contact_id, financial_type_id, minimum_fee, duration_unit, duration_interval, period_type, fixed_period_start_day, fixed_period_rollover_day, relationship_type_id, relationship_direction, visibility, weight, is_active)
1306 VALUES
1307 ('General', 'Regular annual membership.', " . $contact_id . ", 2, 100, 'year', 1, 'rolling',null, null, 7, 'b_a', 'Public', 1, 1),
1308 ('Student', 'Discount membership for full-time students.', " . $contact_id . ", 2, 50, 'year', 1, 'rolling', null, null, 7, 'b_a', 'Public', 2, 1),
1309 ('Lifetime', 'Lifetime membership.', " . $contact_id . ", 2, 1200, 'lifetime', 1, 'rolling', null, null, 7, 'b_a', 'Admin', 3, 1);
1310 ";
1311 $this->_query($membershipType);
1312 }
1313
1314 private function addMembership() {
1315 $contact = new CRM_Contact_DAO_Contact();
1316 $contact->query("SELECT id FROM civicrm_contact where contact_type = 'Individual'");
e7e657f0 1317 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
6a488035
TO
1318 while ($contact->fetch()) {
1319 $contacts[] = $contact->id;
1320 }
1321 shuffle($contacts);
1322
1323 $randomContacts = array_slice($contacts, 20, 30);
1324
1325 $sources = array('Payment', 'Donation', 'Check');
1326 $membershipTypes = array(1, 2);
1327 $membershipTypeNames = array('General', 'Student');
1328 $statuses = array(3, 4);
1329
1330 $membership = "
1331INSERT INTO civicrm_membership
1332 (contact_id, membership_type_id, join_date, start_date, end_date, source, status_id)
1333VALUES
1334";
91da6cd5 1335
6a488035
TO
1336 $activity = "
1337INSERT INTO civicrm_activity
91da6cd5
DL
1338 (source_record_id, activity_type_id, subject, activity_date_time, duration, location, phone_id, phone_number, details, priority_id,parent_id, is_test, status_id)
1339VALUES
1340";
1341
1342 $activityContact = "
1343INSERT INTO civicrm_activity_contact
a24b3694 1344 (activity_id, contact_id, record_type_id)
6a488035
TO
1345VALUES
1346";
1347
91da6cd5 1348 $currentActivityID = CRM_Core_DAO::singleValueQuery("SELECT MAX(id) FROM civicrm_activity");
a24b3694 1349 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
6a488035
TO
1350 foreach ($randomContacts as $count => $dontCare) {
1351 $source = $this->randomItem($sources);
91da6cd5
DL
1352 $activitySourceId = $count + 1;
1353 $currentActivityID++;
a24b3694 1354 $activityContact .= "( $currentActivityID, {$randomContacts[$count]}, {$sourceID} )";
6a488035
TO
1355 if ((($count + 1) % 11 == 0)) {
1356 // lifetime membership, status can be anything
1357 $startDate = date('Y-m-d', mktime(0, 0, 0, date('m'), (date('d') - $count), date('Y')));
1358 $membership .= "( {$randomContacts[$count]}, 3, '{$startDate}', '{$startDate}', null, '{$source}', 1)";
91da6cd5 1359 $activity .= "( {$activitySourceId}, 7, 'Lifetime', '{$startDate} 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 )";
6a488035
TO
1360 }
1361 elseif (($count + 1) % 5 == 0) {
1362 // Grace or expired, memberhsip type is random of 1 & 2
1363 $randIndex = array_rand($membershipTypes);
1364 $membershipTypeId = $membershipTypes[$randIndex];
1365 $membershipStatusId = $statuses[$randIndex];
1366 $membershipTypeName = $membershipTypeNames[$randIndex];
1367 $YearFactor = $membershipTypeId * 2;
1368 //reverse the type and consider as year factor.
1369 if ($YearFactor != 2) {
1370 $YearFactor = 1;
1371 }
1372 $dateFactor = ($count * ($YearFactor) * ($YearFactor) * ($YearFactor));
1373 $startDate = date('Y-m-d', mktime(0, 0, 0,
7bc930af
KJ
1374 date('m'),
1375 (date('d') - ($dateFactor)),
1376 (date('Y') - ($YearFactor))
1377 ));
6a488035
TO
1378 $partOfDate = explode('-', $startDate);
1379 $endDate = date('Y-m-d', mktime(0, 0, 0,
7bc930af
KJ
1380 $partOfDate[1],
1381 ($partOfDate[2] - 1),
1382 ($partOfDate[0] + ($YearFactor))
1383 ));
6a488035
TO
1384
1385 $membership .= "( {$randomContacts[$count]}, {$membershipTypeId}, '{$startDate}', '{$startDate}', '{$endDate}', '{$source}', {$membershipStatusId})";
91da6cd5 1386 $activity .= "( {$activitySourceId}, 7, '{$membershipTypeName}', '{$startDate} 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 )";
6a488035
TO
1387 }
1388 elseif (($count + 1) % 2 == 0) {
1389 // membership type 2
1390 $startDate = date('Y-m-d', mktime(0, 0, 0, date('m'), (date('d') - $count), date('Y')));
1391 $endDate = date('Y-m-d', mktime(0, 0, 0, date('m'), (date('d') - ($count + 1)), (date('Y') + 1)));
1392 $membership .= "( {$randomContacts[$count]}, 2, '{$startDate}', '{$startDate}', '{$endDate}', '{$source}', 1)";
91da6cd5 1393 $activity .= "( {$activitySourceId}, 7, 'Student', '{$startDate} 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 )";
6a488035
TO
1394 }
1395 else {
1396 // membership type 1
1397 $startDate = date('Y-m-d', mktime(0, 0, 0, date('m'), (date('d') - $count), date('Y')));
1398 $endDate = date('Y-m-d', mktime(0, 0, 0, date('m'), (date('d') - ($count + 1)), (date('Y') + 2)));
1399 $membership .= "( {$randomContacts[$count]}, 1, '{$startDate}', '{$startDate}', '{$endDate}', '{$source}', 1)";
91da6cd5 1400 $activity .= "( {$activitySourceId}, 7, 'General', '{$startDate} 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 )";
6a488035
TO
1401 }
1402
1403 if ($count != 29) {
1404 $membership .= ",";
1405 $activity .= ",";
91da6cd5 1406 $activityContact .= ",";
6a488035
TO
1407 }
1408 }
1409
1410 $this->_query($membership);
6a488035 1411 $this->_query($activity);
91da6cd5 1412 $this->_query($activityContact);
6a488035
TO
1413 }
1414
627456b5
EM
1415 /**
1416 * @param $date
1417 *
1418 * @return string
1419 */
906a298c 1420 public static function repairDate($date) {
6a488035
TO
1421 $dropArray = array('-' => '', ':' => '', ' ' => '');
1422 return strtr($date, $dropArray);
1423 }
1424
1425 private function addMembershipLog() {
1426 $membership = new CRM_Member_DAO_Membership();
1427 $membership->query("SELECT id FROM civicrm_membership");
1428 while ($membership->fetch()) {
1429 $ids[] = $membership->id;
1430 }
1431 foreach ($ids as $id) {
1432 $membership = new CRM_Member_DAO_Membership();
1433 $membership->id = $id;
1434 $membershipLog = new CRM_Member_DAO_MembershipLog();
1435 if ($membership->find(TRUE)) {
1436 $membershipLog->membership_id = $membership->id;
1437 $membershipLog->status_id = $membership->status_id;
1438 $membershipLog->start_date = self::repairDate($membership->start_date);
1439 $membershipLog->end_date = self::repairDate($membership->end_date);
1440 $membershipLog->modified_id = $membership->contact_id;
1441 $membershipLog->modified_date = date("Ymd");
1442 $membershipLog->membership_type_id = $membership->membership_type_id;
1443 $membershipLog->save();
1444 }
1445 $membershipLog = NULL;
1446 }
1447 }
1448
1449 private function addEvent() {
1450 $event = "INSERT INTO civicrm_address ( contact_id, location_type_id, is_primary, is_billing, street_address, street_number, street_number_suffix, street_number_predirectional, street_name, street_type, street_number_postdirectional, street_unit, supplemental_address_1, supplemental_address_2, supplemental_address_3, city, county_id, state_province_id, postal_code_suffix, postal_code, usps_adc, country_id, geo_code_1, geo_code_2, timezone)
1451 VALUES
1452 ( NULL, 1, 1, 1, '14S El Camino Way E', 14, 'S', NULL, 'El Camino', 'Way', NULL, NULL, NULL, NULL, NULL, 'Collinsville', NULL, 1006, NULL, '6022', NULL, 1228, 41.8328, -72.9253, NULL),
1453 ( NULL, 1, 1, 1, '11B Woodbridge Path SW', 11, 'B', NULL, 'Woodbridge', 'Path', NULL, NULL, NULL, NULL, NULL, 'Dayton', NULL, 1034, NULL, '45417', NULL, 1228, 39.7531, -84.2471, NULL),
1454 ( NULL, 1, 1, 1, '581O Lincoln Dr SW', 581, 'O', NULL, 'Lincoln', 'Dr', NULL, NULL, NULL, NULL, NULL, 'Santa Fe', NULL, 1030, NULL, '87594', NULL, 1228, 35.5212, -105.982, NULL)
1455 ";
1456 $this->_query($event);
1457
1458 $sql = "SELECT id from civicrm_address where street_address = '14S El Camino Way E'";
1459 $eventAdd1 = CRM_Core_DAO::singleValueQuery($sql);
1460 $sql = "SELECT id from civicrm_address where street_address = '11B Woodbridge Path SW'";
1461 $eventAdd2 = CRM_Core_DAO::singleValueQuery($sql);
1462 $sql = "SELECT id from civicrm_address where street_address = '581O Lincoln Dr SW'";
1463 $eventAdd3 = CRM_Core_DAO::singleValueQuery($sql);
1464
1465 $event = "INSERT INTO civicrm_email (contact_id, location_type_id, email, is_primary, is_billing, on_hold, hold_date, reset_date)
1466 VALUES
1467 (NULL, 1, 'development@example.org', 0, 0, 0, NULL, NULL),
1468 (NULL, 1, 'tournaments@example.org', 0, 0, 0, NULL, NULL),
1469 (NULL, 1, 'celebration@example.org', 0, 0, 0, NULL, NULL)
1470 ";
1471 $this->_query($event);
1472
1473 $sql = "SELECT id from civicrm_email where email = 'development@example.org'";
1474 $eventEmail1 = CRM_Core_DAO::singleValueQuery($sql);
1475 $sql = "SELECT id from civicrm_email where email = 'tournaments@example.org'";
1476 $eventEmail2 = CRM_Core_DAO::singleValueQuery($sql);
1477 $sql = "SELECT id from civicrm_email where email = 'celebration@example.org'";
1478 $eventEmail3 = CRM_Core_DAO::singleValueQuery($sql);
1479
1480 $event = "INSERT INTO civicrm_phone (contact_id, location_type_id, is_primary, is_billing, mobile_provider_id, phone, phone_numeric, phone_type_id)
1481 VALUES
1482 (NULL, 1, 0, 0, NULL, '204 222-1000', '2042221000', '1'),
1483 (NULL, 1, 0, 0, NULL, '204 223-1000', '2042231000', '1'),
1484 (NULL, 1, 0, 0, NULL, '303 323-1000', '3033231000', '1')
1485 ";
1486 $this->_query($event);
1487
1488 $sql = "SELECT id from civicrm_phone where phone = '204 222-1000'";
1489 $eventPhone1 = CRM_Core_DAO::singleValueQuery($sql);
1490 $sql = "SELECT id from civicrm_phone where phone = '204 223-1000'";
1491 $eventPhone2 = CRM_Core_DAO::singleValueQuery($sql);
1492 $sql = "SELECT id from civicrm_phone where phone = '303 323-1000'";
1493 $eventPhone3 = CRM_Core_DAO::singleValueQuery($sql);
1494
1495 $event = "INSERT INTO civicrm_loc_block ( address_id, email_id, phone_id, address_2_id, email_2_id, phone_2_id)
1496 VALUES
1497 ( $eventAdd1, $eventEmail1, $eventPhone1, NULL,NULL,NULL),
1498 ( $eventAdd2, $eventEmail2, $eventPhone2, NULL,NULL,NULL),
1499 ( $eventAdd3, $eventEmail3, $eventPhone3, NULL,NULL,NULL)
1500 ";
1501
1502 $this->_query($event);
1503
1504 $sql = "SELECT id from civicrm_loc_block where phone_id = $eventPhone1 AND email_id = $eventEmail1 AND address_id = $eventAdd1";
1505 $eventLok1 = CRM_Core_DAO::singleValueQuery($sql);
1506 $sql = "SELECT id from civicrm_loc_block where phone_id = $eventPhone2 AND email_id = $eventEmail2 AND address_id = $eventAdd2";
1507 $eventLok2 = CRM_Core_DAO::singleValueQuery($sql);
1508 $sql = "SELECT id from civicrm_loc_block where phone_id = $eventPhone3 AND email_id = $eventEmail3 AND address_id = $eventAdd3";
1509 $eventLok3 = CRM_Core_DAO::singleValueQuery($sql);
1510
1511 $event = "INSERT INTO civicrm_event
1512 ( title, summary, description, event_type_id, participant_listing_id, is_public, start_date, end_date, is_online_registration, registration_link_text, max_participants, event_full_text, is_monetary, financial_type_id, is_map, is_active, fee_label, is_show_location, loc_block_id,intro_text, footer_text, confirm_title, confirm_text, confirm_footer_text, is_email_confirm, confirm_email_text, confirm_from_name, confirm_from_email, cc_confirm, bcc_confirm, default_fee_id, thankyou_title, thankyou_text, thankyou_footer_text, is_pay_later, pay_later_text, pay_later_receipt, is_multiple_registrations, allow_same_participant_emails, currency )
1513 VALUES
1514 ( 'Fall Fundraiser Dinner', 'Kick up your heels at our Fall Fundraiser Dinner/Dance at Glen Echo Park! Come by yourself or bring a partner, friend or the entire family!', 'This event benefits our teen programs. Admission includes a full 3 course meal and wine or soft drinks. Grab your dancing shoes, bring the kids and come join the party!', 3, 1, 1, '" . date('Y-m-d 17:00:00', strtotime("+6 months")) . "', '" . date('Y-m-d 17:00:00', strtotime("+6 months +2 days")) . "', 1, 'Register Now', 100, 'Sorry! The Fall Fundraiser Dinner is full. Please call Jane at 204 222-1000 ext 33 if you want to be added to the waiting list.', 1, 4, 1, 1, 'Dinner Contribution', 1 ,$eventLok1,'Fill in the information below to join as at this wonderful dinner event.', NULL, 'Confirm Your Registration Information', 'Review the information below carefully.', NULL, 1, 'Contact the Development Department if you need to make any changes to your registration.', 'Fundraising Dept.', 'development@example.org', NULL, NULL, NULL, 'Thanks for Registering!', '<p>Thank you for your support. Your contribution will help us build even better tools.</p><p>Please tell your friends and colleagues about this wonderful event.</p>', '<p><a href=http://civicrm.org>Back to CiviCRM Home Page</a></p>', 1, 'I will send payment by check', 'Send a check payable to Our Organization within 3 business days to hold your reservation. Checks should be sent to: 100 Main St., Suite 3, San Francisco CA 94110', 1, 0, 'USD' ),
1515 ( 'Summer Solstice Festival Day Concert', 'Festival Day is coming! Join us and help support your parks.', 'We will gather at noon, learn a song all together, and then join in a joyous procession to the pavilion. We will be one of many groups performing at this wonderful concert which benefits our city parks.', 5, 1, 1, '" . date('Y-m-d 12:00:00', strtotime("-1 day")) . "', '" . date('Y-m-d 17:00:00', strtotime("-1 day")) . "', 1, 'Register Now', 50, 'We have all the singers we can handle. Come to the pavilion anyway and join in from the audience.', 1, 2, NULL, 1, 'Festival Fee', 1, $eventLok2, 'Complete the form below and click Continue to register online for the festival. Or you can register by calling us at 204 222-1000 ext 22.', '', 'Confirm Your Registration Information', '', '', 1, 'This email confirms your registration. If you have questions or need to change your registration - please do not hesitate to call us.', 'Event Dept.', 'events@example.org', '', NULL, NULL, 'Thanks for Your Joining In!', '<p>Thank you for your support. Your participation will help build new parks.</p><p>Please tell your friends and colleagues about the concert.</p>', '<p><a href=http://civicrm.org>Back to CiviCRM Home Page</a></p>', 0, NULL, NULL, 1, 0, 'USD' ),
1516 ( 'Rain-forest Cup Youth Soccer Tournament', 'Sign up your team to participate in this fun tournament which benefits several Rain-forest protection groups in the Amazon basin.', 'This is a FYSA Sanctioned Tournament, which is open to all USSF/FIFA affiliated organizations for boys and girls in age groups: U9-U10 (6v6), U11-U12 (8v8), and U13-U17 (Full Sided).', 3, 1, 1, '" . date('Y-m-d 07:00:00', strtotime("+7 months")) . "', '" . date('Y-m-d 17:00:00', strtotime("+7 months +3 days")) . "', 1, 'Register Now', 500, 'Sorry! All available team slots for this tournament have been filled. Contact Jill Futbol for information about the waiting list and next years event.', 1, 4, NULL, 1, 'Tournament Fees',1, $eventLok3, 'Complete the form below to register your team for this year''s tournament.', '<em>A Soccer Youth Event</em>', 'Review and Confirm Your Registration Information', '', '<em>A Soccer Youth Event</em>', 1, 'Contact our Tournament Director for eligibility details.', 'Tournament Director', 'tournament@example.org', '', NULL, NULL, 'Thanks for Your Support!', '<p>Thank you for your support. Your participation will help save thousands of acres of rainforest.</p>', '<p><a href=http://civicrm.org>Back to CiviCRM Home Page</a></p>', 0, NULL, NULL, 0, 0, 'USD' )
1517 ";
1518 $this->_query($event);
1519
1520 //CRM-4464
1521 $eventTemplates = "INSERT INTO civicrm_event
1522 ( is_template, template_title, event_type_id, default_role_id, participant_listing_id, is_public, is_monetary, is_online_registration, is_multiple_registrations, allow_same_participant_emails, is_email_confirm, financial_type_id, fee_label, confirm_title, thankyou_title, confirm_from_name, confirm_from_email, is_active, currency )
1523 VALUES
1524 ( 1, 'Free Meeting without Online Registration', 4, 1, 1, 1, 0, 0, null, null, null, null, null, null, null, null, null, 1, 'USD' ),
1525 ( 1, 'Free Meeting with Online Registration', 4, 1, 1, 1, 0, 1, 1, 1, 0, null, null, 'Confirm Your Registration Information', 'Thanks for Registering!', null, null, 1, 'USD' ),
1526 ( 1, 'Paid Conference with Online Registration', 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 'Conference Fee', 'Confirm Your Registration Information', 'Thanks for Registering!', 'Event Template Dept.', 'event_templates@example.org', 1, 'USD' )";
1527
1528 $this->_query($eventTemplates);
1529
1530 $ufJoinValues = $tellFriendValues = array();
1531 $profileID = CRM_Core_DAO::singleValueQuery("Select id from civicrm_uf_group where name ='event_registration'");
1532
1533 // grab id's for all events and event templates
1534 $query = "
1535SELECT id
1536 FROM civicrm_event";
1537
1538 $template = CRM_Core_DAO::executeQuery($query);
1539 while ($template->fetch()) {
1540 if ($profileID) {
1541 $ufJoinValues[] = "( 1, 'CiviEvent', 'civicrm_event', {$template->id}, 1, {$profileID} )";
1542 }
1543 $tellFriendValues[] = "( 'civicrm_event', {$template->id}, 'Tell A Friend', '<p>Help us spread the word about this event. Use the space below to personalize your email message - let your friends know why you''re attending. Then fill in the name(s) and email address(es) and click ''Send Your Message''.</p>', 'Thought you might be interested in checking out this event. I''m planning on attending.', NULL, 'Thanks for Spreading the Word', '<p>Thanks for spreading the word about this event to your friends.</p>', 1)";
1544 }
1545
1546 //insert values in civicrm_uf_join for the required event_registration profile - CRM-9587
1547 if (!empty($ufJoinValues)) {
1548 $includeProfile = "INSERT INTO civicrm_uf_join
1549 (is_active, module, entity_table, entity_id, weight, uf_group_id )
1550 VALUES " . implode(',', $ufJoinValues);
1551 $this->_query($includeProfile);
1552 }
1553
1554 //insert values in civicrm_tell_friend
1555 if (!empty($tellFriendValues)) {
1556 $tellFriend = "INSERT INTO civicrm_tell_friend
1557 (entity_table, entity_id, title, intro, suggested_message,
1558 general_link, thankyou_title, thankyou_text, is_active)
1559 VALUES " . implode(',', $tellFriendValues);
1560 $this->_query($tellFriend);
1561 }
1562 }
1563
1564 private function addParticipant() {
1565 $contact = new CRM_Contact_DAO_Contact();
1566 $contact->query("SELECT id FROM civicrm_contact");
1567 while ($contact->fetch()) {
1568 $contacts[] = $contact->id;
1569 }
1570 shuffle($contacts);
1571 $randomContacts = array_slice($contacts, 20, 50);
1572
1573 $participant = "
1574INSERT INTO civicrm_participant
1575 (contact_id, event_id, status_id, role_id, register_date, source, fee_level, is_test, fee_amount, fee_currency)
1576VALUES
1577 ( " . $randomContacts[0] . ", 1, 1, 1, '2009-01-21', 'Check', 'Single', 0, 50, 'USD'),
1578 ( " . $randomContacts[1] . ", 2, 2, 2, '2008-05-07', 'Credit Card', 'Soprano', 0, 50, 'USD'),
1579 ( " . $randomContacts[2] . ", 3, 3, 3, '2008-05-05', 'Credit Card', 'Tiny-tots (ages 5-8)', 0, 800, 'USD') ,
1580 ( " . $randomContacts[3] . ", 1, 4, 4, '2008-10-21', 'Direct Transfer', 'Single', 0, 50, 'USD'),
1581 ( " . $randomContacts[4] . ", 2, 1, 1, '2008-01-10', 'Check', 'Soprano', 0, 50, 'USD'),
1582 ( " . $randomContacts[5] . ", 3, 2, 2, '2008-03-05', 'Direct Transfer', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1583 ( " . $randomContacts[6] . ", 1, 3, 3, '2009-07-21', 'Direct Transfer', 'Single', 0, 50, 'USD'),
1584 ( " . $randomContacts[7] . ", 2, 4, 4, '2009-03-07', 'Credit Card', 'Soprano', 0, 50, 'USD'),
1585 ( " . $randomContacts[8] . ", 3, 1, 1, '2008-02-05', 'Direct Transfer', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1586 ( " . $randomContacts[9] . ", 1, 2, 2, '2008-02-01', 'Check', 'Single', 0, 50, 'USD'),
1587 ( " . $randomContacts[10] . ", 2, 3, 3, '2009-01-10', 'Direct Transfer', 'Soprano', 0, 50, 'USD'),
1588 ( " . $randomContacts[11] . ", 3, 4, 4, '2009-03-06', 'Credit Card', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1589 ( " . $randomContacts[12] . ", 1, 1, 2, '2008-06-04', 'Credit Card', 'Single', 0, 50, 'USD'),
1590 ( " . $randomContacts[13] . ", 2, 2, 3, '2008-01-10', 'Direct Transfer', 'Soprano', 0, 50, 'USD'),
1591 ( " . $randomContacts[14] . ", 3, 4, 1, '2008-07-04', 'Check', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1592 ( " . $randomContacts[15] . ", 1, 4, 2, '2009-01-21', 'Credit Card', 'Single', 0, 50, 'USD'),
1593 ( " . $randomContacts[16] . ", 2, 2, 3, '2008-01-10', 'Credit Card', 'Soprano', 0, 50, 'USD'),
1594 ( " . $randomContacts[17] . ", 3, 3, 1, '2009-03-05', 'Credit Card', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1595 ( " . $randomContacts[18] . ", 1, 2, 1, '2008-10-21', 'Direct Transfer', 'Single', 0, 50, 'USD'),
1596 ( " . $randomContacts[19] . ", 2, 4, 1, '2009-01-10', 'Credit Card', 'Soprano', 0, 50, 'USD'),
1597 ( " . $randomContacts[20] . ", 3, 1, 4, '2008-03-25', 'Check', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1598 ( " . $randomContacts[21] . ", 1, 2, 3, '2009-10-21', 'Direct Transfer', 'Single', 0, 50, 'USD'),
1599 ( " . $randomContacts[22] . ", 2, 4, 1, '2008-01-10', 'Direct Transfer', 'Soprano', 0, 50, 'USD'),
1600 ( " . $randomContacts[23] . ", 3, 3, 1, '2008-03-11', 'Credit Card', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1601 ( " . $randomContacts[24] . ", 3, 2, 2, '2008-04-05', 'Direct Transfer', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1602 ( " . $randomContacts[25] . ", 1, 1, 1, '2009-01-21', 'Check', 'Single', 0, 50, 'USD'),
1603 ( " . $randomContacts[26] . ", 2, 2, 2, '2008-05-07', 'Credit Card', 'Soprano', 0, 50, 'USD'),
1604 ( " . $randomContacts[27] . ", 3, 3, 3, '2009-12-12', 'Direct Transfer', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1605 ( " . $randomContacts[28] . ", 1, 4, 4, '2009-12-13', 'Credit Card', 'Single', 0, 50, 'USD'),
1606 ( " . $randomContacts[29] . ", 2, 1, 1, '2009-12-14', 'Direct Transfer', 'Soprano', 0, 50, 'USD'),
1607 ( " . $randomContacts[30] . ", 3, 2, 2, '2009-12-15', 'Credit Card', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1608 ( " . $randomContacts[31] . ", 1, 3, 3, '2009-07-21', 'Check', 'Single', 0, 50, 'USD'),
1609 ( " . $randomContacts[32] . ", 2, 4, 4, '2009-03-07', 'Direct Transfer', 'Soprano', 0, 50, 'USD'),
1610 ( " . $randomContacts[33] . ", 3, 1, 1, '2009-12-15', 'Credit Card', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1611 ( " . $randomContacts[34] . ", 1, 2, 2, '2009-12-13', 'Direct Transfer', 'Single', 0, 50, 'USD'),
1612 ( " . $randomContacts[35] . ", 2, 3, 3, '2009-01-10', 'Direct Transfer', 'Soprano', 0, 50, 'USD'),
1613 ( " . $randomContacts[36] . ", 3, 4, 4, '2009-03-06', 'Check', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1614 ( " . $randomContacts[37] . ", 1, 1, 2, '2009-12-13', 'Direct Transfer', 'Single', 0, 50, 'USD'),
1615 ( " . $randomContacts[38] . ", 2, 2, 3, '2008-01-10', 'Direct Transfer', 'Soprano', 0, 50, 'USD'),
1616 ( " . $randomContacts[39] . ", 3, 4, 1, '2009-12-14', 'Credit Card', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1617 ( " . $randomContacts[40] . ", 1, 4, 2, '2009-01-21', 'Credit Card', 'Single', 0, 50, 'USD'),
1618 ( " . $randomContacts[41] . ", 2, 2, 3, '2009-12-15', 'Credit Card', 'Soprano', 0, 50, 'USD'),
1619 ( " . $randomContacts[42] . ", 3, 3, 1, '2009-03-05', 'Credit Card', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1620 ( " . $randomContacts[43] . ", 1, 2, 1, '2009-12-13', 'Direct Transfer', 'Single', 0, 50, 'USD'),
1621 ( " . $randomContacts[44] . ", 2, 4, 1, '2009-01-10', 'Direct Transfer', 'Soprano', 0, 50, 'USD'),
1622 ( " . $randomContacts[45] . ", 3, 1, 4, '2009-12-13', 'Check', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1623 ( " . $randomContacts[46] . ", 1, 2, 3, '2009-10-21', 'Credit Card', 'Single', 0, 50, 'USD'),
1624 ( " . $randomContacts[47] . ", 2, 4, 1, '2009-12-10', 'Credit Card', 'Soprano', 0, 50, 'USD'),
1625 ( " . $randomContacts[48] . ", 3, 3, 1, '2009-03-11', 'Credit Card', 'Tiny-tots (ages 5-8)', 0, 800, 'USD'),
1626 ( " . $randomContacts[49] . ", 3, 2, 2, '2009-04-05', 'Check', 'Tiny-tots (ages 5-8)', 0, 800, 'USD');
1627";
1628 $this->_query($participant);
1629
1630 $query = "
1631INSERT INTO civicrm_activity
91da6cd5 1632 (source_record_id, activity_type_id, subject, activity_date_time, duration, location, phone_id, phone_number, details, priority_id,parent_id, is_test, status_id)
6a488035 1633VALUES
91da6cd5
DL
1634 (01, 5, 'NULL', '2009-01-21 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1635 (02, 5, 'NULL', '2008-05-07 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1636 (03, 5, 'NULL', '2008-05-05 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1637 (04, 5, 'NULL', '2008-10-21 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1638 (05, 5, 'NULL', '2008-01-10 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1639 (06, 5, 'NULL', '2008-03-05 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1640 (07, 5, 'NULL', '2009-07-21 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1641 (08, 5, 'NULL', '2009-03-07 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1642 (09, 5, 'NULL', '2008-02-05 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1643 (10, 5, 'NULL', '2008-02-01 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1644 (11, 5, 'NULL', '2009-01-10 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1645 (12, 5, 'NULL', '2009-03-06 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1646 (13, 5, 'NULL', '2008-06-04 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1647 (14, 5, 'NULL', '2008-01-10 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1648 (15, 5, 'NULL', '2008-07-04 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1649 (16, 5, 'NULL', '2009-01-21 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1650 (17, 5, 'NULL', '2008-01-10 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1651 (18, 5, 'NULL', '2009-03-05 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1652 (19, 5, 'NULL', '2008-10-21 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1653 (20, 5, 'NULL', '2009-01-10 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1654 (21, 5, 'NULL', '2008-03-25 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1655 (22, 5, 'NULL', '2009-10-21 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1656 (23, 5, 'NULL', '2008-01-10 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1657 (24, 5, 'NULL', '2008-03-11 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1658 (25, 5, 'NULL', '2008-04-05 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1659 (26, 5, 'NULL', '2009-01-21 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1660 (27, 5, 'NULL', '2008-05-07 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1661 (28, 5, 'NULL', '2009-12-12 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1662 (29, 5, 'NULL', '2009-12-13 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1663 (30, 5, 'NULL', '2009-12-14 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1664 (31, 5, 'NULL', '2009-12-15 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1665 (32, 5, 'NULL', '2009-07-21 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1666 (33, 5, 'NULL', '2009-03-07 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1667 (34, 5, 'NULL', '2009-12-15 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1668 (35, 5, 'NULL', '2009-12-13 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1669 (36, 5, 'NULL', '2009-01-10 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1670 (37, 5, 'NULL', '2009-03-06 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1671 (38, 5, 'NULL', '2009-12-13 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1672 (39, 5, 'NULL', '2008-01-10 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1673 (40, 5, 'NULL', '2009-12-14 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1674 (41, 5, 'NULL', '2009-01-21 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1675 (42, 5, 'NULL', '2009-12-15 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1676 (43, 5, 'NULL', '2009-03-05 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1677 (44, 5, 'NULL', '2009-12-13 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1678 (45, 5, 'NULL', '2009-01-10 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1679 (46, 5, 'NULL', '2009-12-13 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1680 (47, 5, 'NULL', '2009-10-21 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1681 (48, 5, 'NULL', '2009-12-10 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1682 (49, 5, 'NULL', '2009-03-11 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1683 (50, 5, 'NULL', '2009-04-05 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 )
6a488035
TO
1684 ";
1685 $this->_query($query);
91da6cd5
DL
1686
1687 $activityContact = "
1688INSERT INTO civicrm_activity_contact
a24b3694 1689 (contact_id, activity_id, record_type_id)
91da6cd5
DL
1690VALUES
1691";
e7e657f0 1692 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
91da6cd5
DL
1693 $currentActivityID = CRM_Core_DAO::singleValueQuery("SELECT MAX(id) FROM civicrm_activity");
1694 $currentActivityID -= 50;
a24b3694 1695 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
91da6cd5
DL
1696 for ($i = 0; $i < 50; $i++) {
1697 $currentActivityID++;
a24b3694 1698 $activityContact .= "({$randomContacts[$i]}, $currentActivityID, $sourceID)";
91da6cd5
DL
1699 if ($i != 49) {
1700 $activityContact .= ", ";
1701 }
1702 }
1703 $this->_query($activityContact);
6a488035
TO
1704 }
1705
1706 private function addPCP() {
1707 $query = "
1708INSERT INTO `civicrm_pcp`
24f44c4d 1709 (contact_id, status_id, title, intro_text, page_text, donate_link_text, page_id, page_type, is_thermometer, is_honor_roll, goal_amount, currency, is_active, pcp_block_id, is_notify)
6a488035 1710VALUES
24f44c4d 1711 ({$this->Individual[3]}, 2, 'My Personal Civi Fundraiser', 'I''m on a mission to get all my friends and family to help support my favorite open-source civic sector CRM.', '<p>Friends and family - please help build much needed infrastructure for the civic sector by supporting my personal campaign!</p>\r\n<p><a href=\"http://civicrm.org\">You can learn more about CiviCRM here</a>.</p>\r\n<p>Then click the <strong>Contribute Now</strong> button to go to our easy-to-use online contribution form.</p>', 'Contribute Now', 1, 'contribute', 1, 1, 5000.00, 'USD', 1, 1, 1);
6a488035
TO
1712";
1713 $this->_query($query);
1714 }
1715
1716 private function addContribution() {
1717 $query = "
1718INSERT INTO civicrm_contribution
1719 (contact_id, financial_type_id, payment_instrument_id, receive_date, non_deductible_amount, total_amount, trxn_id, check_number, currency, cancel_date, cancel_reason, receipt_date, thankyou_date, source )
1720VALUES
1721 (2, 1, 4, '2010-04-11 00:00:00', 0.00, 125.00, NULL, '1041', 'USD', NULL, NULL, NULL, NULL, 'Apr 2007 Mailer 1' ),
1722 (4, 1, 1, '2010-03-21 00:00:00', 0.00, 50.00, 'P20901X1', NULL, 'USD', NULL, NULL, NULL, NULL, 'Online: Save the Penguins' ),
1723 (6, 1, 4, '2010-04-29 00:00:00', 0.00, 25.00, NULL, '2095', 'USD', NULL, NULL, NULL, NULL, 'Apr 2007 Mailer 1' ),
1724 (8, 1, 4, '2010-04-11 00:00:00', 0.00, 50.00, NULL, '10552', 'USD', NULL, NULL, NULL, NULL, 'Apr 2007 Mailer 1' ),
1725 (16, 1, 4, '2010-04-15 00:00:00', 0.00, 500.00, NULL, '509', 'USD', NULL, NULL, NULL, NULL, 'Apr 2007 Mailer 1' ),
1726 (19, 1, 4, '2010-04-11 00:00:00', 0.00, 175.00, NULL, '102', 'USD', NULL, NULL, NULL, NULL, 'Apr 2007 Mailer 1' ),
1727 (82, 1, 1, '2010-03-27 00:00:00', 0.00, 50.00, 'P20193L2', NULL, 'USD', NULL, NULL, NULL, NULL, 'Online: Save the Penguins' ),
1728 (92, 1, 1, '2010-03-08 00:00:00', 0.00, 10.00, 'P40232Y3', NULL, 'USD', NULL, NULL, NULL, NULL, 'Online: Help CiviCRM' ),
1729 (34, 1, 1, '2010-04-22 00:00:00', 0.00, 250.00, 'P20193L6', NULL, 'USD', NULL, NULL, NULL, NULL, 'Online: Help CiviCRM' ),
1730 (71, 1, 1, '2009-07-01 11:53:50', 0.00, 500.00, 'PL71', NULL, 'USD', NULL, NULL, NULL, NULL, NULL ),
1731 (43, 1, 1, '2009-07-01 12:55:41', 0.00, 200.00, 'PL43II', NULL, 'USD', NULL, NULL, NULL, NULL, NULL ),
1732 (32, 1, 1, '2009-10-01 11:53:50', 0.00, 200.00, 'PL32I', NULL, 'USD', NULL, NULL, NULL, NULL, NULL ),
1733 (32, 1, 1, '2009-12-01 12:55:41', 0.00, 200.00, 'PL32II', NULL, 'USD', NULL, NULL, NULL, NULL, NULL );
1734";
1735 $this->_query($query);
1736
91da6cd5 1737 $currentActivityID = CRM_Core_DAO::singleValueQuery("SELECT MAX(id) FROM civicrm_activity");
6a488035
TO
1738 $query = "
1739INSERT INTO civicrm_activity
91da6cd5 1740 (source_record_id, activity_type_id, subject, activity_date_time, duration, location, phone_id, phone_number, details, priority_id,parent_id, is_test, status_id)
6a488035 1741VALUES
91da6cd5
DL
1742 (1, 6, '$ 125.00-Apr 2007 Mailer 1', '2010-04-11 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1743 (2, 6, '$ 50.00-Online: Save the Penguins', '2010-03-21 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1744 (3, 6, '$ 25.00-Apr 2007 Mailer 1', '2010-04-29 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1745 (4, 6, '$ 50.00-Apr 2007 Mailer 1', '2010-04-11 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1746 (5, 6, '$ 500.00-Apr 2007 Mailer 1', '2010-04-15 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1747 (6, 6, '$ 175.00-Apr 2007 Mailer 1', '2010-04-11 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1748 (7, 6, '$ 50.00-Online: Save the Penguins', '2010-03-27 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1749 (8, 6, '$ 10.00-Online: Save the Penguins', '2010-03-08 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1750 (9, 6, '$ 250.00-Online: Save the Penguins', '2010-04-22 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1751 (10, 6, NULL, '2009-07-01 11:53:50', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1752 (11, 6, NULL, '2009-07-01 12:55:41', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1753 (12, 6, NULL, '2009-10-01 11:53:50', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 ),
1754 (13, 6, NULL, '2009-12-01 12:55:41', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 );
6a488035
TO
1755 ";
1756 $this->_query($query);
91da6cd5
DL
1757
1758 $activityContact = "
1759INSERT INTO civicrm_activity_contact
a24b3694 1760 (contact_id, activity_id, record_type_id)
91da6cd5
DL
1761VALUES
1762";
1763
1764 $arbitraryNumbers = array(2, 4, 6, 8, 16, 19, 82, 92, 34, 71, 43, 32, 32);
1765 for ($i = 0; $i < count($arbitraryNumbers); $i++) {
1766 $currentActivityID++;
a24b3694 1767 $activityContact .= "({$arbitraryNumbers[$i]}, $currentActivityID, 2)";
91da6cd5
DL
1768 if ($i != count($arbitraryNumbers) - 1) {
1769 $activityContact .= ", ";
1770 }
1771 }
1772 $this->_query($activityContact);
6a488035
TO
1773 }
1774
1775 private function addSoftContribution() {
1776
1777 $sql = "SELECT id from civicrm_contribution where contact_id = 92";
1778 $contriId1 = CRM_Core_DAO::singleValueQuery($sql);
1779
1780 $sql = "SELECT id from civicrm_contribution where contact_id = 34";
1781 $contriId2 = CRM_Core_DAO::singleValueQuery($sql);
1782
51fa20cb 1783 $sql = "SELECT cov.value FROM civicrm_option_value cov LEFT JOIN civicrm_option_group cog ON cog.id = cov.option_group_id WHERE cov.name = 'pcp' AND cog.name = 'soft_credit_type'";
1784
1785 $pcpId = CRM_Core_DAO::singleValueQuery($sql);
1786
6a488035
TO
1787 $query = "
1788INSERT INTO `civicrm_contribution_soft`
51fa20cb 1789 ( contribution_id, contact_id ,amount , currency, pcp_id , pcp_display_in_roll ,pcp_roll_nickname,pcp_personal_note, soft_credit_type_id )
6a488035 1790VALUES
51fa20cb 1791 ( $contriId1, {$this->Individual[3]}, 10.00, 'USD', 1, 1, 'Jones Family', 'Helping Hands', $pcpId),
1792 ( $contriId2, {$this->Individual[3]}, 250.00, 'USD', 1, 1, 'Annie and the kids', 'Annie Helps', $pcpId);
6a488035 1793 ";
51fa20cb 1794
6a488035
TO
1795 $this->_query($query);
1796 }
1797
1798 private function addPledge() {
1799 $pledge = "INSERT INTO civicrm_pledge
133e2c99 1800 (contact_id, financial_type_id, contribution_page_id, amount, original_installment_amount, currency,frequency_unit, frequency_interval, frequency_day, installments, start_date, create_date, acknowledge_date, modified_date, cancel_date, end_date, status_id, is_test)
6a488035 1801 VALUES
133e2c99 1802 (71, 1, 1, 500.00, '500', 'USD', 'month', 1, 1, 1, '2009-07-01 00:00:00', '2009-06-26 00:00:00', NULL, NULL, NULL,'2009-07-01 00:00:00', 1, 0),
1803 (43, 1, 1, 800.00, '200', 'USD', 'month', 3, 1, 4, '2009-07-01 00:00:00', '2009-06-23 00:00:00', '2009-06-23 00:00:00', NULL, NULL, '2009-04-01 10:11:40', 5, 0),
1804 (32, 1, 1, 600.00, '200', 'USD', 'month', 1, 1, 3, '2009-10-01 00:00:00', '2009-09-14 00:00:00', '2009-09-14 00:00:00', NULL, NULL, '2009-12-01 00:00:00', 5, 0);
6a488035
TO
1805";
1806 $this->_query($pledge);
1807 }
1808
1809 private function addPledgePayment() {
1810 $pledgePayment = "INSERT INTO civicrm_pledge_payment
1811 ( pledge_id, contribution_id, scheduled_amount, actual_amount, currency, scheduled_date, reminder_date, reminder_count, status_id)
1812 VALUES
1813 (1, 10, 500.00, 500.00, 'USD','2009-07-01 00:00:00', null, 0, 1 ),
1814 (2, 11, 200.00, 200.00, 'USD','2009-07-01 00:00:00', null, 0, 1 ),
1815 (2, null, 200.00, null, 'USD', '2009-10-01 00:00:00', null, 0, 2 ),
1816 (2, null, 200.00, null, 'USD', '2009-01-01 00:00:00', null, 0, 2 ),
1817 (2, null, 200.00, null, 'USD', '2009-04-01 00:00:00', null, 0, 2 ),
1818
1819 (3, 12, 200.00, 200.00, 'USD', '2009-10-01 00:00:00', null, 0, 1 ),
1820 (3, 13, 200.00, 200.00, 'USD', '2009-11-01 00:0:00', '2009-10-28 00:00:00', 1, 1),
1821 (3, null, 200.00, null, 'USD', '2009-12-01 00:00:00', null, 0, 2 );
1822 ";
1823 $this->_query($pledgePayment);
1824 }
1825
1826 private function addContributionLineItem() {
d8d42562
DG
1827 $query = " INSERT INTO civicrm_line_item (`entity_table`, `entity_id`, contribution_id, `price_field_id`, `label`, `qty`, `unit_price`, `line_total`, `participant_count`, `price_field_value_id`, `financial_type_id`)
1828SELECT 'civicrm_contribution', cc.id, cc.id contribution_id, cpf.id as price_field, cpfv.label, 1, cc.total_amount, cc.total_amount line_total, 0, cpfv.id as price_field_value, cpfv.financial_type_id
6a488035
TO
1829FROM civicrm_contribution cc
1830LEFT JOIN civicrm_price_set cps ON cps.name = 'default_contribution_amount'
1831LEFT JOIN civicrm_price_field cpf ON cpf.price_set_id = cps.id
1832LEFT JOIN civicrm_price_field_value cpfv ON cpfv.price_field_id = cpf.id
1833order by cc.id; ";
1834 $this->_query($query);
1835 }
1836
1837 private function addContributionFinancialItem() {
1838
1839 $sql = " SELECT cc.id contribution_id, cli.id as line_item_id, cc.contact_id, cc.receive_date, cc.total_amount, cc.currency, cli.label, cli.financial_type_id, cefa.financial_account_id, cc.payment_instrument_id, cc.check_number, cc.trxn_id
1840FROM `civicrm_contribution` cc
1841INNER JOIN civicrm_line_item cli ON cli.entity_id = cc.id and cli.entity_table = 'civicrm_contribution'
1842INNER JOIN civicrm_entity_financial_account cefa ON cefa.entity_id = cli.financial_type_id
1843WHERE cefa.account_relationship = 1; ";
1844 $result = CRM_Core_DAO::executeQuery($sql);
1845 $financialAccountId = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount();
1846 $this->addFinancialItem($result, $financialAccountId);
1847 }
1848
1849 private function addParticipantFinancialItem() {
1850
557ceeb7 1851 $sql = " SELECT cpp.contribution_id, cli.id as line_item_id, cp.contact_id, now() as receive_date, cp.fee_amount as total_amount, cp.fee_currency as currency, cli.label, cli.financial_type_id, cefa.financial_account_id, 4 as payment_instrument_id, NULL as check_number, NULL as trxn_id
6a488035
TO
1852FROM `civicrm_participant` cp
1853INNER JOIN civicrm_participant_payment cpp ON cpp.participant_id = cp.id
1854INNER JOIN civicrm_line_item cli ON cli.entity_id = cp.id and cli.entity_table = 'civicrm_participant'
1855INNER JOIN civicrm_entity_financial_account cefa ON cefa.entity_id = cli.financial_type_id
1856WHERE cefa.account_relationship = 1";
1857 $result = CRM_Core_DAO::executeQuery($sql);
1858 $this->addFinancialItem($result);
1859 }
1860
627456b5
EM
1861 /**
1862 * @param $result
1863 * @param null $financialAccountId
1864 */
6a488035
TO
1865 private function addFinancialItem($result, $financialAccountId = NULL) {
1866 $defaultFinancialAccount = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_financial_account WHERE is_default = 1");
7bc930af 1867 while ($result->fetch()) {
6a488035
TO
1868 $trxnParams = array(
1869 'trxn_date' => CRM_Utils_Date::processDate($result->receive_date),
1870 'total_amount' => $result->total_amount,
1871 'currency' => $result->currency,
1872 'status_id' => 1,
1873 'trxn_id' => $result->trxn_id,
1874 'contribution_id' => $result->contribution_id,
2edcd692 1875 'to_financial_account_id' => empty($financialAccountId[$result->payment_instrument_id]) ? $defaultFinancialAccount : $financialAccountId[$result->payment_instrument_id],
6a488035 1876 'payment_instrument_id' => $result->payment_instrument_id,
906a298c 1877 'check_number' => $result->check_number,
6a488035
TO
1878 );
1879 $trxn = CRM_Core_BAO_FinancialTrxn::create($trxnParams);
1880 $financialItem = array(
1881 'transaction_date' => CRM_Utils_Date::processDate($result->receive_date),
1882 'amount' => $result->total_amount,
1883 'currency' => $result->currency,
1884 'status_id' => 1,
1885 'entity_id' => $result->line_item_id,
1886 'contact_id' => $result->contact_id,
1887 'entity_table' => 'civicrm_line_item',
1888 'description' => $result->label,
906a298c 1889 'financial_account_id' => $result->financial_account_id,
6a488035
TO
1890 );
1891 $trxnId['id'] = $trxn->id;
7bc930af
KJ
1892 CRM_Financial_BAO_FinancialItem::create($financialItem, NULL, $trxnId);
1893 }
6a488035
TO
1894 }
1895
1896 private function addLineItemParticipants() {
1897 $participant = new CRM_Event_DAO_Participant();
d8d42562
DG
1898 $participant->query("INSERT INTO civicrm_line_item (`entity_table`, `entity_id`, contribution_id, `price_field_id`, `label`, `qty`, `unit_price`, `line_total`, `participant_count`, `price_field_value_id`, `financial_type_id`)
1899SELECT 'civicrm_participant', cp.id, cpp.contribution_id, cpfv.price_field_id, cpfv.label, 1, cpfv.amount, cpfv.amount as line_total, 0, cpfv.id, cpfv.financial_type_id FROM civicrm_participant cp LEFT JOIN civicrm_participant_payment cpp ON cpp.participant_id = cp.id
1900LEFT JOIN civicrm_price_set_entity cpe ON cpe.entity_id = cp.event_id LEFT JOIN civicrm_price_field cpf ON cpf.price_set_id = cpe.price_set_id LEFT JOIN civicrm_price_field_value cpfv ON cpfv.price_field_id = cpf.id WHERE cpfv.label = cp.fee_level");
6a488035
TO
1901 }
1902
1903 private function addMembershipPayment() {
1904 $maxContribution = CRM_Core_DAO::singleValueQuery("select max(id) from civicrm_contribution");
1905 $financialTypeID = CRM_Core_DAO::singleValueQuery("select id from civicrm_financial_type where name = 'Member Dues'");
557ceeb7
DG
1906 $paymentInstrumentID = CRM_Core_DAO::singleValueQuery("select value from civicrm_option_value where name = 'Credit Card' AND option_group_id = (SELECT id from civicrm_option_group where name = 'payment_instrument')");
1907 $sql = "INSERT INTO civicrm_contribution (contact_id,financial_type_id,payment_instrument_id, receive_date, total_amount, currency, source, contribution_status_id)
1908SELECT cm.contact_id, $financialTypeID, $paymentInstrumentID, now(), cmt.minimum_fee, 'USD', CONCAT(cmt.name, ' Membership: Offline signup'), 1 FROM `civicrm_membership` cm
6a488035
TO
1909LEFT JOIN civicrm_membership_type cmt ON cmt.id = cm.membership_type_id;";
1910
1911 $this->_query($sql);
1912
1913 $sql = "INSERT INTO civicrm_membership_payment (contribution_id,membership_id)
1914SELECT cc.id, cm.id FROM civicrm_contribution cc
1915LEFT JOIN civicrm_membership cm ON cm.contact_id = cc.contact_id
1916WHERE cc.id > $maxContribution;";
1917
1918 $this->_query($sql);
1919
d8d42562
DG
1920 $sql = "INSERT INTO civicrm_line_item (entity_table, entity_id, contribution_id, price_field_value_id, price_field_id, label, qty, unit_price, line_total, financial_type_id)
1921SELECT 'civicrm_membership', cm.id, cmp.contribution_id, cpfv.id, cpfv.price_field_id, cpfv.label, 1, cpfv.amount, cpfv.amount as unit_price, cpfv.financial_type_id FROM `civicrm_membership` cm
6a488035
TO
1922LEFT JOIN civicrm_membership_payment cmp ON cmp.membership_id = cm.id
1923LEFT JOIN civicrm_price_field_value cpfv ON cpfv.membership_type_id = cm.membership_type_id
1924LEFT JOIN civicrm_price_field cpf ON cpf.id = cpfv.price_field_id
1925LEFT JOIN civicrm_price_set cps ON cps.id = cpf.price_set_id
1926WHERE cps.name = 'default_membership_type_amount'";
6a488035
TO
1927 $this->_query($sql);
1928
91da6cd5
DL
1929 $sql = "INSERT INTO civicrm_activity(source_record_id, activity_type_id, subject, activity_date_time, status_id, details)
1930SELECT id, 6, CONCAT('$ ', total_amount, ' - ', source), now(), 2, 'Membership Payment' FROM civicrm_contribution WHERE id > $maxContribution";
1931 $this->_query($sql);
6a488035 1932
a24b3694 1933 $sql = "INSERT INTO civicrm_activity_contact(contact_id, activity_id, record_type_id)
1934SELECT c.contact_id, a.id, 2
91da6cd5
DL
1935FROM civicrm_contribution c, civicrm_activity a
1936WHERE c.id > $maxContribution
1937AND a.source_record_id = c.id
1938AND a.details = 'Membership Payment'
1939";
6a488035 1940 $this->_query($sql);
7bc930af 1941 }
6a488035
TO
1942
1943 private function addParticipantPayment() {
1944 $maxContribution = CRM_Core_DAO::singleValueQuery("select max(id) from civicrm_contribution");
1945 $financialTypeID = CRM_Core_DAO::singleValueQuery("select id from civicrm_financial_type where name = 'Event Fee'");
557ceeb7
DG
1946 $paymentInstrumentID = CRM_Core_DAO::singleValueQuery("select value from civicrm_option_value where name = 'Credit Card' AND option_group_id = (SELECT id from civicrm_option_group where name = 'payment_instrument')");
1947 $sql = "INSERT INTO civicrm_contribution (contact_id, financial_type_id, payment_instrument_id, receive_date, total_amount, currency, receipt_date, source, contribution_status_id)
1948SELECT `contact_id`, $financialTypeID, $paymentInstrumentID, now(), `fee_amount`, 'USD', now(), CONCAT(ce.title, ' : Offline registration'), 1 FROM `civicrm_participant` cp
6a488035
TO
1949LEFT JOIN civicrm_event ce ON ce.id = cp.event_id
1950group by `contact_id`;";
1951
1952 $this->_query($sql);
1953
1954 $sql = "INSERT INTO civicrm_participant_payment (contribution_id,participant_id)
1955SELECT cc.id, cp.id FROM civicrm_contribution cc
1956LEFT JOIN civicrm_participant cp ON cp.contact_id = cc.contact_id
1957WHERE cc.id > $maxContribution";
1958
1959 $this->_query($sql);
1960
91da6cd5
DL
1961 $sql = "INSERT INTO civicrm_activity(source_record_id, activity_type_id, subject, activity_date_time, status_id, details)
1962SELECT id, 6, CONCAT('$ ', total_amount, ' - ', source), now(), 2, 'Participant' FROM `civicrm_contribution` WHERE id > $maxContribution";
1963 $this->_query($sql);
6a488035 1964
a24b3694 1965 $sql = "INSERT INTO civicrm_activity_contact(contact_id, activity_id, record_type_id)
1966SELECT c.contact_id, a.id, 2
91da6cd5
DL
1967FROM civicrm_contribution c, civicrm_activity a
1968WHERE c.id > $maxContribution
1969AND a.source_record_id = c.id
1970AND a.details = 'Participant Payment'
1971";
6a488035
TO
1972 $this->_query($sql);
1973 }
906a298c 1974
6a488035
TO
1975}
1976
627456b5
EM
1977/**
1978 * @param null $str
1979 *
1980 * @return bool
1981 */
6a488035
TO
1982function user_access($str = NULL) {
1983 return TRUE;
1984}
1985
627456b5
EM
1986/**
1987 * @return array
1988 */
6a488035
TO
1989function module_list() {
1990 return array();
1991}
1992
721e8594 1993echo ("Starting data generation on " . date("F dS h:i:s A") . "\n");
375fffa8
CW
1994$gcd = new CRM_GCD();
1995$gcd->initID();
1996$gcd->generate('Domain');
1997$gcd->generate('Contact');
1998$gcd->generate('Individual');
1999$gcd->generate('Household');
2000$gcd->generate('Organization');
2001$gcd->generate('Relationship');
2002$gcd->generate('EntityTag');
2003$gcd->generate('Group');
2004$gcd->generate('Note');
2005$gcd->generate('Activity');
2006$gcd->generate('Event');
2007$gcd->generate('Contribution');
2008$gcd->generate('ContributionLineItem');
2009$gcd->generate('Membership');
2010$gcd->generate('MembershipPayment');
2011$gcd->generate('MembershipLog');
2012$gcd->generate('PCP');
2013$gcd->generate('SoftContribution');
2014$gcd->generate('Pledge');
2015$gcd->generate('PledgePayment');
2016$gcd->generate('ContributionFinancialItem');
2017$gcd->generate('Participant');
2018$gcd->generate('ParticipantPayment');
2019$gcd->generate('LineItemParticipants');
2020$gcd->generate('ParticipantFinancialItem');
721e8594 2021echo ("Ending data generation on " . date("F dS h:i:s A") . "\n");