Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

Code Block
languagejavascript
titleui.js
isc.FilterBuilder.create({
    "ID": "advancedFilter",
    "dataSource": "supplyItem",
    "topOperator": "and"
});

The ListGrid also requires additional code to add the FilterBuilder. This will require adding a vertical layout (VStack), together with the grid and the button needed to add for applying the filter on the ListGrid. Also going to add a horizontal layout (HStack) which will contain the two already existing buttons used for saving all data and creating a new record:

Code Block
languagejavascript
titleui.js
isc.ListGrid.create({
    "ID": "supplyItemGridsupplyItem",
    "width": 700, "height": 224, "alternateRecordStyles": true,
    "dataSource": supplyItem,
    "autoFetchData":true,
    "dataPageSize":20,
    "canEdit":true,
    "canRemoveRecords":true,
    "autoSaveEdits": false
});
isc.IButton.create({
    "ID": "filterButton",
    "title": "Filter",
    "click": function () {
        supplyItemGridsupplyItem.filterData(advancedFilter.getCriteria());
    }
});
isc.HStack.create({
    "membersMargin": 10,
    "ID": "gridButtons",
    "members": [
        isc.IButton.create({
            "top": 250,
            "title": "Edit New",
            "click": "supplyItemGridsupplyItem.startEditingNew()"
        }),
        isc.IButton.create({
            "top": 250,
            "left": 100,
            "title": "Save all",
            "click": "supplyItemGridsupplyItem.saveAllEdits()"
        })
    ]
});
isc.VStack.create({
    "membersMargin": 10,
    "members": [advancedFilter, filterButton, supplyItemGridsupplyItem, gridButtons]
});

Also note, the filter has been removed top of the grid, as it is being replaced with the FilterBuilder.

...

Refactor the existing fetch() method and move out the existing criteria building code into a  separate method called buildStandardCriteria():method:

Code Block
languagephp
titleDataSource.php
private function fetch($request)
{               
	// get the DataSource
	$ds = $this->data_source;   
	$tbl_name = $ds['ID'];   
	// check the advanced cretira
	if(empty($request->advancedCriteria)) 
	{
		$query_result = $this->buildStandardCriteria($request); 
	} else
	{
		$query_result = $this->buildAdvancedCriteria($request);                
	}
	
	$query = "select * from $tbl_name s ";
	
	$query .= $query_result['query'];
   
	// sort by
	if( !empty($request->sortBy) )
	{
		// set the orderBy
		$query .= " order by ";
		
		// we start to build a coma separated list of items. First item won't have coma
		// but every possible next will do
		$seperator = "";
		foreach($request->sortBy as $index => $sort)
		{ 
			
			// if column name is with -, then ordering is descending, otherwise ascending
			if ( strpos($index, '-') === 0 )
			{
				$query .= $seperator . $sort . " ASC";
			} else
			{
				$query .= $seperator . substr($sort, 1) . " DESC";
			}
			$separator  = ',';
		}
	}
	
	//DSResponse
	$response = new DSResponse();                 
	$products = R::getAll($query, $query_result['value']);   
	// get the count                    
	$count = count($products);           
	$response->setData($products);
	$response->setStartRow($request->startRow);
	$response->setEndRow($request->endRow);            
	$response->setTotalRows($count);
	$response->setStatus(0);  
	
	// sanity check, if no rows, return 0
	if ($response->getEndRow() < 0 ) 
	{
		$response->setEndRow(0);
	}
	
	return $response;
 
}
 

}

Note a new method called buildAdvancedCriteria() is required which builds the query criteria from the AdvancedCriteria delivered by the FilterBuilder:

Code Block
languagephp
titleRPCManager.php
private function buildStandardCriteriabuildAdvancedCriteria($request)
{
	$query$advancedCriteria = ''$request->getAdvancedCriteria();
	if(!empty($advancedCriteria))
	{
		$query$criteria_countquery = '';$this->buildCriterion($advancedCriteria);
	
	if$return (= countarray($request->getDataKeys()) != 0 )
	{
	   $query .= ' where ';      ;
		$return['query'] = ' where '.$criteria_query['query'];
		$return['value'] = $criteria_query['value'];
		return $return;
		
	}
	return null;          		 
		foreach($request->data as
$key => $value)
		} 

For parsing the tree itself, introduce a recursive function:

Code Block
languagephp
private function buildCriterion($advancedCriteria)
{               
	$criterias = $advancedCriteria['criteria'];
	$operator = $advancedCriteria['operator'];      			// get the field    
	$result = '';
		$field$query = '';
	$value = $this->getFieldarray($key);
	
		if(!empty($fieldforeach($criterias as $c)
	{
		if(isset($c['fieldName']))                
			$fn =                      
			{$c['fieldName'];
		
		if(isset($c['operator']))
				$type$op = $field$c['typeoperator'];
				
		
		if(isset( $type == "text" ||$c['value']))
		{      
					$typeif ($c['value'] === "link" ||TRUE )
					$type$val == "enum" ||'1';
					$typeelse if($c['value'] === "image" ||
FALSE)
					$type$val == "ntext" )'0';
				{else
					$query$val .= "s." . $key . " like " . " '%" . $value . "%' and " ;$c['value'];                    
  
				} else 				{ 					"s." . $key . "=" . " '" . $value . "' and " ;  
		
		if(isset($c['start']))    
				}
			}
								
			$query_count .= "s." . $key . " like " . "'%" . $value . "%' and " ;            
		}
		
		// remove 'and' of the query$start = $c['start'];
		
		if(isset($c['end']))    
			$end = $c['end'];
		
		if(isset($c['criteria']))    
			$criteria = $c['criteria'];
		else
			$criteria = null;
		
		if(empty($criteria))
		{
			switch($op)
			{
				case 'equals':                     		$query = substr($query, 0, strrpos($query, 'and'));  
					$query = "$fn = ?"; 
  
	}  					array_push($value, $val);                                                       
					break;
				case 'notEqual':                            
					$query = "$fn != ?";
					array_push($value, $val);                                                        
					break;
				case 'iEquals':                            
					$query = "UPPER($fn) = ?";
					array_push($value, "UPPER('{$val}')");                                                        
					break;
				case 'iNotEqual':                            
					$query = "UPPER($fn) != ?";  
					array_push($value, "UPPER('{$val}')");                                                      
					break;
				case 'greaterThan':                            
					$query = "$fn > ?"; 
					array_push($value, $val);                                                       
					break;
				case 'lessThan':                            
					$query = "$fn < ?";  
					array_push($value, $val);                                                      
					break;
				case 'greaterOrEqual':                            
					$query = "$fn >= ?";  
				
	return $query;
}

Note a new method called buildAdvancedCriteria() is required which builds the query criteria from the AdvancedCriteria delivered by the FilterBuilder:

Code Block
languagephp
titleRPCManager.php
private function buildAdvancedCriteria($request)
{
	$advancedCriteria = $request->getAdvancedCriteria();
	if(!empty($advancedCriteria))
	{
		$criteria_query = $this->buildCriterion($advancedCriteria);
		$query = ' where '.$criteria_query;
		return $query;
		
	}
	return null;	array_push($value, $val);                                   } 

For parsing the tree itself, introduce a recursive function:

Code Block
languagephp
private function buildCriterion($advancedCriteria) {               
		$criterias = $advancedCriteria['criteria'];
	$operator = $advancedCriteria['operator'];			break;
				case 'lessOrEqual':                            
	$result				$query = ''"$fn <= ?"; 
	
			
	foreach($criterias as $c)
	{
		if(isset($c['fieldName']))array_push($value, $val);                      			$fn = $c['fieldName']; 		
		if(isset($c['operator']))
			$op = $c['operator'];
						
		if(isset($c['value']))
		{               			if ($c['value'] === TRUE ) 				$val = '1'; 			else if($c['value'] === FALSE) 				$val = '0';
 
					elsebreak;
				$val =case $c['value'];contains':                            
			}  		$query = "$fn LIKE ?";  
					array_push($value, "%{$val}%");                  		 		if(isset($c['start']))     			$start = $c['start'];
		
		if(isset($c['end']))          			$end = $c['end']; 		 		if(isset($c['criteria']))     			$criteria = $c['criteria']; 		else 			$criteria = null; 		 		if(empty($criteria)) 		{ 			switch($op)

					{break;
				case 'equalsstartsWith':                            
					$query = "$fn = '$val'"; LIKE ?"; 
					array_push($value, "{$val}%");                                                                                  
					break;
				case 'notEqualendsWith':                            
					$query = "$fn != '$val'LIKE ?";
      					array_push($value, "%{$val}");                      					break; 				case 'iEquals':                             					$query = "UPPER($fn) = UPPER('$val')";                            
					break;
				case 'iNotEqualiContains':                            
					$query = "UPPER($fn) != UPPER('$val')LIKE ?";  
      					array_push($value, "%{$val}%");                   
					break;
				case 'greaterThan':                                					$query = "$fn > '$val'";                            
					break;
				case 'lessThaniStartsWith':                            
					$query = "UPPER($fn < '$val'") LIKE ?"; 
					array_push($value, "UPPER('{$val}%')");                             					break; 				case 'greaterOrEqual':                            
					$query = "$fn >= '$val'";                                  
					break;
				case 'lessOrEqualiEndsWith':                            
					$query = "UPPER($fn <= '$val'") LIKE ?";
					array_push($value, "UPPER('%{$val}')");                             					break; 				case 'contains':                            
					$query = "$fn LIKE '%$val%'";                             					break; 				case 'startsWith':                            
					$query = "$fn LIKE '$val%'";    break;                        
					break;
				case 'endsWithnotContains':                            
					$query = "$fn NOT LIKE '%$val'"; ?";  
					array_push($value, "%{$val}%");                                            					break; 				case 'iContains':                             					$query = "UPPER($fn) LIKE UPPER('%$val%')";                             					break; 				case 'iStartsWith':                            
					$query = "UPPER($fn) LIKE UPPER('$val%')";break;
				case 'notStartsWith':                            
					$query = "$fn NOT LIKE ?";
					array_push($value, "{$val}%");
					break;
				case 'iEndsWithnotEndsWith':                            
					$query = "UPPER($fn) NOT LIKE UPPER('%$val')?"; 
					array_push($value, "%{$val}");
					break;
         				case 'iNotContains':               					break;             
					$query = "UPPER($fn) NOT LIKE ?";  
					array_push($value, "UPPER('%{$val}%')");
					break;
				case 'notContainsiNotStartsWith':                            
					$query = "$fn NOT LIKE '%$val%'";                            
UPPER($fn) NOT LIKE ?";  
					array_push($value, "UPPER('{$val}%')");
					break;
				case 'notStartsWithiNotEndsWith':                            
					$query = "UPPER($fn) NOT LIKE '$val%'?";  
					array_push($value,                         
"UPPER('%{$val}')");
					break;
				case 'notEndsWithisNull':                            
					$query = "$fn NOT LIKE '%$val'IS NULL";                            
					break;
				case 'iNotContainsnotNull':                            
					$query = "UPPER($fn) IS NOT LIKE UPPER('%$val%')NULL";                            
					break;
				case 'iNotStartsWithequalsField':                            
					$query = "UPPER($fn) NOT LIKE UPPER('$val%')"$fn LIKE ?"; 
					array_push($value, "CONCAT('{$val}', '%')");                            
					break;
				case 'iNotEndsWithiEqualsField':                            
					$query =$query = "UPPER($fn) LIKE ?";    
					array_push($value, "UPPER($fn) NOT LIKE UPPER('%$val')"(CONCAT('{$val}', '%'))");                            
					break; 				case 'isNull':                          
				case 'iNotEqualField':
					$query = "UPPER($fn IS NULL") NOT LIKE ?";    
					array_push($value, "UPPER(CONCAT('{$val}', '%'))");                             
					break;
				case 'notNullnotEqualField':                            
					$query = "$fn ISNOT NOTLIKE NULL?";              
             
					break;
				case 'equalsField':                            
					$query = "$fn LIKE CONCAT($valarray_push($value, "CONCAT('{$val}', '%')");                             
					break;
				case 'iEqualsFieldgreaterThanField':                            
					$query = "UPPER($fn) LIKE UPPER(CONCAT($val, '%'))> ?";                     
      
					break;                            
				case 'iNotEqualField':
					$query = "UPPER($fn) LIKE UPPER(CONCAT($valarray_push($value, "CONCAT('{$val}', '%')")";                             
					break;
				case 'notEqualFieldlessThanField':                            
					$query = "$fn NOT LIKE CONCAT($val< ?";                     
					array_push($value, "CONCAT('{$val}', '%')");                             
					break;
				case 'greaterThanFieldgreaterOrEqualField':                            
					$query = "$fn >= ?";                    
					$query =array_push($value, "$fn > CONCAT('{$val}', '%')");                             
					break;
				case 'lessThanFieldlessOrEqualField':                            
					$query = "$fn < CONCAT($val, '%')= ?";                            
					break;
				case 'greaterOrEqualField':                            
					$query = "$fn >= CONCAT($valarray_push($value, "CONCAT('{$val}', '%')");                             
					break;
				case 'lessOrEqualFieldiBetweenInclusive':                            
					$query = "$fn <= CONCAT($val, '%')";BETWEEM ? AND ?";                            
					array_push($value, $start);
					array_push($value, $end);
					break;
				case 'iBetweenInclusivebetweenInclusive':                            
					$query = "$fn BETWEEM $start? AND $end?";                            
					array_push($value, $start);
  					array_push($value, $end);
					break;
				case 'betweenInclusive':     }
											
			$result .= " ".$query." ".$operator." ";                    
  			
		}else
		{
			$query// =build "$fnthe BETWEEMlist $startof ANDsubcriterias $end";   or criterions                    
    
					break$temp = $result;
			}
											$result1 = $this->buildCriterion($c);    
			$result = $temp .= " (".$query$result1['query'].") ".$operator." ";                    
		}else
		{
			// build the list of subcriterias or criterions                    
			$temp = $result;
			$result1 = $this->buildCriterion($cforeach($result1['value'] as $val)
			{
				array_push($value, $val);    
			$result = $temp . "(".$result1.") ".$operator." ";}                    
		}                  
	}
			   
	$result_query = substr($result, 0, strrpos($result, $operator));
	return$advanced_critical['query'] = $result_query;
	$advanced_critical['value'] = $value;
	return $advanced_critical;
}

If criterion exist (criteria property in this case will be null), simply build the query criteria and return it (this is the condition to exit from recursivity). If the criteria property is not null (the else branch), thencall the function again for each child criterion or criteria, and then assemble back everything using the specified operator before returning it to the caller.

...