public function post() { global $auth; //TODO: need to get the user details form the phone $db = new dbConnection(); $res = $db->beginTransaction(); if ($res !== true) { return $res; } //check that the entry doesn't already exist $qry = "SELECT * FROM EntryValue WHERE projectName = '{$this->projectName}' AND formName = '{$this->formName}' AND fieldName = '{$this->form->keyField}' AND value = '{$this->values[$this->form->keyField]}'"; //echo $qry; $num = 0; $res = $db->do_query($qry); if ($res !== true) { return $res; } while ($db->get_row_array()) { $num++; } if ($num > 0) { return "Duplicate Key for {$this->formName} > {$this->form->keyField} > {$this->values[$this->form->keyField]}"; } $uid = 0; //insert basic entry data if (preg_match("/(CHROME|FIREFOX)/i", $_SERVER["HTTP_USER_AGENT"])) { $uid = $auth->getEcUserId(); } else { $sql = "SELECT isUsers FROM user WHERE email = {$_GET['email']}"; } $qry = "INSERT INTO entry (form, projectName, formName, DeviceId, created, uploaded, user) VALUES ({$this->form->id},'{$this->projectName}','{$this->formName}','{$this->deviceId}',{$this->created},now(),{$uid});"; $res = $db->do_query($qry); if ($res !== true) { return $res; } $this->id = $db->last_id(); //$this->fetch(); //get id of entry $qry = "INSERT INTO entryvalue (field, projectName, formName, fieldName, value, entry) "; $ins = array(); foreach (array_keys($this->values) as $key) { if ($this->form->fields[$key]) { if ($this->form->fields[$key]->type == "gps") { array_push($ins, " SELECT {$this->form->fields[$key]->idField}, '{$this->form->survey->name}', '{$this->form->name}', '{$key}', '" . json_encode($this->values[$key]) . "', {$this->id}"); } else { array_push($ins, " SELECT {$this->form->fields[$key]->idField}, '{$this->form->survey->name}', '{$this->form->name}', '{$key}', '{$this->values[$key]}', {$this->id}"); } } else { $res = $db->rollbackTransaction(); echo $res; return "field {$key} is not present in any version of the project definition"; } } $qry .= join(" UNION ", $ins); $res = $db->do_query($qry); if ($res !== true) { $res = $db->commitTransaction(); //echo $res; return $res; } $res = $db->commitTransaction(); //echo $res; return $res; }
public function addToDb() { global $db; if (!$db) { $db = new dbConnection(); } $qry = "SELECT idFieldType FROM fieldtype where name = '{$this->type}'"; $db->do_query($qry); while ($arr = $db->get_row_array()) { $fieldType = $arr["idFieldType"]; } $lbl = $db->escapeArg($this->label); $qry = "INSERT INTO field (form, projectName, formName, type, name, label, language, regex, title, `key`, isinteger, isdouble, active, doubleentry, jump, required, search, group_form, branch_form, display, genkey, upperCase, date, time, setdate, settime, `min`, `max`, `match`, crumb, defaultValue, position, otherFieldProperties) VALUES\n\t\t\t\t\t\t\t\t ({$this->form->id}, '{$this->form->survey->name}', '{$this->form->name}', {$fieldType}, '{$this->name}','{$lbl}', '{$this->language}',"; $qry .= $this->regex != "" ? $db->stringVal($this->regex) . "," : "NULL,"; $qry .= $this->title ? "1," : "0,"; $qry .= $this->key ? "1," : "0,"; $qry .= $this->isInt ? "1," : "0,"; $qry .= $this->isDouble ? "1," : "0,"; $qry .= "1,"; $qry .= $this->doubleEntry ? "1," : "0,"; $qry .= $this->jump ? "'{$this->jump}'," : "NULL,"; $qry .= $this->required ? "1," : "0,"; $qry .= $this->search ? "1," : "0,"; $qry .= $this->group_form ? "'{$this->group_form}'," : "NULL,"; $qry .= $this->branch_form ? "'{$this->branch_form}'," : "NULL,"; $qry .= $this->display ? "1," : "0,"; $qry .= $this->genkey ? "1," : "0,"; $qry .= $this->upperCase ? "1," : "0,"; $qry .= $this->date ? "'{$this->date}'," : "NULL,"; $qry .= $this->time ? "'{$this->time}'," : "NULL,"; $qry .= $this->setDate ? "'{$this->setDate}'," : "NULL,"; $qry .= $this->setTime ? "'{$this->setTime}'," : "NULL,"; $qry .= $this->min || $this->min === '0' ? "{$this->min}," : "NULL,"; $qry .= $this->max || $this->max === '0' ? "{$this->max}," : "NULL,"; $qry .= $this->match ? $db->stringVal($this->match) . ',' : "NULL,"; $qry .= $this->crumb ? "'{$this->crumb}'," : "NULL,"; $qry .= $this->defaultValue || $this->defaultValue === '0' ? $db->stringVal($this->defaultValue) . "," : "NULL,"; $qry .= "{$this->position},"; $qry .= $db->stringVal(json_encode($this->otherAttributes)) . ")"; $res = $db->do_query($qry); if ($res === true) { $this->idField = $db->last_id(); $optcount = count($this->options); if ($optcount > 0) { $optqry = 'INSERT INTO `option` (`index`, `label`, `value`, `field`) VALUES'; //print_r($this->options); for ($x = 0; $x < $optcount; ++$x) { $lab = $db->stringVal($this->options[$x]->label); $val = $db->stringVal($this->options[$x]->value); if ($lab == 'NULL') { throw new Exception(sprintf('The label for option %d of field %s cannot be null.', $x, $this->name)); } if ($val == 'NULL') { throw new Exception(sprintf('The value of option %d of field %s cannot be null.', $x, $this->name)); } $optqry = sprintf('%s%s (%s, %s, %s, %s)', $optqry, $x > 0 ? ',' : '', intval($this->options[$x]->idx), $lab, $val, intval($this->idField)); //$res = $db->exec_sp("addOption", array( // $this->form->survey->name, // $this->form->name, // $this->name, // $opt->idx, // $opt->label, // $opt->value //)); //if($res !== true) return $res; } $res = $db->do_query($optqry); if ($res !== true) { return $res; } } } //echo "$qry\n"; return $res; }