basercms /baser/views/helpers/bc_form.php

Language PHP Lines 1226
MD5 Hash 43daa1e54bb45f4c29b2f70b1b8cbe56
Repository https://github.com/hashing/basercms.git View Raw File
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
<?php
/* SVN FILE: $Id$ */
/**
 * FormHelper 拡張クラス
 *
 * PHP versions 5
 *
 * baserCMS :  Based Website Development Project <http://basercms.net>
 * Copyright 2008 - 2012, baserCMS Users Community <http://sites.google.com/site/baserusers/>
 *
 * @copyright		Copyright 2008 - 2012, baserCMS Users Community
 * @link			http://basercms.net baserCMS Project
 * @package			baser.view.helpers
 * @since			baserCMS v 0.1.0
 * @version			$Revision$
 * @modifiedby		$LastChangedBy$
 * @lastmodified	$Date$
 * @license			http://basercms.net/license/index.html
 */
/**
 * Include files
 */
App::import('Helper',array('Html', 'Form', BC_TIME_HELPER, BC_TEXT_HELPER, BC_CKEDITOR_HELPER));
/**
 * FormHelper 拡張クラス
 *
 * @package baser.views.helpers
 */
class BcFormHelper extends FormHelper {
/**
 * ヘルパー
 *
 * @var array
 * @access public
 */
	var $helpers = array('Html', BC_TIME_HELPER, BC_TEXT_HELPER, 'Javascript', BC_CKEDITOR_HELPER);
/**
 * sizeCounter用の関数読み込み可否
 * 
 * @var boolean
 * @access public
 */
	var $sizeCounterFunctionLoaded = false;
/**
 * 都道府県用のSELECTタグを表示する
 *
 * @param string $fieldName Name attribute of the SELECT
 * @param mixed $selected Selected option
 * @param array $attributes Array of HTML options for the opening SELECT element
 * @return string 都道府県用のSELECTタグ
 * @access public
 */
	function prefTag($fieldName, $selected = null, $attributes = array()) {

		$pref = $this->BcText->prefList();
		return $this->select($fieldName, $pref, $selected, $attributes, false);

	}
/**
 * dateTime 拡張
 *
 * @param string $fieldName Prefix name for the SELECT element
 * @param string $dateFormat DMY, MDY, YMD or NONE.
 * @param string $timeFormat 12, 24, NONE
 * @param string $selected Option which is selected.
 * @param string $attributes array of Attributes
 *						'monthNames' If set and false numbers will be used for month select instead of text.
 *						'minYear' The lowest year to use in the year select
 *						'maxYear' The maximum year to use in the year select
 *						'interval' The interval for the minutes select. Defaults to 1
 *						'separator' The contents of the string between select elements. Defaults to '-'
 * @param boolean $showEmpty Whether or not to show an empty default value.
 * @return string The HTML formatted OPTION element
 * @access public
 */
	function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $selected = null, $attributes = array(), $showEmpty = true) {

		$year = $month = $day = $hour = $min = $meridian = null;

		if (empty($selected)) {
			$selected = $this->value($fieldName);
		}

		if ($selected === null && $showEmpty != true) {
			$selected = time();
		}

		if (!empty($selected)) {
			if (is_array($selected)) {
				extract($selected);
			} else {
				if (is_numeric($selected)) {
					$selected = strftime('%Y-%m-%d %H:%M:%S', $selected);
				}
				$meridian = 'am';
				$pos = strpos($selected, '-');
				if ($pos !== false) {
					$date = explode('-', $selected);
					$days = explode(' ', $date[2]);
					$day = $days[0];
					$month = $date[1];
					$year = $date[0];
				} else {
					$days[1] = $selected;
				}

				if ($timeFormat != 'NONE' && !empty($timeFormat)) {
					$time = explode(':', $days[1]);
					$check = str_replace(':', '', $days[1]);

					if (($check > 115959) && $timeFormat == '12') {
						$time[0] = $time[0] - 12;
						$meridian = 'pm';
					} elseif ($time[0] == '00' && $timeFormat == '12') {
						$time[0] = 12;
					} elseif ($time[0] > 12) {
						$meridian = 'pm';
					}
					if ($time[0] == 0 && $timeFormat == '12') {
						$time[0] = 12;
					}
					$hour = $time[0];
					$min = $time[1];
				}
			}
		}

		$elements = array('Day','Month','Year','Hour','Minute','Meridian');
		// >>> CUSTOMIZE MODIFY 2011/01/11 ryuring	日本対応
		/*$defaults = array(
			'minYear' => null, 'maxYear' => null, 'separator' => '-',
			'interval' => 1, 'monthNames' => true
		);*/
		// ---
		$defaults = array(
			'minYear' => null, 'maxYear' => null, 'separator' => ' ',
			'interval' => 1, 'monthNames' => ''
		);
		// <<<
		$attributes = array_merge($defaults, (array) $attributes);
		if (isset($attributes['minuteInterval'])) {
			$attributes['interval'] = $attributes['minuteInterval'];
			unset($attributes['minuteInterval']);
		}
		$minYear = $attributes['minYear'];
		$maxYear = $attributes['maxYear'];
		$separator = $attributes['separator'];
		$interval = $attributes['interval'];
		$monthNames = $attributes['monthNames'];
		$attributes = array_diff_key($attributes, $defaults);

		if (isset($attributes['id'])) {
			if (is_string($attributes['id'])) {
				// build out an array version
				foreach ($elements as $element) {
					$selectAttrName = 'select' . $element . 'Attr';
					${$selectAttrName} = $attributes;
					${$selectAttrName}['id'] = $attributes['id'] . $element;
				}
			} elseif (is_array($attributes['id'])) {
				// check for missing ones and build selectAttr for each element
				foreach ($elements as $element) {
					$selectAttrName = 'select' . $element . 'Attr';
					${$selectAttrName} = $attributes;
					${$selectAttrName}['id'] = $attributes['id'][strtolower($element)];
				}
			}
		} else {
			// build the selectAttrName with empty id's to pass
			foreach ($elements as $element) {
				$selectAttrName = 'select' . $element . 'Attr';
				${$selectAttrName} = $attributes;
			}
		}

		$opt = '';

		if ($dateFormat != 'NONE') {
			$selects = array();
			foreach (preg_split('//', $dateFormat, -1, PREG_SPLIT_NO_EMPTY) as $char) {
				switch ($char) {
					// >>> CUSTOMIZE ADD 2011/01/11 ryuring	和暦対応
					case 'W':
						$selects[] = $this->wyear($fieldName, $minYear, $maxYear, $year, $selectYearAttr, $showEmpty)."年";
						break;
					// <<<
					case 'Y':
						// >>> CUSTOMIZE MODIFY 2011/01/11 ryuring	日本対応
						/*$selects[] = $this->year(
							$fieldName, $minYear, $maxYear, $year, $selectYearAttr, $showEmpty
						);*/
						// ---
						$suffix = (preg_match('/^W/', $dateFormat)) ? '年' : '';
						$selects[] = $this->year(
							$fieldName, $minYear, $maxYear, $year, $selectYearAttr, $showEmpty
						).$suffix;
						// <<<
					break;
					case 'M':
						$selectMonthAttr['monthNames'] = $monthNames;
						// >>> CUSTOMIZE MODIFY 2011/01/11 ryuring	日本対応
						//$selects[] = $this->month($fieldName, $month, $selectMonthAttr, $showEmpty);
						// ---
						$suffix = (preg_match('/^W/', $dateFormat)) ? '月' : '';
						$selects[] = $this->month($fieldName, $month, $selectMonthAttr, $showEmpty).$suffix;
						// <<<
					break;
					case 'D':
						// >>> CUSTOMIZE MODIFY 2011/01/11 ryuring	日本対応
						//$selects[] = $this->day($fieldName, $day, $selectDayAttr, $showEmpty);
						// ---
						$suffix = (preg_match('/^W/', $dateFormat)) ? '日' : '';
						$selects[] = $this->day($fieldName, $day, $selectDayAttr, $showEmpty).$suffix;
						// <<<
					break;
				}
			}
			$opt = implode($separator, $selects);
		}
		if (!empty($interval) && $interval > 1 && !empty($min)) {
			$min = round($min * (1 / $interval)) * $interval;
		}
		$selectMinuteAttr['interval'] = $interval;
		switch ($timeFormat) {
			case '24':
				$opt .= $this->hour($fieldName, true, $hour, $selectHourAttr, $showEmpty) . ':' .
				$this->minute($fieldName, $min, $selectMinuteAttr, $showEmpty);
			break;
			case '12':
				$opt .= $this->hour($fieldName, false, $hour, $selectHourAttr, $showEmpty) . ':' .
				$this->minute($fieldName, $min, $selectMinuteAttr, $showEmpty) . ' ' .
				$this->meridian($fieldName, $meridian, $selectMeridianAttr, $showEmpty);
			break;
			case 'NONE':
			default:
				$opt .= '';
			break;
		}
		return $opt;
		
	}
/**
 * 和暦年
 *
 * @param string $fieldName Prefix name for the SELECT element
 * @param integer $minYear First year in sequence
 * @param integer $maxYear Last year in sequence
 * @param string $selected Option which is selected.
 * @param array $attributes Attribute array for the select elements.
 * @param boolean $showEmpty Show/hide the empty select option
 * @return string
 */
	function wyear($fieldName, $minYear = null, $maxYear = null, $selected = null, $attributes = array(), $showEmpty = true) {
		
		if ((empty($selected) || $selected === true) && $value = $this->value($fieldName)) {
			if (is_array($value)) {
				extract($value);
				$selected = $year;
			} else {
				if (empty($value)) {
					if (!$showEmpty && !$maxYear) {
						$selected = 'now';

					} elseif (!$showEmpty && $maxYear && !$selected) {
						$selected = $maxYear;
					}
				} else {
					$selected = $value;
				}
			}
		}

		if (strlen($selected) > 4 || $selected === 'now') {
			$wareki = $this->BcTime->convertToWareki(date('Y-m-d', strtotime($selected)));
			$wareki = $this->BcTime->convertToWareki($this->value($fieldName));
			$w = $this->BcTime->wareki($wareki);
			$wyear = $this->BcTime->wyear($wareki);
			$selected = $w.'-'.$wyear;
		} elseif ($selected === false) {
			$selected = null;
		} elseif(strpos($selected, '-')===false) {
			$wareki = $this->BcTime->convertToWareki($this->value($fieldName));
			if($wareki) {
				$w = $this->BcTime->wareki($wareki);
				$wyear = $this->BcTime->wyear($wareki);
				$selected = $w.'-'.$wyear;
			} else {
				$selected = null;
			}
		}
		$yearOptions = array('min' => $minYear, 'max' => $maxYear);

		return $this->hidden($fieldName.".wareki", array('value'=>true)).
		$this->select(
			$fieldName . ".year", $this->__generateOptions('wyear', $yearOptions),
			$selected, $attributes, $showEmpty
		);
		
	}
/**
 * コントロールソースを取得する
 * Model側でメソッドを用意しておく必要がある
 *
 * @param string $field フィールド名
 * @param array $options
 * @return array コントロールソース
 * @access public
 */
	function getControlSource($field,$options = array()) {
		
		$count = preg_match_all('/\./is',$field,$matches);
		if($count == 1) {
			list($modelName,$field) = explode('.',$field);
		} elseif ($count == 2){
			list($plugin,$modelName,$field) = explode('.',$field);
			$modelName = $plugin.'.'.$modelName;
		}
		if(empty($modelName)) {
			$modelName = $this->model();
		}
		if(ClassRegistry::isKeySet($modelName)){
			$model =& ClassRegistry::getObject($modelName);
		}else{
			$model =& ClassRegistry::init($modelName);
		}
		if($model) {
			return $model->getControlSource($field,$options);
		}else {
			return false;
		}

	}
/**
 * モデルよりリストを生成する
 *
 * @param string $modelName
 * @param mixed $conditions
 * @param mixed $fields
 * @param mixed $order
 * @return mixed リストまたは、false
 * @access public
 */
	function generateList($modelName,$conditions = array(),$fields = array(),$order = array()) {

		$model =& ClassRegistry::getObject($modelName);

		if($fields) {
			list($idField,$displayField) = $fields;
		}else {
			$idField = 'id';
			$displayField = $model->getDisplayField();
			$fields = array($idField,$displayField);
		}

		$list = $model->find( 'all', array('conditions' => $conditions, 'fields' => $fields, 'order' => $order));

		if($list) {
			return Set::combine($list,"{n}.".$modelName.".".$idField,"{n}.".$modelName.".".$displayField);
		}else {
			return null;
		}

	}
/**
 * JsonList
 *
 * @param string $field フィールド文字列
 * @param string $attributes
 * @return array 属性
 * @access public
 */
	function jsonList($field,$attributes) {

		am(array("imgSrc"=>"","ajaxAddAction"=>"","ajaxDelAction"=>""),$attributes);
		// JsonDb用Hiddenタグ
		$out = $this->hidden('Json.'.$field.'.db');
		// 追加テキストボックス
		$out .= $this->text('Json.'.$field.'.name');
		// 追加ボタン
		$out .= $this->button('追加',array('id'=>'btnAdd'.$field));
		// リスト表示用ビュー
		$out .= '<div id="Json'.$field.'View"></div>';

		// javascript
		$out .= '<script type="text/javascript"><!--'."\n".
				'jQuery(function(){'."\n".
				'var json_List = new JsonList({"dbId":"Json'.$field.'Db","viewId":"JsonTagView","addButtonId":"btnAdd'.$field.'",'."\n".
				'"deleteButtonType":"img","deleteButtonSrc":"'.$attributes['imgSrc'].'","deleteButtonRollOver":true,'."\n".
				'"ajaxAddAction":"'.$attributes['ajaxAddAction'].'",'."\n".
				'"ajaxDelAction":"'.$attributes['ajaxDelAction'].'"});'."\n".
				'json_List.loadData();'."\n".
				'});'."\n".
				'//--></script>';

		return $out;

	}
/**
 * カレンダーコントロール付きのテキストフィールド
 * jquery-ui-1.7.2 必須
 *
 * @param string フィールド文字列
 * @param array HTML属性
 * @return string html
 * @access public
 */
	function datepicker($fieldName, $attributes = array()) {

		if (!isset($attributes['value'])) {
			$value = $this->value($fieldName);
		}else {
			$value = $attributes['value'];
		}

		if($value) {
			$attributes['value'] = date('Y/m/d',strtotime($this->BcTime->format('Y/m/d',$value)));
		}else {
			unset($attributes['value']);
		}

		$this->setEntity($fieldName);
		$id = $this->domId($fieldName);

		// テキストボックス
		$input = $this->text($fieldName,$attributes);

		// javascript
		$script = <<< DOC_END
<script type="text/javascript">
<!--
jQuery(function($){
	$("#{$id}").datepicker();
});
//-->
</script>
DOC_END;
		
		$out = $input."\n".$script;
		return $out;

	}
/**
 * 日付カレンダーと時間フィールド
 * 
 * @param string $fieldName
 * @param array $attributes
 * @return string
 * @access public
 */
	function dateTimePicker($fieldName, $attributes = array()) {

		$timeAttributes = array('size'=>8,'maxlength'=>8);
		if (!isset($attributes['value'])) {
			$value = $this->value($fieldName);
		}else {
			$value = $attributes['value'];
			unset($attributes['value']);
		}
		if($value && $value != '0000-00-00 00:00:00') {
			$dateValue = date('Y/m/d',strtotime($value));
			$timeValue = date('H:i:s',strtotime($value));
			$attributes['value']=$dateValue;
			$timeAttributes['value']=$timeValue;
		}
		$dateTag = $this->datepicker($fieldName.'_date', $attributes);
		$timeTag = $this->text($fieldName.'_time', $timeAttributes);
		$hiddenTag = $this->hidden($fieldName, array('value'=>$value));
		$domId = $this->domId();
		$_script = <<< DOC_END
$(function(){
   $("#{$domId}Date").change({$domId}ChangeResultHandler);
   $("#{$domId}Time").change({$domId}ChangeResultHandler);
   function {$domId}ChangeResultHandler(){
		var value = $("#{$domId}Date").val().replace(/\//g, '-');
		if($("#{$domId}Time").val()) {
			value += ' '+$("#{$domId}Time").val();
		}
        $("#{$domId}").val(value);
		if(this.id.replace('{$domId}','') == 'Date') {
			if($("#{$domId}Date").val() && !$("#{$domId}Time").val()) {
				$("#{$domId}Time").val('00:00:00');
			}
		}
   }
});
DOC_END;
		$script = $this->Javascript->codeBlock($_script,array('inline'=>false));
		return $dateTag.$timeTag.$hiddenTag;
		
	}
/**
 * Generates option lists for common <select /> menus
 *
 * @param string $name
 * @param array $options
 * @return array option lists
 * @access private
 */
	function __generateOptions($name, $options = array()) {
		
		if (!empty($this->options[$name])) {
			return $this->options[$name];
		}
		$data = array();

		switch ($name) {
			case 'minute':
				if (isset($options['interval'])) {
					$interval = $options['interval'];
				} else {
					$interval = 1;
				}
				$i = 0;
				while ($i < 60) {
					$data[$i] = sprintf('%02d', $i);
					$i += $interval;
				}
			break;
			case 'hour':
				for ($i = 1; $i <= 12; $i++) {
					$data[sprintf('%02d', $i)] = $i;
				}
			break;
			case 'hour24':
				for ($i = 0; $i <= 23; $i++) {
					$data[sprintf('%02d', $i)] = $i;
				}
			break;
			case 'meridian':
				$data = array('am' => 'am', 'pm' => 'pm');
			break;
			case 'day':
				$min = 1;
				$max = 31;

				if (isset($options['min'])) {
					$min = $options['min'];
				}
				if (isset($options['max'])) {
					$max = $options['max'];
				}

				for ($i = $min; $i <= $max; $i++) {
					$data[sprintf('%02d', $i)] = $i;
				}
			break;
			case 'month':
				if ($options['monthNames']) {
					$data['01'] = __('January', true);
					$data['02'] = __('February', true);
					$data['03'] = __('March', true);
					$data['04'] = __('April', true);
					$data['05'] = __('May', true);
					$data['06'] = __('June', true);
					$data['07'] = __('July', true);
					$data['08'] = __('August', true);
					$data['09'] = __('September', true);
					$data['10'] = __('October', true);
					$data['11'] = __('November', true);
					$data['12'] = __('December', true);
				} else {
					for ($m = 1; $m <= 12; $m++) {
						$data[sprintf("%02s", $m)] = strftime("%m", mktime(1, 1, 1, $m, 1, 1999));
					}
				}
			break;
			case 'year':
				$current = intval(date('Y'));

				if (!isset($options['min'])) {
					$min = $current - 20;
				} else {
					$min = $options['min'];
				}

				if (!isset($options['max'])) {
					$max = $current + 20;
				} else {
					$max = $options['max'];
				}
				if ($min > $max) {
					list($min, $max) = array($max, $min);
				}
				for ($i = $min; $i <= $max; $i++) {
					$data[$i] = $i;
				}
				$data = array_reverse($data, true);
			break;
			// >>> CUSTOMIZE ADD 2011/01/11 ryuring	和暦対応
			case 'wyear':
				$current = intval(date('Y'));

				if (!isset($options['min'])) {
					$min = $current - 20;
				} else {
					$min = $options['min'];
				}

				if (!isset($options['max'])) {
					$max = $current + 20;
				} else {
					$max = $options['max'];
				}
				if ($min > $max) {
					list($min, $max) = array($max, $min);
				}
				for ($i = $min; $i <= $max; $i++) {
					$wyears = $this->BcTime->convertToWarekiYear($i);
					if($wyears) {
						foreach($wyears as $value) {
							list($w,$year) = split('-', $value);
							$data[$value] = $this->BcTime->nengo($w).' '.$year;
						}
					}
				}
				$data = array_reverse($data, true);
				break;
			// <<<
		}
		$this->__options[$name] = $data;
		return $this->__options[$name];
		
	}
/**
 * Creates a checkbox input widget.
 * MODIFIED 2008/10/24 egashira
 *          hiddenタグを出力しないオプションを追加
 *
 * @param string $fieldNamem Name of a field, like this "Modelname.fieldname"
 * @param array $options Array of HTML attributes.
 *		'value' - the value of the checkbox
 *		'checked' - boolean indicate that this checkbox is checked.
 * @todo Right now, automatically setting the 'checked' value is dependent on whether or not the
 * 		 checkbox is bound to a model.  This should probably be re-evaluated in future versions.
 * @return string An HTML text input element
 * @access public
 */
	function checkbox($fieldName, $options = array()) {
		
		// CUSTOMIZE ADD 2011/05/07 ryuring
		// >>> hiddenをデフォルトオプションに追加
		$options = array_merge(array('hidden' => true), $options);
		$hidden = $options['hidden'];
		unset($options['hidden']);
		// <<<
		
		$options = $this->_initInputField($fieldName, $options);
		$value = current($this->value());

		if (!isset($options['value']) || empty($options['value'])) {
			$options['value'] = 1;
		} elseif (
			(!isset($options['checked']) && !empty($value) && $value === $options['value']) ||
			!empty($options['checked'])
		) {
			$options['checked'] = 'checked';
		}

		// CUSTOMIZE MODIFY 2011/05/07 ryuring
		// >>> hiddenオプションがある場合のみ、hiddenタグを出力
		/*$hiddenOptions = array(
			'id' => $options['id'] . '_', 'name' => $options['name'],
			'value' => '0', 'secure' => false
		);
		if (isset($options['disabled']) && $options['disabled'] == true) {
			$hiddenOptions['disabled'] = 'disabled';
		}
		$output = $this->hidden($fieldName, $hiddenOptions);*/
		// ---
		if($hidden) {
			$hiddenOptions = array(
				'id' => $options['id'] . '_', 'name' => $options['name'],
				'value' => '0', 'secure' => false
			);
			if (isset($options['disabled']) && $options['disabled'] == true) {
				$hiddenOptions['disabled'] = 'disabled';
			}
			$output = $this->hidden($fieldName, $hiddenOptions);
		}else {
			$output='';
		}
		// <<<
		
		// CUSTOMIZE MODIFY 2011/05/07 ryuring
		// >>> label を追加
		/*return $this->output($output . sprintf(
			$this->Html->tags['checkbox'],
			$options['name'],
			$this->_parseAttributes($options, array('name'), null, ' ')
		));*/
		// ---
		if(!empty($options['label'])) {
			$label = '&nbsp;'.parent::label($fieldName, $options['label']);
		}else {
			$label = '';
		}
		return $this->output($output . sprintf(
			$this->Html->tags['checkbox'],
			$options['name'],
			$this->_parseAttributes($options, array('name'), null, ' ')
		)).$label;
		// <<<
		
	}
/**
 * Returns an array of formatted OPTION/OPTGROUP elements
 * 
 * @return array
 * @access private
 */
	function __selectOptions($elements = array(), $selected = null, $parents = array(), $showParents = null, $attributes = array()) {
		 
		$select = array();
		$attributes = array_merge(array('escape' => true, 'style' => null), $attributes);
		$selectedIsEmpty = ($selected === '' || $selected === null);
		$selectedIsArray = is_array($selected);

		foreach ($elements as $name => $title) {
			$htmlOptions = array();
			if (is_array($title) && (!isset($title['name']) || !isset($title['value']))) {
				if (!empty($name)) {
					if ($attributes['style'] === 'checkbox') {
						$select[] = $this->Html->tags['fieldsetend'];
					} else {
						$select[] = $this->Html->tags['optiongroupend'];
					}
					$parents[] = $name;
				}
				$select = array_merge($select, $this->__selectOptions(
						$title, $selected, $parents, $showParents, $attributes
				));

				if (!empty($name)) {
					if ($attributes['style'] === 'checkbox') {
						$select[] = sprintf($this->Html->tags['fieldsetstart'], $name);
					} else {
						$select[] = sprintf($this->Html->tags['optiongroup'], $name, '');
					}
				}
				$name = null;
			} elseif (is_array($title)) {
				$htmlOptions = $title;
				$name = $title['value'];
				$title = $title['name'];
				unset($htmlOptions['name'], $htmlOptions['value']);
			}

			if ($name !== null) {
				if ((!$selectedIsEmpty && $selected == $name) || ($selectedIsArray && in_array($name, $selected))) {
					if ($attributes['style'] === 'checkbox') {
						$htmlOptions['checked'] = true;
					} else {
						$htmlOptions['selected'] = 'selected';
					}
				}

				if ($showParents || (!in_array($title, $parents))) {
					$title = ($attributes['escape']) ? h($title) : $title;

					if ($attributes['style'] === 'checkbox') {
						$htmlOptions['value'] = $name;

						$tagName = Inflector::camelize(
								$this->model() . '_' . $this->field().'_'.Inflector::underscore($name)
						);
						$htmlOptions['id'] = $tagName;
						$label = array('for' => $tagName);

						if (isset($htmlOptions['checked']) && $htmlOptions['checked'] === true) {
							$label['class'] = 'selected';
						}

						list($name) = array_values($this->__name());

						if (empty($attributes['class'])) {
							$attributes['class'] = 'checkbox';
						}
						$label = $this->label(null, $title, $label);
						$item = sprintf(
								$this->Html->tags['checkboxmultiple'], $name,
								$this->Html->_parseAttributes($htmlOptions)
						);
						// checkboxのdivを外せるオプションを追加
						if(isset($attributes['div']) && $attributes['div']===false) {
							$select[] = $item.$label;
						}else {
							$select[] = $this->Html->div($attributes['class'], $item . $label);
						}
					} else {
						$select[] = sprintf(
								$this->Html->tags['selectoption'],
								$name, $this->Html->_parseAttributes($htmlOptions), $title
						);
					}
				}
			}
		}

		return array_reverse($select, true);
		
	}
/**
 * Returns a formatted SELECT element.
 * Attributes:
 *
 * - 'showParents' - If included in the array and set to true, an additional option element
 *   will be added for the parent of each option group.
 * - 'multiple' - show a multiple select box.  If set to 'checkbox' multiple checkboxes will be
 *   created instead.
 *
 * @param string $fieldName Name attribute of the SELECT
 * @param array $options Array of the OPTION elements (as 'value'=>'Text' pairs) to be used in the
 *    SELECT element
 * @param mixed $selected The option selected by default.  If null, the default value
 *   from POST data will be used when available.
 * @param array $attributes The HTML attributes of the select element.
 * @param mixed $showEmpty If true, the empty select option is shown.  If a string,
 *   that string is displayed as the empty element.
 * @return string Formatted SELECT element
 * @access public
 */
	function select($fieldName, $options = array(), $selected = null, $attributes = array(), $showEmpty = '') {
		
		$select = array();
		$showParents = false;
		$escapeOptions = true;
		$style = null;
		$tag = null;

		if (isset($attributes['escape'])) {
			$escapeOptions = $attributes['escape'];
			unset($attributes['escape']);
		}
		$attributes = $this->_initInputField($fieldName, array_merge(
			(array)$attributes, array('secure' => false)
		));

		if (is_string($options) && isset($this->__options[$options])) {
			$options = $this->__generateOptions($options);
		} elseif (!is_array($options)) {
			$options = array();
		}
		if (isset($attributes['type'])) {
			unset($attributes['type']);
		}
		if (in_array('showParents', $attributes)) {
			$showParents = true;
			unset($attributes['showParents']);
		}

		if (!isset($selected)) {
			$selected = $attributes['value'];
		}

		if (isset($attributes) && array_key_exists('multiple', $attributes)) {
			$style = ($attributes['multiple'] === 'checkbox') ? 'checkbox' : null;
			$template = ($style) ? 'checkboxmultiplestart' : 'selectmultiplestart';
			$tag = $this->Html->tags[$template];
			// >>> CUSTOMIZE MODIFY 2011/01/21 ryuring
			// multiplecheckboxの場合にhiddenをつけないオプションを追加
			//$select[] = $this->hidden(null, array('value' => '', 'id' => null, 'secure' => false));
			// ---
			if(!isset($attributes['hidden']) || $attributes['hidden']!==false) {
				$select[] = $this->hidden(null, array('value' => '', 'id' => null, 'secure' => false));
			}
			// <<<
		} else {
			$tag = $this->Html->tags['selectstart'];
		}

		if (!empty($tag) || isset($template)) {
			$this->__secure();
			$select[] = sprintf($tag, $attributes['name'], $this->_parseAttributes(
				$attributes, array('name', 'value'))
			);
		}
		$emptyMulti = (
			$showEmpty !== null && $showEmpty !== false && !(
				empty($showEmpty) && (isset($attributes) &&
				array_key_exists('multiple', $attributes))
			)
		);

		if ($emptyMulti) {
			$showEmpty = ($showEmpty === true) ? '' : $showEmpty;
			$options = array_reverse($options, true);
			$options[''] = $showEmpty;
			$options = array_reverse($options, true);
		}

		// divを追加すぐ下の__selectOptionsのみで利用
		if(isset($attributes['div'])) {
			if($attributes['div']=='false') {
				$attributes['div'] = false;
			}
			$div = $attributes['div'];
		}else {
			$div = null;
		}
		$_select = $this->__selectOptions(
			array_reverse($options, true),
			$selected,
			array(),
			$showParents,
			array('escape' => $escapeOptions, 'style' => $style, 'div' => $div)
		);
		if(!empty($attributes['separator'])) {
			$separator = $attributes['separator']."\n";
		}else {
			$separator = "\n";
		}
		$select[] = implode($separator, $_select);

		$template = ($style == 'checkbox') ? 'checkboxmultipleend' : 'selectend';
		$select[] = $this->Html->tags[$template];

		// 解除ボタンを追加(jQuery必須)
		if(isset($attributes['multiple']) && $attributes['multiple'] === true) {
			list($model,$field) = explode(".",$fieldName);
			$tagName = Inflector::camelize($model . '_' . $field);
			$out = '<script type="text/javascript">';
			$out .= "jQuery(document).ready(function() {";
			$out .= "jQuery('#".$tagName."Clear').click(function(){";
			$out .= "jQuery('#".$tagName."').val('');";
			$out .= "});";
			$out .= "});";
			$out .= "</script>";
			$out .= '<input type="button" name="'.$tagName.'Clear" id="'.$tagName.'Clear" value=" 解 除 " />';
			return $this->output(implode("\n", $select))."<br />".$out;
		}else {
			return $this->output(implode("\n", $select));
		}
		
	}
/**
 * 文字列保存用複数選択コントロール
 * 
 * @param string $fieldName
 * @param array $options
 * @param mixed $selected
 * @param array $attributes
 * @param mixed $showEmpty
 * @return string
 * @access public
 */
	function selectText($fieldName, $options = array(), $selected = null, $attributes = array(), $showEmpty = '') {

		$_attributes = array('separator'=>'<br />','quotes'=>true);
		$attributes = Set::merge($_attributes,$attributes);

		$quotes = $attributes['quotes'];
		unset($attributes['quotes']);

		$_options = $this->_initInputField($fieldName, $options);
		if(empty($attributes['multiple'])) $attributes['multiple'] = 'checkbox';
		$id = $_options['id'];
		$_id = $_options['id'].'_';
		$name = $_options['name'];
		$out = '<div id="'.$_id.'">'.$this->select($fieldName.'_', $options, $selected, $attributes, $showEmpty).'</div>';
		$out .= $this->hidden($fieldName);
		$script = <<< DOC_END
$(document).ready(function() {
    aryValue = $("#{$id}").val().replace(/\'/g,"").split(",");
    for(key in aryValue){
        var value = aryValue[key];
        $("#"+camelize("{$id}_"+value)).attr('checked',true);
    }
    $("#{$_id} input[type=checkbox]").change(function(){
        var aryValue = [];
        $("#{$_id} input[type=checkbox]").each(function(key,value){
            if($(this).attr('checked')){
                aryValue.push("'"+$(this).val()+"'");
            }
        });
        $("#{$id}").val(aryValue.join(','));
    });
});
DOC_END;
		$out .= $this->Javascript->codeBlock($script);
		return $out;
		
	}
/**
 * Creates a hidden input field.
 *
 * @param string $fieldName Name of a field, in the form"Modelname.fieldname"
 * @param array $options Array of HTML attributes.
 * @return string
 * @access public
 */
	function hidden($fieldName, $options = array()) {
		
		$secure = true;

		if (isset($options['secure'])) {
			$secure = $options['secure'];
			unset($options['secure']);
		}

		// 2010/07/24 ryuring
		// セキュリティコンポーネントのトークン生成の仕様として、
		// ・hiddenタグ以外はフィールド情報のみ
		// ・hiddenタグはフィールド情報と値
		// をキーとして生成するようになっている。
		// その場合、生成の元のなる値は、multipleを想定されておらず、先頭の値のみとなるが
		// multiple な hiddenタグの場合、送信される値は配列で送信されるので値違いで認証がとおらない。
		// という事で、multiple の場合は、あくまでhiddenタグ以外のようにフィールド情報のみを
		// トークンのキーとする事で認証を通すようにする。
		// >>> ADD
		if(!empty($options['multiple'])){
			$secure = false;
			$this->__secure();
		}
		// <<<

		$options = $this->_initInputField($fieldName, array_merge(
			$options, array('secure' => false)
		));
		$model = $this->model();

		if ($fieldName !== '_method' && $model !== '_Token' && $secure) {
			$this->__secure(null, '' . $options['value']);
		}

		// CUSTOMIZE 2010/07/24 ryuring
		// 配列用のhiddenタグを出力できるオプションを追加
		// CUSTOMIZE 2010/08/01 ryuring
		// class属性を指定できるようにした
		// CUSTOMIZE 2011/03/11 ryuring
		// multiple で送信する値が配列の添字となっていたので配列の値に変更した
		// >>> ADD
		$multiple = false;
		$value = '';
		if(!empty($options['multiple'])){
			$multiple = true;
			$tagType = 'hiddenmultiple';
			$options['id'] = null;
			if (!isset($options['value'])) {
				$value = $this->value($fieldName);
			}else {
				$value = $options['value'];
			}
			if(is_array($value) && !$value) {
				unset($options['value']);
			}
			unset($options['multiple']);
		} else {
			$tagType = 'hidden';
		}
		// <<<
		// >>> MODIFY
		/*return $this->output(sprintf(
		$this->Html->tags['hidden'],
			$options['name'],
			$this->_parseAttributes($options, array('name', 'class'), '', ' ')
		));*/
		// ---
		if($multiple && is_array($value)) {
			$out = array();
			foreach($value as $_value) {
				$options['value'] = $_value;
				$out[] = $this->output(sprintf(
					$this->Html->tags[$tagType],
					$options['name'],
					$this->_parseAttributes($options, array('name'), '', ' ')
				));
			}
			return implode("\n", $out);
		} else {
			return $this->output(sprintf(
				$this->Html->tags[$tagType],
				$options['name'],
				$this->_parseAttributes($options, array('name'), '', ' ')
			));
		}
		// <<<
		
	}
/**
 * CKEditorを出力する
 *
 * @param	string	$fieldName
 * @param	array	$options
 * @param	array	$editorOptions
 * @param	array	$styles
 * @return	string
 * @access	public
 */
	function ckeditor($fieldName, $options = array(), $editorOptions = array(), $styles = array()) {

		$_options = array('type'=>'textarea');
		$options = am($_options,$options);
		$method = $options['type'];
		return $this->BcCkeditor->{$method}($fieldName, $options, $editorOptions, $styles, $this);

	}
/**
 * create
 * フック用にラッピング
 * 
 * @param array $model
 * @param array $options
 * @return string
 * @access public
 */
	function create($model = null, $options = array()) {

		$options = $this->executeHook('beforeFormCreate', $model, $options);
		
		$out = parent::create($model, $options);

		return $this->executeHook('afterFormCreate', $out);
		
	}
/**
 * end
 * フック用にラッピング
 *
 * @param	array	$options
 * @return	string
 * @access	public
 */
	function end($options = null) {

		$options = $this->executeHook('beforeFormEnd', $options);

		$out = parent::end($options);
		
		return $this->executeHook('afterFormEnd', $out);
		
	}
/**
 * Generates a form input element complete with label and wrapper div
 *
 * Options - See each field type method for more information. Any options that are part of
 * $attributes or $options for the different type methods can be included in $options for input().
 *
 * - 'type' - Force the type of widget you want. e.g. ```type => 'select'```
 * - 'label' - control the label
 * - 'div' - control the wrapping div element
 * - 'options' - for widgets that take options e.g. radio, select
 * - 'error' - control the error message that is produced
 *
 * @param string $fieldName This should be "Modelname.fieldname"
 * @param array $options Each type of input takes different options.
 * @return string Completed form widget
 */
	function input($fieldName, $options = array()) {

		$options = $this->executeHook('beforeFormInput', $fieldName, $options);
		
		$type = '';
		if(isset($options['type'])) {
			$type = $options['type'];
		}

		if(!isset($options['div'])) {
			$options['div'] = false;
		}

		if(!isset($options['error'])) {
			$options['error'] = false;
		}

		switch($type) {
			case 'text':
			default :
				if(!isset($options['label'])) {
					$options['label'] = false;
				}
				break;
			case 'radio':
				if(!isset($options['legend'])) {
					$options['legend'] = false;
				}
				if(!isset($options['separator'])) {
					$options['separator'] = ' ';
				}
				break;
		}

		$out = parent::input($fieldName, $options);

		/* カウンター */
		if(!empty($options['counter'])) {
			$domId = $this->domId($fieldName, $options);
			$counter = '<span id="'.$domId.'Counter'.'" class="size-counter"></span>';
			$script = '$("#'.$domId.'").keyup(countSize);$("#'.$domId.'").keyup();';
			if(!$this->sizeCounterFunctionLoaded) {
				$script .= <<< DOC_END
function countSize() {
	var len = $(this).val().length;
	var maxlen = $(this).attr('maxlength');
	if(!maxlen || maxlen == -1){
		maxlen = '-';
	}
	$("#"+$(this).attr('id')+'Counter').html(len+'/<small>'+maxlen+'</small>');
}
DOC_END;
				$this->sizeCounterFunctionLoaded = true;
			}
			$out = $out.$counter.$this->Javascript->codeBlock($script);
		}
		
		return $this->executeHook('afterFormInput', $fieldName, $out);
		
	}
/**
 * 日付タグ
 * 和暦実装
 * TODO 未実装
 */
	/*function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $selected = null, $attributes = array(), $showEmpty = true) {

		if($dateFormat == "WYMD"){
			$this->options['month'] = $this->getWarekiMonthes();
			$this->options['day'] = $this->getWarekiDays();
			$this->options['year'] = $this->getWarekiYears($attributes['minYear'],$attributes['maxYear']);
			$dateFormat = "YMD";

		}
		return parent::dateTime($fieldName, $dateFormat, $timeFormat, $selected, $attributes, $showEmpty);

	}*/
}
?>
Back to Top