OSDN Git Service

8e319f15ec0c453dad48ac11b8459b02c0e317bc
[nucleus-jp/nucleus-plugins.git] / NP_ArchiveIndex / NP_ArchiveIndex.php
1 <?php
2 /*
3         NP_ArchiveIndex by yu(http://nucleus.datoka.jp/)
4         Based on NP_ArchiveListEX ver0.6 by nakahara21(http://xx.nakahara21.net/)
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         Usage
13         -----
14         <%ArchiveIndex%>
15         <%ArchiveIndex(5)%>                     //set item amount of each category on archive index
16         <%ArchiveIndex(5,10,desc)%>     //set page amount, show category description
17         <%ArchiveIndex(5,10,none)%>     //set page amount, don't show category description
18         
19         History
20         -------
21         2008/04/15 Ver0.81:
22                 [FIX] small fixes on 'Template: List (begin)' and 'Template: List element'.
23         2004/11/30 Ver0.8:
24                 [ADD] Works with NP_UpdateTime and NP_ContentsList.
25                 [ADD] Blog option.
26 */
27
28 // plugin needs to work on Nucleus versions <=2.0 as well
29 if (!function_exists('sql_table')){
30         function sql_table($name) {
31                 return 'nucleus_' . $name;
32         }
33 }
34
35 class NP_ArchiveIndex extends NucleusPlugin {
36
37         function getName() { return 'Archive Index'; }
38         function getAuthor()  { return 'nakahara21 + yu'; }
39         function getURL() { return 'http://works.datoka.jp/index.php?itemid=167'; }
40         function getVersion() { return '0.81'; }
41         function getMinNucleusVersion() { return 220; }
42         function supportsFeature($what) {
43                 switch($what){
44                         case 'SqlTablePrefix':
45                                 return 1;
46                         default:
47                                 return 0;
48                 }
49         }
50
51         // a description to be shown on the installed plugins listing
52         function getDescription() { 
53                 return "Show all item title on archive list. [Optional] amount:  limit the amount of links shown (e.g. if you only want to show 5 links on each list by category index). page amount: the amount of links in a page. show_catdesc: show category description. 'desc' or 'none'. Usage: &lt;%ArchiveIndex%&gt;, &lt;%ArchiveIndex(5)%&gt;, &lt;%ArchiveIndex(5,10)%&gt;, &lt;%ArchiveIndex(5,10,desc)%&gt;";
54         }
55
56         function install() {
57                 
58                 // global option
59                 $this->createOption('itemOrder', 'Order of title (index)', 'select', 'time,DESC', 'title,DESC|title,DESC|title,ASC|title,ASC|time,DESC|time,DESC|time,ASC|time,ASC');
60                 $this->createOption('itemOrder2', 'Order of title (when a category is selected)', 'select', 'time,DESC', 'title,DESC|title,DESC|title,ASC|title,ASC|time,DESC|time,DESC|time,ASC|time,ASC');
61                 $this->createOption('catDesc','Show category description.','yesno','yes');
62                 $this->createOption('flg_showupd','Show update information (NP_UpdateTime).','yesno','no');
63                 $this->createOption('flg_sortupd','Sort with update information (NP_UpdateTime).','yesno','no');
64                 $this->createOption('idateFormat','Date format for item list','text','Y-m-d H:i');
65                 
66                 $this->createOption('tempListBegin','Template: List (begin)','text','<table><thead title="%catdesc%"><tr><th colspan="2"><a href="%caturl%">%catname%</a></th></tr></thead><tbody>');
67                 $this->createOption('tempListElement','Template: List element','text','<tr%bg%><td><span class="title%up%">%titlelink%</span></td><td class="detail">%detail%</td></tr>');
68                 $this->createOption('tempListMore','Template: List element (morelink)','text','<tr class="more"><td>%pagenavi%</td><td class="detail">%morelink%</td></tr>');
69                 $this->createOption('tempListEnd','Template: List (end)','text','</tbody></table>');
70                 
71                 // blog option
72                 $this->createBlogOption('switch','Use blog option?','yesno','no');
73                 $this->createBlogOption('itemOrder', 'Order of title (index)', 'select', 'time,DESC', 'title,DESC|title,DESC|title,ASC|title,ASC|time,DESC|time,DESC|time,ASC|time,ASC');
74                 $this->createBlogOption('itemOrder2', 'Order of title (when a category is selected)', 'select', 'time,DESC', 'title,DESC|title,DESC|title,ASC|title,ASC|time,DESC|time,DESC|time,ASC|time,ASC');
75                 $this->createBlogOption('catDesc','Show category description.','yesno','yes');
76                 $this->createBlogOption('flg_showupd','Show update information (NP_UpdateTime).','yesno','no');
77                 $this->createBlogOption('flg_sortupd','Sort with update information (NP_UpdateTime).','yesno','no');
78                 $this->createBlogOption('idateFormat','Date format for item list','text','Y-m-d H:i');
79                 
80                 $this->createBlogOption('tempListBegin','Template: List (begin)','text','<table><thead title="%catdesc%"><th><a href="%caturl%">%catname%</a></th><th></th></thead><tbody>');
81                 $this->createBlogOption('tempListElement','Template: List element','text','<tr %bg%><td><span class="title%up%">%titlelink%</span></td><td class="detail">%detail%</td></tr>');
82                 $this->createBlogOption('tempListMore','Template: List element (morelink)','text','<tr class="more"><td>%pagenavi%</td><td class="detail">%morelink%</td></tr>');
83                 $this->createBlogOption('tempListEnd','Template: List (end)','text','</tbody></table>');
84         }
85         
86         function unInstall() {
87                 //nothing to do
88         }
89
90         function doSkinVar($skinType, $amount, $pamount=10, $show_catdesc='') { 
91                 global $manager, $member, $blog, $CONF, $catid, $itemid; 
92                 
93                 if ($blog) $b =& $blog; 
94                 else $b =& $manager->getBlog($CONF['DefaultBlog']);
95                 $bid = $b->getID();
96                 
97                 // get global option
98                 $op_itemOrder =        $this->getOption('itemOrder');
99                 $op_itemOrder2 =       $this->getOption('itemOrder2');
100                 $op_catDesc =          $this->getOption('catDesc');
101                 $op_flg_showupd =      $this->getOption('flg_showupd');
102                 $op_flg_sortupd =      $this->getOption('flg_sortupd');
103                 $op_idateFormat =      $this->getOption('idateFormat');
104                 $op_tempListBegin =    $this->getOption('tempListBegin');
105                 $op_tempListElement =  $this->getOption('tempListElement');
106                 $op_tempListMore =     $this->getOption('tempListMore');
107                 $op_tempListEnd =      $this->getOption('tempListEnd');
108                 
109                 // get blog option
110                 if ($this->getBlogOption($bid, 'switch') == 'yes') {
111                         $op_itemOrder =        $this->getBlogOption($bid, 'itemOrder');
112                         $op_itemOrder2 =       $this->getBlogOption($bid, 'itemOrder2');
113                         $op_catDesc =          $this->getBlogOption($bid, 'catDesc');
114                         $op_flg_showupd =      $this->getBlogOption($bid, 'flg_showupd');
115                         $op_flg_sortupd =      $this->getBlogOption($bid, 'flg_sortupd');
116                         $op_idateFormat =      $this->getBlogOption($bid, 'idateFormat');
117                         $op_tempListBegin =    $this->getBlogOption($bid, 'tempListBegin');
118                         $op_tempListElement =  $this->getBlogOption($bid, 'tempListElement');
119                         $op_tempListMore =     $this->getBlogOption($bid, 'tempListMore');
120                         $op_tempListEnd =      $this->getBlogOption($bid, 'tempListEnd');
121                 }
122                 
123                 if (!is_numeric($amount)) $amount = 5;
124                 if (!is_numeric($pamount)) $pamount = 10;
125                 
126                 if ($show_catdesc == '') {
127                         if ($op_catDesc == 'yes') $show_catdesc = 'desc';
128                         else $show_catdesc = 'none';
129                 }
130                 
131                 if ($blog) { 
132                         $b =& $blog; 
133                 } else { 
134                         $b =& $manager->getBlog($CONF['DefaultBlog']); 
135                 } 
136                 $blogid = $b->getID();
137
138                 if ($catid) {
139                         $this->linkparams = array('catid' => $catid);
140                 }
141
142                 //get value from request
143                 $page = intRequestVar('ap');
144                 if ($catid) {
145                         $item_order = requestVar('ao');
146                         //check value (also used to make flip link)
147                         list($itarget, $iorder) = @split(' ',$item_order);
148                         if(!($itarget=='time' or $itarget=='title' or $iorder =='desc'  or $iorder =='asc')) {
149                                 $item_order = $op_itemOrder2;
150                                 list($itarget, $iorder) = @split(',',$item_order);
151                         }
152                 }
153                 else {
154                         $item_order = $op_itemOrder;
155                         list($itarget, $iorder) = @split(',',$item_order);
156                 }
157                 $iorder = strtolower($iorder);
158                 
159                 $now = time();
160                 $idateformat = $op_idateFormat;
161                 $archivedate = date('Y-m');     
162                 
163                 //get item data
164                 if ($manager->pluginInstalled('NP_UpdateTime')) {
165                         $flg_showupdate = ($op_flg_showupd == 'yes');
166                         $flg_sortupdate = ($op_flg_sortupd == 'yes');
167                 }
168                 if ($flg_showupdate or $flg_sortupdate) {
169                         //get update info
170                         $query = 'SELECT r.up_id as itemid, UNIX_TIMESTAMP(r.updatetime) as utime, i.itime as itime FROM '.sql_table('plugin_rectime') . ' as r, '.sql_table('item') .' as i WHERE r.up_id=i.inumber and r.updatetime>i.itime'
171                                 . ' ORDER BY itemid ASC';
172                         $ut_res = sql_query($query);
173                         while ($utinfo = mysql_fetch_object($ut_res)) {
174                                 if ($catid and 
175                                         $catid != $this->_getCategoryIDFromItemID($utinfo->itemid)) continue;
176                                 $up_ids[] = $utinfo->itemid;
177                                 $up_tstamps[] = $utinfo->utime;
178                                 if ($flg_sortupdate) $up_datetimes[$utinfo->itemid] = $utinfo->itime; // for title prop.
179                                 else $up_datetimes[$utinfo->itemid] = date($idateformat, $utinfo->utime);
180                         }
181                         mysql_free_result($ut_res);
182                 }
183                 
184                 if ($flg_sortupdate and count($up_ids)) {
185                         $str_up_ids = join(',', $up_ids);
186                         $str_up_tstamps = join(',', $up_tstamps);
187                         
188                         //select
189                         $query = 'SELECT inumber, ititle, icat, IF(inumber IN('.$str_up_ids.'), ELT(INTERVAL(inumber,'.$str_up_ids.'), '.$str_up_tstamps.'), UNIX_TIMESTAMP(itime)) as itime, iauthor FROM '.sql_table('item');
190                 }
191                 else {
192                         $query = 'SELECT inumber, ititle, icat, UNIX_TIMESTAMP(itime) as itime, iauthor FROM '.sql_table('item');
193                 }
194                 
195                 $query .= ' WHERE iblog=' . $blogid
196                         .' and UNIX_TIMESTAMP(itime)<='. $now .' and idraft=0';
197                 
198                 if ($catid) {
199                         $query .= ' and icat=' . intval($catid);
200                         
201                         //item count in the category
202                         $resnum = sql_query($query);
203                         $itemnum = mysql_num_rows($resnum);
204                         mysql_free_result($resnum);
205                         $pagemax = ceil($itemnum / $pamount);
206                 }
207                 
208                 if ($itarget == 'title') $query .= ' ORDER BY ititle';
209                 else $query .= ' ORDER BY itime';
210                 if ($iorder == 'asc') $query .= ' ASC';
211                 else $query .= ' DESC';
212                 
213                 if ($catid) {
214                         if ($page < 1) $page = 1;
215                         $poffset = ($page-1) * $pamount;
216                         $query .= " LIMIT $poffset,$pamount";
217                 }
218                 $res = sql_query($query);
219                 
220                 //set data by category
221                 $list_cat  = array();
222                 $list_item = array();
223                 $cnt_item = array();
224                 while ($current = mysql_fetch_object($res)) {
225                         $cnt_item[$current->icat]++;
226                         if (!$catid and $cnt_item[$current->icat] > $amount) { //check the amount
227                                 continue;
228                         }
229                         
230                         $inumber = $current->inumber; //itemid
231                         $idetail = date($idateformat, $current->itime); //itemdate
232                         
233                         $list_cat[$current->icat][] = $inumber;
234                         $list_item[$inumber]['ititle']   = htmlspecialchars(strip_tags($current->ititle));
235                         $list_item[$inumber]['itemlink'] = createItemLink($inumber,$this->linkparams);
236                         $list_item[$inumber]['idetail']  = $idetail;
237                 }
238                 mysql_free_result($res);
239                 
240                 //prepare list by category
241                 $arr_out = array();
242                 foreach ($list_cat as $icat => $arr_icat) {
243                         // buffer category name
244                         $icatName = $b->getCategoryName($icat);
245                         if (!$catid) $icatDesc = $b->getCategoryDesc($icat);
246                         else $icatDesc = '&raquo; Back to index';
247                         
248                         if (!$catid) $extra = array('catid' => $icat);
249                         else $extra = array();
250                         $arclist_link = createArchiveListLink($blogid, $extra);
251                         
252                         $temp_list_b = $op_tempListBegin;
253                         $rep_from = array('/%caturl%/','/%catname%/','/%catdesc%/');
254                         $rep_to   = array($arclist_link, $icatName, $icatDesc);
255                         $temp_list_b = preg_replace($rep_from, $rep_to, $temp_list_b);
256                         $temp_list_e = $op_tempListEnd;
257                         
258                         $icnt = 0;
259                         $arr_title = array();
260                         foreach ($arr_icat as $inumber) {
261                                 //buffer item title
262                                 $ititle   = $list_item[$inumber]['ititle'];
263                                 if (empty($ititle)) $ititle = '(no title)'; 
264                                 $itemlink = $list_item[$inumber]['itemlink'];
265                                 $idetail  = $list_item[$inumber]['idetail'];
266                                 $bg = '';
267                                 $upstr = '';
268                                 $titledesc = '';
269                                 
270                                 if ($icnt % 2 == 1) $bg = " class='stripe'";
271                                 if (count($up_ids) and in_array($inumber, $up_ids)) {
272                                         if($flg_showupdate) $upstr = '-up ';
273                                         if($flg_sortupdate) $titledesc = " title='Posted on ". $up_datetimes[$inumber] ."'";
274                                         else $titledesc = " title='Updated on ". $up_datetimes[$inumber] ."'";
275                                 }
276                                 $titlelink = "<a href='$itemlink' $titledesc>$ititle</a>";
277                                 
278                                 $temp_list_el = $op_tempListElement;
279                                 $rep_from = array('/%bg%/','/%titlelink%/','/%detail%/','/%up%/');
280                                 $rep_to   = array($bg, $titlelink, $idetail, $upstr);
281                                 $temp_list_el = preg_replace($rep_from, $rep_to, $temp_list_el);
282                                 $arr_title[]  = $temp_list_el."\n";
283                                 
284                                 $icnt++;
285                                 if (!$catid and $icnt >= $amount) break;
286                         }
287                         
288                         // last list (navigation)
289                         if (!$catid) {
290                                 if ($cnt_item[$icat] <= $amount) { // category description
291                                         $arclist_link = '<span class="nomore">&raquo; More</span>';
292                                         if ($show_catdesc == 'desc') $icatDescStr = '<span class="catdesc">'.$icatDesc.'</span>';
293                                         else $icatDescStr = '';
294                                         
295                                         $temp_list_more = $op_tempListMore;
296                                         $rep_from = array('/%pagenavi%/','/%morelink%/');
297                                         $rep_to   = array($icatDescStr, $arclist_link);
298                                         $temp_list_more = preg_replace($rep_from, $rep_to, $temp_list_more);
299                                         $arr_title[] = "$temp_list_more\n"; 
300                                 }
301                                 else { // category description with more link
302                                         $extra = array('catid' => $icat);
303                                         $arclist_link = '<a href="'. createArchiveListLink($blogid, $extra) .'">&raquo; More</a>';
304                                         if ($show_catdesc == 'desc') $icatDescStr = '<span class="catdesc">'.$icatDesc.'</span>';
305                                         else $icatDescStr = '';
306                                         
307                                         $temp_list_more = $op_tempListMore;
308                                         $rep_from = array('/%pagenavi%/','/%morelink%/');
309                                         $rep_to   = array($icatDescStr, $arclist_link);
310                                         $temp_list_more = preg_replace($rep_from, $rep_to, $temp_list_more);
311                                         $arr_title[] = "$temp_list_more\n"; 
312                                 }
313                         }
314                         else { // sort order, page navi and all category link
315                                 //make 'order switch'
316                                 $itarget_flip = ($itarget=='title') ? 'time' : 'title'; //mode change
317                                 $iorder_flip  = ($iorder =='desc')  ? 'asc'  : 'desc';  //order change
318                                 $itarget_flip_order = ($itarget_flip =='title') ? 'asc' : 'desc'; //ini-order on mode change
319                                 
320                                 if ($itarget == 'title') {
321                                         $iorder_str      = ($iorder == 'desc') ? 'Z-A' : 'A-Z';
322                                         $iorder_flip_str = ($iorder == 'desc') ? 'A-Z' : 'Z-A';
323                                 }
324                                 else { //time
325                                         $iorder_str      = ($iorder == 'desc') ? 'New' : 'Old';
326                                         $iorder_flip_str = ($iorder == 'desc') ? 'Old' : 'New';
327                                 }
328                                 
329                                 $orderURL = serverVar('REQUEST_URI');
330                                 $orderURL = preg_replace('/[?&]ap=[0-9]*/','',$orderURL); //delete to add 'ap' param to last
331                                 $orderURL = preg_replace('/[?&]ao=[^&0-9]*/','',$orderURL);
332                                 
333                                 if (strpos($orderURL,'?')===false) $orderURL .= "?ao=";
334                                 else $orderURL .= "&ao=";
335                                 
336                                 $orderURL1 = $orderURL . "$itarget_flip+$itarget_flip_order";
337                                 $orderURL2 = $orderURL . "$itarget+$iorder_flip";
338                                 $pagenavi = "Sort: <strong>$itarget</strong>/<a href='$orderURL1'>$itarget_flip</a>, ";
339                                 $pagenavi.= "<strong>$iorder_str</strong>/<a href='$orderURL2'>$iorder_flip_str</a>";
340                                 
341                                 //make 'page navi link'
342                                 $pagenavi.= "&nbsp;&nbsp; Page:";
343                                 for ($i=1; $i<=$pagemax; $i++) {
344                                         if ($i==$page) $pagenavi.= "<strong>&nbsp;$i&nbsp;</strong>";
345                                         else {
346                                                 $pageURL = serverVar('REQUEST_URI');
347                                                 $pageURL = preg_replace('/([?&]ap=)[0-9]*/','',$pageURL);
348                                                 if (strpos($pageURL,'?')===false) $pageURL .= "?ap=$i";
349                                                 else $pageURL .= "&ap=$i";
350                                                 $pagenavi.= "<a href='$pageURL'>&nbsp;$i&nbsp;</a>";
351                                         }
352                                 }
353                                 //and make 'all category link'
354                                 $arclist_link = '<a href="'. createArchiveListLink($blogid) .'">&raquo; Back to index</a>';
355                                 
356                                 $temp_list_more = $op_tempListMore;
357                                 $rep_from = array('/%pagenavi%/','/%morelink%/');
358                                 $rep_to   = array($pagenavi, $arclist_link);
359                                 $temp_list_more = preg_replace($rep_from, $rep_to, $temp_list_more);
360                                 $arr_title[] = "$temp_list_more\n";
361                         }
362                         
363                         $arr_out[$icatName] .= 
364                                 $temp_list_b ."\n". join('',$arr_title) . $temp_list_e ."\n";
365                 } //end of foreach
366                 
367                 //sort by category
368                 if ($manager->pluginInstalled('NP_ContentsList')) {
369                         $plugin =& $manager->getPlugin('NP_ContentsList');
370                         if ($plugin) {
371                                 $query = 'SELECT rid as catid FROM '. sql_table('plug_contentslist_rank') .' WHERE blog=0 AND rank<20 ORDER BY rank ASC'; // you can delete 'AND rank<20'
372                                 $cl_res = sql_query($query); 
373                                 
374                                 $arr_out2 = $arr_out;
375                                 $arr_out = array();
376                                 while ($clrank = mysql_fetch_object($cl_res)) {
377                                         $icatName = $b->getCategoryName($clrank->catid);
378                                         $arr_out[] = $arr_out2[$icatName];
379                                 }
380                                 $arr_out2 = '';
381                         }
382                 }
383                 else {
384                         ksort($arr_out);
385                 }
386                 
387                 //flush the buffer
388                 foreach ($arr_out as $value) {
389                         echo $value;
390                 }
391                 
392         } //end of function doSkinVar
393
394
395         // helper function
396         function _getCategoryIDFromItemID($itemid) {
397                 return quickQuery('SELECT icat as result FROM '.sql_table('item').' WHERE inumber='.intval($itemid));
398         }
399
400 } //end of class
401 ?>