あ~ゆ~どり~ま~様の作られました、携帯用テンプレート切り替えプラグイン「MobileConverter」を改造し、親子関係を持った複数テンプレートの切り替え機能を実装してみました。
凄くややこしいですが、パラメータを追加しているので、設定のサンプルを以下に記載します。
※MobileConverterをある程度使用していて、設定方法が理解できる方向けに記載しておりますので、ご了承くださいませ。
※サンプルはDoCoMoのみ変換する場合です。
※AU,SoftBank等も同じ設計ですので、userAgent2にAUを割り当てるのであれば、sourceTemplate2subset1等で子のパラメータを作ってください。
&userAgent1=Docomo:User Agent;string;Docomo &switchTemplate1=Docomo:Template;string;mobileConvert &switchImgType1=Docomo:Image Type;string;gif
&sourceTemplate1subset1=Top:Source Template Name;string;Top
&switchTemplate1subset1=Docomo:Top Change To Template;string;mobileConvertTop
&sourceTemplate1subset2=ColumnTop:Source Template Name;string;ColumnTop
&switchTemplate1subset2=Docomo:ColumnTop Change To Template;string;mobileConvertColumnTop
解説:
1行目は親の設定であり、デフォルト設定となります。元の設定方法と変更してませんので、ダウングレードはありません。多分。
2行目以降が子の設定で、2行目と3行目の設定が1セットとなり、以下、subsetXで複数セット指定可能です。(Xはシーケンシャルで並ぶ必要があります。)
このサンプルが示す設定は
「ユーザーエージェントがDoCoMo(親設定を参照)であり、ドキュメントのテンプレートがTopであれば、mobileConvertTopに変更。もしくは、ドキュメントのテンプレートがColumnTopであれば、mobileConvertColumnTopに変更。それ以外は、mobileConvert(親設定を参照)に変更」
となります。
で、改造箇所ですが、
MobileConverter1.2を使います。
元プラグインの以下の箇所を置き換え。
165行目付近から193行目付近まで
$switchTemplate = ${'switchTemplate'.$i};
|中略
$i++;
}
を以下の内容に置き換えます。
$switchTemplate = ${'switchTemplate'.$i};
$switchImgType = ${'switchImgType'.$i};
if( ${'convertCharset'.$i} == 'no' ) // default yes
$convertCharset = false;
else
$convertCharset = true;
// ここでUserAgentが確定するので、ソーステンプレートが設定されてたら、ターゲットを設定。
$srcTemplateName = '';
$tbl = $modx->getFullTableName("site_templates");
$whr = 'id='.$modx->documentObject['template'].'';
if( $rs = $modx->db->select('id,templatename',$tbl,$whr) ){
if( $resource = $modx->fetchRow($rs) ){
$srcTemplateName = $resource['templatename'];
}
}
//元のテンプレートネームが取れたら比較してパラメータより新しくするテンプレートIDを割り出し。設定がなければ親設定を使用。
if ($srcTemplateName) {
$subid = 1;
$targetTemplateName = '';
while (isset(${'sourceTemplate'.$i.'subset'.$subid})) {
if (${'sourceTemplate'.$i.'subset'.$subid} === $srcTemplateName) {
$targetTemplateName = ${'switchTemplate'.$i.'subset'.$subid};
}
$subid++;
}
}
//子の置き換えテンプレート名が取れていれば、スイッチするテンプレートを子に置き換え。
if ($targetTemplateName) $switchTemplate = $targetTemplateName;
$whr = 'templatename=\''.$switchTemplate.'\'';
if( $rs = $modx->db->select('id,content',$tbl,$whr) ){
if( $resource = $modx->fetchRow($rs) ){
$modx->documentObject['template'] = $resource['id'];
$this->documentContent = $resource['content'];
}else{
$modx->logEvent(1, 1, 'Template not found:'.$switchTemplate, 'MobileConverter');
$bf= array('[+charset+]','[+template+]');
$af= array($modx->config['modx_charset'],$switchTemplate);
$TemplateNotFound = str_replace($bf,$af,$TemplateNotFound);
$this->documentContent = $TemplateNotFound;
}
if( $convertCharset)
$enableConvertCharset = true;
else
$enableConvertCharset = false;
}
break 2;
}
$i++;
}
一応テストは一通りしてますというか、本番で動いているので問題はないと思いますが、
例によってサポートはしませんので、おかしくなっても自己責任でお願いします。
※絵文字変換ロジックはこんな感じですが、許諾取れてませんので、参考までに手の内を明かします。(苦笑:コメントアウトしてます)
241行目付近
$modx->documentOutput = mb_convert_encoding($modx->documentOutput,$mobileCharset,$modx->config['modx_charset']);
を以下の内容に。
$modx->documentOutput = mb_convert_encoding($modx->documentOutput,$mobileCharset,$modx->config['modx_charset']);
/* ここからは、絵文字変換クラス。これも改造していますが、許諾取れていませんので非公開です。参考になれば。
$pluginfolder = "MobileClass";
include_once $modx->config['base_path'].'assets/plugins/'.$pluginfolder.'/lib/MobileClass.php';
$MobileClass = new MobileClass('/assets/plugins/'.$pluginfolder.'/emoji/');
$modx->documentOutput = $MobileClass->ConvertAll2($modx->documentOutput);
*/
$modx->documentOutput = mb_convert_kana($modx->documentOutput, "knrs", $mobileCharset);
とまあ、強制的に半角カナ変換してますが、携帯サイト用なのでやってます。PCとの併用でのテンプレート切り替えであれば不要ですね。
この場をお借りしまして、MODxの携帯対応という、「難関でモバイルガラパゴスでジャパーンな問題」に、素晴らしいプラグインを制作されました「あ~ゆ~どり~ま~」soushi様に感謝させて頂きます。
これが無ければ当方の携帯サイトは出来ておりませんですから。:-)
過去の経緯:
MODxで携帯サイトを作るには?(推敲中)
MODxで携帯サイトを作るには?続編(テスト中)
MODxで携帯サイトを作るには?その3