TypographicBehavior.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. * Settings are:
  16. * - string $before (validate/save)
  17. * - array $fields (leave empty for auto detection)
  18. * - bool $mergeQuotes (merge single and double into " or any custom char)
  19. *
  20. * TODOS:
  21. * - respect primary and secondary quotations marks as well as alternatives
  22. *
  23. * @author Mark Scherer
  24. * @cakephp 2.x
  25. * @license MIT
  26. * @link http://www.dereuromark.de/2012/08/12/typographic-behavior-and-typography-helper/
  27. * @link http://en.wikipedia.org/wiki/Non-English_usage_of_quotation_marks
  28. * 2011-01-13 ms
  29. */
  30. class TypographicBehavior extends ModelBehavior {
  31. protected $_map = array(
  32. 'in' => array(
  33. '‘' => '\'',
  34. //'&lsquo;' => '\'', # ‘
  35. '’' => '\'',
  36. //'&rsquo;' => '\'', # ’
  37. '‚' => '\'',
  38. //'&sbquo;' => '\'', # ‚
  39. '‛' => '\'',
  40. //'&#8219;' => '\'', # ‛
  41. '“' => '"',
  42. //'&ldquo;' => '"', # “
  43. '”' => '"',
  44. //'&rdquo;' => '"', # ”
  45. '„' => '"',
  46. //'&bdquo;' => '"', # „
  47. '‟' => '"',
  48. //'&#8223;' => '"', # ‟
  49. '«' => '"',
  50. //'&laquo;' => '"', # «
  51. '»' => '"',
  52. //'&raquo;' => '"', # »
  53. '‹' => '\'',
  54. //'&laquo;' => '\'', # ‹
  55. '›' => '\'',
  56. //'&raquo;' => '\'', # ›
  57. ),
  58. 'out'=> array(
  59. # use the TypographyHelper for this at runtime
  60. ),
  61. );
  62. protected $_defaults = array(
  63. 'before' => 'save',
  64. 'fields' => array(),
  65. 'mergeQuotes' => false, // set to true for " or explicitly set a char (" or ')
  66. );
  67. /**
  68. * Initiate behavior for the model using specified settings. Available settings:
  69. *
  70. *
  71. * @param object $Model Model using the behaviour
  72. * @param array $settings Settings to override for model.
  73. * @access public
  74. * 2011-12-06 ms
  75. */
  76. public function setup(Model $Model, $settings = array()) {
  77. if (!isset($this->settings[$Model->alias])) {
  78. $this->settings[$Model->alias] = $this->_defaults;
  79. }
  80. $this->settings[$Model->alias] = array_merge($this->settings[$Model->alias], $settings);
  81. if (empty($this->settings[$Model->alias]['fields'])) {
  82. $schema = $Model->schema();
  83. $fields = array();
  84. foreach ($schema as $field => $v) {
  85. if (!in_array($v['type'], array('string', 'text'))) {
  86. continue;
  87. }
  88. if (!empty($v['key'])) {
  89. continue;
  90. }
  91. if (isset($v['length']) && $v['length'] === 1) { //TODO: also skip UUID (lenght 36)?
  92. continue;
  93. }
  94. $fields[] = $field;
  95. }
  96. $this->settings[$Model->alias]['fields'] = $fields;
  97. }
  98. if ($this->settings[$Model->alias]['mergeQuotes'] === true) {
  99. $this->settings[$Model->alias]['mergeQuotes'] = '"';
  100. }
  101. }
  102. public function beforeValidate(Model $Model) {
  103. parent::beforeValidate($Model);
  104. if ($this->settings[$Model->alias]['before'] === 'validate') {
  105. $this->process($Model);
  106. }
  107. return true;
  108. }
  109. public function beforeSave(Model $Model) {
  110. parent::beforeSave($Model);
  111. if ($this->settings[$Model->alias]['before'] === 'save') {
  112. $this->process($Model);
  113. }
  114. return true;
  115. }
  116. /**
  117. * Run the behavior over all records of this model
  118. * This is useful if you attach it after some records have already been saved without it.
  119. * @param object $Model Model about to be saved.
  120. * @return int $count Number of affected/changed records
  121. * 2012-08-07 ms
  122. */
  123. public function updateTypography(Model $Model, $dryRun = false) {
  124. $options = array('recursive' => -1, 'limit' => 100, 'offset' => 0);
  125. $count = 0;
  126. while ($records = $Model->find('all', $options)) {
  127. foreach ($records as $record) {
  128. $changed = false;
  129. foreach ($this->settings[$Model->alias]['fields'] as $field) {
  130. if (empty($record[$Model->alias][$field])) {
  131. continue;
  132. }
  133. $tmp = $this->_prepareInput($Model, $record[$Model->alias][$field]);
  134. if ($tmp == $record[$Model->alias][$field]) {
  135. continue;
  136. }
  137. $record[$Model->alias][$field] = $tmp;
  138. $changed = true;
  139. }
  140. if ($changed) {
  141. if (!$dryRun) {
  142. $Model->save($record, false);
  143. }
  144. $count++;
  145. }
  146. }
  147. $options['offset'] += 100;
  148. }
  149. return $count;
  150. }
  151. /**
  152. * Run before a model is saved
  153. *
  154. * @param object $Model Model about to be saved.
  155. * @return boolean true if save should proceed, false otherwise
  156. * @access public
  157. */
  158. public function process(Model $Model, $return = true) {
  159. foreach ($this->settings[$Model->alias]['fields'] as $field) {
  160. if (!empty($Model->data[$Model->alias][$field])) {
  161. $Model->data[$Model->alias][$field] = $this->_prepareInput($Model, $Model->data[$Model->alias][$field]);
  162. }
  163. }
  164. return $return;
  165. }
  166. /**
  167. * @param string $input
  168. * @return string $cleanedInput
  169. * 2011-12-06 ms
  170. */
  171. protected function _prepareInput(Model $Model, $string) {
  172. $map = $this->_map['in'];
  173. if ($this->settings[$Model->alias]['mergeQuotes']) {
  174. foreach ($map as $key => $val) {
  175. $map[$key] = $this->settings[$Model->alias]['mergeQuotes'];
  176. }
  177. }
  178. return str_replace(array_keys($map), array_values($map), $string);
  179. }
  180. }