/
EReadMore.php
133 lines (129 loc) · 3.48 KB
/
EReadMore.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* EReadMore
* ---------
* A simple extension to shorten an html code.
*
* ###Requirements
* - [DOMDocument](http://www.php.net/manual/en/intro.dom.php)
*
* ###Description
* This a common functionality found in blogs, where you see a part
* of an article and there is link usually named read more where
* get redirected to the full article.
*
* ###Use
* ####Simple case
* In the view add:
* ~~~
* [php]
* <?php $this->beginWidget('application.extensions.EReadMore.EReadMore', array(
* 'linkUrl'=>$data->url
* ));?>
* <div>
* <h1>EReadMore</h1>
* <p>A simple extension to shorten an html code.</p>
* <h2>Requirements</h2>
* <ul>
* <li><a href='http://www.php.net/manual/en/intro.dom.php' >DOMDocument</a></li>
* </ul>
* </div>
* <?php $this->endWidget(); ?>
* ~~~
* ####Advanced use
* Check the EReadMore code options to see what you can set.
*
* ###Resources
* - [Github repo](http://www.github.com/dmtrs/EReadMore)
* - [Extension site](http://www.yiiframework.com/extension/ereadmore/)
*
* @version 1.0
* @author Dimitrios Mengidis <tydeas.dr@gmail.com>
*/
class EReadMore extends CWidget
{
/**
* @var boolean show the 'Read more...' like link.
*/
public $showLink = true;
/**
* @var array link html options.
* @since 1.0
*/
public $linkHtmlOptions = array();
/**
* @var string link label, default 'Read more...'.
* @since 1.0
*/
public $linkText = 'Read more...';
/**
* @var string link url.
* @since 1.0
*/
public $linkUrl;
/**
* @var string the short version html.
* @since 1.0
*/
public $short = '';
/**
* @var string tag name of the short verion root.
* @since 1.0
*/
public $root = 'div';
/**
* @var array html options of the short version root element.
* @since 1.0
*/
public $rootHtmlOptions = array('classs'=>'ereadmore-short');
/**
* @var string nodename of the last html element rendered.
* @since 1.0
*/
public $nodeName = 'p';
/**
* @var boolean if short version will be returned or echoed.
* @since 1.0
*/
public $return = false;
/**
* @var DOMDocument domdocument used to split the html.
* @since 1.0
*/
private $doc;
public function init()
{
ob_start();
$this->doc = new DOMDocument();
parent::init();
}
public function run()
{
$html = ob_get_clean();
$this->doc->loadHTML($html);
$this->shortHtml();
if($this->return)
return $this->short;
else
echo $this->short;
}
private function shortHtml()
{
foreach($this->doc->getElementsByTagName('body') as $body)
{
$shortdom = new DOMDocument();
$root = CHtml::tag($this->root, $this->rootHtmlOptions);
$shortdom->loadHTML($root);
foreach($body->childNodes as $node)
{
$newnode = $shortdom->importNode($node, true);
$shortdom->documentElement->appendChild($newnode);
if($node->nodeName == $this->nodeName)
break;
}
$this->short = $shortdom->saveHTML();
if($this->showLink)
$this->short .= CHtml::link($this->linkText, $this->linkUrl, $this->linkHtmlOptions);
}
}
}