Пример #1
0
 /**
  * 读取集合内的全部数据
  *
  * @author young
  * @name 读取集合内的全部数据
  * @version 2013.12.23 young
  */
 public function indexAction()
 {
     $rst = array();
     $query = array();
     $sort = array();
     $action = $this->params()->fromQuery('action', null);
     $search = $this->params()->fromQuery('search', null);
     $sort = $this->params()->fromQuery('sort', null);
     $start = intval($this->params()->fromQuery('start', 0));
     $limit = intval($this->params()->fromQuery('limit', 10));
     $start = $start > 0 ? $start : 0;
     if ($action == 'search' || $action == 'excel') {
         $query = $this->searchCondition();
     }
     if ($search != null) {
         if (!isset($this->_schema['combobox']['rshCollectionKeyField'])) {
             return $this->msg(false, '关系集合的值');
         }
         $search = preg_replace("/\\s/", '', $search);
         $explode = explode(',', $search);
         $query['$and'][] = array($this->_schema['combobox']['rshCollectionKeyField'] => myMongoRegex(end($explode)));
     }
     $jsonSearch = $this->jsonSearch();
     if ($jsonSearch) {
         $query['$and'][] = $jsonSearch;
     }
     $linkageSearch = $this->linkageSearch();
     if ($linkageSearch) {
         $query['$and'][] = $linkageSearch;
     }
     if (empty($sort)) {
         $sort = $this->defaultOrder();
     }
     $cursor = $this->_data->find($query, $this->_fields);
     $total = $cursor->count();
     if ($total > 0) {
         $cursor->sort($sort);
         if ($action !== 'excel') {
             $cursor->skip($start)->limit($limit);
         }
         $datas = iterator_to_array($cursor, false);
         $datas = $this->comboboxSelectedValues($datas);
         if ($action == 'excel') {
             // 在导出数据的情况下,将关联数据显示为关联集合的显示字段数据
             $this->dealRshData();
             // 结束
             convertToPureArray($datas);
             array_walk($datas, function (&$value, $key) {
                 ksort($value);
                 array_walk($value, function (&$cell, $field) {
                     if (isset($this->_rshData[$field])) {
                         $cell = $this->_rshData[$field][$cell];
                     }
                 });
             });
             $excel = array('title' => array_values($this->_title), 'result' => $datas);
             arrayToExcel($excel);
         }
         return $this->rst($datas, $total, true);
     } else {
         return $this->rst(array(), 0, true);
     }
 }
Пример #2
0
/**
 * 转化mongo db的输出结果为纯数组
 *
 * @param array $arr            
 */
function convertToPureArray(&$arr)
{
    if (!is_array($arr) || empty($arr)) {
        return array();
    }
    foreach ($arr as $key => $value) {
        if (is_array($value)) {
            $arr[$key] = convertToPureArray($value);
        } else {
            if ($value instanceof \MongoId || $value instanceof \MongoInt64 || $value instanceof \MongoInt32) {
                $value = $value->__toString();
            } elseif ($value instanceof \MongoDate || $value instanceof \MongoTimestamp) {
                $value = date("Y-m-d H:i:s", $value->sec);
            }
            $arr[$key] = $value;
        }
    }
    return $arr;
}
Пример #3
0
 /**
  * 规范返回数据的格式为数组
  *
  * @param mixed $rst            
  * @return array
  */
 private function result($rst)
 {
     if (!empty($this->_fileField) && is_array($rst)) {
         array_walk_recursive($rst, function (&$item, $key) {
             if (in_array($key, array_keys($this->_fileField), true) && $this->_fileField[$key]['type'] === 'filefield') {
                 $item = (empty($this->_fileField[$key]['cdnUrl']) ? DOMAIN : $this->_fileField[$key]['cdnUrl']) . '/file/' . $item;
             }
         });
     }
     switch ($this->_serialize) {
         case self::MSGPACK:
             $rst = msgpack_pack(array('result' => convertToPureArray($rst), 'success' => true));
             break;
         case self::JSON:
             $rst = json_encode(array('result' => convertToPureArray($rst), 'success' => true));
             break;
         default:
             $rst = serialize(array('result' => $rst, 'success' => true));
             break;
     }
     return $rst;
 }
Пример #4
0
 /**
  * 导出数据
  */
 public function exportAction()
 {
     try {
         $cache = $this->cache();
         $this->_worker->addFunction("dataExport", function (\GearmanJob $job) use($cache) {
             $job->handle();
             $workload = $job->workload();
             $params = unserialize($workload);
             $scope = $params['scope'];
             $collection_id = $params['collection_id'];
             $query = $params['query'];
             $fields = $params['fields'];
             $exportKey = md5($workload);
             $exportGearmanKey = md5($scope->_collection_id . serialize($query));
             // 获取映射关系,初始化数据集合model
             $mapCollection = $this->_mapping->findOne(array('project_id' => $scope->_project_id, 'collection_id' => $scope->_collection_id, 'active' => true));
             if ($mapCollection != null) {
                 $this->_data->setCollection($mapCollection['collection'], $mapCollection['database'], $mapCollection['cluster']);
             } else {
                 $this->_data->setCollection(iCollectionName($collection_id));
             }
             $this->_data->setReadPreference(\MongoClient::RP_SECONDARY_PREFERRED);
             $cursor = $this->_data->find($query, $fields);
             $excelDatas = array();
             // 保持拥有全部的字段名,不存在错乱的想象
             $fieldNames = array_keys($fields);
             while ($cursor->hasNext()) {
                 $row = $cursor->getNext();
                 $tmp = array();
                 foreach ($fieldNames as $key) {
                     $tmp[$key] = isset($row[$key]) ? $row[$key] : '';
                 }
                 $excelDatas[] = $tmp;
                 unset($tmp);
             }
             // 在导出数据的情况下,将关联数据显示为关联集合的显示字段数据
             $rshData = array();
             foreach ($scope->_rshCollection as $_id => $detail) {
                 $_id = $this->getCollectionIdByAlias($scope->_project_id, $_id);
                 $model = $this->collection()->secondary(iCollectionName($_id));
                 $cursor = $model->find(array(), array($detail['rshCollectionKeyField'] => true, $detail['rshCollectionValueField'] => true));
                 $datas = array();
                 while ($cursor->hasNext()) {
                     $row = $cursor->getNext();
                     $key = $row[$detail['rshCollectionValueField']];
                     $value = isset($row[$detail['rshCollectionKeyField']]) ? $row[$detail['rshCollectionKeyField']] : '';
                     if ($key instanceof \MongoId) {
                         $key = $key->__toString();
                     }
                     if (!empty($key)) {
                         $datas[$key] = $value;
                     }
                 }
                 $rshData[$detail['collectionField']] = $datas;
             }
             // 结束
             convertToPureArray($excelDatas);
             array_walk($excelDatas, function (&$value, $key) use($rshData, $fields) {
                 $loop = function ($value, $tmp) {
                     $new = $value;
                     $len = count($tmp);
                     for ($i = 0; $i < $len; $i++) {
                         if (isset($new[$tmp[$i]])) {
                             $new = $new[$tmp[$i]];
                         } else {
                             return '';
                         }
                     }
                     return $new;
                 };
                 foreach ($fields as $k => $v) {
                     if (strpos($k, '.') !== false) {
                         $tmp = explode('.', $k);
                         $value[$k] = $loop($value, $tmp);
                     }
                 }
                 ksort($value);
                 array_walk($value, function (&$cell, $field) use($rshData) {
                     if (isset($rshData[$field])) {
                         $cell = isset($rshData[$field][$cell]) ? $rshData[$field][$cell] : '';
                     }
                 });
             });
             $title = array();
             ksort($fields);
             foreach (array_keys($fields) as $field) {
                 $title[] = isset($scope->_title[$field]) ? $scope->_title[$field] : $field;
             }
             $excel = array('title' => $title, 'result' => $excelDatas);
             $temp = tempnam(sys_get_temp_dir(), 'gearman_export_');
             arrayToExcel($excel, $exportKey, $temp);
             $cache->save(file_get_contents($temp), $exportKey, 60);
             unlink($temp);
             $cache->remove($exportGearmanKey);
             $job->sendComplete('complete');
         });
         while ($this->_worker->work()) {
             if ($this->_worker->returnCode() != GEARMAN_SUCCESS) {
                 echo "return_code: " . $this->_worker->returnCode() . "\n";
             }
         }
         return $this->response;
     } catch (\Exception $e) {
         var_dump(exceptionMsg($e));
         $job->sendException(exceptionMsg($e));
         return false;
     }
 }
Пример #5
0
 /**
  * 替换数据
  *
  * @param array $datas            
  * @param array $map            
  * @return array
  */
 private function replaceRshData($datas, $map)
 {
     $this->dealRshData();
     convertToPureArray($datas);
     array_walk($datas, function (&$value, $key) use($map) {
         ksort($value);
         array_walk($value, function (&$cell, $field) use($map) {
             if (isset($map[$field])) {
                 $field = $map[$field];
                 if (isset($this->_rshData[$field])) {
                     $cell = isset($this->_rshData[$field][$cell]) ? $this->_rshData[$field][$cell] : '';
                 }
             }
         });
     });
     return $datas;
 }