TypographicBehavior.php 5.0 KB

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