From b03afaebb3b139459ffd61b04ff84dd27e05d59c Mon Sep 17 00:00:00 2001 From: tanghucheng Date: Wed, 6 Jul 2022 14:35:09 +0800 Subject: [PATCH] * Code for export. --- module/port/config.php | 12 +++- module/port/control.php | 91 ++++++++++++++++++------- module/port/model.php | 143 ++++++++++++++++++++++++++++++++++------ module/task/config.php | 8 +++ 4 files changed, 207 insertions(+), 47 deletions(-) diff --git a/module/port/config.php b/module/port/config.php index 8fe827b3aa..76f54bf36c 100644 --- a/module/port/config.php +++ b/module/port/config.php @@ -3,11 +3,17 @@ $config->port = new stdclass(); $config->port->fieldList['title'] = ''; $config->port->fieldList['width'] = ''; -$config->port->fieldList['required'] = ''; +$config->port->fieldList['required'] = 'no'; $config->port->fieldList['fixed'] = ''; -$config->port->fieldList['control'] = ''; +$config->port->fieldList['control'] = 'input'; $config->port->fieldList['values'] = ''; $config->port->fieldList['class'] = ''; $config->port->fieldList['sort'] = ''; +$config->port->fieldList['foreignKey'] = ''; +$config->port->fieldList['foreignKeySource'] = array('module' => '', 'method' => '', 'params' => '', 'pairs' => '', 'sql' => ''); - +$config->port->dateFeilds = ',estStarted,realStarted,deadline,openedDate,assignedDate,finishedDate,canceledDate,closedDate,lastEditedDate,'; +$config->port->datetimeFeilds = ''; +$config->port->sysLangField = ',pri,status,type,mode,'; +$config->port->sysDataField = ''; +$config->port->initFunction = 'title,control,required,'; diff --git a/module/port/control.php b/module/port/control.php index 2f11b54bb6..41b81d2db4 100644 --- a/module/port/control.php +++ b/module/port/control.php @@ -9,44 +9,85 @@ class port extends control */ public function export($model = '', $params = '') { - $fieldList = $this->initFieldList($model); + $params = explode(',', $params); + $params['executionID'] = 1; + $params['orderBy'] = 'id_desc'; + + extract($params); + + /* save params to session. */ + $this->session->set(($model.'ExportParams'), $params); + + /* Init config fieldList */ + $fieldList = $this->port->initFieldList($model); + + $queryCondition = $this->session->{$model . 'QueryCondition'}; + $onlyCondition = $this->session->{$model . 'OnlyCondition'}; + + $modelDatas = array(); + if($onlyCondition and $queryCondition) + { + $table = zget($this->config->objectTables, $model);; + if(isset($this->config->$model->port->table)) $table = $this->config->$model->port->table; + $modelDatas = $this->dao->select('*')->from($table)->where($queryCondition)->fetchAll('id'); + } + elseif($queryCondition) + { + $stmt = $this->dbh->query($queryCondition . ($this->post->exportType == 'selected' ? " AND t1.id IN({$this->cookie->checkedItem})" : '') . " ORDER BY " . strtr($orderBy, '_', ' ')); + while($row = $stmt->fetch()) $modelDatas[$row->id] = $row; + } + + $exportDatas = $this->getExportDatas($fieldList, $modelDatas); + $exportDatas['kind'] = $model; + + $exportFields= isset($this->config->$model->exportFields) ? $this->config->$model->exportFields : ''; + $exportFields = explode(',', $exportFields); + $exportDatas['exportFields'] = array_keys($exportDatas['fields']); + $exportDatas['fileType'] = 'xlsx'; + $exportDatas['fileName'] = '企业网站第一期-所有任务'; + + $exportDatas['typeList'] = join(',', $exportDatas['typeList']); + $exportDatas['priList'] = join(',', $exportDatas['priList']); + $_POST = $exportDatas; + //a($exportDatas);die; + $this->fetch('file', 'export2' . $_POST['fileType'], $POST); } /** - * Init FieldList . + * Get ExportDatas. * - * @param int $model + * @param int $fieldList + * @param int $modelDatas * @access public * @return void */ - public function initFieldList($model) + public function getExportDatas($fieldList, $modelDatas) { - $portFieldList = $this->config->port->fieldList; - - $this->loadModel($model); - $fields = isset($this->config->$model->exportFields) ? $this->config->$model->exportFields : ''; - - if(empty($fields)) return false; - - $fields = explode(',', $fields); - - /* build module fieldList. */ - foreach ($fields as $field) + $exportDatas = array(); + $foreignKeyList = array(); + foreach ($fieldList as $key => $field) { - $field = trim($field); - - $modelFieldList = isset($this->config->$model->fieldList[$field]) ? $this->config->$model->fieldList[$field] : array(); - - foreach ($portFieldList as $portField => $value) + $exportDatas['fields'][$key] = $field['title']; + if($field['foreignKey']) { - $funcName = 'init' . ucfirst($portField); - if(!array_key_exists($portField, $modelFieldList)) $modelFieldList[$portField] = $this->port->$funcName($model, $field); + $exportDatas['listStyle'][] = $key; + $exportDatas[$key.'List'] = $field['values']; + $foreignKeyList[] = $key; } - - $this->config->$model->fieldList[$field] = $modelFieldList; } - a($this->config->$model->fieldList); + foreach ($modelDatas as $id => $values) + { + foreach($values as $field => $value) + { + if(in_array($field, $foreignKeyList)) + { + $modelDatas[$id]->$field = zget($exportDatas[$field.'List'], $value); + } + } + } + $exportDatas['rows'] = array_values($modelDatas); + return $exportDatas; } /** diff --git a/module/port/model.php b/module/port/model.php index 37e0db4076..fb9dd05126 100644 --- a/module/port/model.php +++ b/module/port/model.php @@ -1,7 +1,50 @@ config->port->fieldList; + + $this->loadModel($model); + $fields = isset($this->config->$model->exportFields) ? $this->config->$model->exportFields : ''; + + if(empty($fields)) return false; + + $fields = explode(',', $fields); + + /* build module fieldList. */ + foreach ($fields as $field) + { + $field = trim($field); + + if(!isset($this->config->$model->fieldList[$field])) $this->config->$model->fieldList[$field] = array(); + $modelFieldList = $this->config->$model->fieldList[$field]; + + foreach ($portFieldList as $portField => $value) + { + $funcName = 'init' . ucfirst($portField); + if(!array_key_exists($portField, $modelFieldList)) + { + $modelFieldList[$portField] = $this->config->port->fieldList[$portField]; + if(strpos($this->config->port->initFunction, $portField) !== false) $modelFieldList[$portField] = $this->$funcName($model, $field); + } + } + + $modelFieldList['values'] = $this->initValues($model, $field, $modelFieldList); + $modelFieldList = $this->initForeignKey($model, $field, $modelFieldList); + + $this->config->$model->fieldList[$field] = $modelFieldList; + } + + return $this->config->$model->fieldList; + } /** * Init Title. @@ -20,9 +63,9 @@ class portModel extends model { $title = $this->lang->$model->$field; } - elseif(array_key_exists($field . 'AB', $this->lang->$model)) + elseif(array_key_exists(($field . 'AB'), $this->lang->$model)) { - $title = $this->lang->$model->$field . 'AB'; + $title = $this->lang->$model->{$field . 'AB'}; } elseif(array_key_exists($field, $this->lang->port->reservedWord)) { @@ -32,25 +75,84 @@ class portModel extends model return $title; } - public function initWidth($model, $field) - { - return '120px'; - - } - + /** + * Init Control . + * + * @param int $model + * @param int $field + * @access public + * @return void + */ public function initControl($model, $field) { - return 'select'; + if(isset($this->lang->$model->{$field.'List'})) return 'select'; + if(strpos($this->config->port->selectParis, $field) !== false) return 'select'; + return $this->config->port->fieldList['control']; } - public function initValues($model, $field) + /** + * Init Values . + * + * @param int $model + * @param int $field + * @access public + * @return void + */ + public function initValues($model, $field, $fieldValue = '') { - return 'value'; + extract($fieldValue['foreignKeySource']); // $module, $method, $params, $paris, $sql + + if($fieldValue['foreignKey'] and $module and $method) + { + $getParams = $this->session->{$model.'ExportParams'}; + + if($params) + { + $sourceParams = explode(',', $params); + foreach($sourceParams as $key => $param) + { + if(strpos($param, '$') !== false) $sourceParams[$key] = $getParams[ltrim($param, '$')]; + } + $params = join(',', $sourceParams); + } + + $values = $this->loadModel($module)->$method($params); + if(!empty($pairs)) + { + $valuePairs = array(); + foreach ($values as $key => $value) + { + if(is_object($value)) $value = get_object_vars($value); + + $valuePairs[$key] = $value[$pairs[1]]; + if(!empty($pairs[0])) $valuePairs[$value[$pairs[0]]] = $value[$pairs[1]]; + } + $values = $valuePairs; + } + return $values; + } + + return $this->config->port->fieldList['values']; } - public function initSort($model, $field) + /** + * Init ForeignKey . + * + * @param int $model + * @param int $field + * @param int $fieldContent + * @access public + * @return void + */ + public function initForeignKey($model, $field, $fieldContent) { - return 'no'; + if(strpos($this->config->port->langList, $field) and !$fieldContent['foreignKey'] and isset($this->lang->$model->{$field.'List'})) + { + $fieldContent['foreignKey'] = true; + $fieldContent['values'] = $this->lang->$model->{$field.'List'}; + } + + return $fieldContent; } /** @@ -71,11 +173,14 @@ class portModel extends model return 'no'; } - public function initFixed($model, $field) - { - return 'left'; - } - + /** + * Init Class. + * + * @param int $model + * @param int $field + * @access public + * @return void + */ public function initClass($model, $field) { return ''; diff --git a/module/task/config.php b/module/task/config.php index 7b488a6f5b..59808d7225 100644 --- a/module/task/config.php +++ b/module/task/config.php @@ -38,6 +38,14 @@ $config->task->exportFields = ' lastEditedBy, lastEditedDate, activatedDate, files '; +$config->task->fieldList['module']['control'] = 'select'; +$config->task->fieldList['module']['foreignKey'] = true; +$config->task->fieldList['module']['foreignKeySource'] = array('module' => 'tree', 'method' => 'getTaskOptionMenu', 'params' => 'task'); + +$config->task->fieldList['story']['control'] = 'select'; +$config->task->fieldList['story']['foreignKey'] = true; +$config->task->fieldList['story']['foreignKeySource'] = array('module' => 'story', 'method' => 'getExecutionStories', 'params' => '$executionID', 'pairs' => array('id', 'title')); + $config->task->customCreateFields = 'story,estStarted,deadline,mailto,pri,estimate'; $config->task->customBatchCreateFields = 'module,story,assignedTo,estimate,estStarted,deadline,desc,pri'; $config->task->customBatchEditFields = 'module,assignedTo,status,pri,estimate,record,left,estStarted,deadline,finishedBy,canceledBy,closedBy,closedReason';