TypographicBehavior.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. if (empty($this->settings[$Model->alias]['fields'])) {
  71. $schema = $Model->schema();
  72. $fields = array();
  73. foreach ($schema as $field => $v) {
  74. if (!in_array($v['type'], array('string', 'text'))) {
  75. continue;
  76. }
  77. if (!empty($v['key'])) {
  78. continue;
  79. }
  80. if (isset($v['length']) && $v['length'] === 1) { //TODO: also skip UUID (lenght 36)?
  81. continue;
  82. }
  83. $fields[] = $field;
  84. }
  85. $this->settings[$Model->alias]['fields'] = $fields;
  86. }
  87. }
  88. public function beforeValidate(Model $Model) {
  89. parent::beforeValidate($Model);
  90. if ($this->settings[$Model->alias]['before'] == 'validate') {
  91. $this->process($Model);
  92. }
  93. return true;
  94. }
  95. public function beforeSave(Model $Model) {
  96. parent::beforeSave($Model);
  97. if ($this->settings[$Model->alias]['before'] == 'save') {
  98. $this->process($Model);
  99. }
  100. return true;
  101. }
  102. /**
  103. * Run the behavior over all records of this model
  104. * This is useful if you attach it after some records have already been saved without it.
  105. * @param object $Model Model about to be saved.
  106. * @return int $count Number of affected/changed records
  107. * 2012-08-07 ms
  108. */
  109. public function updateTypography(Model $Model, $dryRun = false) {
  110. $records = $Model->find('all'); //TODO: in multiple runs with limit
  111. $count = 0;
  112. foreach ($records as $record) {
  113. $changed = false;
  114. foreach ($this->settings[$Model->alias]['fields'] as $field) {
  115. if (empty($record[$Model->alias][$field])) {
  116. continue;
  117. }
  118. $tmp = $this->_prepareInput($record[$Model->alias][$field]);
  119. if ($tmp == $record[$Model->alias][$field]) {
  120. continue;
  121. }
  122. $record[$Model->alias][$field] = $tmp;
  123. $changed = true;
  124. }
  125. if ($changed) {
  126. if (!$dryRun) {
  127. $Model->save($record, false);
  128. }
  129. $count++;
  130. }
  131. }
  132. return $count;
  133. }
  134. /**
  135. * Run before a model is saved
  136. *
  137. * @param object $Model Model about to be saved.
  138. * @return boolean true if save should proceed, false otherwise
  139. * @access public
  140. */
  141. public function process(Model $Model, $return = true) {
  142. foreach ($this->settings[$Model->alias]['fields'] as $field) {
  143. if (!empty($Model->data[$Model->alias][$field])) {
  144. $Model->data[$Model->alias][$field] = $this->_prepareInput($Model->data[$Model->alias][$field]);
  145. }
  146. }
  147. return $return;
  148. }
  149. /**
  150. * @param string $input
  151. * @return string $cleanedInput
  152. * 2011-12-06 ms
  153. */
  154. protected function _prepareInput($string) {
  155. $map = $this->map['in'];
  156. //return $string;
  157. $string = str_replace(array_keys($map), array_values($map), $string);
  158. return $string;
  159. }
  160. }