Result of SQL to Userfunc in TypoScript - typo3

I am very new to Typo3/TypoScript and I wondered if and how it is possible to give the result of a sql-statement in TypoScript to a php function.
I've managed the userFunc so far, but i'm getting confused with the rest.
Here is my try:
temp.pidList = CONTENT
temp.pidList {
table = tx_eepcollect_sessions
#table = tt_content
select {
pidInList < plugin.tx_eepcollect_pi1.pid_list
where {
stdWrap.cObject = TEXT
stdWrap.cObject {
data = global : _COOKIE | tx_eepcollect_pi1
wrap = ses_id='|'
}
}
}
renderObj = COA
renderObj {
10 = TEXT
10.field = ses_data
#30 = TEXT
#30.data = debug:data
}
}
includeLibs.user_idList = fileadmin/services/user_IdList.php
temp.ListOfIds = USER
temp.ListOfIds.userFunc = user_IdList->get_IdList
#temp.pidList = TEXT
#temp.pidList = {"1275":{"id":"1275","tx_eepcollect_pi1":{"prozess":"add","pid":"1275","ctrl":"1360858765"},"cHash":"e90b62584f3f0e4f71bf1100faf39d83"}}
temp.ListOfIds.userFunc.jsonList < temp.pidList
temp.mainContent = COA_INT
temp.mainContent.10 = TEXT
temp.mainContent.10.stdWrap.cObject < temp.ListOfIds
The output is an array of a lot of stuff, but not the result of the database-query.

TypoScript is not a programming language, so it is not executed in any meaning. You should think about TS as set of instructions for the Core.
So, following line:
temp.ListOfIds.userFunc.jsonList < temp.pidList
will not put result of temp.pidList into temp.ListOfIds.userFunc.jsonList, as you would expect from a programming language - it will simply copy set of instructions, so at the end you'll have following structure:
temp.ListOfIds = USER
temp.ListOfIds.userFunc = user_IdList->get_IdList
temp.ListOfIds.userFunc.jsonList = CONTENT
temp.ListOfIds.userFunc.jsonList.table = tx_eepcollect_sessions
[...]
And since jsonList is your custom property you need to bring stdWrap to it by following method:
TypoScript
temp.ListOfIds.userFunc.jsonList.cObject < temp.pidList
PHP
$jsonList = $this->cObj->stdWrap($conf['jsonList'], $conf['jsonList.']);
I assume, that you call it from a method get_IdList of class user_IdList with two params: $content and $conf.
As an additional measerue, you can make it as extension with a kickstarter or extension builder, so it'll be easier for you to work with configuration.
Another thing is, that your code have potential SQL injection vulnerability here:
where {
stdWrap.cObject = TEXT
stdWrap.cObject {
data = global : _COOKIE | tx_eepcollect_pi1
wrap = ses_id='|'
}
}
So, you may consider reading about markers

Related

Pass a variable to a content object containing a tx_news plugin

This is very similar to my question Counter for mask elements in a TYPO3 column
I need to pass a variable (in my case, the value of cObj:parentRecordNumber which is the counter for the current item in it's column) to the template.
In the main page template:
<f:cObject typoscriptObjectPath="lib.content.pageteasers" />
In typoscript:
lib.content {
pageteasers < styles.content.get
pageteasers {
select {
where = colPos=2
max = 8
}
// this passes the variable elementCounter to the fluid template of the rendered mask content element:
renderObj.mask_teaser {
variables {
elementCounter = TEXT
elementCounter.value = {cObj:parentRecordNumber}
elementCounter.insertData = 1
}
}
// this should pass the same value to a rendered tx_news plugin:
variables {
elementCounter = TEXT
elementCounter.value = {cObj:parentRecordNumber}
elementCounter.insertData = 1
}
// it doesn't. what about these trial & error:
renderObj.list < .variables
renderObj.plugin.tx_news < .variables
renderObj.list.20.news_pi1 < .variables
renderObj.news_pi1 < .variables
// none of these seem to work either
}
}
And then in the rendered CE template (News/List.html)
<f:debug title="" inline="1">{_all}</f:debug>
I don't manage to see the above variable in here. What is the correct way to pass the variable from TS to the news fluid template?
PS Another try was using the Good Old Register
pageteasers < styles.content.get
pageteasers {
select {
where = colPos=2
max = 8
}
append = LOAD_REGISTER
append {
elementCounter = TEXT
elementCounter.value = {cObj:parentRecordNumber}
elementCounter.insertData = 1
}
}
And in the template:
{v:variable.register.get(name: 'elementCounter')}
but that is NULL
Your try via register could work. But you have to care, where you are doing what...
Registers
LOAD_REGISTER is of type string/stdWrap and not a cObject per definition. If you wanna use content based on a cObject, you can declare this via the stdWrap-property (as shown in the examples):
1 = LOAD_REGISTER
1.param.cObject = TEXT
1.param.cObject.stdWrap.data = GP:the_id
So, in your case it should like:
elementCounter.cObject = TEXT
elementCounter.cObject.data = cObj:parentRecordNumber
Appending at the right place
You are trying to use append directly as a property of pageteasers (which is a copy of styles.content.get which is of type CONTENT.
CONTENT does not have an append-property. In addition, you would put the register in front of the entire content, not the individual content elements.
=> It's needed as part of the renderObj, so it's rendered per CE.
The renderObj of CONTENT is of type CASE, which also has no stdWrap-properties directly. But it has a property stdWrap with stdWrap-properties...
Conclusion
So, you can end up with this snippet:
lib.content {
pageteasers {
renderObj {
stdWrap {
append = LOAD_REGISTER
append {
elementCounter.cObject = TEXT
elementCounter.cObject.data = cObj:parentRecordNumber
}
}
}
}
}

Pass Fluid variable to TypoScript

I want to pass a variable (uid of category) in Fluid to a TypoScript :
<f:cObject typoscriptObjectPath="lib.testFluid" data="{setting.myvar}/>
Then i want to use the var to get all content elements in folder with pid 942 and the category {setting.myvar}
lib.testFluid = COA
lib.testFluid = CONTENT
lib.testFluid {
table = tt_content
select {
pidInList = 942
where = selected_categories = |
}
}
This does not work, it creates an MySql syntax error. I also tried using current = 1 instead of the where clause without success. I looked at post TYPO3: pass variable to typoscript via cObject? and I can recreate it but it does not work with my script. (TYPO3 8)
If i use
...
where = selected_categories = 13
....
the scrip will succesfully display all CE with category 13. How do i make it work with a var?
could you try this:
<f:cObject typoscriptObjectPath="lib.testFluid" data="{myvar: setting.myvar}/>
lib.testFluid = CONTENT
lib.testFluid {
table = tt_content
select {
pidInList = 942
where.data = field:myvar
where.intval = 1
where.wrap = selected_categories=|
}
}
hard to test for me but it might work ...
I had to solve it once with markers. I couldn't find another simpler way. I give you a very general solution which you may adapt to your needs. For example you could set the pid value via a typoscript setting which is more elegant than to put it in the snippet code. Please try:
<f:cObject typoscriptObjectPath="lib.testFluid" data="{category: setting.myvar, catPid: 942}" currrentValueKey="category" />
The related TypoScript snippet:
lib.testFluid = COA
lib.testFluid {
10 = LOAD_REGISTER
10 {
category.cObject = TEXT
category.cObject.value.current = 1
catPid.cObject = TEXT
catPid.cObject.value.dataWrap = { field: catPid }
}
20 = CONTENT
20 {
table = tt_content
select {
pidInList.cObject = TEXT
pidInList.cObject.dataWrap = {REGISTER:catPid}
where = selected_categories=###category###
markers {
category.data = REGISTER:category
}
}
}
30 = RESTORE_REGISTER
}

Typoscript issue with GP vars?

I want to execute a query using typoscript . My query is
SELECT * FROM TABLE where sld like '{GP:SID}' . I want to warp the SID variable inside a single/double quotes. Tried the below script, but didnt worked for me.
lib.products = CONTENT
lib.products {
table = TABLE
select {
pidInList = 26506
orderBy = name
where = sid like '{GP:SID}'
}
renderObj = COA
renderObj {
10 = COA
10 {
10 = TEXT
10.dataWrap ={field:name}[\n]
}
}
}
Can any help me with this ?
You code is insecure! Don't use it on production.
What you should do instead - is to use TS query markers, which uses prepared statements in a background.
lib.products = CONTENT
lib.products {
table = TABLE
select {
pidInList = 26506
orderBy = name
where = sid like '###sid###'
markers {
sid.data = GP:SID
}
}
renderObj = COA
...
}
Also, if you want to use LIKE, you need % sign to make it work, otherwise it is same as 'equals', but slower. However, I'm not sure, what happens, if GP:SID contains % sign at the beginning or at the end of a string.

fallback for FILES in typoscript

I'm using typoscript go get the first file out of a page. This works and is out of the documentation. But the documentation doesn't tell something about a fallback if no files are found.
lib.files = FILES
lib.files {
references {
table = pages
fieldName = media
}
file =
begin = 0
maxItems = 1
renderObj = TEXT
renderObj {
value = URL: {file:current:publicUrl}
insertData = 1
}
}
A naive .if.isNull failed at the first attpent. Is there a possibilty to define a fallback if the FILES-Object is empty?
Mh,
try to make a second object and check there if lib.files is filled with data.
__
UNTESTED
lib.files = FILES
lib.files {
references {
table = pages
fieldName = media
}
file =
begin = 0
maxItems = 1
renderObj = TEXT
renderObj {
value = URL: {file:current:publicUrl}
insertData = 1
}
}
lib.fileFallback = COA
lib.fileFallback {
10 < lib.files
20 = TEXT
20 {
value = Do something
stdWrap.if.isFalse.cObject < lib.fileFallback.10
}
}
I am using this technique often to slide through pages for slideshow objects.
Let me know, if it is working.
You should check the TypoScript Reference stdWrap.ifEmpty.
In your case the code should look like this
lib.files {
# ....
stdWrap.ifEmpty.cObject = IMAGE
stdWrap.ifEmpty.cObject{
file = fileadmin/.../my_image.png
}
# ....
}
You can use every cObject you want.
Check out Content Objects (cObject)

how to insert the language id in a typolink in typoscript

i want to add the language id in a typolink
so far
LOGO = COA
LOGO {
10 = TEXT
10 {
value = logo
typolink {
parameter = 116
additionalParams = &L={$config.sys_language_uid}
}
}
}
if L=4 it's working
but if i use L={$config.sys_language_uid} it gets ignored altogether
same with L=GP:L
and L=GPvar:L
what would be the proper syntax here
working if i do something like
additionalParams = COA
additionalParams {
10 = TEXT
10.data = GP : L
10.intval = 1
10.wrap = &L=|
}
You do not need to do that on your own. With the following global TypoScript configuration the parameter L will added to every link:
config.linkVars = L(int)
So, if you use HMENU.special = language this will be managed on the switch automatically, too:)
http://docs.typo3.org/typo3cms/TyposcriptReference/Setup/Config/Index.html
I do not know about {$config.sys_language_uid}, but your code will output it as plain text. In order to use variables like {GP:L}, you got to dataWrap it or insert "insertData" after the value.
10 = TEXT
10 {
value = logo
typolink {
parameter = 116
additionalParams.dataWrap = &L={GP:L}
}
}
OR (the best way I'd say):
10 = TEXT
10 {
value = logo
typolink {
parameter = 116
additionalParams.cObject = TEXT
additionalParams.cObject {
wrap = &L=|
data = GP:L
}
}
}
Using {TSFE:sys_language_uid} might be a better option if you are querying the database.
There is no need to make a cObject or COA
additionalParams = type=0&L={GP:L}
additionalParams.insertData = 1
You can do this kind of URL using addQueryString = 1. https://docs.typo3.org/typo3cms/TyposcriptReference/Functions/Typolink/Index.html#addquerystring