NucleusにCKEditorとCKFinderを実装するプラグインの超いい加減な作り方
前回の記事の続編です。
Nucleusの次期バージョンはおそらくPHP5完全対応になるだろうから、プラグインの作り方も変わってくると思うんですよね。
だからというのは理由になりませんが、面倒くさかったので超いい加減なプラグインを作ってしまいました。
その作り方です(笑)。
ちなみに私が使っているのはNucleusCMS 3.41です。
まずnucleus/plugins/にNP_CK.phpというファイルを作りました。
中身はこんな感じです。(圧縮してます)
<?php
class NP_CK extends NucleusPlugin {
function getName() {return 'NP_CK';}
function getAuthor() {return 'Tom Goodsun';}
function getURL() {return 'http://www.tom-gs.com/';}
function getVersion() {return '1.0';}
function getDescription() {return 'Plugin to implement CKEditor and CKFinder.';}
function install() {/* Do nothing */}
function getEventList() {return array('AdminPrePageHead');}
function event_AdminPrePageHead(&$data){
$action = $data['action'];
$extrahead = "\n";
if ($action == 'createitem' || $action == 'itemedit'){
$extrahead .= '<!-- tom goodsun -->' . "\n";
$extrahead .= '<script type="text/javascript" src="' . $this->getAdminURL(). 'ckeditor/nucleus.php"></script>' . "\n";
$extrahead .= '<link type="text/css" rel="stylesheet" href="' . $this->getAdminURL(). 'ckeditor/nucleus.css" />' . "\n";
}
$data['extrahead'] .= $extrahead;
}
}
?>
次にnucleus/plugins/にckというディレクトリを作りました。
その中にckeditorとckfinderというディレクトリを作りました。
公式サイトからパッケージをダウンロードして、解凍した中身をそれぞれのディレクトリに入れました。CKFinderはPHP版をダウンロードしました。
CKEditorの設定
上記プログラムのevent_AdminPrePageHead()という関数を見てもわかるとおりJavaScriptとして、nucleus.phpを読み込むことでCKEditorを実装します。
nucleus/plugins/ck/ckeditor/nucleus.phpの内容は以下の通り(JavaScriptもPHPが使えるっていう裏技(?)です)
<?php
// config.phpを使うと認証系とかもやってくれる
require('../../../../config.php');
if (!$member->isLoggedIn()) {exit('Not logged in.');}
$url = $member->getURL();
?>
var nucleusConfig = {
toolbar: 'NP_CK',
language: 'ja',
toolbar_NP_CK : [/* ツールバーの設定は割愛(実際してます!) */],
// CKFinder integrated code
filebrowserBrowseUrl : '<?php echo $url; ?>nucleus/plugins/ck/ckfinder/ckfinder.html',
filebrowserImageBrowseUrl : '<?php echo $url; ?>nucleus/plugins/ck/ckfinder/ckfinder.html?Type=Images',
filebrowserFlashBrowseUrl : '<?php echo $url; ?>nucleus/plugins/ck/ckfinder/ckfinder.html?Type=Flash',
filebrowserUploadUrl : '<?php echo $url; ?>nucleus/plugins/ck/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files',
filebrowserImageUploadUrl : '<?php echo $url; ?>nucleus/plugins/ck/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
filebrowserFlashUploadUrl : '<?php echo $url; ?>nucleus/plugins/ck/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash',
}
function createCkeditor(){
CKEDITOR.replace('body', nucleusConfig); // 本文
CKEDITOR.replace('more', nucleusConfig); // 本文の続き
}
if ( typeof console != 'undefined' ){console.log();}
if ( window.addEventListener ){
window.addEventListener( 'load', createCkeditor, false );
}else if ( window.attachEvent ){
window.attachEvent( 'onload', createCkeditor );
}
これでCKEditorの設定は終わり。CKEditorの設定でCKFinderの設定もしてしまったので、とっととCKFinderの設定もやっちゃいましょう。
CKFinderの設定
nucleus/plugins/ck/ckfinder/config.phpの設定を変えるだけです。
まずはNucleusを使う前提なので、プログラムの先頭に以下を追加。
require('../../../../../../../config.php');
if (!$member->isLoggedIn()) {exit('Not logged in.');}
CheckAuthentication()関数の戻り値はtrueにする。
function CheckAuthentication(){return true;}
$baseUrlの初期値を変更
$baseUrl = preg_replace('/(http|https):\/\/' . $_SERVER['SERVER_NAME'] . '/', '', $member->getURL() . 'media/');
$config['Thumbnails']はお好みの設定に。
enabledをtrueにするとサムネール表示ができる。
これで終わり。
あとはNucleusの管理画面からプラグインをインストールするだけ。
ちなみにCKFinderのブラウザ画面はNucleusのconfig.phpがインクルードされるので、認証エラーになっちゃうんですね。
問題点といえばレンタルサーバーだとサーバー変数が期待通りの値になっていないことがあるみたいなので、注意することぐらいですかね。
コメント