Skip to content

Instantly share code, notes, and snippets.

@RyujiAMANO
Last active August 29, 2015 14:27
Show Gist options
  • Save RyujiAMANO/1e0f925cede2b9665680 to your computer and use it in GitHub Desktop.
Save RyujiAMANO/1e0f925cede2b9665680 to your computer and use it in GitHub Desktop.
CubeCakeFormHelper
<?php
App::uses('FormHelper', 'View/Helper');
class CubeCakeFormHelper extends FormHelper
{
public $labelDomain = null;
public function __construct(View $View, $settings = array())
{
parent::__construct($View, $settings);
}
/**
* inputオーバーライド
*/
public function input($fieldName, $options = array())
{
$output = '';
// ここでカスタマイズを入れる
$output .= parent::input($fieldName, $options);
return $output;
}
/**
* EUC-JP対策でオーバーライド
* Returns a string to be used as onclick handler for confirm dialogs.
*
* @param string $message Message to be displayed
* @param string $okCode Code to be executed after user chose 'OK'
* @param string $cancelCode Code to be executed after user chose 'Cancel'
* @param array $options Array of options
* @return string onclick JS code
*/
protected function _confirm($message, $okCode, $cancelCode = '', $options = array())
{
$message = mb_convert_encoding($message, 'UTF-8', _CHARSET);
$message = json_encode($message);
$confirm = "if (confirm({$message})) { {$okCode} } {$cancelCode}";
if (isset($options['escape']) && $options['escape'] === false) {
$confirm = h($confirm);
}
$confirm = mb_convert_encoding($confirm, _CHARSET, 'UTF-8');
return $confirm;
}
// protected function construct($date = null) {
// if (empty($date)) {
// $date = date("Y-m-d H:i:s");
// }
//
// $keys = array('year','month','day','hour','min','sec');
// $values = sscanf($date, '%d-%d-%d %d:%d:%d');
// return array_combine($keys, $values);
// }
//
/**
* Generates an input element
*
* @param array $args The options for the input element
* @return string The generated input element
*/
protected function _getInput($args)
{
extract($args);
switch ($type) {
case 'hidden':
return $this->hidden($fieldName, $options);
case 'checkbox':
return $this->checkbox($fieldName, $options);
case 'radio':
return $this->radio($fieldName, $radioOptions, $options);
case 'file':
return $this->file($fieldName, $options);
case 'select':
$options += array('options' => array(), 'value' => $selected);
$list = $options['options'];
unset($options['options']);
return $this->select($fieldName, $list, $options);
case 'time':
$options['value'] = $selected;
return $this->dateTime($fieldName, null, $timeFormat, $options);
case 'date':
// datepicker
$options['value'] = $selected;
return $this->datepicker($fieldName, $options);
// return $this->dateTime($fieldName, $dateFormat, null, $options);
case 'datetime':
$options['value'] = $selected;
return $this->dateTime($fieldName, $dateFormat, $timeFormat, $options);
case 'textarea':
return $this->textarea($fieldName, $options + array('cols' => '30', 'rows' => '6'));
case 'url':
return $this->text($fieldName, array('type' => 'url') + $options);
default:
return $this->{$type}($fieldName, $options);
}
}
function datepicker($fieldName, $options = array())
{
//外部ファイル
// $ext = $this->Html->script('jquery-1.9.1', array('inline' => false))
// . $this->Html->script('jquery-ui-1.10.3.custom', array('inline' => false))
// . $this->Html->script('jquery.ui.datepicker-ja', array('inline' => false))
// . $this->Html->css('jquery-ui-1.10.3.custom', null, array('inline' => false));
// MyTodo 置き場所を任意に設定できるように
$this->Html->script(XOOPS_URL . '/themes/kms/js/jquery-1.9.1.min.js',array('inline' => false));
$this->Html->script(XOOPS_URL . '/themes/kms/js/jquery-ui-1.10.1.custom.min.js',array('inline' => false));
$this->Html->css(XOOPS_URL . '/themes/kms/css/ui-lightness/jquery-ui-1.10.1.custom.min.css',array('inline' => false));
$this->Html->script('http://ajax.googleapis.com/ajax/libs/jqueryui/1/i18n/jquery.ui.datepicker-ja.min.js',array('inline' => false));
//テキストボックスのhtml
$ext = $this->text($fieldName, $options);
//テキストボックスのID
if (isset($options["id"])) {
$id = $options["id"];
} else {
$id = $this->domId(array(), "for");
}
//スクリプト部分
$script =
"jQuery(function($){" .
"$(\"#" . $id["for"] . "\").datepicker({changeMonth: true,changeYear: true});" .
"});";
$this->Html->scriptBlock($script, array('inline' => false));
return $ext;
}
/**
* Returns a formatted LABEL element for HTML FORMs. Will automatically generate
* a `for` attribute if one is not provided.
*
* ### Options
*
* - `for` - Set the for attribute, if its not defined the for attribute
* will be generated from the $fieldName parameter using
* FormHelper::domId().
*
* Examples:
*
* The text and for attribute are generated off of the fieldname
*
* {{{
* echo $this->Form->label('Post.published');
* <label for="PostPublished">Published</label>
* }}}
*
* Custom text:
*
* {{{
* echo $this->Form->label('Post.published', 'Publish');
* <label for="PostPublished">Publish</label>
* }}}
*
* Custom class name:
*
* {{{
* echo $this->Form->label('Post.published', 'Publish', 'required');
* <label for="PostPublished" class="required">Publish</label>
* }}}
*
* Custom attributes:
*
* {{{
* echo $this->Form->label('Post.published', 'Publish', array(
* 'for' => 'post-publish'
* ));
* <label for="post-publish">Publish</label>
* }}}
*
* @param string $fieldName This should be "Modelname.fieldname"
* @param string $text Text that will appear in the label field. If
* $text is left undefined the text will be inflected from the
* fieldName.
* @param array|string $options An array of HTML attributes, or a string, to be used as a class name.
* @return string The formatted LABEL element
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::label
*/
public function label($fieldName = null, $text = null, $options = array()) {
if ($fieldName === null) {
$fieldName = implode('.', $this->entity());
}
if ($text === null) {
if (strpos($fieldName, '.') !== false) {
$fieldElements = explode('.', $fieldName);
$text = array_pop($fieldElements);
} else {
$text = $fieldName;
}
if (substr($text, -3) === '_id') {
$text = substr($text, 0, -3);
}
if(is_null($this->labelDomain)){
$text = __(Inflector::humanize(Inflector::underscore($text)));
}else{
$text = __d($this->labelDomain, Inflector::humanize(Inflector::underscore($text)));
}
}
if (is_string($options)) {
$options = array('class' => $options);
}
if (isset($options['for'])) {
$labelFor = $options['for'];
unset($options['for']);
} else {
$labelFor = $this->domId($fieldName);
}
return $this->Html->useTag('label', $labelFor, $options, $text);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment