function parseRecipients($contribution) { $recipients = $this->getRecipients($contribution); $this->printDebug(" Number of recipients " . count($recipients)); foreach ($recipients as $recipient) { $candidate = $this->getCandidateInfo($recipient[0]); $committee = $this->getCommitteeInfo($recipient[0]); $committee_name = trim($committee[2]); $committee_fec_id = trim($committee[1]); //CHECK FOR EXISTING COMMITTEE unset($current_committee); $current_committee = EntityTable::getByExtensionQuery(array('Org', 'PoliticalFundraising'))->addWhere("org.name = ?", $committee_name)->fetchOne(); if ($current_committee) { $this->printDebug(" Found Committee " . $committee_name . " (" . $committee_fec_id . ")"); } else { //clear cache Doctrine::getTable('ExtensionDefinition')->clear(); $current_committee = new Entity(); $current_committee->addExtension('Org'); $current_committee->addExtension('PoliticalFundraising'); $current_committee->name = LsLanguage::titleize($committee_name); $current_committee->fec_id = $committee_fec_id; $current_committee->save(); $current_committee->addReference($source = $this->fecCommitteeUrl . $committee_fec_id, $excerpt = null, $fields = array('name', 'fec_id'), $name = 'FEC Disclosure Report', $detail = null, $date = null, false); $this->printDebug(" Adding new committee: " . $committee_name . " (" . $committee_fec_id . ")"); } $this->committee = $current_committee; $this->updateCommitteeDetails($current_committee); $transactions = $this->getTransactions($recipient[0]); //RECORD DONATIONS $validate_existance_of_donation = true; foreach ($transactions as $transaction) { list($month, $day, $year) = explode('/', $transaction[1]); $donation_amount = $transaction[2]; $donation_fec_id = $transaction[4]; $donation_date = $year . '-' . $month . '-' . $day; if ($this->hasMeta($this->person->id, $donation_fec_id) && !$this->forceScaper) { $this->printDebug("#{$donation_fec_id} Already scraped"); continue; } if ($validate_existance_of_donation) { $donation_exists = FecFilingTable::getFecFiling($donation_fec_id); $validate_existance_of_donation = false; } if (!$donation_exists) { $this->printDebug(" Donation exists: FALSE "); $this->printDebug(" Donation ({$donation_fec_id}): " . $donation_amount . " on " . $donation_date); $this->printDebug(" Creating relationship between \"" . $this->person->name_first . " " . $this->person->name_last . "\" and \"" . $current_committee->name . "\""); $filing = new FecFiling(); $filing->amount = $donation_amount; $filing->fec_filing_id = $donation_fec_id; $filing->start_date = $donation_date; $filing->end_date = $donation_date; $relationship = null; if ($relationship = $this->person->getRelationshipsWithQuery($current_committee, RelationshipTable::DONATION_CATEGORY)->fetchOne()) { $relationship->addFecFiling($filing); } else { $relationship = new Relationship(); $relationship->Entity1 = $this->person; $relationship->Entity2 = $current_committee; $relationship->setCategory('Donation'); $relationship->description1 = 'Campaign Contribution'; $relationship->is_current = 1; $relationship->save(); $relationship->addFecFiling($filing); $relationship->addReference($source = self::$fecImageUrl . $donation_fec_id, $excerpt = null, $fields = array('amount', 'start_date', 'end_date', 'description1'), $name = 'FEC Filing', $detail = null, $date = null); $filing->save(); $relationship->addReference($source = $this->_url, $excerpt = null, $fields = array('amount', 'start_date', 'end_date', 'description1'), $name = 'FEC contribution search', $detail = null); if ($this->_entity_reference == false) { $this->person->addReference($source = $this->_url, $excerpt = null, $fields = null, $name = 'FEC contribution search'); $this->_entity_reference = true; } } $this->saveMeta($this->person->id, $donation_fec_id, 1); } else { $this->printDebug(" Donation exists: TRUE"); break; } } } $this->printDebug("+ Adding Donation: COMPLETE\n"); }
static function consolidateRelationshipsByEntities($entity1Id, $entity2Id) { $rels = LsDoctrineQuery::create()->from('Relationship r')->leftJoin('r.FecFiling f')->leftJoin('r.Reference ref ON (ref.object_model = \'Relationship\' AND ref.object_id = r.id)')->where('r.entity1_id = ? AND r.entity2_id = ?', array($entity1Id, $entity2Id))->andWhere('r.category_id = ?', RelationshipTable::DONATION_CATEGORY)->andWhere('f.id IS NOT NULL')->execute()->getData(); //can't consolidate if no relationships if (!count($rels)) { return; } //keep array for relationships to delete $toDelete = array(); //separate relationships with crp data from those without $crpRels = array(); $otherRels = array(); foreach ($rels as $rel) { $isCrp = false; foreach ($rel->FecFiling as $filing) { if ($filing['crp_cycle'] && $filing['crp_id']) { $isCrp = true; break; } } if ($isCrp) { $crpRels[] = $rel; } else { $otherRels[] = $rel; } } //if there are relationships with crp data, delete the others (and their filings and references) if (count($crpRels)) { foreach ($otherRels as $rel) { foreach ($rel->FecFiling as $filing) { $toDelete[] = $filing; } foreach ($rel->Reference as $ref) { $toDelete[] = $ref; } $toDelete[] = $rel; } $rels = $crpRels; } //identify base relationship $baseRel = array_shift($rels); $baseRelFilingCount = count($baseRel->FecFiling->getData()); //organize base relationship filings $baseFilings = array(); $updateBaseRel = false; foreach ($baseRel->FecFiling as $filing) { $key = FecFilingTable::generateUniqueKey($filing); if (!isset($baseFilings[$key])) { $baseFilings[$key] = $filing; } else { $toDelete[] = $filing; $updateBaseRel = true; } } //transfer unique filings from remaining relationships foreach ($rels as $rel) { foreach ($rel->FecFiling as $filing) { $key = FecFilingTable::generateUniqueKey($filing); if (!isset($baseFilings[$key])) { $baseFilings[$key] = $filing; $updateBaseRel = true; } else { $toDelete[] = $filing; } } } //change relationship_id of transfered filings foreach ($baseFilings as $filing) { if ($filing['relationship_id'] != $baseRel['id']) { $filing['relationship_id'] = $baseRel['id']; $filing->save(); } } //change object_id of references foreach ($rels as $rel) { foreach ($rel->Reference as $ref) { $ref['object_id'] = $baseRel['id']; $ref->save(); } $toDelete[] = $rel; } //delete duplicates foreach ($toDelete as $record) { $record->delete(); } //update base relationship based on new filings, if there are new filings if ($updateBaseRel) { $baseRel->updateFromFecFilings(); } return $baseRel; }