TypographicBehavior.php 2.9 KB

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