21-6 管理アプリケーションプラグイン

425ページ

config.yaml

callbacks:
    MT::App::CMS::template_source.テンプレートファイル名: ハンドラ

426ページ

callbacks:
    MT::App::CMS::template_source.edit_entry: $Example::Example::CMS::change_source
callbacks:
    MT::App::CMS::template_source.header: $Example::Example::CMS::change_header

CMS.pm

package Example::CMS;
use strict;
 
sub change_source {
    my ($cb, $app, $tmpl) = @_;
    my $old = <<HERE;
    <mt:else eq="excerpt">
HERE
    $old = quotemeta($old);
 
    my $new = <<HERE;
<mtapp:setting id="price" label="Price" label_class="top-label">
<input type="text" name="price" id="price" class="full-width" value="<mt:GetVar name="price" />" />
</mtapp:setting>
    <mt:else eq="excerpt">
HERE
    $$tmpl =~ s/$old/$new/;
}
 
1;

427ページ

config.yaml

callbacks:
    MT::App::CMS::template_param.テンプレートファイル名: 起動関数
callbacks:
    MT::App::CMS::template_param.edit_entry: $Example::Example::CMS::get_product_field

CMS.pm

package Example::CMS;
use strict;
 
sub add_param {
    my ($cb, $app, $param, $tmpl) = @_;
    $param->{ name } = 'Movable Type Book';
    $param->{ price } = '2000';
}
 
1;
<mt:GetVar name="name" />
<mt:GetVar name="price" />

CMS.pm

package Example::CMS;
use strict;
 
sub get_product_field {
    my ($cb, $app, $param, $tmpl) = @_;
    my $field = $tmpl->getElementById('keywords');
    my $node = $tmpl->createElement('app:setting',
                             { id => 'price',
                               label => 'Price',
                               label_class => "top-label" });
    my $innerHTML = '<input type="text" name="price" id="price" class="full-width" value="<mt:GetVar name="price" />" />';
    $node->innerHTML($innerHTML);
    $tmpl->insertAfter($node, $field);
}
 
1;

430ページ

config.yaml

callbacks:
    MT::App::CMS::template_output.テンプレートファイル名: 起動関数
callbacks:
    MT::App::CMS::template_output.edit_entry: $Example::Example::CMS::change_output

CMS.pm

package Example::CMS;
use strict;
 
sub change_output {
    my ($cb, $app, $output, $param, $tmpl) = @_;
    ...中略...
}
 
1;

431ページ

config.yaml

name: Example Plugin
id: Example
version: 1.0
schema_version: 1.0
object_types:
    product: Example::Product
callbacks:
    MT::App::CMS::template_param.edit_entry: $Example::Example::CMS::get_product_field
    MT::App::CMS::cms_post_save.entry: $Example::Example::CMS::save_product
    MT::App::CMS::cms_post_delete.entry: $Example::Example::CMS::delete_product

432ページ

CMS.pm

package Example::CMS;
 
use strict;
use Example::Product;
 
sub get_product_field {
    my ($cb, $app, $param, $tmpl) = @_;
    my $field = $tmpl->getElementById('keywords');
    my $node = $tmpl->createElement('app:setting',
                       { id => 'size', label => 'Size', label_class => "top-label" });
    my $innerHTML = '<input type="text" name="size" id="size" class="full-width" value="<mt:GetVar name="size" />" />';
    $node->innerHTML($innerHTML);
    $tmpl->insertAfter($node, $field);
    $node = $tmpl->createElement('app:setting',
                    { id => 'price', label => 'Price', label_class => "top-label" });
    $innerHTML = '<input type="text" name="price" id="price" class="full-width" value="<mt:GetVar name="price" />" />';
    $node->innerHTML($innerHTML);
    $tmpl->insertAfter($node, $field);
    $node = $tmpl->createElement('app:setting',
                    { id => 'name', label => 'Name', label_class => "top-label" });
    $innerHTML = '<input type="text" name="name" id="name" class="full-width" value="<mt:GetVar name="name" />" />';
    $node->innerHTML($innerHTML);
    $tmpl->insertAfter($node, $field);
    my $id = $param->{id};
    if ($id) {
        my $product = Example::Product->load({ entry_id => $id });
        if ($product) {
            $param->{ name } = $product->name;
            $param->{ price } = $product->price;
            $param->{ size } = $product->size;
        }
    }
}
 
sub save_product {
    my ($cb, $app, $entry, $orig_obj) = @_;
    my $id = $entry->id;
    if ($id) {
        my $product = Example::Product->load({ entry_id => $id });
        $product = Example::Product->new unless $product;
        $product->entry_id($id);
        my $q = $app->param;
        $product->name($q->param('name'));
        $product->price($q->param('price'));
        $product->size($q->param('size'));
        $product->save or die $product->errstr;
    }
}
 
sub delete_product {
    my ($cb, $app, $entry) = @_;
    my $id = $entry->id;
    if ($id) {
        my $product = Example::Product->load({ entry_id => $id });
        return 1 unless defined $product;
        $product->remove;
    }
}
 
1;

config.yaml

name: Example Plugin
id: Example
l10n_lexicon:
    ja:
        List Product: 商品一覧
        Edit Product: 商品の登録
        Update Product: 商品の更新
        Create: 登録
        Update: 更新
        Product: 商品
        product: 商品
        products: 商品
        Price: 価格
        Size: サイズ
        This data has been saved.: データを保存しました
        Delete selected products (x): 商品を削除
        The selected products(s) has been deleted from the database.: 選択した商品を削除しました
version: 1.0
schema_version: 1.0
object_types:
    product: Example::Product
applications:
    cms:
        menus:
            settings:list_product:
                label: List Product
                order: 10
                mode: list_product
                view:
                - blog
                - website
                permission: manage_pages
            settings:edit_product:
                label: Edit Product
                order: 20
                mode: edit_product
                view:
                - blog
                - website
                permission: manage_pages
        methods:
            list_product: $Example::Example::CMS::list_product
            edit_product: $Example::Example::CMS::edit_product

436ページ

package Example::CMS;
 
use strict;
use Example::Product;
 
sub edit_product {
    my $app = shift;
    my %param;
    my $q = $app->param;
    my $id = $q->param('id');
    if (!$id) {
        $param{ new_object } = 1;
        $param{ page_title } = MT->translate('Edit Product');
    } else {
        my $product = Example::Product->load({ id => $id });
        $param{ id } = $q->param('id');
        $param{ name } = $product->name;
        $param{ price } = $product->price;
        $param{ size } = $product->size;
        $param{ page_title } = MT->translate('Update Product');
    }
    $param{ saved } = $q->param('saved');
    my $tmpl = 'edit_product.tmpl';
    return $app->build_page($tmpl, \%param);
}
 
sub list_product {
    my $app = shift;
    my ( %param, %args, %terms );
    my $q = $app->param;
    $param{ saved_deleted } = $q->param('saved_deleted');
    $param{ screen_id } = 'list-product';
    $param{ screen_class } = 'list-product';
    my $code = sub {
        my ( $obj, $row ) = @_;
        $row->{ product_name } = $obj->name;
        $row->{ product_price } = $obj->price;
        $row->{ product_size } = $obj->size;
    };
    return $app->listing(
        {
            type => 'product',
            code => $code,
            args => \%args,
            params => \%param,
            terms => \%terms,
        }
    );
}
 
1;

438ページ

edit_product.tmpl

<mt:Include name="include/header.tmpl">
<mt:if name="saved">
  <mtapp:statusmsg id="saved-added" class="success">
    <__trans phrase="This data has been saved.">
  </mtapp:statusmsg>
</mt:if>
<form method="post" action="<mt:GetVar name="script_url">" id="new_product_form">
<mt:unless name="new_object">
  <input type="hidden" name="id" value="<mt:GetVar name="id" escape="html">" />
</mt:unless>
  <input type="hidden" name="__mode" value="save" />
  <input type="hidden" name="_type" value="product" />
  <input type="hidden" name="blog_id" value="<mt:GetVar name="blog_id" escape="html">" />
  <input type="hidden" name="return_args" value="<mt:GetVar name="return_args" escape="html">" />
  <input type="hidden" name="magic_token" value="<mt:GetVar name="magic_token">" />
  <fieldset>
<mtapp:setting id="name" shown="1" label="<__trans phrase="Name">" label_class="top-label">
    <input type="text" name="name" id="name" class="full-width" value="<mt:GetVar name="name" />" />
</mtapp:setting>
<mtapp:setting id="price" shown="1" label="<__trans phrase="Price">" label_class="top-label">
    <input type="text" name="price" id="price" class="full-width" value="<mt:GetVar name="price" />" />
</mtapp:setting>
<mtapp:setting id="size" shown="1" label="<__trans phrase="Size">" label_class="top-label">
    <input type="text" name="size" id="size" class="full-width" value="<mt:GetVar name="size" />" />
</mtapp:setting>
<mt:if name="new_object">
  <mt:SetVarBlock name="action_buttons">
    <button type="submit" accesskey="s" title="<__trans phrase="Create">" 
      class="create action primary-button"><__trans phrase="Create"></button>
  </mt:SetVarBlock>
<mt:else>
  <mt:SetVarBlock name="action_buttons">
    <button type="submit" accesskey="s" title="<__trans phrase="Update">"
      class="create action primary-button"><__trans phrase="Update"></button>
  </mt:SetVarBlock>
</mt:if>
<mt:Include name="include/actions_bar.tmpl" bar_position="bottom" hide_pager="1"
settings_bar="1">
  </fieldset>
</form>
<mt:Include name="include/footer.tmpl">

440ページ

list_product.tmpl

<mt:SetVarBlock name="page_title"><__trans phrase="Product"><__trans phrase="Manage"></mt:SetVarBlock>
<mt:SetVarBlock name="system_msg">
  <mt:if name="emptied">
    <mtapp:statusmsg
      id="emptied"
      class="success">
      <__trans phrase="All products reported as spam have been removed.">
    </mtapp:statusmsg>
  </mt:if>
  <mt:if name="saved_deleted">
    <mtapp:statusmsg
      id="saved-deleted"
      class="success">
      <__trans phrase="The selected products(s) has been deleted from the database.">
    </mtapp:statusmsg>
  </mt:if>
</mt:SetVarBlock>
<mt:SetVarBlock name="content_header">
  <mt:if name="object_type" eq="product">
    <p><a href="<mt:GetVar name="script_url">?__mode=edit_product&amp;_type=product&amp;blog_id=<mt:GetVar name="blog_id">" class="icon-left icon-create"><__trans phrase="Edit Product"></a></p>
  </mt:if>
</mt:SetVarBlock>
<mt:SetVarBlock name="html_body_footer">
  <mt:Include name="include/display_options.tmpl" />
</mt:SetVarBlock>
<mt:SetVarBlock name="html_head" append="1">
<script type="text/javascript">
/* <![CDATA[ */
var tableSelect;
function init()
{
    // setup
    tableSelect = new TC.TableSelect( "product-listing-table" );
    tableSelect.rowSelect = true;
}
/* ]]> */
</script> 
</mt:SetVarBlock>
<mt:Include name="include/header.tmpl" />
 
<mt:SetVarBlock name="action_buttons">
<button
  onclick="doRemoveItems(getByID('<mt:GetVar name="object_type" />-listing-form'), '<__trans phrase="Product" escape="js">', '<__trans phrase="Product" escape="js">'); return false;"
  accesskey="x"
  title="<__trans phrase="Delete selected products (x)">"
><__trans phrase="Delete"></button>
</mt:SetVarBlock>
<mtapp:listing id="hoge-listing">
  <mt:if name="__first__">
    <mt:SetVarBlock name="table_header">
    <tr>
      <th class="cb"><input type="checkbox" name="id-head" value="all" class="select" /></th>
      <th class="name primary-col"><__trans phrase="[_1] Name" params="<mt:GetVar name="object_label" capitalize="1">"></th>
      <th class="price"><__trans phrase="Price"></th>
      <th class="size"><__trans phrase="Size"></th>
    </tr>
    </mt:SetVarBlock>
  <thead>
    <mt:GetVar name="table_header">
  </thead>
  <tfoot>
    <mt:GetVar name="table_header">
  </tfoot>
  <tbody>
  </mt:if>
    <tr class="<mt:if name="__odd__">odd<mt:else>even</mt:if>">
      <td class="cb"><input type="checkbox" name="id" value="<mt:GetVar name="id">" class="select" /></td>
      <td class="name"><a href="?__mode=edit_product&amp;blog_id=<mt:GetVar name="blog_id">&amp;id=<mt:GetVar name="id">"><mt:GetVar name="product_name" escape="html"></a></td>
      <td class="price"><mt:GetVar name="product_price"></td>
      <td class="size"><mt:GetVar name="product_size"></td>
    </tr>
  <mt:if name="__last__">
  </tbody>
  </mt:if>
</mtapp:listing>
<mt:SetVarBlock name="jq_js_include" append="1">
    init();
    jQuery.mtCheckbox();
    jQuery('button.mt-delete-items').mtDeleteItems({
        id: '<mt:GetVar name="object_type" />-listing-form',
        singular: '<mt:GetVar name="object_label" lower_case="1" escape="js">',
        plural: '<mt:GetVar name="object_label_plural" lower_case="1" escape="js">'
    });
</mt:SetVarBlock>
<mt:Include name="include/footer.tmpl" />

444ページ

config.yaml

name: AddListAction
id: AddListAction
l10n_lexicon:
    ja:
        Review Entries: ブログ記事の承認待ち
applications:
    cms:
        list_actions:
            entry:
                review_entries:
                    label: Review Entries
                    order: 100
                    handler: $AddListAction::AddListAction::review_entries

445ページ

AddListAction.pm

package AddListAction;
use MT::CMS::Entry;
 
sub review_entries {
    my $app = shift;
    require MT::Entry;
    MT::CMS::Entry::update_entry_status( $app, MT::Entry::REVIEW(), $app->param('id') );
}
 
1;

446ページ

config.yaml

name: AddListFilter
id: AddListFilter
l10n_lexicon:
    ja:
        Current Monthly Entries: 今月に投稿したブログ記事
applications:
    cms:
        list_filters:
            entry:
                current_monthly_entry_filter:
                    label: Current Monthly Entries
                    order: 100
                    handler: $AddListFilter::AddListFilter::current_monthly_entry_filter
                    condition: >
                        sub {
                            MT->app->mode eq 'list_entry' || MT->app->param('_type') eq 'entry';
                        }

AddListFilter.pm

package AddListFilter;
 
use strict;
use MT::Util qw( offset_time_list );
 
sub current_monthly_entry_filter {
    my ( $terms, $args ) = @_;
    require POSIX;
    my @now = offset_time_list( time, MT->app->blog );
    my $from = POSIX::strftime( "%Y%m", @now );
    $from .= '01000000';
    my $to = POSIX::strftime( "%Y%m", @now );
    $to .= '31235959';
    $terms->{ authored_on } = [ $from, $to ];
    $args->{ range_incl }{ authored_on } = 1;
}
 
1;

447ページ

config.yaml

name: AddTextFilter
id: AddTextFilter
l10n_lexicon:
    ja:
        Add Paragraph: パラグラフ要素で囲む
text_filters:
    Add Paragraph:
        label: Add Paragraph
        order: 100
        handler: $AddTextFilter::AddTextFilter::add_paragraph

448ページ

AddTextFilter.pm

package AddTextFilter;
 
use strict;
 
sub add_paragraph {
    my $text = shift;
    my $ctx = shift;
    $text =~ s/(.*)/<p>$1<\/p>/;
    return $text;
}
 
1;

config.yaml

name: AddDashBoardWidget
id: AddDashBoardWidget
l10n_lexicon:
    ja:
        Recent Entries: 最近の記事
widgets:
    recent_entries:
        label: Recent Entries
        template: recent_entries.tmpl
        set: sidebar
        singular: 1
        handler: $AddDashBoardWidget::AddDashBoardWidget::recent_entries
        view: blog

449ページ

AddDashBoardWidget.pm

package AddDashBoardWidget;
 
use strict;
 
sub recent_entries {
    my ($app, $tmpl, $param) = @_;
    my $entries = sub {
        my ($terms, $args);
        $terms->{ blog_id } = $app->blog->id;
        $args->{ limit } = 5;
        $args->{ sort } = 'authored_on';
        $args->{ direction } = 'descend';
        my @entries = MT::Entry->load( $terms, $args );
        \@entries;
    };
    my $ctx = $tmpl->context;
    $ctx->{__stash}{ entries } = MT::Promise::delay($entries);
}
 
1;

450ページ

recent_entries.tmpl

<mtapp:widget class="widget" label="<__trans phrase="Recent Entries">" can_close="1">
<mt:Entries>
  <mt:EntriesHeader>
<ul>
  </mt:EntriesHeader>
<li><a href="<mt:GetVar name="script_url"/>?__mode=view&amp;_type=entry&amp;id=<mt:EntryID />&amp;blog_id=<mt:BlogID/>"><mt:EntryTitle /></a></li>
  <mt:EntriesFooter>
</ul>
  </mt:EntriesFooter>
</mt:Entries>
</mtapp:widget>

このアーカイブについて

最近のコンテンツはインデックスページで見られます。過去に書かれたものはアーカイブのページで見られます。