スプーキーズの中の人。

スプーキーズの中の人が徒然なるままに、垂れ流します。

protocalendar.js 用の cakephp ヘルパメソッド(のひな形)を公開しました

デフォルトで lang:'ja' を使っているため、日付フォーマットが yyyy/mm/dd になるように小細工をしています。

また、オプションは lang, startYear, endYear のみ有効になっています。

function dateInput($fieldName, $htmlAttributes = array(), $calOptions = array(), $return = false, $typeModel = true) {
  if (!isset($htmlAttributes['value'])) {
    $date = $this->tagValue($fieldName);
    if ($date) {
      $htmlAttributes['value'] = date('Y/m/d', strtotime($date));
    }
  }
  $result = $this->input($fieldName, $htmlAttributes, $return);

  if(!empty($htmlAttributes['id'])){
    $styleId = $htmlAttributes['id'];
  }else if($typeModel == true){
    $this->setFormTag($fieldName);
    $styleId = $this->model . Inflector::camelize($this->field);
  }else{
    $this->field = $fieldName;
    $styleId = $this->field;
  }

  $script = "  InputCalendar.createOnLoaded('{$styleId}', {";
  $lang = !empty($calOptions['lang']) ? "lang:{$calOptions['lang']} " : "lang:'ja'";
  $script .= $lang;
  if (!empty($calOptions['startYear'])) {
    $script .= ", startYear:{$calOptions['startYear']} ";
  }
  if (!empty($calOptions['endYear'])) {
    $script .= ", endYear:{$calOptions['endYear']} ";
  }
  $script .= "});";

  if (AUTO_OUTPUT && $return == false) {
    echo $result;
    echo $this->Javascript->codeBlock($script, false);
  } else {
    return ($result . $this->Javascript->codeBlock($script, false));
  }
}