Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * Text Helper
4
 *
5
 * Text manipulations: Highlight, excerpt, truncate, strip of links, convert email addresses to mailto: links...
6
 *
7
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
9
 *
10
 * Licensed under The MIT License
11
 * For full copyright and license information, please see the LICENSE.txt
12
 * Redistributions of files must retain the above copyright notice.
13
 *
14
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
15
 * @link          http://cakephp.org CakePHP(tm) Project
16
 * @package       Cake.View.Helper
17
 * @since         CakePHP(tm) v 0.10.0.1076
18
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
19
 */
20
 
21
App::uses('AppHelper', 'View/Helper');
22
App::uses('Hash', 'Utility');
23
 
24
/**
25
 * Text helper library.
26
 *
27
 * Text manipulations: Highlight, excerpt, truncate, strip of links, convert email addresses to mailto: links...
28
 *
29
 * @package       Cake.View.Helper
30
 * @property      HtmlHelper $Html
31
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html
32
 * @see String
33
 */
34
class TextHelper extends AppHelper {
35
 
36
/**
37
 * helpers
38
 *
39
 * @var array
40
 */
41
	public $helpers = array('Html');
42
 
43
/**
44
 * An array of md5sums and their contents.
45
 * Used when inserting links into text.
46
 *
47
 * @var array
48
 */
49
	protected $_placeholders = array();
50
 
51
/**
52
 * String utility instance
53
 *
54
 * @var stdClass
55
 */
56
	protected $_engine;
57
 
58
/**
59
 * Constructor
60
 *
61
 * ### Settings:
62
 *
63
 * - `engine` Class name to use to replace String functionality.
64
 *            The class needs to be placed in the `Utility` directory.
65
 *
66
 * @param View $View the view object the helper is attached to.
67
 * @param array $settings Settings array Settings array
68
 * @throws CakeException when the engine class could not be found.
69
 */
70
	public function __construct(View $View, $settings = array()) {
71
		$settings = Hash::merge(array('engine' => 'String'), $settings);
72
		parent::__construct($View, $settings);
73
		list($plugin, $engineClass) = pluginSplit($settings['engine'], true);
74
		App::uses($engineClass, $plugin . 'Utility');
75
		if (class_exists($engineClass)) {
76
			$this->_engine = new $engineClass($settings);
77
		} else {
78
			throw new CakeException(__d('cake_dev', '%s could not be found', $engineClass));
79
		}
80
	}
81
 
82
/**
83
 * Call methods from String utility class
84
 * @return mixed Whatever is returned by called method, or false on failure
85
 */
86
	public function __call($method, $params) {
87
		return call_user_func_array(array($this->_engine, $method), $params);
88
	}
89
 
90
/**
91
 * Adds links (<a href=....) to a given text, by finding text that begins with
92
 * strings like http:// and ftp://.
93
 *
94
 * ### Options
95
 *
96
 * - `escape` Control HTML escaping of input. Defaults to true.
97
 *
98
 * @param string $text Text
99
 * @param array $options Array of HTML options, and options listed above.
100
 * @return string The text with links
101
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoLinkUrls
102
 */
103
	public function autoLinkUrls($text, $options = array()) {
104
		$this->_placeholders = array();
105
		$options += array('escape' => true);
106
 
107
		$pattern = '#(?<!href="|src="|">)((?:https?|ftp|nntp)://[\p{L}0-9.\-:]+(?:[/?][^\s<]*)?)#ui';
108
		$text = preg_replace_callback(
109
			$pattern,
110
			array(&$this, '_insertPlaceHolder'),
111
			$text
112
		);
113
		$text = preg_replace_callback(
114
			'#(?<!href="|">)(?<!\b[[:punct:]])(?<!http://|https://|ftp://|nntp://)www.[^\n\%\ <]+[^<\n\%\,\.\ <](?<!\))#i',
115
			array(&$this, '_insertPlaceHolder'),
116
			$text
117
		);
118
		if ($options['escape']) {
119
			$text = h($text);
120
		}
121
		return $this->_linkUrls($text, $options);
122
	}
123
 
124
/**
125
 * Saves the placeholder for a string, for later use. This gets around double
126
 * escaping content in URL's.
127
 *
128
 * @param array $matches An array of regexp matches.
129
 * @return string Replaced values.
130
 */
131
	protected function _insertPlaceHolder($matches) {
132
		$key = md5($matches[0]);
133
		$this->_placeholders[$key] = $matches[0];
134
		return $key;
135
	}
136
 
137
/**
138
 * Replace placeholders with links.
139
 *
140
 * @param string $text The text to operate on.
141
 * @param array $htmlOptions The options for the generated links.
142
 * @return string The text with links inserted.
143
 */
144
	protected function _linkUrls($text, $htmlOptions) {
145
		$replace = array();
146
		foreach ($this->_placeholders as $hash => $url) {
147
			$link = $url;
148
			if (!preg_match('#^[a-z]+\://#', $url)) {
149
				$url = 'http://' . $url;
150
			}
151
			$replace[$hash] = $this->Html->link($link, $url, $htmlOptions);
152
		}
153
		return strtr($text, $replace);
154
	}
155
 
156
/**
157
 * Links email addresses
158
 *
159
 * @param string $text The text to operate on
160
 * @param array $options An array of options to use for the HTML.
161
 * @return string
162
 * @see TextHelper::autoLinkEmails()
163
 */
164
	protected function _linkEmails($text, $options) {
165
		$replace = array();
166
		foreach ($this->_placeholders as $hash => $url) {
167
			$replace[$hash] = $this->Html->link($url, 'mailto:' . $url, $options);
168
		}
169
		return strtr($text, $replace);
170
	}
171
 
172
/**
173
 * Adds email links (<a href="mailto:....) to a given text.
174
 *
175
 * ### Options
176
 *
177
 * - `escape` Control HTML escaping of input. Defaults to true.
178
 *
179
 * @param string $text Text
180
 * @param array $options Array of HTML options, and options listed above.
181
 * @return string The text with links
182
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoLinkEmails
183
 */
184
	public function autoLinkEmails($text, $options = array()) {
185
		$options += array('escape' => true);
186
		$this->_placeholders = array();
187
 
188
		$atom = '[\p{L}0-9!#$%&\'*+\/=?^_`{|}~-]';
189
		$text = preg_replace_callback(
190
			'/(?<=\s|^|\()(' . $atom . '*(?:\.' . $atom . '+)*@[\p{L}0-9-]+(?:\.[\p{L}0-9-]+)+)/ui',
191
			array(&$this, '_insertPlaceholder'),
192
			$text
193
		);
194
		if ($options['escape']) {
195
			$text = h($text);
196
		}
197
		return $this->_linkEmails($text, $options);
198
	}
199
 
200
/**
201
 * Convert all links and email addresses to HTML links.
202
 *
203
 * ### Options
204
 *
205
 * - `escape` Control HTML escaping of input. Defaults to true.
206
 *
207
 * @param string $text Text
208
 * @param array $options Array of HTML options, and options listed above.
209
 * @return string The text with links
210
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoLink
211
 */
212
	public function autoLink($text, $options = array()) {
213
		$text = $this->autoLinkUrls($text, $options);
214
		return $this->autoLinkEmails($text, array_merge($options, array('escape' => false)));
215
	}
216
 
217
/**
218
 * @see String::highlight()
219
 *
220
 * @param string $text Text to search the phrase in
221
 * @param string $phrase The phrase that will be searched
222
 * @param array $options An array of html attributes and options.
223
 * @return string The highlighted text
224
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::highlight
225
 */
226
	public function highlight($text, $phrase, $options = array()) {
227
		return $this->_engine->highlight($text, $phrase, $options);
228
	}
229
 
230
/**
231
 * Formats paragraphs around given text for all line breaks
232
 *  <br /> added for single line return
233
 *  <p> added for double line return
234
 *
235
 * @param string $text Text
236
 * @return string The text with proper <p> and <br /> tags
237
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoParagraph
238
 */
239
	public function autoParagraph($text) {
240
		if (trim($text) !== '') {
241
			$text = preg_replace('|<br[^>]*>\s*<br[^>]*>|i', "\n\n", $text . "\n");
242
			$text = preg_replace("/\n\n+/", "\n\n", str_replace(array("\r\n", "\r"), "\n", $text));
243
			$texts = preg_split('/\n\s*\n/', $text, -1, PREG_SPLIT_NO_EMPTY);
244
			$text = '';
245
			foreach ($texts as $txt) {
246
				$text .= '<p>' . nl2br(trim($txt, "\n")) . "</p>\n";
247
			}
248
			$text = preg_replace('|<p>\s*</p>|', '', $text);
249
		}
250
		return $text;
251
	}
252
 
253
/**
254
 * @see String::stripLinks()
255
 *
256
 * @param string $text Text
257
 * @return string The text without links
258
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::stripLinks
259
 */
260
	public function stripLinks($text) {
261
		return $this->_engine->stripLinks($text);
262
	}
263
 
264
/**
265
 * @see String::truncate()
266
 *
267
 * @param string $text String to truncate.
268
 * @param integer $length Length of returned string, including ellipsis.
269
 * @param array $options An array of html attributes and options.
270
 * @return string Trimmed string.
271
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::truncate
272
 */
273
	public function truncate($text, $length = 100, $options = array()) {
274
		return $this->_engine->truncate($text, $length, $options);
275
	}
276
 
277
/**
278
 * @see String::excerpt()
279
 *
280
 * @param string $text String to search the phrase in
281
 * @param string $phrase Phrase that will be searched for
282
 * @param integer $radius The amount of characters that will be returned on each side of the founded phrase
283
 * @param string $ending Ending that will be appended
284
 * @return string Modified string
285
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::excerpt
286
 */
287
	public function excerpt($text, $phrase, $radius = 100, $ending = '...') {
288
		return $this->_engine->excerpt($text, $phrase, $radius, $ending);
289
	}
290
 
291
/**
292
 * @see String::toList()
293
 *
294
 * @param array $list The list to be joined
295
 * @param string $and The word used to join the last and second last items together with. Defaults to 'and'
296
 * @param string $separator The separator used to join all the other items together. Defaults to ', '
297
 * @return string The glued together string.
298
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::toList
299
 */
300
	public function toList($list, $and = 'and', $separator = ', ') {
301
		return $this->_engine->toList($list, $and, $separator);
302
	}
303
 
304
}