TypographicBehavior.php 5.2 KB

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