* Refactor the create and update method of entry.

This commit is contained in:
wangxuepeng
2023-10-23 09:30:03 +08:00
parent bb5a6b22cd
commit b9acfb080e
5 changed files with 55 additions and 27 deletions
+23
View File
@@ -0,0 +1,23 @@
<?php
$config->entry->form = new stdClass();
$config->entry->form->create = array();
$config->entry->form->edit = array();
$config->entry->form->create['name'] = array('type' => 'string', 'required' => true, 'filter' => 'trim');
$config->entry->form->create['code'] = array('type' => 'string', 'required' => true, 'filter' => 'trim');
$config->entry->form->create['account'] = array('type' => 'string', 'required' => true, 'filter' => 'trim', 'default' => '');
$config->entry->form->create['ip'] = array('type' => 'string', 'required' => true, 'filter' => 'trim', 'default' => '*');
$config->entry->form->create['key'] = array('type' => 'string', 'required' => true, 'filter' => 'trim', 'default' => '');
$config->entry->form->create['allIP'] = array('type' => 'string', 'required' => true, 'filter' => 'trim', 'default' => 'off');
$config->entry->form->create['freePasswd'] = array('type' => 'int', 'required' => true);
$config->entry->form->create['desc'] = array('type' => 'string', 'required' => false, 'default' => '', 'control' => 'editor');
$config->entry->form->edit['name'] = array('type' => 'string', 'required' => true, 'filter' => 'trim');
$config->entry->form->edit['code'] = array('type' => 'string', 'required' => true, 'filter' => 'trim');
$config->entry->form->edit['account'] = array('type' => 'string', 'required' => true, 'filter' => 'trim', 'default' => '');
$config->entry->form->edit['ip'] = array('type' => 'string', 'required' => true, 'filter' => 'trim', 'default' => '*');
$config->entry->form->edit['key'] = array('type' => 'string', 'required' => true, 'filter' => 'trim', 'default' => '');
$config->entry->form->edit['allIP'] = array('type' => 'string', 'required' => true, 'filter' => 'trim', 'default' => 'off');
$config->entry->form->edit['freePasswd'] = array('type' => 'int', 'required' => true);
$config->entry->form->edit['desc'] = array('type' => 'string', 'required' => false, 'default' => '', 'control' => 'editor');
+18 -2
View File
@@ -43,7 +43,15 @@ class entry extends control
{
if($_POST)
{
$id = $this->entry->create();
$entry = form::data($this->config->entry->form->create)
->setIF($this->post->allIP === 'on', 'ip', '*')
->setIF($this->post->freePasswd == '1', 'account', '')
->add('createdBy', $this->app->user->account)
->add('createdDate', helper::now())
->remove('allIP')
->get();
$id = $this->entry->create($entry);
if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
$this->loadModel('action')->create('entry', $id, 'created');
@@ -67,7 +75,15 @@ class entry extends control
{
if($_POST)
{
$changes = $this->entry->update($id);
$entry = form::data($this->config->entry->form->create)
->setIF($this->post->allIP === 'on', 'ip', '*')
->setIF($this->post->freePasswd == '1', 'account', '')
->add('createdBy', $this->app->user->account)
->add('createdDate', helper::now())
->remove('allIP')
->get();
$changes = $this->entry->update($id, $entry);
if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
if($changes)
+2
View File
@@ -27,9 +27,11 @@ function toggleAllIP(event)
if($(event.target).prop('checked'))
{
$('#ip').attr('disabled', 'disabled');
$('input[name=ip][type=hidden]').removeAttr('disabled');
}
else
{
$('input[name=ip][type=hidden]').attr('disabled', 'disabled');
$('#ip').removeAttr('disabled');
}
}
+11 -24
View File
@@ -81,23 +81,16 @@ class entryModel extends model
}
/**
* 创建一个应用。
* Create an entry.
*
* @param object $entry
* @access public
* @return bool | int
* @return bool|int
*/
public function create()
public function create(object $entry): bool|int
{
$entry = fixer::input('post')
->setDefault('ip', '*')
->setIF($this->post->allIP, 'ip', '*')
->setIF($this->post->freePasswd == '1', 'account', '')
->add('createdBy', $this->app->user->account)
->add('createdDate', helper::now())
->remove('allIP')
->get();
if($this->post->freePasswd == 1) $this->config->entry->create->requiredFields = 'name, code, key';
if($entry->freePasswd == 1) $this->config->entry->create->requiredFields = 'name, code, key';
$this->dao->insert(TABLE_ENTRY)->data($entry)
->batchCheck($this->config->entry->create->requiredFields, 'notempty')
@@ -111,25 +104,19 @@ class entryModel extends model
}
/**
* 更新一个应用。
* Update an entry.
*
* @param int $entryID
* @param int $entryID
* @param object $entry
* @access public
* @return bool | array
* @return bool|array
*/
public function update($entryID)
public function update(int $entryID, object $entry): bool|array
{
$oldEntry = $this->getById($entryID);
$entry = fixer::input('post')
->setDefault('ip', '*')
->setIF($this->post->allIP, 'ip', '*')
->add('editedBy', $this->app->user->account)
->add('editedDate', helper::now())
->remove('allIP')
->get();
if($this->post->freePasswd == 1) $this->config->entry->edit->requiredFields = 'name, code, key';
if($entry->freePasswd == 1) $this->config->entry->edit->requiredFields = 'name, code, key';
$this->dao->update(TABLE_ENTRY)->data($entry)
->batchCheck($this->config->entry->edit->requiredFields, 'notempty')
+1 -1
View File
@@ -150,7 +150,7 @@ formPanel
(
set::name('desc'),
set::rows('3'),
set::value($entry->desc),
html($entry->desc),
)
),
);