Example #1
0
 protected static function analyzeCsv($bytes, $biobank_id, $fileImportedId, $add)
 {
     $import = fopen(CommonTools::data_uri($bytes, 'text/csv'), 'r');
     $row = 1;
     $keysArray = array();
     $listBadSamples = array();
     $newSamples = array();
     //        $tempSaveList = new MongoInsertBatch(Sample::model()->getCollection());
     $tempSaveList = array();
     /**
      * Version 1 : Les champs non repertorés sont ajoutés en notes
      */
     while (($data = fgetcsv($import, 1000, ",")) !== FALSE) {
         /*
          * Traitement de la ligne d'entete
          */
         if ($row == 1) {
             foreach ($data as $key => $value) {
                 if ($value != null && $value != "") {
                     $keysArray[$key] = $value;
                 }
             }
         } else {
             $model = new Sample();
             $model->disableBehavior('LoggableBehavior');
             $model->_id = new MongoId();
             while (!$model->validate(array('_id'))) {
                 $model->_id = new MongoId();
             }
             $model->biobank_id = $biobank_id;
             $model->file_imported_id = $fileImportedId;
             foreach ($keysArray as $key2 => $value2) {
                 if ($value2 != "biobank_id" && $value2 != "file_imported_id") {
                     if (in_array($value2, Sample::model()->attributeNames())) {
                         $model->{$value2} = $data[$key2];
                         if (!$model->validate(array($value2))) {
                             //   Yii::log("Problem with item" . $model->getAttributeLabel($value2) . ",set to null.\n " . implode(", ", $model->errors[$value2]), CLogger::LEVEL_ERROR);
                             $model->{$value2} = null;
                         }
                     } else {
                         $note = new Note();
                         $note->key = $value2;
                         $note->value = $data[$key2];
                         $model->notes[] = $note->attributes;
                     }
                 }
             }
             if (!$model->validate()) {
                 Yii::log("Problem with sample validation " . print_R($model->errors, true), CLogger::LEVEL_ERROR);
                 $listBadSamples[] = $row;
             } else {
                 $tempSaveList[] = $model->attributes;
                 //                        $tempSaveList->add($model->attributes);
                 $newSamples[] = $model->_id;
             }
         }
         $row++;
         if ($row != 2 && $row % 400 == 2) {
             Yii::log("Nb treated : " . $row, 'error');
             Sample::model()->getCollection()->batchInsert($tempSaveList, array());
             $tempSaveList = array();
             //$tempSaveList->execute(array());
             //$tempSaveList = new MongoInsertBatch(Sample::model()->getCollection());
         }
     }
     Sample::model()->getCollection()->batchInsert($tempSaveList, array("w" => 1));
     /*
      * Version 2 : seuls nes champs dont la colonne est annotée avec le préfixe 'notes' sont pris en note
      */
     //        while (($data = fgetcsv($import, 1000, ",")) !== FALSE) {
     //
     //            /*
     //             * Traitement de la ligne d'entete
     //             */
     //
     //
     //
     //            if ($row == 1) {
     //                foreach ($data as $key => $value) {
     //                    if (in_array($value, Sample::model()->attributeNames())) {
     //                        $keysArray[$key] = $value;
     //                    } elseif (substr($value, 0, 5) == 'notes') {
     //                        $keysArray[$key] = $value;
     //                    }
     //                }
     //                /*
     //                 * Traitement des lignes de données
     //                 */
     //            } else {
     //                $model = new Sample();
     //                $model->disableBehavior('LoggableBehavior');
     //                $model->biobank_id = $biobank_id;
     //                $model->file_imported_id = $fileImportedId;
     //                foreach ($keysArray as $key2 => $value2) {
     //                    if (substr($value2, 0, 5) != 'notes') {
     //
     //                        $model->$value2 = $data[$key2];
     //                        if (!$model->validate($value2)) {
     //
     //                            Yii::log("Problem with item" . $model->getAttributeLabel($value2) . ",set to null.", CLogger::LEVEL_ERROR);
     //                            $model->$value2 = null;
     //                        }
     //                    } else {
     //
     //                        $noteKey = end(explode(':', $value2));
     //                        $note = new Note();
     //                        $note->key = $noteKey;
     //                        $note->value = $data[$key2];
     //                        $model->notes[] = $note;
     //                    }
     //                }
     //
     //                if (!$model->save()) {
     //                    $listBadSamples[] = $row;
     //                } else {
     //                    $newSamples[] = $model->_id;
     //                }
     //            }
     //            $row++;
     //        }
     fclose($import);
     if (!$add && count($newSamples) > 0) {
         $deleteCriteria = new EMongoCriteria();
         $deleteCriteria->biobank_id('==', $biobank_id);
         $deleteCriteria->_id('notIn', $newSamples);
         Sample::model()->deleteAll($deleteCriteria);
     }
     if (count($listBadSamples) != 0) {
         $log = '';
         foreach ($listBadSamples as $badSample) {
             $log = 'Error with manual import. File id : ' . $fileImportedId . ' - line : ' . $badSample;
             Yii::log($log, CLogger::LEVEL_ERROR);
         }
     }
     return count($listBadSamples);
 }
Example #2
0
 /**
  * 从cursor游标得到数组
  * 同时获取该记录的操作者
  */
 public static function getRowsFromCursor($e_cursor)
 {
     $rows = array();
     $e_cursor->next();
     $_ids = array();
     $user_ids = array();
     while ($row = $e_cursor->current()) {
         $t = $row->attributes;
         $rows[] = $t;
         $_ids[] = $t['_id'];
         if (isset($t['user'])) {
             if (!is_numeric($t['user'])) {
                 //不是管理员用户
                 $user_ids[] = $t['user'];
             }
         }
         $e_cursor->next();
     }
     $total = count($rows);
     if ($total > 0) {
         $model = $e_cursor->getModel();
         $db_name = $model->getMongoDBComponent()->dbName;
         $c_name = $model->getCollectionName();
         $criteria = new EMongoCriteria();
         $criteria->db_name('==', $db_name);
         $criteria->c_name('==', $c_name);
         $criteria->r_id('in', $_ids);
         $criteria->limit($total);
         $cursor = DbAction::model()->findAll($criteria);
         //var_dump($_ids);exit;
         if ($cursor->count() > 0) {
             $action_info = array();
             $admin_user_ids = array();
             foreach ($cursor as $v) {
                 $_id = (string) $v->r_id;
                 $action = $v->action;
                 $last = count($action) - 1;
                 $admin_user_ids[] = $action[$last]['user'];
                 $action_info[$_id] = array('action_time' => date("Y-m-d H:i", $action[$last]['time']), 'admin_id' => $action[$last]['user'], 'action_log' => isset($action[$last]['action_log']) ? $action[$last]['action_log'] : '');
             }
             $criteria = new EMongoCriteria();
             $criteria->_id('in', $admin_user_ids);
             $user_cursor = User::model()->findAll($criteria);
             $ruser_cursor = RUser::model()->findAll($criteria);
             $admin_names = array();
             foreach ($user_cursor as $v) {
                 $admin_names[$v->_id] = $v->name;
             }
             foreach ($ruser_cursor as $v) {
                 $admin_names[(string) $v->_id] = $v->user_name;
             }
             foreach ($rows as $k => $v) {
                 $_id = (string) $v['_id'];
                 if (isset($action_info[$_id])) {
                     $admin_id = (string) $action_info[$_id]['admin_id'];
                     $admin_user = $admin_names[$admin_id];
                     $rows[$k]['action_user'] = $admin_user;
                     $rows[$k]['action_time'] = $action_info[$_id]['action_time'];
                     $rows[$k]['action_log'] = $action_info[$_id]['action_log'];
                 } else {
                     $rows[$k]['action_user'] = '';
                     $rows[$k]['action_time'] = '';
                     $rows[$k]['action_log'] = '';
                 }
             }
         } else {
             foreach ($rows as $k => $v) {
                 $rows[$k]['action_user'] = '';
                 $rows[$k]['action_time'] = '';
                 $rows[$k]['action_log'] = '';
             }
         }
     }
     return $rows;
 }