Modx - Getresource filter by TV date - modx-revolution

I am trying to use getResources to filter based on a date-type TV.
I ant to create a filter that allows me to either show Future events and Past events.
TVname = voucherdate
What I have done is:
Create a current date snippet with the following:
<?php
return date('Y-m-d H:i:s');
Then I created the following getResource:
Current events
[[!getResources?
...
&tvFilters=`voucherdate>=[[curDate]]`
]]
Past events
[[!getResources?
...
&tvFilters=`voucherdate<<[[curDate]]`
]]
But it does not seem to be working. I have tested to se where the sniper works by calling only [[curDate]] in a template and that shows the current date.
Could anyone offer some advice.
thanks

all your template variables are store as strings, so you are going to have to do some string comparison..... which is probably not going to work in the getresources call...
best thing to do is to pass the date string value to a snippet and do your date comparison there, something like:
[[!getResources? ...
&tvFilters=`[[!DateCompare? &date=`[[*voucherdate]]` &range=`future|past` ]] == 'true']]
]]
convert your dates to epoch or something in the DateCompare snippett and return true or false... then getResources ~should~ return the correct resources ... I think, not tested... I con't think of an easier way off the top of my head. UNLESS you want to store your dates as an epoch value initially... [that would be better anyway] then your string comparison you were trying would 'just work'

Related

Find and Replace Multiple srings with different values in zapier

I have a zap that gets a block of text. Within that block of text they are about 30 variables that I need to replace e.g.{{variable1}}, {{variable2}}, {{variable3}} etc.
I need to replace each of these variables with a different string that I get in another step of the zap.
Zapier has a tool called find and replace text which works well; however it only works for 1 string at a time so this would mean I would need to do this 30 times.
Is there a way to complete this is one or two steps. Is it possible to use Code by Zapier (either Python or JS) to achieve this?
Yep! This can be done very easily using a code step in Python:
You'll pass any data into the Code step in the input_data dict. I'm not sure how it's coming in or formatted, but hopefully you can build a dict out of it. I'll make one up in this example.
keys = input_data['keys'] # something like "{{a}},{{b}},{{c}}"
values = input_data['values'] # something like "1,2,3"
replacers = {k:v for k,v in zip(keys.split(','), values.split(','))} # makes a mapping like {"{{a}}": "1", ...}
str_to_replace = input_data['string_to_repalace']
for k, v in replacers.items():
str_to_replace = str_to_replace.replace(k, v)
return {'result': str_to_replace}
How exactly this fits your use case depends on how your variables and stuff come in, but it should get you most of the way there.

Twilio fetch all call logs from last 5 days in ruby

I want to fetch the call logs for the last 5 days, I read on the documentation
You can also specify an inequality, such as EndTime<=YYYY-MM-DD, to read calls that ended on or before midnight of this date
I am trying the following with no luck
#client.calls.list(to: phone_number, end_time: ">=#{Time.now - 5.days}")
Twilio developer evangelist here.
There are several things here, and I need to apologise for at least one of them.
Firstly, the less than/greater than equal that the Twilio API implemented was actually a bit of a hack with the way the parameters are formatted. The parameter as the docs point out is EndTime<=YYYY-MM-DD but this is made of the parameter name EndTime< and the parameter value YYYY-MM-DD separated by =. I apologise that this seemed like a cool hack but actually made things harder.
The Ruby library actually tries to unpick this and make it more sensible again. You can use the parameter end_time_after instead of trying to form the correct end_time format.
Second, I ran the string you were using for the end_time and it produced this:
irb(main):001:0> ">=#{Time.now - 5.days}"
=> ">=2021-02-28 14:35:44 +1100"
So when a time is stringified in Ruby, it doesn't just show up in the YYYY-MM-DD format.
So, to fix your API call, should format the date to YYYY-MM-DD and use end_time_after. Note, since you're using ActiveSupport, you can also call on 5.days.ago instead of Time.now - 5.days.
This should work for you:
#client.calls.list(to: phone_number, end_time_after: "#{5.days.ago.strftime("%Y-%m-%d")}")
Let me know if this helps at all.

Split datetime value received from external API in Rails app

I have a datetime value which comes from the API in this format: 2015-07-07T17:30:00+00:00. I simply want to split it up between the date and time values at this point. I am not using an Active Record model and I prefer not to use an sql database if I can.
The way I have set up the app means that the value is "stored" like this in my view: #search.dining_date_and_time
I have tried two approaches to solving this problem:
Manually based on this previous stackoverflow question from 2012: Using multiple input fields for one attribute - but the error I get is the attribute is "nil" even though I put a "try"
Using this gem, https://github.com/ccallebs/split_date_time which is a bit more recent and seems to be a more elegant solution, but after closely following the doc, I get this error, saying my Search model is not initalized and there is no method: undefined method dining_date' for #<Search not initialized>
This is when instead I put #search.dining_date in the view, which seems to be the equivalent of the doc's example (its not that clear). The doc also says the method will be automatically generated.
Do I need to alter my model so I receive the data from the API in another way? ie. not get the variable back as #search.dining_date_and_time from the Search model for any of this to work?
Do I need an Active Record model so that before_filter or before_save logic works - so i can (re)concatenate after splitting so the data is sent back to the API in a format it understands. Can I avoid this - it seems a bit of overkill to restructure the whole app and put in a full database just so I can split and join date/time as needed.
Happy to provide further details, code snippets if required.
As I am not using a conventional Rails DB like MySql Lite or Postgresql, I found that the best solution to the problem was by using this jQuery date Format plugin: https://github.com/phstc/jquery-dateFormat to split the date and time values for display when I get the data back from the API.
The Github docs were not too expansive, but once I put the simply put the library file in my Rails javascript assets folder, I just had to write a few lines of jQuery to get the result and format I wanted:
$(function() {
var rawDateTime = $('#searchDiningDateTime').html();
// console.log(rawDateTime);
var cleanDate = $.format.date(rawDateTime, "ddd, dd/MM/yyyy");
// console.log(cleanDate);
$('#searchDiningDateTime').html(cleanDate);
var cleanTime = $.format.date(rawDateTime, "HH:mm");
// console.log(cleanTime);
$('#searchTime').html(cleanTime);
});
Next challenge: rejoin the values on submit, so the API can read the data by sending/receiving a valid request/response. (The values can't be split like this when sent to the remote service).

Grails make one g:datePicker depend on other g:datePicker

imagine that you have an index.gsp with two dates: Starting day and Ending Day.
When the user picks the starting day, how can I calculate + 2 months in the ending day?
I know in the controller I could have something like:
use (TimeCategory) {
c = new Date() + 2.month
}
But how is the best way to change the Ending Day in the gsp? With remoteFunction?
EDIT:
Yes it worked, thank you very much lukelazarovic.
Like you said the only thing needed to change was the last line, I changed to: $("#myid_month").val(month+3);
function dateWasChanged(){
....
$("#myid_month").val(month+3);
}
EDIT2:
I also needed to increment year, for some reason they are strings so remember to do:
myInteger = parseInt(myString);
I assume you want to do it on the client - without reloading page +2 months date is set into the end date datePicker.
That means you have to do it using Javascript or Javascript-based framework (JQuery).
You can go along with the solution suggested in this SO question: Get Date value from Grails DatePicker
Only change would be to set the value of end datePicker on the last line of function dateWasChanged like this:
$("#endDatePickerId").datpicker("setDate", date);

Grails Timepicker

I need to save Date Time in the (oracle) database in one column, which is sqlType of timestamp (looks like 01-JAN-14 12.00.00.000000 AM). While learning grails I've been using the Joda lib with it's "time picker".
The Joda timepicker has worked well, but now that I'm looking to go primetime I'm looking for something a little more user friendly. Frankly, text boxes might be more user friendly than the drops downs joda gives you.
Anyway, I'd like to remove joda and use something like this:
http://trentrichardson.com/examples/timepicker/
but I can't figure out how to implement it in grails. In my view, if I put:
<input type="text" name="endDate" id="endDate" value="${exampleInstance?.endDate}" />
in place of the g:datePicker, it works fine (the picker that is), except nothing gets saved to the database, and no errors are generated. I hit Save and the Show view comes up with an empty endDate field. Do I need more input tags?
Is there some easy way to implement a modern looking date+time picker that I've missed?
Furthermore, I see there is a plugin for this picker here
http://grails.org/plugin/jquery-ui-timepicker
But being that there isn't any documentation, I'm not sure how to use that either (?)
ANSWER
in controller save/update put something like:
def endDate = params.date('endDate', 'yy-MM-dd h:mm')
//println "Date from Picker was "+endDate
params.endDate = endDate
No further casting was necessary being that it ended up I could format the datepicker control to a very close format as what's in the database, but had I needed to cast from one odd format, or a string, to another, I toyed with this code, which is more psuedo than anything as I was thinking through the process (I'm sure there's a totally Groovy way to do this same thing):
SimpleDateFormat inputFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss.S");
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy h.mm.ss.S a");
def v = params.endDate
Date date = inputFormat.parse(v);
String temp = sdf.format(date)
Date dateOut = sdf.parse(temp)
println dateOut
The datepicker, is your UI component therefore, you can have any library that you wish for UI and anything else for back-end. Mostly they are easy to implement, if they provide a little bit of documentation!!.
The timepicker for jQuery ui plugin, that you provided the link, is exposing a resource called jqueryUiTimePicker which depends on jQuery and jQuery-ui. So simply by including this resource into you resources configuration you should be able to utilize it. Its no different than defining your own resource and use it.
About saving issue that you have, on your save pass parameter failOnError:true so you can see the errors if any.
I have created a sample project that utilizes this plugin hope it helps
In your controller, you will need to parse the parameter value to a Date value. Something like,
def endDate = params.date('endDate', 'dd-MM-yyyy')
dd-MM-yyyy is whatever format the jquery plugin submits the date value. (println params for this or look up the plugin documentation)
If you want a date format binding to happen automatically, check the Date Formats For Data Binding in the doc http://grails.org/doc/latest/guide/single.html#dataBinding for a way to globally specify the format

Resources