OSDN Git Service

2de3bfb10fc9189e51cd483733b7b2c3020084eb
[nucleus-jp/nucleus-plugins.git] / NP_DateLink / NP_DateLink.php
1 <?php
2
3 class NP_DateLink extends NucleusPlugin
4 {
5         function getEventList()
6         {
7                 return array();
8         }
9
10         function getName()
11         {
12                 return 'DateLink';
13         }
14
15         function getAuthor()
16         {
17                 return 'nakahara21';
18         }
19
20         function getURL()
21         {
22                 return 'http://nakahara21.com';
23         }
24
25         function getMinNucleusVersion()
26         {
27                 return 200;     
28         }
29
30         function getVersion()
31         {
32                 return '0.9';
33         }
34
35         function getDescription()
36         {
37                 return '&lt;%DateLink%&gt; on TEMPLATE displays links to the archives for the same date. ';
38         }
39         
40         function supportsFeature($what)
41         {
42                 switch ($what) {
43                         case 'SqlTablePrefix':
44                                 return 1;
45                         default:
46                                 return 0;
47                 }
48         }
49         
50         function install()
51         {
52                 $this->createOption('dl_limit',                 'Limit to create links (years)',                        'text',         '3');
53                 $this->createOption('dl_header',                        'Header of links','text',                                       '<ul>');
54                 $this->createOption('dl_footer',                        'Footer of links','text',                                       '</ul>');
55                 $this->createOption('template_ago',             'Template for older (Blank for no link.)',      'text',         '<li><a href="<%linkurl%>" title="Archive for <%date%>">&laquo; <%year%> year(s) ago</a></li>');
56                 $this->createOption('template_after',           'Template for newer (Blank for no link.)',      'text',         '<li><a href="<%linkurl%>" title="Archive for <%date%>"><%year%> year(s) after &raquo;</a></li>');
57                 $this->createOption('template_separator',       'Separator for links',                                          'text',         '');
58         }
59
60         function doTemplateVar(&$item)
61         {
62                 $today = $item->timestamp;
63                 $tcat = intval($item->catid);
64                 if ($linkForPrint = $this->LinksDate($today, $tcat, $this->getOption('dl_limit'))) {
65                         echo $this->getOption('dl_header');
66                         echo @join($this->getOption('template_separator'), $linkForPrint);
67                         echo $this->getOption('dl_footer');
68                 }
69         }
70         
71         function LinksDate($timestamp, $catid, $limitYear)
72         {
73                 global $manager, $blog, $CONF, $archive;
74
75                 if (!$this->getOption('template_ago') && !$this->getOption('template_after')) {
76                         return FALSE;
77                 }
78
79                 $blogid = intval(getBlogIDFromCatID($catid));
80                 $b =& $manager->getBlog($blogid);
81                 $from = array('<%linkurl%>', '<%date%>', '<%year%>');
82
83                 for ($i=$limitYear;$i>0;$i--) {
84                         if (!$this->getOption('template_ago')) break;
85                         $target_date = date('Y-m-d', strtotime("-" . $i . " years", $timestamp));
86                         $s = $this->getArchiveForDate($target_date, $b);
87                         if ($s) {
88                                 $linkurl = createArchiveLink($blogid, $target_date);
89                                 $to = array(
90                                         $linkurl,
91                                         $target_date,
92                                         $i
93                                 );
94                                 $print_data[] = str_replace($from, $to, $this->getOption('template_ago'));
95                         }
96                 }
97
98                 for ($i=1;$i<=$limitYear;$i++) {
99                         if (!$this->getOption('template_after')) break;
100                         $target_date = date('Y-m-d', strtotime("+" . $i . " years", $timestamp));
101                         $s = $this->getArchiveForDate($target_date, $b);
102                         if ($s) {
103                                 $linkurl = createArchiveLink($blogid, $target_date);
104                                 $to = array(
105                                         $linkurl,
106                                         $target_date,
107                                         $i
108                                 );
109                                 $print_data[] = str_replace($from, $to, $this->getOption('template_after'));
110                         }
111                 }
112
113                 return $print_data;
114         }
115         
116         function getArchiveForDate ($target_date, $blog)
117         {
118                 $query = 'SELECT inumber FROM ' . sql_table('item')
119                         . ' WHERE iblog = ' . $blog->getID() . ' AND itime BETWEEN "'
120                         . $target_date . ' 00:00:00" AND "'
121                         . $target_date . ' 23:59:59"'
122                         . ' AND idraft = 0'
123                         . ' AND itime <= ' . mysqldate($blog->getCorrectTime())
124                         . ' LIMIT 1';
125                 $result = sql_query($query);
126                 if (mysql_num_rows($result)) return TRUE;
127                 return FALSE;
128         }
129 }
130 ?>