<?php
/*
Plugin Name: Doy Calendar
Plugin URI: http://labo.utsubopeo.com/
Description: Display Doy Calendar
Version: 1.0.0
Author: k-utsubo
Author URI: http://labo.utsubopeo.com/
License: GPL2
*/
?>
<?php
/*  Copyright 2016 k-utsubo 
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2, as
     published by the Free Software Foundation.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
?>
<?php
 
date_default_timezone_set('Asia/Tokyo'); // php5.1以降のWarning対応
function utbt_calendar_func($atts,$content=''){
    $year = (isset($_GET["utbt_year"]) && $_GET["utbt_year"] != "") ? $_GET["utbt_year"] : "";
    $year = htmlspecialchars($year, ENT_QUOTES);
    $cal= new UtbtCalendar;
    return $cal->calendar($year);
}
 
add_shortcode('utbt_calendar','utbt_calendar_func');
  class UtbtCalendar{
 
    function calendar($year=''){
 
 
        if (empty($year) && empty($month)) {
            $year = date('Y');
        }
        $html="<form method='get' action='".$_SERVER['REQUEST_URI']."'>Input Yea
r:<input type='text' name='utbt_year' value='$year'><input type='submit'>";
        for($i=1;$i<=12;$i++){
          $html.=$this->calendar_exec($year,$i);
        }
        $html.="</form>";
        return $html;
    }
    //http://shanabrian.com/web/php_calendar.php
    private function calendar_exec($year = '', $month = '') {
        $tab="";
        $str="";
        if (empty($year) && empty($month)) {
            $year = date('Y');
            $month = date('n');
        }
        //月末の取得
        $l_day = date('j', mktime(0, 0, 0, $month + 1, 0, $year));
        //初期出力
        $html = <<<EOM
    <table class="calendar">
        <caption>{$year}/{$month}</caption>
        <tr>
            <th><font color="#FF0000">Sun</font></th>
            <th>Mon</th>
            <th>Tue</th>
            <th>Wed</th>
            <th>Thr</th>
            <th>Fri</th>
            <th><font color="#0000FF">Sat</font></th>
        </tr>\n
EOM;
        $lc = 0;
        // 月末分繰り返す
        for ($i = 1; $i < $l_day + 1;$i++) {
            $classes = array();
            $class   = '';
 
            // 曜日の取得
            $week = date('w', mktime(0, 0, 0, $month, $i, $year));
 
            // 曜日が日曜日の場合
            if ($week == 0) {
                $str .= $tab."\t\t<tr>\n";
                $lc++;
            }
 
            // 1日の場合
            if ($i == 1) {
                if($week != 0) {
                    $str .= $tab."\t\t<tr>\n";
                    $lc++;
                }
                $str .= $this->repeatEmptyTd($week);
            }
 
            if ($week == 6) {
                array_push($classes, '#0000FF');
            } else if ($week == 0) {
                array_push($classes , '#FF0000');
            }
 
            if (count($classes) > 0) {
                $class = '"'.implode(' ', $classes).'"';
            }
            $doy = date('z', mktime(0, 0, 0, $month, $i, $year))+1;
            $str .= $tab."\t\t\t".'<td><font color='.$class.'>'.$i.'('.$doy.')</font></td>'."\n";
 
            // 月末の場合
            if ($i == $l_day) {
                $str .= $this->repeatEmptyTd(6 - $week);
            }
            // 土曜日の場合
            if ($week == 6) {
                $str .= $tab."\t\t</tr>\n";
            }
 
        }
 
        $html .=$str;
 
        $html .= "</table>\n";
        return $html;
    }
 
    private function repeatEmptyTd($n = 0) {
        return str_repeat("\t\t<td> </td>\n", $n);
    }
  }
?>