Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
11 posts Page 1 of 2
I understand from other posts in the forum that while using and editable field for an event start date is not ideal, but that it is possible.

My client would rather not have to go to advanced settings each time. (Their adding a lot of dates) How would one go about doing this, so that it works with the calendar entries tag?

Also in the course of trying a work around for this I found that jquery in a template <cms: editable> field with the message type is not agreeable to the admin panel. Is this right?

Thanks!
For the jquery part try putting this into a type message:

Code: Select all
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
jQuery.noConflict();
(function($) {
    //use '$' as you normally would
    $(document).ready(function() {


    });
})(jQuery);

</script>
Hello and welcome, skribe :)

As @lb rightly mentioned above, the noconfict setting allows us to use both jQuery and mootools together. If you need one more example please see viewtopic.php?f=2&t=4087&p=5333#p5333.

As for the date problem you mentioned -
I agree with you that using the advanced setting dropdown each time for setting a start date can be a chore.

So let us now make it easier :)

The plan is that we define an editable region (with the datepicker if required) for the start_date too. However, while saving the page, we insert this editable region's value into the page's publish_date automatically.
This way we get the best of both the worlds - the client can set the date into a normal editable region and we can use the (much more efficient) publish date while listing the events.

This is how we do it -
Please copy and paste the following PHP code in your site's (or the one found in 'couch/addons' folder) kfunctions.php.
Code: Select all
class MyEvent{
    function start_date( $field ){
        global $FUNCS;
       
        $start_date = trim($field->get_data());
        if( !preg_match('/(?:19|20)\d\d-(?:0[1-9]|1[012])-(?:0[1-9]|[12][0-9]|3[01])/', $start_date ) ){
            return KFuncs::raise_error( "Incorrect date format" );
        }
       
        // Set publish_date to value of this field
        $f = &$field->siblings[3];
        $publish_date = trim( $f->get_data() );
        if( $publish_date != '0000-00-00 00:00:00' ){
            $start_date = $FUNCS->make_date( $start_date );
            if( $publish_date != $start_date ){
                $f->store_posted_changes( $start_date );
            }
        }
        unset( $f );
       
        return true;
    }
}

Then define the editable region for the start_date this way -
Code: Select all
<cms:editable 
    name='start_date'
    label='Start Date'
    desc='Enter date in yyyy-mm-dd format e.g. 2012-12-31'
    type='text'
    validator='MyEvent::start_date'
    separator='#'
    validator_msg='myevent::start_date=Incorrect date format'
    required='1'
    width='155'
/>

Notice how we are associating the PHP code we pasted into kfunctions.php as step one with this editable region via its 'validator' parameter. So each time a page containing this editable region is saved, the validator code kicks in. It validates the date's format and more importantly also pushes this date into the page's system field that holds the publish_date (this is the one we access using the 'Advanced setting' dropdown).

This saves the client from having to go into the advanced setting.

Hope this helps.
Do let us know.

Thanks.

EDIT:
Instead of using a type 'text' editable region, as we did above, we can use a type 'datetime' editable region instead -
date.jpg
date.jpg (4.12 KiB) Viewed 1175 times

Please see viewtopic.php?p=25098#p25098 for details.
Great! Thank you both for the solutions. I had not run across the kfunctions file yet. I will make use of that in the future for sure! I have never really use moo tools before, and was not aware there was a conflict between the two.

Coming from years of working with Wordpress, Couch is a breath of fresh air for small sites! Yet I can't believe how powerful it is.
I added the code to my kfunctions file & template, and it worked first crack out of the box. :) Thanks again! Perhaps a link should be added to this from the tips and tricks section?
I'm interested in using this and indeed extending it. I was planning to post seeking to check my understanding, but as I went on I got more confused about what some of the code is doing!

I'd like to give the site manager two options for event date/time.
1. A datetime field "start_date"(for occasional events)
  • If the datetime field is completed, then I'd like to set the publish date/time to that value.

2. A text field "regular_start" which can contain a string like 'First Thursday of the month' (for regular events)
  • If the date/time is not completed, then I'd like to set the publish date/time to that day of the month in August 2010
  • (Since that month started on a Sunday, this will mean that dates in the format 'First Wednesday', Second Thursday' etc will be in the natural order through the month)
  • PHP's strtotime function can handle "First Thursday of August 2010' and output a valid PHP date

This means that the site manager can enter all events using the same form, and I can output lists of regular events and occasional events, with each list logically ordered.

Given that I've got either (1) a valid datetime from a Couch editable field, or (2) a valid PHP date, can anyone help me with the code I need to get that value into the publish date field? (Part of the original code is checking for validity of a date in a text field).
Replying to my own post; I've done some work on this, and I have what seems to be working code - but also a question.

My events form has two fields for the date - a text field for the 'regular monthly' sort , which will contain something like 'Second Saturday', and a datetime field for events with actual start dates. This code will work with either - but if I attach this as a validator to both fields,
  • it will raise an error if the 'regular monthly' field is blank,
  • and (assuming it works through the fields in order) it will override any 'regular monthly' field with the default datetime content anyway
.

I can deal with the first issue by explicitly ignoring blanks, but I can't see how to deal with the second one - to validate the second field only if the first is blank. That issue wouldn't be resolved by separate validators either.

Here's the code:
Code: Select all
class MyEvent{
    //Function to take a date and push it into the published date field of an entry
    //Accepts a standard date from a date picker, or a phrasing such as 'First Sunday' for monthly recurring events
    function start_date( $field ){
        global $FUNCS;
       
      //  Get the contents of the field and clean it up a bit
      $start_date = trim($field->get_data());
      
      // If it doesn't start with two digits, it's not come from a date picker, so try treating it as a regular monthly event
      // That is, append ' August 2010' so that strtotime will order such dates in a logical way, using a Sunday-start week, because 1 Aug 2010 was a Sunday.
      if ((strlen($start_date)>0) && (!is_numeric(substr($start_date, 0, 2)))){
            $start_date .= " August 2010";
         }
      
      //  Find out whether strtotime can make sense of it.  If strtotime returns 'false', report an error,
      $derived_date = strtotime($start_date);
      if(!$derived_date) {
        return KFuncs::raise_error( "Incorrect date format" );
         }
      
      // Put the strtotime output into SQL date format
      $date_string  = date("Y-m-d H:i:s", $derived_date );
       
        // Insert the result into the published date field
        $f = &$field->siblings[3];
        $publish_date = trim( $f->get_data() );
        if( $publish_date != '0000-00-00 00:00:00' ){
            if( $publish_date != $date_string ){
                $f->store_posted_changes( $date_string );
            }
        }
        unset( $f );
      
        return true;
    }
}
I understood the first problem from your first post: copy datetime editable to the 'publish_date'.
Can I please ask something about the second problem?

daldred wrote:
2. ... 'First Thursday of the month' (for regular events)
If the date/time is not completed, then I'd like to set the publish date/time to that day of the month in August 2010



A side note: regular (or recurrent) events are not yet covered by core tags, but still we can use regular cms:pages tag to find such events and embed them into calendar view (although it requires deeper knowledge how "cms:calendar, cms:week, cms:days, cms:entries" tags work).

So what I don't yet understand is if client puts "first monday of the month" (which is more advanced than just "on mondays" - how this event becomes recurrent in your plan? Suppose, you achieve what you do in php and this 'text representation' gets converted to one real date and gets pushed to 'publish_date' of the page. Then what?

I'm deeply interested in this conversation, because another friend asked me to code this "recurrent events" thing and I planned to handle it exclusively on the front-end (as in the side note above).
Maybe we could move this topic to a separate thread?
I'm not actually planning to use the calendar as such with this - or at least I wasn't!

A little background: I'm making a website for the regional presence of an organisation which is organised on a local basis. Each local group will have a couple of regular meetings per month, and in addition other events are organised, either at group or regional level, which can be thought of as 'special' events. All the fields for both sorts of events are the same, with the exception of how dates are handled.

So I'm approaching this by having separate listings - one for the regular ones, and one for the special ones - and for this group that's quite a natural way of thinking of them. Each listing should being in a logical order, but they aren't shown in the same list. The fact that the supposed actual dates for the regular events listing is in 2010 doesn't matter, as long as they are in a good order - I don't expose the 'published date' anywhere, just sort by it.

It would be absolutely ideal if there was a way of handling regular events so that they organise themselves into the calendar. Though, thinking about it, if you were only showing a month of calendar at a time, PHP's relative date formats could do quite a bit of the heavy lifting; once you'd put in the actual dated events, you'd then iterate through the regular ones converting each to a date this month, and inserting the result into the calendar.
daldred wrote: I'm not actually planning to use the calendar as such with this - or at least I wasn't!
...
Though, thinking about it, if you were only showing a month of calendar at a time, PHP's relative date formats could do quite a bit of the heavy lifting; once you'd put in the actual dated events, you'd then iterate through the regular ones converting each to a date this month, and inserting the result into the calendar.


What I meant about calendar system was well described in docs - http://docs.couchcms.com/concepts/events-calendar.html
That Concept is based exclusively on 'publish_date'. However, in the view of modern CouchCMS 2.0+ you may configure admin's list-vew as far as you'd like, including showing (and ordering by) your custom date editable in the column of publish_date. Also publish_date itself may be just placed anywhere in the form-view. This particular example was mentioned in "CouchCMS v2.0" topic. viewtopic.php?f=5&t=10241#p24680
Code: Select all
<cms:config_form_view>
    <cms:field 'k_publish_date' group='_custom_fields_'  />
</cms:config_form_view>



Now, looping through all regular events and make the date converted into this month's date looks as an interesting approach.
Did you happen to look at this topic with relative dates? A lot of cases in CouchCMS are perfectly covered and do not require php code at all. viewtopic.php?f=8&t=10559#p28383
What I mean is that if you focus on front-end and try to use regular editables and make the heavy-lifting with core tags, it may suffice, don't you think?

Events that occur more than twice a month or week might need special attention, but hopefully nothing that could require raw php. Splitting a comma-separated string 'first monday this month, second tuesday this month' is a breeze with "cms:each sep=',' " and maybe even have repeatable regions to introduce several appearances of event as rows instead of comma-separated string...

P.S. In same Couch2 topic you may even borrow the example of setting publish_date and instead save to it your editable with date (which may also be dynamic if needed). The current thread we are in is from 2013, so it is outdated and meant for older V1.4.7 which did not support extensive customization features of modern CouchCMS..

Code: Select all
    <cms:persist
        k_publish_date="<cms:if "<cms:not frm_your_custom_editable_here />" >0000-00-00 00:00:00<cms:else/><cms:show frm_your_custom_editable_here /></cms:if>"
        _auto_title='1'
    />
11 posts Page 1 of 2

Who is online

In total there are 3 users online :: 0 registered, 0 hidden and 3 guests
(based on users active over the past 5 minutes)

Users browsing this forum: No registered users and 3 guests