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