Merge pull request #17860 from eileenmcnaughton/test
[civicrm-core.git] / sql / GenerateReportData.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 * $Id$
17 *
18 */
19
20 /**
21 * This class generates data for the schema located in Contact.sql
22 *
23 * each public method generates data for the concerned table.
24 * so for example the addContactDomain method generates and adds
25 * data to the contact_domain table
26 *
27 * Data generation is a bit tricky since the data generated
28 * randomly in one table could be used as a FKEY in another
29 * table.
30 *
31 * In order to ensure that a randomly generated FKEY matches
32 * a field in the referened table, the field in the referenced
33 * table is always generated linearly.
34 *
35 *
36 *
37 *
38 * Some numbers
39 *
40 * Domain ID's - 1 to NUM_DOMAIN
41 *
42 * Context - 3/domain
43 *
44 * Contact - 1 to NUM_CONTACT
45 * 75% - Individual
46 * 15% - Household
47 * 10% - Organization
48 *
49 * Contact to Domain distribution should be equal.
50 *
51 *
52 * Contact Individual = 1 to 0.75*NUM_CONTACT
53 *
54 * Contact Household = 0.75*NUM_CONTACT to 0.9*NUM_CONTACT
55 *
56 * Contact Organization = 0.9*NUM_CONTACT to NUM_CONTACT
57 *
58 * Contact Location = 15% for Households, 10% for Organizations, (75-(15*4))% for Individuals.
59 * (Assumption is that each household contains 4 individuals)
60 *
61 */
62
63 /**
64 *
65 * Note: implication of using of mt_srand(1) in constructor
66 * The data generated will be done in a consistent manner
67 * so as to give the same data during each run (but this
68 * would involve populating the entire db at one go - since
69 * mt_srand(1) is in the constructor, if one needs to be able
70 * to get consistent random numbers then the mt_srand(1) shld
71 * be in each function that adds data to each table.
72 *
73 */
74
75 /*
76 * Note as of 2019-07-15 this file does not appear to be called
77 * from anywhere and seems to have issues running on more recent
78 * php versions.
79 * @todo look to remove this file completely.
80 */
81
82
83 require_once '../civicrm.config.php';
84
85 // autoload
86 require_once 'CRM/Core/ClassLoader.php';
87 CRM_Core_ClassLoader::singleton()->register();
88
89 /**
90 * Class CRM_GCD
91 */
92 class CRM_GCD {
93
94 /**
95 * constants
96 */
97 const DATA_FILENAME = "sample_data.xml";
98 const NUM_DOMAIN = 1;
99 const NUM_CONTACT = 5000;
100 const NUM_CONTRIBUTION = 2000;
101 const NUM_MEMBERSHIP = 2000;
102 const NUM_PARTICIPANT = 2000;
103 const INDIVIDUAL_PERCENT = 75;
104 const HOUSEHOLD_PERCENT = 15;
105 const ORGANIZATION_PERCENT = 10;
106 const NUM_INDIVIDUAL_PER_HOUSEHOLD = 4;
107 const NUM_ACTIVITY = 150;
108
109 // relationship types from the table crm_relationship_type
110 const CHILD_OF = 1;
111 const SPOUSE_OF = 2;
112 const SIBLING_OF = 3;
113 const HEAD_OF_HOUSEHOLD = 6;
114 const MEMBER_OF_HOUSEHOLD = 7;
115
116
117 // location types from the table crm_location_type
118 const HOME = 1;
119 const WORK = 2;
120 const MAIN = 3;
121 const OTHER = 4;
122 const ADD_TO_DB = TRUE;
123 //const ADD_TO_DB=FALSE;
124 const DEBUG_LEVEL = 1;
125
126 /***
127 * private members
128 */
129
130 /**
131 * enum's from database
132 * @var array
133 */
134 private $preferredCommunicationMethod = array('1', '2', '3', '4', '5');
135 private $contactType = array('Individual', 'Household', 'Organization');
136 private $phoneType = array('1', '2', '3', '4');
137
138 /**
139 * customizable enums (foreign keys)
140 * @var array
141 */
142 private $prefix = array(1 => 'Mrs', 2 => 'Ms', 3 => 'Mr', 4 => 'Dr');
143 private $suffix = array(1 => 'Jr', 2 => 'Sr');
144 private $gender = array(1 => 'Female', 2 => 'Male');
145 private $greetingType = array(1 => 'Dear [first]', 2 => 'Dear [prefix] [first] [last]', 3 => 'Dear [prefix] [last]');
146
147 /**
148 * store domain id's
149 * @var array
150 */
151 private $domain = array();
152
153 /**
154 * store contact id's
155 * @var array
156 */
157 private $contact = array();
158 private $individual = array();
159 private $household = array();
160 private $organization = array();
161
162
163 /**
164 * store names, firstnames, street 1, street2
165 * @var array
166 */
167 private $firstName = array();
168 private $lastName = array();
169 private $streetName = array();
170 private $supplementalAddress1 = array();
171 private $city = array();
172 private $state = array();
173 private $country = array();
174 private $addressDirection = array();
175 private $streetType = array();
176 private $emailDomain = array();
177 private $emailTLD = array();
178 private $organizationName = array();
179 private $organizationField = array();
180 private $organizationType = array();
181 private $group = array();
182 private $note = array();
183 private $activity_type = array();
184 private $module = array();
185 private $callback = array();
186 private $party_registration = array();
187 private $degree = array();
188 private $school = array();
189
190 /**
191 * stores the strict individual id and household id to individual id mapping
192 * @var array
193 */
194 private $strictIndividual = array();
195 private $householdIndividual = array();
196
197 /**
198 * sample data in xml format
199 * @var int
200 */
201 private $sampleData = NULL;
202
203 /**
204 * private vars
205 * @var int
206 */
207 private $numIndividual = 0;
208 private $numHousehold = 0;
209 private $numOrganization = 0;
210 private $numStrictIndividual = 0;
211
212 private $CSC = array(
213 // united states
214 1228 => array(
215 // california
216 1004 => array('San Francisco', 'Los Angeles', 'Palo Alto'),
217 // new york
218 1031 => array('New York', 'Albany'),
219 ),
220 // india
221 1101 => array(
222 // maharashtra
223 1113 => array('Mumbai', 'Pune', 'Nasik'),
224 // karnataka
225 1114 => array('Bangalore', 'Mangalore', 'Udipi'),
226 ),
227 // poland
228 1172 => array(
229 // mazowieckie
230 1115 => array('Warszawa', 'PÅ‚ock'),
231 // pomorskie
232 1116 => array('Gdańsk', 'Gdynia'),
233 ),
234 );
235 /**
236 * @var array
237 */
238 private $groupMembershipStatus = array('Added', 'Removed', 'Pending');
239 private $subscriptionHistoryMethod = array('Admin', 'Email');
240
241 /**
242 * private methods
243 *
244 * @param int $size
245 * @return string
246 */
247
248 /**
249 * Get a randomly generated string.
250 *
251 * @param int $size
252 */
253 private function _getRandomString($size = 32) {
254 $string = "";
255
256 // get an ascii code for each character
257 for ($i = 0; $i < $size; $i++) {
258 $random_int = mt_rand(65, 122);
259 if (($random_int < 97) && ($random_int > 90)) {
260 // if ascii code between 90 and 97 substitute with space
261 $random_int = 32;
262 }
263 $random_char = chr($random_int);
264 $string .= $random_char;
265 }
266 return $string;
267 }
268
269 /**
270 * @return string
271 */
272 private function _getRandomChar() {
273 return chr(mt_rand(65, 90));
274 }
275
276 /**
277 * @return int
278 */
279 private function getRandomBoolean() {
280 return mt_rand(0, 1);
281 }
282
283 /**
284 * @param $array1
285 *
286 * @return mixed
287 */
288 private function _getRandomElement(&$array1) {
289 return $array1[mt_rand(1, count($array1)) - 1];
290 }
291
292 /**
293 * @param $array1
294 *
295 * @return int
296 */
297 private function _getRandomIndex(&$array1) {
298 return mt_rand(1, count($array1));
299 }
300
301 // country state city combo
302
303 /**
304 * @return array
305 */
306 private function _getRandomCSC() {
307 $array1 = array();
308
309 // $c = array_rand($this->CSC);
310 $c = 1228;
311
312 // the state array now
313 $s = array_rand($this->CSC[$c]);
314
315 // the city
316 $ci = array_rand($this->CSC[$c][$s]);
317 $city = $this->CSC[$c][$s][$ci];
318
319 $array1[] = $c;
320 $array1[] = $s;
321 $array1[] = $city;
322
323 return $array1;
324 }
325
326 /**
327 * Generate a random date.
328 *
329 * If both $startDate and $endDate are defined generate
330 * date between them.
331 *
332 * If only startDate is specified then date generated is
333 * between startDate + 1 year.
334 *
335 * if only endDate is specified then date generated is
336 * between endDate - 1 year.
337 *
338 * if none are specified - date is between today - 1year
339 * and today
340 *
341 * @param int $startDate Start Date in Unix timestamp
342 * @param int $endDate End Date in Unix timestamp
343 * @access private
344 *
345 * @return string randomly generated date in the format "Ymd"
346 *
347 */
348 private function _getRandomDate($startDate = 0, $endDate = 0) {
349
350 // number of seconds per year
351 // $numSecond = 31536000;
352 // number of seconds for 2 year
353 $numSecond = 63072000;
354
355 $dateFormat = "YmdHis";
356 $today = time();
357
358 // both are defined
359 if ($startDate && $endDate) {
360 return date($dateFormat, mt_rand($startDate, $endDate));
361 }
362
363 // only startDate is defined
364 if ($startDate) {
365 // $nextYear = mktime(0, 0, 0, date("m", $startDate), date("d", $startDate), date("Y")+1);
366 return date($dateFormat, mt_rand($startDate, $startDate + $numSecond));
367 }
368
369 // only endDate is defined
370 if ($startDate) {
371 return date($dateFormat, mt_rand($endDate - $numSecond, $endDate));
372 }
373
374 // none are defined
375 return date($dateFormat, mt_rand($today - $numSecond, $today));
376 }
377
378 // insert data into db's
379
380 /**
381 * @param $dao
382 */
383 private function _insert(&$dao) {
384 if (self::ADD_TO_DB) {
385 if (!$dao->insert()) {
386 echo "ERROR INSERT: " . mysqli_error($dao->getConnection()->connection) . "\n";
387 print_r($dao);
388 exit(1);
389 }
390 }
391 }
392
393 // update data into db's
394
395 /**
396 * @param $dao
397 */
398 private function _update($dao) {
399 if (self::ADD_TO_DB) {
400 if (!$dao->update()) {
401 echo "ERROR UPDATE: " . mysqli_error($dao->getConnection()->connection) . "\n";
402 print_r($dao);
403 exit(1);
404 }
405 }
406 }
407
408 /**
409 * Insert a note
410 *
411 * Helper function which randomly populates "note" and
412 * "date_modified" and inserts it.
413 *
414 * @param $note
415 * @access private
416 *
417 */
418 private function _insertNote($note) {
419 $note->note = $this->_getRandomElement($this->note);
420 $note->modified_date = $this->_getRandomDate();
421 $this->_insert($note);
422 }
423
424 /**
425 *
426 * Start of public functions
427 *
428 */
429 public function __construct() {
430 // initialize all the vars
431 $this->numIndividual = self::INDIVIDUAL_PERCENT * self::NUM_CONTACT / 100;
432 $this->numHousehold = self::HOUSEHOLD_PERCENT * self::NUM_CONTACT / 100;
433 $this->numOrganization = self::ORGANIZATION_PERCENT * self::NUM_CONTACT / 100;
434 $this->numStrictIndividual = $this->numIndividual - ($this->numHousehold * self::NUM_INDIVIDUAL_PER_HOUSEHOLD);
435 }
436
437 public function parseDataFile() {
438
439 $sampleData = simplexml_load_file(self::DATA_FILENAME);
440
441 // first names
442 foreach ($sampleData->first_names->first_name as $first_name) {
443 $this->firstName[] = trim($first_name);
444 }
445
446 // last names
447 foreach ($sampleData->last_names->last_name as $last_name) {
448 $this->lastName[] = trim($last_name);
449 }
450
451 // street names
452 foreach ($sampleData->street_names->street_name as $street_name) {
453 $this->streetName[] = trim($street_name);
454 }
455
456 // supplemental address 1
457 foreach ($sampleData->supplemental_addresses_1->supplemental_address_1 as $supplemental_address_1) {
458 $this->supplementalAddress1[] = trim($supplemental_address_1);
459 }
460
461 // cities
462 foreach ($sampleData->cities->city as $city) {
463 $this->city[] = trim($city);
464 }
465
466 // address directions
467 foreach ($sampleData->address_directions->address_direction as $address_direction) {
468 $this->addressDirection[] = trim($address_direction);
469 }
470
471 // street types
472 foreach ($sampleData->street_types->street_type as $street_type) {
473 $this->streetType[] = trim($street_type);
474 }
475
476 // email domains
477 foreach ($sampleData->email_domains->email_domain as $email_domain) {
478 $this->emailDomain[] = trim($email_domain);
479 }
480
481 // email top level domain
482 foreach ($sampleData->email_tlds->email_tld as $email_tld) {
483 $this->emailTLD[] = trim($email_tld);
484 }
485
486 // organization name
487 foreach ($sampleData->organization_names->organization_name as $organization_name) {
488 $this->organization_name[] = trim($organization_name);
489 }
490
491 // organization field
492 foreach ($sampleData->organization_fields->organization_field as $organization_field) {
493 $this->organizationField[] = trim($organization_field);
494 }
495
496 // organization type
497 foreach ($sampleData->organization_types->organization_type as $organization_type) {
498 $this->organizationType[] = trim($organization_type);
499 }
500
501 // group
502 foreach ($sampleData->groups->group as $group) {
503 $this->group[] = trim($group);
504 }
505
506 // notes
507 foreach ($sampleData->notes->note as $note) {
508 $this->note[] = trim($note);
509 }
510
511 // activity type
512 foreach ($sampleData->activity_types->activity_type as $activity_type) {
513 $this->activity_type[] = trim($activity_type);
514 }
515
516 // module
517 foreach ($sampleData->modules->module as $module) {
518 $this->module[] = trim($module);
519 }
520
521 // callback
522 foreach ($sampleData->callbacks->callback as $callback) {
523 $this->callback[] = trim($callback);
524 }
525
526 // custom data - party registration
527 foreach ($sampleData->party_registrations->party_registration as $party_registration) {
528 $this->party_registration[] = trim($party_registration);
529 }
530
531 // custom data - degrees
532 foreach ($sampleData->degrees->degree as $degree) {
533 $this->degree[] = trim($degree);
534 }
535
536 // custom data - schools
537 foreach ($sampleData->schools->school as $school) {
538 $this->school[] = trim($school);
539 }
540
541 // custom data - issue
542 foreach ($sampleData->issue->status as $status) {
543 $this->issue[] = trim($status);
544 }
545
546 // custom data - gotv
547 require_once 'CRM/Core/BAO/CustomOption.php';
548 foreach ($sampleData->gotv->status as $status) {
549 $this->gotv[] = CRM_Core_DAO::VALUE_SEPARATOR . trim($status) . CRM_Core_DAO::VALUE_SEPARATOR;
550 }
551
552 // custom data - marital_status
553 foreach ($sampleData->marital_status->status as $status) {
554 $this->marital_status[] = trim($status);
555 }
556 }
557
558 /**
559 * @param $id
560 *
561 * @return string
562 */
563 public function getContactType($id) {
564 if (in_array($id, $this->individual)) {
565 return 'Individual';
566 }
567 if (in_array($id, $this->household)) {
568 return 'Household';
569 }
570 if (in_array($id, $this->organization)) {
571 return 'Organization';
572 }
573 }
574
575 public function initDB() {
576 $config = CRM_Core_Config::singleton();
577 }
578
579 /**
580 *
581 * this function creates arrays for the following
582 *
583 * domain id
584 * contact id
585 * contact_location id
586 * contact_contact_location id
587 * contact_email uuid
588 * contact_phone_uuid
589 * contact_instant_message uuid
590 * contact_relationship uuid
591 * contact_task uuid
592 * contact_note uuid
593 *
594 */
595 public function initID() {
596
597 // may use this function in future if needed to get
598 // a consistent pattern of random numbers.
599
600 // get the domain and contact id arrays
601 $this->domain = range(1, self::NUM_DOMAIN);
602 shuffle($this->domain);
603 $this->contact = range(2, self::NUM_CONTACT + 1);
604 shuffle($this->contact);
605
606 // get the individual, household and organizaton contacts
607 $offset = 0;
608 $this->individual = array_slice($this->contact, $offset, $this->numIndividual);
609 $offset += $this->numIndividual;
610 $this->household = array_slice($this->contact, $offset, $this->numHousehold);
611 $offset += $this->numHousehold;
612 $this->organization = array_slice($this->contact, $offset, $this->numOrganization);
613
614 // get the strict individual contacts (i.e individual contacts not belonging to any household)
615 $this->strictIndividual = array_slice($this->individual, 0, $this->numStrictIndividual);
616
617 // get the household to individual mapping array
618 $this->householdIndividual = array_diff($this->individual, $this->strictIndividual);
619 $this->householdIndividual = array_chunk($this->householdIndividual, self::NUM_INDIVIDUAL_PER_HOUSEHOLD);
620 $this->householdIndividual = array_combine($this->household, $this->householdIndividual);
621 }
622
623 /**
624 *
625 * addDomain()
626 *
627 * This method adds NUM_DOMAIN domains and then adds NUM_REVISION
628 * revisions for each domain with the latest revision being the last one..
629 *
630 */
631 public function addDomain() {
632
633 /* Add a location for domain 1 */
634
635 // FIXME FOR NEW LOCATION BLOCK STRUCTURE
636 // $this->_addLocation(self::MAIN, 1, true);
637
638 $domain = new CRM_Core_DAO_Domain();
639 for ($id = 2; $id <= self::NUM_DOMAIN; $id++) {
640 // domain name is pretty simple. it is "Domain $id"
641 $domain->name = "Domain $id";
642 $domain->description = "Description $id";
643 $domain->contact_name = $this->randomName();
644
645 // insert domain
646 $this->_insert($domain);
647 // FIXME FOR NEW LOCATION BLOCK STRUCTURE
648 // $this->_addLocation(self::MAIN, $id, true);
649 }
650 }
651
652 /**
653 * @return string
654 */
655 public function randomName() {
656 $prefix = $this->_getRandomIndex($this->prefix);
657 $first_name = ucfirst($this->_getRandomElement($this->firstName));
658 $middle_name = ucfirst($this->_getRandomChar());
659 $last_name = ucfirst($this->_getRandomElement($this->lastName));
660 $suffix = $this->_getRandomIndex($this->suffix);
661
662 return $this->prefix[$prefix] . " $first_name $middle_name $last_name " . $this->suffix[$suffix];
663 }
664
665 /**
666 *
667 * addContact()
668 *
669 * This method adds data to the contact table
670 *
671 * id - from $contact
672 * contact_type 'Individual' 'Household' 'Organization'
673 * preferred_communication (random 1 to 3)
674 *
675 */
676 public function addContact() {
677
678 // add contacts
679 $contact = new CRM_Contact_DAO_Contact();
680
681 for ($id = 1; $id <= self::NUM_CONTACT; $id++) {
682 $contact->contact_type = $this->getContactType($id + 1);
683 $contact->do_not_phone = mt_rand(0, 1);
684 $contact->do_not_email = mt_rand(0, 1);
685 $contact->do_not_post = mt_rand(0, 1);
686 $contact->do_not_trade = mt_rand(0, 1);
687 $contact->preferred_communication_method = $this->_getRandomElement($this->preferredCommunicationMethod);
688 $this->_insert($contact);
689 }
690 }
691
692 /**
693 *
694 * addIndividual()
695 *
696 * This method adds individual's data to the contact table
697 *
698 * The following fields are generated and added.
699 *
700 * contact_uuid - individual
701 * contact_rid - latest one
702 * first_name 'First Name $contact_uuid'
703 * middle_name 'Middle Name $contact_uuid'
704 * last_name 'Last Name $contact_uuid'
705 * job_title 'Job Title $contact_uuid'
706 * greeting_type - randomly select from the enum values
707 * custom_greeting - "custom greeting $contact_uuid'
708 *
709 */
710 public function addIndividual() {
711
712 $contact = new CRM_Contact_DAO_Contact();
713
714 for ($id = 1; $id <= $this->numIndividual; $id++) {
715 $contact->first_name = ucfirst($this->_getRandomElement($this->firstName));
716 $contact->middle_name = ucfirst($this->_getRandomChar());
717 $contact->last_name = ucfirst($this->_getRandomElement($this->lastName));
718 $contact->prefix_id = $this->_getRandomIndex($this->prefix);
719 $contact->suffix_id = $this->_getRandomIndex($this->suffix);
720 $contact->greeting_type_id = $this->_getRandomIndex($this->greetingType);
721 $contact->gender_id = $this->_getRandomIndex($this->gender);
722 $contact->birth_date = date("Ymd", mt_rand(0, time()));
723 $contact->is_deceased = mt_rand(0, 1);
724
725 $contact->id = $this->individual[($id - 1)];
726
727 // also update the sort name for the contact id.
728 $contact->display_name = trim($this->prefix[$contact->prefix_id] . " $contact->first_name $contact->middle_name $contact->last_name " . $this->suffix[$contact->suffix_id]);
729 $contact->sort_name = $contact->last_name . ', ' . $contact->first_name;
730 $contact->hash = crc32($contact->sort_name);
731 $this->_update($contact);
732 }
733 }
734
735 /**
736 *
737 * addHousehold()
738 *
739 * This method adds household's data to the contact table
740 *
741 * The following fields are generated and added.
742 *
743 * contact_uuid - household_individual
744 * contact_rid - latest one
745 * household_name 'household $contact_uuid primary contact $primary_contact_uuid'
746 * nick_name 'nick $contact_uuid'
747 * primary_contact_uuid = $household_individual[$contact_uuid][0];
748 *
749 */
750 public function addHousehold() {
751
752 $contact = new CRM_Contact_DAO_Contact();
753 for ($id = 1; $id <= $this->numHousehold; $id++) {
754 $cid = $this->household[($id - 1)];
755 $contact->primary_contact_id = $this->householdIndividual[$cid][0];
756
757 // get the last name of the primary contact id
758 $individual = new CRM_Contact_DAO_Contact();
759 $individual->id = $contact->primary_contact_id;
760 $individual->find(TRUE);
761 $firstName = $individual->first_name;
762 $lastName = $individual->last_name;
763
764 // need to name the household and nick name appropriately
765 $contact->household_name = "$firstName $lastName" . "'s home";
766 $contact->nick_name = "$lastName" . "'s home";
767
768 $contact->id = $this->household[($id - 1)];
769 // need to update the sort name for the main contact table
770 $contact->display_name = $contact->sort_name = $contact->household_name;
771 $contact->hash = crc32($contact->sort_name);
772 $this->_update($contact);
773 }
774 }
775
776 /**
777 *
778 * addOrganization()
779 *
780 * This method adds organization data to the contact table
781 *
782 * The following fields are generated and added.
783 *
784 * contact_uuid - organization
785 * contact_rid - latest one
786 * organization_name 'organization $contact_uuid'
787 * legal_name 'legal $contact_uuid'
788 * nick_name 'nick $contact_uuid'
789 * sic_code 'sic $contact_uuid'
790 * primary_contact_id - random individual contact uuid
791 *
792 */
793 public function addOrganization() {
794
795 $contact = new CRM_Contact_DAO_Contact();
796
797 for ($id = 1; $id <= $this->numOrganization; $id++) {
798 $contact->id = $this->organization[($id - 1)];
799 $name = $this->_getRandomElement($this->organization_name) . " " . $this->_getRandomElement($this->organization_field) . " " . $this->_getRandomElement($this->organization_type);
800 $contact->organization_name = $name;
801 $contact->primary_contact_id = $this->_getRandomElement($this->strict_individual);
802
803 // need to update the sort name for the main contact table
804 $contact->display_name = $contact->sort_name = $contact->organization_name;
805 $contact->hash = crc32($contact->sort_name);
806 $this->_update($contact);
807 }
808 }
809
810 /**
811 *
812 * addRelationship()
813 *
814 * This method adds data to the contact_relationship table
815 *
816 * it adds the following fields
817 *
818 */
819 public function addRelationship() {
820
821 $relationship = new CRM_Contact_DAO_Relationship();
822
823 // all active for now.
824 $relationship->is_active = 1;
825
826 foreach ($this->householdIndividual as $household_id => $household_member) {
827 // add child_of relationship
828 // 2 for each child
829 $relationship->relationship_type_id = self::CHILD_OF;
830 $relationship->contact_id_a = $household_member[2];
831 $relationship->contact_id_b = $household_member[0];
832 $this->_insert($relationship);
833 $relationship->contact_id_a = $household_member[3];
834 $relationship->contact_id_b = $household_member[0];
835 $this->_insert($relationship);
836 $relationship->contact_id_a = $household_member[2];
837 $relationship->contact_id_b = $household_member[1];
838 $this->_insert($relationship);
839 $relationship->contact_id_a = $household_member[3];
840 $relationship->contact_id_b = $household_member[1];
841 $this->_insert($relationship);
842
843 // add spouse_of relationship 1 for both the spouses
844 $relationship->relationship_type_id = self::SPOUSE_OF;
845 $relationship->contact_id_a = $household_member[1];
846 $relationship->contact_id_b = $household_member[0];
847 $this->_insert($relationship);
848
849 // add sibling_of relationship 1 for both the siblings
850 $relationship->relationship_type_id = self::SIBLING_OF;
851 $relationship->contact_id_a = $household_member[3];
852 $relationship->contact_id_b = $household_member[2];
853 $this->_insert($relationship);
854
855 // add head_of_household relationship 1 for head of house
856 $relationship->relationship_type_id = self::HEAD_OF_HOUSEHOLD;
857 $relationship->contact_id_a = $household_member[0];
858 $relationship->contact_id_b = $household_id;
859 $this->_insert($relationship);
860
861 // add member_of_household relationship 3 for all other members
862 $relationship->relationship_type_id = self::MEMBER_OF_HOUSEHOLD;
863 $relationship->contact_id_a = $household_member[1];
864 $this->_insert($relationship);
865 $relationship->contact_id_a = $household_member[2];
866 $this->_insert($relationship);
867 $relationship->contact_id_a = $household_member[3];
868 $this->_insert($relationship);
869 }
870 }
871
872 /**
873 *
874 * addLocation()
875 *
876 * This method adds data to the location table
877 *
878 */
879 public function addLocation() {
880 // strict individuals
881 foreach ($this->strictIndividual as $contactId) {
882 $this->_addLocation(self::HOME, $contactId);
883 }
884
885 //household
886 foreach ($this->household as $contactId) {
887 $this->_addLocation(self::HOME, $contactId);
888 }
889
890 //organization
891 foreach ($this->organization as $contactId) {
892 $this->_addLocation(self::MAIN, $contactId);
893 }
894
895 // some individuals.
896 $someIndividual = array_diff($this->individual, $this->strictIndividual);
897 $someIndividual = array_slice($someIndividual, 0, (int) (75 * ($this->numIndividual - $this->numStrictIndividual) / 100));
898 foreach ($someIndividual as $contactId) {
899 $this->_addLocation(self::HOME, $contactId, FALSE, TRUE);
900 }
901 }
902
903 /**
904 * @param $locationTypeId
905 * @param $contactId
906 * @param bool $domain
907 * @param bool $isPrimary
908 */
909 private function _addLocation($locationTypeId, $contactId, $domain = FALSE, $isPrimary = TRUE) {
910 $this->_addAddress($locationTypeId, $contactId, $isPrimary);
911
912 // add two phones for each location
913 $this->_addPhone($locationTypeId, $contactId, '1', $isPrimary);
914 $this->_addPhone($locationTypeId, $contactId, '2', FALSE);
915
916 // need to get sort name to generate email id
917 $contact = new CRM_Contact_DAO_Contact();
918 $contact->id = $contactId;
919 $contact->find(TRUE);
920 // get the sort name of the contact
921 $sortName = $contact->sort_name;
922 if (!empty($sortName)) {
923 // add 2 email for each location
924 for ($emailId = 1; $emailId <= 2; $emailId++) {
925 $this->_addEmail($locationTypeId, $contactId, $sortName, ($emailId == 1) && $isPrimary);
926 }
927 }
928 }
929
930 /**
931 * @param $locationTypeId
932 * @param $contactId
933 * @param bool $isPrimary
934 * @param null $locationBlockID
935 * @param int $offset
936 */
937 private function _addAddress($locationTypeId, $contactId, $isPrimary = FALSE, $locationBlockID = NULL, $offset = 1) {
938 $addressDAO = new CRM_Core_DAO_Address();
939
940 // add addresses now currently we are adding only 1 address for each location
941 $addressDAO->location_type_id = $locationTypeId;
942 $addressDAO->contact_id = $contactId;
943 $addressDAO->is_primary = $isPrimary;
944
945 $addressDAO->street_number = mt_rand(1, 1000);
946 $addressDAO->street_number_suffix = ucfirst($this->_getRandomChar());
947 $addressDAO->street_number_predirectional = $this->_getRandomElement($this->addressDirection);
948 $addressDAO->street_name = ucwords($this->_getRandomElement($this->streetName));
949 $addressDAO->street_type = $this->_getRandomElement($this->streetType);
950 $addressDAO->street_number_postdirectional = $this->_getRandomElement($this->addressDirection);
951 $addressDAO->street_address = $addressDAO->street_number_predirectional . " " . $addressDAO->street_number . $addressDAO->street_number_suffix . " " . $addressDAO->street_name . " " . $addressDAO->street_type . " " . $addressDAO->street_number_postdirectional;
952 $addressDAO->supplemental_address_1 = ucwords($this->_getRandomElement($this->supplementalAddress1));
953
954 // some more random skips
955 // hack add lat / long for US based addresses
956 list($addressDAO->country_id, $addressDAO->state_province_id, $addressDAO->city,
957 $addressDAO->postal_code, $addressDAO->geo_code_1, $addressDAO->geo_code_2
958 ) = self::getZipCodeInfo();
959
960 //$addressDAO->county_id = 1;
961
962 $this->_insert($addressDAO);
963 }
964
965 /**
966 * @param $sortName
967 *
968 * @return mixed
969 */
970 private function _sortNameToEmail($sortName) {
971 $email = preg_replace("([^a-zA-Z0-9_-]*)", "", $sortName);
972 return $email;
973 }
974
975 /**
976 * @param $locationTypeId
977 * @param $contactId
978 * @param $phoneType
979 * @param bool $isPrimary
980 * @param null $locationBlockID
981 * @param int $offset
982 */
983 private function _addPhone($locationTypeId, $contactId, $phoneType, $isPrimary = FALSE, $locationBlockID = NULL, $offset = 1) {
984 if ($contactId % 3) {
985 $phone = new CRM_Core_DAO_Phone();
986 $phone->location_type_id = $locationTypeId;
987 $phone->contact_id = $contactId;
988 $phone->is_primary = $isPrimary;
989 $phone->phone = mt_rand(11111111, 99999999);
990 $phone->phone_type_id = $phoneType;
991 $this->_insert($phone);
992 }
993 }
994
995 /**
996 * @param $locationTypeId
997 * @param $contactId
998 * @param $sortName
999 * @param bool $isPrimary
1000 * @param null $locationBlockID
1001 * @param int $offset
1002 */
1003 private function _addEmail($locationTypeId, $contactId, $sortName, $isPrimary = FALSE, $locationBlockID = NULL, $offset = 1) {
1004 if ($contactId % 2) {
1005 $email = new CRM_Core_DAO_Email();
1006 $email->location_type_id = $locationTypeId;
1007 $email->contact_id = $contactId;
1008 $email->is_primary = $isPrimary;
1009
1010 $emailName = $this->_sortNameToEmail($sortName);
1011 $emailDomain = $this->_getRandomElement($this->emailDomain);
1012 $tld = $this->_getRandomElement($this->emailTLD);
1013 $email->email = strtolower($emailName . "@" . $emailDomain . "." . $tld);
1014 $this->_insert($email);
1015 }
1016 }
1017
1018 /**
1019 *
1020 * addTagEntity()
1021 *
1022 * This method populates the crm_entity_tag table
1023 *
1024 */
1025 public function addEntityTag() {
1026
1027 $entity_tag = new CRM_Core_DAO_EntityTag();
1028
1029 // add categories 1,2,3 for Organizations.
1030 for ($i = 0; $i < $this->numOrganization; $i += 2) {
1031 $org_id = $this->organization[$i];
1032 // echo "org_id = $org_id\n";
1033 $entity_tag->contact_id = $this->organization[$i];
1034 $entity_tag->tag_id = mt_rand(1, 3);
1035 $this->_insert($entity_tag);
1036 }
1037
1038 // add categories 4,5 for Individuals.
1039 for ($i = 0; $i < $this->numIndividual; $i += 2) {
1040 $entity_tag->contact_id = $this->individual[$i];
1041 if (($entity_tag->contact_id) % 3) {
1042 $entity_tag->tag_id = mt_rand(4, 5);
1043 $this->_insert($entity_tag);
1044 }
1045 else {
1046 // some of the individuals are in both categories (4 and 5).
1047 $entity_tag->tag_id = 4;
1048 $this->_insert($entity_tag);
1049 $entity_tag->tag_id = 5;
1050 $this->_insert($entity_tag);
1051 }
1052 }
1053 }
1054
1055 /**
1056 *
1057 * addGroup()
1058 *
1059 * This method populates the crm_entity_tag table
1060 *
1061 */
1062 public function addGroup() {
1063 // add the 3 groups first
1064 $numGroup = count($this->group);
1065 require_once 'CRM/Contact/BAO/Group.php';
1066 for ($i = 0; $i < $numGroup; $i++) {
1067 $group = new CRM_Contact_BAO_Group();
1068 $group->name = $this->group[$i];
1069 $group->title = $this->group[$i];
1070 $group->group_type = "\ 11\ 12\ 1";
1071 $group->visibility = 'Public Pages';
1072 $group->is_active = 1;
1073 $group->save();
1074 }
1075
1076 // 60 are for newsletter
1077 for ($i = 0; $i < 60; $i++) {
1078 $groupContact = new CRM_Contact_DAO_GroupContact();
1079 // newsletter subscribers
1080 $groupContact->group_id = 2;
1081 $groupContact->contact_id = $this->individual[$i];
1082 // membership status
1083 $groupContact->status = $this->_getRandomElement($this->groupMembershipStatus);
1084
1085 $subscriptionHistory = new CRM_Contact_DAO_SubscriptionHistory();
1086 $subscriptionHistory->contact_id = $groupContact->contact_id;
1087
1088 $subscriptionHistory->group_id = $groupContact->group_id;
1089 $subscriptionHistory->status = $groupContact->status;
1090 // method
1091 $subscriptionHistory->method = $this->_getRandomElement($this->subscriptionHistoryMethod);
1092 $subscriptionHistory->date = $this->_getRandomDate();
1093 if ($groupContact->status != 'Pending') {
1094 $this->_insert($groupContact);
1095 }
1096 $this->_insert($subscriptionHistory);
1097 }
1098
1099 // 15 volunteers
1100 for ($i = 0; $i < 15; $i++) {
1101 $groupContact = new CRM_Contact_DAO_GroupContact();
1102 // Volunteers
1103 $groupContact->group_id = 3;
1104 $groupContact->contact_id = $this->individual[$i + 60];
1105 // membership status
1106 $groupContact->status = $this->_getRandomElement($this->groupMembershipStatus);
1107
1108 $subscriptionHistory = new CRM_Contact_DAO_SubscriptionHistory();
1109 $subscriptionHistory->contact_id = $groupContact->contact_id;
1110 $subscriptionHistory->group_id = $groupContact->group_id;
1111 $subscriptionHistory->status = $groupContact->status;
1112 // method
1113 $subscriptionHistory->method = $this->_getRandomElement($this->subscriptionHistoryMethod);
1114 $subscriptionHistory->date = $this->_getRandomDate();
1115
1116 if ($groupContact->status != 'Pending') {
1117 $this->_insert($groupContact);
1118 }
1119 $this->_insert($subscriptionHistory);
1120 }
1121
1122 // 8 advisory board group
1123 for ($i = 0; $i < 8; $i++) {
1124 $groupContact = new CRM_Contact_DAO_GroupContact();
1125 // advisory board group
1126 $groupContact->group_id = 4;
1127 $groupContact->contact_id = $this->individual[$i * 7];
1128 // membership status
1129 $groupContact->status = $this->_getRandomElement($this->groupMembershipStatus);
1130
1131 $subscriptionHistory = new CRM_Contact_DAO_SubscriptionHistory();
1132 $subscriptionHistory->contact_id = $groupContact->contact_id;
1133 $subscriptionHistory->group_id = $groupContact->group_id;
1134 $subscriptionHistory->status = $groupContact->status;
1135 // method
1136 $subscriptionHistory->method = $this->_getRandomElement($this->subscriptionHistoryMethod);
1137 $subscriptionHistory->date = $this->_getRandomDate();
1138
1139 if ($groupContact->status != 'Pending') {
1140 $this->_insert($groupContact);
1141 }
1142 $this->_insert($subscriptionHistory);
1143 }
1144
1145 //In this function when we add groups that time we are cache the contact fields
1146 //But at the end of setup we are appending sample custom data, so for consistency
1147 //reset the cache.
1148 Civi::cache('fields')->flush();
1149 CRM_Core_BAO_Cache::resetCaches();
1150 }
1151
1152 /**
1153 *
1154 * addNote()
1155 *
1156 * This method populates the crm_note table
1157 *
1158 */
1159 public function addNote() {
1160
1161 $note = new CRM_Core_DAO_Note();
1162 $note->entity_table = 'civicrm_contact';
1163 $note->contact_id = 1;
1164
1165 for ($i = 0; $i < self::NUM_CONTACT; $i++) {
1166 $note->entity_id = $this->contact[$i];
1167 if ($this->contact[$i] % 5 || $this->contact[$i] % 3 || $this->contact[$i] % 2) {
1168 $this->_insertNote($note);
1169 }
1170 }
1171 }
1172
1173 /**
1174 *
1175 * addActivity()
1176 *
1177 * This method populates the crm_activity_history table
1178 *
1179 */
1180 public function addActivity() {
1181 $contactDAO = new CRM_Contact_DAO_Contact();
1182 $contactDAO->contact_type = 'Individual';
1183 $contactDAO->selectAdd();
1184 $contactDAO->selectAdd('id');
1185 $contactDAO->orderBy('sort_name');
1186 $contactDAO->find();
1187
1188 $count = 0;
1189 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
1190
1191 while ($contactDAO->fetch()) {
1192 if ($count++ > 2) {
1193 break;
1194 }
1195 for ($i = 0; $i < self::NUM_ACTIVITY; $i++) {
1196 $activityDAO = new CRM_Activity_DAO_Activity();
1197 $activityDAO->source_contact_id = $contactDAO->id;
1198 $activityTypeID = mt_rand(7, 10);
1199
1200 $activityDAO->activity_type_id = $activityTypeID;
1201 $activityDAO->subject = "Subject for $activity[$activityTypeID]";
1202 $activityDAO->activity_date_time = $this->_getRandomDate();
1203 $activityDAO->duration = mt_rand(1, 6);
1204 $activityDAO->status_id = 2;
1205 $this->_insert($activityDAO);
1206
1207 $activityContactDAO = new CRM_Activity_DAO_ActivityContact();
1208 $activityContactDAO->activity_id = $activityDAO->id;
1209 $activityContactDAO->contact_id = mt_rand(1, 101);
1210 $activityContactDAO->record_type_id = CRM_Utils_Array::key('Activity Source', $activityContacts);
1211 $this->_insert($activityContactDAO);
1212
1213 if (in_array($activityTypeID, array(
1214 6, 9,
1215 ))) {
1216 $activityTargetDAO = new CRM_Activity_DAO_ActivityContact();
1217 $activityTargetDAO->activity_id = $activityDAO->id;
1218 $activityTargetDAO->contact_id = mt_rand(1, 101);
1219 $activityTargetDAO->record_type_id = CRM_Utils_Array::key('Activity Targets', $activityContacts);
1220 $this->_insert($activityTargetDAO);
1221 }
1222
1223 if ($activityTypeID == 7) {
1224 $activityAssignmentDAO = new CRM_Activity_DAO_ActivityContact();
1225 $activityAssignmentDAO->activity_id = $activityDAO->id;
1226 $activityAssignmentDAO->contact_id = mt_rand(1, 101);
1227 $activityAssignmentDAO->record_type_id = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
1228 $this->_insert($activityAssignmentDAO);
1229 }
1230 }
1231 }
1232 }
1233
1234 /**
1235 * @return array
1236 */
1237 public static function getZipCodeInfo() {
1238 $stateID = mt_rand(1000, 5132);
1239 $offset = mt_rand(1, 4132);
1240
1241 $query = "SELECT id, country_id from civicrm_state_province LIMIT $offset, 1";
1242 $dao = new CRM_Core_DAO();
1243 $dao->query($query);
1244 while ($dao->fetch()) {
1245 return array($dao->country_id, $dao->id);
1246 }
1247
1248 return array();
1249 }
1250
1251 /**
1252 * @param $zipCode
1253 *
1254 * @return array
1255 */
1256 public static function getLatLong($zipCode) {
1257 $query = "http://maps.google.com/maps?q=$zipCode&output=js";
1258 $userAgent = "Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0";
1259
1260 $ch = curl_init();
1261 curl_setopt($ch, CURLOPT_URL, $query);
1262 curl_setopt($ch, CURLOPT_HEADER, FALSE);
1263 curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
1264 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
1265
1266 // grab URL and pass it to the browser
1267 $outstr = curl_exec($ch);
1268
1269 // close CURL resource, and free up system resources
1270 curl_close($ch);
1271
1272 $preg = "/'(<\?xml.+?)',/s";
1273 preg_match($preg, $outstr, $matches);
1274 if ($matches[1]) {
1275 $xml = simplexml_load_string($matches[1]);
1276 $attributes = $xml->center->attributes();
1277 if (!empty($attributes)) {
1278 return array((float ) $attributes['lat'], (float ) $attributes['lng']);
1279 }
1280 }
1281 return array(NULL, NULL);
1282 }
1283
1284 public function addMembershipType() {
1285 $organizationDAO = new CRM_Contact_DAO_Contact();
1286 $organizationDAO->id = 5;
1287 $organizationDAO->find(TRUE);
1288 $contact_id = $organizationDAO->contact_id;
1289
1290 $membershipType = "INSERT INTO civicrm_membership_type
1291 (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)
1292 VALUES
1293 ('General', 'Regular annual membership.', " . $contact_id . ", 3, 100, 'year', 1, 'rolling',null, null, 7, 'b_a', 'Public', 1, 1),
1294 ('Student', 'Discount membership for full-time students.', " . $contact_id . ", 1, 50, 'year', 1, 'rolling', null, null, 7, 'b_a', 'Public', 2, 1),
1295 ('Lifetime', 'Lifetime membership.', " . $contact_id . ", 2, 1200, 'lifetime', 1, 'rolling', null, null, 7, 'b_a', 'Admin', 3, 1);
1296 ";
1297 CRM_Core_DAO::executeQuery($membershipType);
1298 }
1299
1300 public function addMembership() {
1301 $contact = new CRM_Contact_DAO_Contact();
1302 $contact->query("SELECT id FROM civicrm_contact where contact_type = 'Individual'");
1303 while ($contact->fetch()) {
1304 $contacts[] = $contact->id;
1305 }
1306 shuffle($contacts);
1307
1308 $randomContacts = array_slice($contacts, 0, 350);
1309
1310 $sources = array('Payment', 'Donation', 'Check');
1311 $membershipTypes = array(2, 1);
1312 $membershipTypeNames = array('Student', 'General');
1313 $statuses = array(3, 4);
1314
1315 $membership = "
1316 INSERT INTO civicrm_membership
1317 (contact_id, membership_type_id, join_date, start_date, end_date, source, status_id)
1318 VALUES
1319 ";
1320 $activity = "
1321 INSERT INTO civicrm_activity
1322 (source_contact_id, 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)
1323 VALUES
1324 ";
1325
1326 foreach ($randomContacts as $count => $dontCare) {
1327 $source = self::_getRandomElement($sources);
1328 $acititySourceId = $count + 1;
1329 if ((($count + 1) % 11 == 0)) {
1330 // lifetime membership, status can be anything
1331 $startDate = date('Y-m-d', mktime(0, 0, 0, date('m'), (date('d') - $count), date('Y')));
1332 $membership .= "( {$randomContacts[$count]}, 3, '{$startDate}', '{$startDate}', null, '{$source}', 1)";
1333 $activity .= "( {$randomContacts[$count]}, {$acititySourceId}, 7, 'Lifetime', '{$startDate} 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 )";
1334 }
1335 elseif (($count + 1) % 5 == 0) {
1336 // Grace or expired, memberhsip type is random of 1 & 2
1337 $randId = array_rand($membershipTypes);
1338 $membershipType = self::_getRandomElement($membershipTypes);
1339 $startDate = date('Y-m-d', mktime(0, 0, 0,
1340 date('m'),
1341 (date('d') - ($count * ($randId + 1) * ($randId + 1) * ($randId + 1))),
1342 (date('Y') - ($randId + 1))
1343 ));
1344 $partOfDate = explode('-', $startDate);
1345 $endDate = date('Y-m-d', mktime(0, 0, 0,
1346 $partOfDate[1],
1347 ($partOfDate[2] - 1),
1348 ($partOfDate[0] + ($randId + 1))
1349 ));
1350 $membership .= "( {$randomContacts[$count]}, {$membershipType}, '{$startDate}', '{$startDate}', '{$endDate}', '{$source}', {$statuses[$randId]})";
1351 $activity .= "( {$randomContacts[$count]}, {$acititySourceId}, 7, '{$membershipTypeNames[$randId]}', '{$startDate} 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 )";
1352 }
1353 elseif (($count + 1) % 2 == 0) {
1354 // membership type 2
1355 $startDate = date('Y-m-d', mktime(0, 0, 0, date('m'), (date('d') - $count), date('Y')));
1356 $endDate = date('Y-m-d', mktime(0, 0, 0, date('m'), (date('d') - $count), (date('Y') + 1)));
1357 $membership .= "( {$randomContacts[$count]}, 2, '{$startDate}', '{$startDate}', '{$endDate}', '{$source}', 1)";
1358 $activity .= "( {$randomContacts[$count]}, {$acititySourceId}, 7, 'Student', '{$startDate} 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 )";
1359 }
1360 else {
1361 // membership type 1
1362 $startDate = date('Y-m-d', mktime(0, 0, 0, date('m'), (date('d') - $count), date('Y')));
1363 $endDate = date('Y-m-d', mktime(0, 0, 0, date('m'), (date('d') - $count), (date('Y') + 2)));
1364 $membership .= "( {$randomContacts[$count]}, 1, '{$startDate}', '{$startDate}', '{$endDate}', '{$source}', 1)";
1365 $activity .= "( {$randomContacts[$count]}, {$acititySourceId}, 7, 'General', '{$startDate} 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 )";
1366 }
1367
1368 if ($count != 349) {
1369 $membership .= ",";
1370 $activity .= ",";
1371 }
1372 }
1373
1374 CRM_Core_DAO::executeQuery($membership);
1375
1376 CRM_Core_DAO::executeQuery($activity);
1377 }
1378
1379 /**
1380 * @param $date
1381 *
1382 * @return string
1383 */
1384 public static function repairDate($date) {
1385 $dropArray = array('-' => '', ':' => '', ' ' => '');
1386 return strtr($date, $dropArray);
1387 }
1388
1389 public function addMembershipLog() {
1390 $membership = new CRM_Member_DAO_Membership();
1391 $membership->query("SELECT id FROM civicrm_membership");
1392 while ($membership->fetch()) {
1393 $ids[] = $membership->id;
1394 }
1395 require_once 'CRM/Member/DAO/MembershipLog.php';
1396 foreach ($ids as $id) {
1397 $membership = new CRM_Member_DAO_Membership();
1398 $membership->id = $id;
1399 $membershipLog = new CRM_Member_DAO_MembershipLog();
1400 if ($membership->find(TRUE)) {
1401 $membershipLog->membership_id = $membership->id;
1402 $membershipLog->status_id = $membership->status_id;
1403 $membershipLog->start_date = self::repairDate($membership->start_date);
1404 $membershipLog->end_date = self::repairDate($membership->end_date);
1405 $membershipLog->modified_id = $membership->contact_id;
1406 $membershipLog->modified_date = date("Ymd");
1407 $membershipLog->save();
1408 }
1409 $membershipLog = NULL;
1410 }
1411 }
1412
1413 public function createEvent() {
1414 $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)
1415 VALUES
1416 ( NULL, 1, 1, 1, 'S 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),
1417 ( NULL, 1, 1, 1, 'E 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),
1418 ( NULL, 1, 1, 1, 'E 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)
1419 ";
1420 CRM_Core_DAO::executeQuery($event);
1421
1422 $sql = "SELECT id from civicrm_address where street_address = 'S 14S El Camino Way E'";
1423 $eventAdd1 = CRM_Core_DAO::singleValueQuery($sql);
1424 $sql = "SELECT id from civicrm_address where street_address = 'E 11B Woodbridge Path SW'";
1425 $eventAdd2 = CRM_Core_DAO::singleValueQuery($sql);
1426 $sql = "SELECT id from civicrm_address where street_address = 'E 581O Lincoln Dr SW'";
1427 $eventAdd3 = CRM_Core_DAO::singleValueQuery($sql);
1428
1429 $event = "INSERT INTO civicrm_email (contact_id, location_type_id, email, is_primary, is_billing, on_hold, hold_date, reset_date)
1430 VALUES
1431 (NULL, 1, 'development@example.org', 0, 0, 0, NULL, NULL),
1432 (NULL, 1, 'tournaments@example.org', 0, 0, 0, NULL, NULL),
1433 (NULL, 1, 'celebration@example.org', 0, 0, 0, NULL, NULL)
1434 ";
1435 CRM_Core_DAO::executeQuery($event);
1436
1437 $sql = "SELECT id from civicrm_email where email = 'development@example.org'";
1438 $eventEmail1 = CRM_Core_DAO::singleValueQuery($sql);
1439 $sql = "SELECT id from civicrm_email where email = 'tournaments@example.org'";
1440 $eventEmail2 = CRM_Core_DAO::singleValueQuery($sql);
1441 $sql = "SELECT id from civicrm_email where email = 'celebration@example.org'";
1442 $eventEmail3 = CRM_Core_DAO::singleValueQuery($sql);
1443
1444 $event = "INSERT INTO civicrm_phone (contact_id, location_type_id, is_primary, is_billing, mobile_provider_id, phone, phone_type_id)
1445 VALUES
1446 (NULL, 1, 0, 0, NULL,'204 222-1000', '1'),
1447 (NULL, 1, 0, 0, NULL,'204 223-1000', '1'),
1448 (NULL, 1, 0, 0, NULL,'303 323-1000', '1')
1449 ";
1450 CRM_Core_DAO::executeQuery($event);
1451
1452 $sql = "SELECT id from civicrm_phone where phone = '204 222-1000'";
1453 $eventPhone1 = CRM_Core_DAO::singleValueQuery($sql);
1454 $sql = "SELECT id from civicrm_phone where phone = '204 223-1000'";
1455 $eventPhone2 = CRM_Core_DAO::singleValueQuery($sql);
1456 $sql = "SELECT id from civicrm_phone where phone = '303 323-1000'";
1457 $eventPhone3 = CRM_Core_DAO::singleValueQuery($sql);
1458
1459 $event = "INSERT INTO civicrm_loc_block ( address_id, email_id, phone_id, address_2_id, email_2_id, phone_2_id)
1460 VALUES
1461 ( $eventAdd1, $eventEmail1, $eventPhone1, NULL,NULL,NULL),
1462 ( $eventAdd2, $eventEmail2, $eventPhone2, NULL,NULL,NULL),
1463 ( $eventAdd3, $eventEmail3, $eventPhone3, NULL,NULL,NULL)
1464 ";
1465
1466 CRM_Core_DAO::executeQuery($event);
1467
1468 $sql = "SELECT id from civicrm_loc_block where phone_id = $eventPhone1 AND email_id = $eventEmail1 AND address_id = $eventAdd1";
1469 $eventLok1 = CRM_Core_DAO::singleValueQuery($sql);
1470 $sql = "SELECT id from civicrm_loc_block where phone_id = $eventPhone2 AND email_id = $eventEmail2 AND address_id = $eventAdd2";
1471 $eventLok2 = CRM_Core_DAO::singleValueQuery($sql);
1472 $sql = "SELECT id from civicrm_loc_block where phone_id = $eventPhone3 AND email_id = $eventEmail3 AND address_id = $eventAdd3";
1473 $eventLok3 = CRM_Core_DAO::singleValueQuery($sql);
1474
1475 //create event fees
1476 $optionGroup = "INSERT INTO civicrm_option_group ( name, is_reserved, is_active)
1477 VALUES
1478 ( 'civicrm_event.amount.1', 0, 1),
1479 ( 'civicrm_event.amount.2', 0, 1),
1480 ( 'civicrm_event.amount.3', 0, 1)
1481 ";
1482 CRM_Core_DAO::executeQuery($optionGroup);
1483
1484 $sql = "SELECT max(id) from civicrm_option_group where name = 'civicrm_event.amount.1'";
1485 $page1 = CRM_Core_DAO::singleValueQuery($sql);
1486
1487 $sql = "SELECT max(id) from civicrm_option_group where name = 'civicrm_event.amount.2'";
1488 $page2 = CRM_Core_DAO::singleValueQuery($sql);
1489
1490 $sql = "SELECT max(id) from civicrm_option_group where name = 'civicrm_event.amount.3'";
1491 $page3 = CRM_Core_DAO::singleValueQuery($sql);
1492
1493 $optionValue = "INSERT INTO civicrm_option_value (option_group_id, label, value, is_default, weight, is_optgroup, is_reserved, is_active)
1494 VALUES
1495 ($page1, 'Single', '50', 0, 1, 0, 0, 1),
1496 ($page1, 'Couple', '100', 0, 2, 0, 0, 1),
1497 ($page1, 'Family', '200', 0, 3, 0, 0, 1),
1498 ($page2, 'Bass', '25', 0, 1, 0, 0, 1),
1499 ($page2, 'Tenor', '40', 0, 2, 0, 0, 1),
1500 ($page2, 'Soprano', '50', 0, 3, 0, 0, 1),
1501 ($page3, 'Tiny-tots (ages 5-8)', '800', 0, 1, 0, 0, 1),
1502 ($page3, 'Junior Stars (ages 9-12)', '1000', 0, 2, 0, 0, 1),
1503 ($page3, 'Super Stars (ages 13-18)', '1500', 0, 3, 0, 0, 1)";
1504
1505 CRM_Core_DAO::executeQuery($optionValue);
1506
1507 $sql = "SELECT max(id) FROM civicrm_option_value WHERE civicrm_option_value.option_group_id = $page1 AND civicrm_option_value.weight=2";
1508 $defaultFee1 = CRM_Core_DAO::singleValueQuery($sql);
1509
1510 $sql = "SELECT max(id) FROM civicrm_option_value WHERE civicrm_option_value.option_group_id = $page2 AND civicrm_option_value.weight=2";
1511 $defaultFee2 = CRM_Core_DAO::singleValueQuery($sql);
1512
1513 $sql = "SELECT max(id) FROM civicrm_option_value WHERE civicrm_option_value.option_group_id = $page3 AND civicrm_option_value.weight=2";
1514 $defaultFee3 = CRM_Core_DAO::singleValueQuery($sql);
1515
1516 $event = "INSERT INTO civicrm_event
1517 ( 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 )
1518 VALUES
1519 ( '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, '2010-11-21 17:00:00', '2010-11-21 23:00:00', 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, {$defaultFee1}, '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=https://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', 0, 0 ),
1520 ( '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, '2011-06-01 12:00:00', '2011-06-01 17:00:00', 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, {$defaultFee2}, '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=https://civicrm.org>Back to CiviCRM Home Page</a></p>', 0, NULL, NULL, 1, 0 ),
1521 ( '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, '2011-12-27 07:00:00', '2011-12-29 17:00:00', 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, {$defaultFee3}, 'Thanks for Your Support!', '<p>Thank you for your support. Your participation will help save thousands of acres of rainforest.</p>', '<p><a href=https://civicrm.org>Back to CiviCRM Home Page</a></p>', 0, NULL, NULL, 0, 0 )
1522 ";
1523 CRM_Core_DAO::executeQuery($event);
1524 }
1525
1526 public function addParticipant() {
1527 // add participant
1528 $participant = new CRM_Event_DAO_Participant();
1529
1530 for ($id = 1; $id <= self::NUM_PARTICIPANT; $id++) {
1531 $participant->contact_id = mt_rand(1, self::NUM_CONTACT);
1532 $participant->event_id = mt_rand(1, 3);
1533 $participant->status_id = mt_rand(1, 5);
1534 $participant->role_id = mt_rand(1, 4);
1535 $participant->register_date = $this->_getRandomDate();
1536 $participant->source = "Credit Card";
1537
1538 if ($participant->event_id == 1) {
1539 $fee_level = "Single";
1540 $fee_amount = 50;
1541 }
1542 elseif ($participant->event_id == 2) {
1543 $fee_level = "Soprano";
1544 $fee_amount = 50;
1545 }
1546 else {
1547 $fee_level = "Tiny-tots (ages 5-8)";
1548 $fee_amount = 800;
1549 }
1550 $participant->fee_level = $fee_level;
1551 $participant->fee_amount = $fee_amount;
1552 $participant->is_test = 0;
1553
1554 $this->_insert($participant);
1555 }
1556 }
1557
1558 public function addPCP() {
1559 $query = "
1560 INSERT INTO `civicrm_pcp`
1561 (contact_id, status_id, title, intro_text, page_text, donate_link_text, contribution_page_id, is_thermometer, is_honor_roll, goal_amount, referer, is_active)
1562 VALUES
1563 ({$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=\"https://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, 1, 1, 5000.00, NULL, 1);
1564 ";
1565 CRM_Core_DAO::executeQuery($query);
1566 }
1567
1568 public function addContribution() {
1569 // add contributions
1570 $contribution = new CRM_Contribute_DAO_Contribution();
1571
1572 for ($id = 1; $id <= self::NUM_CONTRIBUTION; $id++) {
1573 $contribution->contact_id = mt_rand(1, self::NUM_CONTACT);
1574 $contribution->financial_type_id = mt_rand(1, 4);
1575 $contribution->contribution_page_id = mt_rand(1, 3);
1576 $contribution->payment_instrument_id = mt_rand(1, 5);
1577 $contribution->receive_date = $this->_getRandomDate();
1578 $contribution->total_amount = mt_rand(10, 99);
1579 $contribution->contribution_status_id = mt_rand(1, 6);
1580 $contribution->trxn_id = "#" . md5($contribution->receive_date);
1581 $this->_insert($contribution);
1582 }
1583 }
1584
1585 public function addSoftContribution() {
1586 $pcpRollNickNAme = array('Jones Family', 'Annie and the kids', 'Anonymous', 'Adam Family');
1587
1588 $pcpPersonalNote = array('Helping Hands', 'Annie Helps', 'Anonymous', 'Adam Helps');
1589
1590 $softContribution = new CRM_Contribute_DAO_ContributionSoft();
1591
1592 $sql = "SELECT DISTINCT(contact_id), id, total_amount from civicrm_contribution LIMIT 200";
1593
1594 $contriInfo = CRM_Core_DAO::executeQuery($sql);
1595
1596 $prevContactID = NULL;
1597
1598 while ($contriInfo->fetch()) {
1599 if ($prevContactID) {
1600 $softContribution->contribution_id = $contriInfo->id;
1601 $softContribution->contact_id = $prevContactID;
1602 $softContribution->amount = $contriInfo->total_amount;
1603 $softContribution->pcp_id = 1;
1604 $softContribution->pcp_display_in_roll = 1;
1605 $softContribution->pcp_roll_nickname = $this->_getRandomElement($pcpRollNickNAme);
1606 $softContribution->pcp_personal_note = $this->_getRandomElement($pcpPersonalNote);
1607 $this->_insert($softContribution);
1608 }
1609 $prevContactID = $contriInfo->contact_id;
1610 }
1611 }
1612
1613 public function addPledge() {
1614 $pledge = "INSERT INTO civicrm_pledge
1615 (contact_id, financial_type_id, contribution_page_id, amount, frequency_unit, frequency_interval, frequency_day, installments, start_date, create_date, acknowledge_date, modified_date, cancel_date, end_date, honor_contact_id, honor_type_id, status_id, is_test)
1616 VALUES
1617 (71, 1, 1, 500.00, 'month', 1, 1, 1, '2010-07-01 21:19:02', '2010-06-26 00:00:00', NULL, NULL, NULL,'2010-07-01 00:00:00', NULL, NULL, 1, 0),
1618 (43, 1, 1, 800.00, 'month', 3, 1, 4, '2010-07-01 10:11:09', '2010-06-23 10:11:14', '2010-06-23 10:11:18', NULL, NULL, '2010-04-01 10:11:40', NULL, NULL, 5, 0),
1619 (32, 1, 1, 600.00, 'month', 1, 1, 3, '2010-06-01 10:12:35', '2010-05-14 10:12:44', '2010-05-14 10:12:52', NULL, NULL, '2010-08-01 10:13:11', NULL, NULL, 5, 0);
1620 ";
1621 CRM_Core_DAO::executeQuery($pledge);
1622 }
1623
1624 public function addPledgePayment() {
1625 $pledgePayment = "INSERT INTO civicrm_pledge_payment
1626 ( pledge_id, contribution_id, scheduled_amount, scheduled_date, reminder_date, reminder_count, status_id)
1627 VALUES
1628 (1, 10, 500.00, '2010-07-01 13:03:45', null, 0, 1),
1629 (2, 11, 200.00, '2010-07-01 10:59:35', null, 0, 1),
1630 (2, null, 200.00, '2010-10-01 10:59:35',null, 0, 2),
1631 (2, null, 200.00, '2010-01-01 10:59:35',null, 0, 2),
1632 (2, null, 200.00, '2010-04-01 10:59:35',null, 0, 2),
1633 (3, 12, 200.00, '2010-06-01 11:00:12', null, 0, 1),
1634 (3, 13, 200.00, '2010-07-01 10:59:35', '2010-06-28 10:59:41', 1, 1),
1635 (3, null, 200.00, '2010-08-01 11:00:12', null, 0, 2);
1636 ";
1637 CRM_Core_DAO::executeQuery($pledgePayment);
1638 }
1639
1640 public function addMembershipPayment() {
1641 $amount = array('50', '100', '1200');
1642
1643 $contribution = new CRM_Contribute_DAO_Contribution();
1644 for ($id = 1; $id <= 200; $id++) {
1645 $contribution->contact_id = mt_rand(1, self::NUM_CONTACT);
1646 $contribution->financial_type_id = mt_rand(1, 4);
1647 $contribution->payment_instrument_id = mt_rand(1, 5);
1648 $contribution->receive_date = $this->_getRandomDate();
1649 $contribution->total_amount = $this->_getRandomElement($amount);
1650 $contribution->contribution_status_id = mt_rand(1, 6);
1651 $contribution->trxn_id = "#" . md5($contribution->receive_date);
1652 $this->_insert($contribution);
1653 }
1654 for ($i = 0; $i < 3; $i++) {
1655 $contributionsArray = $membershipArray = array();
1656 $contributionSQL = "
1657 SELECT id
1658 FROM civicrm_contribution
1659 WHERE contribution_page_id IS NULL AND
1660 total_amount = {$amount[$i]} limit 0, 50 ";
1661
1662 $contributionDAO = CRM_Core_DAO::executeQuery($contributionSQL);
1663
1664 while ($contributionDAO->fetch()) {
1665 $contributionsArray[] = $contributionDAO->id;
1666 }
1667 $j = $i + 1;
1668 $membershipSQL = "
1669 SELECT id
1670 FROM civicrm_membership
1671 WHERE civicrm_membership.membership_type_id = {$j} limit 0, 50";
1672 $membershipDAO = CRM_Core_DAO::executeQuery($membershipSQL);
1673
1674 while ($membershipDAO->fetch()) {
1675 $membershipArray[] = $membershipDAO->id;
1676 }
1677
1678 $payemntOBJ = new CRM_Member_DAO_MembershipPayment();
1679 foreach ($membershipArray as $key => $membershipid) {
1680 $payemntOBJ->contribution_id = $contributionsArray[$key];
1681 $payemntOBJ->membership_id = $membershipid;
1682 $this->_insert($payemntOBJ);
1683 }
1684 }
1685 }
1686
1687 }
1688
1689 /**
1690 * @param null $str
1691 *
1692 * @return bool
1693 */
1694 function user_access($str = NULL) {
1695 return TRUE;
1696 }
1697
1698 /**
1699 * @return array
1700 */
1701 function module_list() {
1702 return array();
1703 }
1704
1705 echo ("Starting data generation on " . date("F dS h:i:s A") . "\n");
1706 $obj1 = new CRM_GCD();
1707 $obj1->initID();
1708 $obj1->parseDataFile();
1709 $obj1->initDB();
1710 $obj1->addDomain();
1711 $obj1->addContact();
1712 $obj1->addIndividual();
1713 $obj1->addHousehold();
1714 $obj1->addOrganization();
1715 $obj1->addRelationship();
1716 $obj1->addLocation();
1717 $obj1->addEntityTag();
1718 $obj1->addGroup();
1719 $obj1->addNote();
1720 $obj1->addActivity();
1721 $obj1->addMembership();
1722 $obj1->addMembershipLog();
1723 $obj1->createEvent();
1724 $obj1->addParticipant();
1725 $obj1->addContribution();
1726 $obj1->addPCP();
1727 $obj1->addSoftContribution();
1728 $obj1->addPledge();
1729 $obj1->addPledgePayment();
1730 $obj1->addMembershipPayment();
1731 echo ("Ending data generation on " . date("F dS h:i:s A") . "\n");