Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 19th, 2012  |  syntax: None  |  size: 2.47 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # app/views/elements/form/input_datetime.ctp
  2. # This is a reusable bit to display the date/time inputs.
  3. <?php $model = isset( $model ) ? $model : $this->model ?>
  4.  
  5. <div class="input datetime<?php echo isset( $required ) && $required ? ' required' : '' ?>">
  6.   <?php echo $this->Form->input( $model . '.' . $field . '.date' , array( 'type' => 'text', 'label' => $label, 'value' => !empty( $value ) ? date( DATE_FORMAT_LONG, strtotime( $value ) ) : '', 'div' => false, 'class' => 'date' ) ) ?>
  7.   <?php echo $this->Form->input( $model . '.' . $field, array( 'type' => 'time', 'label' => false, 'div' => false, 'class' => 'time', 'empty' => true ) ) ?>
  8. </div>
  9.  
  10. # app/views/<wherever>/<whatever.ctp>
  11. # The following code should go within your form
  12. <?php echo $this->element( 'form/input_datetime', array( 'field' => 'start_time', 'label' => 'Start Time', 'value' => $this->data['Event']['start_time'], 'required' => true ) ) ?>
  13.  
  14. # app/app_model.php
  15. # The comments on this method pretty much describe what it does
  16. /**
  17.  * Override Model::deconstruct() in order to use an integrated date
  18.  * value, but multipart time value. The parent method expects both
  19.  * date and time to be segmented, but, if a date picker is used to
  20.  * select the date, then that component is unified.
  21.  *
  22.  * In order to use what's already in place, we'll deconstruct the date
  23.  * portion here and then pass everything to the parent method for
  24.  * reassimilation.
  25.  *
  26.  * @param  string $field  The name of the field to be deconstructed
  27.  * @param  mixed  $data   An array or object to be deconstructed into a field
  28.  * @return mixed          The resulting data that should be assigned to a field
  29.  */
  30. public function deconstruct( $field, $data ) {
  31.   $type = $this->getColumnType( $field );
  32.  
  33.   if( in_array( $type, array( 'datetime', 'timestamp' ) ) ) {
  34.     if( isset( $data['date'] ) && !empty( $data['date'] ) ) {
  35.       $date = date( 'U', strtotime( $data['date'] ) );
  36.  
  37.       if( $date ) {
  38.         $data['month'] = date( 'm', $date );
  39.         $data['day']   = date( 'd', $date );
  40.         $data['year']  = date( 'Y', $date );
  41.       }
  42.     }
  43.   }
  44.  
  45.   return parent::deconstruct( $field, $data );
  46. }
  47.  
  48. # app/webroot/js/<somewhere>/<something>.js
  49. # Add this to engage the picker.
  50. /**
  51.  * Set the datepicker date format, where applicable.
  52.  */
  53. if( $('.date').length ) {
  54.   $('.date').datepicker({ dateFormat: 'MM d, yy' });
  55. }
  56.  
  57. if( Date.format ) {
  58.   Date.format = 'm/d/yyyy';
  59.   $('.date input')
  60.     .datePicker( { clickInput: true, createButton: false } )
  61.     .val( new Date().asString() );
  62. }