logo

protocalendar.js 用の CakePHP1.2対応のヘルパメソッド(のひな形)セキュリティコンポーネント対応版

redgasukiさんが protocalendar.js 用の CakePHP1.2 対応ヘルパーメソッド(のひな形)のセキュリティコンポーネント対応版を作成してくれました。

ありがとうございます!

詳細は以下のページでご覧下さい。

protocalendar.js 用の CakePHP1.2対応のヘルパメソッド(のひな形)をセキュリティコンポーネントに対応させて見た | ねねとまつの小部屋

http://blog.ne2ma2.com/archives/420

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

CakePHP1.2 対応版を作ったので公開します。

以下の funciton を FormHelper を継承した上で、JavascriptHelper を読み込んだヘルパーに追加します。(今回は SpHelper とします。)

<?php
class SpHelper extends FormHelper {
  var $helpers = array('Html', 'Javascript');

  function dateInput($fieldName, $options = array(), $calOptions = array()) {
    if (!isset($options['value'])) {
      $date = $this->value($fieldName);
      if ($date) {
        $options['value'] = date('Y/m/d', strtotime($date));
      }
    }
    $result = $this->text($fieldName, $options);

    if (!empty($options['id'])) {
      $styleId = $options['id'];
    } else {
      $styleId = Inflector::camelize($this->model()) . Inflector::camelize($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 .= "});";

    return ($result . $this->Javascript->codeBlock($script));
  }
}
?>

layout ファイルでは prototype.js を読み込み、$scripts_for_layout を使って、ビューから javascript を追加できるようにします。

(省略)
  <head>
(省略)
  <?php
    echo $javascript->link('prototype');
    echo $scripts_for_layout;
  ?>
  </head>
(省略)

view ファイルでは $scripts_for_layout の部分に追加する css, javascript をまず読み込んだ上で、SpHelper の dateInput function を使って protocalendar を表示します。


<?php $html->css('protocalendar/simple', null, array(), false); ?>
<?php $javascript->link('protocalendar/protocalendar', false); ?>
<?php $javascript->link('protocalendar/lang_ja', false); ?>
(省略)
<?php echo $sp->dateInput('date'); ?>
(省略)

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));
  }
}

Return to page top