I need to send out a reminder email the DAY BEFORE a Calendar Event as well as the DAY AFTER. Unfortunately, I can't use Rules Scheduler, because the Tokens can't be manipulated with PHP. It doesn't work if I have
[node:field_event_date-datetime] -1 day
as the scheduled time.
What I've ended up doing is creating two "dummy" date fields for DAY BEFORE and DAY AFTER, and I'm trying to hook into the form, grabbing the event date, using some PHP like strtotime() to add/subtract a day, and make these the values that would go into the database.
I've tried linking to the #submit part of the form, but in phpMyAdmin, all values are NULL.
For this code i haven't even changed the date, I'm just trying to get values to appear in the database.
function mymodule_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == "event_node_form") {
$form['#submit'][] = '_mymodule_after_build';
// Makes the fields invisible
$form["field_event_day_before"]["#access"] = FALSE;
$form["field_event_day_after"]["#access"] = FALSE;
}
}
function _mymodule_after_build($form, &$form_state) {
$eventcopy = array();
// copy the value part from the Event
$eventcopy = $form['field_event_date'][0]['#value'];
// without doing any PHP yet, simply copy the values. Doesn't show up in db.
$form['field_event_day_before'][0]['#value'] = $eventcopy;
dsm($form);
return $form;
}
I've read the tutorials about using Rules Scheduler with CCK and
I'm also following Schedule email to go out based on CCK date field - not working for me
Am I using the right hooks? How do I intercept the inputted date value properly?
I don't think you are approaching your problem the correct way. If you want to try to go down the path you are proposing then you would want to look at hook_nodeapi(). You can add some code for the 'insert' and/or 'save' (or maybe even 'presave') operations so you can update your $node->field_event_day_before'][0]['#value'] and $node->field_event_day_after'][0]['#value'] fields based on the event_date value.
However, you really don't want to add extra fields for date before and date after when you can just calculate those from the event_date.
What I think the better solution is to just implement hook_cron() and have that function handle querying for all events in your database whose event day is TODAY() +1. For all those results, send out an email. Do another query that looks for any event whose event_date is TODAY() - 1 and send out an email for those results. You'll want to make sure you only run this process once in every 24 hour period.
I want to share the answer, thanks to help from the community. If you run into this same problem, try this:
function mymodule_form_event_node_form_alter(&$form, &$form_state) {
// hide these dummy fields, will fill in programatically
$form["field_event_day_before"]["#access"] = FALSE;
$form["field_event_day_after"]["#access"] = FALSE;
}
function mymodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){
switch ($op) {
//if the node is inserted in the database
case 'insert':
if($node->type == 'event') {
// Day before (+10 hours because I'm in Hawai`i, far from GMT)
$daybefore = strtotime('-1 day +10 hours', strtotime($node->field_event_date[0]['value']));
$daybefore = date('Y-m-j\TH:i:s', $daybefore);
$node->field_event_day_before[0]['value'] = $daybefore;
// Day after (+10 hours because I'm in Hawai`i)
$dayafter = strtotime('+1 day +10 hours', strtotime($node->field_event_date[0]['value']));
$dayafter = date('Y-m-j\TH:i:s', $dayafter);
$node->field_event_day_after[0]['value'] = $dayafter;
}
}
}
The rules scheduler can then take tokens from the day_before/day_after fields, and you can use their interface for scheduling.
You can do this by using the rules module, i did this in my one project, basically you have to create two rules, one for one day before, and another for one day after. Let me know if you want any clarification.
Thanks
K
Related
Because selectOverlap function only passes through the event being overlapped, and not the selection, it is difficult to customise how to handle event creation.
In my case, we are working on a calendar/diary system, with background events showing the employee's shifts, and events showing their individual bookings.
At this point, other than the background events - absolutely no events should be able to overlap each other.
However... on top of that - we are then overlaying 'all day events' - which might be any number of things, but for examples sake, let's say they're 'staff birthdays' - therefore, you might have a couple of events today, but there will be an event in the all day section, showing someone's birthday.
I am checking for eventOverlap and doing some other checks on eventDrop and eventResize which handle different clashes, but these only work for existing events being moved or resized. I would like to do the same on event creation - which happens during the select. In order to disallow the select of spaces which already have events, I am using the example function from the selectOverlap documentation:
function(event) {
return event.rendering === 'background';
}
This works fantastically. However, if I try to create a new All Day event, it will 'overlap' any other events that exist on that day, and not pass this check.
I was hoping to be able to use the selection's object to check it for an allDay=true, but the function is only passed the existing event, and there is no way to check the selected section.
You can see a very simplified demo here:
https://codepen.io/anon/pen/NQrxOO
Try to create an allday event on the day which already has events.
Is there a better way to do this? I can completely remove the selectOverlap and do everything in the select callback instead, but I would need to essentially duplicate the overlap checks just to make this work, and I feel like that seems like overkill for something that should be relatively simple.
Is it possible to get not only the overlapped event object, but also the selection object when doing a selectOverlap function?
Current workaround is to remove the selectOverlap check, and instead do it myself within the select callback.
Below is a simplified version of a quick function I wrote to call when using select={this.handleEventCreate}:
class Diary extends React.Component {
//additional functions, state definitions, etc etc etc here.
//Define calendarRef as it will be needed in the function below
calendarRef = React.createRef();
handleEventCreate = (info) => {
// Get the Calendar API to allow unselection
let calendarApi = this.calendarRef.current.getApi();
// Get all Events
let checkEvents = calendarApi.getEvents();
// Loop through them
checkEvents.forEach(function(event){
// If it's not a background element, check whether the new element contains the existing, or vice versa.
if(event.rendering !== "inverse-background" &&
(
(event.start >= info.start && event.start <= info.end) ||
(event.end >= info.start && event.end <= info.end) ||
(info.start >= event.start && info.start <= event.end) ||
(info.end >= event.start && info.end <= event.end)
)
){
// It is an overlapping event, so we reject it.
return calendarApi.unselect();
}
});
alert('All good here to create the event.');
//extra event creation code to fire here.
}
I'm using Fullcalendar and I need to allow event overlap for one day: the last still event day can overlap on the first new event day.
For resizing and drop I resolved in this way:
eventOverlap: function(stillEvent, movingEvent) {
var a = movingEvent.start.startOf('day');
var b = stillEvent.end.startOf('day');
if(a.unix() == b.unix()){
return true;
}
return false;
}
Now I need to do the same on selecting, according to documentation, I have to use selectOverlap option by define a function, but differently of eventOverlap, this function not have an argument that include the current selection period, then I don't know how to make the check.
Somebody can say me how to do so?
UPDATE 19/05/2017:
I opened an issue on github's project repository here
I have three fields created via ACF with Datepicker: event start date, event end date and survey start date.
What I want to achieve is when I set date via datepicker on event start date, then this date is copied to event end date and survey start date.
I googled almost everything and no code is working. Actually, the nearest working solution is below:
acf.add_action('load', function( $el ){
var $field_start_date = $el.find('.acf-field-5800010541984');
var $field_end_date = $el.find('.acf-field-5800014941985 .input-alt');
$field_start_date.change(function() {
var $field_start_date_value = $('#acf-field_5800010541984').val();
$('#acf-field_5800014941985').datepicker( 'setDate', $field_start_date_value );
});
Value is copied to another field (event end date) – attribute value is changing – but copied value doesn't show on input.
BTW, $('#acf-field_5800014941985').datepicker('update'); doesn't work too.
Like Leeloo in The Fifth Element – "Pleeeeeeeeeeeeeese. Heeeeeeeeeeeeeelp"
Best regards,
Milosz!
When we recieve orders from web it creates a sales id and stores it. But if i recieve order from web at same time in two instances, it creates two sales orders for the same web order. So how can i stop it?
I kept as Index for weborder number Allow Duplicates:No. But still it doesnt work. Any Suggestions?
(Added as a answer bit late, 'cause I'm slow that way :))
Send a unique identifier like a GUID from the web, save it in SalesTable and in insert check if it already exists - or make a unique index for the field, but you might log these attempted duplicates and it's easier to code it yourself in insert or validateWrite.
This is because the user presses submit button several times. You need to track the number of clicks on the button. For this you need to use js.
var submit = 0;
function checkIsRepeat(){
var isValid = Page_ClientValidate();
if(isValid) {
if(++ submit > 1){
alert('Yours message here');
return false;
}
}
return isValid;
}
I'm trying to show updated results for a CCK Computed Field.
The computation is based on fields in another node, so are not being automatically updated.
So: I'm calling node_save($node) in hook_view, which does make the adjustment but the results don't show until I refresh the page.
Is there a way to refresh the page automatically, or should I be approaching this from a different angle?
Edit: In response to Henrik's questions, here's more detail:
The hook_view and its node_save are below, the rest of the code is in a Computed Field in the 'project' content type, summing up values from another node. Without the node_save, I have to edit and save the 'project' node to get the result. With it, I just need to refresh the page.
Adding drupal_goto(drupal_get_destination()) in the hook_view gives a 'page not found', rather than the vicious loop I was expecting. Is there another place I could put it?
function mymodule_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
switch ($op) {
case 'view':
if($node->type == 'project') {
project_view($node);
break;
}
}
}
function project_view($node) {
node_save($node);
return $node;
}
Edit 1: Given the newly posted code and additional explanations, I have three suggestions that might solve the problem without redirecting:
As project_view() does not take the node argument by reference, you might want to actually grab its (potentially updated) result in mymodule_nodeapi by writing
$node = project_view($node);
instead of just
project_view($node);
If that works, it should also work without the indirection via project_view() by just calling node_save($node) directly in mymodule_nodeapi. (node_save() takes the node argument by reference).
AFAIK, computed fields basically provide two working modes that you can switch via checkbox on the field configuration form:
Computing the field once on node_save(), storing the result in the database, updating only on new save operations.
Not storing the field at all, instead recomputing it every time the node is viewed.
Have you tried the 'always recompute' option already?
Edit 2: My original answer was flawed in two ways at once, as it used a completely wrong function to retrieve the current request URI and did not check for recursion (as lazysoundsystem pointed out very courteously ;)
So the following has been updated to an actually tested version of doing the redirection:
Is there a way to refresh the page
automatically
You could try:
if (!$_REQUEST['stop_redirect']) {
drupal_goto(request_uri(), array('stop_redirect' => true));
}
This will cause Drupal to send a redirect header to the client, causing a new request of the current page, making sure not to redirect again immediately.
If the value is only ever going to be computed, you could just add something to your node at load time.
function mymodule_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
switch ($op) {
case 'load':
if($node->type == 'project') {
$node->content['myfield'] = array('#value' => mymodule_calculate_value(), '#weight' => 4, '#theme' => 'my_theme');
}
break;
}
}
}