From 6927587203ef352cf166d3056e2faedef02b2260 Mon Sep 17 00:00:00 2001 From: xieqiyu Date: Tue, 26 Dec 2023 21:22:57 +0800 Subject: [PATCH] * Refactor zanodeModel::editSnapshot. --- module/zanode/config/dtable.php | 8 +++---- module/zanode/config/form.php | 12 +++++----- module/zanode/control.php | 5 +++- module/zanode/model.php | 19 +++++---------- module/zanode/test/model/editsnapshot.php | 28 +++++++++++++++++++++++ module/zanode/test/zanode.class.php | 17 ++++++++++++++ 6 files changed, 65 insertions(+), 24 deletions(-) create mode 100644 module/zanode/test/model/editsnapshot.php diff --git a/module/zanode/config/dtable.php b/module/zanode/config/dtable.php index 3930b860ef..e89badbbff 100644 --- a/module/zanode/config/dtable.php +++ b/module/zanode/config/dtable.php @@ -120,10 +120,10 @@ $config->zanode->snapshotDtable->fieldList['actions']['title'] = $lang->actions; $config->zanode->snapshotDtable->fieldList['actions']['type'] = 'actions'; $config->zanode->snapshotDtable->fieldList['actions']['menu'] = array('editSnapshot', 'restoreSnapshot', 'deleteSnapshot'); -$config->zanode->snapshotDtable->fieldList['actions']['list']['editSnapshot']['icon'] = 'edit'; -$config->zanode->snapshotDtable->fieldList['actions']['list']['editSnapshot']['hint'] = $lang->zanode->editSnapshot; -$config->zanode->snapshotDtable->fieldList['actions']['list']['editSnapshot']['className'] = 'editSnapshot'; -$config->zanode->snapshotDtable->fieldList['actions']['list']['editSnapshot']['url'] = array('module' => 'zanode', 'method' => 'editSnapshot', 'params' => 'snapshotID={id}'); +$config->zanode->snapshotDtable->fieldList['actions']['list']['editSnapshot']['icon'] = 'edit'; +$config->zanode->snapshotDtable->fieldList['actions']['list']['editSnapshot']['hint'] = $lang->zanode->editSnapshot; +$config->zanode->snapshotDtable->fieldList['actions']['list']['editSnapshot']['className'] = 'editSnapshot'; +$config->zanode->snapshotDtable->fieldList['actions']['list']['editSnapshot']['url'] = array('module' => 'zanode', 'method' => 'editSnapshot', 'params' => 'snapshotID={id}'); $config->zanode->snapshotDtable->fieldList['actions']['list']['restoreSnapshot']['icon'] = 'restart'; $config->zanode->snapshotDtable->fieldList['actions']['list']['restoreSnapshot']['hint'] = $lang->zanode->restoreSnapshot; diff --git a/module/zanode/config/form.php b/module/zanode/config/form.php index fa1c04824f..f3b2eece17 100644 --- a/module/zanode/config/form.php +++ b/module/zanode/config/form.php @@ -33,10 +33,10 @@ $config->zanode->form->createsnapshot = array(); $config->zanode->form->createsnapshot['name'] = array('type' => 'string', 'required' => true, 'filter' => 'trim'); $config->zanode->form->createsnapshot['desc'] = array('type' => 'string', 'required' => false, 'control' => 'editor'); -$config->zanode->form->editSnapshot = array(); -$config->zanode->form->editSnapshot['name'] = array('type' => 'string', 'required' => true, 'filter' => 'trim'); -$config->zanode->form->editSnapshot['desc'] = array('type' => 'string', 'required' => false, 'control' => 'editor'); +$config->zanode->form->editsnapshot = array(); +$config->zanode->form->editsnapshot['name'] = array('type' => 'string', 'required' => true, 'filter' => 'trim'); +$config->zanode->form->editsnapshot['desc'] = array('type' => 'string', 'required' => false, 'control' => 'editor'); -$config->zanode->form->ajaxUpdateImage = array(); -$config->zanode->form->ajaxUpdateImage['status'] = array('type' => 'string', 'required' => false); -$config->zanode->form->ajaxUpdateImage['path'] = array('type' => 'string', 'required' => false); +$config->zanode->form->ajaxupdateimage = array(); +$config->zanode->form->ajaxupdateimage['status'] = array('type' => 'string', 'required' => false); +$config->zanode->form->ajaxupdateimage['path'] = array('type' => 'string', 'required' => false); diff --git a/module/zanode/control.php b/module/zanode/control.php index a68e75b485..22c51fcf7c 100644 --- a/module/zanode/control.php +++ b/module/zanode/control.php @@ -311,7 +311,10 @@ class zanode extends control if($_POST) { - $this->zanode->editSnapshot($snapshotID); + $formData = form::data()->get(); + if(is_numeric($formData->name)) return $this->sendError(array('name' => sprintf($this->lang->error->code, $this->lang->zanode->name))); + + $this->zanode->editSnapshot($snapshotID, $formData); if(dao::isError()) return $this->sendError(dao::getError()); $this->loadModel('action')->create('zanode', $snapshot->host, 'editSnapshot', '', $snapshot->localName ? $snapshot->localName : $snapshot->name); diff --git a/module/zanode/model.php b/module/zanode/model.php index d8e3a19f7b..fb703c3e80 100644 --- a/module/zanode/model.php +++ b/module/zanode/model.php @@ -191,22 +191,16 @@ class zanodemodel extends model } /** - * Edit Snapshot. + * 编辑快照。 + * Edit snapshot. * - * @param int $snapshotID + * @param int $snapshotID + * @param object $data * @access public - * @return bool + * @return void */ - public function editSnapshot($snapshotID) + public function editSnapshot(int $snapshotID, object $data): void { - $data = form::data()->get(); - - if(empty($data->name)) dao::$errors['name'] = $this->lang->zanode->imageNameEmpty; - if(dao::isError()) return false; - - if(is_numeric($data->name)) dao::$errors['name'] = sprintf($this->lang->error->code, $this->lang->zanode->name); - if(dao::isError()) return false; - $newSnapshot = new stdClass(); $newSnapshot->localName = $data->name; $newSnapshot->desc = $data->desc; @@ -216,7 +210,6 @@ class zanodemodel extends model ->where('id')->eq($snapshotID) ->autoCheck() ->exec(); - return true; } /** diff --git a/module/zanode/test/model/editsnapshot.php b/module/zanode/test/model/editsnapshot.php new file mode 100644 index 0000000000..a9fca19253 --- /dev/null +++ b/module/zanode/test/model/editsnapshot.php @@ -0,0 +1,28 @@ +#!/usr/bin/env php +editSnapshot(). +cid=1 + +- 测试编辑快照 + - 属性name @defaultSnap + - 属性localName @test + - 属性desc @test + +*/ + +include dirname(__FILE__, 5) . '/test/lib/init.php'; +include dirname(__FILE__, 2) . '/zanode.class.php'; + +zdTable('image')->config('image')->gen(1); +zdTable('user')->gen(5); +su('admin'); + +$snapshot = new stdclass(); +$snapshot->name = 'test'; +$snapshot->desc = 'test'; + +$zanode = new zanodeTest(); +r($zanode->editSnapshotTest(1, $snapshot)) && p('name,localName,desc') && e('defaultSnap,test,test'); //测试编辑快照 diff --git a/module/zanode/test/zanode.class.php b/module/zanode/test/zanode.class.php index e432a8b619..2acb4a8501 100644 --- a/module/zanode/test/zanode.class.php +++ b/module/zanode/test/zanode.class.php @@ -154,4 +154,21 @@ class zanodeTest return $this->objectModel->dao->select('*')->from(TABLE_IMAGE)->where('id')->eq($snapshotID)->fetch(); } + + /** + * 测试编辑快照。 + * Test edit snapshot. + * + * @param int $snapshotID + * @param object $data + * @access public + * @return void + */ + public function editSnapshotTest(int $snapshotID, object $data): object|array + { + $this->editSnapshot($snapshotID, $data); + if(dao::isError()) return dao::getError(); + + return $this->objectModel->dao->select('*')->from(TABLE_IMAGE)->where('id')->eq($snapshotID)->fetch(); + } }