* Optimize bug batchCreate function.

This commit is contained in:
wangyuting
2023-05-29 14:48:11 +08:00
parent 40556d2b89
commit a06a557a94
5 changed files with 142 additions and 179 deletions
+21 -16
View File
@@ -670,7 +670,6 @@ class bug extends control
/* Assign. */
$this->view->title = $this->lang->bug->linkBugs . "BUG #$bug->id $bug->title {$this->lang->dash} " . $this->products[$bug->product];
$this->view->bug = $bug;
$this->view->bugs2Link = $this->bug->getBugs2Link($bugID, $bySearch, $excludeBugs, $queryID, $pager);
$this->view->users = $this->user->getPairs('noletter');
$this->view->pager = $pager;
@@ -702,23 +701,29 @@ class bug extends control
$bugs = $this->bugZen->checkBugsForBatchCreate($bugs, $productID);
if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
/* Batch create bugs. */
$actions = $this->bug->batchCreate($bugs, $productID, $output, $this->post->uploadImage, $this->session->bugImagesFile);
helper::setcookie('bugModule', 0, 0);
/* Remove upload image file and session. */
if(!empty($this->post->uploadImage) and !empty($this->session->bugImagesFile))
$uploadImages = $this->post->uploadImage;
$bugImagesFiles = $this->session->bugImagesFile;
$bugIDList = array();
$message = '';
foreach($bugs as $index => $bug)
{
$classFile = $this->app->loadClass('zfile');
$file = current($_SESSION['bugImagesFile']);
$realPath = dirname($file['realpath']);
if(is_dir($realPath)) $classFile->removeDir($realPath);
unset($_SESSION['bugImagesFile']);
}
$uploadImage = !empty($uploadImages[$index]) ? $uploadImages[$index] : '';
$response = $this->bugZen->responseAfterBatchCreate($productID, $branch, $executionID, $actions ? $actions : array());
return $this->send($response);
$file = $this->bugZen->processImageForBatchCreate($bug, $uploadImage, $bugImagesFiles);
$bug->id = $this->bug->create($bug);
/* Processing other operations after batch creation. */
$this->bugZen->afterBatchCreate($bug, $output, $uploadImage, $file);
$message = $this->executeHooks($bug->id);
$bugIDList[] = $bug->id;
}
if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
$this->loadModel('score')->create('ajax', 'batchCreate');
if(!$message) $message = $this->lang->saveSuccess;
return $this->bugZen->responseAfterBatchCreate($productID, $branch, $executionID, $bugIDList, $message);
}
/* Get product, then set menu. */
+10 -136
View File
@@ -56,9 +56,9 @@ class bugModel extends model
*/
public function create(object $bug, string $action = 'Opened'): int|false
{
$this->dao->insert(TABLE_BUG)->data($bug)
$this->dao->insert(TABLE_BUG)->data($bug, 'laneID')
->autoCheck()
->checkIF($bug->notifyEmail, 'notifyEmail', 'email')
->checkIF(!empty($bug->notifyEmail), 'notifyEmail', 'email')
->batchCheck($this->config->bug->create->requiredFields, 'notempty')
->checkFlow()
->exec();
@@ -69,58 +69,17 @@ class bugModel extends model
$this->loadModel('action')->create('bug', $bugID, $action);
return $bugID;
}
/**
* 批量创建bug
* Batch create bugs.
*
* @param int $productID
* @param array $extra
* @param array|bool $extra
* @access public
* @return array
*/
public function batchCreate(array $bugs, int $productID, array $output = array(), array|bool $uploadImages = false, array|bool $bugImagesFiles = false)
{
/* Load module and init vars. */
$this->loadModel('action');
if(!empty($uploadImages)) $this->loadModel('file');
$actions = array();
foreach($bugs as $index => $bug)
/* Add score for create. */
if(!empty($bug->case))
{
/* Get lane id, remove laneID from bug. */
$laneID = !empty($bug->laneID) ? $bug->laneID : zget($output, 'laneID', 0);
unset($bug->laneID);
$uploadImage = !empty($uploadImages[$index]) ? $uploadImages[$index] : '';
$file = $this->processImageForBatchCreate($bug, $uploadImage, $bugImagesFiles);
/* Create a bug. */
$this->dao->insert(TABLE_BUG)->data($bug)
->autoCheck()
->batchCheck($this->config->bug->create->requiredFields, 'notempty')
->checkFlow()
->exec();
if(dao::isError()) return false;
$bug->id = $this->dao->lastInsertID();
/* Processing other operations after batch creation. */
$actions[$bug->id] = $this->afterBatchCreate($bug, $laneID, $output, $uploadImage, $file);
if(dao::isError())
{
dao::$errors['message'][] = 'bug#' . ($index) . dao::getError(true);
return false;
}
$this->loadModel('score')->create('bug', 'createFormCase', $bug->case);
}
else
{
$this->loadModel('score')->create('bug', 'create', $bugID);
}
if(!dao::isError()) $this->loadModel('score')->create('ajax', 'batchCreate');
return $actions;
return $bugID;
}
/**
@@ -2698,91 +2657,6 @@ class bugModel extends model
return $index;
}
/**
* 批量创建bug前处理上传图片。
* Before batch creating bugs, process the uploaded images.
*
* @param object $bug
* @param string $uploadImage
* @param array|bool $bugImagesFiles
* @access protected
* @return array|false
*/
protected function processImageForBatchCreate(object $bug, string $uploadImage, array|bool $bugImagesFiles): array|false
{
/* When the bug is created by uploading an image, add the image to the step of the bug. */
if(!empty($uploadImage))
{
$this->loadModel('file');
$file = $bugImagesFiles[$uploadImage];
$realPath = $file['realpath'];
if(rename($realPath, $this->file->savePath . $this->file->getSaveName($file['pathname'])))
{
if(in_array($file['extension'], $this->config->file->imageExtensions))
{
$file['addedBy'] = $this->app->user->account;
$file['addedDate'] = helper::now();
$this->dao->insert(TABLE_FILE)->data($file, 'realpath')->exec();
$fileID = $this->dao->lastInsertID();
$bug->steps .= '<img src="{' . $fileID . '.' . $file['extension'] . '}" alt="" />';
}
}
else
{
unset($file);
}
}
return !empty($file) ? $file : false;
}
/**
* 批量创建bug后的其他处理。
* Processing after batch creation of bug.
*
* @param object $bug
* @param int $laneID
* @param array $output
* @param string $uploadImage
* @param array|bool $file
* @access protected
* @return int|bool
*/
protected function afterBatchCreate(object $bug, int $laneID, array $output, string $uploadImage, array|bool $file): int|bool
{
/* Record log. */
$actionID = $this->loadModel('action')->create('bug', $bug->id, 'Opened');
$this->loadModel('score')->create('bug', 'create', $bug->id);
$this->executeHooks($bug->id);
/* If bug has the execution, update kanban data. */
if($bug->execution)
{
$columnID = $this->loadModel('kanban')->getColumnIDByLaneID($laneID, 'unconfirmed');
if(empty($columnID)) $columnID = zget($output, 'columnID', 0);
if(!empty($laneID) and !empty($columnID)) $this->kanban->addKanbanCell($bug->execution, $laneID, $columnID, 'bug', $bug->id);
if(empty($laneID) or empty($columnID)) $this->kanban->updateLane($bug->execution, 'bug');
}
/* When the bug is created by uploading the image, add the image to the file of the bug. */
if(!empty($uploadImage) and !empty($file))
{
$file['objectType'] = 'bug';
$file['objectID'] = $bug->id;
$file['addedBy'] = $this->app->user->account;
$file['addedDate'] = helper::now();
$this->dao->insert(TABLE_FILE)->data($file, 'realpath')->exec();
unset($file);
}
return $actionID;
}
/**
* 批量编辑 bug 后的其他处理。
* Processing after batch edit of bug.
+2 -2
View File
@@ -13,8 +13,8 @@ namespace zin;
foreach($bugs2Link as $bug)
{
$bug->productName = zget($products, $bug->product);
$bug->openedBy = zget($users, $bug->openedBy);
$bug->assignedTo = zget($users, $bug->assignedTo);
$bug->openedBy = zget($users, $bug->openedBy);
$bug->assignedTo = zget($users, $bug->assignedTo);
}
$cols = array_values($config->bug->linkBugs->dtable->fieldList);
+5 -2
View File
@@ -92,9 +92,12 @@ foreach($browserList as $browser)
}
$mailtoList = '';
foreach(explode(',', str_replace(' ', '', $bug->mailto)) as $account)
if(!empty($bug->mailto))
{
$mailtoList .= ' ' . zget($users, $account);
foreach(explode(',', str_replace(' ', '', $bug->mailto)) as $account)
{
$mailtoList .= ' ' . zget($users, $account);
}
}
$linkBugs = array();
+104 -23
View File
@@ -490,6 +490,88 @@ class bugZen extends bug
return $this->send(array('result' => 'success', 'message' => $message, 'closeModal' => true, 'load' => $this->createLink('bug', 'view', "bugID=$bugID")));
}
/**
* 批量创建bug前处理上传图片。
* Before batch creating bugs, process the uploaded images.
*
* @param object $bug
* @param string $uploadImage
* @param array|bool $bugImagesFiles
* @access protected
* @return array|false
*/
protected function processImageForBatchCreate(object $bug, string $uploadImage, array|bool $bugImagesFiles): array|false
{
/* When the bug is created by uploading an image, add the image to the step of the bug. */
if(!empty($uploadImage))
{
$this->loadModel('file');
$file = $bugImagesFiles[$uploadImage];
$realPath = $file['realpath'];
if(rename($realPath, $this->file->savePath . $this->file->getSaveName($file['pathname'])))
{
if(in_array($file['extension'], $this->config->file->imageExtensions))
{
$file['addedBy'] = $this->app->user->account;
$file['addedDate'] = helper::now();
$this->dao->insert(TABLE_FILE)->data($file, 'realpath')->exec();
$fileID = $this->dao->lastInsertID();
$bug->steps .= '<img src="{' . $fileID . '.' . $file['extension'] . '}" alt="" />';
}
}
else
{
unset($file);
}
}
return !empty($file) ? $file : false;
}
/**
* 批量创建bug后的其他处理。
* Processing after batch creation of bug.
*
* @param object $bug
* @param array $output
* @param string $uploadImage
* @param array|bool $file
* @access protected
* @return bool
*/
protected function afterBatchCreate(object $bug, array $output, string $uploadImage, array|bool $file): bool
{
/* If bug has the execution, update kanban data. */
if($bug->execution)
{
/* Get lane id, remove laneID from bug. */
$laneID = !empty($bug->laneID) ? $bug->laneID : zget($output, 'laneID', 0);
unset($bug->laneID);
$columnID = $this->loadModel('kanban')->getColumnIDByLaneID($laneID, 'unconfirmed');
if(empty($columnID)) $columnID = zget($output, 'columnID', 0);
if(!empty($laneID) and !empty($columnID)) $this->kanban->addKanbanCell($bug->execution, $laneID, $columnID, 'bug', $bug->id);
if(empty($laneID) or empty($columnID)) $this->kanban->updateLane($bug->execution, 'bug');
}
/* When the bug is created by uploading the image, add the image to the file of the bug. */
if(!empty($uploadImage) and !empty($file))
{
$file['objectType'] = 'bug';
$file['objectID'] = $bug->id;
$file['addedBy'] = $this->app->user->account;
$file['addedDate'] = helper::now();
$this->dao->insert(TABLE_FILE)->data($file, 'realpath')->exec();
unset($file);
}
return !dao::isError();
}
/**
* 在弹窗中操作后的返回。
* Respond after operating in modal.
@@ -550,16 +632,6 @@ class bugZen extends bug
$bugID = $bug->id;
$todoID = isset($output['todoID']) ? $output['todoID'] : 0;
/* Add score for create. */
if(empty($bug->case))
{
$this->loadModel('score')->create('bug', 'create', $bugID);
}
else
{
$this->loadModel('score')->create('bug', 'createFormCase', $bug->case);
}
if($todoID)
{
$this->dao->update(TABLE_TODO)->set('status')->eq('done')->where('id')->eq($todoID)->exec();
@@ -1522,31 +1594,40 @@ class bugZen extends bug
* 批量创建bug后返回响应。
* Response after batch create.
*
* @param int $productID
* @param string $branch
* @param int $executionID
* @param array $actions
* @param int $productID
* @param string $branch
* @param int $executionID
* @param array $bugIDList
* @param string $message
* @access protected
* @return void
*/
protected function responseAfterBatchCreate(int $productID, string $branch, int $executionID, array $actions)
protected function responseAfterBatchCreate(int $productID, string $branch, int $executionID, array $bugIDList, string $message = '')
{
helper::setcookie('bugModule', '0', 0);
/* Remove upload image file and session. */
if(!empty($this->post->uploadImage) and !empty($this->session->bugImagesFile))
{
$classFile = $this->app->loadClass('zfile');
$file = current($_SESSION['bugImagesFile']);
$realPath = dirname($file['realpath']);
if(is_dir($realPath)) $classFile->removeDir($realPath);
unset($_SESSION['bugImagesFile']);
}
if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
/* Return bug id list when call the API. */
if($this->viewType == 'json')
{
$bugIDList = array_keys($actions);
return array('result' => 'success', 'message' => $this->lang->saveSuccess, 'idList' => $bugIDList);
}
if($this->viewType == 'json') return $this->send(array('result' => 'success', 'message' => $message, 'idList' => $bugIDList));
/* Respond after updating in modal. */
if(isonlybody() && $executionID) $this->responseInModal($executionID);
if(isonlybody() && $executionID) return $this->responseInModal($executionID);
/* If link from no head then reload. */
if(isonlybody()) return array('result' => 'success', 'message' => $this->lang->saveSuccess, 'closeModal' => true);
if(isonlybody()) return $this->send(array('result' => 'success', 'message' => $message, 'closeModal' => true));
return array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => $this->createLink('bug', 'browse', "productID={$productID}&branch={$branch}&browseType=unclosed&param=0&orderBy=id_desc"));
return $this->send(array('result' => 'success', 'message' => $message, 'load' => $this->createLink('bug', 'browse', "productID={$productID}&branch={$branch}&browseType=unclosed&param=0&orderBy=id_desc")));
}
/**