Problems, need help? Have a tip or advice? Post it here.
3 posts Page 1 of 1
In list view in the calendar, how do I access a global variable calculated in a <cms:php> section?

Background: I'm using Couch to set up a calendar for special events on a restaurant's website. For easy data entry and for display to the public, I'm using informal times in text such as 10am, 10:30am, 11am, etc.

I want to provide an "Add to Calendar" button for site visitors using addtocalendar.js from http://addtocalendar.com. This requires a datestamp formatted as Y-m-d H:i:s, e.g., 2017-06-23 14:30:00. I also want a "Make Reservation" button going to Opentable, which needs the time formatted more or less similarly.

In my template, I can use <cms:php> and global and CTX commands to convert the informal time to a global variable with the exact formatted time (modified from viewtopic.php?f=4&t=8173&view=next#p14479). When I display an individual page, I simply combine the Y-m-d of the event date with the H:i:s of the formatted time and this works fine. This method also works on an archive page where I list all the events for the month (without any calendar grid).

I like offering both the archive list view and the calendar grid view, and want to allow site visitors to switch between the two. I set up buttons to do this, but I didn't like the slowness of loading a page whenever I clicked on a button to switch to the other view. A much faster alternative is to use the "list view," i.e., calendar grid view, as the basis and have a javascript button that simply adds a CSS class to the calendar div that will override the table, hide days with no events, and present the events in a list similar to archive view. This is almost instantaneous, a huge improvement over the slowness of loading another page.

Where I'm running into trouble is accessing the calculated global variables for start_hour2 and end_hour2 within "list view". I copied the <cms:php> section that I used for page view, made unique variable names, and placed it within the <cms:entries> section and set skip_custom_fields=0. Here is a segment of code where start_time and end_time are the informal times and start_hour2 and end_hour2 are the calculated global variables I need:

Code: Select all
      <cms:set start_hour2='700' />
      <cms:set end_hour2='800' />
      <cms:php>
         global $CTX;
         $start_time_d2 = strtotime("<cms:show start_time />");
         $end_time_d2 = strtotime("<cms:show end_time />");
         $CTX->set( 'start_hour2', date('H:i:s', $start_time_d2), 'global' );
         $CTX->set( 'end_hour2', date('H:i:s', $end_time_d2), 'global' );
         echo "PHP Start Time: <cms:show start_time /> | ";
         echo 'Unix Time: ' . $start_time_d2 . ' | ';
      </cms:php>
      <cms:dump />

The PHP is working, as evidenced by echoing the variables within the PHP, including the accurate conversion from the informal time (start_time) to Unix Time (start_time_d2).

As in the code segment, when I have <cms:dump /> following the <cms:php> section, I see only the initial values set for start_hour2 (700) and end_hour2 (800) prior to <cms:php> instead of the values calculated within <cms:php> (dump list header is "entries").

To further confound this, I have more evidence that the <cms:php> is calculating the global variables, but I simply can't access them: When I include <cms:dump /> after </cms:calendar>, I see start_hour2 and end_hour2 with the correctly calculated and formatted times for the last event in the month (dump list header is "__ROOT__").

I have another project in the works that will require a calendar and an "Add to Calendar" button, so I'd like to get this figured out.

I'm stumped. Any suggestions?

Thanks,
Tim
Hi,

Please try setting the same scope (i.e. 'global') for the initial statements too -
Code: Select all
<cms:set start_hour2='700' 'global' />
<cms:set end_hour2='800' 'global' />
<cms:php>
    ...

Please let me know if this helps.
It worked perfectly after setting the same scope. Thanks, KK!
3 posts Page 1 of 1