From 243b27e7595ed4d4cc53c010933a65afea884f2a Mon Sep 17 00:00:00 2001 From: liwenrui Date: Wed, 19 Jul 2023 09:42:24 +0800 Subject: [PATCH] * add action for prompt create / update, task #97648. --- module/ai/model.php | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/module/ai/model.php b/module/ai/model.php index 473bd19875..97c8577388 100644 --- a/module/ai/model.php +++ b/module/ai/model.php @@ -421,21 +421,43 @@ class aiModel extends model ->check('name', 'unique') ->autoCheck() ->exec(); + if(dao::isError()) return false; - return dao::isError() ? false : $this->dao->lastInsertID(); + $promptId = $this->dao->lastInsertID(); + $this->loadModel('action')->create('prompt', $promptId, 'created'); + + return $promptId; } /** * Update a prompt. * - * TODO: fully implement this. - * * @param object $prompt + * @param object $originalPrompt optional, original prompt to compare with and generate action. * @access public * @return bool */ - public function updatePrompt($prompt) + public function updatePrompt($prompt, $originalPrompt = null) { + /* Action name to create action record with. */ + $actionType = 'updated'; + + /* Compare with original, check what changed. */ + if(!empty($originalPrompt)) + { + $changedFields = array(); + foreach($prompt as $key => $value) + { + if($value != $originalPrompt->$key) $changedFields[] = $key; + } + + /* If only status changed, action is either published or unpublished. */ + if(count($changedFields) == 1 && current($changedFields) == 'status') + { + $actionType = $prompt->status == 'draft' ? 'unpublished' : 'published'; + } + } + $prompt->editedDate = helper::now(); $prompt->editedBy = $this->app->user->account; @@ -449,8 +471,10 @@ class aiModel extends model ->autoCheck() ->where('id')->eq($prompt->id) ->exec(); + if(dao::isError()) return false; - return !dao::isError(); + $this->loadModel('action')->create('prompt', $prompt->id, $actionType); + return true; } /**