public function delete(Doctrine_Connection $conn = null, $updateList = true) { if ($conn == null) { $conn = $this->_table->getConnection(); } try { $conn->beginTransaction(); $ret = parent::delete($conn); if ($updateList) { $this->LsList->last_user_id = LsVersionableListener::getUserId(); $this->LsList->updated_at = date('Y-m-d H:i:s'); $this->LsList->save(); } $conn->commit(); } catch (Exception $e) { $conn->rollback(); throw $e; } return $ret; }
public function logCustomFieldChange($key, $oldValue, $newValue, $modificationId = null) { $object = $this->getInvoker(); if (!($id = $object->id)) { throw Exception("Can't log custom field changes for object without id"); } $model = get_class($object); $db = Doctrine_Manager::connection(); try { $db->beginTransaction(); if (!$modificationId) { //insert modification $userId = LsVersionableListener::getUserId(); $time = LsDate::getCurrentDateTime(); $sql = 'INSERT INTO modification (object_model, object_id, user_id, created_at, updated_at) ' . 'VALUES (?, ?, ?, ?, ?)'; $stmt = $db->execute($sql, array($model, $id, $userId, $time, $time)); $modificationId = $db->lastInsertId('modification'); } //insert modification field $sql = 'INSERT INTO modification_field (modification_id, field_name, old_value, new_value) VALUES (?, ?, ?, ?)'; $stmt = $db->execute($sql, array($modificationId, $key, $oldValue, $newValue)); $db->commit(); } catch (Exception $e) { $db->rollback(); throw $e; } }
static function mergeAll(Entity $e1, Entity $e2) { $db = Doctrine_Manager::connection(); try { $db->beginTransaction(); //MERGE ENTITIES AND EXTENSIONS $e1 = self::mergeBasic($e1, $e2); //MERGE CONTACT INFO //phones are moved to e1 foreach ($e2->Phone as $phone) { if ($e1->id) { $q = LsDoctrineQuery::create()->from('Phone p')->where('p.entity_id = ? AND p.number = ?', array($e1->id, $phone->number)); //not if e1 already has a phone with same number if ($q->count()) { continue; } } $phone->entity_id = $e1['id']; $phone->setMerge(true); $phone->save(); } //emails are moved to e1 foreach ($e2->Email as $email) { if ($e1->id) { $q = LsDoctrineQuery::create()->from('Email e')->where('e.entity_id = ? AND e.address = ?', array($e1->id, $email->address)); //not if e1 already has the same email if ($q->count()) { continue; } } $email->entity_id = $e1['id']; $email->setMerge(true); $email->save(); } //addresses foreach ($e2->Address as $address) { if ($e1->id) { if ($address->latitude && $address->longitude) { //not if e1 already has an address with the same coords if (AddressTable::getByCoordsQuery($address->longitude, $address->latitude, $e1)->count()) { continue; } } } $address->entity_id = $e1['id']; $address->setMerge(true); $address->save(); } //RELATIONSHIPS if ($e2->id) { //where e2 is first foreach ($e2->getRelationshipsQuery(1)->execute() as $rel) { //don't try to merge relationships, there isn't a good way to do this automatically $rel->entity1_id = $e1['id']; $rel->setMerge(true); $rel->save(); } //where e2 is second foreach ($e2->getRelationshipsQuery(2)->execute() as $rel) { //don't try to merge relationships, there isn't a good way to do this automatically $rel->entity2_id = $e1['id']; $rel->setMerge(true); $rel->save(); } } //LISTS //lists are moved to e1 foreach ($e2->LsListEntity as $listEntity) { if ($e1->id) { $q = LsQuery::getByModelAndFieldsQuery('LsListEntity', array('entity_id' => $e1->id, 'list_id' => $listEntity->list_id)); //not if e1 is already on the list if ($q->count()) { continue; } } $listEntity->entity_id = $e1; $listEntity->setMerge(true); $listEntity->save(); } //IMAGES //images are moved to e1 foreach ($e2->Image as $image) { $image->entity_id = $e1; $image->setMerge(true); $image->save(); } //ALIASES //aliases are moved to e1 foreach ($e2->Alias as $alias) { //not if e1 already has that alias if ($e1->id && $e1->hasName($alias->name, $matchContext = true, $alias->context)) { continue; } $alias->entity_id = $e1; $alias->is_primary = false; $alias->setMerge(true); $alias->save(); } //save now so that template merges work $e1->last_user_id = LsVersionableListener::getUserId(); $e1->updated_at = LsDate::getCurrentDateTime(); $e1->setMerge(true); $e1->save(); //set merged_id for e2 $e2->merged_id = $e1['id']; //TEMPLATES foreach ($e2->_table->getTemplates() as $template) { //templates must have mergeFrom method if (method_exists($template, 'mergeFrom')) { $template->setInvoker($e1); $template->mergeFrom($e2); } } //OTHER RELATIONS foreach ($e2->Child as $child) { $child->parent_id = $e1->id; $child->setMerge(true); $child->save(); } foreach ($e2->PartyMember as $member) { $member->party_id = $e1->id; $member->setMerge(true); $member->save(); } foreach ($e2->BusinessIndustry as $bi) { $q = LsDoctrineQuery::create()->from('BusinessIndustry bi')->where('bi.business_id = ? AND bi.industry_id = ?', array($e1->id, $bi->industry_id)); if (!$q->count()) { $bi->business_id = $e1->id; $bi->save(); } } foreach ($e2->OverseenPosition as $position) { $position->boss_id = $e1->id; $position->save(); } foreach ($e2->BundledDonation as $donation) { $donation->bundler_id = $e1->id; $donation->save(); } foreach ($e2->LobbyFilingLobbyist as $lfl) { $lfl->lobbyist_id = $e1->id; $lfl->save(); } foreach ($e2->Contact1Transaction as $t) { $t->contact1_id = $e1->id; $t->save(); } foreach ($e2->Contact2Transaction as $t) { $t->contact2_id = $e1->id; $t->save(); } //NOTES sfLoader::loadHelpers('Url'); $notes = LsDoctrineQuery::create()->from('Note n')->where('n.entity_ids LIKE ?', '%,' . $e2->id . ',%')->execute(); foreach ($notes as $note) { $note->body = preg_replace('#/(person|org)/' . $e2->id . '/([^"]+)#i', '/\\1/' . $e1->id . '/' . LsSlug::convertNameToSlug($e1->name), $note->body); $note->body_raw = preg_replace('#@entity:' . $e2->id . '(\\[([^\\]]+)\\])?#', '@entity:' . $e1->id . '\\1', $note->body_raw); $note->entity_ids = str_replace(',' . $e2->id . ',', ',' . $e1->id . ',', $note->entity_ids); $note->save(); } //DONATION MATCHES //get matches for e2 if ($e2->id != '') { $matches2 = Doctrine::getTable('OsEntityTransaction')->findByEntityId($e2->id); //get matches for e1 $matches1 = Doctrine::getTable('OsEntityTransaction')->findByEntityId($e1->id); $matches1Sorted = array(); //sort matches for e1 foreach ($matches1 as $match1) { $matches1Sorted[$match1['cycle']][$match1['transaction_id']] = $match1; } //loop through e2 matches foreach ($matches2 as $match2) { //if e1 matched exists, update as necessary if ($match1 = @$matches1Sorted[$match2['cycle']][$match2['transaction_id']]) { $save = false; if (!$match1['is_verified'] && $match2['is_verified']) { $match1->is_verified = true; $save = true; } if (!$match1['is_processed'] && $match2['is_processed']) { $match1->is_processed = true; $save = true; } if (!$match1['is_synced'] && $matche2['is_synced']) { $match1->is_synced = true; $save = true; } if ($save) { $match1->save(); } $match2->delete(); } else { //if e1 match doesn't exist, update the entity_id of the e2 match $match2->entity_id = $e1->id; $match2->save(); } } //DONATION MATCHING PREPROCESS LOG $cycles2 = Doctrine::getTable('OsEntityPreprocess')->findByEntityId($e2->id); $cycles1 = OsEntityPreprocessTable::getCyclesByEntityId($e1->id); foreach ($cycles2 as $cycle2) { //if e1 already preprocessed for this cycle, delete if (in_array($cycle2['cycle'], $cycles1)) { $cycle2->delete(); } else { //if not, update entity_id $cycle2->entity_id = $e1->id; $cycle2->save(); } } } //EXTERNAL KEYS foreach ($e2->ExternalKey as $key) { $q = LsDoctrineQuery::create()->from('ExternalKey k')->where('k.entity_id = ?', $e1->id)->andWhere('k.domain_id = ?', $key['domain_id']); if (!($existingKey = $q->fetchOne())) { $key->entity_id = $e1->id; $key->save(); } } $db->commit(); } catch (Exception $e) { $db->rollback(); throw $e; } $e2->clearRelated(); return $e1; }
public function updateEntitiesTimestamp() { if (!$this->entity1_id || !$this->entity2_id) { throw new Exception("Can't update timestamps for entities; entities aren't set"); } $db = Doctrine_Manager::connection(); $sql = 'UPDATE entity e SET e.last_user_id = ?, e.updated_at = ? WHERE e.id IN (?, ?)'; $params = array(LsVersionableListener::getUserId(), date('Y-m-d H:i:s'), $this->entity1_id, $this->entity2_id); $stmt = $db->execute($sql, $params); }
static function logMerge(Doctrine_Record $r) { $mod = new Modification(); $mod->object_model = get_class($r); $mod->object_id = $r->id; $mod->object_name = $r->getName(); $mod->user_id = LsVersionableListener::getUserId(); $mod->is_merge = true; $mod->save(); }
public function mergeFrom(Doctrine_Record $r) { $object = $this->getInvoker(); if (!$r->exists() || !$object->exists()) { return false; } //create delete record for merged entity LsVersionableListener::logDelete($r, $object['id']); return true; }