How to define and access local variable in Typoscript 2 (Neos)? - neoscms

I have created a custom NodeType "Events" with a custom TS2 file in Neos, but I guess it is more a general question about Typoscript 2.
prototype(Some.Namespace:Events) < prototype(TYPO3.Neos:Document) {
...
sortOrder = ${request.arguments.sortOrder == 'asc' ? 'asc' : 'desc'}
otherVariable = ${sortOrder}
...
}
Of course this is simplified to focus on the issue:
I want to assign the value of the variable sortOrder (which is "asc" or "desc") to another variable named otherVariable.
How can I do that? I cannot access the value using ${sortOrder}, which returns always NULL.

All you need to do is add this as below and {otherVariable} in your fluid template will work. Flush cache in case you sill have NULL.
sortOrder = ${request.arguments.sortOrder == 'asc' ? 'asc' : 'desc'}
otherVariable = ${this.sortOrder}

Related

proper date and time management entity and html helpers

I'm trying to bind 2 different fields in 2 different classes
one is a date
I have to force "value", for some reason, the binding works for post, but not at get
#Html.EditorFor(model => model.FilterStartDate, new { type = "date", #class="form-control", #Value = (Model.FilterStartDate == null ? "" : Model.FilterStartDate.Value.ToString("yyyy-MM-dd")) })
Is there a way to do it better ? also, why does the class attribute is ignored ?
Also, why do I still have the time ? i'd like to just be about dates

Drools - check if argument is null before calling query

Inside the drools rule file, I'm trying to match the request object against inserted facts using a query (backward chaining). How do I check for null for the request object attribute? If the attribute is not null, I want to pass it to the query. If the attribute is null, I want to keep it unbound so that it will match all results. Since there are many request attributes, I'm looking for a generic solution instead of different rules for each attribute.
To give an example, lets assume I have two attributes currency and country in the goal: Goal() object and I want to call the query isMatching(String country,String currency)
if goal.getCountry() and goal.getCurrency() is not null, I want to call isMatching with goal.getCountry()and goal.getCurrency().
isMatching(goal.getCountry(),goal.getCurrency())
if goal.getCountry() is null and goal.getCurrency() is not null, I want to call isMatching with unbound variable country and goal.getCurrency()
isMatching(country,goal.getCurrency())
if goal.getCountry() is not null and goal.getCurrency() is null, I want to call isMatching with goal.getCountry() and unbound variable currency
isMatching(goal.getCountry(),currency)
if both goal.getCountry() and goal.getCurrency() are null, I want to call isMatching with unbound variable country and currency
isMatching(country,currency)
Best practice is to have a separate rule for each combination.
rule "both country and currency"
when
Goal( $country: country != null, $currency: currency != null )
$isMatching: Boolean() from isMatching( $country, $currency )
then
//
end
Not sure what you're referring to as an "unbound" variable in your question for your other use cases.
If you insist on not following best practices and try to kludge all of this into a single rule, you could either do your null check on the right hand side, or possibly abuse conditional and named consequences to do this. Doing it in the rule consequences ("then") will cause you to lose all of the performance optimization done by the Drools engine, which is done on the left hand side only.
Alternatively you could just update the query to handle the null case.
query isMatching( String $country, String $currency) {
$country := String( this == null )
or
$currency := String( this == null )
or
( actual implementation )
}
rule "example"
when
Goal( $country: country, $currency: currency )
isMatching( $country, $currency )
then
// ...
end
Actual implementation may vary; I have no idea how you'd implement a currency <-> country check.

JPQL: How to write dynamic where conditions

I am struggling with JPQL dynamic where condition. I tried searching the syntax for the same but coluldn't find one.
in my case if user is passing the name parameter then the select query should be
select * from user where name = 'sanjay'
if user is not passing name parameter then select query should be
select * from user
Below is my jpql query format which fails when name parameter is not passed.
entity_manager.createQuery("select u from user u where u.name = :name").setParameter("name",params[:name]).getResultList()
How can i update above JPQL query to support both the cases i.e when the name parameter is passed and when the name parameter is not passed ??
This is not possible in JPQL. You even cannot do something like
createQuery("select u from user u where u.name = :name OR :name IS NULL")
It is not possible. That simple. Use two queries or use the Criteria API.
This is the answer I get when I tries to do like you it is working with some modification.
In my case I had the problem that my optional parameter was a List<String> and the solution was the following:
#Query(value = "SELECT *
FROM ...
WHERE (COLUMN_X IN :categories OR COALESCE(:categories, null) IS NULL)"
, nativeQuery = true)
List<Archive> findByCustomCriteria1(#Param("categories") List<String> categories);
This way:
If the parameter has one or more values it is selected by the left side of the OR operator
If the parameter categories is null, meaning that i have to select all values for COLUMN_X, will always return TRUE by the right side of the OR operator
Why COALESCE and why a null value inside of it?
Let's explore the WHERE clause in all conditions:
Case 1: categories = null
(COLUMN_X IN null OR COALESCE(null, null) IS NULL)
The left part of the OR will return false, while the right part of the OR will always return true, in fact COALESCE will return the first non-null value if present and returns null if all arguments are null.
Case 2: categories = ()
(COLUMN_X IN null OR COALESCE(null, null) IS NULL)
JPA will automatically identify an empty list as a null value, hence same result of Case 1.
Case 3: categories = ('ONE_VALUE')
(COLUMN_X IN ('ONE_VALUE') OR COALESCE('ONE_VALUE', null) IS NULL)
The left part of the OR will return true only for those values for which COLUMN_X = 'ONE_VALUE' while the right part of the OR will never return true, because it is equals to 'ONE_VALUE' IS NULL (that is false).
Why the null as second parameter? Well, that's because COALESCE needs at least two parameters.
Case 4: categories = ('ONE_VALUE', 'TWO_VALUE')
(COLUMN_X IN ('ONE_VALUE', 'TWO_VALUE') OR COALESCE('ONE_VALUE', 'TWO_VALUE', null) IS NULL)
As in Case 3, the left part of the OR operator will select only the rows for which COLUMN_X is equale to 'ONE_VALUE' or 'TWO_VALUE'.

TYPO3 Extbase: How to store NULL in DB for empty numeric fields?

In a form Viewhelper I have lots of numeric fields, which should be empty by default, not filled with '0' or '0.00'.
I need this, because there should be a different handling of the data in the saveAction, depending on left empty fields or filled in values (including 0).
For this reason I set the field properties in the database table to NULL by default, like:
CREATE TABLE IF NOT EXISTS `tx_myext_domain_model_result` (
mw double (10,2) DEFAULT NULL,
mw2 double (10,2) DEFAULT NULL,
....
);
TCA looks like this:
'mw' => array(
...
'config' => array(
'type' => 'input',
'size' => 30,
'eval' => 'double2,null'
)
),
Also, in the Result model class, the corresponding properties are initialized with NULL;
/**
* mw
*
* #var float
*/
protected $mw = null;
I did this check to assure that NULL is handled over to the getter for empty form fields:
public function setMw($mw) {
if ($mw == NULL)
$this->mw = 999;
else
$this->mw = $mw;
}
This did what I excepted: The mw field DB was set to '999' for a empty form field.
But now, switched back to normal setter,
public function setMw($mw) {
$this->mw = $mw;
}
fields in the DB table are only left NULL when all other form fields are left empty, too. I.e. as soon as I enter a value in one of the form fields, all other empty form fields are set to '0' on save.
Adding null to the TCA eval field didn't do the trick, neither.
How can I change this behaviour? I'm using TYPO3 6.2.x
step 1: in initializeAction set null for the property
step 2: the idea with checking $mw is null is preaty good idea. TYPO3 not always do what you can assume using logical thinking :) So check if(is_null($mw)) $this->mw = null
this two things should do the trick
you can also set a default value in setter param
setMw($mw = null)

Strange drools syntax

I am having difficulty understanding the part value : value == 0? How does this code work?
rule "My rule"
when
m : MyClass( value : value == 0)
then
end
Assuming you are using Drools 5.4 or a newer snapshot, you can write any boolean expression as a constraint, so value == 0 is a constraint where "value" is a field name in MyClass.
Drools also allows you do use ":" to bind an attribute to a variable name, like this:
<variable_name> : <fieldName>
So, you can write:
MyClass( $var : value == 0)
Finally, since Drools uses a "context-aware" parser, you can have a variable with the same name as the attribute name, because Drools knows what comes before the : is a variable name, not a field. So, in your example: the variable "value" will be bound to the attribute "value" and the constraint "value == 0" will be true if the value attribute is equal to 0.
Hope it helps.