coldfusion StructFindValue not working - coldfusion

I have many structures, I'm using StructFindValue() to determine if a a key occurs many times.
I get the expected array returned for "singles", however I get an empty array for "doubles" and "triples" - this code is actually inside a different code segment, -my is a structure in itself...
If I try ANY of the lines "x= StructFindValue( y, 3, 'all' );" in a stand alone template - CF finds the '2' and '3' values just fine - I get an array with the data - but inthe above code - ONLY the values of '1' return anything...
I'm confused.
UPDATE
OK, in response to the request for more information, my server details are:
Version ColdFusion 10,282462
Edition Developer
Operating System Windows XP
Java Version 1.6.0_29
OS Version 5.1
Update Level /C:/ColdFusion10/cfusion/lib/updates/chf10000002.jar
Adobe Driver Version 4.1 (Build 0001)
As you can see from the code example below, my array has a few different names, of differing repetitive nature. I want to know which values repeat a specified number of times. As stated above, the structkeyFind() works IF (and only) on the key value of 1. It doesn't work with the key values of 2, 3 (or 4 or 5 etc, not included here for brevity).
<cfscript>
_myArry = listToArray('bob,bob,bob,joe,jane,jane,john,john,john,alex,greg');
_myStats = getDataStats( _myArry );
writeDump( _myStats );
</cfscript>
<cffunction name="getDataStats">
<cfargument name="data" required="yes" type="array" >
<cfscript>
var _hv = {};
// default some values
_hv.vals = {};
_hv.threes = false;
_hv.twos = false;
_hv.ones =false;
// loop the data put it into separate containers
for ( var i=1; i LTE arrayLen( arguments.data ); i++ ) {
switch ( lcase( arguments.data[i] ) ) {
case 'bob': // bob
if ( structKeyExists( _hv.vals, 'bob' ) ) { _hv.vals.bob = _hv.vals.bob + 1; }
else { _hv.vals.bob = 1; }
break;
case 'joe': // joe
if ( structKeyExists( _hv.vals, 'joe' ) ) { _hv.vals.joe = _hv.vals.joe + 1; }
else { _hv.vals.joe = 1; }
break;
case 'jane': // jane
if ( structKeyExists( _hv.vals, 'jane' ) ) { _hv.vals.jane = _hv.vals.jane + 1; }
else { _hv.vals.jane = 1; }
break;
case 'john': // john
if ( structKeyExists( _hv.vals, 'john' ) ) { _hv.vals.john = _hv.vals.john + 1; }
else { _hv.vals.john = 1; }
break;
case 'alex': // alex
if ( structKeyExists( _hv.vals, 'alex' ) ) { _hv.vals.alex = _hv.vals.alex + 1; }
else { _hv.vals.alex = 1; }
break;
case 'greg': // greg
if ( structKeyExists( _hv.vals, 'greg' ) ) { _hv.vals.greg = _hv.vals.greg + 1; }
else { _hv.vals.greg = 1; }
break;
}
}
// give me a return struct for testing so i can 'see' where I'm at
var _thisReturn = {
'threes' = StructFindValue( _hv.vals, 3, 'all' ),
'twos' = StructFindValue( _hv.vals, 2, 'all' ),
'ones' = StructFindValue( _hv.vals, 1, 'all' ),
'values' = arguments.data
};
</cfscript>
<cfreturn _thisReturn />
</cffunction>
In an attempt to 'cast' the values, I have tried each of these variations. However the results are UNCHANGED from the original.
'ones' = StructFindValue( _hv.vals, '1', 'all' ),
'twos' = StructFindValue( _hv.vals, '2', 'all' ),
'threes' = StructFindValue( _hv.vals, '3', 'all' ),
And then
'ones' = StructFindValue( _hv.vals, val( 1 ), 'all' ),
'twos' = StructFindValue( _hv.vals, val( 2 ), 'all' ),
'threes' = StructFindValue( _hv.vals, val( 3 ), 'all' ),

The issue here appears to be how CF is storing / displaying / comparing the values.
Here is a simple demonstration of the problem:
<cfset Data =
{ Bob : 2
, Joe : 1+1
, Jane : "2"
, John : 2.0
, Alex : 4/2
} />
<cfdump var=#Data# />
<cfdump var=#StructFindValue(Data,2,"all")# />
<cfdump var=#StructFindValue(Data,"2","all")# />
<cfdump var=#StructFindValue(Data,2.0,"all")# />
The first dump displays all values as 2 except for John who is 2.0
However, the first two StructFindValue calls both only return Bob,Jane.
The third StructFindValue call returns Joe,John, Alex.
This basically demonstrates that CF's StructFindValue does a very crude comparison for checking equality, and basically isn't to be trusted when it comes to dealing with numbers.
(The issue doesn't exist with Railo, which probably uses the exact same comparison as it would when doing an EQ test, coercing types accordingly. Only tested on CF10,0,0,282462.)
To solve your problem, it seems you may need to manually walk the struct and replicate the behaviour of StructFindValue yourself.

Related

Wordpress query search 's' exact word

I see this question has been posted multiple times but all of the examples I've tried are not working.
I want to limit the search by keywords to only search for exact word not a phrase or mixed content, so if I search for cheese It need to be limited only for word cheese, right now it will return content with word cheeseburger also.
The part of the search query in my modified template query is right here:
if ( 'featured' === $args['orderby'] ) {
$query_args['orderby'] = [
'menu_order' => 'ASC',
'date' => 'DESC',
'ID' => 'DESC',
];
}
if ( 'rand_featured' === $args['orderby'] ) {
$query_args['orderby'] = [
'menu_order' => 'ASC',
'rand' => 'ASC',
];
}
$job_manager_keyword = sanitize_text_field( $args['search_keywords'] );
if ( ! empty( $job_manager_keyword ) && strlen( $job_manager_keyword ) >= apply_filters( 'job_manager_get_listings_keyword_length_threshold', 2 ) ) {
$query_args['s'] = $job_manager_keyword;
//add_filter( 'posts_search', 'get_job_listings_keyword_search' );
}
$query_args = apply_filters( 'job_manager_get_listings', $query_args, $args );
if ( empty( $query_args['meta_query'] ) ) {
unset( $query_args['meta_query'] );
}
if ( empty( $query_args['tax_query'] ) ) {
unset( $query_args['tax_query'] );
}
/** This filter is documented in wp-job-manager.php */
$query_args['lang'] = apply_filters( 'wpjm_lang', null );
// Filter args.
$query_args = apply_filters( 'get_job_listings_query_args', $query_args, $args );
do_action( 'before_get_job_listings', $query_args, $args );
I can see that $query_args['s'] is standard query for search keywords but some standard query modifications I've tried like this examples
https://wordpress.stackexchange.com/questions/177183/make-a-wp-query-search-match-exactly-the-search-term/17786
WordPress exact search query
Query for exact word search
And other examples too, none of them are working for me.
Anyone have an idea how i can modify it so it can search only for exact word in content?

How to compare column values in cfscript?

I would like to loop over query and compare column values. Here is example of CFML code:
<cfquery name="qryUserPerm" datasource="#Application.dsn#">
SELECT AccessType, AccessLevel, State, City, Building
FROM Permissions
WHERE AccountID = <cfqueryparam cfsqltype="cf_sql_integer" value="#trim(session.AccountID)#">
</cfquery>
<cfset local.permissionType = "">
<cfset local.permissionLevel = "">
<cfset local.permissionList = "">
<cfif qryUserPerm.AccessLevel EQ "S">
<cfset local.permissionType = qryUserPerm.AccessType>
<cfset local.permissionLevel = qryUserPerm.AccessLevel>
<cfset local.permissionList = qryUserPerm.State>
<cfelseif qryUserPerm.AccessLevel EQ "C">
<cfset local.permissionType = qryUserPerm.AccessType>
<cfset local.permissionLevel = qryUserPerm.AccessLevel>
<cfset local.permissionList = ListRemoveDuplicates(ValueList(permissionList,qryUserPerm.City))>
<cfelseif qryUserPerm.AccessLevel EQ "B">
<cfset local.permissionType = qryUserPerm.AccessType>
<cfset local.permissionLevel = qryUserPerm.AccessLevel>
<cfset local.permissionList = ListRemoveDuplicates(ValueList(permissionList,qryUserPerm.Building))>
</cfif>
Code above should be translated to cfscript, I got this far but can't figure it out how to access column values.
<cfscript>
public string function permissionList(required string AccountID) {
local.fnResults = "";
local.permissionList = "";
try{
local.qryPermissions = new Query();
local.qryPermissions.setDatasource("#Application.dsn#");
local.qryPermissions.setSQL("SELECT AccessType, AccessLevel, State, City, Building FROM Permissions WHERE AccountID = :AccountID");
local.qryPermissions.addParam(name="AccountID",value="#trim(arguments.AccountID)#",cfsqltype="cf_sql_idstamp");
local.qryRes = qryPermissions.execute();
for ( i = 1 ; i <= qryRes.getResult().recordCount ; i++ ) {
if(qryRes["AccessLevel"][i] EQ "S"){
local.permissionList = "";
}else if(qryRes["AccessLevel"][i] EQ "S"){
local.permissionList = ListRemoveDuplicates(ValueList(qryRes.Agency,","));
}else if(qryRes["AccessLevel"][i] EQ "C"){
local.permissionList = ListRemoveDuplicates(ValueList(qryRes.District,","));
}else if(qryRes["AccessLevel"][i] EQ "B"){
local.permissionList = ListRemoveDuplicates(ValueList(qryRes.Building,","));
}
}
local.fnResults = permissionList;
}catch(any e){
local.fnResults = e.message;
//writeOutput(e.message);
}
return fnResults;
}
writeOutput(permissionList(AccountID));
</cfscript>
If anyone can help please let me know.
(From comments ...)
The issue is local.qryRes doesn't actually contain a query object. Confusingly, calling execute() doesn't return a query, but calling execute().getResult() does. Try changing the assignment from:
local.qryRes = qryPermissions.execute();
To:
local.qryRes = qryPermissions.execute().getResult();
A few other observations:
It is important to local scope ALL function variables, including your loop index i. Otherwise, you may get some bizarre and unpredictable results if the component is stored in a shared scope.
Although I don't think a loop is necessary, if you do loop, consider the simpler for..in syntax, instead of an indexed loop:
for (local.row in local.qryPermissions ) {
if (local.row.AccessType eq "S") {
//... code here
}
....
}
Since the access fields are so closely related, I'd probably have the function return a structure containing all three keys (AccessType, AccessLevel, PermissionList) rather than having three separate functions.
Rather than using a loop, consider going with one of the suggestions on your other thread,
Best way to store permissions for the user account?
You can also use :
local.qryPermissions = queryExecute(
"SELECT AccessType, AccessLevel, State, City, Building
FROM Permissions
WHERE AccountID = :AccountID" ,
{AccountID={value="#trim(arguments.AccountID)#", cfsqltype="cf_sql_idstamp"}} // Or "?" and "[value=xxx,cfsqltype=xxx]"
) ;
And then just build out your permissions pieces without the loop:
local.permissionType = qryPermissions.AccessType ;
local.permissionLevel = qryPermissions.AccessLevel ;
switch( qryPermissions.AccessLevel ) {
case "S" : local.permissionList = qryPermissions.State ;
break ;
case "C" : local.permissionList = ListRemoveDuplicates(ValueList(qryPermissions.City)) ;
break ;
case "B" : local.permissionList = ListRemoveDuplicates(ValueList(qryPermissions.Building)) ;
break ;
}
Also see my notes on the other question about potential for unintentional, semi-related data.

How to Dynamically write implicit getters and setters in ColdFusion/Lucee Components?

I want to be able to dynamically write a set of getters and setters in CFML/LUCEE components ( No hardcoded cfproperty tags).
<!--- MyComp.cfc --->
<cfcomponent displayname="MyComp" hint="MyComp" accessors="true">
<cffunction name="init">
<cfargument name="dynamicprops" type="array">
<cfloop array="#dynamicprops#" index="item">
<!---
Now what? I cannot do a cfsavecontent and write props here.
It demands cfproperty just after the cfcomponent begins. I
tried to do with closures but they are not acually setters
and getters. Does anyone know how to better do it?
--->
</cfloop>
</cffunction>
</cfcomponent>
<!--- example call --->
<cfset mc = CreateObject("component","MyComp").init( [{"name"="a","default"=1}] ) />
Then I want to be able to call mc.setA( 100 ) and mc.getA(). But does not happen.
So my humble question is how can I dynamically write setters and getters on component?
PS: Please remeber that I have tried the closure way:
variables[item.name] = item.default;
variables["set"&item.name] = function(_val){ variables[item.name] =_val; }
variables["get"&item.name] = function(){ return variables[item.name; }
Couldn't get worked. How can I do it?
Thanks :)
You could use onMissingMethod() for this.
component name="myComponent" hint="myComponent.cfc"{
function init( struct dynamicProperties={} ){
dynamicProperties.each( function( name, default ){
variables[ name ] = default;
} );
return this;
}
function onMissingMethod( name, args ){
if( name.Left( 3 ) IS "get" )
return get( name );
if( ( name.Left( 3 ) IS "set" ) AND ( args.Len() IS 1 ) )
return set( name, args[ 1 ] );
cfthrow( type="NonExistentMethod", message="The method '#name#' doesn't exist" );
}
public any function get( required string accessorName ){
var propertyName = parsePropertyName( accessorName );
if( !variables.KeyExists( propertyName ) )
cfthrow( type="NonExistentProperty", message="The property '#propertyName#' doesn't exist" );
return variables[ propertyName ];
}
public void function set( required string accessorName, required any value ){
var propertyName = parsePropertyName( accessorName );
if( !variables.KeyExists( propertyName ) )
cfthrow( type="NonExistentProperty", message="The property '#propertyName#' doesn't exist" );
variables[ propertyName ] = value;
}
private string function parsePropertyName( accessorName ){
return accessorName.RemoveChars( 1, 3 );
}
}
Pass it your struct of property names/default values and it will "listen" for getters/setters that match. Any that don't will result in an exception.
<cfscript>
myDynamicProperties = { A: 0, B: 0 }; // this is a struct of names and default values
mc = new myComponent( myDynamicProperties );
mc.setA( 100 );
WriteDump( mc.getA() ); // 100
WriteDump( mc.getB() ); // 0
WriteDump( mc.getC() ); // exception
</cfscript>
UPDATE 1: Property name array replaced with name/default value struct as init argument to allow default values to be set.
UPDATE 2: If you want to pass an array of structs containing your name/default value pairs e.g.
dynamicProperties = [ { name: "A", default: 1 }, { name: "B", default: 2 } ];
then the init() method would be:
function init( array dynamicProperties=[] ){
dynamicProperties.each( function( item ){
variables[ item.name ] = item.default;
} );
return this;
}
UPDATE 3: If you must use tags and <cfloop> to set your dynamic properties then this is all you need in your init method:
<cfloop array="#dynamicProperties#" item="item">
<cfset variables[ item.name ] = item.default>
</cfloop>
onMissingMethod won't fire if you try to invoke the dynamic methods as properties like this:
method = mc[ "set#property#" ];
method( value );
Instead just make the set() and get() methods in the component public and invoke them directly:
mc.set( property, value );
mc.get( property );
Consider using accessors
<cfcomponent displayname="MyComp" hint="MyComp" accessors="true">
Source: https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-tags/tags-c/cfcomponent.html

Titanium Appceleratory Alloy and Regex: Unable to delete characters in textfield with keypad

This function:
function maskPhoneNumber( phoneNumber ){
//assure that it is a string
var stringyPhone = String( phoneNumber );
//refresh the number by stripping it of odd characters
stringyPhone = stringyPhone.replace( /[^0-9]/g, '' );
//return only the first 11 digits (if it starts with a 1)
if( stringyPhone.first(1) == 1 ){
stringyPhone = stringyPhone.first( 11 );
}else{
stringyPhone = stringyPhone.first( 10 );
}
stringyPhone = stringyPhone.replace( /^([1]?)(\d{1,3})/, "$1 ($2) " );
stringyPhone = stringyPhone.replace( /(\d{3})(\d{1,4})$/, "$1-$2" );
return stringyPhone;
}
Is called here:
//on change of the phone number
//mask it and set the value of the form to the masked value
$.loginPhone.addEventListener( 'change', function( event ){
$.loginPhone.setValue( maskPhoneNumber( $.loginPhone.getValue() ) );
} );
And it masks quite well on the way up (typing in produces "1 (555) 555-5555)" however there is a strange bug where it stops deleting on the way down (backspacing through the textfield hangs at "1 (555) ").
Is this a regEx error on my part, or am I missing something else? Anyone wiser out there have an idea?

Doctrine 2 date in where-clause (querybuilder)

I've the following problem: I want a where-clause that checks if a user is active AND if he has the right date.
A user contains the following:
State
Startdate
Enddate
So, State should stay on 1, then he should look for state = 1 AND the current date is between the start and enddate. I've the following right now, and it works fine. But the start and enddate is not required. So it could be NULL. How can i get a query like:
SELECT *
FROM user/entity/user
WHERE
state = 1
AND (
(startdate <= CURRENT_DATE AND enddate >= CURRENT_DATE)
OR startdate == NULL
OR enddate == NULL
)
, so i get all my active users, and not only the temporary users.
I've set up the following code right now:
Repository:
public function searchUser($columns, $order_by, $order)
{
//Create a Querybuilder
$qb = $this->_em->createQueryBuilder();
//andx is used to build a WHERE clause like (expression 1 AND expression 2)
$or = $qb->expr()->andx();
//Select all from the User-Entity
$qb->select('u')
->from('User\Entity\User', 'u');
foreach($columns as $column => $name)
{
if($column == 'state')
{
if($columns['state']['value'] == '1') {
$or = $this->betweenDate($qb, $or);
$or = $this->like($columns, $qb, $or);
} elseif($columns['state']['value'] == '2') {
$or = $this->like($columns, $qb, $or);
} elseif($columns['state']['value'] == '3') {
$or = $this->outOfDate($qb, $or);
}
} else {
//Get a where clause from the like function
$or = $this->like($columns, $qb, $or);
}
}
//Set the where-clause
$qb->where($or);
//When there is a order_by, set it with the given parameters
if(isset($order_by)) {
$qb->orderBy("u.$order_by", $order);
}
//Make the query
$query = $qb->getQuery();
/*echo('<pre>');
print_r($query->getResult());
echo('</pre>');*/
//Return the result
return $query->getResult();
}
public function betweenDate($qb, $or)
{
$or->add($qb->expr()->lte("u.startdate", ":currentDate"));
$or->add($qb->expr()->gte("u.enddate", ":currentDate"));
//$or->add($qb->expr()->orx($qb->expr()->eq("u.startdate", null)));
$qb->setParameter('currentDate', new \DateTime('midnight'));
return $or;
}
//This one works perfect
public function like($columns, $qb, $or)
{
//Foreach column, get the value from the inputfield/dropdown and check if the value
//is in the given label.
foreach($columns as $label=>$column){
$value = $column['value'];
$or->add($qb->expr()->like("u.$label", ":$label"));
$qb->setParameter($label, '%' . $value . '%');
}
return $or;
}
I use this "usersearch" also for other fields. So it should pick all the data out of the database, only state is different because "out of date" is not in the database. So it has to check differently. Hope somebody can help.
Problem solved
$startdate = $qb->expr()->gt("u.startdate", ":currentDate");
$enddate = $qb->expr()->lt("u.enddate", ":currentDate");
$or->add($qb->expr()->orX($startdate, $enddate));
That's how I would build the where clause:
//CONDITION 1 -> STATE = 1
$state = $qb->expr()->eq( 'state', ':state' );
//CONDITION 2 -> startdate <= CURRENT_DATE AND enddate >= CURRENT_DATE
$betweenDates = $qb->expr()->andX(
$qb->expr()->lte("u.startdate", ":currentDate"),
$qb->expr()->gte("u.enddate", ":currentDate")
);
//CONDITION 3 -> startdate == NULL
$startDateNull = $qb->expr()->isNull( 'startdate' );
//CONDITION 4 -> enddate == NULL
$endDateNull = $qb->expr()->isNull( 'enddate' );
//CONDITION 5 -> <CONDITION 2> OR <CONDITION 3> OR <CONDITION 4>
$dates = $qb->expr()->orX( $betweenDates, $startDateNull, $endDateNull );
//CONDITION 6 -> <CONDITION 1> AND <CONDITION 5>
$whereClause = $qb->expr()->andX( $state, $dates );