TypographicBehavior.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. App::uses('ModelBehavior', 'Model');
  3. /**
  4. * Replace regionalized chars with standard ones on input.
  5. *
  6. * “smart quotes” become "dumb quotes" on save
  7. * „low-high“ become "high-high"
  8. * same for single quotes (apostrophes)
  9. * in order to unify them. Basic idea is a unified non-regional version in the database.
  10. *
  11. * Using the TypographyHelper we can then format the output
  12. * according to the language/regional setting (in some languages
  13. * the high-high smart quotes, in others the low-high ones are preferred)
  14. *
  15. * @link http://en.wikipedia.org/wiki/Non-English_usage_of_quotation_marks
  16. * @cakephp 2.x
  17. * @license MIT
  18. * 2011-01-13 ms
  19. */
  20. class TypographicBehavior extends ModelBehavior {
  21. protected $map = array(
  22. 'in' => array(
  23. '‘' => '"',
  24. //'&lsquo;' => '"', # ‘
  25. '’' => '"',
  26. //'&rsquo;' => '"', # ’
  27. '‚' => '"',
  28. //'&sbquo;' => '"', # ‚
  29. '‛' => '"',
  30. //'&#8219;' => '"', # ‛
  31. '“' => '"',
  32. //'&ldquo;' => '"', # “
  33. '”' => '"',
  34. //'&rdquo;' => '"', # ”
  35. '„' => '"',
  36. //'&bdquo;' => '"', # „
  37. '‟' => '"',
  38. //'&#8223;' => '"', # ‟
  39. '«' => '"',
  40. //'&laquo;' => '"', # «
  41. '»' => '"',
  42. //'&raquo;' => '"', # »
  43. '‹' => '"',
  44. //'&laquo;' => '"', # ‹
  45. '›' => '"',
  46. //'&raquo;' => '"', # ›
  47. ),
  48. 'out'=> array(
  49. # use the TypographyHelper for this at runtime
  50. ),
  51. );
  52. /**
  53. * Initiate behavior for the model using specified settings. Available settings:
  54. *
  55. *
  56. * @param object $Model Model using the behaviour
  57. * @param array $settings Settings to override for model.
  58. * @access public
  59. * 2011-12-06 ms
  60. */
  61. public function setup(Model $Model, $settings = array()) {
  62. $default = array(
  63. 'before' => 'save',
  64. 'fields' => array()
  65. );
  66. if (!isset($this->settings[$Model->alias])) {
  67. $this->settings[$Model->alias] = $default;
  68. }
  69. $this->settings[$Model->alias] = array_merge($this->settings[$Model->alias], is_array($settings) ? $settings : array());
  70. }
  71. public function beforeValidate(Model $Model) {
  72. parent::beforeValidate($Model);
  73. if ($this->settings[$Model->alias]['before'] == 'validate') {
  74. $this->process($Model);
  75. }
  76. return true;
  77. }
  78. public function beforeSave(Model $Model) {
  79. parent::beforeSave($Model);
  80. if ($this->settings[$Model->alias]['before'] == 'save') {
  81. $this->process($Model);
  82. }
  83. return true;
  84. }
  85. /**
  86. * Run before a model is saved
  87. *
  88. * @param object $Model Model about to be saved.
  89. * @return boolean true if save should proceed, false otherwise
  90. * @access public
  91. */
  92. public function process(Model $Model, $return = true) {
  93. foreach ($this->settings[$Model->alias]['fields'] as $field) {
  94. if (!empty($Model->data[$Model->alias][$field])) {
  95. $Model->data[$Model->alias][$field] = $this->_prepareInput($Model->data[$Model->alias][$field]);
  96. }
  97. }
  98. return $return;
  99. }
  100. /**
  101. * @param string $input
  102. * @return string $cleanedInput
  103. * 2011-12-06 ms
  104. */
  105. protected function _prepareInput($string) {
  106. $map = $this->map['in'];
  107. //return $string;
  108. $string = str_replace(array_keys($map), array_values($map), $string);
  109. return $string;
  110. }
  111. }