示例#1
0
 public function parse($xml, $edit = false)
 {
     global $XML_VERSION;
     $root = simplexml_load_string($xml);
     foreach ($root->attrubutes as $name => $val) {
         if ($name == 'version') {
             $ecv = doubleval($val);
             if ($ecv <= $XML_VERSION) {
                 $this->ecml_version = $ecv;
             } else {
                 throw new Exception(sprintf('This version of the server will only handle XML version %s or earlier.', $XML_VERSION));
             }
         }
     }
     $model = $root->model[0];
     if ($model->uploadToLocalServer) {
         $this->uploadToLocalServer = (string) $model->uploadToLocalServer[0];
     }
     if ($model->downloadFromLocalServer) {
         $this->downloadFromLocalServer = (string) $model->downloadFromLocalServer[0];
     }
     $adeIsSet = false;
     foreach ($model->submission[0]->attributes() as $name => $val) {
         switch ($name) {
             case "id":
                 $this->submission_id = (string) $val;
                 break;
             case "projectName":
                 $this->name = (string) $val;
                 break;
             case "allowDownloadEdits":
                 try {
                     $this->allowDownloadEdits = parseBool((string) $val);
                     $adeIsSet = true;
                 } catch (Exception $e) {
                     throw new InvalidArgumentException("allowDownloadEdits must be true or false");
                 }
                 break;
             case "versionNumber":
                 $this->versionNumber = (string) $val;
                 break;
         }
     }
     if (!$this->submission_id || $this->submission_id == '_' || $this->submission_id == '') {
         $this->submission_id = strtolower($this->name);
     }
     if (!$adeIsSet) {
         throw new Exception("allowDownloadEdits must be set for every project.");
     }
     //check the version of the xml (version 1 does not contain table tags)
     if ($root->description) {
         $this->description = (string) $root->description[0];
     }
     //Clear table array to prevent discrepancy
     $this->tables = array();
     if ($root->form) {
         $this->ecVersionNumber = "3";
         for ($t = 0; $t < count($root->form); $t++) {
             $atts = $root->form[$t]->attributes();
             if (!array_key_exists((string) $atts['name'], $this->tables)) {
                 $tbl = new EcTable($this);
             } elseif ($this->tables[(string) $atts['name']]->id) {
                 $oldTbl = $this->tables[(string) $atts['name']];
                 //unset($this->tables[(string)$atts['name']]);
                 $tbl = new EcTable($this);
                 $tbl->id = $oldTbl->id;
                 foreach ($oldTbl->fields as $name => $fld) {
                     $tbl->fields[$name] = new EcField();
                     $tbl->fields[$name]->idField = $fld->idField;
                 }
                 unset($oldTbl);
             } else {
                 throw new Exception("Table names must be unique. More that one table called " . (string) $atts['name'] . " in {$this->name}");
                 //$tbl = $this->tables[(string)$atts['name']];
             }
             $tbl->parse($root->form[$t]);
             $this->tables[$tbl->name] = $tbl;
         }
     } elseif ($root->table) {
         //parse version 2 tables
         $this->ecVersionNumber = "2";
         if ($model->uploadToLocalServer) {
             $this->uploadToLocalServer = (string) $model->uploadToLocalServer[0];
         }
         for ($t = 0; $t < count($root->table); $t++) {
             if (!array_key_exists((string) $root->table[$t]->name, $this->tables) || $this->tables[(string) $root->table[$t]->name]->id) {
                 $tbl = new EcTable($this);
             } else {
                 throw new Exception("Table names must be unique. More that one table called " . (string) $root->table[$t]->name . "in {$this->name}");
                 //$tbl = $this->tables[(string)$atts['name']];
             }
             $tbl->parse($root->table[$t]);
             $tbl->version = $this->versionNumber;
             $this->tables[$tbl->name] = $tbl;
         }
     } else {
         //parse version 1 table
         $this->ecVersionNumber = "1";
         $tbl = new EcTable($this);
         $tbl->parse($root);
         $tbl->projectName = $this->name;
         $this->tables[$this->name] = $tbl;
         foreach ($tbl->fields as $fld) {
             if ($fld->title) {
                 $this->tables[$this->name]->key = $fld->name;
                 break;
             }
         }
     }
     $this->uploadToServer = (string) $model->uploadToServer[0];
     foreach ($this->tables as $t) {
         if (!$t->isMain) {
             continue;
         }
         $tn = $this->getNextTable($t->name, true);
         if ($tn && !array_key_exists($t->key, $tn->fields)) {
             $f = new EcField();
             $f->name = $t->fields[$t->key]->name;
             $f->label = $t->fields[$t->key]->label;
             $f->form = $tn;
             $f->type = 'input';
             $f->fkTable = $t->name;
             $f->fkField = $t->key;
             $tn->fields[$f->name] = $f;
         }
     }
 }
示例#2
0
 public function parse($xml)
 {
     $root = simplexml_load_string($xml);
     $model = $root->model[0];
     foreach ($model->submission[0]->attributes() as $name => $val) {
         switch ($name) {
             case "id":
                 $this->submission_id = (string) $val;
                 break;
             case "projectName":
                 $this->name = (string) $val;
                 break;
             case "allowDownloadEdits":
                 $this->allowDownloadEdits = (string) $val == "true";
                 break;
             case "versionNumber":
                 $this->versionNumber = (string) $val;
                 break;
         }
     }
     //check the version of the xml (version 1 does not contain table tags)
     if ($root->form) {
         $this->ecVersionNumber = "3";
         if ($model->uploadToLocalServer) {
             $this->uploadToLocalServer = (string) $model->uploadToLocalServer[0];
         }
         for ($t = 0; $t < count($root->form); $t++) {
             $atts = $root->form[$t]->attributes();
             if (!array_key_exists((string) $atts['name'], $this->tables)) {
                 $tbl = new EcTable($this);
             } else {
                 $tbl = $this->tables[(string) $atts['name']];
             }
             $tbl->parse($root->form[$t]);
             $this->tables[$tbl->name] = $tbl;
         }
     } elseif ($root->table) {
         //parse version 2 tables
         $this->ecVersionNumber = "2";
         if ($model->uploadToLocalServer) {
             $this->uploadToLocalServer = (string) $model->uploadToLocalServer[0];
         }
         for ($t = 0; $t < count($root->table); $t++) {
             if (!array_key_exists((string) $root->table[$t]->name, $this->tables)) {
                 $tbl = new EcTable($this);
             } else {
                 $tbl = $this->tables[$root->table[$t]->name];
             }
             $tbl->parse($root->table[$t]);
             $tbl->version = $this->versionNumber;
             $this->tables[$tbl->name] = $tbl;
         }
     } else {
         //parse version 1 table
         $this->ecVersionNumber = "1";
         $tbl = new EcTable($this);
         $tbl->parse($root);
         $tbl->projectName = $this->name;
         $this->tables[$this->name] = $tbl;
     }
     $this->uploadToServer = (string) $model->uploadToServer[0];
 }
示例#3
0
 public static function postEntries($entries)
 {
     global $db;
     $qry = 'INSERT INTO entry (form, projectName, formName, DeviceId, created, uploaded, user, bulk_insert_key) VALUES ';
     $len = count($entries);
     $sessId = session_id();
     $prj = new EcProject();
     $prj->name = $entries[0]->projectName;
     $prj->fetch();
     $keyfield = $prj->tables[$entries[0]->formName]->key;
     for ($i = 0; $i < $len; ++$i) {
         if (!$entries[$i]->created || $entries[$i]->created == "NULL") {
             $entries[$i]->created = getTimestamp();
         } else {
             if (!is_numeric($entries[$i]->created)) {
                 $entries[$i]->created = EcTable::unformatCreated($entries[$i]->created);
             }
         }
         if ($prj->tables[$entries[$i]->formName]->checkExists($entries[$i]->values[$keyfield])) {
             throw new Exception(sprintf('Your data could not be uploaded, there was a duplicate key for entry %s on line %s of your CSV file', $entries[$i]->values[$keyfield], $i + 2));
         }
         $entries[$i]->insert_key = sprintf('%s%s', $sessId, $i);
         $qry .= sprintf('%s (%s, %s, %s, %s, %s, \'%s\', 0, \'%s\')', $i > 0 ? ',' : '', $entries[$i]->form->id, $db->stringVal($entries[$i]->projectName), $db->stringVal($entries[$i]->formName), $db->stringVal($entries[$i]->deviceId), $entries[$i]->created, getTimestamp("Y-m-d H:i:s"), $entries[$i]->insert_key);
         //echo $_SERVER['REQUEST_TIME'] . '<br />\r\n';
     }
     $res = $db->do_query($qry);
     if ($res !== true) {
         die($res);
     }
     $qry = sprintf('SELECT bulk_insert_key, idEntry FROM  entry where bulk_insert_key Like \'%s%%\'', $sessId);
     $res = $db->do_query($qry);
     if ($res !== true) {
         die($res);
     }
     $insert_keys = array();
     while ($arr = $db->get_row_array()) {
         $insert_keys[$arr["bulk_insert_key"]] = $arr["idEntry"];
     }
     $qry = 'INSERT INTO entryvalue (field, projectName, formName, fieldName, value, entry) VALUES ';
     for ($i = 0; $i < $len; ++$i) {
         if (trim($entries[$i]->values[$entries[$i]->form->key]) == '') {
             return 'Key values cannot be blank';
         }
         $keys = array_keys($entries[$i]->values);
         $length = count($keys);
         for ($j = 0; $j < $length; ++$j) {
             if ($entries[$i]->form->fields[$keys[$j]]) {
                 if (($entries[$i]->form->fields[$keys[$j]]->type == 'gps' || $entries[$i]->form->fields[$keys[$j]]->type == 'location') && !is_string($entries[$i]->values[$keys[$j]])) {
                     $qry .= sprintf('%s ( %s, \'%s\', \'%s\', \'%s\', %s, %s )', $i > 0 || $j > 0 ? ',' : '', $entries[$i]->form->fields[$keys[$j]]->idField, $entries[$i]->form->survey->name, $entries[$i]->form->name, $keys[$j], $db->stringVal(json_encode($entries[$i]->values[$keys[$j]])), $insert_keys[$entries[$i]->insert_key]);
                 } else {
                     $qry .= sprintf('%s ( %s, \'%s\', \'%s\', \'%s\', %s, %s)', $i > 0 || $j > 0 ? ',' : '', $entries[$i]->form->fields[$keys[$j]]->idField, $entries[$i]->form->survey->name, $entries[$i]->form->name, $keys[$j], $db->stringVal($entries[$i]->values[$keys[$j]]), $insert_keys[$entries[$i]->insert_key]);
                 }
             }
         }
     }
     $res = $db->do_query($qry);
     if ($res !== true) {
         die($res);
     }
     $qry = sprintf('UPDATE entry SET  bulk_insert_key = NULL WHERE bulk_insert_key Like \'%s%%\'', $sessId);
     $res = $db->do_query($qry);
     return $res;
 }