We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36544
    • 14 Posts
    I wonder if it's possible to change the setting for the pub_date field so it defaults to a certain pre-defined time when a date is selected, for example 9:00 am, instead of the current time.
    Any ideas if this is possible?

    This question has been answered by chrisderover. See the first response.

    [ed. note: chrisderover last edited this post 13 years, 10 months ago.]
      • 40045
      • 534 Posts
      I think it's possible, but how to do it is another story. What I know is that the pub_date is stored in the database as a unix timestamp, so the splitting up in date and time "pickers" happens in the manager itself and probably with extjs (which is nothing I can help anybody with...unfortunately). The closest I got when searching for the responsible code (for splitting up the timestamp into 2 parts) was in

      // /manager/assets/modext/widgets/resource/modx.panel.resource.js line 586
      {
                  xtype: MODx.config.publish_document ? 'xdatetime' : 'hidden'
                  ,fieldLabel: _('resource_publishdate')
                  ,description: '<b>[[*pub_date]]</b><br />'+_('resource_publishdate_help')
                  ,name: 'pub_date'
                  ,id: 'modx-resource-pub-date'
                  ,allowBlank: true
                  ,dateFormat: MODx.config.manager_date_format
                  ,timeFormat: MODx.config.manager_time_format // something here maybe??
                  ,startDay: parseInt(MODx.config.manager_week_start)
                  ,dateWidth: 120
                  ,timeWidth: 120
                  ,value: config.record.pub_date
              }
      


      but I don't know if that really would help because that would basically not be the problem if the timestamp was recalculated on saving the resource and just taking the submitted pub_date timestamp and setting it to that date, but on 9:00. So it depends if that 9:00 should be set initially (when no pub_date is set yet) or when one is set by the editor and then saved it should be "reset" to 9:00 (form whatever time the editor has set probably manually...).

      I'm not really sure if that is possible because it's not possible to set only a time with a unix timestamp (as far as I know), also when you just pick a time, it will automatically take the actual day.

      So what I would try to do is write a plugin that sets the pub_date on OnDocFormPrerender or something similar, the problems I see there are:


      1. the editor needs to check the pub_date every time he edits a resource to be sure it's set correctly.
      2. if a "custom" pub_date" is set, it would be reset to "today at 9:00" every time the resource is edited (could probably be solved by checking if pub_date is empty or not in the plugin and then take different actions ie take todays date with time 9:00 or take the already set date and modifiy only it's time to 9:00)
      3. don't see this work when creating new resources because you cannot access the fields of that new resource before it was saved to the db...

      but I would be interested to see your approach if you decide to implement a plugin like that =) [ed. note: exside last edited this post 13 years, 9 months ago.]
        • 3749
        • 24,544 Posts
        Yes, you could definitely do it with a plugin attached to OnDocFormSave.

        $timeString = $resource->get('pub_date') should get you the value. I'm not sure if you would have a human-readable date or a unix timestamp at that point.

        What you'd do next depends on which form it's in.


          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
        • discuss.answer
          • 36544
          • 14 Posts
          Stumbled upon my own post again after more than a year ;-)
          Lost it's priority back then, but a similar question popped up in my mailbox this week, and this time I managed to come up with a solution.

          The specific goal this time: Automatically set the time of the unpub_date field to 09:00 AM when selecting a date and saving the resource (By default the current time is entered and has to be changed manually).

          I came up with this solution, a plugin that executes OnDocFormSave:

          <?php
          $template = $resource->get('template'); //get template id
          $unpubdate = $resource->get('unpub_date'); //get current unpub_date
          if ( !empty($unpubdate) && $template == 11 ) { //only execute when an unpub_date has been entered AND the resource has the desired template
              $date = new DateTime($unpubdate);
              $date->setTime(9, 00, 00); //set desired time
              $newDate =  $date->format('Y-m-d H:i:s');
              $resource->set('unpub_date', $newDate);
              $resource->save();
              }


          This works for my case, though I'm by no means a php wizard, so please provide some feedback if this code can be improved (and it probably can).


            • 3749
            • 24,544 Posts
            It looks great. I don't think there's any way to make it significantly faster. You might shave a few milliseconds by reversing the terms in the test, depending on which is more likely, or using the code below, but I'm sure it wouldn't make a noticeable difference (if any).

            /* Only execute when an unpub_date has been entered AND the resource has the desired template */
            if ( empty($unpubdate) || $template != 11 ) { 
                return;
            } else {
                $date = new DateTime($unpubdate);
                $date->setTime(9, 00, 00); //set desired time
                $newDate =  $date->format('Y-m-d H:i:s');
                $resource->set('unpub_date', $newDate);
                $resource->save();
            }




              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting