From 254a6db995b16da1bba33980ae3a915528656431 Mon Sep 17 00:00:00 2001 From: liumengyi Date: Tue, 23 May 2023 13:40:26 +0800 Subject: [PATCH] * Optimize code for bug-batchCreate. --- module/bug/config/form.php | 33 +++++++------ module/bug/model.php | 13 ++--- module/bug/ui/batchcreate.html.php | 28 +++++------ module/bug/zen.php | 78 +++++------------------------- 4 files changed, 46 insertions(+), 106 deletions(-) diff --git a/module/bug/config/form.php b/module/bug/config/form.php index d5eca2e9f7..04a2da2720 100644 --- a/module/bug/config/form.php +++ b/module/bug/config/form.php @@ -124,22 +124,23 @@ $config->bug->form->batchActivate['assignedToList'] = array('type' => 'array', $config->bug->form->batchActivate['openedBuildList'] = array('type' => 'array', 'required' => false, 'default' => array()); $config->bug->form->batchActivate['commentList'] = array('type' => 'array', 'required' => false, 'default' => array()); -$config->bug->form->batchCreate = array(); -$config->bug->form->batchCreate['modules'] = array('required' => false, 'type' => 'array', 'default' => array()); -$config->bug->form->batchCreate['projects'] = array('required' => false, 'type' => 'array', 'default' => array()); -$config->bug->form->batchCreate['executions'] = array('required' => false, 'type' => 'array', 'default' => array()); -$config->bug->form->batchCreate['branches'] = array('required' => false, 'type' => 'array', 'default' => array()); -$config->bug->form->batchCreate['lanes'] = array('required' => false, 'type' => 'array', 'default' => array()); -$config->bug->form->batchCreate['openedBuilds'] = array('required' => true, 'type' => 'array', 'default' => array()); -$config->bug->form->batchCreate['title'] = array('required' => true, 'type' => 'array', 'default' => array()); -$config->bug->form->batchCreate['deadlines'] = array('required' => false, 'type' => 'array', 'default' => array()); -$config->bug->form->batchCreate['stepses'] = array('required' => false, 'type' => 'array', 'default' => array()); -$config->bug->form->batchCreate['types'] = array('required' => false, 'type' => 'array', 'default' => array()); -$config->bug->form->batchCreate['pris'] = array('required' => false, 'type' => 'array', 'default' => array()); -$config->bug->form->batchCreate['severities'] = array('required' => false, 'type' => 'array', 'default' => array()); -$config->bug->form->batchCreate['oses'] = array('required' => false, 'type' => 'array', 'default' => array()); -$config->bug->form->batchCreate['browsers'] = array('required' => false, 'type' => 'array', 'default' => array()); -$config->bug->form->batchCreate['keywords'] = array('required' => false, 'type' => 'array', 'default' => array()); +$config->bug->form->batchCreate = common::formConfig('bug', 'batchCreate'); +$config->bug->form->batchCreate['module'] = array('required' => false, 'type' => 'int', 'default' => 0); +$config->bug->form->batchCreate['project'] = array('required' => false, 'type' => 'int', 'default' => 0); +$config->bug->form->batchCreate['execution'] = array('required' => false, 'type' => 'int', 'default' => 0); +$config->bug->form->batchCreate['branch'] = array('required' => false, 'type' => 'int', 'default' => 0); +$config->bug->form->batchCreate['laneID'] = array('required' => false, 'type' => 'int', 'default' => 0); +$config->bug->form->batchCreate['openedBuild'] = array('required' => true, 'type' => 'array', 'default' => '', 'filter' => 'join'); +$config->bug->form->batchCreate['title'] = array('required' => true, 'type' => 'string', 'default' => '', 'base' => true); +$config->bug->form->batchCreate['deadline'] = array('required' => false, 'type' => 'date', 'default' => ''); +$config->bug->form->batchCreate['steps'] = array('required' => false, 'type' => 'string', 'default' => ''); +$config->bug->form->batchCreate['type'] = array('required' => false, 'type' => 'string', 'default' => ''); +$config->bug->form->batchCreate['color'] = array('required' => false, 'type' => 'string', 'default' => ''); +$config->bug->form->batchCreate['pri'] = array('required' => false, 'type' => 'int', 'default' => 0); +$config->bug->form->batchCreate['severity'] = array('required' => false, 'type' => 'int', 'default' => 0); +$config->bug->form->batchCreate['os'] = array('required' => false, 'type' => 'array', 'default' => '', 'filter' => 'join'); +$config->bug->form->batchCreate['browser'] = array('required' => false, 'type' => 'array', 'default' => '', 'filter' => 'join'); +$config->bug->form->batchCreate['keywords'] = array('required' => false, 'type' => 'string', 'default' => ''); $config->bug->form->batchEdit = array(); $config->bug->form->batchEdit['types'] = array('required' => false, 'type' => 'array', 'default' => array()); diff --git a/module/bug/model.php b/module/bug/model.php index a0905e8e02..f434c8c8b8 100644 --- a/module/bug/model.php +++ b/module/bug/model.php @@ -91,12 +91,8 @@ class bugModel extends model foreach($bugs as $index => $bug) { /* Get lane id, remove laneID from bug. */ - $laneID = zget($output, 'laneID', 0); - if(isset($bug->laneID)) - { - $laneID = $bug->laneID; - unset($bug->laneID); - } + $laneID = !empty($bug->laneID) ? zget($output, 'laneID', 0) : $bug->laneID; + unset($bug->laneID); $uploadImage = !empty($uploadImages[$index]) ? $uploadImages[$index] : ''; @@ -2985,14 +2981,11 @@ class bugModel extends model continue; } - /* Title cannot be empty. */ - if(empty($bug->title)) dao::$errors['message'][] = sprintf($this->lang->error->notempty, $this->lang->bug->title); - /* Check required fields. */ foreach(explode(',', $this->config->bug->create->requiredFields) as $field) { $field = trim($field); - if($field and empty($bug->$field) and $field != 'title') dao::$errors['message'][] = sprintf($this->lang->error->notempty, $this->lang->bug->$field); + if($field and empty($bug->$field) and $field != 'title') dao::$errors["{$filed}[{$index}]"] = sprintf($this->lang->error->notempty, $this->lang->bug->$field); } } diff --git a/module/bug/ui/batchcreate.html.php b/module/bug/ui/batchcreate.html.php index 5b0d1fd041..39ef1425c2 100644 --- a/module/bug/ui/batchcreate.html.php +++ b/module/bug/ui/batchcreate.html.php @@ -40,7 +40,7 @@ $items[] = array /* Field of branch. */ $items[] = array ( - 'name' => 'branches', + 'name' => 'branch', 'label' => $lang->product->branchName[$product->type], 'hidden' => zget($visibleFields, $product->type, true, false), 'control' => 'select', @@ -53,7 +53,7 @@ $items[] = array /* Field of module. */ $items[] = array ( - 'name' => 'modules', + 'name' => 'module', 'label' => $lang->bug->module, 'hidden' => zget($visibleFields, 'module', true, false), 'control' => 'select', @@ -67,7 +67,7 @@ $items[] = array /* Field of project. */ $items[] = array ( - 'name' => 'projects', + 'name' => 'project', 'label' => $lang->bug->project, 'hidden' => zget($visibleFields, 'project', true, false), 'control' => 'select', @@ -81,7 +81,7 @@ $items[] = array /* Field of execution. */ $items[] = array ( - 'name' => 'executions', + 'name' => 'execution', 'label' => isset($project->model) && $project->model == 'kanban' ? $lang->bug->kanban : $lang->bug->execution, 'hidden' => zget($visibleFields, 'execution', true, false), 'control' => 'select', @@ -95,7 +95,7 @@ $items[] = array /* Field of openedBuild. */ $items[] = array ( - 'name' => 'openedBuilds', + 'name' => 'openedBuild', 'label' => $lang->bug->openedBuild, 'control' => array( 'type' => 'select', @@ -122,7 +122,7 @@ if(isset($executionType) && $executionType == 'kanban') { $items[] = array ( - 'name' => 'regions', + 'name' => 'region', 'label' => $lang->kanbancard->region, 'control' => 'select', 'value' => $regionID, @@ -132,7 +132,7 @@ if(isset($executionType) && $executionType == 'kanban') $items[] = array ( - 'name' => 'lanes', + 'name' => 'laneID', 'label' => $lang->kanbancard->lane, 'control' => 'select', 'value' => $laneID, @@ -144,7 +144,7 @@ if(isset($executionType) && $executionType == 'kanban') /* Field of deadline. */ $items[] = array ( - 'name' => 'deadlines', + 'name' => 'deadline', 'label' => $lang->bug->deadline, 'hidden' => zget($visibleFields, 'deadline', true, false), 'control' => 'date', @@ -156,7 +156,7 @@ $items[] = array /* Field of steps. */ $items[] = array ( - 'name' => 'stepses', + 'name' => 'steps', 'label' => $lang->bug->steps, 'hidden' => zget($visibleFields, 'steps', true, false), 'width' => '240px', @@ -166,7 +166,7 @@ $items[] = array /* Field of type. */ $items[] = array ( - 'name' => 'types', + 'name' => 'type', 'label' => $lang->typeAB, 'hidden' => zget($visibleFields, 'type', true, false), 'control' => 'select', @@ -180,7 +180,7 @@ $items[] = array /* Field of pri. */ $items[] = array ( - 'name' => 'pris', + 'name' => 'pri', 'label' => $lang->bug->pri, 'hidden' => zget($visibleFields, 'pri', true, false), 'control' => 'select', @@ -193,7 +193,7 @@ $items[] = array /* Field of severity. */ $items[] = array ( - 'name' => 'severities', + 'name' => 'severity', 'label' => $lang->bug->severity, 'hidden' => zget($visibleFields, 'severity', true, false), 'control' => 'select', @@ -206,7 +206,7 @@ $items[] = array /* Field of os. */ $items[] = array ( - 'name' => 'oses', + 'name' => 'os', 'label' => $lang->bug->os, 'hidden' => zget($visibleFields, 'os', true, false), 'control' => 'select', @@ -218,7 +218,7 @@ $items[] = array /* Field of browser. */ $items[] = array ( - 'name' => 'browsers', + 'name' => 'browser', 'label' => $lang->bug->browser, 'hidden' => zget($visibleFields, 'browser', true, false), 'control' => 'select', diff --git a/module/bug/zen.php b/module/bug/zen.php index 1323d24fef..0f412d154d 100644 --- a/module/bug/zen.php +++ b/module/bug/zen.php @@ -1692,7 +1692,7 @@ class bugZen extends bug */ protected function buildBugsForBatchCreate(int $productID, string $branch, array|false $bugImagesFile): array { - $data = form::data($this->config->bug->form->batchCreate)->get(); + $bugs = form::batchData($this->config->bug->form->batchCreate)->get(); /* Get pairs(moduleID => moduleOwner) for bug. */ $stmt = $this->dbh->query($this->loadModel('tree')->buildMenuQuery($productID, 'bug', 0, $branch)); @@ -1700,80 +1700,26 @@ class bugZen extends bug while($module = $stmt->fetch()) $moduleOwners[$module->id] = $module->owner; /* Construct data. */ - $module = 0; - $project = 0; - $execution = 0; - $type = ''; - $pri = 0; - $bugs = array(); - $extendFields = $this->bug->getFlowExtendFields(); - foreach($data->title as $index => $title) + foreach($bugs as $bug) { - $title = trim($title); - if(empty($title)) continue; - - $bug = new stdClass(); $bug->openedBy = $this->app->user->account; $bug->openedDate = helper::now(); + $bug->openedBuild = implode(',', $bug->openedBuild); $bug->product = $productID; - $bug->branch = (int)zget($data->branches, $index, 0); - $bug->module = (int)zget($data->modules, $index, 0); - $bug->project = (int)zget($data->projects, $index, 0); - $bug->execution = (int)zget($data->executions, $index, 0); - $bug->openedBuild = isset($data->openedBuilds) && is_array($data->openedBuilds[$index]) ? implode(',', $data->openedBuilds[$index]) : zget($data->openedBuilds, $index); - $bug->title = $title; - $bug->deadline = zget($data->deadlines, $index, null); - $bug->steps = nl2br($data->stepses[$index]); - $bug->type = zget($data->types, $index); - $bug->pri = zget($data->pris, $index); - $bug->severity = $data->severities[$index]; - $bug->keywords = $data->keywords[$index]; + $bug->steps = nl2br($bug->steps); + $bug->os = implode(',', $bug->os); + $bug->browser = implode(',', $bug->browser); - $bugs[$index] = $this->buildDataForBatchCreate($bug, $data, $index, $moduleOwners, $extendFields, $bugImagesFile); + /* Assign the bug to the person in charge of the module. */ + if(!empty($moduleOwners[$bug->module])) + { + $bug->assignedTo = $moduleOwners[$bug->module]; + $bug->assignedDate = helper::now(); + } } return $bugs; } - /** - * 为批量创建bug构造数据。 - * Construct data for batch bug creation. - * - * @param object $bug - * @param object $data - * @param int $index - * @param array $moduleOwners - * @param array $extendFields - * @param array $bugImagesFile - * @access protected - * @return object - */ - protected function buildDataForBatchCreate(object $bug, object $data, int $index, array $moduleOwners, array $extendFields, array|false $bugImagesFile): object - { - $oses = is_array($data->oses[$index]) ? array_filter($data->oses[$index]) : array(); - $browsers = is_array($data->browsers[$index]) ? array_filter($data->browsers[$index]) : array(); - $bug->os = implode(',', $oses); - $bug->browser = implode(',', $browsers); - - if(isset($data->lanes[$index])) $bug->laneID = $data->lanes[$index]; - - /* Assign the bug to the person in charge of the module. */ - if(!empty($moduleOwners[$bug->module])) - { - $bug->assignedTo = $moduleOwners[$bug->module]; - $bug->assignedDate = helper::now(); - } - - /* Get extend fields. */ - foreach($extendFields as $extendField) - { - $bug->{$extendField->field} = $data->{$extendField->field}[$index]; - if(is_array($bug->{$extendField->field})) $bug->{$extendField->field} = implode(',', $bug->{$extendField->field}); - $bug->{$extendField->field} = htmlSpecialString($bug->{$extendField->field}); - } - - return $bug; - } - /** * 批量创建bug后返回响应。 * Response after batch create.