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