* Rewrite editor-edit page.

This commit is contained in:
wangyidong
2023-11-22 15:56:37 +08:00
parent aac0211340
commit 8a9a7f0b7d
4 changed files with 62 additions and 62 deletions
+9 -35
View File
@@ -66,58 +66,32 @@ class editor extends control
* @access public
* @return void
*/
public function edit($filePath = '', $action = '', $isExtends = '')
public function edit(string $filePath = '', string $action = '', string $isExtends = '')
{
$this->view->safeFilePath = $filePath;
$fileContent = '';
$extension = 'php';
if($filePath)
{
$filePath = realpath(helper::safe64Decode($filePath));
$filePath = helper::safe64Decode($filePath);
if(strpos(strtolower($filePath), strtolower($this->app->getBasePath())) !== 0) return print($this->lang->editor->editFileError);
if($action == 'extendOther' and file_exists($filePath)) $this->view->showContent = file_get_contents($filePath);
if($action == 'edit' or $action == 'override')
if(($action == 'edit' or $action == 'override') && !file_exists($filePath)) $filePath = '';
if($action == 'extendControl' and empty($isExtends))
{
if(file_exists($filePath))
{
$fileContent = file_get_contents($filePath);
if($action == 'override')
{
$fileContent = str_replace("'../../", '$this->app->getModuleRoot() . \'', $fileContent);
$fileContent = str_replace(array('\'./', '"./'), array('\'../../view/', '"../../view'), $fileContent);
}
}
else
{
$filePath = '';
}
}
elseif($action == 'extendModel')
{
$fileContent = $this->editor->extendModel($filePath);
}
elseif($action == 'extendControl')
{
$okUrl = $this->editor->getExtendLink($filePath, 'extendControl', 'yes');
$okUrl = $this->editor->getExtendLink($filePath, 'extendControl', 'yes');
$cancelUrl = $this->editor->getExtendLink($filePath, 'extendControl', 'no');
if(!$isExtends) return print(js::confirm($this->lang->editor->extendConfirm, $okUrl, $cancelUrl));
$fileContent = $this->editor->extendControl($filePath, $isExtends);
}
elseif($action == 'newPage')
{
$fileContent = $this->editor->newControl($filePath);
}
elseif(strrpos(basename($filePath), '.php') !== false and empty($fileContent))
{
$fileContent = "<?php\n";
return print(js::confirm($this->lang->editor->extendConfirm, $okUrl, $cancelUrl));
}
$fileName = basename($filePath);
$fileContent = $this->editorZen->buildContentByAction($filePath, $action, $isExtends);
$fileName = basename($filePath);
if(strpos($fileName, '.') !== false) $extension = substr($fileName, strpos($fileName, '.') + 1);
if(strtolower($action) == 'newjs') $extension = 'js';
if(strtolower($action) == 'newcss') $extension = 'css';
}
$this->view->fileContent = $fileContent;
$this->view->filePath = $filePath;
$this->view->fileExtension = $extension;
+16 -25
View File
@@ -294,7 +294,7 @@ class editorModel extends model
* @access public
* @return string
*/
public function getExtendLink($filePath, $action, $isExtends = '')
public function getExtendLink(string $filePath, string $action, string $isExtends = ''): string
{
return inlink('edit', "filePath=" . helper::safe64Encode($filePath) . "&action=$action&isExtends=$isExtends");
}
@@ -355,7 +355,7 @@ class editorModel extends model
* @access public
* @return string
*/
public function extendModel($filePath)
public function extendModel(string $filePath): string
{
$className = basename(dirname(dirname($filePath)));
if(!class_exists($className)) helper::import(dirname($filePath));
@@ -419,7 +419,7 @@ EOD;
* @access public
* @return string
*/
public function newControl($filePath)
public function newControl(string $filePath): string
{
$className = $this->getClassNameByPath($filePath);
$methodName = basename($filePath, '.php');
@@ -443,34 +443,25 @@ EOD;
* @access public
* @return string
*/
public function getParam($className, $methodName, $ext = '')
public function getParam(string $className, string $methodName, string $ext = ''): string
{
$method = new ReflectionMethod($className . $ext, $methodName);
$methodParam = '';
foreach ($method->getParameters() as $param)
$method = new ReflectionMethod($className . $ext, $methodName);
$methodParams = array();
foreach($method->getParameters() as $param)
{
$methodParam .= '$' . $param->getName();
$methodParam = '$' . $param->getName();
if($param->isOptional())
{
$defaultParam = $param->getDefaultValue();
if(is_string($defaultParam))
{
$methodParam .= "='$defaultParam', ";
}
else
{
if(is_array($defaultParam) and empty($defaultParam)) $defaultParam = 'array()';
if(is_null($defaultParam)) $defaultParam = 'null';
$methodParam .= "=$defaultParam, ";
}
}
else
{
$methodParam .= ', ';
if(is_string($defaultParam)) $defaultParam = "'$defaultParam'";
if(is_array($defaultParam) and empty($defaultParam)) $defaultParam = 'array()';
if(is_null($defaultParam)) $defaultParam = 'null';
$methodParam .= "=$defaultParam";
}
$methodParams[] = $methodParam;
}
$methodParam = rtrim($methodParam, ', ');
return $methodParam;
return implode(', ', $methodParams);
}
/**
@@ -482,7 +473,7 @@ EOD;
* @access public
* @return string
*/
public function getMethodCode($className, $methodName, $ext = '')
public function getMethodCode(string $className, string $methodName, string $ext = ''): string
{
$method = new ReflectionMethod($className . $ext, $methodName);
$fileName = $method->getFileName();
+2 -2
View File
@@ -290,7 +290,7 @@ class editorTest
$modulePath = $this->objectModel->app->getModulePath('', 'todo') . 'control.php';
include $modulePath;
$params = $this->objectModel->getParam('todo', 'create');
return $params == "\$date='today', \$userID='', \$from='todo'" ? 1 : 0;
return $params == "\$date='today', \$from='todo'" ? 1 : 0;
}
/**
@@ -304,7 +304,7 @@ class editorTest
$modulePath = $this->objectModel->app->getModulePath('', 'todo') . 'control.php';
include $modulePath;
$code = $this->objectModel->getMethodCode('todo', 'create');
return strpos($code, "public function create(\$date = 'today', \$userID = '', \$from = 'todo')") !== false ? 1 : 0;
return strpos($code, "public function create(") !== false ? 1 : 0;
}
/**
+35
View File
@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
/**
* The zen file of editor module of ZenTaoPMS.
*
* @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.zentao.net)
* @license ZPL(https://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
* @author Yidong Wang <yidong@easycorp.ltd>
* @package editor
* @link https://www.zentao.net
*/
class editorZen extends editor
{
protected function buildContentByAction(string $filePath, string $action, string $isExtends = ''): string
{
if(empty($filePath)) return '';
if($action == 'extendModel') return $this->editor->extendModel($filePath);
if($action == 'newPage') return $this->editor->newControl($filePath);
if($action == 'extendControl' && !empty($isExtends)) return $this->editor->extendControl($filePath, $isExtends);
if(($action == 'edit' or $action == 'override') && file_exists($filePath))
{
$fileContent = file_get_contents($filePath);
if($action == 'override')
{
$fileContent = str_replace("'../../", '$this->app->getModuleRoot() . \'', $fileContent);
$fileContent = str_replace(array('\'./', '"./'), array('\'../../view/', '"../../view'), $fileContent);
}
return $fileContent;
}
if(strrpos(basename($filePath), '.php') !== false) return "<?php\n";
return '';
}
}