ExtPHP is an ExtJS development class library based on the Thinkphp development framework. Using this kind of library can easily generate ExtJS JavaScript code.
/** * PHPExtJs base object * @License: ( http://www.apache.org/licenses/LICENSE-2.0 ) * @Author: wb
*/
class ExtBase {
/**
* The base directory of ExtJS, this parameter is a path
* @var String
*/
public $exthome = '';
/**
* ExtJS locale configuration, the default is zh_cn (Chinese)
* @var String
*/
public $extlang = 'zh_cn';
/**
* ExtJS debugging mode, default is false
* @var Boolean
*/
public $debugmode = false;
/**
* Kernel mode for ExtJS, default is false
* @var Boolean
*/
public $coremode = false;
/**
* Base directory of ExtJS’s environment directory
* @var String
*/
public $extbasedir = "";
/**
*Basic code for ExtJS
*@var String
*/
public $extbasecode = "";
/**
* Css files required by the page
* @var Array
*/
public $pageCss = array();
/**
* Js files required for the page
* @var Array
*/
public $pageJs = array();
/**
* ExtJs css file
* @var String
*/
private $extcss = "";
/**
* The index of all files in the ExtJS directory is in the format: array (file name => file path)
* @var Array
*/
public $ExtALLFiels = array();
/**
* Define the basic running file of ExtJS. The format is: array (name => file name). Here are only the basic ones defined
* Such as: base, all, css, core, debug
* @var Array
*/
public $ExtBaseFile = array(
'base' => 'ext-base.js',
'all' => 'ext-all.js',
'css' => 'ext-all.css',
'core' => 'ext-core.js',
'debug' => 'ext-all-debug.js',
);
/**
* Set the basic environment of Extjs according to basic parameters
*
* @param string $exthome The directory where ExtJS is located, relative to the directory specified by $basedir
* @param boolen $extdebue Whether to enable debugging mode
* @param boolen $extcore Whether It is core mode
* @param string $extlang Set the ExtJS language
* @param string $basedir The directory where the $exthome directory is located
*/
public function __construct($exthome = '', $basedir='', $extdebue=false, $extcore=false, $extlang='zh_cn') {
//设置基本运行环境
$this->setExtBase($exthome, $basedir, $extdebue, $extcore, $extlang);
}
/**
* Set the base directory of Extjs
*
* @param String $exthome The directory where the ExtJs file is located
* @param String $basedir The directory where the directory is located is based on that directory. The default is ‘/’
* @return Boolean
*/
public function setExtHome($exthome="", $basedir="/") {
//TODO - 设置Extjs的基本目录
if (!empty($basedir)) {
$this->extbasedir = str_replace("/./", "/", $basedir);
}
if (!empty($exthome)) {
$this->ReadALLFile($exthome, $this->ExtALLFiels);
if (!empty($this->ReadALLFile[$this->ExtBaseFile['base']])) {
throw new Exception("不正确的exthome目录($exthome)!");
}
$this->exthome = $exthome;
}
return TRUE;
}
/**
* Set the basic environment of Extjs
*
* @param string $exthome The directory where ExtJS is located, relative to the directory specified by $basedir
* @param boolen $extdebue Whether to enable debugging mode
* @param boolen $extcore Whether it is core mode
* @param string $extlang Set the ExtJS language
* @param string $basedir The directory where the $exthome directory is located
* @return Boolean
*/
public function setExtBase($exthome = '', $basedir='', $extdebue=false, $extcore=false, $extlang='zh-CN') {
//设置Extjs的基本环境
$this->setExtHome($exthome, $basedir);
$this->setExtLang($extlang);
$this->debugmode = $extdebue;
return TRUE;
}
/**
* Set the language of extjs
*
* @param String $lang The language here can only be the language part of the file name of the language file in ExtJs, such as:
* ext-lang-zh_cn.js language file, as long as zh_cn will do
*/
public function setExtLang($lang='') {
//TODO - 设置extjs的语言
if (!empty($lang))
$this->extlang = $lang;
}
/**
* Get the Styel setting string of the object
*/
public function getExtBaseStyel() {
$tmpstr = '';
if (is_array($this->ExtALLFiels[$this->ExtBaseFile['css']])) {
$cssfile = '';
foreach ($this->ExtALLFiels[$this->ExtBaseFile['css']] as $v) {
if (preg_match('//docs/i', $v) == FALSE) {
$cssfile = $v;
break;
}
}
$tmpstr .= " n";
} else {
$tmpstr .= " n";
}
//设置其它css
if (!empty($this->pageCss)) {
foreach ($this->pageCss as $f) {
if (is_array($f)) {
$tmpstr .= "n";
} else {
$tmpstr .= " n";
}
}
}
return $tmpstr;
}
/**
* Get the basic Script configuration string of the object
* @return String
*/
public function getExtBaseScript() {
$tmp = '';
$tmpstr = '';
if (is_array($this->ExtALLFiels[$this->ExtBaseFile['base']])) {
foreach ($this->ExtALLFiels[$this->ExtBaseFile['base']] as $v) {
if (preg_match('/source/i', $v) == FALSE) {
$tmp = $v;
break;
}
}
if (empty($tmp))
$tmp = $this->ExtALLFiels[$this->ExtBaseFile['base']][0];
}else {
$tmp = $this->ExtALLFiels[$this->ExtBaseFile['base']];
}
$tmpstr .= "n";
if ($this->debugmode) {
$tmpstr .= "n";
} else {
$tmpstr .= "n";
}
if ($this->coremode) {
$tmpstr .= "n";
}
//设置语言
$ExtLangJS = 'ext-lang-{lang}.js';
if (!empty($this->extlang)) {
$tmpfile = strtolower(str_replace("{lang}", $this->extlang, $ExtLangJS));
if (isset($this->ExtALLFiels[$tmpfile])) {
$tmpstr .= "n";
}
}
//并入其它Js文件
$tmpstr .= $this->getExtPageJs();
return $tmpstr;
}
/**
* Get other settings of ExtJs
* @return String
*/
public function getExtPageJs(){
$tmpstr = "";
//设置其它js
if (!empty($this->pageJs)) {
foreach ($this->pageJs as $f) {
if (is_array($f)) {
$tmpstr .= "n";
} else {
$tmpstr .= "n";
}
}
}
return $tmpstr;
}
/**
* Get the basic page configuration string of ExtJs
* @return string
*/
public function getExtBaseJs() {
//s.gif
$tmpstr = '';
$tmpstr .= "n";
if (!empty($this->extcss) && isset($this->ExtALLFiels[$this->extcss])) {
$tmpstr .= "n";
}
return $tmpstr;
}
/**
* Get all configuration strings of ExtJs
* @return String
*/
public function getExtBaseCode() {
$this->extbasecode .= $this->getExtBaseStyel();
$this->extbasecode .= $this->getExtBaseScript();
$this->extbasecode .= $this->getExtBaseJs();
return $this->extbasecode;
}
/**
* Set other css files on the page
* @param String Css file name and path
*/
public function setPageCssFile($fileName) {
if (!empty($fileName)) {
$this->pageCss[] = $fileName;
}
}
/**
* Set the style of the page
* @param $cssString style string
*/
public function setPageCss($cssString) {
if (!empty($cssString)) {
$this->pageCss[] = array("sytle" => $cssString);
}
}
/**
* Set other js files on the page
* @param String JS file name and path
*/
public function setPageJsFile($fileName) {
if (!empty($fileName)) {
$this->pageJs[] = $fileName;
}
}
/**
* Set the JS code of the page
* @param $Js can be an ExtFunction object or a js string
*/
public function setPageJs($Js) {
if (!empty($Js)) {
$this->pageJs[] = array("js" => $Js);
}
}
/**
* Set the style of extjs
*
* @param String $cssName css style name The default is default
*/
public function setExtCss($cssName="default") {
if ($cssName != "default") {
$this->extcss = "xtheme-" . $cssName . ".css";
}
}
/**
* Format $data into ExtJs object Json string
*
* @param Array $data
* @return String
*/
public function ExtJsonFormat($data) {
$i = 0;
$retstr .= "{";
foreach ($data as $k => $v) {
if ($i > 0)
$retstr .= ",";
if (is_string($v) && !is_numeric($v) && strtolower($v) != "true" && strtolower($v) != "false") {
$retstr .= "$k:'$v'";
}
else
$retstr .= "$k:$v";
$i++;
}
$retstr .= "}";
return $retstr;
}
/**
* Read all contents in the pointed folder $floder (including all contents in files, folders and subfolders)
*
* @param String $floder The folder name (directory name) can be a relative directory
* @ param Array POT $retarr Array pointer where the content is stored
*/
public function ReadALLFile($floder, &$retarr = array()) {
//TODO - Read all contents in the specified folder $floder (including files and files folder, the contents of subfolders), returned to the $retarr pointer
$tpath = '';
$app_path = str_replace('\', '/', getcwd()) . "/";
//echo "APP_PATH:".$app_path." BASE:".$this->extbasedir." n";
if (strpos($this->extbasedir, $app_path) == FALSE) {
$ tpath = $app_path . "/" . $floder;
} else {
$tpath = $this->extbasedir . "/" . $floder;
}
$tpath = preg_replace(array('/{2,} /', '//{2,}/'), '/', $tpath);
$tmparr = $this->ReadFloder($tpath);
if ($tmparr != FALSE && is_array($ tmparr)) {
foreach ($tmparr[0] as $v) {
$this->ReadALLFile($floder . '/' . $v, $retarr);
}
if (!empty($tmparr[ 1])) {
foreach ($tmparr[1] as $v) {
$k = strtolower($v);
if (isset($retarr[$k])) {
$tmpstr = preg_replace('/ /{2,}/', "/", $floder . '/' . $v);
if (is_array($retarr[$k])) {
$retarr[$k][] = $tmpstr;
} else {
$retarr[$k] = array($retarr[$k], $tmpstr);
}
} else {
$retarr[$k] = preg_replace('//{2,}/' , "/", $floder . '/' . $v);
}
}
}
}
array_change_key_case($retarr);
}
/**
* Read the contents (including files and folders) of the specified folder $floder
*
* @param String $floder
* @return Array
*/
public function ReadFloder($floder ) {
//TODO - Read the contents of the specified folder $floder (including files and folders)
if (!is_dir($floder)) {
throw new ThinkException("Cannot set the operating environment of ExtJs, Please check the set directory: $floder");
}
$flod = array();
$files = array();
$dh = opendir($floder);
if (!$dh) {
throw new ThinkException("Open directory:" . dirname("../") . " Error! ");
}
while (false !== ($filename = readdir($dh))) {
if ($filename != "." && $filename != "..") {
if (strpos( $filename, ".") <= 0) $flod[] = $filename; else $files[] = $filename; } } return array($flod, $files); } /** * Set the properties of the object * @param String $key * @param Mixed $val */ public function __set($key, $val) { if (property_exists($this, $key)) { if ($key == "extlang") { $this-> ;setExtLang($val); } else { $this->$key = $val;
}
}
}
/**
* Get the object attribute value
* @param String $key
* @return Mixed
*/
public function __get($key) {
if ( empty($key))
return false;
if (property_exists($this, $key)) {
if ($key == "extbasecode")
return $this->getExtBaseCode();
else
return $ this->$key;
}
return true;
}
/**
* Return the object as String
* @return String
*/
public function __toString() {
return $this->getExtBaseCode();
}
}
?> ;
Copy code
/** * PHPExtJs object generation class * @License: ( http://www.apache.org/licenses/LICENSE-2.0 ) * @Author: wb
*/
class ExtFunction {
/**
* Object parameter set
* @var Array parameter set
*/
protected $param = array();
/**
* Object code
* @var String Object code string
*/
protected $code = '';
/**
* The name of JS object notation
* @var String Object name
*/
protected $clsname = '';
/**
* Set the Ext function object according to the parameter $param, code $code and $clsnames
*
* @param Mixed $param function parameter list such as: "val, val1" or array("val", "val1")
* @param Mixed $code functiond code, you can follow the object
* @param String $clsname Ext custom object name
*
*/
public function __construct($param = null, $code = null, $clsname = null) {
$this->SetParam($param);
$this->SetCode($code);
$this->clsname = $clsname;
}
/**
* Set the parameters of the object
* @param String $param parameter can be an array
*/
public function SetParam($param) {
if (is_array($param)) {
$this->param = array_merge($this->param, $param);
} elseif (is_string($param) && preg_match("/,/", $param)) {
$this->param = array_merge($this->param, split(',', $param));
} else {
$this->param [$param] = $param;
}
}
/**
* Code to set the object
* @param Mixed $code can be a code string or other object of PHPExtJS
*/
public function SetCode($code) {
if (!empty($this->code) && is_object($this->code) && method_exists($this->code, 'render')) {
$this->code = $this->code->render();
}
if (is_object($code) && method_exists($code, 'render')) {
$this->code .= $code->render();
} else if (is_string($code)) {
$this->code .= $code;
}
if (is_array($code)) {
foreach ($code as $key => $val) {
if ($key === "return") {
//echo "KEY:$key n";
$this->code .= "return ";
}
$this->SetCode($val);
$this->code .= ";";
}
}
}
/**
* @param String $name DOM name
* @param String $clsname Object name
*/
public function render($name = '', $clsname = "") {
$str = '';
if (!empty($name)) {
$str .= "var $name = function ";
} else {
$str .= "function ";
}
if (!empty($clsname))
$this->clsname = $clsname;
if (!empty($this->clsname)) {
$str .= " " . $this->clsname . " ";
$this->param = array();
}
$str .= "(" . implode(',', $this->param) . ")";
if (!empty($this->code)) {
$str .= "{";
if (is_object($this->code) && method_exists($this->code, "render")) {
$str .= $this->code->render();
} elseif (is_string($this->code)) {
$str .= $this->code;
}
$str .= "}";
}
if (!empty($name))
$str .= ";";
//去除注释行
$search = array(
'/(//.*)|(/*.**/)/i', //去掉注释
'/[fnrt]*/i', //去掉回车符
'/{(s)*/i',
'/}(s)*}/i',
'/}(s)*/i',
//'/}(s)*if/i',
'/(s)*}/',
'/;(s)*/',
'/,(s)*/i'
);
$replace = array(
'',
'',
'{',
'}}',
'}',
//'}if',
'}',
';',
','
);
$str = preg_replace($search, $replace, $str);
return $str;
}
public function __toString() {
return $this->render();
}
}
?>
Copy code
require_once 'ExtData.class.php'; class ExtObject { protected static $indent = ''; public $state = Array(); public $showkeys = true; public $extClass = ''; public $rendername = ''; public $extend = ''; /** * Create an Ext object based on the $properties property * * @param String $ExtClass Object name such as: Ext.TabPanel, Ext.grid.GridPanel, etc. * @param Array $properties Object property array such as: * Array('labelWidth ' => 150,
* 'url' => 'part.submit.php',
* 'frame' => true,
* 'bodyStyle' => 'padding: 5px 5px 0',
* 'width' => 500,
* 'defaults' => new ExtObject(null, Array('width' => 290)),
* 'defaultType' => 'textfield'
* )
* @ param String $name var name. For example: $name='test', the code generated is var test = new $ExtClass () {}
* @param Boolen $showkeys Whether to display the label of the configuration array $properties
*/
public function __construct($ExtClass = null, $properties = null, $name = null, $showkeys = true) {
$this->extClass = $ExtClass;
if (is_array($properties)) {
$this->state = $properties;
}
$this->showkeys = $showkeys;
$this->rendername = $name;
}
/**
* Set the attributes of the object, that is, $key = $val;
*
* @param String $key attribute name must meet the requirements of ExtJS objects
* @param Anly_type $val
*/
public function __set($key, $val) {
if ($key == 'indent') {
$this->indent = $val;
} else {
$this->state [$key] = $val;
}
}
public function __get($key) {
if (isset($this->state[$key]))
return $this->state [$key];
}
public function __isset($key) {
return isset($this->state [$key]);
}
public function del($key) {
$this->__unset($key);
}
public function __unset($key) {
unset($this->state [$key]);
}
public function __toString() {
return $this->render();
}
/**
* Set the attribute value of attribute $name to $property
*
* @param String $name attribute name
* @param Mixed $property attribute value
*/
public function setProperty($name, $property) {
if (!empty($name)) {
$this->state [$name] = $property;
}
}
/**
* Set the ExtClass property according to the configuration array $properties
*
* @param ConfigArray $properties configuration array
*/
public function setProperties($properties) {
$this->state = array_merge($this->state, $properties);
}
public function setExtendsClass($ExtClass) {
$this->extend = $ExtClass;
}
public function JSRender($items, $showkeys = true, $isparam = false) {
//self::$indent .= ' ';
$str = '';
$total = count($items);
$cnt = 1;
if ($isparam && $total == 2 && is_object($items [0]) && is_array($items [1])) {
$str .= "{{$this->JSRender($items[0])}},";
$str .= "[{$this->JSRender($items[1])}]";
} else {
foreach ($items as $element => $value) {
if ($showkeys) {
if (is_numeric($showkeys)) {
$str .= self::$indent . "'$element':";
} else {
if (!is_numeric($element))
$str .= self::$indent . "$element: ";
}
}
if (is_string($value)) {
$str .= "'$value'";
} else if (is_bool($value)) {
$str .= ( $value) ? "true" : "false";
} else if (is_object($value)) {
if (method_exists($value, 'render')) {
$str .= $value->render();
}
} else if (is_array($value)) {
if (count($value) == 1 && is_string($value [0])) {
$str .= $value [0];
}else {
$str .= "[";
$str .= $this->JSRender($value, false);
$str .= self::$indent . "]";
}
} else if (is_numeric($value)) {
$str .= $value;
} else if ($value == '') {
$str .= "''";
} else {
$str .= $value;
}
if ($cnt != $total) {
$str .= ",";
}
$cnt++;
}
}
self::$indent = substr(self::$indent, 0, - 2);
return $str;
}
/**
* Return the Js code of the constructed ExtJs object
*
* @param String $name
* @return String
*/
public function render($name = null) {
$str = '';
if (!empty($name))
$this->rendername = $name;
if (
preg_match('/.alert/', $this->extClass) || preg_match('/.prompt/', $this->extClass)
|| preg_match('/.show/', $this->extClass) || preg_match('/.confirm/', $this->extClass)
|| preg_match('/.progress/', $this->extClass) || preg_match('/.wait/', $this->extClass)
|| preg_match('/.updateProgress/', $this->extClass)
|| preg_match('/.updateText/', $this->extClass)
) {
if (!empty($this->rendername))
$str = self::$indent . "var $this->rendername = $this->extClass(";
else
$str = self::$indent . "$this->extClass (";
$str .= $this->JSRender($this->state, FALSE);
$str .= ");";
} elseif (
preg_match('/.ColumnModel/', $this->extClass) || preg_match('/.Record.create/', $this->extClass)
) {
if (!empty($this->rendername))
$str = self::$indent . "var $this->rendername = new $this->extClass([";
else
$str = self::$indent . "new $this->extClass ([";
$str .= $this->JSRender($this->state, TRUE);
$str .= "])";
if ($this->rendername) {
$str .= ";";
}
} elseif (
preg_match('/.JsonReader/', $this->extClass) || preg_match('/.ArrayReader/', $this->extClass)
) {
if (!empty($this->rendername))
$str = self::$indent . "var $this->rendername = new $this->extClass(";
else
$str = self::$indent . "new $this->extClass (";
if (!empty($this->state['fields'])) {
$str .= "{totalProperty:'" . $this->state['totalProperty'] . "', ";
$str .= "root:'" . $this->state['root'] . "'},";
$str .= "[" . $this->JSRender($this->state['fields'], TRUE) . "]";
} else {
$str .= $this->JSRender($this->state, TRUE);
}
$str .= ")";
if ($this->rendername) {
$str .= ";";
}
} elseif ($this->extend) { //如果是扩展对象
$str = self::$indent . $this->extClass . " = Ext.extend( $this->extend ,{";
$str .= $this->JSRender($this->state, TRUE);
$str .= "});";
} else {
if ($this->rendername) {
if ($this->extClass) {
$str = self::$indent . "var $this->rendername = new $this->extClass({";
} else {
$str = self::$indent . "var $this->rendername = {";
}
} elseif ($this->extClass) {
echo self::$indent;
$str = self::$indent . "new $this->extClass({";
} else {
$str = self::$indent . "{";
}
$str .= $this->JSRender($this->state, $this->showkeys);
$str .= self::$indent . "}";
if ($this->extClass) {
$str .= ")";
}
if ($this->rendername) {
$str .= ";";
}
}
return $str;
}
}
?>
复制代码
/** * PHPExtJs ExtJs page object * @License: ( http://www.apache.org/licenses/LICENSE-2.0 ) * @Author: wb
*/
class ExtPage {
public $extjs = '';
public $extbase = '';
public $body = '';
public $bodyPapm = '';
public $title = '';
public $charset = '';
public $template = "";
/**
* Output extjshtml code according to the page template
* The template can include {charset}, {title}, {extbase}, {extjs}, {body}
*
* @param String $title Page title
* @param String $ extjs extjs code
* @param String $body page body
* @param String $charset page encoding setting, the default is UTF-8
* @param String $template page template
*/
public function __construct($title='', $extjs='', $extbase='', $body='', $charset='utf-8', $template='') {
$this->title = $title;
$this->extjs = $extjs;
$this->extbase = $extbase;
$this->body = $body;
$this->charset = $charset;
if(!empty($template)) $this->template = $template;
else $this->template = "
{title}
{extbase}
{extjs}
{body}
";
}
public function render() {
if(!empty($this->template)){
$search = array("{charset}","{title}","{extbase}","{extjs}","{body}","{bodyPapm}");
$replace = array($this->charset,$this->title,$this->extbase,$this->extjs,$this->body,$this->bodyPapm);
$this->template = str_replace($search,$replace,$this->template);
echo $this->template;
}else{
throw new Exception("页面模板为空,请先设置页面模板!");
}
}
public function __set($key, $val) {
switch($key) {
case 'extjs':
$this->extjs = $val;
break;
case 'body':
$this->body = $val;
break;
case 'bodyPapm':
$this->bodyPapm = $val;
break;
case 'charset':
$this->body = $val;
break;
case 'template':
$this->template = $val;
break;
case 'extbase':
$this->extbase = $val;
break;
default:
throw new Exception("非法的ExtPage属性 ExtPage::$key");
}
}
public function __get($key) {
switch($key) {
case 'extjs':
return $this->extjs;
case 'body':
return $this->body;
case 'bodyPapm':
return $this->bodyPapm;
case 'charset':
return $this->charset;
case 'template':
return $this->template;
default:
throw new Exception("非法的ExtPage属性 ExtPage::$key");
}
}
}
复制代码
vendor("com.qldx.ext.*"); class FormWin extends Form { /** * Object for loading initial data in the form * @var ExtObject */ public $formLoad = null; /** * The object from which the form reads data * @var ExtObject */ public $formreader = null; /** * Parameters passed when loading data * @var Mixed */ public $formLoadParam = null; /** * Window Object * @var ExtObject */ public $windolg = null; /** * Form field set * @var Array */ public $fieldset = array(); /** * Code to initialize the form object * @var String */ public $initcorde = ''; /** * The form does not contain buttons. The default is false, which means it contains buttons. * @var Bloon */ public $noButton = false; /** * Construct the form * @param String $formName form name * @param String $ModelName form associated data table model name * @param Mixed $dataId form associated data ID * @param Array $Properties window Body attribute array */ public function __construct($formName = '', $ModelName = "", $dataId = "", $Properties = array()) { parent::__construct($formName, $ModelName, $dataId, $Properties); $this->formbody->setProperty("labelWidth", 80);
$this->formbody->setProperty("defaults", array("{xtype:'textfield',anchor:'100%'}"));
$this->windolg = new ExtObject("FormWin",
array(
'id' => $this->formName,
'name' => $this->formName,
'dataID' => $this->dataId,
'title' => $this->formbody->title,
'collapsible' => true,
'maximizable' => true,
'layout' => 'fit',
'plain' => true,
'bodyStyle' => 'padding:5px;',
'buttonAlign' => 'center',
"msk" => array("new Ext.LoadMask(Ext.getBody(), {msg : '正加载数据,请稍等...'})"),
"createFormPanel" => null,
"initComponent" => null
)
);
$this->initcorde = new ExtFunction(NULL, "
this.keys={
key: Ext.EventObject.ENTER,
fn: this.save,
scope: this
};
FormWin.superclass.initComponent.call(this);
this.fp=this.createFormPanel();
this.add(this.fp);
if(!this.dataID && this.loadParam.id){
this.dataID = this.loadParam.id
}
");
}
/**
* Set the default initialization code of the form
* @param Mixed $code code string or ExtObject object
*/
public function setFormInitCode($code) {
$this->initcorde->SetCode($code);
}
/**
* Set the form loading event. Note: Add the default Loader when $obj is empty. If you want to pass other parameters, you must
* First set other objects during loading through the setFormLoaderParam method
* @param ExtObject $obj form's Loader object
*/
public function setFormLoader($obj = null) {
$tobj = null;
$param = null;
if ($this->dataId) {
$param = new ExtObject(null, array('id' => $this->dataId));
} else {
$param = new ExtObject(null, array('id' => array('this.dataID')));
}
if (!empty($obj) && is_object($obj)) {
if (!isset($obj->param) || empty($obj->param)) {
$this->setProperty("loadParam", $obj->param);
$this->del('param');
} else { //如果加载的对象不含param则并入预先设置的loadParam
$this->setProperty("loadParam", $param);
}
$obj->param = array("this.loadParam");
$tobj = $obj;
}else {
$this->setProperty("loadParam", $param);
$tobj = new ExtObject(null, array(
"url" => __URL__ . "/getFormWinData",
"params" => array('this.loadParam'),
"success" => new ExtFunction(Null, "
this.msk.hide();
"),
"scope" => array('this')
));
}
$this->formLoad = $tobj;
}
/**
* Set the properties of the form's data loading Loader object
* @param String $attrib
* @param Mixed $value
*/
public function setFormLoaderProperty($attrib, $value) {
$this->formLoad->setProperty($attrib, $value);
}
/**
* Set additional parameters of the form loading object
* @param String $param parameter name
* @param Mixed $value parameter value
*/
public function setFormLoaderParam($param, $value) {
$this->formLoadParam->setProperty($param, $value);
}
/**
* Set the attributes of the window container
* @param String $attrib
* @param Mixed $value
*/
public function setWindowsProperty($attrib, $value) {
$this->windolg->setProperty($attrib, $value);
}
/**
* Set the form reader data identifier form reader
*/
private function setFormReader() {
$this->formreader = new ExtObject(
'Ext.data.JsonReader',
array(
new ExtObject(
null,
array("root" => "data")
),
$this->fieldset
)
);
}
private function setFormInt() {
$twidth = 0;
$tmpwidth = 16;
$tmpheight = 40;
//form窗体的读取数据的标志 字段名称列表
foreach ($this->formFields as $n => $f) {
$this->fieldset[] = new ExtObject(null, array('name' => $n, 'mapping' => $n));
}
//并且计算窗体的高度
if (empty($this->windolg->height)) {
foreach ($this->formFields as $n => $f) {
if (isset($f->height) && $f->height > 0) {
$tmpheight += $f->height;
} else {
$tmpheight += 32;
}
if (isset($f->width) && $f->width > $tmpwidth) {
$twidth = $f->width;
}
}
} else {
$tmpheight = $this->windolg->height;
$twidth = $this->windolg->width;
}
if (empty($tmpheight)) {
$tmpheight = 200;
} elseif ($tmpheight > 750) {
$tmpheight = 750;
}
if (empty($twidth)) {
$tmpwidth += $twidth;
}
if (empty($tmpwidth) || $tmpwidth == 16) {
$tmpwidth = 340;
}
$this->windolg->setProperty("width", $tmpwidth);
$this->windolg->setProperty("height", $tmpheight);
$this->windolg->setProperty("minWidth", $tmpwidth);
$this->windolg->setProperty("minHeight", $tmpheight);
}
/**
* Add the default add button of the form
* @param String $name Default is: save
* @param String $title Default is: save
* @param ExtFunction $hander Default event response object
*/
public function addSaveButton($name = 'save', $title='保存', $hander=null) {
if (empty($hander)) {
$hander = new ExtFunction(null, "
if(this.fp.form.isValid()){
var turl = '" . __URL__ . "/saveFormWinData';
if(this.dataID){
turl += '/id/'+ this.dataID;
}
var fw = this;
this.fp.form.submit({
waitTitle: 'Please wait',
waitMsg : 'Processing request...',
url : turl,
params: this.loadParam,
success : function(form, action){
fw.close();
if(form.rGrid){
if(form.rGrid.root){
form.rGrid.getLoader().load(form.rGrid.root );
}else{
form.rGrid.getLoader().load();
}
}
},
failure : function() {
fw.close;
Ext.Msg.alert('System Error', 'An error occurred on the server, please try again later! ');
}
});
}
");
}
$this->addButton($name, $title);
$this->setButtonAttrib( $name, 'handler', $hander);
}
/**
* Add default cancel button
* @param String $name
* @param String $title
* @param ExtFunction $hander
*/
public function addCancelButton($name = 'cancle', $title='Cancel', $hander=null) {
if (empty($hander)) {
$hander = new ExtFunction(null, "
this.close();
");
}
$this->addButton($name, $title);
$this- >setButtonAttrib($name, 'handler', $hander);
}
/**
* Set FormWin's data model according to the model object name
*
* @param String $modelName model object name
* @param Mixed $id to be edited to the record number
*/
public function setDataModel($modelObject, $id) {
$this->setDataSource($modelObject, $id);
}
/**
* Set the FormWin data model according to the model object name
* @param String $modelName model object name
*/
public function setDataModelByName($modelName) {
if (!empty($modelName)) {
$model = D($modelName);
$this- >setDataModel($model);
}
}
/**
* This method returns the JS string of this object
* @return String The JS string of this object
*/
public function getJavascript() {
$this->initForm();
$this->setFormInt();
$this->setFormReader();
$this->setFormLoader();
//Data loading object incorporated into the form
$this->setFormInitCode("
this.fp.load(" . $this ->formLoad->render() . ");
");
//Set the basic properties of the form
if (empty($this->formbody->baseCls)) {
$this-> formbody->setproperty('baseCls', 'x-plain');
}
if (empty($this->formbody->reader)) {
$this->formbody->setProperty(" reader", $this->formreader);
}
$this->formbody->setProperty("items", $this->getElementArray());
//Create window
$this-> windolg->setProperty(
"createFormPanel",
new ExtFunction(null,
array("return" => $this->formbody->render())
)
);
//Add button
if (!$this->noButton) {
if (!empty($this->formButtons) && is_array($this->formButtons)) {
foreach ($this->formButtons as $k => ; $v) {
$this->initcorde->SetCode("this.addButton('" . $v->text . "',this." . $k . ",this);");
$this->windolg->setProperty($k, $v->handler);
}
} else {
$this->initcorde->SetCode("
this.addButton('Save' ,this.save,this);
this.addButton('Cancel', function(){this.close();},this);
");
}
}
$this->windolg-> setProperty("initComponent", $this->initcorde);
$this->windolg->setExtendsClass("Ext.Window");
return $this->formExtendJs . $this->windolg-> ;render();
}
}
?>
Copy code