OSDN Git Service

merged from v3.31sp1
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / libs / ITEMACTIONS.php
1 <?php
2 /*
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4  * Copyright (C) 2002-2007 The Nucleus Group
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  * (see nucleus/documentation/index.html#license for more info)
11  */
12 /**
13  * This class to parse item templates
14  *
15  * @license http://nucleuscms.org/license.txt GNU General Public License
16  * @copyright Copyright (C) 2002-2007 The Nucleus Group
17  * @version $Id: ITEMACTIONS.php,v 1.6 2008-02-08 09:31:22 kimitake Exp $
18  * @version $NucleusJP: ITEMACTIONS.php,v 1.5.2.3 2007/10/30 19:05:20 kmorimatsu Exp $
19  */
20 class ITEMACTIONS extends BaseActions {
21
22         // contains an assoc array with parameters that need to be included when
23         // generating links to items/archives/... (e.g. catid)
24         var $linkparams;
25
26         // true when the current user is a blog admin (and thus allowed to edit all items)
27         var $allowEditAll;
28
29         // timestamp of last visit
30         var $lastVisit;
31
32         // item currently being handled (mysql result object, see BLOG::showUsingQuery)
33         var $currentItem;
34
35         // reference to the blog currently being displayed
36         var $blog;
37
38         // associative array with template info (part name => contents)
39         var $template;
40
41         // true when comments need to be displayed
42         var $showComments;
43
44         function ITEMACTIONS(&$blog) {
45                 // call constructor of superclass first
46                 $this->BaseActions();
47
48                 // extra parameters for created links
49                 global $catid;
50                 if ($catid)
51                         $this->linkparams = array('catid' => $catid);
52
53                 // check if member is blog admin (and thus allowed to edit all items)
54                 global $member;
55                 $this->allowEditAll = ($member->isLoggedIn() && $member->blogAdminRights($blog->getID()));
56                 $this->setBlog($blog);
57         }
58
59         function getDefinedActions() {
60                 return array(
61                         'blogid',
62                         'title',
63                         'body',
64                         'more',
65                         'smartbody',
66                         'itemid',
67                         'morelink',
68                         'category',
69                         'categorylink',
70                         'author',
71                         'authorid',
72                         'authorlink',
73                         'catid',
74                         'karma',
75                         'date',
76                         'time',
77                         'query',
78                         'itemlink',
79                         'blogurl',
80                         'closed',
81                         'syndicate_title',
82                         'syndicate_description',
83                         'karmaposlink',
84                         'karmaneglink',
85                         'new',
86                         'image',
87                         'popup',
88                         'media',
89                         'daylink',
90                         'query',
91                         'include',
92                         'phpinclude',
93                         'parsedinclude',
94                         'skinfile',
95                         'set',
96                         'plugin',
97                         'edit',
98                         'editlink',
99                         'editpopupcode',
100                         'comments',
101                         'relevance'/*,
102                         'if',
103                         'else',
104                         'endif',
105                         'elseif',
106                         'ifnot',
107                         'elseifnot'*/
108                 );
109         }
110
111         function setLastVisit($lastVisit) {
112                 $this->lastVisit = $lastVisit;
113         }
114         
115         function setParser(&$parser) {
116                 $this->parser =& $parser;
117         }
118         
119         function setCurrentItem(&$item) {
120                 $this->currentItem =& $item;
121         }
122         
123         function setBlog(&$blog) {
124                 $this->blog =& $blog;
125         }
126         
127         function setTemplate($template) {
128                 $this->template =& $template;
129         }
130         
131         function setShowComments($val) {
132                 $this->showComments = $val;
133         }
134
135         // methods used by parser to insert content
136
137         function parse_blogid() {
138                 echo $this->blog->getID();
139         }
140         
141         function parse_body() {
142                 $this->highlightAndParse($this->currentItem->body);
143         }
144         
145         function parse_more() {
146                 $this->highlightAndParse($this->currentItem->more);
147         }
148         
149         function parse_itemid() {
150                 echo $this->currentItem->itemid;
151         }
152         
153         function parse_category() {
154                 echo $this->currentItem->category;
155         }
156         
157         function parse_categorylink() {
158                 echo createLink('category', array('catid' => $this->currentItem->catid, 'name' => $this->currentItem->category));
159         }
160         
161         function parse_catid() {
162                 echo $this->currentItem->catid;
163         }
164         
165         function parse_authorid() {
166                 echo $this->currentItem->authorid;
167         }
168         
169         function parse_authorlink() {
170                 echo createLink(
171                         'member',
172                         array(
173                                 'memberid' => $this->currentItem->authorid,
174                                 'name' => $this->currentItem->author,
175                                 'extra' => $this->linkparams
176                         )
177                 );
178         }
179         
180         function parse_query() {
181                 echo $this->strHighlight;
182         }
183         
184         function parse_itemlink() {
185                 echo createLink(
186                         'item',
187                         array(
188                                 'itemid' => $this->currentItem->itemid,
189                                 'title' => $this->currentItem->title,
190                                 'timestamp' => $this->currentItem->timestamp,
191                                 'extra' => $this->linkparams
192                         )
193                 );
194         }
195         
196         function parse_blogurl() {
197                 echo $this->blog->getURL();
198         }
199         
200         function parse_closed() {
201                 echo $this->currentItem->closed;
202         }
203         
204         function parse_relevance() {
205                 echo round($this->currentItem->score,2);
206         }
207
208         function parse_title($format = '') {
209                 if (is_array($this->currentItem)) {
210                         $itemtitle = $this->currentItem['title'];
211                 } elseif (is_object($this->currentItem)) {
212                         $itemtitle = $this->currentItem->title;
213                 }
214                 switch ($format) {
215                         case 'xml':
216 //                              echo stringToXML ($this->currentItem->title);
217                                 echo stringToXML ($itemtitle);
218                                 break;
219                         case 'attribute':
220 //                              echo stringToAttribute ($this->currentItem->title);
221                                 echo stringToAttribute ($itemtitle);
222                                 break;
223                         case 'raw':
224 //                              echo $this->currentItem->title;
225                                 echo $itemtitle;
226                                 break;
227                         default:
228 //                              $this->highlightAndParse($this->currentItem->title);
229                                 $this->highlightAndParse($itemtitle);
230                                 break;
231                 }
232         }
233
234         function parse_karma($type = 'totalscore') {
235                 global $manager;
236
237                 // get karma object
238                 $karma =& $manager->getKarma($this->currentItem->itemid);
239
240                 switch($type) {
241                         case 'pos':
242                                 echo $karma->getNbPosVotes();
243                                 break;
244                         case 'neg':
245                                 echo $karma->getNbNegVotes();
246                                 break;
247                         case 'votes':
248                                 echo $karma->getNbOfVotes();
249                                 break;
250                         case 'posp':
251                                 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbPosVotes() / $karma->getNbOfVotes()) : 50;
252                                 echo number_format($percentage,2), '%';
253                                 break;
254                         case 'negp':
255                                 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbNegVotes() / $karma->getNbOfVotes()) : 50;
256                                 echo number_format($percentage,2), '%';
257                                 break;
258                         case 'totalscore':
259                         default:
260                                 echo $karma->getTotalScore();
261                                 break;
262                 }
263
264         }
265
266         function parse_author($which = '') {
267                 switch($which)
268                 {
269                         case 'realname':
270                                 echo $this->currentItem->authorname;
271                                 break;
272                         case 'id':
273                                 echo $this->currentItem->authorid;
274                                 break;
275                         case 'email':
276                                 echo $this->currentItem->authormail;
277                                 break;
278                         case 'url':
279                                 echo $this->currentItem->authorurl;
280                                 break;
281                         case 'name':
282                         default:
283                                 echo $this->currentItem->author;
284                 }
285         }
286
287         function parse_smartbody() {
288                 if (!$this->currentItem->more) {
289                         $this->highlightAndParse($this->currentItem->body);
290                 } else {
291                         $this->highlightAndParse($this->currentItem->more);
292                 }
293         }
294
295         function parse_morelink() {
296                 if ($this->currentItem->more)
297                         $this->parser->parse($this->template['MORELINK']);
298         }
299
300         function parse_date($format = '') {
301                 echo formatDate($format, $this->currentItem->timestamp, $this->template['FORMAT_DATE'], $this->blog);
302         }
303
304         /**
305           * @param format optional strftime format
306           */
307         function parse_time($format = '') {
308                 echo strftime($format ? $format : $this->template['FORMAT_TIME'],$this->currentItem->timestamp);
309         }
310
311         /**
312           * @param maxLength optional maximum length
313           */
314         function parse_syndicate_title($maxLength = 100) {
315                 $syndicated = strip_tags($this->currentItem->title);
316                 echo htmlspecialchars(shorten($syndicated,$maxLength,'...'),ENT_QUOTES);
317         }
318
319         /**
320           * @param maxLength optional maximum length
321           */
322         function parse_syndicate_description($maxLength = 250, $addHighlight = 0) {
323                 $syndicated = strip_tags($this->currentItem->body);
324                 if ($addHighlight) {
325                         $tmp_highlight = htmlspecialchars(shorten($syndicated,$maxLength,'...'),ENT_QUOTES);
326                         echo $this->highlightAndParse($tmp_highlight);
327                 } else {
328                         echo htmlspecialchars(shorten($syndicated,$maxLength,'...'),ENT_QUOTES);
329                 }
330         }
331
332         function parse_karmaposlink($text = '') {
333                 global $CONF;
334                 $link = $CONF['ActionURL'] . '?action=votepositive&amp;itemid='.$this->currentItem->itemid;
335                 echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;
336         }
337
338         function parse_karmaneglink($text = '') {
339                 global $CONF;
340                 $link = $CONF['ActionURL'] . '?action=votenegative&amp;itemid='.$this->currentItem->itemid;
341                 echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;
342         }
343
344         function parse_new() {
345                 if (($this->lastVisit != 0) && ($this->currentItem->timestamp > $this->lastVisit))
346                         echo $this->template['NEW'];
347         }
348
349
350         function parse_daylink() {
351                 echo createArchiveLink($this->blog->getID(), strftime('%Y-%m-%d',$this->currentItem->timestamp), $this->linkparams);
352         }
353
354         function parse_comments($maxToShow = 0) {
355                 if ($maxToShow == 0)
356                         $maxToShow = $this->blog->getMaxComments();
357
358                 // add comments
359                 if ($this->showComments && $this->blog->commentsEnabled()) {
360                         $comments =& new COMMENTS($this->currentItem->itemid);
361                         $comments->setItemActions($this);
362                         $comments->showComments($this->template, $maxToShow, $this->currentItem->closed ? 0 : 1, $this->strHighlight);
363                 }
364         }
365
366         /**
367           * Executes a plugin templatevar
368           *
369           * @param pluginName name of plugin (without the NP_)
370           *
371           * extra parameters can be added
372           */
373         function parse_plugin($pluginName) {
374                 global $manager;
375
376                 // only continue when the plugin is really installed
377                 if (!$manager->pluginInstalled('NP_' . $pluginName))
378                         return;
379
380                 $plugin =& $manager->getPlugin('NP_' . $pluginName);
381                 if (!$plugin) return;
382
383                 // get arguments
384                 $params = func_get_args();
385
386                 // remove plugin name
387                 array_shift($params);
388
389                 // add item reference (array_unshift didn't work)
390                 $params = array_merge(array(&$this->currentItem),$params);
391
392                 call_user_func_array(array(&$plugin,'doTemplateVar'), $params);
393         }
394
395         function parse_edit() {
396                 global $member, $CONF;
397                 if ($this->allowEditAll || ($member->isLoggedIn() && ($member->getID() == $this->currentItem->authorid)) ) {
398                         $this->parser->parse($this->template['EDITLINK']);
399                 }
400         }
401
402         function parse_editlink() {
403                 global $CONF;
404                 echo $CONF['AdminURL'],'bookmarklet.php?action=edit&amp;itemid=',$this->currentItem->itemid;
405         }
406
407         function parse_editpopupcode() {
408                 echo "if (event &amp;&amp; event.preventDefault) event.preventDefault();winbm=window.open(this.href,'nucleusbm','scrollbars=yes,width=600,height=550,left=10,top=10,status=yes,resizable=yes');winbm.focus();return false;";
409         }
410
411         // helper functions
412
413         /**
414          * Parses highlighted text, with limited actions only (to prevent not fully trusted team members
415          * from hacking your weblog.
416          * 'plugin variables in items' implementation by Andy
417          */
418         function highlightAndParse(&$data) {
419                 $actions =& new BODYACTIONS($this->blog);
420                 $parser =& new PARSER($actions->getDefinedActions(), $actions);
421                 $actions->setTemplate($this->template);
422                 $actions->setHighlight($this->strHighlight);
423                 $actions->setCurrentItem($this->currentItem);
424                 //$actions->setParser($parser);
425                 $parser->parse($actions->highlight($data));
426         }
427
428         /*
429         // this is the function previous to the 'plugin variables in items' implementation by Andy
430         function highlightAndParse(&$data) {
431                 // allow only a limited subset of actions (do not allow includes etc, they might be evil)
432                 $this->parser->actions = array('image','media','popup');
433                 $tmp_highlight = $this->highlight($data);
434                 $this->parser->parse($tmp_highlight);
435                 $this->parser->actions = $this->getDefinedActions();
436         }
437         */
438
439 }
440         
441
442
443 ?>