FCKeditor を HTML_QuickForm で使う

FCKEditor の HTML_QuickForm のエレメントクラスを作ってくれた人がいたのでありがたく頂く.

FCKeditor element for HTML_QuickForm

ただし,このままでは確認画面などで入力フォームを Freeze(編集できないように)させているときにうまくいかない.
※通常,QuickFormでは自動で Hiddenパラメータを用意してくれる.

function getFrozenHtml() を書き換える.

* @access public
*/
class HTML_QuickForm_FCKeditor extends HTML_QuickForm_textarea
{
/**
* The width of the editor in pixels or percent
*
* @var string
* @access public
*/
var $Width = '100%';

/**
* The height of the editor in pixels or percent
*
* @var string
* @access public
*/
var $Height = '200';

/**
* The Toolbar set to use
*
* @var string
* @access public
*/
var $ToolbarSet = 'Default';

/**
* The path where to find the editor
*
* @var string
* @access public
*/
var $BasePath = '/fckeditor/';

/**
* Check for browser compatibility
*
* @var boolean
* @access public
*/
var $CheckBrowser = true;

/**
* Configuration settings for the editor
*
* @var array
* @access public
*/
var $Config = array();

/**
* Class constructor
*
* @param string FCKeditor instance name
* @param string FCKeditor instance label
* @param array Config settings for FCKeditor
* @param string Attributes for the textarea
* @access public
* @return void
*/
function HTML_QuickForm_htmlarea($elementName=null, $elementLabel=null, $options=array(), $attributes=null)
{
HTML_QuickForm_element::HTML_QuickForm_element($elementName, $elementLabel, $attributes);
$this->_persistantFreeze = true;
$this->_type = 'FCKeditor';

if (is_array($options)) {
$this->Config = $options;
}
}

/**
* Set config variable for FCKeditor
*
* @param mixed Key of config setting
* @param mixed Value of config settinh
* @access public
* @return void
*/
function SetConfig($key, $value = null)
{
if (is_array($key)) {
foreach ($key as $k => $v) {
$this->Config[$k] = $v;
}
} else {
$this->Config[$key] = $value;
}
}

/**
* Check if the browser is compatible (IE 5.5+, Gecko > 20030210)
*
* @access public
* @return boolean
*/
function IsCompatible()
{
if (isset($_SERVER['HTTP_USER_AGENT']))
{
$agent = strtolower($_SERVER['HTTP_USER_AGENT']);
if ( ($msie = strpos($agent, 'msie') ) !== false &&
strpos($agent, 'opera') === false &&
strpos($agent, 'mac') === false)
{
return ( (float) substr($agent, $msie + 5, 3) >= 5.5);
} elseif ( ($gecko = strpos($agent, 'gecko')) !== false) {
return ( (int) substr($agent, $gecko + 6, 8 ) >= 20030210);
}
}
return false;
}

/**
* Make a string of the configuration to pass along in a hidden field
*
* @access private
* @return string
*/
function _getConfigFieldString()
{
$value = '';
$first = true;
foreach ($this->Config as $k => $v) {
if (!$first) {
$value .= '&';
} else {
$first = false;
}
$value .= $this->_encodeValue($k) . '=';
if ($v === true) {
$value .= 'true';
} elseif ($v === false) {
$value .= 'false';
} else {
$value .= $this->_encodeValue($v);
}
}
return $value;
}

/**
* Encode the given string so it can be used inside a value attribute
*
* @access private
* @param string
* @return string
*/
function _encodeValue($value)
{
$chars = array('&' => '%26D', '=' => '%3D', '"' => '%22');
return (strtr($value, $chars));
}

/**
* Return the htmlarea in HTML
*
* @access public
* @return string
*/
function toHtml()
{
if ($this->_flagFrozen) {
return $this->getFrozenHtml();
} elseif (!$this->IsCompatible()) {
return parent::toHtml();
} else {
$html = '';
$name = $this->getAttribute('name');
$this->updateAttributes(array('id' => $name));
$cname = $name . '___Config';
if (!defined('HTML_QUICKFORM_FCKEDITOR_LOADED')) {
// load FCKeditor
$html .= sprintf(
'',
$this->BasePath . 'fckeditor.js'
);
define('HTML_QUICKFORM_FCKEDITOR_LOADED', true);
}
// make link for iframe src
$link = $this->BasePath . 'editor/fckeditor.html?InstanceName=' . $name;
if (strlen($this->ToolbarSet)) {
$link .= '&Toolbar=' . $this->ToolbarSet;
}
// render the linked hidden field
$html .= sprintf('',
$name, $name, htmlspecialchars($this->getValue()));
// render the config hidden field
$html .= sprintf('',
$cname, $cname, $this->_getConfigFieldString());
// render the editor iframe
$html .= sprintf(
'',
$name . '___Frame', $link, $this->Width, $this->Height
);
return $html;
}
}

// }}}
// {{{ getFrozenHtml()

/**
* Returns the value of field without HTML tags (in this case, value is changed to a mask)
*
* @since 1.0
* @access public
* @return string
*/
function getFrozenHtml()
{
$value = htmlspecialchars($this->getValue());
if ($this->getAttribute('wrap') == 'off') {
$html = $this->_getTabs() . '<pre>' . $value."</pre>\n";
} else {
$html = nl2br($value)."\n";
}
return $html . $this->_getPersistantData();
} //end func getFrozenHtml

// }}}

これを HTML/QuickForm/fckeditor.php として保存

下記のように使う


<?php

require_once 'HTML/QuickForm.php';
require_once 'HTML/QuickForm/fckeditor.php';

$form = new HTML_QuickForm;

$example1 =& HTML_QuickForm::createElement('fckeditor', 'example1', 'Example 1');
$example1->Width = 500;
$example1->Height = 200;
$example1->BasePath = './FCKeditor/';
$example1->ToolbarSet = 'limited';
$example1->Config['AutoDetectLanguage'] = false;
$example1->Config['DefaultLanguage'] = 'de';
$example1->Config['CustomConfigurationsPath'] = dirname($_SERVER['PHP_SELF']) . '/FCKeditor/myconfig.js';
>?>