OSDN Git Service

Corresponds to event_PreComment.
[nucleus-jp/nucleus-plugins.git] / trunk / NP_ImageCalendar / NP_ImageCalendar.php
1 <?php
2
3 //history
4 //      0.2:    $archive, $blogid and $catid suppot ($exmode=all ready)
5 //                      echos 'no images' 
6 //      0.3:    add strtolower 
7 //                      Initialize $this->exquery
8 //      0.4:    change css
9 //                      allblogmode ready
10 //      0.5:    use createGlobalArchiveLink
11 //                      sql_table support :-P
12 //      0.6:    support gif format
13 //                      print archive month for the first
14 //      0.7:    linkmode: [0]archive day link, [1]item link for the image
15
16
17 // plugin needs to work on Nucleus versions <=2.0 as well
18 if (!function_exists('sql_table')){
19         function sql_table($name) {
20                 return 'nucleus_' . $name;
21         }
22 }
23
24
25 class NP_ImageCalendar extends NucleusPlugin {
26         function getName () {return 'ImageCalendar'; }
27         function getAuthor () {return 'nakahara21'; }
28         function getURL () {return 'http://nakahara21.com/';}
29         function getVersion () {return '0.7';}
30         function supportsFeature($what) {
31                 switch($what){
32                         case 'SqlTablePrefix':
33                                 return 1;
34                         default:
35                                 return 0;
36                 }
37         }
38         function getDescription () {
39                 return 'Embed the image of day into Calendar.';
40         }
41
42         function install () {
43 /*
44                 $this->createOption('default_catname','Default Category Name.','text','');
45 */      
46         }
47
48         function init() {
49                 $this->fileex = array('.gif','.jpg','.png');
50                 $this->linkmode = 0; // create link for image [0]archive day link, [1]item link for the image
51 //              $this->random = 1;
52         }
53         
54         function doSkinVar($skinType, $cnt=1, $wsize=50, $hsize=35, $point=0, $exmode=0) {
55                 global $CONF, $blog;
56                 ($blog)?
57                         $b =& $blog :
58                         $b =& $manager->getBlog($CONF['DefaultBlog']);
59                 
60                 if($cnt=='') $cnt = 1;
61                 if($hsize=='') $hsize = 80;
62                 if($wsize=='') $wsize = 80;
63                 if($exmode != 'all') $exmode = 0;
64                 if($point != 'lefttop') $point = 0;
65                 $this->exquery = '';
66
67                 switch($skinType){
68                         case 'archive': 
69                                 global $archive;
70                                 sscanf($archive,'%4c-%2c-%2c',$year,$month,$day);
71                                 if ($day == 0) {
72                                         $timestamp_start = mktime(0,0,0,$month,1,$year);
73                                         $timestamp_end = mktime(0,0,0,$month+1,1,$year);  // also works when $month==12
74                                 } else {
75                                         $timestamp_start = mktime(0,0,0,$month,$day,$year);
76                                         $timestamp_end = mktime(0,0,0,$month,$day+1,$year);  
77                                 }
78                                 $this->exquery .= ' and itime>=' . mysqldate($timestamp_start)
79                                                 . ' and itime<' . mysqldate($timestamp_end);
80                                 $monthForPrint = getDate(mktime (0,0,0,$month,1, $year));
81
82
83 //                      break;
84                         default:
85                                 if(!$exmode){
86                                         $this->exquery .= ' and iblog =' . $b->getID();
87                                         global $catid;
88                                         if($catid)      $this->exquery .= ' and icat =' . $catid;
89                                 }
90                 }
91
92
93                 $filelist = array();
94                 $this->imglists = array();
95                 $this->imgfilename = array();
96                 if(!($filelist = $this->listup())){
97                         echo 'No images here.';
98                         return;
99                 }
100 //              print_r($filelist);
101 /*
102                 echo '<div>';
103                 foreach($filelist as $key => $value){
104 //                      $itemlink = createItemLink($value[1], '');
105                         $archivedaylink = createArchiveLink($b->getID(),$key,'');
106                         
107                         $exq = '';
108                         if($point)      $exq = '&pnt=lefttop';
109                                 
110                         echo '<a href="'.$archivedaylink.'">';
111                         echo '<img src="'.$CONF['ActionURL'].'?action=plugin&name=ImageCalendar&type=draw&p='.$value[0].'&wsize='.$wsize.'&hsize='.$hsize.$exq.'" />';
112                         echo "</a>\n";
113                 }
114                 echo "</div>\n";
115 */
116
117
118 //==Calendar start==============
119         $waku = '#9fdf9f';
120         $waku = '#e8e9da';
121         $wakurgb = $this->func_SetRgbValueFromHex($waku);
122         $wakugr = $this->func_GetRgbFamilyFromRgb($waku,3,23,5);
123 //      print_r($wakugr);
124 ?>
125 <style type="text/css">
126 <!--
127         table.imgcalendar th{
128                 FONT-FAMILY: verdana, arial, sans-serif;
129                 FONT-WEIGHT: bold;
130                 FONT-SIZE: 9px;
131                 text-align:center;
132                 background-color:<?php echo $waku; ?>;
133                 color:RGB(<?php echo $wakugr[9]; ?>);
134         }
135         table.imgcalendar th.monthname{
136                 text-align:center;
137                 background-color:RGB(<?php echo $wakugr[2]; ?>);
138                 border:0px solid RGB(<?php echo $wakugr[2]; ?>);
139         }
140         td.empcell{
141                 width : <?php echo $wsize; ?>px;
142                 height : <?php echo $hsize; ?>px;
143                 Vertical-align: middle;
144                 text-align: center;
145                 padding:0px;
146                 margin:1px;
147                 background-color:none;
148                 border: 1px solid RGB(<?php echo $wakurgb; ?>);
149                 FONT-FAMILY: verdana, arial, sans-serif;
150                 FONT-WEIGHT: bold;
151                 FONT-SIZE: 9px;
152                 COLOR: <?php echo $waku; ?>;
153         }
154         td.imgcell{
155                 width : <?php echo $wsize; ?>px;
156                 Vertical-align: top;
157                 text-align: left;
158                 padding:0px;
159                 margin:1px;
160                 background-color:RGB(<?php echo $wakugr[8]; ?>);
161                 border: 1px solid <?php echo $waku; ?>;
162         }
163         .cellimg {
164                 MARGIN-TOP: 0px;
165                 MARGIN-LEFT: 0px;
166                 POSITION: absolute;
167         }
168         .cellspacer{
169                 height : <?php echo $hsize; ?>px;
170         }
171         p.anchor{
172                 width : <?php echo $wsize; ?>px;
173                 height : <?php echo $hsize; ?>px;
174                 background-color:silver;
175         }
176         div.d{
177                 FONT-FAMILY: verdana, arial, sans-serif;
178                 FONT-WEIGHT: bold;
179                 FONT-SIZE: 9px;
180                 COLOR: RGB(<?php echo $wakugr[2]; ?>);
181                 background-color:#fff;
182                 border-right: 1px solid <?php echo $waku; ?>;
183                 border-bottom: 1px solid <?php echo $waku; ?>;
184                 width:2em;
185                 TEXT-ALIGN: center;
186                 z-index:2;
187                 POSITION: absolute;
188         }
189 -->
190 </style>
191 <?php
192         $monthForPrint = ($monthForPrint)? $monthForPrint: getDate($b->getCorrectTime());
193
194 for($a=0;$a<$cnt;$a++){
195         $p_month = date("Y-m",mktime(0,0,0,$monthForPrint['mon']-$a,1,$monthForPrint['year']));
196         sscanf($p_month,'%4c-%2c',$year,$month);
197         $date = getDate(mktime (0,0,0,$month,1, $year));
198         $d = 1; 
199         $out = '<table class="imgcalendar"><tr><th colspan="7" class="monthname">'.$p_month.'</th></tr><tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr><tr>';
200         while (checkdate($date['mon'], $d, $date['year'])){
201                 $p = date("w",mktime(0,0,0,$date['mon'],$d,$date['year']));
202                 $di = date("Y-m-d",mktime(0,0,0,$date['mon'],$d,$date['year']));
203                 if($d == 1) for($s=0;$s<$p;$s++){ $out .= '<td class="empcell">*</td>';}        // filling
204                 if(!$p) $out .= "</tr><tr>";
205                 $out .= '<td class="imgcell">';
206
207                 if($filelist[$di]){
208                         if($this->linkmode){
209                                 $linkurl = createItemLink($filelist[$di][1],'');
210                                 $itemtitle = quickQuery('SELECT ititle as result FROM '.sql_table('item').' WHERE inumber='.intval($filelist[$di][1]));
211                                 $alt = shorten(strip_tags($itemtitle),25,'..');
212                         }else{
213                                 $linkurl = $this->createGlobalArchiveLink($filelist[$di][2],$di,'');
214                                 $alt = $di;
215                         }
216                                 
217                         $out .= '<div class="d">'.$d.'</div>';
218                         $out .= '<DIV class=cellimg>';
219                         $out .= '<a href="'.$linkurl.'">';
220                         $out .= '<img src="'.$CONF['ActionURL'].'?action=plugin&amp;name=ImageCalendar&amp;type=draw&amp;p='.$filelist[$di][0].'&amp;wsize='.$wsize.'&amp;hsize='.$hsize.'" alt="'.$alt.'" />';
221                         $out .= '</a>';
222                         $out .= '</div>';
223                 }else{
224                         $out .= '<div class="d">'.$d.'</div>';
225                 }
226
227
228                 $out .= '<div class="cellspacer">&#160;<div>';
229                 $out .= '</td>';
230                 if($d == date("t",mktime(0,0,0,$date['mon'],$d,$date['year']))) for($s=$p+1;$s<=6;$s++){ $out .= '<td class="empcell">*</td>';} // filling
231                 $d++;
232         }
233         $out .= '</tr></table>';
234         echo $out;
235 }
236 //==Calendar end==============
237
238         }
239
240 //==color manegagement start==========
241 function func_SetRgbValueFromHex ($pHexColor)
242 {
243         // INPUT :
244         // $pHexColor : ie #339933
245         // OUTPUT :
246         // return rgb array
247         // Source : Rini Setiadarma, http://www.oodie.com/
248
249         $l_returnarray = array ();
250         $pHexColor = str_replace ("#", "", $pHexColor);
251         for ($l_counter=0; $l_counter<3; $l_counter++){ 
252                 $l_temp = substr($pHexColor, 2*$l_counter, 2); 
253                 $l_returnarray[$l_counter] = 16 * hexdec(substr($l_temp, 0, 1)) + hexdec(substr($l_temp, 1, 1)); 
254         } 
255         return implode(",",$l_returnarray);
256 }
257
258 function func_GetRgbFamilyFromRgb ($pHexColor, $pPrimary, $pIncrement, $pNum){
259         
260         $rgbarr = array();
261         $pHexColor = str_replace ("#", "", $pHexColor);
262         for ($l=0; $l<3; $l++){ 
263                 $l_temp = substr($pHexColor, 2*$l, 2); 
264                 $rgbarr[$l] = 16 * hexdec(substr($l_temp, 0, 1)) + hexdec(substr($l_temp, 1, 1)); 
265         } 
266         $pRgbColor = implode(",",$rgbarr);
267         
268         
269         // INPUT :
270         // $pRgbColor : ie 255,255,255 ; $pPrimary = pos. int (0-3); $pIncrement = pos. int ; $pNum = pos. int
271         // OUTPUT :
272         // array family from input
273         
274         $l_returnarray = array();
275         $l_rgbarray = explode (",",$pRgbColor);
276         if (($l_rgbarray != array ()) AND (count ($l_rgbarray) == 3))
277         {       
278                 $l_start = $l_basevalue - ($pNum*$pIncrement);
279                 $l_end = $l_basevalue + ($pNum*$pIncrement);
280                 If ($pPrimary == 3)
281                 {
282                         $l_startin = 0;
283                         $l_endin = 2;
284                 } else {
285                         $l_startin = $pPrimary;
286                         $l_endin = $pPrimary;
287                 }
288                 for ($l_counter=$l_start;$l_counter<$l_end;$l_counter = $l_counter+$pIncrement)
289                 {
290                         $l_rgbarraytemp = $l_rgbarray;
291                         for ($l_counterint=$l_startin;$l_counterint<=$l_endin;$l_counterint++){
292                                 $l_value = $l_rgbarray[$l_counterint]+$l_counter;
293                                 if ($l_value < 0) $l_value = 0;
294                                 if ($l_value > 255) $l_value = 255;
295                                 $l_rgbarraytemp[$l_counterint] = $l_value;
296                         }
297                         if ($l_rgbarraytemp != array ()) $l_returnarray[] = implode (",",$l_rgbarraytemp);
298                 }
299         }
300         return $l_returnarray;
301 }
302
303 //==color manegagement end==========
304
305
306         function listup(){
307                 global $blog;
308                 ($blog)?
309                         $b =& $blog :
310                         $b =& $manager->getBlog($CONF['DefaultBlog']);
311
312                 $query = 'SELECT inumber as itemid, ititle as title, ibody as body, iauthor, itime, imore as more, iblog, ' ;
313                 $query .= ' icat as catid, iclosed as closed' ;
314                 $query .= ' FROM '.sql_table('item');
315                 $query .= ' WHERE idraft=0';
316                 $query .= ' and itime <=' . mysqldate($b->getCorrectTime());    // don't show future items!
317                 $query .= $this->exquery;
318                 $query .= ' ORDER BY itime DESC'; 
319         
320                 $res = sql_query($query);
321                 
322                 if(!mysql_num_rows($res)) return FALSE;
323                 
324                 while ($it = mysql_fetch_object($res)){
325                         $txt = $it->body.$it->more;
326                         $item_ymd = date("Y-m-d",strtotime($it->itime));
327                         preg_match_all("/\<\%image\((.*)\)\%\>/Us",$txt,$imgpnt,PREG_PATTERN_ORDER);
328                         @array_walk($imgpnt[1], array(&$this, "exarray"), array($it->itemid,$it->iauthor,$item_ymd,$it->iblog));
329                 }
330                 return $this->imglists;
331         }
332
333         function exarray($imginfo,$key,$iaid){
334                 $imginfo = explode("|",$imginfo);
335                 if(!in_array(strtolower(strrchr($imginfo[0], "." )),$this->fileex)) return;
336 //              if(in_array($imginfo[0],$this->imgfilename)) return;
337 //              $this->imgfilename[] = $imginfo[0];
338                 list($iid,$auid,$item_ymd,$iblog) = $iaid;
339                 if (!strstr($imginfo[0],'/')) {
340                         $imginfo[0] = $auid . '/' . $imginfo[0];
341                 }
342 //              $this->imglists[] = array($imginfo,$iaid[0]);
343 //              $this->imglists[] = array($imginfo[0],$iaid[0],$iaid[2]);
344                 $this->imglists[$item_ymd] = array($imginfo[0],$iid,$iblog);
345         }
346
347         function baseimageCreate($p,$imgtype){
348                 switch($imgtype){
349                         case 1:
350                         return ImageCreateFromGif($p);
351                         case 2:
352                         return ImageCreateFromJpeg($p);
353                         case 3:
354                         return ImageCreateFromPng($p);
355                         default:
356                         return;
357                 }
358         }
359
360         function doAction($type) {
361                 global $CONF;
362                 global $DIR_MEDIA;
363                 $return = serverVar('HTTP_REFERER');
364                 switch($type) {
365                         case draw:
366                                 if(!requestVar('p')) return;
367                                 $p = $DIR_MEDIA.requestVar('p');        //path
368                 
369                                 list($imgwidth, $imgheight, $imgtype) = GetImageSize($p);
370                 
371                                 $tsize['w'] = requestVar('wsize');
372                                 $tsize['h'] = requestVar('hsize');
373                                 $point = requestVar('pnt');
374                                 
375                                 if($imgwidth / $imgheight < $tsize['w'] / $tsize['h']){ // height longer
376                                         $trimX = 0;
377                                         $trimW = $imgwidth;
378                                         $trimH = intval($tsize['h']/$tsize['w']*$imgwidth);
379                                         $trimY = intval(($imgheight - $trimH) / 2);
380                                 }else{ // width longer
381                                         $trimY = 0;
382                                         $trimH = $imgheight;
383                                         $trimW = intval($tsize['w']/$tsize['h']*$imgheight);
384                                         $trimX = intval(($imgwidth - $trimW) / 2);
385                                 }
386                                 
387                                 if($point == 'lefttop'){
388                                         $trimX = $trimY = 0;
389                                 }
390                                 
391                                 $im_r = $this->baseimageCreate($p,$imgtype);
392                                 $im = ImageCreateTrueColor($tsize['w'],$tsize['h']);
393                                 ImageCopyResampled( $im, $im_r, 0, 0, $trimX, $trimY, $tsize['w'], $tsize['h'], $trimW, $trimH);
394                                 switch($imgtype){
395                                         case 1:
396                                         header ("Content-type: image/gif");
397                                         ImageGif($im);
398                                         imagedestroy($im);
399                                         break;
400                                         case 2:
401                                         header ("Content-type: image/jpeg");
402                                         ImageJpeg($im);
403                                         imagedestroy($im);
404                                         break;
405                                         case 3:
406                                         header ("Content-type: image/png");
407                                         ImagePng($im);
408                                         imagedestroy($im);
409                                         break;
410                                         default:
411                                         return;
412                                 }
413                         break;
414
415                         default:
416                                 Header('Location: ' . $return);
417                                 break;
418 //_=======
419                 }
420         }
421
422         function canEdit() {
423                 global $member, $manager;
424                 if (!$member->isLoggedIn()) return 0;
425                 return $member->isAdmin();
426         }
427
428         function createGlobalArchiveLink($blogid, $archive, $extra = '') {
429                 global $CONF, $manager;
430                 if ($CONF['URLMode'] == 'pathinfo'){
431                         $link = $CONF['ArchiveURL'] . '/archive/'.$blogid.'/' . $archive;
432                 }else{
433                         $script_name = $CONF['Self'];
434                         if(!$script_name) $script_name = 'index.php';
435                         
436                         $b_tmp =& $manager->getBlog($blogid);
437                         $blogurl = $b_tmp->getURL() ;
438                         if(!$blogurl){
439                                 $blogurl = $CONF['IndexURL'];
440                         }
441                         if(substr($blogurl, -4) != '.php'){
442                                 if(substr($blogurl, -1) != '/')
443                                         $blogurl .= '/';
444                                 $blogurl .= 'index.php';
445                         }
446
447                         $link = $blogurl . '?blogid='.$blogid.'&amp;archive=' . $archive;
448                 }
449                 return addLinkParams($link, $extra);
450         }
451
452 }
453 ?>