OSDN Git Service

50ad4c701b82a30b3211b65a3759fd82b665e242
[nucleus-jp/nucleus-plugins.git] / trunk / NP_ItemNaviEX / NP_ItemNaviEX.php
1 <?
2 /**
3  *
4  * BreadCrumbsList PLUG-IN FOR NucleusCMS
5  * PHP versions 4 and 5
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * (see nucleus/documentation/index.html#license for more info)
12  *
13  * @author     Original Author nakahara21
14  * @copyright  2005-2006 nakahara21
15  * @license     http://www.gnu.org/licenses/gpl.txt  GNU GENERAL PUBLIC LICENSE Version 2, June 1991
16  * @version    0.41
17  * @link       http://nakahara21.com
18  *
19  * 0.991        add sub-blog home mode
20  * 0.99         sec fix
21  *
22  **************************************************************************
23  *
24  * THESE PLUG-INS ARE DEDICATED TO ALL THOSE NucleusCMS USERS
25  * WHO FIGHT CORRUPTION AND IRRATIONAL IN EVERY DAY OF THEIR LIVES.
26  *
27  **************************************************************************/
28
29 class NP_ItemNaviEX extends NucleusPlugin
30 {
31         function getName()
32         {
33                 return 'Navigation Bar'; 
34         }
35
36         function getAuthor()
37         { 
38                 return 'nakahara21 + shizuki'; 
39         }
40
41         function getURL()
42         {
43                 return 'http://nakahara21.com/'; 
44         }
45
46         function getVersion()
47         {
48                 return '0.991'; 
49         }
50
51         function getDescription()
52         { 
53                 return 'Add link to prev item and next item. Usage: &lt;%ItemNaviEX()%&gt; or &lt;%ItemNaviEX(0)%&gt;';
54         }
55
56         function supportsFeature($what)
57         {
58                 switch($what){
59                         case 'SqlTablePrefix':
60                                 return 1;
61                         default:
62                                 return 0;
63                 }
64         }
65
66         function scanEndKey($array)
67         {
68                 $keys = array_keys($array);
69                 rsort($keys);
70                 return $keys[0];
71         }
72
73         function createNaviLink($unitArray)
74         {
75                 if ($unitArray[1]) {
76                         $tempLink = '<a href="' . htmlspecialchars($unitArray[1]) . '">' . htmlspecialchars($unitArray[0]) . '</a>';
77                 } else {
78                         $tempLink = htmlspecialchars($unitArray[0]);
79                 }
80                 return $tempLink;
81         }
82
83         function checkParent()
84         {
85                 global $manager; 
86 //              if ($manager->pluginInstalled('NP_MultipleCategories')) {
87 //                      $mplugin =& $manager->getPlugin('NP_MultipleCategories');
88 //                      if (method_exists($mplugin,"getRequestName")) {
89                                 $res = sql_query('SHOW FIELDS FROM ' . sql_table('plug_multiple_categories_sub'));
90                                 while ($co = mysql_fetch_assoc($res)) {
91                                         if ($co['Field'] == 'parentid') {
92                                                 return TRUE;
93                                         }
94                                 }
95 //                      }
96 //              }
97         }
98
99         function doSkinVar($skinType, $showHome = 1)
100         { 
101                 global $manager, $CONF, $blog, $itemid, $itemidprev, $itemidnext;
102                 global $catid, $subcatid, $archive, $archiveprev, $archivenext, $param; 
103
104 // sanitize
105                 $y = $m = $d = '';
106                 $itemid = intval($itemid);
107                 $catid = intval($catid);
108                 $subcatid = intval($subcatid);
109                 $itemidprev = intval($itemidprev);
110                 $itemidnext = intval($itemidnext);
111                 if (isset($archive)) {
112                         sscanf($archive,'%d-%d-%d', $y, $m, $d);
113                         if ($y && $m && !empty($d)) {
114                                 $archive = sprintf('%04d-%02d-%02d', $y, $m, $d);
115                         } elseif ($y && $m && empty($d)) {
116                                 $archive = sprintf('%04d-%02d', $y, $m);
117                         }                       
118                 }
119                 if (isset($archiveprev)) {
120                         sscanf($archiveprev,'%d-%d-%d', $y, $m, $d);
121                         if ($y && $m && !empty($d)) {
122                                 $archiveprev = sprintf('%04d-%02d-%02d', $y, $m, $d);
123                         } elseif ($y && $m && empty($d)) {
124                                 $archiveprev = sprintf('%04d-%02d', $y, $m);
125                         }                       
126                 }
127                 if (isset($archiveprev)) {
128                         sscanf($archiveprev,'%d-%d-%d', $y, $m, $d);
129                         if ($y && $m && !empty($d)) {
130                                 $archiveprev = sprintf('%04d-%02d-%02d', $y, $m, $d);
131                         } elseif ($y && $m && empty($d)) {
132                                 $archiveprev = sprintf('%04d-%02d', $y, $m);
133                         }                       
134                 }
135                 if ($showHome == '') {
136                         $showHome = '1';
137                 }
138
139                 if ($catid) { 
140                         $blogid = getBlogIDFromCatID($catid);
141                         $b =& $manager->getBlog($blogid); 
142                 } elseif ($blog) { 
143                         $b =& $blog; 
144                 } else { 
145                         $b =& $manager->getBlog($CONF['DefaultBlog']); 
146                 } 
147                 $blogid = intval($b->getID());
148
149                 $abuf = '';
150                 $mtable = '';
151                 $where .= ' and i.iblog=' . $blogid;
152                 if (!empty($catid)) {
153                         if ($manager->pluginInstalled('NP_MultipleCategories')) {
154                                 $where .= ' and ((i.inumber = p.item_id and (p.categories REGEXP "(^|,)' . $catid . '(,|$)"' .
155                                                 ' or i.icat = ' . $catid . ')) or (i.icat = ' . $catid . ' and p.item_id IS NULL))';
156                                 $mtable = ' LEFT JOIN ' . sql_table('plug_multiple_categories') . ' as p ON  i.inumber = p.item_id';
157                                 $mplugin =& $manager->getPlugin('NP_MultipleCategories');
158                                 if ($subcatid && method_exists($mplugin, 'getRequestName')) {
159 //family
160                                         if ($this->checkParent()) {
161                                                 $Children = array();
162                                                 $Children = explode('/', $subcatid . $this->getChildren($subcatid));
163                                         }
164                                         if ($Children[1]) {
165                                                 for ($i=0;$i<count($Children);$i++) {
166                                                         $temp_whr[] = ' p.subcategories REGEXP "(^|,)' . intval($Children[$i]) . '(,|$)" ';
167                                                 }
168                                                 $where .= ' and ';
169                                                 $where .= ' ( ';
170                                                 $where .= join(' or ', $temp_whr);
171                                                 $where .= ' )';
172                                         }else{
173                                                 $where .= ' and p.subcategories REGEXP "(^|,)' . $subcatid . '(,|$)"';
174                                         }
175 //family end
176                                 }
177                         } else {
178                                 $where .= ' and i.icat=' . $catid;
179                         }
180                 }
181
182                 $naviUnit = array();
183                 $subNaviUnit = array();
184                 $this->linkparams = array();
185 //store Home =====================================
186 // comment out this block when HOME is sub-blog top
187                 if ($showHome == 1) {
188                         $naviUnit[] = array(
189                                 0 => 'Home',
190                                 1 => $CONF['IndexURL'],
191                                 2 => createArchiveListLink($blogid)
192                         );
193                 }
194
195 //store Blog =====================================
196                 if ($showHome == 1 && ($blogid <> $CONF['DefaultBlog'])) {
197                         $naviUnit[] = array(
198                                 0 => getBlogNameFromID($blogid),
199                                 1 => createBlogidLink($blogid),
200                                 2 => createArchiveListLink($blogid)
201                         );
202                 } elseif ($showHome >= 2) {
203                         $naviUnit[] = array(
204                                 0 => 'Home',            // when HOME is sub-blog top
205                                 1 => $CONF['BlogURL'] . '/',            // when HOME is sub-blog top
206                                 2 => createArchiveListLink($blogid)
207                         );
208                 }
209
210 //store Category =====================================
211                 if (!empty($catid)) {
212                         $this->linkparams['catid'] = $catid;
213                         $naviUnit[] = array(
214                                 0 => $b->getCategoryName($catid),
215                                 1 => createCategoryLink($catid),
216 //                              1 => createBlogidLink($blogid, $this->linkparams),
217                                 2 => createArchiveListLink($blogid, $this->linkparams)
218                         );
219                 }
220
221 //store subCategory =====================================
222                 if (!empty($subcatid)) {
223                         if ($manager->pluginInstalled('NP_MultipleCategories')) {
224                                 $mplugin =& $manager->getPlugin('NP_MultipleCategories');
225                                 if (method_exists($mplugin, 'getRequestName')) {
226                                         $subrequest = $mplugin->getRequestName(array());
227                                         $this->linkparams[$subrequest] = $subcatid;
228                                         if ($this->checkParent()) {
229                                                 $tog = $this->getParenta($subcatid, $blogid);
230                                                 for ($i=0;$i<count($this->r);$i++) {
231                                                         $naviUnit[] = $this->r[$i];
232                                                 }
233                                                 $naviUnit[] = $tog;
234                                         } else {
235                                                 $naviUnit[] = array(
236                                                         0 => $mplugin->_getScatNameFromID($subcatid),
237                                                         1 => createCategoryLink($catid, array($subrequest => $subcatid)),
238 //                                                      1 => createCategoryLink($catid, array('subcatid' => $subcatid)),
239 //                                                      1 => createBlogidLink($blogid, $this->linkparams),
240                                                         2 => createArchiveListLink($blogid, $this->linkparams)
241                                                 );
242                                         }
243                                 }
244                         }
245                 }
246
247 //store Page ===================================== todo How to get PageNo. ? ...cookie... 
248                 if (requestVar('page')) {
249                         $naviUnit[] = array(
250                                 0 => 'Page.' . intRequestVar('page'),
251                                 2 => createArchiveListLink($blogid, $this->linkparams)
252                         );
253                 }
254
255 //store Item =====================================
256                 if ($skinType == 'item') {
257                         $item =& $manager->getItem($itemid, 0, 0);
258                         $naviUnit[] = array(
259                                 0 => $item['title']
260                         );
261
262
263                         $query = 'SELECT i.ititle, i.inumber'
264                                         . ' FROM ' . sql_table('item') . ' as i' . $mtable
265                                         . ' WHERE i.idraft = 0'
266                                         . " and i.itime < '" . $item['itime'] . "' " . $where;
267                         $query .= ' ORDER BY i.itime DESC'; 
268                         $res = sql_query($query);
269                         if ($ares = mysql_fetch_row($res)) {
270                                 $alink = createItemLink($ares[1], $this->linkparams);
271                                 $subNaviUnit[1] = '<a href="' . htmlspecialchars($alink) . '" rel="prev"> &laquo; ' . 
272                                                                 shorten($ares[0], 14, '...') . '</a>';
273                         }
274
275
276
277                         $query = 'SELECT i.ititle, i.inumber'
278                                         . ' FROM ' . sql_table('item') . ' as i' . $mtable
279                                         . ' WHERE i.idraft = 0'
280                                         . " and i.itime > '" . $item['itime'] . "' " . $where;
281                         $query .= ' ORDER BY i.itime ASC'; 
282                         $res = sql_query($query);
283                         if ($ares = mysql_fetch_row($res)) {
284                                 $alink = createItemLink($ares[1], $this->linkparams);
285                                 $subNaviUnit[2] = '<a href="' . htmlspecialchars($alink) . '" rel="next"> ' .
286                                                                 shorten($ares[0], 14, '...') . ' &raquo;</a>';
287                         }
288
289
290                 }
291
292 //store ArchiveList =====================================
293                 if ($skinType == 'archivelist' || $skinType == 'archive') {
294                         $naviUnit[] = array(
295                                 0 => 'ArchiveList',
296                                 1 => createArchiveListLink($blogid, $this->linkparams)
297                         );
298                 }
299
300                 if ($skinType == 'archive') {
301                         sscanf($archive,'%d-%d-%d', $y, $m, $d);
302 //store ArchiveMonth
303                         $archiveMonth = $y . '-' . $m;
304                                 $naviUnit[] = array(
305                                         0 => $archiveMonth,
306                                         1 => createArchiveLink($blogid, $archiveMonth, $this->linkparams)
307                                 );
308                         if (empty($d)) {
309                                 $timestamp_start = mktime(0, 0, 0, $m, 1, $y);
310                                 $timestamp_end = mktime(0, 0, 0, $m+1, 1, $y);
311                                 $date_str = 'SUBSTRING(i.itime, 1, 7)';
312                         } else {
313                                 $timestamp_start = mktime(0, 0, 0, $m, $d, $y);
314                                 $timestamp_end = mktime(0, 0, 0, $m, $d+1, $y);
315                                 $date_str = 'SUBSTRING(i.itime, 1, 10)';
316 //store ArchiveDay
317                                 $naviUnit[] = array(
318                                         0 => $y . '-' . $m . '-' . $d,
319                                         1 => createArchiveLink($blogid, $archive, $this->linkparams)
320                                 );
321                         }
322
323 //=============================
324                         $query = 'SELECT ' . $date_str . ' as Date'
325                                         . ' FROM ' . sql_table('item') . ' as i' . $mtable
326                                         . ' WHERE i.idraft = 0'
327                                         . ' and i.itime < ' . mysqldate($timestamp_start) . $where;
328                         $query .= ' GROUP BY Date';
329                         $query .= ' ORDER BY i.itime DESC'; 
330                         $res = sql_query($query);
331                         if ($ares = mysql_fetch_row($res)) {
332 //                              $prev_date = $ares[0];
333                                 sscanf($ares[0], '%d-%d-%d', $y, $m, $d);
334                                 if (empty($d)) {
335                                         $prev_date = sprintf('%04d-%02d', $y, $m);
336                                 } else {
337                                         $prev_date = sprintf('%04d-%02d-%02d', $y, $m, $d);
338                                 }
339                                 $prev_alink = createArchiveLink($blogid, $prev_date, $this->linkparams);
340                                 $subNaviUnit[1] = '<a href="' . htmlspecialchars($prev_alink) . '" class="prevlink" rel="prev">' .
341                                                 ' &laquo; ' . htmlspecialchars($prev_date) . '</a>';
342 //                              $abuf .= '<a href="'.$prev_alink.'" class="prevlink" rel="prev">'.$prev_date.'</a>';
343 //                      } else {
344 //                              $today_link = createBlogidLink($b->getID(), $this->linkparams);
345 //                              $abuf .= '  ( <a href="'.$today_link.'">Today</a> )';
346                         }
347                         $abuf .= ' | <strong>' . htmlspecialchars($archive) . '</strong> ';
348 //=============================
349                         $query = 'SELECT ' . $date_str . ' as Date'
350                                         . ' FROM ' . sql_table('item') . ' as i' . $mtable
351                                         . ' WHERE i.idraft = 0'
352                                         . ' and i.itime < ' . mysqldate($b->getCorrectTime())
353                                         . ' and i.itime >= ' . mysqldate($timestamp_end) . $where;
354                         $query .= ' GROUP BY Date';
355                         $query .= ' ORDER BY i.itime ASC'; 
356                         $res = sql_query($query);
357                         if ($ares = mysql_fetch_row($res)) {
358 //                              $next_date = $ares[0];
359                                 sscanf($ares[0],'%d-%d-%d', $y, $m, $d);
360                                 if (empty($d)) {
361                                         $next_date = sprintf('%04d-%02d', $y, $m);
362                                 } else {
363                                         $next_date = sprintf('%04d-%02d-%02d', $y, $m, $d);
364                                 }
365                                 $next_alink = createArchiveLink($blogid, $next_date, $this->linkparams);
366                                 $subNaviUnit[2] = '<a href="' . htmlspecialchars($next_alink) . '" class="nextlink" rel="next">'
367                                                                 . htmlspecialchars($next_date) . ' &raquo;</a>';
368 //                              $a2buf = ' | <a href="'.$next_alink.'" class="nextlink" rel="next">'.$next_date.'</a>';
369 //                      } else {
370 //                              $today_link = createBlogidLink($b->getID(), $this->linkparams);
371 //                              $a2buf .= ' | ( <a href="'.$today_link.'">Today</a> )';
372                         }
373                 }
374 //============================= // end of archive(s)
375
376 // Print subNavi
377 // todo Henceforth to template
378 //              echo ' <div style="text-align: center;">';
379 //              echo ' <span style="text-align:right;">';
380                 echo ' <span class="prevnextnavi">';
381                 $endKey = $this->scanEndKey($naviUnit);
382                 if ($skinType != 'archivelist' && $skinType != 'archive' && $skinType != 'item') {
383                         echo '<a href="' . $naviUnit[$endKey][2] . '">&raquo; ArchiveList</a>';
384                 }
385                 echo @join(' :: ', $subNaviUnit);
386 //              echo '</div>';
387                 echo '</span>';
388
389 // Print mainNavi
390                 unset($naviUnit[$endKey][1]);
391                 $naviVar = array_map(array(&$this, 'createNaviLink'), $naviUnit);
392                 echo '<span class="breadcrumbslist">', @join(' &gt; ', $naviVar);
393
394 //add Taginfo =====================================
395 // display selected TAGs whith link mod by shizuki
396                 if ($manager->pluginInstalled('NP_TagEX')) {
397 //                      if (requestVar('tag')) {
398                         $tagPlugin =& $manager->getPlugin('NP_TagEX');
399                         $requestT = $tagPlugin->getNoDecodeQuery('tag');
400                         if (!empty($requestT)) {
401                                 $requestTarray = $tagPlugin->splitRequestTags($requestT);
402                                 $reqAND = array_map(array(&$tagPlugin, "_rawdecode"), $requestTarray['and']);
403                                 if ($requestTarray['or']) {
404                                         $reqOR = array_map(array(&$tagPlugin, "_rawdecode"), $requestTarray['or']);
405                                 }
406                                 if ($reqOR) {
407                                         $reqTags = array_merge($reqAND, $reqOR);
408                                 } else {
409                                         $reqTags = $reqAND;
410                                 }
411                                 for ($i=0;$i<count($reqTags);$i++) {
412                                         $tag = trim($reqTags[$i]);
413                                         $taglist[$i] = '<a href="' . $tagPlugin->creatTagLink($tag, 0) . '">'
414                                                                 . htmlspecialchars($tag) . '</a>';
415                                 }
416                                 echo ' <small style="font-family:Tahoma;">';
417 //                              echo ' (Tag for "'.$tagPlugin->_rawdecode(requestVar('tag')).'")';
418                                 echo ' (Tag for "' . @join(' / ', $taglist) . '")';
419                                 echo '</small>';
420                         }
421                 }
422                 
423                 echo '</span>';
424
425         }
426
427     function getParenta($subcat_id, $blogid=0)
428     {
429         $subcat_id = intval($subcat_id);
430         $blogid = intval($blogid);
431         $r = array();
432         $que = 'SELECT scatid, parentid, sname, catid FROM %s WHERE scatid = %d';
433         $res = sql_query(sprintf($que, sql_table('plug_multiple_categories_sub'), $subcat_id));
434         list ($sid, $parent, $sname, $cat_id) = mysql_fetch_row($res);
435                 if (intval($parent) != 0) {
436                         $this->r[] =  $this->getParenta(intval($parent), $blogid);
437                         $this->linkparams[subcatid] = $sid;
438                         $r =  array(
439                                 0 => $sname,
440                                 1 => createBlogidLink($blogid, $this->linkparams),
441                                 2 => createArchiveListLink($blogid, $this->linkparams)
442                                 );
443                 }else{
444                         $this->linkparams[subcatid] = $sid;
445                         $r =  array(
446                                 0 => $sname,
447                                 1 => createBlogidLink($blogid, $this->linkparams),
448                                 2 => createArchiveListLink($blogid, $this->linkparams)
449                                 );
450                 }
451         return $r;
452     }
453
454         function getParent($subcat_id)
455     {
456         $subcat_id = intval($subcat_id);
457         $que = 'SELECT scatid, parentid, sname FROM %s WHERE scatid = %d';
458         $res = sql_query(sprintf($que, sql_table('plug_multiple_categories_sub'), $subcat_id));
459         list ($sid, $parent, $sname) = mysql_fetch_row($res);
460         if (intval($parent) != 0) {
461                 $r = $this->getParent(intval($parent)) . " -> <a href=$subcat_id>$sname</a>";
462         } else {
463                 $r = "<a href=$subcat_id>" . htmlspecialchars($sname) . "</a>";
464         }
465         return $r;
466     }
467
468     function getChildren($subcat_id)
469     {
470         $subcat_id = intval($subcat_id);
471         $que = 'SELECT scatid, parentid, sname FROM %s WHERE parentid = %d';
472         $res = sql_query(sprintf($que, sql_table('plug_multiple_categories_sub'), $subcat_id));
473                 while ($so =  mysql_fetch_object($res)) {
474                         $r .= $this->getChildren($so->scatid) . '/' . intval($so->scatid);
475                 }
476         return $r;
477     }
478
479 }
480 ?>