PageRenderTime 93ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/baser/views/helpers/bc_freeze.php

https://github.com/hashing/basercms
PHP | 606 lines | 371 code | 62 blank | 173 comment | 92 complexity | fd446cd0c89b33f44d0f1a5df7c50996 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * FreezeHelper
  5. *
  6. * PHP versions 5
  7. *
  8. * baserCMS : Based Website Development Project <http://basercms.net>
  9. * Copyright 2008 - 2012, baserCMS Users Community <http://sites.google.com/site/baserusers/>
  10. *
  11. * @copyright Copyright 2008 - 2012, baserCMS Users Community
  12. * @link http://basercms.net baserCMS Project
  13. * @package baser.view.helpers
  14. * @since baserCMS v 0.1.0
  15. * @version $Revision$
  16. * @modifiedby $LastChangedBy$
  17. * @lastmodified $Date$
  18. * @license http://basercms.net/license/index.html
  19. */
  20. /**
  21. * Include files
  22. */
  23. App::import('Helper', BC_FORM_HELPER, BC_UPLOAD_HELPER);
  24. /**
  25. * @package baser.view.helpers
  26. */
  27. class BcFreezeHelper extends BcFormHelper {
  28. /**
  29. * 凍結状態
  30. *
  31. * @var boolean
  32. * @access public
  33. */
  34. var $freezed = false;
  35. /**
  36. * ヘルパー
  37. *
  38. * @var array
  39. * @access public
  40. */
  41. var $helpers = array('Html', BC_FORM_HELPER, BC_UPLOAD_HELPER, BC_TEXT_HELPER, BC_TIME_HELPER, 'Javascript');
  42. /**
  43. * フォームを凍結させる
  44. *
  45. * @return void
  46. * @access public
  47. */
  48. function freeze() {
  49. $this->freezed = true;
  50. }
  51. /**
  52. * テキストボックスを表示する
  53. *
  54. * @param string $fieldName フィールド文字列
  55. * @param array $attributes html属性
  56. * @return string htmlタグ
  57. * @access public
  58. */
  59. function text($fieldName,$attributes = array()) {
  60. if($this->freezed) {
  61. list($model,$field) = explode('.',$fieldName);
  62. if(isset($attributes)) {
  63. $attributes = $attributes + array('type'=>'hidden');
  64. }else {
  65. $attributes = array('type'=>'hidden');
  66. }
  67. if(isset($attributes["value"])) {
  68. $value = $attributes["value"];
  69. }else {
  70. $value = $this->data[$model][$field];
  71. }
  72. return parent::text($fieldName, $attributes) . $value;
  73. }else {
  74. return parent::text($fieldName, $attributes);
  75. }
  76. }
  77. /**
  78. * select プルダウンメニューを表示
  79. *
  80. * @param string $fieldName フィールド文字列
  81. * @param array $options コントロールソース
  82. * @param mixed $selected
  83. * @param array $attributes html属性
  84. * @param array 空データの表示有無
  85. * @return string $showEmpty htmlタグ
  86. * @access public
  87. */
  88. function select($fieldName, $options, $selected = null, $attributes = array(), $showEmpty = true) {
  89. if($this->freezed) {
  90. return $this->freezeControll($fieldName, $options, $attributes);
  91. }else {
  92. // 横幅を設定する
  93. // 指定した文字数より足りない文字数分スペースを埋める処理としている為、
  94. // 等幅フォントを設定しないとちゃんとした横幅にはならない
  95. if(!empty($attributes['cols'])) {
  96. foreach($options as $key => $option) {
  97. if($attributes['cols'] > mb_strlen($option)) {
  98. $pad = str_repeat( ' ', $attributes['cols'] - mb_strlen($option));
  99. $options[$key] = $option.$pad;
  100. }
  101. }
  102. }
  103. return parent::select($fieldName, $options, $selected, $attributes, $showEmpty);
  104. }
  105. }
  106. /**
  107. * 日付タグを表示
  108. *
  109. * @param string $fieldName フィールド文字列
  110. * @param string $dateFormat 日付フォーマット
  111. * @param string $timeFormat 時間フォーマット
  112. * @param mixed $selected
  113. * @param array $attributes html属性
  114. * @param array $showEmpty 空データの表示有無
  115. * @return string htmlタグ
  116. * @access public
  117. */
  118. function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $selected = null, $attributes = array(), $showEmpty = true) {
  119. if($this->freezed) {
  120. $year = $month = $day = $hour = $min = $meridian = null;
  121. if (empty($selected)) {
  122. $selected = $this->value($fieldName);
  123. }
  124. if ($selected === null && $showEmpty != true) {
  125. $selected = time();
  126. }
  127. if (!empty($selected)) {
  128. if (is_array($selected)) {
  129. extract($selected);
  130. } else {
  131. if (is_numeric($selected)) {
  132. $selected = strftime('%Y-%m-%d %H:%M:%S', $selected);
  133. }
  134. $meridian = 'am';
  135. $pos = strpos($selected, '-');
  136. if ($pos !== false) {
  137. $date = explode('-', $selected);
  138. $days = explode(' ', $date[2]);
  139. $day = $days[0];
  140. $month = $date[1];
  141. $year = $date[0];
  142. } else {
  143. $days[1] = $selected;
  144. }
  145. if ($timeFormat != 'NONE' && !empty($timeFormat)) {
  146. $time = explode(':', $days[1]);
  147. $check = str_replace(':', '', $days[1]);
  148. if (($check > 115959) && $timeFormat == '12') {
  149. $time[0] = $time[0] - 12;
  150. $meridian = 'pm';
  151. } elseif ($time[0] == '00' && $timeFormat == '12') {
  152. $time[0] = 12;
  153. } elseif ($time[0] > 12) {
  154. $meridian = 'pm';
  155. }
  156. if ($time[0] == 0 && $timeFormat == '12') {
  157. $time[0] = 12;
  158. }
  159. $hour = $time[0];
  160. $min = $time[1];
  161. }
  162. }
  163. }
  164. $elements = array('Day','Month','Year','Hour','Minute','Meridian');
  165. $defaults = array(
  166. 'minYear' => null, 'maxYear' => null, 'separator' => '&nbsp;'
  167. );
  168. $attributes = array_merge($defaults, (array) $attributes);
  169. $minYear = $attributes['minYear'];
  170. $maxYear = $attributes['maxYear'];
  171. $separator = $attributes['separator'];
  172. if (isset($attributes['id'])) {
  173. if (is_string($attributes['id'])) {
  174. // build out an array version
  175. foreach ($elements as $element) {
  176. $selectAttrName = 'select' . $element . 'Attr';
  177. ${$selectAttrName} = $attributes;
  178. ${$selectAttrName}['id'] = $attributes['id'] . $element;
  179. }
  180. } elseif (is_array($attributes['id'])) {
  181. // check for missing ones and build selectAttr for each element
  182. foreach ($elements as $element) {
  183. $selectAttrName = 'select' . $element . 'Attr';
  184. ${$selectAttrName} = $attributes;
  185. ${$selectAttrName}['id'] = $attributes['id'][strtolower($element)];
  186. }
  187. }
  188. } else {
  189. // build the selectAttrName with empty id's to pass
  190. foreach ($elements as $element) {
  191. $selectAttrName = 'select' . $element . 'Attr';
  192. ${$selectAttrName} = $attributes;
  193. }
  194. }
  195. $selects = array();
  196. if(preg_match('/^W/',$dateFormat)){
  197. $selects[] = $this->wyear($fieldName, $minYear, $maxYear, $year, $selectYearAttr, $showEmpty)."年";
  198. }else{
  199. $selectYearAttr['value'] = $year;
  200. $selects[] = $this->freezeControll($fieldName.".year", array(),$selectYearAttr)."年";
  201. }
  202. // TODO 値の出力はBcTextにまとめた方がよいかも
  203. // メール本文への出力も同じ処理を利用する。(改行の処理などはどうするか。。)
  204. $selectMonthAttr['value'] = $month;
  205. $selectDayAttr['value'] = $day;
  206. $selects[] = $this->freezeControll($fieldName.".month", array() ,$selectMonthAttr)."月";
  207. $selects[] = $this->freezeControll($fieldName.".day", array() ,$selectDayAttr)."日";
  208. return implode($separator, $selects);;
  209. }else {
  210. return parent::dateTime($fieldName, $dateFormat, $timeFormat, $selected, $attributes, $showEmpty);
  211. }
  212. }
  213. /**
  214. * 和暦年
  215. *
  216. * @param string $fieldName Prefix name for the SELECT element
  217. * @param integer $minYear First year in sequence
  218. * @param integer $maxYear Last year in sequence
  219. * @param string $selected Option which is selected.
  220. * @param array $attributes Attribute array for the select elements.
  221. * @param boolean $showEmpty Show/hide the empty select option
  222. * @return string
  223. * @access public
  224. */
  225. function wyear($fieldName, $minYear = null, $maxYear = null, $selected = null, $attributes = array(), $showEmpty = true) {
  226. if($this->freezed) {
  227. if ((empty($selected) || $selected === true) && $value = $this->value($fieldName)) {
  228. if (is_array($value)) {
  229. extract($value);
  230. $selected = $year;
  231. } else {
  232. if (empty($value)) {
  233. if (!$showEmpty && !$maxYear) {
  234. $selected = 'now';
  235. } elseif (!$showEmpty && $maxYear && !$selected) {
  236. $selected = $maxYear;
  237. }
  238. } else {
  239. $selected = $value;
  240. }
  241. }
  242. }
  243. if (strlen($selected) > 4 || $selected === 'now') {
  244. $wareki = $this->BcTime->convertToWareki(date('Y-m-d', strtotime($selected)));
  245. $w = $this->BcTime->wareki($wareki);
  246. $wyear = $this->BcTime->wyear($wareki);
  247. $selected = $w.'-'.$wyear;
  248. $freezeText = $this->BcTime->nengo($w) . ' ' . $wyear;
  249. } elseif ($selected === false) {
  250. $selected = null;
  251. $freezeText = '';
  252. } elseif(strpos($selected, '-')===false) {
  253. $wareki = $this->BcTime->convertToWareki($this->value($fieldName));
  254. if($wareki) {
  255. $w = $this->BcTime->wareki($wareki);
  256. $wyear = $this->BcTime->wyear($wareki);
  257. $selected = $w.'-'.$wyear;
  258. $freezeText = $this->BcTime->nengo($w) . ' ' . $wyear;
  259. } else {
  260. $selected = null;
  261. $freezeText = '';
  262. }
  263. }
  264. return $freezeText.$this->hidden($fieldName.".wareki", array('value'=>true)).$this->hidden($fieldName.".year", array('value'=>$selected));
  265. } else {
  266. return parent::wyear($fieldName, $minYear, $maxYear, $selected, $attributes, $showEmpty);
  267. }
  268. }
  269. /**
  270. * チェックボックスを表示する
  271. *
  272. * @param string $fieldName フィールド文字列
  273. * @param title $title タイトル
  274. * @param array $attributes html属性
  275. * @return string htmlタグ
  276. * @access public
  277. */
  278. function checkbox($fieldName,$title,$attributes = array()) {
  279. if($this->freezed) {
  280. $options = array(0=>'',1=>$title);
  281. return $this->freezeControll($fieldName, $options,$attributes);
  282. }else {
  283. return parent::checkbox($fieldName,$attributes);
  284. }
  285. }
  286. /**
  287. * テキストエリアを表示する
  288. *
  289. * @param string フィールド文字列
  290. * @param array html属性
  291. * @return string htmlタグ
  292. * @access public
  293. */
  294. function textarea($fieldName,$attributes = array()) {
  295. if($this->freezed) {
  296. list($model,$field) = explode('.',$fieldName);
  297. $attributes = $attributes + array('type'=>'hidden');
  298. if(isset($attributes["value"])) {
  299. $value = $attributes["value"];
  300. }else {
  301. $value = $this->data[$model][$field];
  302. }
  303. if($value) {
  304. return parent::text($fieldName, $attributes) . nl2br($value);
  305. }else {
  306. return "&nbsp;";
  307. }
  308. }else {
  309. return parent::textarea($fieldName, $attributes);
  310. }
  311. }
  312. /**
  313. * ラジオボタンを表示する
  314. *
  315. * @param string $fieldName フィールド文字列
  316. * @param array $options コントロールソース
  317. * @param array $attributes html属性
  318. * @return string htmlタグ
  319. * @access public
  320. */
  321. function radio($fieldName,$options, $attributes = array()) {
  322. if($this->freezed) {
  323. return $this->freezeControll($fieldName, $options,$attributes);
  324. }else {
  325. return parent::radio($fieldName,$options, $attributes);
  326. }
  327. }
  328. /**
  329. * ファイルタグを出力
  330. *
  331. * @param string $fieldName
  332. * @param array $options
  333. * @return string
  334. * @access public
  335. */
  336. function file($fieldName, $options = array()) {
  337. if($this->freezed) {
  338. $value = $this->value($fieldName);
  339. if(is_array($value) && isset($value['session_key'])) {
  340. $value = $value['session_key'];
  341. return parent::hidden($fieldName."_tmp",array('value'=>$value)).$this->BcUpload->fileLink($fieldName,$options);
  342. }else {
  343. if(!is_array($value)) {
  344. $delValue = $this->value($fieldName.'_delete');
  345. if($delValue) {
  346. return parent::hidden($fieldName,array('value'=>$value)).parent::hidden($fieldName.'_delete',array('value'=>true)).$this->BcUpload->fileLink($fieldName,$options).'<br />削除する';
  347. }else {
  348. return parent::hidden($fieldName,array('value'=>$value)).$this->BcUpload->fileLink($fieldName,$options);
  349. }
  350. }
  351. }
  352. }else {
  353. return $this->BcUpload->file($fieldName,$options);
  354. }
  355. }
  356. /**
  357. * ファイルコントロール(画像)を表示する
  358. * TODO 確認画面には未チェック
  359. *
  360. * @param string $fieldName フィールド文字列
  361. * @param array $attributes html属性
  362. * @param array $attributes 画像属性
  363. * @return string htmlタグ
  364. * @access public
  365. */
  366. function image($fieldName, $attributes = array(), $imageAttributes = array()) {
  367. if(!$attributes)$attributes = array();
  368. $output = "";
  369. $imageAttributes = array_merge(array('ext' => 'jpg','alt' => '','dir' => '', 'id' => ''), $imageAttributes);
  370. if(!empty($imageAttributes['subdir']))$imageAttributes['subdir'].=DS;
  371. list($model,$field) = explode('.',$fieldName);
  372. if($this->freezed) {
  373. $attributes = array_merge($attributes,array('type' => 'hidden'));
  374. // 確認画面
  375. if(!empty($this->data[$model][$field]['name'])) {
  376. $path = "tmp".DS.Inflector::underscore($model).DS."img".DS.$field.$imageAttributes['ext']."?".rand();
  377. unset($imageAttributes['ext']);
  378. $output = parent::text($fieldName."_exists", $attributes);
  379. $output .= sprintf($this->Html->tags['image'], $path, $this->Html->_parseAttributes($imageAttributes));
  380. return $output;
  381. // 通常表示
  382. }else {
  383. if(!empty($this->data[$model][$field.'_exists'])) {
  384. $path = DS.$imageAttributes['dir'].DS.Inflector::tableize($model).DS.$imageAttributes['id'].DS.$field.".".$imageAttributes['ext']."?".rand();
  385. unset($imageAttributes['ext']);
  386. return sprintf($this->Html->tags['image'], $path, $this->Html->_parseAttributes($imageAttributes));
  387. }else {
  388. return "&nbsp;";
  389. }
  390. }
  391. }else {
  392. if(!empty($this->data[$model][$field.'_exists'])) {
  393. $path = DS.$imageAttributes['dir'].DS.Inflector::tableize($model).DS.$imageAttributes['id'].DS.$field.".".$imageAttributes['ext']."?".rand();
  394. unset($imageAttributes['ext']);
  395. $output = sprintf($this->Html->tags['image'], $path, $this->Html->_parseAttributes($imageAttributes));
  396. $output .= "<br />".$this->checkbox($fieldName."_delete","削除する");
  397. }
  398. return parent::file($fieldName, $attributes)."<br />".$output;
  399. }
  400. }
  401. /**
  402. * JsonList
  403. * TODO 確認画面用の実装は全くしてない
  404. *
  405. * @param string $fieldName フィールド文字列
  406. * @param array $attributes html属性
  407. * @return string htmlタグ
  408. * @access public
  409. */
  410. function jsonList($fieldName,$attributes) {
  411. if($this->freezed) {
  412. $out='';
  413. if(!empty($this->data[$fieldName])) {
  414. $out = '<div id="JsonTagView"><ul class="freezed">';
  415. foreach($this->data[$fieldName] as $tag) {
  416. $out.='<li>'.$tag['name'].'</li>';
  417. }
  418. $out.='</ul></div>';
  419. }
  420. return $out;
  421. }else {
  422. return parent::jsonList($fieldName,$attributes);
  423. }
  424. }
  425. /**
  426. * カレンダーコントロール付きのテキストフィールド
  427. * jquery-ui-1.7.2 必須
  428. *
  429. * @param string $fieldName フィールド文字列
  430. * @param array $attributes HTML属性
  431. * @return string html
  432. * @access public
  433. */
  434. function datepicker($fieldName, $attributes = array()) {
  435. if($this->freezed) {
  436. list($model,$field) = explode('.',$fieldName);
  437. if(isset($attributes)) {
  438. $attributes = am($attributes,array('type'=>'hidden'));
  439. }else {
  440. $attributes = array('type'=>'hidden');
  441. }
  442. if(!empty($this->data[$model][$field])) {
  443. $value = date('Y/m/d',strtotime($this->data[$model][$field]));
  444. }else {
  445. $value = "";
  446. }
  447. return parent::text($fieldName, $attributes) . $value;
  448. }else {
  449. return parent::datepicker($fieldName, $attributes);
  450. }
  451. }
  452. /**
  453. * 凍結時用のコントロールを取得する
  454. * @param string フィールド文字列
  455. * @param array コントロールソース
  456. * @param array html属性
  457. * @return string htmlタグ
  458. * @access public
  459. */
  460. function freezeControll($fieldName,$options,$attributes=array()) {
  461. $attributes = array_merge(array('class' => ''), $attributes);
  462. unset($attributes["separator"]);
  463. if(preg_match_all("/\./",$fieldName,$regs)==2) {
  464. list($model,$field,$detail) = explode('.',$fieldName);
  465. }else {
  466. list($model,$field) = explode('.',$fieldName);
  467. }
  468. // 値を取得
  469. if(isset($attributes["value"])) {
  470. $value = $attributes["value"];
  471. }else {
  472. // HABTAM
  473. if(!empty($attributes["multiple"]) && $attributes["multiple"] !== 'checkbox') {
  474. $value = $this->data[$model];
  475. }else {
  476. if(!empty($this->data[$model][$field])) {
  477. $value = $this->data[$model][$field];
  478. }else {
  479. $value = null;
  480. }
  481. }
  482. }
  483. // optionsによるソース有 「0」は通す
  484. if($options && $value!=='' && !is_null($value)) {
  485. // HABTAM
  486. if(!empty($attributes["multiple"]) && $attributes["multiple"] !== 'checkbox') {
  487. $_value = "";
  488. foreach($value as $data) {
  489. if(isset($options[$data['id']])) {
  490. $_value .= sprintf($this->Html->tags['li'], null, $options[$data['id']]);
  491. }
  492. }
  493. $value = sprintf($this->Html->tags['ul'], " ".$this->_parseAttributes($attributes, null, '', ' '),$_value);
  494. $out = parent::hidden($fieldName, $attributes) . $value;
  495. // マルチチェック
  496. }elseif(!empty($attributes["multiple"]) && $attributes["multiple"] === 'checkbox') {
  497. $_value = "";
  498. foreach($value as $key => $data) {
  499. if(isset($options[$data])) {
  500. $_value .= sprintf($this->Html->tags['li'], null, $options[$data]);
  501. }
  502. }
  503. $out = sprintf($this->Html->tags['ul'], " ".$this->_parseAttributes($attributes, null, '', ' '),$_value);
  504. $out .= $this->hidden($fieldName, array('value'=>$value,'multiple'=>true));
  505. // 一般
  506. }elseif(empty($detail)) {
  507. if(isset($options[$value])) {
  508. $value = $options[$value];
  509. }else {
  510. $value = '';
  511. }
  512. $out = parent::hidden($fieldName, $attributes) . $value;
  513. // datetime
  514. }else {
  515. if($value[$detail]) {
  516. $value = $options[$value[$detail]];
  517. }else {
  518. $value = "";
  519. }
  520. $out = parent::hidden($fieldName, $attributes) . $value;
  521. }
  522. // 値なし
  523. }else {
  524. if($options) {
  525. if(!$value && !empty($options[0])) {
  526. $value = $options[0];
  527. }else {
  528. $value = "";
  529. }
  530. }elseif(empty($detail)) {
  531. if(!empty($value)) {
  532. $value = $value;
  533. }else {
  534. $value = null;
  535. }
  536. }elseif(is_array ($value) && isset($value[$detail])) {
  537. $value = $value[$detail];
  538. }
  539. $out = parent::hidden($fieldName, $attributes) . $value;
  540. }
  541. return $out;
  542. }
  543. }
  544. ?>