2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Text Helper
|
|
|
|
*
|
|
|
|
* Text manipulations: Highlight, excerpt, truncate, strip of links, convert email addresses to mailto: links...
|
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2012-03-13 02:46:46 +00:00
|
|
|
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2012-03-13 02:46:46 +00:00
|
|
|
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.View.Helper
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 0.10.0.1076
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-12-04 18:10:24 +00:00
|
|
|
App::uses('AppHelper', 'View/Helper');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Text helper library.
|
|
|
|
*
|
|
|
|
* Text manipulations: Highlight, excerpt, truncate, strip of links, convert email addresses to mailto: links...
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.View.Helper
|
2011-08-01 00:28:35 +00:00
|
|
|
* @property HtmlHelper $Html
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html
|
2012-02-07 01:05:39 +00:00
|
|
|
* @see String
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class TextHelper extends AppHelper {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-07-03 18:55:42 +00:00
|
|
|
/**
|
|
|
|
* helpers
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $helpers = array('Html');
|
|
|
|
|
2011-10-29 16:08:43 +00:00
|
|
|
/**
|
|
|
|
* An array of md5sums and their contents.
|
|
|
|
* Used when inserting links into text.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_placeholders = array();
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-02-07 01:05:39 +00:00
|
|
|
* String utility instance
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-14 16:14:18 +00:00
|
|
|
protected $_engine;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-02-07 01:05:39 +00:00
|
|
|
* Constructor
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-02-14 16:26:55 +00:00
|
|
|
* ### Settings:
|
|
|
|
*
|
|
|
|
* - `engine` Class name to use to replace String functionality.
|
|
|
|
* The class needs to be placed in the `Utility` directory.
|
|
|
|
*
|
2012-02-07 01:05:39 +00:00
|
|
|
* @param View $View the view object the helper is attached to.
|
|
|
|
* @param array $settings Settings array Settings array
|
2012-03-03 22:10:12 +00:00
|
|
|
* @throws CakeException when the engine class could not be found.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2012-02-07 01:05:39 +00:00
|
|
|
public function __construct(View $View, $settings = array()) {
|
2012-03-11 01:57:18 +00:00
|
|
|
$settings = Hash::merge(array('engine' => 'String'), $settings);
|
2012-02-07 01:05:39 +00:00
|
|
|
parent::__construct($View, $settings);
|
2012-02-14 15:56:48 +00:00
|
|
|
list($plugin, $engineClass) = pluginSplit($settings['engine'], true);
|
|
|
|
App::uses($engineClass, $plugin . 'Utility');
|
2012-02-13 02:12:29 +00:00
|
|
|
if (class_exists($engineClass)) {
|
2012-02-14 16:14:18 +00:00
|
|
|
$this->_engine = new $engineClass($settings);
|
2012-02-13 02:12:29 +00:00
|
|
|
} else {
|
|
|
|
throw new CakeException(__d('cake_dev', '%s could not be found', $engineClass));
|
|
|
|
}
|
2012-02-07 01:05:39 +00:00
|
|
|
}
|
|
|
|
|
2012-02-14 16:26:55 +00:00
|
|
|
/**
|
|
|
|
* Call methods from String utility class
|
|
|
|
*/
|
2012-02-07 01:05:39 +00:00
|
|
|
public function __call($method, $params) {
|
2012-02-14 16:14:18 +00:00
|
|
|
return call_user_func_array(array($this->_engine, $method), $params);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Adds links (<a href=....) to a given text, by finding text that begins with
|
|
|
|
* strings like http:// and ftp://.
|
|
|
|
*
|
2011-10-29 16:08:43 +00:00
|
|
|
* ### Options
|
|
|
|
*
|
|
|
|
* - `escape` Control HTML escaping of input. Defaults to true.
|
|
|
|
*
|
|
|
|
* @param string $text Text
|
|
|
|
* @param array $options Array of HTML options, and options listed above.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string The text with links
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoLinkUrls
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-10-29 16:08:43 +00:00
|
|
|
public function autoLinkUrls($text, $options = array()) {
|
|
|
|
$this->_placeholders = array();
|
|
|
|
$options += array('escape' => true);
|
|
|
|
|
2010-07-03 18:55:42 +00:00
|
|
|
$text = preg_replace_callback(
|
2011-09-21 20:50:48 +00:00
|
|
|
'#(?<!href="|src="|">)((?:https?|ftp|nntp)://[^\s<>()]+)#i',
|
2011-10-29 16:08:43 +00:00
|
|
|
array(&$this, '_insertPlaceHolder'),
|
2010-07-03 18:55:42 +00:00
|
|
|
$text
|
|
|
|
);
|
2011-10-29 16:08:43 +00:00
|
|
|
$text = preg_replace_callback(
|
2012-02-14 13:08:37 +00:00
|
|
|
'#(?<!href="|">)(?<!\b[[:punct:]])(?<!http://|https://|ftp://|nntp://)www.[^\n\%\ <]+[^<\n\%\,\.\ <](?<!\))#i',
|
2011-10-29 16:08:43 +00:00
|
|
|
array(&$this, '_insertPlaceHolder'),
|
2010-07-03 18:55:42 +00:00
|
|
|
$text
|
|
|
|
);
|
2011-10-29 16:08:43 +00:00
|
|
|
if ($options['escape']) {
|
|
|
|
$text = h($text);
|
|
|
|
}
|
|
|
|
return $this->_linkUrls($text, $options);
|
2010-07-03 18:55:42 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-03 18:55:42 +00:00
|
|
|
/**
|
2011-10-29 16:08:43 +00:00
|
|
|
* Saves the placeholder for a string, for later use. This gets around double
|
|
|
|
* escaping content in URL's.
|
2010-07-03 18:55:42 +00:00
|
|
|
*
|
2011-10-29 16:08:43 +00:00
|
|
|
* @param array $matches An array of regexp matches.
|
|
|
|
* @return string Replaced values.
|
2010-07-03 18:55:42 +00:00
|
|
|
*/
|
2011-10-29 16:08:43 +00:00
|
|
|
protected function _insertPlaceHolder($matches) {
|
|
|
|
$key = md5($matches[0]);
|
|
|
|
$this->_placeholders[$key] = $matches[0];
|
|
|
|
return $key;
|
2010-07-03 18:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-29 16:08:43 +00:00
|
|
|
* Replace placeholders with links.
|
2010-07-03 18:55:42 +00:00
|
|
|
*
|
2011-10-29 16:08:43 +00:00
|
|
|
* @param string $text The text to operate on.
|
|
|
|
* @param array $htmlOptions The options for the generated links.
|
|
|
|
* @return string The text with links inserted.
|
2010-07-03 18:55:42 +00:00
|
|
|
*/
|
2011-10-29 16:08:43 +00:00
|
|
|
protected function _linkUrls($text, $htmlOptions) {
|
|
|
|
$replace = array();
|
|
|
|
foreach ($this->_placeholders as $md5 => $url) {
|
|
|
|
$link = $url;
|
|
|
|
if (!preg_match('#^[a-z]+\://#', $url)) {
|
|
|
|
$url = 'http://' . $url;
|
|
|
|
}
|
|
|
|
$replace[$md5] = $this->Html->link($link, $url, $htmlOptions);
|
|
|
|
}
|
|
|
|
return strtr($text, $replace);
|
2010-07-03 18:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Links email addresses
|
|
|
|
*
|
2011-10-29 16:08:43 +00:00
|
|
|
* @param string $text The text to operate on
|
|
|
|
* @param array $options An array of options to use for the HTML.
|
2010-07-03 18:55:42 +00:00
|
|
|
* @return string
|
2011-10-29 16:08:43 +00:00
|
|
|
* @see TextHelper::autoLinkEmails()
|
2010-07-03 18:55:42 +00:00
|
|
|
*/
|
2011-10-29 16:08:43 +00:00
|
|
|
protected function _linkEmails($text, $options) {
|
|
|
|
$replace = array();
|
|
|
|
foreach ($this->_placeholders as $md5 => $url) {
|
|
|
|
$replace[$md5] = $this->Html->link($url, 'mailto:' . $url, $options);
|
|
|
|
}
|
|
|
|
return strtr($text, $replace);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Adds email links (<a href="mailto:....) to a given text.
|
|
|
|
*
|
2011-10-29 16:08:43 +00:00
|
|
|
* ### Options
|
|
|
|
*
|
|
|
|
* - `escape` Control HTML escaping of input. Defaults to true.
|
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $text Text
|
2011-10-29 16:08:43 +00:00
|
|
|
* @param array $options Array of HTML options, and options listed above.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string The text with links
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoLinkEmails
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function autoLinkEmails($text, $options = array()) {
|
2011-10-29 16:08:43 +00:00
|
|
|
$options += array('escape' => true);
|
|
|
|
$this->_placeholders = array();
|
|
|
|
|
2011-01-17 16:11:30 +00:00
|
|
|
$atom = '[a-z0-9!#$%&\'*+\/=?^_`{|}~-]';
|
2011-10-29 16:08:43 +00:00
|
|
|
$text = preg_replace_callback(
|
2011-03-27 23:09:46 +00:00
|
|
|
'/(' . $atom . '+(?:\.' . $atom . '+)*@[a-z0-9-]+(?:\.[a-z0-9-]+)+)/i',
|
2011-10-29 16:08:43 +00:00
|
|
|
array(&$this, '_insertPlaceholder'),
|
2010-07-03 18:55:42 +00:00
|
|
|
$text
|
2011-08-16 03:55:08 +00:00
|
|
|
);
|
2011-10-29 16:08:43 +00:00
|
|
|
if ($options['escape']) {
|
|
|
|
$text = h($text);
|
|
|
|
}
|
|
|
|
return $this->_linkEmails($text, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-12-02 05:58:09 +00:00
|
|
|
* Convert all links and email addresses to HTML links.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-10-29 16:08:43 +00:00
|
|
|
* ### Options
|
|
|
|
*
|
|
|
|
* - `escape` Control HTML escaping of input. Defaults to true.
|
|
|
|
*
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param string $text Text
|
2011-10-29 16:08:43 +00:00
|
|
|
* @param array $options Array of HTML options, and options listed above.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string The text with links
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoLink
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function autoLink($text, $options = array()) {
|
2011-10-29 16:08:43 +00:00
|
|
|
$text = $this->autoLinkUrls($text, $options);
|
|
|
|
return $this->autoLinkEmails($text, array_merge($options, array('escape' => false)));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-02-07 01:05:39 +00:00
|
|
|
* @see String::highlight()
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-02-07 01:05:39 +00:00
|
|
|
* @param string $text Text to search the phrase in
|
|
|
|
* @param string $phrase The phrase that will be searched
|
|
|
|
* @param array $options An array of html attributes and options.
|
|
|
|
* @return string The highlighted text
|
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::highlight
|
|
|
|
*/
|
|
|
|
public function highlight($text, $phrase, $options = array()) {
|
2012-02-14 16:14:18 +00:00
|
|
|
return $this->_engine->highlight($text, $phrase, $options);
|
2012-02-07 01:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see String::stripLinks()
|
2009-10-29 03:23:44 +00:00
|
|
|
*
|
2012-02-07 01:05:39 +00:00
|
|
|
* @param string $text Text
|
|
|
|
* @return string The text without links
|
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::stripLinks
|
|
|
|
*/
|
|
|
|
public function stripLinks($text) {
|
2012-02-14 16:14:18 +00:00
|
|
|
return $this->_engine->stripLinks($text);
|
2012-02-07 01:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see String::truncate()
|
2009-10-29 03:23:44 +00:00
|
|
|
*
|
2011-07-29 02:45:47 +00:00
|
|
|
* @param string $text String to truncate.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @param integer $length Length of returned string, including ellipsis.
|
2009-10-29 03:23:44 +00:00
|
|
|
* @param array $options An array of html attributes and options.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return string Trimmed string.
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::truncate
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function truncate($text, $length = 100, $options = array()) {
|
2012-02-14 16:14:18 +00:00
|
|
|
return $this->_engine->truncate($text, $length, $options);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-02-07 01:05:39 +00:00
|
|
|
* @see String::excerpt()
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $text String to search the phrase in
|
|
|
|
* @param string $phrase Phrase that will be searched for
|
|
|
|
* @param integer $radius The amount of characters that will be returned on each side of the founded phrase
|
|
|
|
* @param string $ending Ending that will be appended
|
|
|
|
* @return string Modified string
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::excerpt
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function excerpt($text, $phrase, $radius = 100, $ending = '...') {
|
2012-02-14 16:14:18 +00:00
|
|
|
return $this->_engine->excerpt($text, $phrase, $radius, $ending);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2012-02-07 01:05:39 +00:00
|
|
|
* @see String::toList()
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param array $list The list to be joined
|
2009-11-25 03:06:55 +00:00
|
|
|
* @param string $and The word used to join the last and second last items together with. Defaults to 'and'
|
2011-12-02 05:58:09 +00:00
|
|
|
* @param string $separator The separator used to join all the other items together. Defaults to ', '
|
2009-11-25 03:06:55 +00:00
|
|
|
* @return string The glued together string.
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::toList
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function toList($list, $and = 'and', $separator = ', ') {
|
2012-02-14 16:14:18 +00:00
|
|
|
return $this->_engine->toList($list, $and, $separator);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2012-02-07 01:05:39 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|