TOP
Home > Blog > Concrete5: Store Dates in a Custom Block

Concrete5: Store Dates in a Custom Block

Posted by Derek on February 12, 2015

When creating a custom block in Concrete5 there are some great helper classes that allow you to quickly and easily create an easy to use form for the editor. This includes a date and date/time helper that will display a jQuery date picker. However, when adding and editing date fields there is one step that is not immediately obvious. The date has to be formatted correctly before it is saved in the database.

In this example we'll be dealing with a MySQL database where the date format is the standard YYYY-MM-DD.

Really there are two ways to accomplish this. One is to modify the Javascript date picker settings so that the date is in the correct format on the front-end before it is submitted. The other is to change the values in the controller. In this example we'll be using the latter.

First, create a custom bock with a date field.

db.xml
<?xml version="1.0"?>
<schema version="0.3">
  <table name="btEvent">
    <field name="bID" type="I">
      <key />
      <unsigned />
    </field>
    <field name="name" type="C2" size="50">
    </field>
    <field name="startDate" type="D">
    </field>
    <field name="endDate" type="D">
    </field>
  </table>
</schema>

This will give us a table with two DATE columns - startDate and endDate.

In our add and edit form we'll use the date_time helper to display date selectors.

<?php 
  defined('C5_EXECUTE') or die("Access Denied.");
  $form = Loader::helper('form');
  $dh = Loader::helper('form/date_time');
?>
<div id="ccm-block-fields">
  <div class="clearfix">
    <?php
      echo $form->label('name', t('Event Name'));
      echo $form->text('name', $bObj->name);
    ?>
  </div>
  <div class="clearfix">
    <?php
      echo $form->label('startDate', t('Start Date'));
      echo $dh->date('startDate', $bObj->startDate);
      $dh->translate('startDate');
    ?>
  </div>
  <div class="clearfix">
    <?php
      echo $form->label('endDate', t('End Date'));
      echo $dh->date('endDate', $bObj->endDate);
      $dh->translate('endDate');
    ?>
  </div>
</div>

By default this form will submit the date in MM/DD/YYYY format, which will be rejected by MySQL. To remedy this we simply need to override the save method in the block's controller.

controller.php
<?php defined('C5_EXECUTE') or die(_("Access Denied."));
  
class EventBlockController extends BlockController {
  
  protected $btTable = "btEvent";
  protected $btInterfaceWidth = "350";
  protected $btInterfaceHeight = "300";

  public function getBlockTypeName() {
    return t('Event');
  }

  public function getBlockTypeDescription() {
    return t('A simple block for displaying an event name and start/end date.');
  }

  public function save($args) {
    if (isset($args['startDate']))
      $args['startDate'] = date('Y-m-d', strtotime($args['startDate']));

    if (isset($args['endDate']))
      $args['endDate'] = date('Y-m-d', strtotime($args['endDate']));
    parent::save($args);
  }
}

Leave a Reply



(Your email will not be publicly displayed.)


Captcha Code

Click the image to see another captcha.