顧客で定休日・個人休を管理できる出勤表のメモ。エラー処理・セキュリティ等の細部は詰めてない。
1ヶ月版は下記に記載。
» WordPressのカスタム投稿タイプ・カスタムフィールドを使った出勤表 : WordPress


/**
* Custom Post Type schedule3(Schedule Of 3 Month)
*/
add_action('init', 'create_schedule3_sequence');
function create_schedule3_sequence(){
$labels = array(
'name' => '出勤表 3ヶ月版',
'singular_name' => '出勤表 3ヶ月版',
'add_new' => '出勤表 3ヶ月版を追加',
'add_new_item' => '新しい出勤表を追加',
'edit_item' => '出勤表を編集',
'new_item' => '新しい出勤表',
'view_item' => '出勤表を編集',
'search_items' => '出勤表を探す',
'not_found' => '出勤表はありません',
'not_found_in_trash' => 'ゴミ箱に出勤表はありません',
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'capability_type' => 'post',
'hierarchical' => false,
'supports' => array(
'title'
),
'register_meta_box_cb' => 'schedule3_meta_box' /* カスタムフィールド */
);
register_post_type('schedule3', $args);
/**
* カスタムフィールド
*/
function schedule3_meta_box($post){
add_meta_box('schedule3_meta', '出勤表入力域', 'schedule3_meta_func', 'schedule3', 'normal', 'high');
}
function schedule3_meta_func($post, $box){
/**
* カスタムフィールドの値を取得
*/
// 出勤表1
$year1 = intval(get_post_meta($post->ID, 'year1', true));
$month1 = intval(get_post_meta($post->ID, 'month1', true));
$days1 = array();
for ($i=1; $i<=31; $i++) {
$key = 'day1-' . $i;
$value1 = get_post_meta($post->ID, $key, true);
if (($value1 == 'present') || ($value1 == 'holiday') || ($value1 == 'close')) {
$days1[$i] = $value1;
} else {
// エラー処理
}
}
// 出勤表2
$year2 = intval(get_post_meta($post->ID, 'year2', true));
$month2 = intval(get_post_meta($post->ID, 'month2', true));
$days2 = array();
for ($i=1; $i<=31; $i++) {
$key = 'day2-' . $i;
$value2 = get_post_meta($post->ID, $key, true);
if (($value2 == 'present') || ($value2 == 'holiday') || ($value2 == 'close')) {
$days2[$i] = $value2;
} else {
// エラー処理
}
}
// 出勤表3
$year3 = intval(get_post_meta($post->ID, 'year3', true));
$month3 = intval(get_post_meta($post->ID, 'month3', true));
$days3 = array();
for ($i=1; $i<=31; $i++) {
$key = 'day3-' . $i;
$value3 = get_post_meta($post->ID, $key, true);
if (($value3 == 'present') || ($value3 == 'holiday') || ($value3 == 'close')) {
$days3[$i] = $value3;
} else {
// エラー処理
}
}
/* nonce セキュリティで必要 */
echo wp_nonce_field('schedule3_meta', 'my_meta_nonce');
/*
* カスタムフィールド用のフォームを表示
*/
/* 出勤表1 */
// 年
echo '<p>年 : <select name="year1"><option value="-">--</option>';
for ($y=2011; $y<2020; $y++){
if ($y == $year1) {
echo '<option value="' . $year1 . '" selected="selected">' . $year1. '</option>';
} else {
echo '<option value="' . $y . '">' . $y . '</option>';
}
}
echo '</select> ';
// 月
echo '月 : <select name="month1"><option value="-">--</option>';
for ($m=1; $m<13; $m++){
if ($m == $month1) {
echo '<option value="' . $month1 . '" selected = "selected">' . $month1. '</option>';
} else {
echo '<option value="' . $m . '">' . $m . '</option>';
}
}
echo '</select></p>';
// 日付
echo '<p>';
for ($i=1, $j=1; $i<=31; $i++) {
($i <= 9) ? $date = '0' . $i : $date = $i;
if ($days1[$i] == 'close') {
echo $date . '日' .
'<select name="day1-' . $i. '" />' .
'<option value="present">--</option>' .
'<option value="close" selected="selected">定休</option>' .
'<option value="holiday">休日</option>' .
'</select>  ';
} else if($days1[$i] == 'holiday') {
echo $date . '日' .
'<select name="day1-' . $i. '" />' .
'<option value="present">--</option>' .
'<option value="close">定休</option>' .
'<option value="holiday" selected="selected">休日</option>' .
'</select>  ';
} else {
echo $date . '日' .
'<select name="day1-' . $i. '" />' .
'<option value="present">--</option>' .
'<option value="close">定休</option>' .
'<option value="holiday">休日</option>' .
'</select>  ';
}
if ($j == 5) {
echo '<br />';
$j = 1;
} else {
$j++;
}
}
echo '</p>';
/* 出勤表2 */
// 年
echo '<p>年 : <select name="year2"><option value="-">--</option>';
for ($y=2011; $y<2020; $y++){
if ($y == $year2) {
echo '<option value="' . $year2 . '" selected="selected">' . $year2. '</option>';
} else {
echo '<option value="' . $y . '">' . $y . '</option>';
}
}
echo '</select> ';
// 月
echo '月 : <select name="month2"><option value="-">--</option>';
for ($m=1; $m<13; $m++){
if ($m == $month2) {
echo '<option value="' . $month2 . '" selected = "selected">' . $month2. '</option>';
} else {
echo '<option value="' . $m . '">' . $m . '</option>';
}
}
echo '</select></p>';
// 日付
echo '<p>';
for ($i=1, $j=1; $i<=31; $i++) {
($i <= 9) ? $date2 = '0' . $i : $date2 = $i;
if ($days2[$i] == 'close') {
echo $date2 . '日' .
'<select name="day2-' . $i. '" />' .
'<option value="present">--</option>' .
'<option value="close" selected="selected">定休</option>' .
'<option value="holiday">休日</option>' .
'</select>  ';
} else if($days2[$i] == 'holiday') {
echo $date2 . '日' .
'<select name="day2-' . $i. '" />' .
'<option value="present">--</option>' .
'<option value="close">定休</option>' .
'<option value="holiday" selected="selected">休日</option>' .
'</select>  ';
} else {
echo $date2 . '日' .
'<select name="day2-' . $i. '" />' .
'<option value="present">--</option>' .
'<option value="close">定休</option>' .
'<option value="holiday">休日</option>' .
'</select>  ';
}
if ($j == 5) {
echo '<br />';
$j = 1;
} else {
$j++;
}
}
echo '</p>';
/* 出勤表3 */
// 年
echo '<p>年 : <select name="year3"><option value="-">--</option>';
for ($y=2011; $y<2020; $y++){
if ($y == $year3) {
echo '<option value="' . $year3 . '" selected="selected">' . $year3. '</option>';
} else {
echo '<option value="' . $y . '">' . $y . '</option>';
}
}
echo '</select> ';
// 月
echo '月 : <select name="month3"><option value="-">--</option>';
for ($m=1; $m<13; $m++){
if ($m == $month3) {
echo '<option value="' . $month3 . '" selected = "selected">' . $month3. '</option>';
} else {
echo '<option value="' . $m . '">' . $m . '</option>';
}
}
echo '</select></p>';
// 日付
echo '<p>';
for ($i=1, $j=1; $i<=31; $i++) {
($i <= 9) ? $date = '0' . $i : $date = $i;
if ($days3[$i] == 'close') {
echo $date . '日' .
'<select name="day3-' . $i. '" />' .
'<option value="present">--</option>' .
'<option value="close" selected="selected">定休</option>' .
'<option value="holiday">休日</option>' .
'</select>  ';
} else if ($days3[$i] == 'holiday') {
echo $date . '日' .
'<select name="day3-' . $i. '" />' .
'<option value="present">--</option>' .
'<option value="close">定休</option>' .
'<option value="holiday" selected="selected">休日</option>' .
'</select>  ';
} else {
echo $date . '日' .
'<select name="day3-' . $i. '" />' .
'<option value="present">--</option>' .
'<option value="close">定休</option>' .
'<option value="holiday">休日</option>' .
'</select>  ';
}
if ($j == 5) {
echo '<br />';
$j = 1;
} else {
$j++;
}
}
echo '</p>';
}
// 保存処理
add_action('save_post', 'schedule3_meta_update');
function schedule3_meta_update($post_id){
if (!wp_verify_nonce( $_POST['my_meta_nonce'], 'schedule3_meta')) {
return $post_id;
}
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
if ('schedule3' == $_POST['post_type']) {
if(!current_user_can('edit_post', $post_id)) {
return $post_id;
}
} else {
return $post_id;
}
/* 値の取得 */
// 出勤表1
$year1 = $_POST['year1'];
$month1 = $_POST['month1'];
$days1 = array();
for ($i=1; $i<=31; $i++) {
$key = 'day1-' . $i;
$days1[$i] = $_POST[$key];
}
// 出勤表2
$year2 = $_POST['year2'];
$month2 = $_POST['month2'];
$days2 = array();
for ($i=1; $i<=31; $i++) {
$key = 'day2-' . $i;
$days2[$i] = $_POST[$key];
}
// 出勤表3
$year3 = $_POST['year3'];
$month3 = $_POST['month3'];
$days3 = array();
for ($i=1; $i<=31; $i++) {
$key = 'day3-' . $i;
$days3[$i] = $_POST[$key];
}
if($year1 == '') {
delete_post_meta($post_id, 'year1');
}
else {
update_post_meta($post_id, 'year1', $year1);
}
if($month1 == '') {
delete_post_meta($post_id, 'month1');
}
else {
update_post_meta($post_id, 'month1', $month1);
}
for ($i=1; $i<=31; $i++) {
$key = 'day1-' . $i;
if ($days1[$i] == '') {
delete_post_meta($post_id, $key);
} else {
update_post_meta($post_id, $key, $days1[$i]);
}
}
if($year2 == '') {
delete_post_meta($post_id, 'year2');
}
else {
update_post_meta($post_id, 'year2', $year2);
}
if($month2 == '') {
delete_post_meta($post_id, 'month2');
}
else {
update_post_meta($post_id, 'month2', $month2);
}
for ($i=1; $i<=31; $i++) {
$key = 'day2-' . $i;
if ($days2[$i] == '') {
delete_post_meta($post_id, $key);
} else {
update_post_meta($post_id, $key, $days2[$i]);
}
}
if($year3 == '') {
delete_post_meta($post_id, 'year3');
}
else {
update_post_meta($post_id, 'year3', $year3);
}
if($month3 == '') {
delete_post_meta($post_id, 'month3');
}
else {
update_post_meta($post_id, 'month3', $month3);
}
for ($i=1; $i<=31; $i++) {
$key = 'day3-' . $i;
if ($days3[$i] == '') {
delete_post_meta($post_id, $key);
} else {
update_post_meta($post_id, $key, $days3[$i]);
}
}
}
}
[/php]
<h2>カレンダー出力関数</h2>
<p>カスタム投稿ポストの単一ページsingle-schedule3.phpから呼び出す。</p>
[php]
/*
* カレンダー出力関数
* @param $id 投稿ID
* @param $y 年 数字4桁
* @param $m 月 数字1~13
* @param $days 日付 配列 添え字[1~31] 値[空, close, holiday]
*/
function drawSchedule ($id, $y, $m, $days) {
// $y 年 $m 月
$t = mktime(0, 0, 0, $m, 1, $y); //$y年$m月1日のUNIXTIME
$w = date('w', $t); //1日の曜日(0:日~6:土)
$n = date('t', $t); //$y年$m月の日数
if ($m<10) {
$m = "0" . $m;
}
print <<<HTML
<table class="schedule">
<caption>{$y}年{$m}月</caption>
<tr>
<th class="textRed">日</th>
<th>月</th>
<th>火</th>
<th>水</th>
<th>木</th>
<th>金</th>
<th class="textBlue">土</th>
</tr>
HTML;
for($i=1-$w; $i<= $n+7; $i++){
if ((($i + $w) % 7) == 1) {
print "<tr>n";
}
// 日付が有効な場合の処理
if ((0 < $i) && ($i <= $n)) {
$value = $days[$i];
$html = "<td";
// 曜日の取得
$hizuke = mktime(0, 0, 0, $m, $i, $y); //$y年$m月$i日のUNIXTIME
$youbi = date('w', $hizuke); //1日の曜日(0:日~6:土)
// 定休日
if ($value == 'close') { // 定休日
$html .= ' class="close"';
} else if ($value == 'holiday') { // 休日
$html .= ' class="holiday"';
} else if ($youbi == 0) { // 日曜
$html .= ' class="sun"';
}
$html .= '>' . $i . '</td>';
print($html);
} else {
print "<td> </td>n";
}
if ((($i + $w ) % 7 ) == 0 ) {
print "</tr>n";
if ( $i >= $n ) {
break;
}
}
}
print "</table>n";
}
<?php if(have_posts()):while(have_posts()):the_post() ?>
<h3 style="clear: both">出勤表</h3>
<?php
// カレンダー1
$year1 = get_post_meta($post->ID, 'year1' , true);
$month1 = get_post_meta($post->ID, 'month1', true);
for ($i=1; $i<=31; $i++) {
$key = 'day1-' . $i;
$days1[$i] = get_post_meta($post->ID, $key, true);
}
$year2 = get_post_meta($post->ID, 'year2' , true);
$month2 = get_post_meta($post->ID, 'month2', true);
for ($i=1; $i<=31; $i++) {
$key = 'day2-' . $i;
$days2[$i] = get_post_meta($post->ID, $key, true);
}
$year3 = get_post_meta($post->ID, 'year3' , true);
$month3 = get_post_meta($post->ID, 'month3', true);
for ($i=1; $i<=31; $i++) {
$key = 'day3-' . $i;
$days3[$i] = get_post_meta($post->ID, $key, true);
}
if (!intVal($year1) || !intVal($month1)) {
$timestamp1 = 0;
} else {
$timestamp1 = mktime(1, $month1, $year1);
}
if (!intVal($year2) || !intVal($month2)) {
$timestamp2 = 0;
} else {
$timestamp2 = mktime(1, $month2, $year2);
}
if (!intVal($year3) || !intVal($month3)) {
$timestamp3 = 0;
} else {
$timestamp3 = mktime(1, $month3, $year3);
}
$schedule = array(
0 => array(
'timestamp' => $timestamp1,
'year' => $year1,
'month' => $month1,
'days' => $days1
),
1 => array(
'timestamp' => $timestamp2,
'year' => $year2,
'month' => $month2,
'days' => $days2
),
2 => array(
'timestamp' => $timestamp3,
'year' => $year3,
'month' => $month3,
'days' => $days3
)
);
foreach ($schedule as $key => $row) {
$data[$key] = $row["timestamp"];
}
array_multisort($data,SORT_ASC,$schedule);
if ($data[2] != 0) {
for ($i = 0; $i<3; $i++) {
if($data[$i] > 0) {
drawSchedule($post->ID, $schedule[$i]['year'],$schedule[$i]['month'], $schedule[$i]['days']);
}
}
} else {
echo '申し訳ごぜません。ただいまスタッフ別の出勤表を準備しております。';
}
?>
<?php endwhile;endif; ?>
table.schedule {
float: left;
margin-right: 10px;
}
td.close {
background: #ccc;
}
td.sun {
background: #ffc0cb;
}
td.holiday {
background: #ff0000;
}
No comments yet.
改行と段落タグは自動で挿入されます。
メールアドレスは表示されません。