GenerateReportData.php 65.1 KB
Newer Older
totten's avatar
totten committed
1 2 3
<?php
/*
 +--------------------------------------------------------------------+
4
 | CiviCRM version 5                                                  |
totten's avatar
totten committed
5
 +--------------------------------------------------------------------+
Seamus Lee's avatar
Seamus Lee committed
6
 | Copyright CiviCRM LLC (c) 2004-2019                                |
totten's avatar
totten committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License and the CiviCRM Licensing Exception along                  |
 | with this program; if not, contact CiviCRM LLC                     |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 +--------------------------------------------------------------------+
26
 */
totten's avatar
totten committed
27 28 29 30

/**
 *
 * @package CRM
Seamus Lee's avatar
Seamus Lee committed
31
 * @copyright CiviCRM LLC (c) 2004-2019
totten's avatar
totten committed
32 33 34 35
 * $Id$
 *
 */

36
/**
totten's avatar
totten committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
 * This class generates data for the schema located in Contact.sql
 *
 * each public method generates data for the concerned table.
 * so for example the addContactDomain method generates and adds
 * data to the contact_domain table
 *
 * Data generation is a bit tricky since the data generated
 * randomly in one table could be used as a FKEY in another
 * table.
 *
 * In order to ensure that a randomly generated FKEY matches
 * a field in the referened table, the field in the referenced
 * table is always generated linearly.
 *
 *
 *
 *
 * Some numbers
 *
 * Domain ID's - 1 to NUM_DOMAIN
 *
 * Context - 3/domain
 *
 * Contact - 1 to NUM_CONTACT
 *           75% - Individual
 *           15% - Household
 *           10% - Organization
 *
 *           Contact to Domain distribution should be equal.
 *
 *
 * Contact Individual = 1 to 0.75*NUM_CONTACT
 *
 * Contact Household = 0.75*NUM_CONTACT to 0.9*NUM_CONTACT
 *
 * Contact Organization = 0.9*NUM_CONTACT to NUM_CONTACT
 *
 * Contact Location = 15% for Households, 10% for Organizations, (75-(15*4))% for Individuals.
 *                     (Assumption is that each household contains 4 individuals)
 *
77
 */
totten's avatar
totten committed
78

79
/**
totten's avatar
totten committed
80 81 82 83 84 85 86 87 88
 *
 * Note: implication of using of mt_srand(1) in constructor
 * The data generated will be done in a consistent manner
 * so as to give the same data during each run (but this
 * would involve populating the entire db at one go - since
 * mt_srand(1) is in the constructor, if one needs to be able
 * to get consistent random numbers then the mt_srand(1) shld
 * be in each function that adds data to each table.
 *
89
 */
totten's avatar
totten committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113


require_once '../civicrm.config.php';

require_once 'CRM/Core/Config.php';
require_once 'CRM/Core/Error.php';
require_once 'CRM/Core/I18n.php';

require_once 'CRM/Core/DAO/Address.php';
require_once 'CRM/Core/DAO.php';
require_once 'CRM/Core/DAO/Phone.php';
require_once 'CRM/Core/DAO/Email.php';
require_once 'CRM/Core/DAO/EntityTag.php';
require_once 'CRM/Core/DAO/Note.php';
require_once 'CRM/Core/DAO/Domain.php';

require_once 'CRM/Contact/DAO/Group.php';
require_once 'CRM/Contact/DAO/GroupContact.php';
require_once 'CRM/Contact/DAO/SubscriptionHistory.php';
require_once 'CRM/Contact/DAO/Contact.php';
require_once 'CRM/Contact/DAO/Relationship.php';
require_once 'CRM/Event/DAO/Participant.php';
require_once 'CRM/Contribute/DAO/ContributionSoft.php';
require_once 'CRM/Member/DAO/MembershipPayment.php';
114 115 116 117

/**
 * Class CRM_GCD
 */
totten's avatar
totten committed
118 119
class CRM_GCD {

120
  /**
totten's avatar
totten committed
121
   * constants
122
   */
123 124 125 126 127 128 129 130 131 132 133
  const DATA_FILENAME = "sample_data.xml";
  const NUM_DOMAIN = 1;
  const NUM_CONTACT = 5000;
  const NUM_CONTRIBUTION = 2000;
  const NUM_MEMBERSHIP = 2000;
  const NUM_PARTICIPANT = 2000;
  const INDIVIDUAL_PERCENT = 75;
  const HOUSEHOLD_PERCENT = 15;
  const ORGANIZATION_PERCENT = 10;
  const NUM_INDIVIDUAL_PER_HOUSEHOLD = 4;
  const NUM_ACTIVITY = 150;
totten's avatar
totten committed
134 135

  // relationship types from the table crm_relationship_type
136 137 138 139 140
  const CHILD_OF = 1;
  const SPOUSE_OF = 2;
  const SIBLING_OF = 3;
  const HEAD_OF_HOUSEHOLD = 6;
  const MEMBER_OF_HOUSEHOLD = 7;
totten's avatar
totten committed
141 142 143


  // location types from the table crm_location_type
144 145 146 147 148
  const HOME = 1;
  const WORK = 2;
  const MAIN = 3;
  const OTHER = 4;
  const ADD_TO_DB = TRUE;
totten's avatar
totten committed
149
  //const ADD_TO_DB=FALSE;
150
  const DEBUG_LEVEL = 1;
totten's avatar
totten committed
151

152
  /***
totten's avatar
totten committed
153
   * private members
154
   */
totten's avatar
totten committed
155

156 157 158 159
  /**
   * enum's from database
   * @var array
   */
totten's avatar
totten committed
160 161 162 163
  private $preferredCommunicationMethod = array('1', '2', '3', '4', '5');
  private $contactType = array('Individual', 'Household', 'Organization');
  private $phoneType = array('1', '2', '3', '4');

164 165 166 167
  /**
   * customizable enums (foreign keys)
   * @var array
   */
totten's avatar
totten committed
168 169 170 171 172
  private $prefix = array(1 => 'Mrs', 2 => 'Ms', 3 => 'Mr', 4 => 'Dr');
  private $suffix = array(1 => 'Jr', 2 => 'Sr');
  private $gender = array(1 => 'Female', 2 => 'Male');
  private $greetingType = array(1 => 'Dear [first]', 2 => 'Dear [prefix] [first] [last]', 3 => 'Dear [prefix] [last]');

173 174 175 176
  /**
   * store domain id's
   * @var array
   */
totten's avatar
totten committed
177 178
  private $domain = array();

179 180 181 182
  /**
   * store contact id's
   * @var array
   */
totten's avatar
totten committed
183 184 185 186 187 188
  private $contact = array();
  private $individual = array();
  private $household = array();
  private $organization = array();


189 190 191 192
  /**
   * store names, firstnames, street 1, street2
   * @var array
   */
totten's avatar
totten committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
  private $firstName = array();
  private $lastName = array();
  private $streetName = array();
  private $supplementalAddress1 = array();
  private $city = array();
  private $state = array();
  private $country = array();
  private $addressDirection = array();
  private $streetType = array();
  private $emailDomain = array();
  private $emailTLD = array();
  private $organizationName = array();
  private $organizationField = array();
  private $organizationType = array();
  private $group = array();
  private $note = array();
  private $activity_type = array();
  private $module = array();
  private $callback = array();
  private $party_registration = array();
  private $degree = array();
  private $school = array();

216 217 218 219
  /**
   * stores the strict individual id and household id to individual id mapping
   * @var array
   */
totten's avatar
totten committed
220 221 222
  private $strictIndividual = array();
  private $householdIndividual = array();

223 224 225 226
  /**
   * sample data in xml format
   * @var int
   */
totten's avatar
totten committed
227 228
  private $sampleData = NULL;

229 230 231 232
  /**
   * private vars
   * @var int
   */
totten's avatar
totten committed
233 234 235 236 237 238
  private $numIndividual = 0;
  private $numHousehold = 0;
  private $numOrganization = 0;
  private $numStrictIndividual = 0;

  private $CSC = array(
239
    // united states
totten's avatar
totten committed
240 241 242 243 244 245
    1228 => array(
      // california
      1004 => array('San Francisco', 'Los Angeles', 'Palo Alto'),
      // new york
      1031 => array('New York', 'Albany'),
    ),
246
    // india
totten's avatar
totten committed
247 248 249 250 251 252
    1101 => array(
      // maharashtra
      1113 => array('Mumbai', 'Pune', 'Nasik'),
      // karnataka
      1114 => array('Bangalore', 'Mangalore', 'Udipi'),
    ),
253
    // poland
totten's avatar
totten committed
254 255 256 257 258 259 260
    1172 => array(
      // mazowieckie
      1115 => array('Warszawa', 'Płock'),
      // pomorskie
      1116 => array('Gdańsk', 'Gdynia'),
    ),
  );
261 262 263
  /**
   * @var array
   */
totten's avatar
totten committed
264 265 266
  private $groupMembershipStatus = array('Added', 'Removed', 'Pending');
  private $subscriptionHistoryMethod = array('Admin', 'Email');

267
  /**
totten's avatar
totten committed
268
   * private methods
269
   *
270 271 272
   * @param int $size
   * @return string
   */
totten's avatar
totten committed
273

274 275 276 277 278
  /**
   * Get a randomly generated string.
   *
   * @param int $size
   */
totten's avatar
totten committed
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
  private function _getRandomString($size = 32) {
    $string = "";

    // get an ascii code for each character
    for ($i = 0; $i < $size; $i++) {
      $random_int = mt_rand(65, 122);
      if (($random_int < 97) && ($random_int > 90)) {
        // if ascii code between 90 and 97 substitute with space
        $random_int = 32;
      }
      $random_char = chr($random_int);
      $string .= $random_char;
    }
    return $string;
  }

295 296 297
  /**
   * @return string
   */
totten's avatar
totten committed
298 299 300 301
  private function _getRandomChar() {
    return chr(mt_rand(65, 90));
  }

302 303 304
  /**
   * @return int
   */
totten's avatar
totten committed
305 306 307 308
  private function getRandomBoolean() {
    return mt_rand(0, 1);
  }

309 310 311 312 313
  /**
   * @param $array1
   *
   * @return mixed
   */
totten's avatar
totten committed
314 315 316 317
  private function _getRandomElement(&$array1) {
    return $array1[mt_rand(1, count($array1)) - 1];
  }

318 319 320 321 322
  /**
   * @param $array1
   *
   * @return int
   */
totten's avatar
totten committed
323 324 325 326 327
  private function _getRandomIndex(&$array1) {
    return mt_rand(1, count($array1));
  }

  // country state city combo
328

329 330 331
  /**
   * @return array
   */
totten's avatar
totten committed
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
  private function _getRandomCSC() {
    $array1 = array();

    // $c = array_rand($this->CSC);
    $c = 1228;

    // the state array now
    $s = array_rand($this->CSC[$c]);

    // the city
    $ci = array_rand($this->CSC[$c][$s]);
    $city = $this->CSC[$c][$s][$ci];

    $array1[] = $c;
    $array1[] = $s;
    $array1[] = $city;

    return $array1;
  }

  /**
   * Generate a random date.
   *
   *   If both $startDate and $endDate are defined generate
   *   date between them.
   *
   *   If only startDate is specified then date generated is
   *   between startDate + 1 year.
   *
   *   if only endDate is specified then date generated is
   *   between endDate - 1 year.
   *
   *   if none are specified - date is between today - 1year
   *   and today
   *
   * @param  int $startDate Start Date in Unix timestamp
   * @param  int $endDate   End Date in Unix timestamp
   * @access private
   *
   * @return string randomly generated date in the format "Ymd"
   *
   */
  private function _getRandomDate($startDate = 0, $endDate = 0) {

    // number of seconds per year
    // $numSecond = 31536000;
    // number of seconds for 2 year
    $numSecond = 63072000;

381
    $dateFormat = "YmdHis";
totten's avatar
totten committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
    $today = time();

    // both are defined
    if ($startDate && $endDate) {
      return date($dateFormat, mt_rand($startDate, $endDate));
    }

    // only startDate is defined
    if ($startDate) {
      // $nextYear = mktime(0, 0, 0, date("m", $startDate),   date("d", $startDate),   date("Y")+1);
      return date($dateFormat, mt_rand($startDate, $startDate + $numSecond));
    }

    // only endDate is defined
    if ($startDate) {
      return date($dateFormat, mt_rand($endDate - $numSecond, $endDate));
    }

    // none are defined
    return date($dateFormat, mt_rand($today - $numSecond, $today));
  }

  // insert data into db's
405

406 407 408
  /**
   * @param $dao
   */
totten's avatar
totten committed
409 410 411
  private function _insert(&$dao) {
    if (self::ADD_TO_DB) {
      if (!$dao->insert()) {
412
        echo "ERROR INSERT: " . mysqli_error($dao->getConnection()->connection) . "\n";
totten's avatar
totten committed
413 414 415 416 417 418 419
        print_r($dao);
        exit(1);
      }
    }
  }

  // update data into db's
420

421 422 423
  /**
   * @param $dao
   */
totten's avatar
totten committed
424 425 426
  private function _update($dao) {
    if (self::ADD_TO_DB) {
      if (!$dao->update()) {
427
        echo "ERROR UPDATE: " . mysqli_error($dao->getConnection()->connection) . "\n";
totten's avatar
totten committed
428 429 430 431 432 433 434 435 436 437 438 439
        print_r($dao);
        exit(1);
      }
    }
  }

  /**
   * Insert a note
   *
   *   Helper function which randomly populates "note" and
   *   "date_modified" and inserts it.
   *
440
   * @param  $note
totten's avatar
totten committed
441 442 443 444 445 446 447 448 449
   * @access private
   *
   */
  private function _insertNote($note) {
    $note->note = $this->_getRandomElement($this->note);
    $note->modified_date = $this->_getRandomDate();
    $this->_insert($note);
  }

450
  /**
totten's avatar
totten committed
451 452 453
   *
   * Start of public functions
   *
454 455
   */
  public function __construct() {
totten's avatar
totten committed
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
    // initialize all the vars
    $this->numIndividual = self::INDIVIDUAL_PERCENT * self::NUM_CONTACT / 100;
    $this->numHousehold = self::HOUSEHOLD_PERCENT * self::NUM_CONTACT / 100;
    $this->numOrganization = self::ORGANIZATION_PERCENT * self::NUM_CONTACT / 100;
    $this->numStrictIndividual = $this->numIndividual - ($this->numHousehold * self::NUM_INDIVIDUAL_PER_HOUSEHOLD);
  }

  public function parseDataFile() {

    $sampleData = simplexml_load_file(self::DATA_FILENAME);

    // first names
    foreach ($sampleData->first_names->first_name as $first_name) {
      $this->firstName[] = trim($first_name);
    }

    // last names
    foreach ($sampleData->last_names->last_name as $last_name) {
      $this->lastName[] = trim($last_name);
    }

    //  street names
    foreach ($sampleData->street_names->street_name as $street_name) {
      $this->streetName[] = trim($street_name);
    }

    //  supplemental address 1
    foreach ($sampleData->supplemental_addresses_1->supplemental_address_1 as $supplemental_address_1) {
      $this->supplementalAddress1[] = trim($supplemental_address_1);
    }

    //  cities
    foreach ($sampleData->cities->city as $city) {
      $this->city[] = trim($city);
    }

    //  address directions
    foreach ($sampleData->address_directions->address_direction as $address_direction) {
      $this->addressDirection[] = trim($address_direction);
    }

    // street types
    foreach ($sampleData->street_types->street_type as $street_type) {
      $this->streetType[] = trim($street_type);
    }

    // email domains
    foreach ($sampleData->email_domains->email_domain as $email_domain) {
      $this->emailDomain[] = trim($email_domain);
    }

    // email top level domain
    foreach ($sampleData->email_tlds->email_tld as $email_tld) {
      $this->emailTLD[] = trim($email_tld);
    }

    // organization name
    foreach ($sampleData->organization_names->organization_name as $organization_name) {
      $this->organization_name[] = trim($organization_name);
    }

    // organization field
    foreach ($sampleData->organization_fields->organization_field as $organization_field) {
      $this->organizationField[] = trim($organization_field);
    }

    // organization type
    foreach ($sampleData->organization_types->organization_type as $organization_type) {
      $this->organizationType[] = trim($organization_type);
    }

    // group
    foreach ($sampleData->groups->group as $group) {
      $this->group[] = trim($group);
    }

    // notes
    foreach ($sampleData->notes->note as $note) {
      $this->note[] = trim($note);
    }

    // activity type
    foreach ($sampleData->activity_types->activity_type as $activity_type) {
      $this->activity_type[] = trim($activity_type);
    }

    // module
    foreach ($sampleData->modules->module as $module) {
      $this->module[] = trim($module);
    }

    // callback
    foreach ($sampleData->callbacks->callback as $callback) {
      $this->callback[] = trim($callback);
    }

    // custom data - party registration
    foreach ($sampleData->party_registrations->party_registration as $party_registration) {
      $this->party_registration[] = trim($party_registration);
    }

    // custom data - degrees
    foreach ($sampleData->degrees->degree as $degree) {
      $this->degree[] = trim($degree);
    }

    // custom data - schools
    foreach ($sampleData->schools->school as $school) {
      $this->school[] = trim($school);
    }

    // custom data - issue
    foreach ($sampleData->issue->status as $status) {
      $this->issue[] = trim($status);
    }

    // custom data - gotv
    require_once 'CRM/Core/BAO/CustomOption.php';
    foreach ($sampleData->gotv->status as $status) {
      $this->gotv[] = CRM_Core_DAO::VALUE_SEPARATOR . trim($status) . CRM_Core_DAO::VALUE_SEPARATOR;
    }

    // custom data - marital_status
    foreach ($sampleData->marital_status->status as $status) {
      $this->marital_status[] = trim($status);
    }
  }

584 585 586 587 588
  /**
   * @param $id
   *
   * @return string
   */
totten's avatar
totten committed
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
  public function getContactType($id) {
    if (in_array($id, $this->individual)) {
      return 'Individual';
    }
    if (in_array($id, $this->household)) {
      return 'Household';
    }
    if (in_array($id, $this->organization)) {
      return 'Organization';
    }
  }

  public function initDB() {
    $config = CRM_Core_Config::singleton();
  }

605
  /**
totten's avatar
totten committed
606 607 608 609 610 611 612 613 614 615 616 617 618 619
   *
   * this function creates arrays for the following
   *
   * domain id
   * contact id
   * contact_location id
   * contact_contact_location id
   * contact_email uuid
   * contact_phone_uuid
   * contact_instant_message uuid
   * contact_relationship uuid
   * contact_task uuid
   * contact_note uuid
   *
620
   */
totten's avatar
totten committed
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
  public function initID() {

    // may use this function in future if needed to get
    // a consistent pattern of random numbers.

    // get the domain and contact id arrays
    $this->domain = range(1, self::NUM_DOMAIN);
    shuffle($this->domain);
    $this->contact = range(2, self::NUM_CONTACT + 1);
    shuffle($this->contact);

    // get the individual, household  and organizaton contacts
    $offset = 0;
    $this->individual = array_slice($this->contact, $offset, $this->numIndividual);
    $offset += $this->numIndividual;
    $this->household = array_slice($this->contact, $offset, $this->numHousehold);
    $offset += $this->numHousehold;
    $this->organization = array_slice($this->contact, $offset, $this->numOrganization);

    // get the strict individual contacts (i.e individual contacts not belonging to any household)
    $this->strictIndividual = array_slice($this->individual, 0, $this->numStrictIndividual);

    // get the household to individual mapping array
    $this->householdIndividual = array_diff($this->individual, $this->strictIndividual);
    $this->householdIndividual = array_chunk($this->householdIndividual, self::NUM_INDIVIDUAL_PER_HOUSEHOLD);
    $this->householdIndividual = array_combine($this->household, $this->householdIndividual);
  }

649
  /**
totten's avatar
totten committed
650 651 652 653 654 655
   *
   * addDomain()
   *
   * This method adds NUM_DOMAIN domains and then adds NUM_REVISION
   * revisions for each domain with the latest revision being the last one..
   *
656
   */
totten's avatar
totten committed
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
  public function addDomain() {

    /* Add a location for domain 1 */

    // FIXME FOR NEW LOCATION BLOCK STRUCTURE
    // $this->_addLocation(self::MAIN, 1, true);

    $domain = new CRM_Core_DAO_Domain();
    for ($id = 2; $id <= self::NUM_DOMAIN; $id++) {
      // domain name is pretty simple. it is "Domain $id"
      $domain->name = "Domain $id";
      $domain->description = "Description $id";
      $domain->contact_name = $this->randomName();

      // insert domain
      $this->_insert($domain);
      // FIXME FOR NEW LOCATION BLOCK STRUCTURE
      // $this->_addLocation(self::MAIN, $id, true);
    }
  }

678 679 680
  /**
   * @return string
   */
totten's avatar
totten committed
681 682 683 684 685 686 687 688 689 690
  public function randomName() {
    $prefix      = $this->_getRandomIndex($this->prefix);
    $first_name  = ucfirst($this->_getRandomElement($this->firstName));
    $middle_name = ucfirst($this->_getRandomChar());
    $last_name   = ucfirst($this->_getRandomElement($this->lastName));
    $suffix      = $this->_getRandomIndex($this->suffix);

    return $this->prefix[$prefix] . " $first_name $middle_name $last_name " . $this->suffix[$suffix];
  }

691
  /**
totten's avatar
totten committed
692 693 694 695 696 697 698 699 700
   *
   * addContact()
   *
   * This method adds data to the contact table
   *
   * id - from $contact
   * contact_type 'Individual' 'Household' 'Organization'
   * preferred_communication (random 1 to 3)
   *
701
   */
totten's avatar
totten committed
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
  public function addContact() {

    // add contacts
    $contact = new CRM_Contact_DAO_Contact();

    for ($id = 1; $id <= self::NUM_CONTACT; $id++) {
      $contact->contact_type = $this->getContactType($id + 1);
      $contact->do_not_phone = mt_rand(0, 1);
      $contact->do_not_email = mt_rand(0, 1);
      $contact->do_not_post = mt_rand(0, 1);
      $contact->do_not_trade = mt_rand(0, 1);
      $contact->preferred_communication_method = $this->_getRandomElement($this->preferredCommunicationMethod);
      $this->_insert($contact);
    }
  }

718
  /**
totten's avatar
totten committed
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
   *
   * addIndividual()
   *
   * This method adds individual's data to the contact table
   *
   * The following fields are generated and added.
   *
   * contact_uuid - individual
   * contact_rid - latest one
   * first_name 'First Name $contact_uuid'
   * middle_name 'Middle Name $contact_uuid'
   * last_name 'Last Name $contact_uuid'
   * job_title 'Job Title $contact_uuid'
   * greeting_type - randomly select from the enum values
   * custom_greeting - "custom greeting $contact_uuid'
   *
735
   */
totten's avatar
totten committed
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
  public function addIndividual() {

    $contact = new CRM_Contact_DAO_Contact();

    for ($id = 1; $id <= $this->numIndividual; $id++) {
      $contact->first_name = ucfirst($this->_getRandomElement($this->firstName));
      $contact->middle_name = ucfirst($this->_getRandomChar());
      $contact->last_name = ucfirst($this->_getRandomElement($this->lastName));
      $contact->prefix_id = $this->_getRandomIndex($this->prefix);
      $contact->suffix_id = $this->_getRandomIndex($this->suffix);
      $contact->greeting_type_id = $this->_getRandomIndex($this->greetingType);
      $contact->gender_id = $this->_getRandomIndex($this->gender);
      $contact->birth_date = date("Ymd", mt_rand(0, time()));
      $contact->is_deceased = mt_rand(0, 1);

      $contact->id = $this->individual[($id - 1)];

      // also update the sort name for the contact id.
      $contact->display_name = trim($this->prefix[$contact->prefix_id] . " $contact->first_name $contact->middle_name $contact->last_name " . $this->suffix[$contact->suffix_id]);
      $contact->sort_name = $contact->last_name . ', ' . $contact->first_name;
      $contact->hash = crc32($contact->sort_name);
      $this->_update($contact);
    }
  }

761
  /**
totten's avatar
totten committed
762 763 764 765 766 767 768 769 770 771 772 773 774
   *
   * addHousehold()
   *
   * This method adds household's data to the contact table
   *
   * The following fields are generated and added.
   *
   * contact_uuid - household_individual
   * contact_rid - latest one
   * household_name 'household $contact_uuid primary contact $primary_contact_uuid'
   * nick_name 'nick $contact_uuid'
   * primary_contact_uuid = $household_individual[$contact_uuid][0];
   *
775
   */
totten's avatar
totten committed
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
  public function addHousehold() {

    $contact = new CRM_Contact_DAO_Contact();
    for ($id = 1; $id <= $this->numHousehold; $id++) {
      $cid = $this->household[($id - 1)];
      $contact->primary_contact_id = $this->householdIndividual[$cid][0];

      // get the last name of the primary contact id
      $individual = new CRM_Contact_DAO_Contact();
      $individual->id = $contact->primary_contact_id;
      $individual->find(TRUE);
      $firstName = $individual->first_name;
      $lastName = $individual->last_name;

      // need to name the household and nick name appropriately
      $contact->household_name = "$firstName $lastName" . "'s home";
      $contact->nick_name = "$lastName" . "'s home";

      $contact->id = $this->household[($id - 1)];
      // need to update the sort name for the main contact table
      $contact->display_name = $contact->sort_name = $contact->household_name;
      $contact->hash = crc32($contact->sort_name);
      $this->_update($contact);
    }
  }

802
  /**
totten's avatar
totten committed
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
   *
   * addOrganization()
   *
   * This method adds organization data to the contact table
   *
   * The following fields are generated and added.
   *
   * contact_uuid - organization
   * contact_rid - latest one
   * organization_name 'organization $contact_uuid'
   * legal_name 'legal  $contact_uuid'
   * nick_name 'nick $contact_uuid'
   * sic_code 'sic $contact_uuid'
   * primary_contact_id - random individual contact uuid
   *
818
   */
totten's avatar
totten committed
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
  public function addOrganization() {

    $contact = new CRM_Contact_DAO_Contact();

    for ($id = 1; $id <= $this->numOrganization; $id++) {
      $contact->id = $this->organization[($id - 1)];
      $name = $this->_getRandomElement($this->organization_name) . " " . $this->_getRandomElement($this->organization_field) . " " . $this->_getRandomElement($this->organization_type);
      $contact->organization_name = $name;
      $contact->primary_contact_id = $this->_getRandomElement($this->strict_individual);

      // need to update the sort name for the main contact table
      $contact->display_name = $contact->sort_name = $contact->organization_name;
      $contact->hash = crc32($contact->sort_name);
      $this->_update($contact);
    }
  }

836
  /**
totten's avatar
totten committed
837 838 839 840 841 842 843
   *
   * addRelationship()
   *
   * This method adds data to the contact_relationship table
   *
   * it adds the following fields
   *
844
   */
totten's avatar
totten committed
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
  public function addRelationship() {

    $relationship = new CRM_Contact_DAO_Relationship();

    // all active for now.
    $relationship->is_active = 1;

    foreach ($this->householdIndividual as $household_id => $household_member) {
      // add child_of relationship
      // 2 for each child
      $relationship->relationship_type_id = self::CHILD_OF;
      $relationship->contact_id_a = $household_member[2];
      $relationship->contact_id_b = $household_member[0];
      $this->_insert($relationship);
      $relationship->contact_id_a = $household_member[3];
      $relationship->contact_id_b = $household_member[0];
      $this->_insert($relationship);
      $relationship->contact_id_a = $household_member[2];
      $relationship->contact_id_b = $household_member[1];
      $this->_insert($relationship);
      $relationship->contact_id_a = $household_member[3];
      $relationship->contact_id_b = $household_member[1];
      $this->_insert($relationship);

      // add spouse_of relationship 1 for both the spouses
      $relationship->relationship_type_id = self::SPOUSE_OF;
      $relationship->contact_id_a = $household_member[1];
      $relationship->contact_id_b = $household_member[0];
      $this->_insert($relationship);

      // add sibling_of relationship 1 for both the siblings
      $relationship->relationship_type_id = self::SIBLING_OF;
      $relationship->contact_id_a = $household_member[3];
      $relationship->contact_id_b = $household_member[2];
      $this->_insert($relationship);

      // add head_of_household relationship 1 for head of house
      $relationship->relationship_type_id = self::HEAD_OF_HOUSEHOLD;
      $relationship->contact_id_a = $household_member[0];
      $relationship->contact_id_b = $household_id;
      $this->_insert($relationship);

      // add member_of_household relationship 3 for all other members
      $relationship->relationship_type_id = self::MEMBER_OF_HOUSEHOLD;
      $relationship->contact_id_a = $household_member[1];
      $this->_insert($relationship);
      $relationship->contact_id_a = $household_member[2];
      $this->_insert($relationship);
      $relationship->contact_id_a = $household_member[3];
      $this->_insert($relationship);
    }
  }

898
  /**
totten's avatar
totten committed
899 900 901 902 903
   *
   * addLocation()
   *
   * This method adds data to the location table
   *
904
   */
totten's avatar
totten committed
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
  public function addLocation() {
    // strict individuals
    foreach ($this->strictIndividual as $contactId) {
      $this->_addLocation(self::HOME, $contactId);
    }

    //household
    foreach ($this->household as $contactId) {
      $this->_addLocation(self::HOME, $contactId);
    }

    //organization
    foreach ($this->organization as $contactId) {
      $this->_addLocation(self::MAIN, $contactId);
    }

    // some individuals.
    $someIndividual = array_diff($this->individual, $this->strictIndividual);
923
    $someIndividual = array_slice($someIndividual, 0, (int) (75 * ($this->numIndividual - $this->numStrictIndividual) / 100));
totten's avatar
totten committed
924 925 926 927 928
    foreach ($someIndividual as $contactId) {
      $this->_addLocation(self::HOME, $contactId, FALSE, TRUE);
    }
  }

929 930 931 932 933 934
  /**
   * @param $locationTypeId
   * @param $contactId
   * @param bool $domain
   * @param bool $isPrimary
   */
totten's avatar
totten committed
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
  private function _addLocation($locationTypeId, $contactId, $domain = FALSE, $isPrimary = TRUE) {
    $this->_addAddress($locationTypeId, $contactId, $isPrimary);

    // add two phones for each location
    $this->_addPhone($locationTypeId, $contactId, '1', $isPrimary);
    $this->_addPhone($locationTypeId, $contactId, '2', FALSE);

    // need to get sort name to generate email id
    $contact = new CRM_Contact_DAO_Contact();
    $contact->id = $contactId;
    $contact->find(TRUE);
    // get the sort name of the contact
    $sortName = $contact->sort_name;
    if (!empty($sortName)) {
      // add 2 email for each location
      for ($emailId = 1; $emailId <= 2; $emailId++) {
        $this->_addEmail($locationTypeId, $contactId, $sortName, ($emailId == 1) && $isPrimary);
      }
    }
  }

956 957 958 959 960 961 962
  /**
   * @param $locationTypeId
   * @param $contactId
   * @param bool $isPrimary
   * @param null $locationBlockID
   * @param int $offset
   */
totten's avatar
totten committed
963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
  private function _addAddress($locationTypeId, $contactId, $isPrimary = FALSE, $locationBlockID = NULL, $offset = 1) {
    $addressDAO = new CRM_Core_DAO_Address();

    // add addresses now currently we are adding only 1 address for each location
    $addressDAO->location_type_id = $locationTypeId;
    $addressDAO->contact_id = $contactId;
    $addressDAO->is_primary = $isPrimary;

    $addressDAO->street_number = mt_rand(1, 1000);
    $addressDAO->street_number_suffix = ucfirst($this->_getRandomChar());
    $addressDAO->street_number_predirectional = $this->_getRandomElement($this->addressDirection);
    $addressDAO->street_name = ucwords($this->_getRandomElement($this->streetName));
    $addressDAO->street_type = $this->_getRandomElement($this->streetType);
    $addressDAO->street_number_postdirectional = $this->_getRandomElement($this->addressDirection);
    $addressDAO->street_address = $addressDAO->street_number_predirectional . " " . $addressDAO->street_number . $addressDAO->street_number_suffix . " " . $addressDAO->street_name . " " . $addressDAO->street_type . " " . $addressDAO->street_number_postdirectional;
    $addressDAO->supplemental_address_1 = ucwords($this->_getRandomElement($this->supplementalAddress1));

    // some more random skips
    // hack add lat / long for US based addresses
    list($addressDAO->country_id, $addressDAO->state_province_id, $addressDAO->city,
      $addressDAO->postal_code, $addressDAO->geo_code_1, $addressDAO->geo_code_2
    ) = self::getZipCodeInfo();

    //$addressDAO->county_id = 1;

    $this->_insert($addressDAO);
  }

991 992 993 994 995
  /**
   * @param $sortName
   *
   * @return mixed
   */
totten's avatar
totten committed
996 997 998 999 1000
  private function _sortNameToEmail($sortName) {
    $email = preg_replace("([^a-zA-Z0-9_-]*)", "", $sortName);
    return $email;
  }

1001 1002 1003 1004 1005 1006 1007 1008
  /**
   * @param $locationTypeId
   * @param $contactId
   * @param $phoneType
   * @param bool $isPrimary
   * @param null $locationBlockID
   * @param int $offset
   */
totten's avatar
totten committed
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
  private function _addPhone($locationTypeId, $contactId, $phoneType, $isPrimary = FALSE, $locationBlockID = NULL, $offset = 1) {
    if ($contactId % 3) {
      $phone = new CRM_Core_DAO_Phone();
      $phone->location_type_id = $locationTypeId;
      $phone->contact_id = $contactId;
      $phone->is_primary = $isPrimary;
      $phone->phone = mt_rand(11111111, 99999999);
      $phone->phone_type_id = $phoneType;
      $this->_insert($phone);
    }
  }

1021 1022 1023 1024 1025 1026 1027 1028
  /**
   * @param $locationTypeId
   * @param $contactId
   * @param $sortName
   * @param bool $isPrimary
   * @param null $locationBlockID
   * @param int $offset
   */
totten's avatar
totten committed
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
  private function _addEmail($locationTypeId, $contactId, $sortName, $isPrimary = FALSE, $locationBlockID = NULL, $offset = 1) {
    if ($contactId % 2) {
      $email = new CRM_Core_DAO_Email();
      $email->location_type_id = $locationTypeId;
      $email->contact_id = $contactId;
      $email->is_primary = $isPrimary;

      $emailName    = $this->_sortNameToEmail($sortName);
      $emailDomain  = $this->_getRandomElement($this->emailDomain);
      $tld          = $this->_getRandomElement($this->emailTLD);
      $email->email = strtolower($emailName . "@" . $emailDomain . "." . $tld);
      $this->_insert($email);
    }
  }

1044
  /**
totten's avatar
totten committed
1045 1046 1047 1048 1049
   *
   * addTagEntity()
   *
   * This method populates the crm_entity_tag table
   *
1050
   */
totten's avatar
totten committed
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
  public function addEntityTag() {

    $entity_tag = new CRM_Core_DAO_EntityTag();

    // add categories 1,2,3 for Organizations.
    for ($i = 0; $i < $this->numOrganization; $i += 2) {
      $org_id = $this->organization[$i];
      // echo "org_id = $org_id\n";
      $entity_tag->contact_id = $this->organization[$i];
      $entity_tag->tag_id = mt_rand(1, 3);
      $this->_insert($entity_tag);
    }

    // add categories 4,5 for Individuals.
    for ($i = 0; $i < $this->numIndividual; $i += 2) {
      $entity_tag->contact_id = $this->individual[$i];
      if (($entity_tag->contact_id) % 3) {
        $entity_tag->tag_id = mt_rand(4, 5);
        $this->_insert($entity_tag);
      }
      else {
        // some of the individuals are in both categories (4 and 5).
        $entity_tag->tag_id = 4;
        $this->_insert($entity_tag);
        $entity_tag->tag_id = 5;
        $this->_insert($entity_tag);
      }
    }
  }

1081
  /**
totten's avatar
totten committed
1082 1083 1084 1085 1086
   *
   * addGroup()
   *
   * This method populates the crm_entity_tag table
   *
1087
   */
totten's avatar
totten committed
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
  public function addGroup() {
    // add the 3 groups first
    $numGroup = count($this->group);
    require_once 'CRM/Contact/BAO/Group.php';
    for ($i = 0; $i < $numGroup; $i++) {
      $group             = new CRM_Contact_BAO_Group();
      $group->name       = $this->group[$i];
      $group->title      = $this->group[$i];
      $group->group_type = "12";
      $group->visibility = 'Public Pages';
      $group->is_active  = 1;
      $group->save();
      $group->buildClause();
      $group->save();
    }

    // 60 are for newsletter
    for ($i = 0; $i < 60; $i++) {
      $groupContact = new CRM_Contact_DAO_GroupContact();
      // newsletter subscribers
      $groupContact->group_id = 2;
      $groupContact->contact_id = $this->individual[$i];
      // membership status
      $groupContact->status = $this->_getRandomElement($this->groupMembershipStatus);

      $subscriptionHistory = new CRM_Contact_DAO_SubscriptionHistory();
      $subscriptionHistory->contact_id = $groupContact->contact_id;

      $subscriptionHistory->group_id = $groupContact->group_id;
      $subscriptionHistory->status = $groupContact->status;
      // method
      $subscriptionHistory->method = $this->_getRandomElement($this->subscriptionHistoryMethod);
      $subscriptionHistory->date = $this->_getRandomDate();
      if ($groupContact->status != 'Pending') {
        $this->_insert($groupContact);
      }
      $this->_insert($subscriptionHistory);
    }

    // 15 volunteers
    for ($i = 0; $i < 15; $i++) {
      $groupContact = new CRM_Contact_DAO_GroupContact();
      // Volunteers
      $groupContact->group_id = 3;
      $groupContact->contact_id = $this->individual[$i + 60];
      // membership status
      $groupContact->status = $this->_getRandomElement($this->groupMembershipStatus);

      $subscriptionHistory = new CRM_Contact_DAO_SubscriptionHistory();
      $subscriptionHistory->contact_id = $groupContact->contact_id;
      $subscriptionHistory->group_id = $groupContact->group_id;
      $subscriptionHistory->status = $groupContact->status;
      // method
      $subscriptionHistory->method = $this->_getRandomElement($this->subscriptionHistoryMethod);
      $subscriptionHistory->date = $this->_getRandomDate();

      if ($groupContact->status != 'Pending') {
        $this->_insert($groupContact);
      }
      $this->_insert($subscriptionHistory);
    }

    // 8 advisory board group
    for ($i = 0; $i < 8; $i++) {
      $groupContact = new CRM_Contact_DAO_GroupContact();
      // advisory board group
      $groupContact->group_id = 4;
      $groupContact->contact_id = $this->individual[$i * 7];
      // membership status
      $groupContact->status = $this->_getRandomElement($this->groupMembershipStatus);

      $subscriptionHistory = new CRM_Contact_DAO_SubscriptionHistory();
      $subscriptionHistory->contact_id = $groupContact->contact_id;
      $subscriptionHistory->group_id = $groupContact->group_id;
      $subscriptionHistory->status = $groupContact->status;
      // method
      $subscriptionHistory->method = $this->_getRandomElement($this->subscriptionHistoryMethod);
      $subscriptionHistory->date = $this->_getRandomDate();

      if ($groupContact->status != 'Pending') {
        $this->_insert($groupContact);
      }
      $this->_insert($subscriptionHistory);
    }

    //In this function when we add groups that time we are cache the contact fields
    //But at the end of setup we are appending sample custom data, so for consistency
    //reset the cache.
    require_once 'CRM/Core/BAO/Cache.php';
    CRM_Core_BAO_Cache::deleteGroup('contact fields');
  }

1180
  /**
totten's avatar
totten committed
1181 1182 1183 1184 1185
   *
   * addNote()
   *
   * This method populates the crm_note table
   *
1186
   */
totten's avatar
totten committed
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
  public function addNote() {

    $note               = new CRM_Core_DAO_Note();
    $note->entity_table = 'civicrm_contact';
    $note->contact_id   = 1;

    for ($i = 0; $i < self::NUM_CONTACT; $i++) {
      $note->entity_id = $this->contact[$i];
      if ($this->contact[$i] % 5 || $this->contact[$i] % 3 || $this->contact[$i] % 2) {
        $this->_insertNote($note);
      }
    }
  }

1201
  /**
totten's avatar
totten committed
1202 1203 1204 1205 1206
   *
   * addActivity()
   *
   * This method populates the crm_activity_history table
   *
1207
   */
totten's avatar
totten committed
1208 1209 1210 1211 1212 1213 1214 1215 1216
  public function addActivity() {
    $contactDAO = new CRM_Contact_DAO_Contact();
    $contactDAO->contact_type = 'Individual';
    $contactDAO->selectAdd();
    $contactDAO->selectAdd('id');
    $contactDAO->orderBy('sort_name');
    $contactDAO->find();

    $count = 0;
1217
    $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
totten's avatar
totten committed
1218 1219 1220 1221 1222 1223 1224 1225 1226

    while ($contactDAO->fetch()) {
      if ($count++ > 2) {
        break;
      }
      for ($i = 0; $i < self::NUM_ACTIVITY; $i++) {
        $activityDAO = new CRM_Activity_DAO_Activity();
        $activityDAO->source_contact_id = $contactDAO->id;
        $activityTypeID = mt_rand(7, 10);
yashodha's avatar
yashodha committed
1227

totten's avatar
totten committed
1228 1229 1230 1231 1232 1233 1234
        $activityDAO->activity_type_id = $activityTypeID;
        $activityDAO->subject = "Subject for $activity[$activityTypeID]";
        $activityDAO->activity_date_time = $this->_getRandomDate();
        $activityDAO->duration = mt_rand(1, 6);
        $activityDAO->status_id = 2;
        $this->_insert($activityDAO);

yashodha's avatar
yashodha committed
1235 1236 1237 1238 1239 1240
        $activityContactDAO = new CRM_Activity_DAO_ActivityContact();
        $activityContactDAO->activity_id = $activityDAO->id;
        $activityContactDAO->contact_id = mt_rand(1, 101);
        $activityContactDAO->record_type_id = CRM_Utils_Array::key('Activity Source', $activityContacts);
        $this->_insert($activityContactDAO);

totten's avatar
totten committed
1241
        if (in_array($activityTypeID, array(
1242 1243
          6, 9,
        ))) {
yashodha's avatar
yashodha committed
1244
          $activityTargetDAO = new CRM_Activity_DAO_ActivityContact();
totten's avatar
totten committed
1245
          $activityTargetDAO->activity_id = $activityDAO->id;
yashodha's avatar
yashodha committed
1246 1247
          $activityTargetDAO->contact_id = mt_rand(1, 101);
          $activityTargetDAO->record_type_id = CRM_Utils_Array::key('Activity Targets', $activityContacts);
totten's avatar
totten committed
1248 1249 1250 1251
          $this->_insert($activityTargetDAO);
        }

        if ($activityTypeID == 7) {
yashodha's avatar
yashodha committed
1252
          $activityAssignmentDAO = new CRM_Activity_DAO_ActivityContact();
totten's avatar
totten committed
1253
          $activityAssignmentDAO->activity_id = $activityDAO->id;
yashodha's avatar
yashodha committed
1254 1255
          $activityAssignmentDAO->contact_id = mt_rand(1, 101);
          $activityAssignmentDAO->record_type_id = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
totten's avatar
totten committed
1256 1257 1258 1259 1260 1261
          $this->_insert($activityAssignmentDAO);
        }
      }
    }
  }

1262 1263 1264
  /**
   * @return array
   */
1265
  public static function getZipCodeInfo() {
totten's avatar
totten committed
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
    $stateID = mt_rand(1000, 5132);
    $offset = mt_rand(1, 4132);

    $query = "SELECT id, country_id from civicrm_state_province LIMIT $offset, 1";
    $dao = new CRM_Core_DAO();
    $dao->query($query);
    while ($dao->fetch()) {
      return array($dao->country_id, $dao->id);
    }

    return array();
  }

1279 1280 1281 1282 1283
  /**
   * @param $zipCode
   *
   * @return array
   */
1284
  public static function getLatLong($zipCode) {
totten's avatar
totten committed
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
    $query = "http://maps.google.com/maps?q=$zipCode&output=js";
    $userAgent = "Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $query);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    // grab URL and pass it to the browser
    $outstr = curl_exec($ch);

    // close CURL resource, and free up system resources
    curl_close($ch);

    $preg = "/'(<\?xml.+?)',/s";
    preg_match($preg, $outstr, $matches);
    if ($matches[1]) {
      $xml = simplexml_load_string($matches[1]);
      $attributes = $xml->center->attributes();
      if (!empty($attributes)) {
        return array((float ) $attributes['lat'], (float ) $attributes['lng']);
      }
    }
    return array(NULL, NULL);
  }

1312
  public function addMembershipType() {
totten's avatar
totten committed
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
    $organizationDAO = new CRM_Contact_DAO_Contact();
    $organizationDAO->id = 5;
    $organizationDAO->find(TRUE);
    $contact_id = $organizationDAO->contact_id;

    $membershipType = "INSERT INTO civicrm_membership_type
        (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)
        VALUES
        ('General', 'Regular annual membership.', " . $contact_id . ", 3, 100, 'year', 1, 'rolling',null, null, 7, 'b_a', 'Public', 1, 1),
        ('Student', 'Discount membership for full-time students.', " . $contact_id . ", 1, 50, 'year', 1, 'rolling', null, null, 7, 'b_a', 'Public', 2, 1),
        ('Lifetime', 'Lifetime membership.', " . $contact_id . ", 2, 1200, 'lifetime', 1, 'rolling', null, null, 7, 'b_a', 'Admin', 3, 1);
        ";
    CRM_Core_DAO::executeQuery($membershipType, CRM_Core_DAO::$_nullArray);
  }

1328
  public function addMembership() {
totten's avatar
totten committed
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
    $contact = new CRM_Contact_DAO_Contact();
    $contact->query("SELECT id FROM civicrm_contact where contact_type = 'Individual'");
    while ($contact->fetch()) {
      $contacts[] = $contact->id;
    }
    shuffle($contacts);

    $randomContacts = array_slice($contacts, 0, 350);

    $sources             = array('Payment', 'Donation', 'Check');
    $membershipTypes     = array(2, 1);
    $membershipTypeNames = array('Student', 'General');
    $statuses            = array(3, 4);

eileen's avatar
eileen committed
1343 1344
    $membership = "
 INSERT INTO civicrm_membership
totten's avatar
totten committed
1345
        (contact_id, membership_type_id, join_date, start_date, end_date, source, status_id)
eileen's avatar
eileen committed
1346 1347
VALUES
 ";
totten's avatar
totten committed
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406
    $activity = "
INSERT INTO civicrm_activity
        (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)
VALUES
";

    foreach ($randomContacts as $count => $dontCare) {
      $source = self::_getRandomElement($sources);
      $acititySourceId = $count + 1;
      if ((($count + 1) % 11 == 0)) {
        // lifetime membership, status can be anything
        $startDate = date('Y-m-d', mktime(0, 0, 0, date('m'), (date('d') - $count), date('Y')));
        $membership .= "( {$randomContacts[$count]}, 3, '{$startDate}', '{$startDate}', null, '{$source}', 1)";
        $activity .= "( {$randomContacts[$count]}, {$acititySourceId}, 7, 'Lifetime', '{$startDate} 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 )";
      }
      elseif (($count + 1) % 5 == 0) {
        // Grace or expired, memberhsip type is random of 1 & 2
        $randId         = array_rand($membershipTypes);
        $membershipType = self::_getRandomElement($membershipTypes);
        $startDate      = date('Y-m-d', mktime(0, 0, 0,
            date('m'),
            (date('d') - ($count * ($randId + 1) * ($randId + 1) * ($randId + 1))),
            (date('Y') - ($randId + 1))
          ));
        $partOfDate = explode('-', $startDate);
        $endDate = date('Y-m-d', mktime(0, 0, 0,
            $partOfDate[1],
            ($partOfDate[2] - 1),
            ($partOfDate[0] + ($randId + 1))
          ));
        $membership .= "( {$randomContacts[$count]}, {$membershipType}, '{$startDate}', '{$startDate}', '{$endDate}', '{$source}', {$statuses[$randId]})";
        $activity .= "( {$randomContacts[$count]}, {$acititySourceId}, 7, '{$membershipTypeNames[$randId]}', '{$startDate} 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 )";
      }
      elseif (($count + 1) % 2 == 0) {
        // membership type 2
        $startDate = date('Y-m-d', mktime(0, 0, 0, date('m'), (date('d') - $count), date('Y')));
        $endDate = date('Y-m-d', mktime(0, 0, 0, date('m'), (date('d') - $count), (date('Y') + 1)));
        $membership .= "( {$randomContacts[$count]}, 2, '{$startDate}', '{$startDate}', '{$endDate}', '{$source}', 1)";
        $activity .= "( {$randomContacts[$count]}, {$acititySourceId}, 7, 'Student', '{$startDate} 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 )";
      }
      else {
        // membership type 1
        $startDate = date('Y-m-d', mktime(0, 0, 0, date('m'), (date('d') - $count), date('Y')));
        $endDate = date('Y-m-d', mktime(0, 0, 0, date('m'), (date('d') - $count), (date('Y') + 2)));
        $membership .= "( {$randomContacts[$count]}, 1, '{$startDate}', '{$startDate}', '{$endDate}', '{$source}', 1)";
        $activity .= "( {$randomContacts[$count]}, {$acititySourceId}, 7, 'General', '{$startDate} 00:00:00', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 2 )";
      }

      if ($count != 349) {
        $membership .= ",";
        $activity .= ",";
      }
    }

    CRM_Core_DAO::executeQuery($membership, CRM_Core_DAO::$_nullArray);

    CRM_Core_DAO::executeQuery($activity, CRM_Core_DAO::$_nullArray);
  }

1407 1408 1409 1410 1411
  /**
   * @param $date
   *
   * @return string
   */
1412
  public static function repairDate($date) {
totten's avatar
totten committed
1413 1414 1415 1416
    $dropArray = array('-' => '', ':' => '', ' ' => '');
    return strtr($date, $dropArray);
  }

1417
  public function addMembershipLog() {
totten's avatar
totten committed
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
    $membership = new CRM_Member_DAO_Membership();
    $membership->query("SELECT id FROM civicrm_membership");
    while ($membership->fetch()) {
      $ids[] = $membership->id;
    }
    require_once 'CRM/Member/DAO/MembershipLog.php';
    foreach ($ids as $id) {
      $membership     = new CRM_Member_DAO_Membership();
      $membership->id = $id;
      $membershipLog  = new CRM_Member_DAO_MembershipLog();
      if ($membership->find(TRUE)) {
        $membershipLog->membership_id = $membership->id;
        $membershipLog->status_id = $membership->status_id;
        $membershipLog->start_date = self::repairDate($membership->start_date);
        $membershipLog->end_date = self::repairDate($membership->end_date);
        $membershipLog->modified_id = $membership->contact_id;
        $membershipLog->modified_date = date("Ymd");
        $membershipLog->save();
      }
      $membershipLog = NULL;
    }
  }

1441
  public function createEvent() {
totten's avatar
totten committed
1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
    $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)
      VALUES
      ( 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),
      ( 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),
      ( 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)
      ";
    CRM_Core_DAO::executeQuery($event, CRM_Core_DAO::$_nullArray);

    $sql       = "SELECT id from civicrm_address where street_address = 'S 14S El Camino Way E'";
    $eventAdd1 = CRM_Core_DAO::singleValueQuery($sql, CRM_Core_DAO::$_nullArray);
    $sql       = "SELECT id from civicrm_address where street_address = 'E 11B Woodbridge Path SW'";
    $eventAdd2 = CRM_Core_DAO::singleValueQuery($sql, CRM_Core_DAO::$_nullArray);
    $sql       = "SELECT id from civicrm_address where street_address = 'E 581O Lincoln Dr SW'";
    $eventAdd3 = CRM_Core_DAO::singleValueQuery($sql, CRM_Core_DAO::$_nullArray);

    $event = "INSERT INTO civicrm_email (contact_id, location_type_id, email, is_primary, is_billing, on_hold, hold_date, reset_date)
       VALUES
       (NULL, 1, 'development@example.org', 0, 0, 0, NULL, NULL),
       (NULL, 1, 'tournaments@example.org', 0, 0, 0, NULL, NULL),
       (NULL, 1, 'celebration@example.org', 0, 0, 0, NULL, NULL)
       ";
    CRM_Core_DAO::executeQuery($event, CRM_Core_DAO::$_nullArray);

    $sql         = "SELECT id from civicrm_email where email = 'development@example.org'";
    $eventEmail1 = CRM_Core_DAO::singleValueQuery($sql, CRM_Core_DAO::$_nullArray);
    $sql         = "SELECT id from civicrm_email where email = 'tournaments@example.org'";
    $eventEmail2 = CRM_Core_DAO::singleValueQuery($sql, CRM_Core_DAO::$_nullArray);
    $sql         = "SELECT id from civicrm_email where email = 'celebration@example.org'";
    $eventEmail3 = CRM_Core_DAO::singleValueQuery($sql, CRM_Core_DAO::$_nullArray);

    $event = "INSERT INTO civicrm_phone (contact_id, location_type_id, is_primary, is_billing, mobile_provider_id, phone, phone_type_id)
       VALUES
       (NULL, 1, 0, 0, NULL,'204 222-1000', '1'),
       (NULL, 1, 0, 0, NULL,'204 223-1000', '1'),
       (NULL, 1, 0, 0, NULL,'303 323-1000', '1')
       ";
    CRM_Core_DAO::executeQuery($event, CRM_Core_DAO::$_nullArray);

    $sql         = "SELECT id from civicrm_phone where phone = '204 222-1000'";
    $eventPhone1 = CRM_Core_DAO::singleValueQuery($sql, CRM_Core_DAO::$_nullArray);
    $sql         = "SELECT id from civicrm_phone where phone = '204 223-1000'";
    $eventPhone2 = CRM_Core_DAO::singleValueQuery($sql, CRM_Core_DAO::$_nullArray);
    $sql         = "SELECT id from civicrm_phone where phone = '303 323-1000'";
    $eventPhone3 = CRM_Core_DAO::singleValueQuery($sql, CRM_Core_DAO::$_nullArray);

    $event = "INSERT INTO civicrm_loc_block ( address_id, email_id, phone_id, address_2_id, email_2_id, phone_2_id)
       VALUES
      ( $eventAdd1, $eventEmail1, $eventPhone1, NULL,NULL,NULL),
      ( $eventAdd2, $eventEmail2, $eventPhone2, NULL,NULL,NULL),
      ( $eventAdd3, $eventEmail3, $eventPhone3, NULL,NULL,NULL)
       ";

    CRM_Core_DAO::executeQuery($event, CRM_Core_DAO::$_nullArray);

    $sql       = "SELECT id from civicrm_loc_block where phone_id = $eventPhone1 AND email_id = $eventEmail1 AND address_id = $eventAdd1";
    $eventLok1 = CRM_Core_DAO::singleValueQuery($sql, CRM_Core_DAO::$_nullArray);
    $sql       = "SELECT id from civicrm_loc_block where phone_id = $eventPhone2 AND email_id = $eventEmail2 AND address_id = $eventAdd2";
    $eventLok2 = CRM_Core_DAO::singleValueQuery($sql, CRM_Core_DAO::$_nullArray);
    $sql       = "SELECT id from civicrm_loc_block where phone_id = $eventPhone3 AND email_id = $eventEmail3 AND address_id = $eventAdd3";
    $eventLok3 = CRM_Core_DAO::singleValueQuery($sql, CRM_Core_DAO::$_nullArray);

    //create event fees
    $optionGroup = "INSERT INTO civicrm_option_group ( name, is_reserved, is_active)
      VALUES
      ( 'civicrm_event.amount.1', 0, 1),
      ( 'civicrm_event.amount.2', 0, 1),
      ( 'civicrm_event.amount.3', 0, 1)
";
    CRM_Core_DAO::executeQuery($optionGroup, CRM_Core_DAO::$_nullArray);

    $sql = "SELECT max(id) from civicrm_option_group where name = 'civicrm_event.amount.1'";
    $page1 = CRM_Core_DAO::singleValueQuery($sql, CRM_Core_DAO::$_nullArray);

    $sql = "SELECT max(id) from civicrm_option_group where name = 'civicrm_event.amount.2'";
    $page2 = CRM_Core_DAO::singleValueQuery($sql, CRM_Core_DAO::$_nullArray);

    $sql = "SELECT max(id) from civicrm_option_group where name = 'civicrm_event.amount.3'";
    $page3 = CRM_Core_DAO::singleValueQuery($sql, CRM_Core_DAO::$_nullArray);

    $optionValue = "INSERT INTO civicrm_option_value (option_group_id, label, value, is_default, weight, is_optgroup, is_reserved, is_active)
      VALUES
      ($page1, 'Single', '50', 0, 1, 0, 0, 1),
      ($page1, 'Couple', '100', 0, 2, 0, 0, 1),
      ($page1, 'Family', '200', 0, 3, 0, 0, 1),
      ($page2, 'Bass', '25', 0, 1, 0, 0, 1),
      ($page2, 'Tenor', '40', 0, 2, 0, 0, 1),
      ($page2, 'Soprano', '50', 0, 3, 0, 0, 1),
      ($page3, 'Tiny-tots (ages 5-8)', '800', 0, 1, 0, 0, 1),
      ($page3, 'Junior Stars (ages 9-12)', '1000', 0, 2, 0, 0, 1),
      ($page3, 'Super Stars (ages 13-18)', '1500', 0, 3, 0, 0, 1)";

    CRM_Core_DAO::executeQuery($optionValue, CRM_Core_DAO::$_nullArray);

    $sql = "SELECT max(id) FROM civicrm_option_value WHERE civicrm_option_value.option_group_id = $page1 AND civicrm_option_value.weight=2";
    $defaultFee1 = CRM_Core_DAO::singleValueQuery($sql, CRM_Core_DAO::$_nullArray);

    $sql = "SELECT max(id) FROM civicrm_option_value WHERE civicrm_option_value.option_group_id = $page2 AND civicrm_option_value.weight=2";
    $defaultFee2 = CRM_Core_DAO::singleValueQuery($sql, CRM_Core_DAO::$_nullArray);

    $sql = "SELECT max(id) FROM civicrm_option_value WHERE civicrm_option_value.option_group_id = $page3 AND civicrm_option_value.weight=2";
    $defaultFee3 = CRM_Core_DAO::singleValueQuery($sql, CRM_Core_DAO::$_nullArray);

    $event = "INSERT INTO civicrm_event
        ( 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 )
        VALUES
1547 1548 1549
        ( '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 ),
        ( '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 ),
        ( '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 )
totten's avatar
totten committed
1550 1551 1552 1553
         ";
    CRM_Core_DAO::executeQuery($event, CRM_Core_DAO::$_nullArray);
  }

1554
  public function addParticipant() {
totten's avatar
totten committed
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
    // add participant
    $participant = new CRM_Event_DAO_Participant();

    for ($id = 1; $id <= self::NUM_PARTICIPANT; $id++) {
      $participant->contact_id = mt_rand(1, self::NUM_CONTACT);
      $participant->event_id = mt_rand(1, 3);
      $participant->status_id = mt_rand(1, 5);
      $participant->role_id = mt_rand(1, 4);
      $participant->register_date = $this->_getRandomDate();
      $participant->source = "Credit Card";

      if ($participant->event_id == 1) {
        $fee_level = "Single";
        $fee_amount = 50;
      }
      elseif ($participant->event_id == 2) {
        $fee_level = "Soprano";
        $fee_amount = 50;
      }
      else {
        $fee_level = "Tiny-tots (ages 5-8)";
        $fee_amount = 800;
      }
      $participant->fee_level = $fee_level;
      $participant->fee_amount = $fee_amount;
      $participant->is_test = 0;

      $this->_insert($participant);
    }
  }

1586
  public function addPCP() {
totten's avatar
totten committed
1587 1588 1589 1590
    $query = "
INSERT INTO `civicrm_pcp`
    (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)
VALUES
1591
    ({$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);
totten's avatar
totten committed
1592
";
eileen's avatar
eileen committed
1593
    CRM_Core_DAO::executeQuery($query);
totten's avatar
totten committed
1594 1595
  }

1596
  public function addContribution() {
totten's avatar
totten committed
1597 1598 1599 1600 1601
    // add contributions
    $contribution = new CRM_Contribute_DAO_Contribution();

    for ($id = 1; $id <= self::NUM_CONTRIBUTION; $id++) {
      $contribution->contact_id = mt_rand(1, self::NUM_CONTACT);
1602
      $contribution->financial_type_id   = mt_rand(1, 4);
totten's avatar
totten committed
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
      $contribution->contribution_page_id = mt_rand(1, 3);
      $contribution->payment_instrument_id = mt_rand(1, 5);
      $contribution->receive_date = $this->_getRandomDate();
      $contribution->total_amount = mt_rand(10, 99);
      $contribution->contribution_status_id = mt_rand(1, 6);
      $contribution->trxn_id = "#" . md5($contribution->receive_date);
      $this->_insert($contribution);
    }
  }

1613
  public function addSoftContribution() {
totten's avatar
totten committed
1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640
    $pcpRollNickNAme = array('Jones Family', 'Annie and the kids', 'Anonymous', 'Adam Family');

    $pcpPersonalNote = array('Helping Hands', 'Annie Helps', 'Anonymous', 'Adam Helps');

    $softContribution = new CRM_Contribute_DAO_ContributionSoft();

    $sql = "SELECT DISTINCT(contact_id), id, total_amount from civicrm_contribution LIMIT 200";

    $contriInfo = CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);

    $prevContactID = NULL;

    while ($contriInfo->fetch()) {
      if ($prevContactID) {
        $softContribution->contribution_id = $contriInfo->id;
        $softContribution->contact_id = $prevContactID;
        $softContribution->amount = $contriInfo->total_amount;
        $softContribution->pcp_id = 1;
        $softContribution->pcp_display_in_roll = 1;
        $softContribution->pcp_roll_nickname = $this->_getRandomElement($pcpRollNickNAme);
        $softContribution->pcp_personal_note = $this->_getRandomElement($pcpPersonalNote);
        $this->_insert($softContribution);
      }
      $prevContactID = $contriInfo->contact_id;
    }
  }

1641
  public function addPledge() {
totten's avatar
totten committed
1642
    $pledge = "INSERT INTO civicrm_pledge
eileen's avatar
eileen committed
1643 1644
        (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)
        VALUES
totten's avatar
totten committed
1645 1646 1647 1648 1649 1650 1651
       (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),
       (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),
       (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);
";
    CRM_Core_DAO::executeQuery($pledge, CRM_Core_DAO::$_nullArray);
  }

1652
  public function addPledgePayment() {
eileen's avatar
eileen committed
1653 1654 1655
    $pledgePayment = "INSERT INTO civicrm_pledge_payment
        ( pledge_id, contribution_id, scheduled_amount, scheduled_date, reminder_date, reminder_count, status_id)
       VALUES
totten's avatar
totten committed
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
         (1, 10, 500.00, '2010-07-01 13:03:45', null, 0, 1),
         (2, 11, 200.00, '2010-07-01 10:59:35', null, 0, 1),
         (2, null, 200.00, '2010-10-01 10:59:35',null, 0, 2),
         (2, null, 200.00, '2010-01-01 10:59:35',null, 0, 2),
         (2, null, 200.00, '2010-04-01 10:59:35',null, 0, 2),
         (3, 12, 200.00, '2010-06-01 11:00:12', null, 0, 1),
         (3, 13, 200.00, '2010-07-01 10:59:35', '2010-06-28 10:59:41', 1, 1),
         (3, null, 200.00, '2010-08-01 11:00:12', null, 0, 2);
        ";
    CRM_Core_DAO::executeQuery($pledgePayment, CRM_Core_DAO::$_nullArray);
  }

1668
  public function addMembershipPayment() {
totten's avatar
totten committed
1669 1670 1671 1672 1673
    $amount = array('50', '100', '1200');

    $contribution = new CRM_Contribute_DAO_Contribution();
    for ($id = 1; $id <= 200; $id++) {
      $contribution->contact_id = mt_rand(1, self::NUM_CONTACT);
1674
      $contribution->financial_type_id   = mt_rand(1, 4);
totten's avatar
totten committed
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684
      $contribution->payment_instrument_id = mt_rand(1, 5);
      $contribution->receive_date = $this->_getRandomDate();
      $contribution->total_amount = $this->_getRandomElement($amount);
      $contribution->contribution_status_id = mt_rand(1, 6);
      $contribution->trxn_id = "#" . md5($contribution->receive_date);
      $this->_insert($contribution);
    }
    for ($i = 0; $i < 3; $i++) {
      $contributionsArray = $membershipArray = array();
      $contributionSQL = "
eileen's avatar
eileen committed
1685
            SELECT  id
totten's avatar
totten committed
1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
                FROM    civicrm_contribution
                WHERE   contribution_page_id IS NULL AND
                        total_amount = {$amount[$i]} limit 0, 50 ";

      $contributionDAO = CRM_Core_DAO::executeQuery($contributionSQL, CRM_Core_DAO::$_nullArray);

      while ($contributionDAO->fetch()) {
        $contributionsArray[] = $contributionDAO->id;
      }
      $j = $i + 1;
      $membershipSQL = "
eileen's avatar
eileen committed
1697 1698
            SELECT  id
                FROM  civicrm_membership
totten's avatar
totten committed
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713
                WHERE civicrm_membership.membership_type_id = {$j} limit 0, 50";
      $membershipDAO = CRM_Core_DAO::executeQuery($membershipSQL, CRM_Core_DAO::$_nullArray);

      while ($membershipDAO->fetch()) {
        $membershipArray[] = $membershipDAO->id;
      }

      $payemntOBJ = new CRM_Member_DAO_MembershipPayment();
      foreach ($membershipArray as $key => $membershipid) {
        $payemntOBJ->contribution_id = $contributionsArray[$key];
        $payemntOBJ->membership_id = $membershipid;
        $this->_insert($payemntOBJ);
      }
    }
  }
1714

totten's avatar
totten committed
1715 1716
}

1717 1718 1719 1720 1721
/**
 * @param null $str
 *
 * @return bool
 */
totten's avatar
totten committed
1722 1723 1724 1725
function user_access($str = NULL) {
  return TRUE;
}

1726 1727 1728
/**
 * @return array
 */
totten's avatar
totten committed
1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759
function module_list() {
  return array();
}

echo ("Starting data generation on " . date("F dS h:i:s A") . "\n");
$obj1 = new CRM_GCD();
$obj1->initID();
$obj1->parseDataFile();
$obj1->initDB();
$obj1->addDomain();
$obj1->addContact();
$obj1->addIndividual();
$obj1->addHousehold();
$obj1->addOrganization();
$obj1->addRelationship();
$obj1->addLocation();
$obj1->addEntityTag();
$obj1->addGroup();
$obj1->addNote();
$obj1->addActivity();
$obj1->addMembership();
$obj1->addMembershipLog();
$obj1->createEvent();
$obj1->addParticipant();
$obj1->addContribution();
$obj1->addPCP();
$obj1->addSoftContribution();
$obj1->addPledge();
$obj1->addPledgePayment();
$obj1->addMembershipPayment();
echo ("Ending data generation on " . date("F dS h:i:s A") . "\n");