カスタムブロックは、通常legacyモジュールやaltsysで作ることができます。ここでは、プリロードでカスタムブロックを作ることを紹介します。
その前に、ここで紹介するTipsは、管理画面でカスタムブロックを作るより、はるかに回りくどくPHPの知識が必要な点を注意しておきます。なので、特殊な機能や要件を必要としない限り、カスタムブロックは管理画面で作るべきです。
まず、XOOPS Cubeのプリロードでカスタムブロックを作るメリットはいくつかあります。
特にこの中でも、ファイルとして扱えるメリットは大きいです。たとえば、開発環境で一定の変数を出力したりとデバッグ用途にブロックをプリロードで実装します。あとは、公開するときにプリロードを削除するだけで、データベースを汚したりすることは一切ありません。管理画面で操作するブロックだと、ついつい消し忘れ・消し損ないなんてこともありますが、プリロードだとファイルを確実に削除すれば、消し損なうこともありません。
ダウンロード次に、プリロードのソースを紹介します。16行目の$indexが、ブロックを表示する位置になります。l, r, cl, cc, crの順に、左・右・中央左・中央中・中央右を意味します。
18行目から22行目はブロックの表示に関する設定です。nameはブロック名で、アルファベット英数ですきな名前を指定します。titleはブロックの題目で、ブロックの上部に表示されます。contentはブロックの内容で、HTMLなどで書くことができます。weightはブロックの並び順で、数値が小さいほど上に、数値が多きほど下にブロックが表示されます。
<?php
/**
* A simple description for this script
*
* PHP Version 5.2.4 or Upper version
*
* @package PreloadBlock
* @author Hidehito NOZAWA aka Suin <http://suin.asia/>
* @copyright 2009 Hidehito NOZAWA
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU GPL v2.0
*
*/
class PreloadBlock extends XCube_ActionFilter
{
protected $index = 'l';
protected $block = array(
'id' => -1,
'name' => 'preload_block',
'title' => 'プリロードブロック',
'content' => '<i>ブロックの内容</i>',
'weight' => -1, // 並び順
);
protected $indexes = array(
'l' => 0,
'r' => 1,
'cl' => 3,
'cr' => 4,
'cc' => 5
);
protected $blocks = array(
'l' => 'xoops_lblocks',
'r' => 'xoops_rblocks',
'cl' => 'xoops_clblocks',
'cr' => 'xoops_crblocks',
'cc' => 'xoops_ccblocks'
);
public function postFilter()
{
if ( $this->mController->_mStrategy->mStatusFlag != LEGACY_CONTROLLER_STATE_PUBLIC )
{
return;
}
$this->mRoot->mDelegateManager->add('Legacy_RenderSystem.BeginRender', array(&$this, 'beginRender'));
$this->mRoot->mContext->mAttributes['legacy_BlockShowFlags'][$this->indexes[$this->index]] = true;
$this->mRoot->mContext->mAttributes['legacy_BlockContents'][$this->indexes[$this->index]][] = $this->block;
}
protected function beginRender(&$tpl)
{
if ( !empty($tpl->_tpl_vars['isFileTheme']) )
{
uasort($tpl->_tpl_vars[$this->blocks[$this->index]], array(&$this, 'reOrder'));
}
}
protected function reOrder($a, $b)
{
if ($a['weight'] == $b['weight'])
{
return 0;
}
return $a['weight'] > $b['weight'] ? 1 : -1;
}
}
?>
なお、このプリロードはしゃのさんのnowプリロードを参考にさせていただきました。
トラバURL : http://suin.asia/trackback/320
氷川 XOOPS Module 開発室