public function executeOnePercentSearch($request) { $this->search_form = new OnePercentSearchForm(); if ($request->isMethod('post')) { $searchParams = $request->getParameter('onepercent'); $url = $searchParams['url']; $text = $searchParams['text']; $this->search_form->bind($searchParams); if (!$this->search_form->isValid()) { return; } if ($url) { $browser = new sfWebBrowser(); if (!$browser->get($url)->responseIsError()) { $text = $browser->getResponseText(); } } if ($text) { $html = stripos($text, "<html"); $entity_types = 'people'; $names = array(); if ($html !== false) { $names = LsTextAnalysis::getHtmlEntityNames($text, $entity_types); } else { $names = LsTextAnalysis::getTextEntityNames($text, $entity_types); } $this->matches = array(); foreach ($names as $name) { $name_terms = $name; $name_parts = preg_split('/\\s+/', $name); if (count($name_parts) > 1) { $name_terms = PersonTable::nameSearch($name); } $pager = EntityTable::getSphinxPager($terms, $page = 1, $num = 20, $listIds = null, $aliases = true, $primary_ext = "Person"); $match['name'] = $name; $match['search_results'] = $pager->execute(); $this->matches[] = $match; } } } }
public function executeFindConnections() { $request = $this->getRequest(); if ($id2 = $request->getParameter('id2')) { if (!($this->entity2 = EntityApi::get($id2))) { $this->forward404(); } $page = $this->page; $num = $request->getParameter('num', 10); $options = array('cat_ids' => $request->getParameter('cat_ids', '1')); //get all chains $chains = SearchApi::getEntitiesChains($this->entity['id'], $id2, $options); $offset = ($page - 1) * $num; $flat_chains = array(); foreach ($chains as $degree => $ary) { foreach ($ary as $ids) { $flat_chains[] = $ids; } } $page_chains = array_slice($flat_chains, $offset, $num); $full_chains = array(); foreach ($page_chains as $ids) { $full = array(); $chain = SearchApi::buildRelationshipChain($ids, explode(',', $options['cat_ids'])); foreach ($chain as $id => $rels) { $entity = EntityApi::get($id); $entity['Relationships'] = count($rels) ? BatchApi::getRelationships($rels, array()) : array(); $full[] = $entity; } $full_chains[] = $full; } // foreach ($page_chains as $degree => $ary) // { // foreach ($ary as $ids) // { // if ($count == $page) // { // $chain = SearchApi::buildRelationshipChain($ids, explode(',', $options['cat_ids'])); // break 2; // } // $count++; // } // } // count total number of chains // $total = 0; // foreach ($chains as $degree => $ary) // { // $total += count($ary); // } if ($total = count($flat_chains)) { $chainAry = array_fill(0, $total, null); array_splice($chainAry, $offset, $num, $full_chains); $this->chain_pager = new LsDoctrinePager($chainAry, $page, $num); } // get entities for chain // if ($chain) // { // $this->entities = array(); // foreach ($chain as $id => $rels) // { // $entity = EntityApi::get($id); // $entity['Relationships'] = count($rels) ? BatchApi::getRelationships($rels, array()) : array(); // $this->entities[] = $entity; // } // $chainAry = array_fill(0, $total, null); // $chainAry[$page-1] = $this->entities; // $this->chain_pager = new LsDoctrinePager($chainAry, $page, $num); // } } else { //form submission, display matching persons if ($request->hasParameter('q')) { $num = $request->getParameter('num', 10); $page = $request->getParameter('page', 1); if (!($terms = $request->getParameter('q'))) { $this->entity_pager = new LsDoctrinePager(array(), $page, $num); } else { switch (sfConfig::get('app_search_engine')) { case 'sphinx': $this->entity_pager = EntityTable::getSphinxPager($terms, $page, $num); break; case 'lucene': $ary = EntityTable::getLuceneArray($terms, null); $this->entity_pager = new LsDoctrinePager($ary, $page, $num); break; case 'mysql': default: $terms = explode(' ', $terms); $q = EntityTable::getSimpleSearchQuery($terms); $this->entity_pager = new Doctrine_Pager($q, $page, $num); break; } } } } }
public function executeSimple($request) { $page = $request->getParameter('page', 1); $num = $request->getParameter('num', 20); $terms = $request->getParameter('q', ''); //networks to search $networkIds = (array) $request->getParameter('network_ids'); //only show network options if user has home network other than United States if (count($networkIds)) { $this->networks = LsDoctrineQuery::create()->from('LsList l')->whereIn('l.id', $networkIds)->fetchArray(); } else { $networkIds = null; } if ($this->getUser()->hasCredential('admin') || !sfConfig::get('app_blacklist_search_enabled')) { $this->results_pager = EntityTable::getSphinxPager($terms, $page, $num, $networkIds); } else { $blacklistIds = (array) sfConfig::get('app_blacklist_search_ids'); $entities = array(); $result = EntityTable::getSphinxHits($terms, $page, $num, $networkIds); if ($result['total_found'] > 0 && isset($result['matches'])) { $ids = array_diff(array_keys($result['matches']), $blacklistIds); if (count($ids) > 0) { $db = Doctrine_Manager::connection(); $sql = 'SELECT e.*, FIELD(e.id, ' . implode(',', $ids) . ') AS field ' . 'FROM entity e WHERE e.id IN (' . implode(',', $ids) . ') AND e.is_deleted = 0 ' . 'ORDER BY field'; $stmt = $db->execute($sql); $entities = $stmt->fetchAll(PDO::FETCH_ASSOC); } } $pager = new LsDoctrinePager($entities, $page, $num); $pager->setNumResults(count($ids)); $pager->isSubsetWithCount(true); $this->results_pager = $pager; } //search lists if (strlen($terms) > 2) { $this->lists = LsListTable::getListsFromSphinxSearch($terms, $this->getUser()->hasCredential('admin')); // $db = Doctrine_Manager::connection(); // $sql = 'SELECT l.* FROM ls_list l WHERE l.name LIKE ? AND l.is_deleted = 0 AND l.is_network = 0 ' . // ($this->getUser()->hasCredential('admin') ? '' : 'AND l.is_admin = 0 ') . // 'ORDER BY l.name ASC'; // $stmt = $db->execute($sql, array('%' . $terms . '%')); // $this->lists = $stmt->fetchAll(PDO::FETCH_ASSOC); } //search groups if (strlen($terms) > 2) { $db = Doctrine_Manager::connection(); if (sfConfig::get('app_rails_enabled')) { $sql = 'SELECT g.* FROM groups g WHERE (g.name LIKE ? OR g.tagline LIKE ? OR g.description LIKE ? OR g.slug LIKE ?) AND g.is_private = 0 ORDER BY g.name ASC'; $stmt = $db->execute($sql, array('%' . $terms . '%', '%' . $terms . '%', '%' . $terms . '%', '%' . $terms . '%')); } else { $sql = 'SELECT g.* FROM sf_guard_group g WHERE (g.display_name LIKE ? OR g.name LIKE ? OR g.blurb LIKE ?) AND g.is_working = 1 AND g.is_private = 0 ORDER BY g.display_name ASC'; $stmt = $db->execute($sql, array('%' . $terms . '%', '%' . $terms . '%', '%' . $terms . '%')); } $this->groups = $stmt->fetchAll(PDO::FETCH_ASSOC); } //search campaigns if (strlen($terms) > 2 && sfConfig::get('app_rails_enabled')) { $db = Doctrine_Manager::connection(); $sql = 'SELECT c.* FROM campaigns c WHERE (c.name LIKE ? OR c.tagline LIKE ? OR c.description LIKE ? OR c.slug LIKE ?) ORDER BY c.name ASC'; $stmt = $db->execute($sql, array('%' . $terms . '%', '%' . $terms . '%', '%' . $terms . '%', '%' . $terms . '%')); $this->campaigns = $stmt->fetchAll(PDO::FETCH_ASSOC); } }
public function executeConnectTo($request) { $this->checkList($request); $entityId = $request->getParameter('entity_id'); $num = $request->getParameter('num', 40); $page = $request->getParameter('page', 1); if ($entityId && ($this->entity = Doctrine::getTable('Entity')->find($entityId))) { $db = Doctrine_Manager::connection(); $q = LsDoctrineQuery::create()->select('e.*, GROUP_CONCAT(DISTINCT l.relationship_id) as relationship_ids')->from('Entity e')->leftJoin('e.LsListEntity le')->leftJoin('e.Link l')->where('le.list_id = ?', $this->list['id'])->andWhere('l.entity2_id = ?', $entityId)->groupBy('l.entity1_id'); if ($catId = $request->getParameter('cat_id')) { $q->andWhere('l.category_id = ?', $catId); } if ($isCurrent = $request->getParameter('is_current')) { $q->andWhere('l.is_current = ?', $isCurrent); } if ($primaryExt = $request->getParameter('primary_ext')) { $q->andWhere('e.primary_ext = ?', $primaryExt); } if ($matches = $q->fetchArray()) { $this->matches_pager = new LsDoctrinePager($matches, $page, $num); } } else { //form submission, display matching persons if ($request->hasParameter('q')) { if (!($terms = $request->getParameter('q'))) { $this->entity_pager = new LsDoctrinePager(array(), $page, $num); } else { $this->entity_pager = EntityTable::getSphinxPager($terms, $page, $num); } } } }
public function executeSearchEntities($request) { $this->setResponseFormat(); $num = $request->getParameter('num', 10); if ($terms = $request->getParameter('q')) { $entities = EntityTable::getSphinxPager($terms, 1, $num)->execute(); $entity_ids = array_map(function ($e) { return $e["id"]; }, $entities); $this->entities = EntityTable::getEntitiesForMap($entity_ids); } else { $this->entities = array(); } return $this->renderText(json_encode($this->entities)); }
public function executeToolbarSearch($request) { $this->checkToolbarCredentials(); if (!$request->isMethod('post')) { $this->forward404(); } else { $num = $request->getParameter('num', 7); $this->page = $request->getParameter('page', 1); $this->position = $request->getParameter('position'); if (!($terms = $request->getParameter('q'))) { $this->entities = array(); } else { $pager = EntityTable::getSphinxPager($terms, $this->page, $num); $this->entities = $pager->execute(); $this->total = $pager->getNumResults(); } } }
public function executeAddBulk($request) { $this->checkEntity($request, false, false); $this->reference_form = new ReferenceForm(); $this->reference_form->setSelectObject($this->entity); $this->add_bulk_form = new AddBulkForm(); //get possible default categories $this->categories = LsDoctrineQuery::create()->select('c.name, c.name')->from('RelationshipCategory c')->orderBy('c.id')->fetchAll(PDO::FETCH_KEY_PAIR); array_unshift($this->categories, ''); if ($request->isMethod('post') && in_array($request->getParameter('commit'), array('Begin', 'Continue'))) { if ($request->hasParameter('ref_id')) { $this->ref_id = $request->getParameter('ref_id'); } else { $refParams = $request->getParameter('reference'); $this->reference_form->bind($refParams); $restOfParams = (array) $request->getParameterHolder(); $restOfParams = array_shift($restOfParams); $this->add_bulk_form->bind($restOfParams, $request->getFiles()); if (!$this->reference_form->isValid() || !$this->add_bulk_form->isValid()) { return; } if ($this->ref_id = $refParams['existing_source']) { $ref = Doctrine::getTable('Reference')->find($this->ref_id); $url = $ref->source; } else { $ref = new Reference(); $ref->object_model = 'Entity'; $ref->object_id = $this->entity->id; $ref->source = $refParams['source']; $ref->name = $refParams['name']; $ref->source_detail = $refParams['source_detail']; $ref->publication_date = $refParams['publication_date']; $ref->save(); } $this->ref_id = $ref->id; $this->reference = $ref; } $verify_method = $request->getParameter('verify_method'); if ($this->add_method = $request->getParameter('add_method')) { if ($this->add_method == 'scrape') { //scrape ref url //set names to confirm $browser = new sfWebBrowser(); $entity_types = $request->getParameter('entity_types'); //FIND NAMES AT URL USING COMBO OF OPENCALAIS & LS CUSTOM HTML PARSING if (!$browser->get($ref->source)->responseIsError()) { $text = $browser->getResponseText(); $this->names = LsTextAnalysis::getHtmlEntityNames($text, $entity_types); $text = LsHtml::findParagraphs($text); $this->text = preg_replace('/<[^b][^>]*>/is', " ", $text); $this->confirm_names = true; return; } else { $request->setError('csv', 'problems finding names at that url'); } } else { if ($this->add_method == 'upload') { $file = $this->add_bulk_form->getValue('file'); $filename = 'uploaded_' . sha1($file->getOriginalName()); $extension = $file->getExtension($file->getOriginalExtension()); $filePath = sfConfig::get('sf_temp_dir') . '/' . $filename . $extension; $file->save($filePath); if ($filePath) { if ($spreadsheetArr = LsSpreadsheet::parse($filePath)) { $names = $spreadsheetArr['rows']; if (!in_array('name', $spreadsheetArr['headers'])) { $request->setError('file', 'The file you uploaded could not be parsed properly because there is no "name" column.'); return; } if (in_array('summary', $spreadsheetArr['headers'])) { foreach ($names as &$name) { $name['summary'] = str_replace(array('?', "'"), "'", $name['summary']); $name['summary'] = str_replace(array('?', '?', '"'), '"', $name['summary']); if (isset($name['title'])) { $name['description1'] = $name['title']; } } unset($name); } } else { $request->setError('file', 'The file you uploaded could not be parsed properly.'); return; } } else { $request->setError('file', 'You need to upload a file.'); return; } } else { if ($this->add_method == 'summary') { //parse summary for names $this->text = $this->entity->summary; $entity_types = $request->getParameter('entity_types'); $this->names = LsTextAnalysis::getTextEntityNames($this->text, $entity_types); $this->confirm_names = true; return; } else { if ($this->add_method == 'text') { $manual_names = $request->getParameter('manual_names'); if ($manual_names && $manual_names != "") { $manual_names = preg_split('#[\\r\\n]+#', $manual_names); $manual_names = array_map('trim', $manual_names); $names = array(); foreach ($manual_names as $name) { $names[] = array('name' => $name); } } else { $request->setError('csv', 'You did not add names properly.'); return; } } else { if ($this->add_method == 'db_search') { $this->db_search = true; } } } } } } //intermediate scrape page -- takes confirmed names, builds names arr if ($confirmed_names = $request->getParameter('confirmed_names')) { $restOfParams = (array) $request->getParameterHolder(); $restOfParams = array_shift($restOfParams); $this->add_bulk_form->bind($restOfParams, $request->getFiles()); if (!$this->add_bulk_form->isValid()) { $this->reference = Doctrine::getTable('reference')->find($this->ref_id); $this->names = unserialize(stripslashes($request->getParameter('names'))); $this->confirm_names = true; return; } $names = array(); foreach ($confirmed_names as $cn) { $names[] = array('name' => $cn); } $manual_names = $request->getParameter('manual_names'); if ($manual_names && $manual_names != "") { $manual_names = preg_split('#[\\r\\n]+#', $manual_names); $manual_names = array_map('trim', $manual_names); foreach ($manual_names as $name) { $names[] = array('name' => $name); } } } // LOAD IN RELATIONSHIP DEFAULTS if (isset($verify_method)) { $defaults = $request->getParameter('relationship'); if ($verify_method == 'enmasse') { $this->default_type = $request->getParameter('default_type'); $this->order = $request->getParameter('order'); $category_name = $request->getParameter('relationship_category_all'); $this->extensions = ExtensionDefinitionTable::getByTier(2, $this->default_type); $extensions_arr = array(); foreach ($this->extensions as $ext) { $extensions_arr[] = $ext->name; } } else { $category_name = $request->getParameter('relationship_category_one'); } if ($category_name) { $this->category_name = $category_name; if (!($category = Doctrine::getTable('RelationshipCategory')->findOneByName($category_name))) { $request->setError('csv', 'You did not select a relationship category.'); return; } $formClass = $category_name . 'Form'; $categoryForm = new $formClass(new Relationship()); $categoryForm->setDefaults($defaults); $this->form_schema = $categoryForm->getFormFieldSchema(); if (in_array($category_name, array('Position', 'Education', 'Membership', 'Donation', 'Lobbying', 'Ownership'))) { $this->field_names = array('description1', 'start_date', 'end_date', 'is_current'); } else { $this->field_names = array('description1', 'description2', 'start_date', 'end_date', 'is_current'); } $extraFields = array('Position' => array('is_board', 'is_executive'), 'Education' => array('degree_id'), 'Donation' => array('amount'), 'Transaction' => array('amount'), 'Lobbying' => array('amount'), 'Ownership' => array('percent_stake', 'shares')); if (isset($extraFields[$category_name])) { $this->field_names = array_merge($this->field_names, $extraFields[$category_name]); } } $this->matches = array(); // BOOT TO TOOLBAR OR LOOK FOR MATCHES FOR ENMASSE ADD if (isset($names) && count($names) > 0 || isset($this->db_search)) { if ($verify_method == 'onebyone') { if (isset($category_name)) { $defaults['category'] = $category_name; } $toolbar_names = array(); foreach ($names as $name) { $toolbar_names[] = $name['name']; } $this->getUser()->setAttribute('toolbar_names', $toolbar_names); $this->getUser()->setAttribute('toolbar_entity', $this->entity->id); $this->getUser()->setAttribute('toolbar_defaults', $defaults); $this->getUser()->setAttribute('toolbar_ref', $this->ref_id); $this->redirect('relationship/toolbar'); } else { $this->category_name = $category_name; if (isset($this->db_search)) { $num = $request->getParameter('num', 10); $page = $request->getParameter('page', 1); $q = LsDoctrineQuery::create()->from('Entity e')->where('(e.summary rlike ? or e.blurb rlike ?)', array('[[:<:]]' . $this->entity->name . '[[:>:]]', '[[:<:]]' . $this->entity->name . '[[:>:]]')); foreach ($this->entity->Alias as $alias) { $q->orWhere('(e.summary rlike ? or e.blurb rlike ?)', array('[[:<:]]' . $alias->name . '[[:>:]]', '[[:<:]]' . $alias->name . '[[:>:]]')); } $q->setHydrationMode(Doctrine::HYDRATE_ARRAY); $cat_id = constant('RelationshipTable::' . strtoupper($category_name) . '_CATEGORY'); $q->whereParenWrap(); $q->andWhere('NOT EXISTS (SELECT DISTINCT l.relationship_id FROM Link l ' . 'WHERE l.entity1_id = e.id AND l.entity2_id = ? AND l.category_id = ?)', array($this->entity['id'], $cat_id)); $summary_matches = $q->execute(); foreach ($summary_matches as $summary_match) { $aliases = array(); foreach ($this->entity->Alias as $alias) { $aliases[] = LsString::escapeStringForRegex($alias->name); } $aliases = implode("|", $aliases); $summary_match['summary'] = preg_replace('/(' . $aliases . ')/is', '<strong>$1</strong>', $summary_match['summary']); $this->matches[] = array('search_results' => array($summary_match)); } } else { for ($i = 0; $i < count($names); $i++) { if (isset($names[$i]['name']) && trim($names[$i]['name']) != '') { $name = $names[$i]['name']; $name_terms = $name; if ($this->default_type == 'Person') { $name_parts = preg_split('/\\s+/', $name); if (count($name_parts) > 1) { $name_terms = PersonTable::nameSearch($name); } $terms = $name_terms; $primary_ext = "Person"; } else { if ($this->default_type == 'Org') { $name_terms = OrgTable::nameSearch($name); $terms = $name_terms; $primary_ext = "Org"; } else { $terms = $name_terms; $primary_ext = null; } } $pager = EntityTable::getSphinxPager($terms, $page = 1, $num = 20, $listIds = null, $aliases = true, $primary_ext); $match = $names[$i]; $match['search_results'] = $pager->execute(); if (isset($names[$i]['types'])) { $types = explode(',', $names[$i]['types']); $types = array_map('trim', $types); $match['types'] = array(); foreach ($types as $type) { if (in_array($type, $extensions_arr)) { $match['types'][] = $type; } } } $this->matches[] = $match; } } } } } } } else { if ($page = $this->getRequestParameter('page')) { $this->page = $page; $this->num = $this->getRequestParameter('num', 50); } else { if ($request->isMethod('post') && $request->getParameter('commit') == 'Submit') { $this->ref_id = $this->getRequestParameter('ref_id'); $entity_ids = array(); $relationship_category = $this->getRequestParameter('category_name'); $order = $this->getRequestParameter('order'); $default_type = $request->getParameter('default_type'); $default_ref = Doctrine::getTable('Reference')->find($request->getParameter('ref_id')); for ($i = 0; $i < $this->getRequestParameter('count'); $i++) { if ($entity_id = $request->getParameter('entity_' . $i)) { $selected_entity_id = null; $relParams = $request->getParameter("relationship_" . $i); if ($relParams['ref_name']) { $ref['source'] = $relParams['ref_source']; $ref['name'] = $relParams['ref_name']; } if ($entity_id == 'new') { $name = $request->getParameter('new_name_' . $i); if ($default_type == 'Person') { $new_entity = PersonTable::parseFlatName($name); } else { $new_entity = new Entity(); $new_entity->addExtension('Org'); $new_entity->name = trim($name); } $new_entity->save(); $new_entity->blurb = $request->getParameter('new_blurb_' . $i); $new_entity->summary = $request->getParameter('new_summary_' . $i); if (!$ref) { $ref = $default_ref; } $new_entity->addReference($ref['source'], null, null, $ref['name']); if ($types = $request->getParameter('new_extensions_' . $i)) { foreach ($types as $type) { $new_entity->addExtension($type); } } $new_entity->save(); $selected_entity_id = $new_entity->id; } else { if ($entity_id > 0) { $selected_entity_id = $entity_id; LsCache::clearEntityCacheById($selected_entity_id); } } if ($selected_entity_id) { $startDate = $relParams['start_date']; $endDate = $relParams['end_date']; unset($relParams['start_date'], $relParams['end_date'], $relParams['ref_name'], $relParams['ref_url']); $rel = new Relationship(); $rel->setCategory($relationship_category); if ($order == '1') { $rel->entity1_id = $this->entity['id']; $rel->entity2_id = $selected_entity_id; } else { $rel->entity2_id = $this->entity['id']; $rel->entity1_id = $selected_entity_id; } //only set dates if valid if ($startDate && preg_match('#^\\d{4}-\\d{2}-\\d{2}$#', Dateable::convertForDb($startDate))) { $rel->start_date = Dateable::convertForDb($startDate); } if ($endDate && preg_match('#^\\d{4}-\\d{2}-\\d{2}$#', Dateable::convertForDb($endDate))) { $rel->end_date = Dateable::convertForDb($endDate); } $rel->fromArray($relParams, null, $hydrateCategory = true); if ($request->hasParameter('add_method') && $request->getParameter('add_method') == 'db_search') { $refs = EntityTable::getSummaryReferences($selected_entity_id); if (count($refs)) { $ref = $refs[0]; } else { $refs = EntityTable::getAllReferencesById($selected_entity_id); if (count($refs)) { $ref = $refs[0]; } } } if (!$ref) { $ref = $default_ref; } $rel->saveWithRequiredReference(array('source' => $ref['source'], 'name' => $ref['name'])); $ref = null; } } } $this->clearCache($this->entity); $this->redirect($this->entity->getInternalUrl()); } else { if ($request->isMethod('post') && $request->getParameter('commit') == 'Cancel') { $this->redirect($this->entity->getInternalUrl()); } } } } }