ColdFusionで1ヶ月24時間を色で確認

元記事はこちらです。

■Application.cfm

<cfsetting enableCFoutputOnly="yes">
<cfapplication name="#hash(GetDirectoryFromPath(GetCurrentTemplatePath()))#">

■index.cfm

<!---/////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////--->
<cfparam name="url.p" default="">
<cfparam name="form.year" default="2014">
<cfparam name="form.month" default="3">
<cfparam name="form.holiday" default="2,9,16,23,30">
<cfparam name="form.data" default="
##FF9966|201403010420-201403010530
red|201403020423-201403020541
aqua|201403030440-201403030515
red|201403032340-201403040515
red|201403050400-201403050450
##CCFF99|201403060410-201403060500
red|201403070420-201403070510
red|201403080430-201403080520
aqua|201403080520-201403080620
red|201403132240-201403150515
red|201403010000-201403010010
aqua|201403010010-201403010025
red|201403010025-201403010045
aqua|201403010045-201403010100
">

<cfset obj = createObject("component", "monthtbl").init(form.year, form.month, form.holiday)>
<cfset obj.setHeight(20)>
<cfset obj.setWidth(20)>
<cfloop index="idx" list="#form.data#" delimiters="#chr(10)##chr(13)#">
    <cfset obj.setDate(idx)>
</cfloop>
<cfset obj.chgDate()>

<cfset content=cst_table(obj.dispTbl())>
<cfset layout(content)>
<!---=================================================================
cst_table
==================================================================--->
<cffunction name="cst_table" returnType="string" output="no">
    <cfargument name="content" type="string" required ="yes">

    <cfset var local = structNew()>

    <cfsavecontent variable="local.tmp">
    <cfoutput>
    #arguments.content#

    <form action="index.cfm?p=1" method="post">
    <table border="1">
        <tr>
            <td></td>
            <td>
                <input type="text" name="year" value="#form.year#">
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <input type="text" name="month" value="#form.month#">
            </td>
        </tr>
        <tr>
            <td>休日</td>
            <td>
                <input type="text" name="holiday" value="#form.holiday#">
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <textarea name="data" cols="50" rows="10">#form.data#</textarea>
            </td>
        </tr>
    </table>
    <input type="submit" value="send">
    </form>
    </cfoutput>
    </cfsavecontent>

    <cfreturn local.tmp>
</cffunction>
<!---=================================================================
layout
==================================================================--->
<cffunction name="layout" returnType="void" output="yes">
    <cfargument name="content" type="string" required ="yes">

    <cfoutput>
    <!DOCTYPE HTML>
    <html lang="ja">
    <head>
        <meta charset="utf-8">
        <title>test</title>
    </head>
    <body>
    #arguments.content#
    </body>
    </html>
    </cfoutput>
</cffunction>

■monthtbl.cfc

<component>
    <cfset variables.year       = 0>
    <cfset variables.month      = 0>
    <cfset variables.lastDay    = 0>
    <cfset variables.listHoliday= "">
    <cfset variables.height     = 30>
    <cfset variables.width      = 25>
    <cfset variables.tbl        = arrayNew(2)>
    <cfset variables.aryDate    = arrayNew(1)>

    <!---===============================
    init
    ================================--->
    <cffunction name="init" returnType="monthtbl" output="no">
        <cfargument name="year"         type="string" required ="yes">
        <cfargument name="month"        type="string" required ="yes">
        <cfargument name="listHoliday"  type="string" required ="yes">

        <cfset var local = structNew()>
        <cfset variables.year       = arguments.year>
        <cfset variables.month      = arguments.month>
        <cfset variables.lastDay    = DaysInMonth(createDate(variables.year, variables.month, 1))>
        <cfset variables.listHoliday= arguments.listHoliday>

        <cfloop index="local.day" from="1" to="#variables.lastDay#">
            <cfloop index="local.hh" from="1" to="24">
                <cfset variables.tbl[local.day][local.hh] = "">
            </cfloop>
        </cfloop>

        <cfreturn this>
    </cffunction>

    <!---===============================
    chgDate
    ================================--->
    <cffunction name="chgDate" returnType="void" output="no">
        <cfset var local = structNew()>

        <cfloop index="local.idx" array="#variables.aryDate#">
            <cfset local.color  = ListFirst(local.idx, "|")>
            <cfset local.date   = ListLast(local.idx, "|")>
            <cfset local.from   = ListFirst(local.date, "-")>
            <cfset local.to     = ListLast(local.date, "-")>

            <!--- fromtoを元に配列にリスト形式で時間をセット --->
            <cftry>
                <cfset local.from = CreateDateTime(
                                                mid(local.from, 1, 4),
                                                mid(local.from, 5, 2),
                                                mid(local.from, 7, 2),
                                                mid(local.from, 9, 2),
                                                mid(local.from, 11, 2),
                                                0)>
                <cfset local.to = CreateDateTime(
                                                mid(local.to, 1, 4),
                                                mid(local.to, 5, 2),
                                                mid(local.to, 7, 2),
                                                mid(local.to, 9, 2),
                                                mid(local.to, 11, 2),
                                                0)>
                <cfloop index="local.idx2" from="#local.from#" to="#local.to#" step="#CreateTimeSpan(0,0,1,0)#">
                    <cfset local.year   = year(local.idx2)>
                    <cfset local.month  = month(local.idx2)>
                    <cfset local.day    = day(local.idx2)>
                    <cfset local.hour   = hour(local.idx2)>
                    <cfset local.Minute = Minute(local.idx2)>
                    <cfif (local.year eq variables.year) and (local.month eq variables.month)>
                        <cfset variables.tbl[local.day][local.hour + 1] = ListAppend(variables.tbl[local.day][local.hour + 1], "#local.color#-#local.Minute#")>
                    </cfif>
                </cfloop>

                <cfcatch>
                </cfcatch>
            </cftry>
        </cfloop>

        <!--- 配列内をdivタグに変換 --->
        <cfset chgDate2()>
    </cffunction>

    <!---===============================
    chgDate2
    ================================--->
    <cffunction name="chgDate2" returnType="void" output="no">
        <cfset var local = structNew()>

        <cfloop index="local.day" from="1" to="#variables.lastDay#">
            <cfloop index="local.hh" from="1" to="24">
                <cfif variables.tbl[local.day][local.hh] neq "">
                    <cfset variables.tbl[local.day][local.hh] = chgDate3(variables.tbl[local.day][local.hh])>
                </cfif>
            </cfloop>
        </cfloop>
    </cffunction>

    <!---===============================
    chgDate3
    ================================--->
    <cffunction name="chgDate3" returnType="string" output="no">
        <cfargument name="str" type="string" required ="yes">

        <cfset var local = structNew()>

        <!--- strを元に1から60の配列に色をセット --->
        <cfset local.aryMM = ArrayNew(1)>
        <cfloop index="local.idx" from="1" to="60">
            <cfset local.aryMM[local.idx] = "">
        </cfloop>
        <cfloop index="local.idx" list="#arguments.str#">
            <cfset local.color = ListFirst(local.idx, "-")>
            <cfset local.mm = ListLast(local.idx, "-")>
            <cfset local.aryMM[local.mm + 1] = local.color>
        </cfloop>

        <cfset local.ary = arrayNew(1)>
        <cfset local.color_old = local.aryMM[1]>
        <cfset local.cnt = 0>
        <cfloop index="local.idx" from="1" to="60">
            <cfset local.color = local.aryMM[local.idx]>
            <cfif local.color_old neq local.color>
                <cfset local.st = structNew()>
                <cfset local.st.color   = local.color_old>
                <cfset local.st.cnt     = local.cnt>
                <cfset ArrayAppend(local.ary, local.st)>

                <cfset local.color_old = local.color>
                <cfset local.cnt = 0>
            </cfif>
            <cfset local.cnt = local.cnt + 1>
        </cfloop>
        <cfset local.st = structNew()>
        <cfset local.st.color   = local.color_old>
        <cfset local.st.cnt     = local.cnt>
        <cfset ArrayAppend(local.ary, local.st)>

        <cfset local.data = "">
        <cfloop index="local.idx" array="#local.ary#">
            <cfset local.h = local.idx.cnt / 60 * 100>
            <cfset local.data = local.data & '<div style="height:#local.h#%;background-color:#local.idx.color#;">&nbsp;</div>'>
        </cfloop>

        <cfreturn local.data>
    </cffunction>

    <!---===============================
    dispTbl
    ================================--->
    <cffunction name="dispTbl" returnType="string" output="no">
        <cfset var local = structNew()>

        <cfsavecontent variable="local.tmp">
        <cfoutput>
        <style type="text/css">
        <!--
        table.monthtbl {
            border: solid 1px black;
            border-collapse: separate;
            border-spacing: 0px;
            }
        table.monthtbl .top, table.monthtbl .bottom , table.monthtbl .hh {
            padding:0;
            margin:0;
            height:#variables.height#px;
            width:#variables.width#px;
            border-left: solid 1px black;
            border-right: solid 1px black;
            text-align:center;
            vertical-align:middle;
            }
        table.monthtbl .hh {
            border-top:1px dashed black;
            }
        table.monthtbl .top {
            border-top:1px solid black;
            }
        table.monthtbl .bottom {
            border-bottom:1px solid black;
            }
        table.monthtbl .holiday {
            background-color:red;
            color:white;
            }
        -->
        </style>

        #variables.year##variables.month#<table class="monthtbl">
            <tr>
                <td class="hh top">&nbsp;</td>
                <cfloop index="local.day" from="1" to="#variables.lastDay#">
                    <cfset local.cls = "">
                    <cfif ListFind(variables.listHoliday, local.day)>
                        <cfset local.cls = "holiday">
                    </cfif>

                    <td class="hh top #local.cls#">#local.day#</td>
                </cfloop>
            </tr>

            <cfloop index="local.hh" from="0" to="23">
                <cfset local.cls = "">
                <cfif local.hh eq 0>
                    <cfset local.cls = "top">
                </cfif>
                <cfif local.hh eq 23>
                    <cfset local.cls = "bottom">
                </cfif>

                <tr>
                    <td class="hh #local.cls#">#local.hh#</td>

                    <cfloop index="local.day" from="1" to="#variables.lastDay#">
                        <td class="hh #local.cls#">#variables.tbl[local.day][local.hh + 1]#</td>
                    </cfloop>
                </tr>
            </cfloop>
        </table>
        </cfoutput>
        </cfsavecontent>

        <cfreturn local.tmp>
    </cffunction>

    <!---===============================
    setDate
    ================================--->
    <cffunction name="setDate" returnType="void" output="no">
        <cfargument name="fromtodate" type="string" required ="yes">

        <cfset ArrayAppend(variables.aryDate, arguments.fromtodate)>
    </cffunction>

    <!--- setter --->
    <!---===============================
    setHeight
    ================================--->
    <cffunction name="setHeight" returnType="void" output="no">
        <cfargument name="height" type="string" required ="yes">

        <cfset variables.height = arguments.height>
    </cffunction>
    <!---===============================
    setWidth
    ================================--->
    <cffunction name="setWidth" returnType="void" output="no">
        <cfargument name="width" type="string" required ="yes">

        <cfset variables.width = arguments.width>
    </cffunction>

</component>