gavinhewitt
1/27/2014 - 8:04 AM

gistfile1.php

<?php

  Form::macro('select_year', function($field_name, $date=null, $options = array(), $html_options = array() ) {
    $date = DateHelper::normalize_datetime($date);

    $current_year  = date_format($date, 'Y');
    $start_year    = isset( $options["start_year"] ) ? $options["start_year"] : $current_year  - 5;
    $end_year      = isset( $options["end_year"] ) ? $options["end_year"] : $current_year  + 5;
    $prompt        = isset( $options["prompt"] ) ? $options["prompt"] : false;
    $include_blank = isset( $options["include_blank"] ) ? $options["include_blank"] : false;

    $year_range = array();

    // Reverse Order
    if ( $start_year > $end_year ) {
      for ($i=$end_year; $i <= $start_year; $i++) {
        $year_range[$i] = $i;
      }

      if($include_blank) {
        $year_range[""] = "";
      }

      if( $prompt ) {
        $year_range[] = $prompt;
        $current_year = 0;
      }
      $year_range = array_reverse($year_range);
    }
    else {

      if($include_blank) {
        $year_range[""] = "";
      }

      if( $prompt ) {
        $year_range[] = $prompt;
        $current_year = 0;
      }
      for ($i=$start_year; $i <= $end_year; $i++) {
        $year_range[$i] = $i;
      }
    }

    return Form::select($field_name, $year_range, $current_year, $html_options);

  });


  Form::macro('select_month', function($field_name, $date=null, $options = array(), $html_options = array() ) {
    $date = DateHelper::normalize_datetime($date);

    $lang_date_file = DateHelper::lang_file();

    $current_month     = date_format($date, 'n');
    $use_month_numbers = isset( $options["use_month_numbers"] ) ? true : false;
    $add_month_numbers = isset( $options["add_month_numbers"] ) ? true : false;
    $use_short_month   = isset( $options["use_short_month"] ) ? true : false;
    $prompt            = isset( $options["prompt"] ) ? $options["prompt"] : false;
    $include_blank     = isset( $options["include_blank"] ) ? $options["include_blank"] : false;

    $month_range = DateHelper::prepare_range($current_month, $include_blank, $prompt);

    for ($i=1; $i <= 12; $i++) {
        if ( $use_month_numbers ) {
          $month_range[$i] = $i;
        }
        else if( $add_month_numbers ) {
          $month_range[$i] = $i . " - " . $lang_date_file['month_names'][$i-1];
        }
        else if( $use_short_month ) {
          $month_range[$i] = $lang_date_file['abbr_month_names'][$i-1];
        }
        else {
          $month_range[$i] = $lang_date_file['month_names'][$i-1];
        }

    }

    return Form::select($field_name, $month_range, $current_month, $html_options);

  });

  Form::macro('select_day', function($field_name, $date=null, $options = array(), $html_options = array() ) {
    $date = DateHelper::normalize_datetime($date);

    $current_day   = date_format($date, 'j');
    $prompt        = isset( $options["prompt"] ) ? $options["prompt"] : false;
    $include_blank = isset( $options["include_blank"] ) ? $options["include_blank"] : false;

    $day_range = DateHelper::build_standard_range(1, 59, $current_day, $include_blank, $prompt);

    return Form::select($field_name, $day_range, $current_day, $html_options);

  });

  Form::macro('select_hour', function($field_name, $date=null, $options = array(), $html_options = array() ) {
    $date = DateHelper::normalize_datetime($date);

    $lang_date_file = DateHelper::lang_file();

    $current_hour   = date_format($date, 'G');
    $prompt         = isset( $options["prompt"] ) ? $options["prompt"] : false;
    $include_blank  = isset( $options["include_blank"] ) ? $options["include_blank"] : false;

    $hour_range = DateHelper::build_standard_range(1, 59, $current_hour, $include_blank, $prompt);

    return Form::select($field_name, $hour_range, $current_hour, $html_options);

  });

  Form::macro('select_minute', function($field_name, $date=null, $options = array(), $html_options = array() ) {
    $date = DateHelper::normalize_datetime($date);

    $lang_date_file = DateHelper::lang_file();

    $current_minute = date_format($date, 'i');
    $prompt         = isset( $options["prompt"] ) ? $options["prompt"] : false;
    $include_blank  = isset( $options["include_blank"] ) ? $options["include_blank"] : false;

    $minute_range = DateHelper::build_standard_range(1, 59, $current_minute, $include_blank, $prompt);

    return Form::select($field_name, $minute_range, $current_minute, $html_options);

  });

  Form::macro('select_second', function($field_name, $date=null, $options = array(), $html_options = array() ) {
    $date = DateHelper::normalize_datetime($date);

    $lang_date_file = DateHelper::lang_file();

    $current_second = date_format($date, 's');
    $prompt         = isset( $options["prompt"] ) ? $options["prompt"] : false;
    $include_blank  = isset( $options["include_blank"] ) ? $options["include_blank"] : false;

    $second_range = DateHelper::build_standard_range(1, 59, $current_second, $include_blank, $prompt);

    return Form::select($field_name, $second_range, $current_second, $html_options);

  });

  Form::macro('select_date', function($field_name, $date=null, $options = array(), $html_options = array() ) {
    $date = DateHelper::normalize_datetime($date);

    $current_month  = date_format($date, 'n');
    $current_year   = date_format($date, 'Y');
    $lang_date_file = DateHelper::lang_file();

    $discard_day    = isset( $options["discard_day"] ) ? true : false;
    $discard_month  = isset( $options["discard_month"] ) ? true : false;
    $discard_year   = isset( $options["discard_year"] ) ? true : false;
    $order          = isset( $options["order"] ) ? $options["order"] : $lang_date_file["order"];

    $day_options    = $options;
    $month_options  = $options;
    $year_options   = $options;

    if ( isset( $options["prompt"] ) ) {
      if (is_array( $options["prompt"] ) ) {
        $day_options["prompt"]   = isset($options["prompt"]["day"]) ? $options["prompt"]["day"] : false ;
        $month_options["prompt"] = isset($options["prompt"]["month"]) ? $options["prompt"]["month"] : false ;
        $year_options["prompt"]  = isset($options["prompt"]["year"]) ? $options["prompt"]["year"] : false ;
      }
      else if ( $options["prompt"] == true ) {
        $day_options["prompt"]   = $lang_date_file["prompts"]["day"];
        $month_options["prompt"] = $lang_date_file["prompts"]["month"];
        $year_options["prompt"]  = $lang_date_file["prompts"]["year"];
      }
    }

    $selects = [];

    $selects["day"]   = Form::select_day('day_'.$field_name, $date, $day_options, $html_options);
    $selects["month"] = Form::select_month('month_'.$field_name, $date, $month_options, $html_options);
    $selects["year"]  = Form::select_year('year_'.$field_name, $date, $year_options, $html_options);

    if ( $discard_day ) {
      $selects["day"]   = Form::hidden('day_'.$field_name, 1, $html_options);
    }

    if ($discard_year) {
     $selects["year"]   = Form::hidden('year_'.$field_name, $current_year, $html_options);
    }

    if ( $discard_month ) {
      $selects["day"]   = Form::hidden('day_'.$field_name, 1, $html_options);
      $selects["month"] = Form::hidden('month_'.$field_name, $current_month, $html_options);
    }

    $select_date = "";
    if (count($order) != 3 ) {
      $order = array("day", "month", "year");
    }
    foreach ($order as $value) {
      $select_date .= $selects[$value];
    }

    return $select_date;

  });

  Form::macro('select_datetime', function($field_name, $date=null, $options = array(), $html_options = array() ) {

    $date = DateHelper::normalize_datetime($date);

    $current_hour   = date_format($date, 'G');
    $current_minute = date_format($date, 'i');
    $current_second = date_format($date, 's');

    $lang_date_file = DateHelper::lang_file();

    $discard_hour   = isset( $options["discard_hour"] ) ? true : false;
    $discard_minute = isset( $options["discard_minute"] ) ? true : false;
    $discard_second = isset( $options["discard_second"] ) ? true : false;

    $hour_options   = $options;
    $minute_options = $options;
    $second_options = $options;

    if ( isset( $options["prompt"] ) ) {
      if (is_array( $options["prompt"] ) ) {
        $hour_options["prompt"]   = isset($options["prompt"]["hour"]) ? $options["prompt"]["hour"] : false ;
        $minute_options["prompt"] = isset($options["prompt"]["minute"]) ? $options["prompt"]["minute"] : false ;
        $second_options["prompt"] = isset($options["prompt"]["second"]) ? $options["prompt"]["second"] : false ;
      }
      else if ( $options["prompt"] == true ) {
        $hour_options["prompt"]   = $lang_date_file["prompts"]["hour"];
        $minute_options["prompt"] = $lang_date_file["prompts"]["minute"];
        $second_options["prompt"] = $lang_date_file["prompts"]["second"];
      }
    }

    $select_date   = Form::select_date( $field_name, $date, $options, $html_options );
    $select_hour   = Form::select_hour( "hour_".$field_name, $date, $hour_options, $html_options );
    $select_minute = Form::select_minute( "minute_".$field_name, $date, $minute_options, $html_options );
    $select_second = Form::select_second( "second_".$field_name, $date, $second_options, $html_options );

    if ( $discard_hour ) {
      $select_hour = Form::hidden('hour_'.$field_name, $current_hour, $html_options);
    }

    if ($discard_minute) {
     $select_minute = Form::hidden('minute_'.$field_name, $current_minute, $html_options);
    }

    if ( $discard_second ) {
      $select_second = Form::hidden('second_'.$field_name, $current_second, $html_options);
    }

    return $select_date . $select_hour . $select_minute . $select_second;

  });

  class DateHelper {

    public static function normalize_datetime($date) {
      $date           = $date ? $date : new DateTime();

      if( is_string($date) ) {
        $date = string_to_datetime($date);
        $date = new DateTime($date);
      }
      else if ( is_integer($date) ) {
        $datetime_format = '%Y-%m-%d %H:%M:%S';
        $date = new DateTime(strftime($datetime_format, $date));
      }
      return $date;
    }

    public static function lang_file() {
      return Lang::file('application', Config::get('application.language'), 'date');
    }

    public static function prepare_range(&$selected, $include_blank = false, $prompt = false) {
      $range = array();

      if($include_blank) {
        $range[""] = "";
      }

      if( $prompt ) {
        $range[] = $prompt;
        $selected = 0;
      }
    }

    public static function build_standard_range($from, $to, &$selected, $include_blank = false, $prompt = false) {
      $range = self::prepare_range($selected, $include_blank, $prompt);

      for ($i=$from; $i <= $to; $i++) {
        $range[$i] = $i;
      }
      return $range;
    }

  }

?>