+ add for editor inline.

This commit is contained in:
wangyidong
2011-06-29 10:36:24 +00:00
parent 2b8219b5b7
commit 1479d9cbc7
5 changed files with 433 additions and 48 deletions
+36 -10
View File
@@ -17,21 +17,45 @@ class editor extends control
* @access public
* @return void
*/
public function index($editFileName = '', $action = '')
public function index($filePath = '', $action = '', $isExtends = '')
{
$allModules = $this->editor->getModuleFiles();
$this->view->tree = $this->editor->printTree($allModules);
$editFileName = helper::safe64Decode($editFileName);
$this->view->tree = $this->editor->printTree($allModules);
$this->view->safeFilePath = $filePath;
$filePath = helper::safe64Decode($filePath);
$fileContent = '';
if($editFileName and file_exists($editFileName))
if($filePath)
{
if($action == 'edit')
if($action == 'extendOther' and file_exists($filePath))
{
$fileContent = file_get_contents($editFileName);
$this->view->showContent = htmlspecialchars(file_get_contents($filePath));
}
elseif($action == 'edit' or $action == 'override')
{
if(file_exists($filePath))
{
$fileContent = file_get_contents($filePath);
}
else
{
$filePath = '';
}
}
elseif($action == 'extendModel')
{
$fileContent = $this->editor->extendModel($filePath);
}
elseif($action == 'extendControl')
{
$okUrl = $this->editor->getExtendLink($filePath, 'extendControl', 'yes');
$cancelUrl = $this->editor->getExtendLink($filePath, 'extendControl', 'no');
if(!$isExtends) die(js::confirm($this->lang->editor->extendConfirm, $okUrl, $cencelUrl));
$fileContent = $this->editor->extendControl($filePath);
}
}
$this->view->fileContent = $fileContent;
$this->view->filePath = empty($fileContent) ? '' : $editFileName;
$this->view->filePath = $filePath;
$this->view->action = $action;
$this->display();
}
@@ -42,13 +66,15 @@ class editor extends control
* @access public
* @return void
*/
public function save($filePath = '')
public function save($filePath = '', $action = '')
{
if($filePath and $_POST)
{
$filePath = helper::safe64Decode($filePath);
if($action != 'edit') $filePath = $this->editor->getSavePath($filePath, $action);
if($action != 'edit' and file_exists($filePath) and !$this->post->override) die(js::error($this->lang->editor->repeatFile));
$this->editor->save($filePath);
die(js::reload('parent'));
die(js::locate(inlink('index', "filePath=" . helper::safe64Encode($filePath) . "&action=edit"), 'parent'));
}
}
@@ -68,7 +94,7 @@ class editor extends control
}
$filePath = helper::safe64Decode($filePath);
if(file_exists($filePath) and unlink($filePath)) die(js::reload('parent'));
die(js::alert($this->lang->editor->noDelete));
die(js::alert($this->lang->editor->notDelete));
}
}
+27
View File
@@ -0,0 +1,27 @@
<?php
$lang->editor->newMethod = 'New method';
$lang->editor->extend = 'Extend';
$lang->editor->newLang = 'New language';
$lang->editor->newConfig = 'New config';
$lang->editor->newHook = 'New hook';
$lang->editor->newExtend = 'Nwe extend';
$lang->editor->cover = 'Cover';
$lang->editor->edit = 'Edit extend';
$lang->editor->filePath = "File Path:";
$lang->editor->fileName = "Extend file name:";
$lang->editor->fileContent = "File content";
$lang->editor->overrideFile = "Override File";
$lang->editor->isOverride = "Is override";
$lang->editor->exampleHook = "For example:***.html.hook.php";
$lang->editor->exampleJs = "For example:***.js";
$lang->editor->exampleCss = "For example:***.css";
$lang->editor->examplePHP = "For example:***.php";
$lang->editor->deleteConfirm = 'Are you sure?';
$lang->editor->extendConfirm = 'Would you reuse the code.';
$lang->editor->repeatFile = 'File name repeat';
$lang->editor->notWritable = "Can't writable, maybe no power. please try chmod 777 -R ";
$lang->editor->notDelete = "Can't delete, please check power!";
$lang->editor->emptyFileName = 'Please write file name';
+27
View File
@@ -0,0 +1,27 @@
<?php
$lang->editor->newMethod = '新增方法';
$lang->editor->extend = '扩展';
$lang->editor->newLang = '新增语言';
$lang->editor->newConfig = '新增配置';
$lang->editor->newHook = '新增钩子';
$lang->editor->newExtend = '新增扩展';
$lang->editor->override = '覆盖';
$lang->editor->edit = '编辑扩展';
$lang->editor->filePath = "文件路径:";
$lang->editor->fileName = "扩展文件名:";
$lang->editor->fileContent = "文件内容:";
$lang->editor->overrideFile = "覆盖文件";
$lang->editor->isOverride = "是否覆盖";
$lang->editor->exampleHook = "例如:***.html.hook.php";
$lang->editor->exampleJs = "例如:***.js";
$lang->editor->exampleCss = "例如:***.css";
$lang->editor->examplePHP = "例如:***.php";
$lang->editor->deleteConfirm = '是否要删除?';
$lang->editor->extendConfirm = '是否要重用原来代码?';
$lang->editor->repeatFile = '文件名重复';
$lang->editor->notWritable = "无法写入,可能没有权限。请尝试执行 chmod 777 -R ";
$lang->editor->notDelete = '无法删除,请检查权限!';
$lang->editor->emptyFileName = '请写入一个文件名!';
+277 -31
View File
@@ -14,7 +14,6 @@ class editorModel extends model
/**
* Get module files, contain control's methods and model's method but except ext.
*
* @param string $moduleRoot
* @access public
* @return array
*/
@@ -42,7 +41,8 @@ class editorModel extends model
}
elseif(is_dir($moduleFullFile))
{
foreach(glob($moduleFullFile . '/' . '*.php') as $fileName) $allModules[$moduleFullDir][$moduleFullFile][$fileName] = basename($fileName);
$ext = ($moduleFile == 'js' or $moduleFile == 'css') ? $moduleFile : 'php';
foreach(glob($moduleFullFile . '/' . "*.$ext") as $fileName) $allModules[$moduleFullDir][$moduleFullFile][$fileName] = basename($fileName);
}
else
{
@@ -74,23 +74,8 @@ class editorModel extends model
/* extend of lang is more a grade of directroy. */
if($extensionDir == 'lang' or $extensionDir == 'js' or $extensionDir == 'css')
{
$langDirs = scandir($extensionFullDir);
foreach($langDirs as $langDir)
{
if($langDir == '.' or $langDir == '..' or $langDir == '.svn') continue;
$langFullDir = $extensionFullDir . '/' . $langDir;
$extensionList[$extensionFullDir][$langFullDir] = array();
if(is_dir($langFullDir))
{
$langFiles = scandir($langFullDir);
foreach($langFiles as $langFile)
{
if($langFile == '.' or $langFile == '..' or $langFile == '.svn') continue;
$langFullFile = $langFullDir . '/' . $langFile;
$extensionList[$extensionFullDir][$langFullDir][$langFullFile] = $langFile;
}
}
}
$extensionList[$extensionFullDir] = $this->getTwoGradeFiles($extensionFullDir);
continue;
}
$extensionFiles = scandir($extensionFullDir);
@@ -105,6 +90,36 @@ class editorModel extends model
return $extensionList;
}
/**
* if a directory has two grage, this method will get files
*
* @param string $extensionFullDir
* @access public
* @return string
*/
public function getTwoGradeFiles($extensionFullDir)
{
$fileList = array();
$langDirs = scandir($extensionFullDir);
foreach($langDirs as $langDir)
{
if($langDir == '.' or $langDir == '..' or $langDir == '.svn') continue;
$langFullDir = $extensionFullDir . '/' . $langDir;
$fileList[$langFullDir] = array();
if(is_dir($langFullDir))
{
$langFiles = scandir($langFullDir);
foreach($langFiles as $langFile)
{
if($langFile == '.' or $langFile == '..' or $langFile == '.svn') continue;
$langFullFile = $langFullDir . '/' . $langFile;
$fileList[$langFullDir][$langFullFile] = $langFile;
}
}
}
return $fileList;
}
/**
* Analysis methods of control and model.
*
@@ -125,7 +140,7 @@ class editorModel extends model
{
$methodName = $method->name;
if($method->getFileName() != $fileName) continue;
$classMethod[$methodName] = $methodName;
$classMethod[$fileName . '/' . $methodName] = $methodName;
}
return $classMethod;
}
@@ -146,19 +161,12 @@ class editorModel extends model
$tree .= "<li>\n";
if(is_array($file))
{
$tree .= basename($key) . "\n";
$tree .= $this->addLink4Dir($key);
$tree .= $this->printTree($file, false);
}
else
{
if(strpos($key, '/ext/') !== false)
{
$tree .= $file . html::a(inlink('index', "editFileName=" . helper::safe64Encode($key) . "&action=edit"), $this->lang->edit) . html::a(inlink('delete', 'path=' . helper::safe64Encode($key)), $this->lang->delete, 'hiddenwin') . "\n";
}
else
{
$tree .= $file . "\n";
}
$tree .= $this->addLink4File($key, $file);
}
$tree .= "</li>\n";
}
@@ -166,6 +174,85 @@ class editorModel extends model
return $tree;
}
/**
* Add link for directory or has children grade
*
* @param string $filePath
* @access public
* @return string
*/
public function addLink4Dir($filePath)
{
$tree = '';
$fileName = basename($filePath);
if(strpos($filePath, '/ext/') !== false)
{
if($fileName == 'lang' or $fileName == 'js' or $fileName == 'css')
{
$tree .= $fileName;
}
else
{
$tree .= $fileName . html::a($this->getExtendLink($filePath, "newExtend"), $this->lang->editor->newExtend);
}
}
elseif($fileName == 'model.php' or $fileName == 'control.php')
{
$tree .= $fileName . html::a($this->getExtendLink($filePath, 'newMethod'), $this->lang->editor->newMethod);
}
else
{
$tree .= $fileName;
}
return $tree;
}
/**
* Add link for file
*
* @param string $filePath
* @param string $file
* @access public
* @return string
*/
public function addLink4File($filePath, $file)
{
$tree = '';
if(strpos($filePath, '/ext/') !== false)
{
$tree .= $file . html::a($this->getExtendLink($filePath, "edit"), $this->lang->edit) . html::a(inlink('delete', 'path=' . helper::safe64Encode($filePath)), $this->lang->delete, 'hiddenwin') . "\n";
}
elseif(basename(dirname($filePath))== 'view')
{
$tree .= $file . html::a($this->getExtendLink($filePath, "override"), $this->lang->editor->override) . html::a($this->getExtendLink($filePath, "newHook"), $this->lang->editor->newHook) . "\n";
}
else
{
$parentDir = basename(dirname($filePath));
$action = 'extendOther';
if($parentDir == 'control.php') $action = 'extendControl';
if($parentDir == 'model.php') $action = 'extendModel';
$tree .= $file . html::a($this->getExtendLink($filePath, $action), $this->lang->editor->extend);
if($parentDir == 'lang') $tree .= html::a($this->getExtendLink($filePath, "new" . str_replace('-', '_', basename($filePath, '.php'))), $this->lang->editor->newLang);
if(basename($filePath) == 'config.php') $tree .= html::a($this->getExtendLink($filePath, "newConfig"), $this->lang->editor->newConfig);
}
return $tree;
}
/**
* Get extend link
*
* @param string $filePath
* @param string $action
* @param string $isExtends
* @access public
* @return string
*/
public function getExtendLink($filePath, $action, $isExtends = '')
{
return inlink('index', "filePath=" . helper::safe64Encode($filePath) . "&action=$action&isExtends=$isExtends");
}
/**
* Save file to extension.
*
@@ -177,13 +264,172 @@ class editorModel extends model
{
$fileContent = $this->post->fileContent;
if(get_magic_quotes_gpc()) $fileContent = stripslashes($fileContent);
if(is_writable($filePath))
$dirPath = dirname($filePath);
if(!is_dir($dirPath) and is_writable(dirname($dirPath))) mkdir($dirPath, 0777, true);
if(is_writable($dirPath))
{
file_put_contents($filePath, $fileContent);
}
else
{
die(js::alert($this->lang->editor->noWritable));
$extFilePath = substr($filePath, 0, strpos($filePath, '/ext/') + 4);
die(js::alert($this->lang->editor->notWritable . $extFilePath));
}
}
/**
* Extend model.php and get file content.
*
* @param string $filePath
* @access public
* @return string
*/
public function extendModel($filePath)
{
$className = basename(dirname(dirname($filePath)));
$methodName = basename($filePath);
$methodParam = $this->getParam($className, $methodName, 'Model');
return $fileContent = <<<EOD
<?php
public function $methodName($methodParam)
{
return parent::$methodName($methodParam);
}
EOD;
}
/**
* Extend control.php and get file content.
*
* @param string $filePath
* @access public
* @return string
*/
public function extendControl($filePath)
{
$className = basename(dirname(dirname($filePath)));
$methodName = basename($filePath);
if($isExtends == 'yes')
{
$methodParam = $this->getParam($className, $methodName);
return $fileContent = <<<EOD
include '../../control.php';
class my$className extends $className
{
public function $methodName($methodParam)
{
return parent::$methodName($methodParam);
}
}
EOD;
}
else
{
$methodCode = $this->getMethodCode($className, $methodName);
return $fileContent = <<<EOD
class $className extends control
{
$methodCode
}
EOD;
}
}
/**
* Get method's parameters.
*
* @param string $className
* @param string $methodName
* @param string $ext
* @access public
* @return string
*/
public function getParam($className, $methodName, $ext = '')
{
$method = new ReflectionMethod($className . $ext, $methodName);
$methodParam = '';
foreach ($method->getParameters() as $param)
{
$methodParam .= '$' . $param->getName();
if($param->isOptional())
{
$defaultParam = $param->getDefaultValue();
if(is_string($defaultParam)) $methodParam .= "='$defaultParam', ";
else $methodParam .= "=$defaultParam, ";
}
else
{
$methodParam .= ', ';
}
}
$methodParam = rtrim($methodParam, ', ');
return $methodParam;
}
/**
* Get method code.
*
* @param string $className
* @param string $methodName
* @param string $ext value may be Model
* @access public
* @return string
*/
public function getMethodCode($className, $methodName, $ext = '')
{
$method = new ReflectionMethod($className . $ext, $methodName);
$fileName = $method->getFileName();
$startLine = $method->getStartLine();
$endLine = $method->getEndLine();
$file = file($fileName);
$code = '';
for($i = $startLine - 1; $i <= $endLine; $i++)
{
$code .= $file[$i];
}
return $code;
}
/**
* Get save path.
*
* @param string $filePath
* @param string $action
* @access public
* @return string
*/
public function getSavePath($filePath, $action)
{
$fileName = empty($_POST['fileName']) ? '' : trim($this->post->fileName);
$moduleName = strstr($filePath, '/module/');
$moduleName = substr($moduleName, 0, strpos($moduleName, '/', 9));
$moduleName = basename($moduleName);
$extPath = $this->app->getModuleRoot() . $moduleName . '/ext/';
switch($action)
{
case 'extendModel':
$fileName = empty($fileName) ? basename($filePath) . '.php' : $fileName;
return $extPath . 'model/' . $fileName;
case 'extendControl':
$fileName = basename($filePath) . '.php';
return $extPath . 'control/' . $fileName;
case 'override':
$fileName = basename($filePath);
return $extPath . 'view/' . $fileName;
case 'extendOther':
$editName = basename($filePath);
$fileName = empty($fileName) ? $editName: $fileName;
if($editName == 'config.php') return $extPath . 'config/' . $fileName;
elseif(strpos($editName, '.php') !== false) return $extPath . 'lang/' . str_replace('.php', '', $editName) . '/' . $fileName;
else return $extPath . substr($editName, strrpos($editName, '.') + 1) . '/' . substr($editName, 0, strrpos($editName, '.')) . '/' . $fileName;
default:
if(empty($fileName)) die(js::error($this->lang->editor->emptyFileName));
$action = strtolower(str_replace('new', '', $action));
if($action == 'hook') return $extPath . 'view/' . $fileName;
elseif($action == 'method') return $extPath . basename($filePath, '.php') . '/' . $fileName;
elseif($action == 'extend') return $filePath . '/' . $fileName;
elseif($action == 'config') return $extPath . 'config/' . $fileName;
else return $extPath . 'lang/' . str_replace('_', '-', $action) . '/' . $fileName;
}
}
}
+66 -7
View File
@@ -14,17 +14,76 @@
<?php include '../../common/view/treeview.html.php';?>
<table class='table-1'>
<tr>
<td width='300'>
<td width='300' valign='top'>
<?php echo $tree?>
<br />
</td>
<td valign='top'>
<span><?php if($filePath) printf($lang->editor->editFile,$filePath)?></span>
<form method='post' target='hiddenwin' action='<?php echo inlink('save', "filePath=" . helper::safe64Encode($filePath))?>'>
<?php echo html::textarea('fileContent', $fileContent, "class='area-1' rows=40")?>
<br/>
<?php echo html::submitButton()?>
</form>
<form method='post' target='hiddenwin' action='<?php echo inlink('save', "filePath=$safeFilePath&action=$action")?>'>
<table class='table-1'>
<caption>
<?php
if(empty($action)) echo '';
else echo isset($lang->editor->$action) ? $lang->editor->$action : $lang->editor->extend;
?>
</caption>
<?php if($filePath):?>
<tr>
<td><?php echo $lang->editor->filePath?></td>
<td><?php echo $filePath?></td>
</tr>
<?php endif?>
<tr>
<td class='w-80px'><?php echo $lang->editor->fileContent?></td>
<td>
<?php if(isset($showContent))
{
echo "<pre>$showContent</pre>";
$rows = 20;
}
else
{
$rows = 40;
}
?>
<?php echo html::textarea('fileContent', $fileContent, "class='area-1' rows=$rows")?>
</td>
</tr>
<?php if($action and $action != 'edit' and $action != 'extendControl' and $action != 'override'):?>
<tr>
<td><?php echo $lang->editor->fileName?></td>
<td>
<?php
echo html::input('fileName', '', "class=text-5");
if($action == 'newHook')
{
echo $lang->editor->exampleHook;
}
elseif(strpos(basename($filePath), '.js') !== false)
{
echo $lang->editor->exampleJs;
}
elseif(strpos(basename($filePath), '.css') !== false)
{
echo $lang->editor->exampleCss;
}
else
{
echo $lang->editor->examplePHP;
}
?>
</td>
</tr>
<?php endif;?>
<?php if($action != 'edit'):?>
<tr>
<td><?php echo $lang->editor->overrideFile?></td>
<td><input type='checkbox' name='override' id='override' /> <?php echo $lang->editor->isOverride?></td>
</tr>
<?php endif;?>
<tr><td colspan='2' align='center'><?php echo html::submitButton()?><td></tr>
</table>
</form>
</td>
</tr>
</table>