PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/baser/views/helpers/bc_text.php

https://github.com/hashing/basercms
PHP | 547 lines | 248 code | 62 blank | 237 comment | 50 complexity | 580be826a287693cf974dc7d2d6a7e6a MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * Textヘルパー拡張
  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. * Include files
  21. */
  22. App::import("Helper",array("Text","BcTime"));
  23. /**
  24. * Textヘルパー拡張
  25. *
  26. * @package baser.views.helpers
  27. */
  28. class BcTextHelper extends TextHelper {
  29. /**
  30. * ヘルパー
  31. *
  32. * @var array
  33. * @access public
  34. */
  35. var $helpers = array(BC_TIME_HELPER, BC_FORM_HELPER);
  36. /**
  37. * 文字数カット(日本語対応)
  38. * Cuts a string to the length of $length and replaces the last characters
  39. * with the ending if the text is longer than length.
  40. *
  41. * @param string $text String to truncate.
  42. * @param integer $length Length of returned string, including ellipsis.
  43. * @param mixed $ending If string, will be used as Ending and appended to the trimmed string.
  44. * Can also be an associative array that can contain the last three params of this method.
  45. * @param boolean $exact If false, $text will not be cut mid-word
  46. * @param boolean $considerHtml If true, HTML tags would be handled correctly
  47. * @return string Trimmed string.
  48. * @access public
  49. */
  50. function mbTruncate($text, $length = 100, $ending = '...', $exact = true, $considerHtml = false) {
  51. if (is_array($ending)) {
  52. extract($ending);
  53. }
  54. if ($considerHtml) {
  55. if (mb_strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
  56. return $text;
  57. }
  58. preg_match_all('/(<.+?>)?([^<>]*)/s', $text, $lines, PREG_SET_ORDER);
  59. $total_length = mb_strlen($ending);
  60. $open_tags = array();
  61. $truncate = '';
  62. foreach ($lines as $line_matchings) {
  63. if (!empty($line_matchings[1])) {
  64. if (preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1])) {
  65. } elseif (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings)) {
  66. $pos = array_search($tag_matchings[1], $open_tags);
  67. if ($pos !== false) {
  68. unset($open_tags[$pos]);
  69. }
  70. } elseif (preg_match('/^<\s*([^\s>!]+).*?>$/s', $line_matchings[1], $tag_matchings)) {
  71. array_unshift($open_tags, strtolower($tag_matchings[1]));
  72. }
  73. $truncate .= $line_matchings[1];
  74. }
  75. $content_length = mb_strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ' ', $line_matchings[2]));
  76. if ($total_length+$content_length > $length) {
  77. $left = $length - $total_length;
  78. $entities_length = 0;
  79. if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', $line_matchings[2], $entities, PREG_OFFSET_CAPTURE)) {
  80. foreach ($entities[0] as $entity) {
  81. if ($entity[1]+1-$entities_length <= $left) {
  82. $left--;
  83. $entities_length += mb_strlen($entity[0]);
  84. } else {
  85. break;
  86. }
  87. }
  88. }
  89. $truncate .= mb_substr($line_matchings[2], 0, $left+$entities_length,mb_internal_encoding());
  90. break;
  91. } else {
  92. $truncate .= $line_matchings[2];
  93. $total_length += $content_length;
  94. }
  95. if ($total_length >= $length) {
  96. break;
  97. }
  98. }
  99. } else {
  100. if (mb_strlen($text) <= $length) {
  101. return $text;
  102. } else {
  103. $truncate = mb_substr($text, 0, $length - mb_strlen($ending),mb_internal_encoding());
  104. }
  105. }
  106. if (!$exact) {
  107. $spacepos = strrpos($truncate, ' ');
  108. if (isset($spacepos)) {
  109. $truncate = mb_substr($truncate, 0, $spacepos,mb_internal_encoding());
  110. }
  111. }
  112. $truncate .= $ending;
  113. if ($considerHtml) {
  114. foreach ($open_tags as $tag) {
  115. $truncate .= '</' . $tag . '>';
  116. }
  117. }
  118. return $truncate;
  119. }
  120. /**
  121. * boolean型を○―マークで出力
  122. *
  123. * @param boolean
  124. * @return string ○―マーク
  125. * @access public
  126. */
  127. function booleanMark($value) {
  128. if($value) {
  129. return "○";
  130. }else {
  131. return "―";
  132. }
  133. }
  134. /**
  135. * boolean型用のリストを○―マークで出力
  136. *
  137. * @return array ○―マークリスト
  138. * @access public
  139. */
  140. function booleanMarkList() {
  141. return array(0=>"―",1=>"○");
  142. }
  143. /**
  144. * boolean型用のリストを有無で出力
  145. *
  146. * @return array 有無リスト
  147. * @access public
  148. */
  149. function booleanExistsList() {
  150. return array(0=>"無",1=>"有");
  151. }
  152. /**
  153. * boolean型用のリストを可、不可で出力
  154. *
  155. * @return array 可/不可リスト
  156. * @access public
  157. */
  158. function booleanAllowList() {
  159. return array(0=>"不可",1=>"可");
  160. }
  161. /**
  162. * boolean型用のリストを[〜する/〜しない]形式で出力する
  163. *
  164. * @param string $doText Do文字列
  165. * @return array [〜する/〜しない]形式のリスト
  166. * @access public
  167. */
  168. function booleanDoList($doText = null) {
  169. return array(0=>$doText."しない",1=>$doText."する");
  170. }
  171. /**
  172. * boolean型用のリストを[〜る/〜ない]形式で出力する
  173. *
  174. * @param string $requireText Do文字列
  175. * @return array
  176. * @access public
  177. */
  178. function booleanRequireList($requireText = null) {
  179. return array(0=>$requireText.'ない',1=>$requireText.'る');
  180. }
  181. /**
  182. * boolean型用のリストを[〜る/〜ない]形式で出力する
  183. *
  184. * @param mixed $value
  185. * @param string $text
  186. * @return mixed
  187. */
  188. function booleanRequire($value,$text) {
  189. $booleanList = $this->booleanRequireList($text);
  190. return $booleanList[$value];
  191. }
  192. /**
  193. * boolean型のデータを [〜する / 〜しない] 形式で出力する
  194. *
  195. * @param boolean $value 値
  196. * @param string $doText Do文字列
  197. * @return string
  198. * @access public
  199. */
  200. function booleanDo($value,$doText = null) {
  201. $booleanDoList = $this->booleanDoList($doText);
  202. return $booleanDoList[$value];
  203. }
  204. /**
  205. * 都道府県のリストを出力
  206. *
  207. * @return array 都道府県リスト
  208. * @access public
  209. */
  210. function prefList($empty = '都道府県') {
  211. $pref = array();
  212. if($empty) {
  213. $pref = array(""=>$empty);
  214. } elseif($pref !== false) {
  215. $pref = array(""=>"");
  216. }
  217. $pref = $pref + array(1=>"北海道",2=>"青森県",3=>"岩手県",4=>"宮城県",5=>"秋田県",6=>"山形県",
  218. 7=>"福島県",8=>"茨城県",9=>"栃木県",10=>"群馬県",11=>"埼玉県",12=>"千葉県",13=>"東京都",14=>"神奈川県",
  219. 15=>"新潟県",16=>"富山県",17=>"石川県",18=>"福井県",19=>"山梨県",20=>"長野県",21=>"岐阜県",22=>"静岡県",
  220. 23=>"愛知県",24=>"三重県",25=>"滋賀県",26=>"京都府",27=>"大阪府",28=>"兵庫県",29=>"奈良県",30=>"和歌山県",
  221. 31=>"鳥取県",32=>"島根県",33=>"岡山県",34=>"広島県",35=>"山口県",36=>"徳島県",37=>"香川県",38=>"愛媛県",
  222. 39=>"高知県",40=>"福岡県",41=>"佐賀県",42=>"長崎県",43=>"熊本県",44=>"大分県",45=>"宮崎県",46=>"鹿児島県",
  223. 47=>"沖縄県");
  224. return $pref;
  225. }
  226. /**
  227. * 性別を出力
  228. *
  229. * @param array $value
  230. * @return string
  231. * @access public
  232. */
  233. function sex($value) {
  234. $sexes = array(1=>'男',2=>'女');
  235. return $sexes[$value];
  236. }
  237. /**
  238. * 郵便番号にハイフンをつけて出力
  239. * TODO とりあえずの仕様
  240. *
  241. * @param string $value ハイフンなしの郵便番号
  242. * @return string 〒マーク、ハイフン付きの郵便番号
  243. * @access public
  244. */
  245. function zipFormat($value) {
  246. $right = substr($value,0,3);
  247. $left = substr($value,3,4);
  248. return "〒 ".$right."-".$left;
  249. }
  250. /**
  251. * 番号を都道府県に変換して出力
  252. *
  253. * @param int $value 都道府県番号
  254. * @param string $noValue 都道府県名
  255. * @return string 都道府県名
  256. * @access public
  257. */
  258. function pref($value, $noValue='') {
  259. if(!$value) {
  260. return $noValue;
  261. }
  262. $list = $this->prefList();
  263. return $list[(int)$value];
  264. }
  265. /**
  266. * データをチェックして空の場合に指定した値を返す
  267. *
  268. * @param mixed $value
  269. * @param mixed $noValue
  270. * @return mixed
  271. * @access public
  272. */
  273. function noValue($value, $noValue) {
  274. if(!$value) {
  275. return $noValue;
  276. }else {
  277. return $value;
  278. }
  279. }
  280. /**
  281. * boolean型用を可、不可で出力
  282. *
  283. * @param boolean $value
  284. * @return string 可/不可
  285. * @access public
  286. */
  287. function booleanAllow($value) {
  288. $list = $this->booleanAllowList();
  289. return $list[(int)$value];
  290. }
  291. /**
  292. * boolean型用を有無で出力
  293. *
  294. * @param boolean $value
  295. * @return string 有/無
  296. * @access public
  297. */
  298. function booleanExists($value) {
  299. $list = $this->booleanExistsList();
  300. return $list[(int)$value];
  301. }
  302. /**
  303. * form::dateTimeで取得した和暦データを文字列データに変換する
  304. *
  305. * @param array $arrDate
  306. * @return string 和暦
  307. * @access public
  308. */
  309. function dateTimeWareki($arrDate) {
  310. if(!is_array($arrDate)) return;
  311. if(!$arrDate['wareki'] || !$arrDate['year'] || !$arrDate['month'] || !$arrDate['day'])
  312. return;
  313. list($w,$year) = split('-', $arrDate['year']);
  314. $wareki = $this->BcTime->nengo($w);
  315. return $wareki." ".$year."年 ".$arrDate['month']."月 ".$arrDate['day'].'日';
  316. }
  317. /**
  318. * 通貨表示
  319. *
  320. * @param int $value
  321. * @param string $prefix
  322. * @return string
  323. */
  324. function moneyFormat($value, $prefix='¥') {
  325. if($value) {
  326. return $prefix.number_format($value);
  327. } else {
  328. return '';
  329. }
  330. }
  331. /**
  332. * form::dateTimeで取得したデータを文字列データに変換する
  333. *
  334. * @param array $arrDate
  335. * @return string 日付
  336. * @access public
  337. */
  338. function dateTime($arrDate) {
  339. if(!$arrDate['year'] || !$arrDate['month'] || !$arrDate['day'])
  340. return;
  341. return $arrDate['year']."/".$arrDate['month']."/".$arrDate['day'];
  342. }
  343. /**
  344. * 文字をフォーマット形式で出力する
  345. *
  346. * @param string $format フォーマット
  347. * @param mixed $value 値
  348. * @param mixed $noValue データがなかった場合の初期値
  349. * @return string 変換後の文字列
  350. * @access public
  351. */
  352. function format($format,$value, $noValue = '') {
  353. if($value === '' || is_null($value)) {
  354. return $noValue;
  355. } else {
  356. return sprintf($format,$value);
  357. }
  358. }
  359. /**
  360. * モデルのコントロールソースより表示用データを取得する
  361. *
  362. * @param string $field フィールド名
  363. * @param mixed $value 値
  364. * @return string 表示用データ
  365. * @access public
  366. */
  367. function listValue($field,$value) {
  368. $list = $this->BcForm->getControlSource($field);
  369. if($list && isset($list[$value])) {
  370. return $list[$value];
  371. }else {
  372. return false;
  373. }
  374. }
  375. /**
  376. * 区切り文字で区切られたテキストを配列に変換する
  377. *
  378. * @param string $separator
  379. * @param string $value
  380. * @return array
  381. * @access public
  382. */
  383. function toArray($separator,$value) {
  384. if($separator != '"') {
  385. $value = str_replace('"','',$value);
  386. }
  387. if($separator != "'") {
  388. $value = str_replace("'",'',$value);
  389. }
  390. if(strpos($value,$separator) === false) {
  391. if($value) {
  392. return array($value);
  393. }else {
  394. return array();
  395. }
  396. }
  397. $values = split($separator,$value);
  398. return $values;
  399. }
  400. /**
  401. * 配列とキーを指定して値を取得する
  402. *
  403. * @param int $key
  404. * @param mixed $value
  405. * @param array $array
  406. * @param mixed type $noValue
  407. * @return mixied
  408. */
  409. function arrayValue($key, $array, $noValue = '') {
  410. if(is_numeric($key)) {
  411. $key = (int)$key;
  412. }
  413. if(isset($array[$key])) {
  414. return $array[$key];
  415. }
  416. return $noValue;
  417. }
  418. /**
  419. * 連想配列とキーのリストより値のリストを取得し文字列で返す
  420. * 文字列に結合する際、指定した区切り文字を指定できる
  421. *
  422. * @param string $glue
  423. * @param array $keys
  424. * @param array $array
  425. * @return string
  426. * @access public
  427. */
  428. function arrayValues($glue, $keys, $array) {
  429. $values = array();
  430. foreach($keys as $key) {
  431. if(isset($array[$key])) {
  432. $values[] = $array[$key];
  433. }
  434. }
  435. if($values) {
  436. return implode($glue, $values);
  437. } else {
  438. return '';
  439. }
  440. }
  441. /**
  442. * 日付より年齢を取得する
  443. *
  444. * @param string $birthday
  445. * @param string $suffix
  446. * @param mixed $noValue
  447. * @return mixed
  448. * @access public
  449. */
  450. function age($birthday, $suffix='歳', $noValue = '不明') {
  451. if(!$birthday) {
  452. return $noValue;
  453. }
  454. $byear = date('Y', strtotime($birthday));
  455. $bmonth = date('m', strtotime($birthday));
  456. $bday = date('d', strtotime($birthday));
  457. $tyear = date('Y');
  458. $tmonth = date('m');
  459. $tday = date('d');
  460. $age = $tyear - $byear;
  461. if($tmonth * 100 + $tday < $bmonth * 100 + $bday) $age--;
  462. return $age.$suffix;
  463. }
  464. /**
  465. * boolean型用のリストを有効、無効で出力
  466. *
  467. * @return array 可/不可リスト
  468. * @access public
  469. */
  470. function booleanStatusList() {
  471. return array(0=>"無効",1=>"有効");
  472. }
  473. /**
  474. * boolean型用を無効・有効で出力
  475. *
  476. * @param boolean
  477. * @return string 無効/有効
  478. * @access public
  479. */
  480. function booleanStatus($value) {
  481. $list = $this->booleanStatusList();
  482. return $list[(int)$value];
  483. }
  484. }
  485. ?>