CoolFace
Apppublic

kenken999/php

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
searchsuggest.php187 linesDownload Raw Back to public
1<?php2@ini_set("display_errors","1");3@ini_set("display_startup_errors","1");4 5require_once("include/dbcommon.php");6require_once getabspath('classes/searchclause.php');7add_nocache_headers();8 9$shortTable = postvalue("table");10$table = GetTableByShort( $shortTable );11if( !$table )12	exit(0);13 14$page = postvalue('page');15 16$pageType = postvalue('pageType');17if ( !$pageType )  18	$pageType = PAGE_LIST;19 20$searchField = postvalue('searchField');21 22if( $searchField )23{24	if( !Security::userHasFieldPermissions( $table, $searchField, $pageType, $page, true ) )25		return;	26}27 28	29$searchFor = trim( postvalue('searchFor') );30// if nothing to search 31if($searchFor == '')32{33	echo printJSON( array('success' => true, 'result' => '') ); 34	return;35}36 37require_once getabspath('classes/controls/EditControlsContainer.php');38 39$searchOpt = postvalue("start") ? "Starts with" : "Contains";40$searchField = GoodFieldName( $searchField );41$numberOfSuggests = GetGlobalData("searchSuggestsNumber", 10);42$whereClauses = array();43 44 45$forLookupPage = postvalue('forLookup');46 47$forDashboardSimpleSearch = !$searchField && $pageType == PAGE_DASHBOARD ;48if( $forDashboardSimpleSearch ) 49{50	$dashSettings = new ProjectSettings( $table, PAGE_DASHBOARD, $page );51	$dashGoogleLikeFields = $dashSettings->getGoogleLikeFields();52	$dashSearchFields = $dashSettings->getDashboardSearchFields();53 54	$sfdata = array();55	// group fields by table56	foreach( $dashGoogleLikeFields as $g ) 57	{58		foreach( $dashSearchFields[ $g ] as $j => $data )59		{60			$sfdata[ $data['table'] ][] = $data['field'];61		}		62	}63	64	$result = array();65	66	foreach( $sfdata as $table => $fields )67	{68		if ( $numberOfSuggests <= count( $result ) ) 69			break;70	71		if ( !$result ) 72			$result = array();73	74		$_result = getListOfSuggests( $fields, $table, $numberOfSuggests - count( $result ) , $searchOpt, $searchFor );75		76		// add only distinct values77		foreach( $_result as $_data ) 78		{79			$found = false;80			foreach( $result as $data ) 81			{82				if( $data['realValue'] == $_data['realValue'] )83				{84					$found = true;85					break;86				}87			}88		89			if( !$found )90				$result[] = $_data;91		}92	}93	94	echo printJSON( array( 'success' => true, 'result' => $result ) );95	exit();96}97 98 99if( $pageType == PAGE_DASHBOARD )100{101	$dashSettings = new ProjectSettings( $table, PAGE_DASHBOARD, $page );102	$dashSearchFields = $dashSettings->getDashboardSearchFields();	103 104	$sfData = $dashSearchFields[ $searchField ][0];105	$searchField =  $sfData['field'];106	$table = $sfData['table'];107	108	foreach( $dashSettings->getDashboardElements() as $elem ) 109	{110		if( $elem['table'] == $table ) 111		{112			$pageType = PAGE_LIST;113			114			if ( $elem['type'] == DASHBOARD_CHART ) 115				$pageType = PAGE_CHART;116			else if( $elem['type'] == DASHBOARD_REPORT )117				$pageType = PAGE_REPORT;118			119			break;120		}121	}122}123 124 125$pSetList = new ProjectSettings( $table, $pageType, $page );126if( $searchField == "" ) {127	$allSearchFields = $pSetList->getGoogleLikeFields();128}129else {130	// array of fields which were added in wizard for search131	$allSearchFields = $pSetList->getAllPageFields();132}133 134 135$pSet = new ProjectSettings( $table, PAGE_SEARCH, $page );136$cipherer = new RunnerCipherer( $table );137 138$conditions = array();139 140//	master-details141$masterTable = postvalue("mastertable");142$detailKeys = array();143if( $masterTable != "" )144{	145	$masterKetsReq = RunnerPage::readMasterKeysFromRequest();146	$detailKeys = $pSet->getDetailKeysByMasterTable( $masterTable );147 148	for($j = 0; $j < count($detailKeys); $j++)149	{150		$conditions[] = DataCondition::FieldEquals( $detailKeys[$j], $masterKetsReq[ $j + 1 ] );151	}152}153 154//	suggest within filtered data 155$searchClauseObj = SearchClause::getSearchObject( $table, "", $table, $cipherer );156$conditions[] = $searchClauseObj->getFilterCondition( $pSet );157 158//	apply dependent driodown filter159$parentCtrlsData = my_json_decode( postvalue('parentCtrlsData') );160if( $forLookupPage && $parentCtrlsData ) 161{162	require_once( getabspath( "classes/controls/Control.php" ) );163	require_once( getabspath( "classes/controls/LookupField.php" ) );164 165	$mainField = postvalue("mainField");166	$mainTable = postvalue("mainTable");167	$mainPageType = postvalue("mainPageType");168	$mainPSet = new ProjectSettings( $mainTable, $mainPageType );169			170	$parentWhereParts = array();171	foreach( $mainPSet->getParentFieldsData( $mainField ) as $cData )172	{173		if( !isset( $parentCtrlsData[ $cData["main"] ] ) )174			continue;175		$conditions[] = LookupField::categoryCondition( $mainPSet, $cData["main"], $cData["lookup"], $parentCtrlsData[ $cData["main"] ] );176	}177 178}179 180$result = getListOfSuggests( $allSearchFields, $table, $numberOfSuggests, $searchOpt, $searchFor, $searchField, $detailKeys, $conditions );181 182$returnJSON = array();183$returnJSON['success'] = true;184$returnJSON['result'] = $result;185echo printJSON($returnJSON);186exit();187?>