Drupal7で営業日カレンダーのブロックモジュールを作ってみた。

Drupal7で営業日カレンダーをちょっと作ってみました。
Drupal7でブロックモジュール周りのAPIがだいぶ変わったようでほとんど英語の情報ばかりでしたけれど。。。
一応http://api.drupal.org/api/examples/block_example–block_example.module/7/source
を参考に頑張ってみました。

未だにcheckboxesがよく分からないので避けていたり、そもそもAPIもちゃんと理解しているか怪しいので、かなり冗長なコードになってしまっていますが・・・・・

[php]
<?php
// $Id: businesscalender.module, v 0.6 2011/5/2 dries Exp $

/**
* @file
* business calender.
*/

//モジュールの基本設定
function businesscalender_menu() {
$items[‘businesscalender’] = array(
‘page callback’ => ‘businesscalender_page’,
‘access callback’ => TRUE,
‘title’ => ‘営業日カレンダー’,
);
return $items;
}

function businesscalender_page() {
$page = array(
‘#type’ => ‘markup’,
‘#markup’ => t("営業日カレンダー", array(‘@url’ => url(‘admin/structure/block’))),
);
return $page;
}

//headに追加
function businesscalender_init() {
drupal_add_css(drupal_get_path(‘module’, ‘businesscalender’) . ‘/css/style.css’);
drupal_add_js(drupal_get_path(‘module’, ‘businesscalender’) . ‘/js/script.js’);
}

//ブロックの情報
function businesscalender_block_info() {
$blocks[‘businesscalender’] = array(
‘info’ => ‘営業日カレンダー’,
‘cache’ => DRUPAL_CACHE_PER_ROLE, // default
);
return $blocks;
}

//フォームの設定
function businesscalender_block_configure() {

$form = array();

$form[‘businesscalender_holiday_sun’] = array(
‘#type’ => ‘checkbox’,
‘#title’ => ‘日曜日’,
‘#default_value’ => variable_get(‘businesscalender_holiday_sun’),
‘#required’ => FALSE,
);
$form[‘businesscalender_holiday_mon’] = array(
‘#type’ => ‘checkbox’,
‘#title’ => ‘月曜日’,
‘#default_value’ => variable_get(‘businesscalender_holiday_mon’),
‘#required’ => FALSE,
);
$form[‘businesscalender_holiday_tue’] = array(
‘#type’ => ‘checkbox’,
‘#title’ => ‘火曜日’,
‘#default_value’ => variable_get(‘businesscalender_holiday_tue’),
‘#required’ => FALSE,
);
$form[‘businesscalender_holiday_wed’] = array(
‘#type’ => ‘checkbox’,
‘#title’ => ‘水曜日’,
‘#default_value’ => variable_get(‘businesscalender_holiday_wed’),
‘#required’ => FALSE,
);
$form[‘businesscalender_holiday_thu’] = array(
‘#type’ => ‘checkbox’,
‘#title’ => ‘木曜日’,
‘#default_value’ => variable_get(‘businesscalender_holiday_thu’),
‘#required’ => FALSE,
);
$form[‘businesscalender_holiday_fri’] = array(
‘#type’ => ‘checkbox’,
‘#title’ => ‘金曜日’,
‘#default_value’ => variable_get(‘businesscalender_holiday_fri’),
‘#required’ => FALSE,
);
$form[‘businesscalender_holiday_sat’] = array(
‘#type’ => ‘checkbox’,
‘#title’ => ‘土曜日’,
‘#default_value’ => variable_get(‘businesscalender_holiday_sat’),
‘#required’ => FALSE,
);

$form[‘businesscalender_holiday’] = array(
‘#type’ => ‘textarea’,
‘#title’ => ‘休日’,
‘#default_value’ => variable_get(‘businesscalender_holiday’),
‘#required’ => FALSE,
);

return $form;
}

//フォーム内容の保存
function businesscalender_block_save($delta = ”, $edit = array()) {
variable_set(‘businesscalender_holiday_sun’, $edit[‘businesscalender_holiday_sun’]);
variable_set(‘businesscalender_holiday_mon’, $edit[‘businesscalender_holiday_mon’]);
variable_set(‘businesscalender_holiday_tue’, $edit[‘businesscalender_holiday_tue’]);
variable_set(‘businesscalender_holiday_wed’, $edit[‘businesscalender_holiday_wed’]);
variable_set(‘businesscalender_holiday_thu’, $edit[‘businesscalender_holiday_thu’]);
variable_set(‘businesscalender_holiday_fri’, $edit[‘businesscalender_holiday_fri’]);
variable_set(‘businesscalender_holiday_sat’, $edit[‘businesscalender_holiday_sat’]);
variable_set(‘businesscalender_holiday’, $edit[‘businesscalender_holiday’]);
return;
}

//ブロックの表示コンテンツ
function businesscalender_block_view() {
$block[‘subject’] = ‘営業日カレンダー’;

$now = time();
$block[‘content’] = array(‘#markup’ => businesscalender_core(date("Y",$now),date("m",$now)));
return $block;
}

function businesscalender_block_list_alter(&$blocks) {
foreach ($blocks as $bid => $block) {
if (($block->module == ‘search’) && ($block->delta == ‘form’)) {
unset($blocks[$bid]);
$blocks[$bid] = $block;
break;
}
}
}

function businesscalender_block_view_alter(&$data, $block) {
if (!isset($data[‘content’]) || !is_string($data[‘content’])) {
return;
}

if (strstr($data[‘content’], ‘magic string’)) {
$data[‘subject’] = isset($data[‘subject’]) ? drupal_strtoupper($data[‘subject’]) : ”;
}
}

function businesscalender_core($year, $month) {

$month = abs($month);
$calender = "";
$holiday_week = array();

//休みの週

$holiday_week[] = variable_get(‘businesscalender_holiday_sun’);
$holiday_week[] = variable_get(‘businesscalender_holiday_mon’);
$holiday_week[] = variable_get(‘businesscalender_holiday_tue’);
$holiday_week[] = variable_get(‘businesscalender_holiday_wed’);
$holiday_week[] = variable_get(‘businesscalender_holiday_thu’);
$holiday_week[] = variable_get(‘businesscalender_holiday_fri’);
$holiday_week[] = variable_get(‘businesscalender_holiday_sat’);

//休日を取得
$holidays = explode("\n",variable_get(‘businesscalender_holiday’));
$formed_holidays = array();

foreach ($holidays as $holiday){
if(preg_match("/([0-9]{4})\/([0-9]{1,2})\/([0-9]{1,2})/",$holiday,$matches)){
$formed_holidays[] = array(
"year" => $matches[1],
"month" => abs($matches[2]),
"day" => abs($matches[3])
);
}
elseif(preg_match("/([0-9]{1,2})\/([0-9]{1,2})/",$holiday,$matches)){
$formed_holidays[] = array(
"year" => "",
"month" => abs($matches[1]),
"day" => abs($matches[2])
);
}
elseif(preg_match("/([0-9]{1,2})/",$holiday,$matches)){
$formed_holidays[] = array(
"year" => "",
"month" => "",
"day" => abs($matches[1])
);
}
}

//前後2ヶ月分の表示
for($i=-2;$i<3;$i++){
$setMon = $month + $i;
$class= "hide";

if($i==0) $class = "visible";
if($setMon < 1){
$calender .= get_calender($year-1, $setMon+12,$formed_holidays,$holiday_week,$class);
}else{
$calender .= get_calender($year, $setMon,$formed_holidays,$holiday_week,$class);

}

}

return $calender;

}

function get_calender($year, $month,$holidays,$holiday_week,$class = "") {

//カレンダーの作成
$timestamp =mktime(0,0,0,$month,1,$year);
$jpweek = Array(‘日’, ‘月’, ‘火’, ‘水’, ‘木’, ‘金’, ‘土’);
$days = date("t",$timestamp);
$week = date("w",$timestamp);
$day = array_pad(Array(), 42, 0);

for($i = $week; $i <= $week + $days – 1; $i++){
$day[$i] = $i – $week + 1;
}

$calender .= ‘<div class="calender ‘.$class.’"><h2>’.$month.’月</h2>’;
$calender .= ‘<table><tbody>’;

//週
$calender .= ‘<tr>’;
foreach($jpweek as $value){$calender .= ‘<th>’ . $value . ‘</th>’;}
$calender .= ‘</tr>’;

//日
foreach($day as $key => $value) {
$holiday = 0;//フラグ
abs($value);

if($key % 7 == 0) $calender .= ‘<tr>’;

//休日の判定
if($value){
if(
$holiday_week[$key % 7] or
is_int(array_search(array("year" => $year,"month" => $month,"day" => $value),$holidays)) or
is_int(array_search(array("year" => ”,"month" => $month,"day" => $value),$holidays)) or
is_int(array_search(array("year" => ”,"month" => ”,"day" => $value),$holidays))
){
$holiday = 1;
}
}

$calender .= ($holiday) ? ‘<td class="holiday" style="color:red">’ : ‘<td>’;
$calender .= ($value) ? $value : ‘&nbsp’;
$calender .= ‘</td>’;
if($key % 7 == 6) $calender .= ‘</tr>’;
if($key >= $week + $days – 1 && $key % 7 == 6) break;
}

$calender .= ‘</tbody></table></div>’;

return $calender;

}

[/php]

こういうカレンダーなどのモジュールって配列の作り方とかで頭の悪さが露呈するような気がして泣けてきます。。。。
それでも一応動作はしてるのでまぁ許容範囲かなと。

またモジュールの作り方もそのうち書きます。