KtaiLibraryのヘルパー、imageメソッドをセッション維持するよう改良

· application, 携帯

MASA-P氏のKtaiLibraryにはかんなりお世話になっているのですが、少し、自分用にカスタマイズしたのでそのエントリー。

そもそも改良しようとした発端は、imgタグのsrc属性にセッションを持たせる必要が出てきたためでした。もともとは、(version 0.2.0)

function image($path, $htmlAttributes = array()){
	if(isset($htmlAttributes['width']) && isset($htmlAttributes['height'])){
		$arr = $this->_lib3gk->stretch_image_size($htmlAttributes['width'], $htmlAttributes['height']);
		$htmlAttributes['width']  = $arr[0];
		$htmlAttributes['height'] = $arr[1];
	}
	return $this->Html->image($path, $htmlAttributes);
}

このように画像サイズを適切に変更して、cakePHPのHtml->image()に渡すのですが、これに追記する形で、

function image($path, $htmlAttributes = array()){

	if(isset($htmlAttributes['width']) && isset($htmlAttributes['height'])){
		$arr = $this->_lib3gk->stretch_image_size($htmlAttributes['width'], $htmlAttributes['height']);
		$htmlAttributes['width']  = $arr[0];
		$htmlAttributes['height'] = $arr[1];
	}

	// papettoTV add start
	if($this->options['enable_ktai_session'] &&
		($this->options['use_redirect_session_id'] || $this->is_imode())){
		if(strpos($path, '?') === false){
			$path .= '?';
		}else{
			$path .= '&';
		}
		$path .= sprintf("%s=%s", session_name(),urlencode(session_id()));
	}
	// papettoTV add end

	return $this->Html->image($path, $htmlAttributes);
}

このように、クッキー非対応携帯の場合にセッションキーをURLに付与する処理を入れてみました。今のところこれで問題なく動いております。

(こういうことってgitとか使って報告していいのか迷い中、というかgitをまだよく分かっていない(汗))

画像未復旧: hatena.gif

関連があるかもしれないエントリー