Under the Bridge

a Picture of the Late Him

MT4.1:プラグイン作成におけるDBへのフィールド追加について(ブログ毎編)

2008年5月 5日 16:05 | Writer: yoshi | 記事本文 | コメント(0) | トラックバック(0)

では、ブログ毎の、揮発しない設定系のデータをシステムへ追加する場合には?これも同じようにMovable Typeが扱うDBのテーブルへフィールドを追加して取り扱う方法がある。これの書き方とか、チェックの仕方についてメモ。

カテゴリ毎にデータを追加する

こういうコードがあったとする。※追加するデータは2個とする

sub init_registry
{
    my $plugin = shift;
    $plugin->registry({
        object_types => {
           'blog' => {
              'calendar_holiday' => 'string(255)',
              'calendar_currentmonth' => 'string(255)',
           },
        },
        tags => {
            function => {
                'HolidayCalendar' => \&_hdlr_outputcalendar,
                'HolidayCalendarHolidayData' => \&_hdlr_get_holiday,
                'HolidayCalendarCurrentDate' => \&_hdlr_get_date,
            },
        },
    });
    
}

sub _hdlr_get_holiday {
    my ($ctx, $args) = @_;
    my $tag = 'HolidayCalendarHolidayData';
    (my $blog = $ctx->stash('blog')) || return $ctx->_no_entry_error('MT' . $tag);
    return $blog->calendar_holiday || '';
}

sub _hdlr_get_date {
    my ($ctx, $args) = @_;
    my $tag = 'HolidayCalendarCurrentDate';
    (my $blog = $ctx->stash('blog')) || return $ctx->_no_entry_error('MT' . $tag);
    return $blog->calendar_currentmonth || '';
}

データの追加は以下。phpMyAdminで確認すると、mt_blogテーブルへ、blog_calendar_holiday、blog_calendar_currentmonthという名前でフィールドが生成される。ブログの設定画面ページなどからこのフィールドへデータを書き込むと、ブログ毎に個別のレコードが生成される。以下例ではvarchar(255)で生成される。型やデータ長は色々変更できる。

        object_types => {
           'blog' => {
              'calendar_holiday' => 'string(255)',
              'calendar_currentmonth' => 'string(255)',
           },
        },

データの取り出し方は以下の箇所。

sub _hdlr_get_holiday {
    my ($ctx, $args) = @_;
    my $tag = 'HolidayCalendarHolidayData';
    (my $blog = $ctx->stash('blog')) || return $ctx->_no_entry_error('MT' . $tag);
    return $blog->calendar_holiday || '';
}
sub _hdlr_get_date {
    my ($ctx, $args) = @_;
    my $tag = 'HolidayCalendarCurrentDate';
    (my $blog = $ctx->stash('blog')) || return $ctx->_no_entry_error('MT' . $tag);
    return $blog->calendar_currentmonth || '';
}

管理画面を拡張して、拡張DBデータフィールドへ値を書き込む例。tmpl\cmsフォルダ内のcfg_prefs.tmplファイルを、alt-tmplフォルダへコピーして編集する。するとシステムは次回より、alt-tmplフォルダ内のcfg_prefs.tmplを優先して読み込むようになる。青い字の箇所が変更点。

<mt:setvar name="page_title" value="<__trans phrase="General Settings">">
<$mt:setvar name="position_actions_bottom" value="1"$>
<mt:setvar name="general-settings" value="1">
<MTSetVarBlock name="system_msg">
    <mt:if name="error">
        <mtapp:statusmsg
            id="generic-error"
            class="error">
            <mt:var name="error">
        </mtapp:statusmsg>
    </mt:if>
    <mt:if name="saved">
        <mtapp:statusmsg
            id="saved"
            class="success"
            rebuild="all">
            <__trans phrase="Your preferences have been saved.">
        </mtapp:statusmsg>
    </mt:if>
</MTSetVarBlock>
<MTSetVarBlock name="content_nav">
    <mt:include name="include/cfg_content_nav.tmpl">
</MTSetVarBlock>
<mt:include name="include/header.tmpl">

<script type="text/javascript">
<!--
function doRemoveLicense () {
    document.cfg_form.cc_license.value = '';
    var e = getByID('has-license');
    if (e) e.style.display = 'none';
    e = getByID('no-license');
    if (e) e.style.display = 'block';
}

function validate (f) {
    if (!f.name.value) {
        alert('<__trans phrase="You must set your Blog Name." escape="singlequotes">');
        return false;
    } else if (f.server_offset.value == '') {
        alert('<__trans phrase="You did not select a timezone." escape="singlequotes">');
        return false;
    }
    return true;
}

function setLicense() {
    var w = window.open('http://creativecommons.org/license/?partner=SixApart&amp;jurisdiction_choose=1&amp;exit_url=<mt:var name="script_full_url">?__mode=cc_return%26license_code=[license_code]%26license_url=[license_url]%26license_button=[license_button]', 'cc', 'width=600,height=650,scrollbars=yes,resizable=no');
    if ( w ) w.focus();
   return false;
}
//-->
</script>

<form name="cfg_form" method="post" action="<mt:var name="script_url">" onsubmit="return validate(this)">
<input type="hidden" name="id" value="<mt:var name="id">" />
<input type="hidden" name="__mode" value="save" />
<input type="hidden" name="_type" value="blog" />
<input type="hidden" name="cfg_screen" value="cfg_prefs" />
<input type="hidden" name="blog_id" value="<mt:var name="blog_id">" />
<input type="hidden" name="return_args" value="<mt:var name="return_args" escape="html">" />
<input type="hidden" name="magic_token" value="<mt:var name="magic_token">" />

<fieldset>
    
    <h3><__trans phrase="Blog Settings"></h3>

    <mtapp:setting
        id="name"
        label="<__trans phrase="Name">"
        hint="<__trans phrase="Name your blog. The blog name can be changed at any time.">"
        help_page="blog_settings_general"
        help_section="blog_name">
        <div class="textarea-wrapper">
            <input name="name" id="name" class="full-width" value="<mt:var name="name" escape="html">" size="30" />
        </div>
    </mtapp:setting>

    <mtapp:setting
        id="description"
        label="<__trans phrase="Description">"
        hint="<__trans phrase="Enter a description for your blog.">"
        help_page="blog_settings_general"
        help_section="blog_description">
        <textarea name="description" id="description" class="full-width short" cols="" rows=""><mt:var name="description" escape="html"></textarea>
    </mtapp:setting>

    <mtapp:setting
        id="server_offset"
        label="<__trans phrase="Timezone">"
        hint="<__trans phrase="Select your timezone from the pulldown menu.">"
        help_page="blog_settings_general"
        help_section="blog_timezone">
        <select name="server_offset" id="server_offset" class="half-width">
        <option value=""><__trans phrase="Time zone not selected"></option>
        <option value="13"<mt:if name="SERVER_OFFSET_13"> selected="selected"</mt:if>><__trans phrase="UTC+13 (New Zealand Daylight Savings Time)"></option>
        <option value="12"<mt:if name="SERVER_OFFSET_12"> selected="selected"</mt:if>><__trans phrase="UTC+12 (International Date Line East)"></option>
        <option value="11"<mt:if name="SERVER_OFFSET_11"> selected="selected"</mt:if>><__trans phrase="UTC+11"></option>
        <option value="10"<mt:if name="SERVER_OFFSET_10"> selected="selected"</mt:if>><__trans phrase="UTC+10 (East Australian Time)"></option>
        <option value="9.5"<mt:if name="SERVER_OFFSET_9_5"> selected="selected"</mt:if>><__trans phrase="UTC+9.5 (Central Australian Time)"></option>
        <option value="9"<mt:if name="SERVER_OFFSET_9"> selected="selected"</mt:if>><__trans phrase="UTC+9 (Japan Time)"></option>
        <option value="8"<mt:if name="SERVER_OFFSET_8"> selected="selected"</mt:if>><__trans phrase="UTC+8 (China Coast Time)"></option>
        <option value="7"<mt:if name="SERVER_OFFSET_7"> selected="selected"</mt:if>><__trans phrase="UTC+7 (West Australian Time)"></option>
        <option value="6.5"<mt:if name="SERVER_OFFSET_6_5"> selected="selected"</mt:if>><__trans phrase="UTC+6.5 (North Sumatra)"></option>
        <option value="6"<mt:if name="SERVER_OFFSET_6"> selected="selected"</mt:if>><__trans phrase="UTC+6 (Russian Federation Zone 5)"></option>
        <option value="5.5"<mt:if name="SERVER_OFFSET_5_5"> selected="selected"</mt:if>><__trans phrase="UTC+5.5 (Indian)"></option>
        <option value="5"<mt:if name="SERVER_OFFSET_5"> selected="selected"</mt:if>><__trans phrase="UTC+5 (Russian Federation Zone 4)"></option>
        <option value="4"<mt:if name="SERVER_OFFSET_4"> selected="selected"</mt:if>><__trans phrase="UTC+4 (Russian Federation Zone 3)"></option>
        <option value="3.5"<mt:if name="SERVER_OFFSET_3_5"> selected="selected"</mt:if>><__trans phrase="UTC+3.5 (Iran)"></option>
        <option value="3"<mt:if name="SERVER_OFFSET_3"> selected="selected"</mt:if>><__trans phrase="UTC+3 (Baghdad Time/Moscow Time)"></option>
        <option value="2"<mt:if name="SERVER_OFFSET_2"> selected="selected"</mt:if>><__trans phrase="UTC+2 (Eastern Europe Time)"></option>
        <option value="1"<mt:if name="SERVER_OFFSET_1"> selected="selected"</mt:if>><__trans phrase="UTC+1 (Central European Time)"></option>
        <option value="0"<mt:if name="SERVER_OFFSET_0"> selected="selected"</mt:if>><__trans phrase="UTC+0 (Universal Time Coordinated)"></option>
        <option value="-1"<mt:if name="SERVER_OFFSET__1"> selected="selected"</mt:if>><__trans phrase="UTC-1 (West Africa Time)"></option>
        <option value="-2"<mt:if name="SERVER_OFFSET__2"> selected="selected"</mt:if>><__trans phrase="UTC-2 (Azores Time)"></option>
        <option value="-3"<mt:if name="SERVER_OFFSET__3"> selected="selected"</mt:if>><__trans phrase="UTC-3 (Atlantic Time)"></option>
        <option value="-3.5"<mt:if name="SERVER_OFFSET__3_5"> selected="selected"</mt:if>><__trans phrase="UTC-3.5 (Newfoundland)"></option>
        <option value="-4"<mt:if name="SERVER_OFFSET__4"> selected="selected"</mt:if>><__trans phrase="UTC-4 (Atlantic Time)"></option>
        <option value="-5"<mt:if name="SERVER_OFFSET__5"> selected="selected"</mt:if>><__trans phrase="UTC-5 (Eastern Time)"></option>
        <option value="-6"<mt:if name="SERVER_OFFSET__6"> selected="selected"</mt:if>><__trans phrase="UTC-6 (Central Time)"></option>
        <option value="-7"<mt:if name="SERVER_OFFSET__7"> selected="selected"</mt:if>><__trans phrase="UTC-7 (Mountain Time)"></option>
        <option value="-8"<mt:if name="SERVER_OFFSET__8"> selected="selected"</mt:if>><__trans phrase="UTC-8 (Pacific Time)"></option>
        <option value="-9"<mt:if name="SERVER_OFFSET__9"> selected="selected"</mt:if>><__trans phrase="UTC-9 (Alaskan Time)"></option>
        <option value="-10"<mt:if name="SERVER_OFFSET__10"> selected="selected"</mt:if>><__trans phrase="UTC-10 (Aleutians-Hawaii Time)"></option>
        <option value="-11"<mt:if name="SERVER_OFFSET__11"> selected="selected"</mt:if>><__trans phrase="UTC-11 (Nome Time)"></option>
        </select>
    </mtapp:setting>

    <mtapp:setting
        id="has-license"
        label="<__trans phrase="License">"
        content_class="field-content-text"
        help_page="blog_settings_general"
        help_section="creative_commons_license">

<div id="has-license" style="display: none;">
<mt:if name="cc_license"><a href="<mt:var name="cc_license_url">"><img src="<mt:var name="cc_license_image_url">" /></a><br /></mt:if>
<__trans phrase="Your blog is currently licensed under:"> <strong id="cc-license-name"><mt:var name="cc_license_name"></strong><br />
<a href="javascript:void(0);" onclick="return setLicense();"><__trans phrase="Change license"></a> | <a href="javascript:void(0);" onclick="return doRemoveLicense();"><__trans phrase="Remove license"></a>
</div>
<div id="no-license" style="display: none;">
<__trans phrase="Your blog does not have an explicit Creative Commons license."><br />
<a href="javascript:void(0);" onclick="return setLicense();"><__trans phrase="Select a license"></a>
</div>
<mt:if name="cc_license">
<script type="text/javascript">
    var e = getByID('has-license');
    if (e) e.style.display = 'block';
</script>
<mt:else>
<script type="text/javascript">
    var e = getByID('no-license');
    if (e) e.style.display = 'block';
</script>
</mt:if>
<input type="hidden" name="cc_license" value="<mt:var name="cc_license" escape="html">" />
    </mtapp:setting>

    </fieldset>


<!-- HolidayCalendar DateData Add -->
    <fieldset>

    <h3>Input Target Month</h3>
    <h4>Example: 2008/05</h4>
<mtapp:setting
    id="calendar_currentmonth"
    label="TargetMonth">
    <div class="textarea-wrapper">
        <input name="calendar_currentmonth" id="calendar_currentmonth" class="full-width" maxlength="100" value="<mt:var name="calendar_currentmonth" escape="html">" class="wide" />
    </div>
</mtapp:setting>

    <h3>Input Holiday Data</h3>
    <h4>Example:  5,6,12,13,20,21,28,29</h4>
<mtapp:setting
    id="calendar_holiday"
    label="HolidayData">
    <div class="textarea-wrapper">
        <input name="calendar_holiday" id="calendar_holiday" class="full-width" maxlength="100" value="<mt:var name="calendar_holiday" escape="html">" class="wide" />
    </div>
</mtapp:setting>
    </fieldset>
<!-- HolidayCalendar DateData Add -->


<mt:setvarblock name="action_buttons">
    <button
        type="submit"
        accesskey="s"
        title="<__trans phrase="Save changes to these settings (s)">"
        class="primary-button"
        ><__trans phrase="Save Changes"></button>
</mt:setvarblock>
<mt:include name="include/actions_bar.tmpl" bar_position="bottom" hide_pager="1" settings_bar="1">

</form>

<mt:include name="include/footer.tmpl">

トラックバック(0)

このブログ記事を参照しているブログ一覧: MT4.1:プラグイン作成におけるDBへのフィールド追加について(ブログ毎編)

このブログ記事に対するトラックバックURL:

コメントする






Categories
Entries
Feed
スポンサードリンク

parts

フィードメーター - Under the Bridge

あわせて読みたい

なかのひと

2008 yoshi(apstar)