public function update($data) { if (!$this->edit) { return false; } if (!$this->_buildFields()) { die("Could not update - fields can not be build"); } $datefmt = $this->userdateformat; $timefmt = $this->usertimeformat; $custom = false; $tableFields = array_keys($this->fields); $rowFields = array_intersect($tableFields, array_keys($data)); $updateFields = array(); $binds = array(); $types = array(); $pk = $this->getPrimaryKeyId(); foreach ($rowFields as $key => $field) { $t = $this->fields[$field]["type"]; $value = $data[$field]; switch ($t) { case 'date': $v = $datefmt != $this->dbdateformat ? jqGridUtils::parseDate($datefmt, $value, $this->dbdateformat) : $value; break; case 'datetime': $v = $timefmt != $this->dbtimeformat ? jqGridUtils::parseDate($timefmt, $value, $this->dbtimeformat) : $value; break; case 'time': $v = jqGridUtils::parseDate($timefmt, $value, 'H:i:s'); break; default: $v = $value; } if ($this->decodeinput) { $v = htmlspecialchars_decode($v); } if ($field != $pk) { $updateFields[] = $field . " = ?"; $binds[] = $v; $types[] = $t; } else { if ($field == $pk) { $v2 = $v; $t2 = $t; } } unset($v); } if (!isset($v2)) { die("Primary value is missing"); } $binds[] = $v2; $types[] = $t2; $result = false; if (count($updateFields) > 0) { $sql = "UPDATE " . $this->table . " SET " . implode(', ', $updateFields) . " WHERE " . $pk . " = ?"; $stmt = $this->parseSql($sql, $binds, false); if ($this->debug) { $this->logQuery($sql, $binds, $types, $data, $this->fields, $this->primaryKey); } if ($stmt) { jqGridDB::bindValues($stmt, $binds, $types); if ($this->trans) { jqGridDB::beginTransaction($this->pdo); $test = jqGridDB::execute($stmt, $binds); if ($test) { $result = jqGridDB::commit($this->pdo); jqGridDB::closeCursor($stmt); } else { jqGridDB::rollBack($this->pdo); $result = false; } } else { $result = jqGridDB::execute($stmt, $binds); jqGridDB::closeCursor($stmt); } } } if ($this->debug) { $this->debugout(); } return $result; }
/** * * Update the data into the database according the table element * A primaryKey should be set. If the key is not set It can be obtained * from jqGridDB::getPrimaryKey * Return true on success, false when the operation is not succefull * @todo possibility to set additional where clause * @param array $data associative array which key values correspond to the * names in the table * @return boolean */ public function update($data) { if (!$this->edit) { return false; } if (!$this->_buildFields()) { return false; } if (!$this->checkPrimary()) { return false; } $datefmt = $this->userdateformat; $timefmt = $this->usertimeformat; $custom = false; $tableFields = array_keys($this->fields); $rowFields = array_intersect($tableFields, array_keys($data)); // Get "col = :col" pairs for the update query $updateFields = array(); $binds = array(); $types = array(); $pk = $this->getPrimaryKeyId(); foreach ($rowFields as $key => $field) { $t = $this->fields[$field]["type"]; $value = $data[$field]; if (strtolower($this->encoding) != 'utf-8') { $value = iconv("utf-8", $this->encoding . "//TRANSLIT", $value); } if (strtolower($value) == 'null') { $v = NULL; } else { switch ($t) { case 'date': $v = $datefmt != $this->dbdateformat ? jqGridUtils::parseDate($datefmt, $value, $this->dbdateformat) : $value; break; case 'datetime': $v = $timefmt != $this->dbtimeformat ? jqGridUtils::parseDate($timefmt, $value, $this->dbtimeformat) : $value; break; case 'time': $v = jqGridUtils::parseDate($timefmt, $value, 'H:i:s'); break; default: $v = $value; } if ($this->decodeinput) { $v = htmlspecialchars_decode($v); } } if ($field != $pk) { $updateFields[] = $field . " = ?"; $binds[] = $v; $types[] = $t; } else { if ($field == $pk) { $v2 = $v; $t2 = $t; } } unset($v); } $result = false; if (!isset($v2)) { $this->errorMessage = "Primary key/value is missing or is not correctly set!"; if ($this->showError) { $this->sendErrorHeader(); } return $result; } $binds[] = $v2; $types[] = $t2; if (count($updateFields) > 0) { // build the statement $sql = "UPDATE " . $this->table . " SET " . implode(', ', $updateFields) . " WHERE " . $pk . " = ?"; // Prepare update query $stmt = $this->parseSql($sql, $binds, false); if ($stmt) { // Bind values to columns jqGridDB::bindValues($stmt, $binds, $types); if ($this->trans) { try { jqGridDB::beginTransaction($this->pdo); $result = $this->_actionsCRUDGrid('edit', 'before'); if ($this->debug) { $this->logQuery($sql, $binds, $types, $data, $this->fields, $this->primaryKey); } if ($result) { $result = jqGridDB::execute($stmt, $binds); } jqGridDB::closeCursor($stmt); if ($result) { $result = $this->_actionsCRUDGrid('edit', 'after'); } if ($result) { $result = jqGridDB::commit($this->pdo); } else { $this->errorMessage = jqGridDB::errorMessage($this->pdo); throw new Exception($this->errorMessage); } } catch (Exception $e) { jqGridDB::rollBack($this->pdo); $result = false; if (!$this->errorMessage) { $this->errorMessage = $e->getMessage(); } } } else { try { $result = $this->_actionsCRUDGrid('edit', 'before'); if ($this->debug) { $this->logQuery($sql, $binds, $types, $data, $this->fields, $this->primaryKey); } if ($result) { $result = jqGridDB::execute($stmt, $binds); } jqGridDB::closeCursor($stmt); if ($result) { $result = $this->_actionsCRUDGrid('edit', 'after'); } if (!$result) { $this->errorMessage = jqGridDB::errorMessage($this->pdo); throw new Exception($this->errorMessage); } } catch (Exception $e) { $result = false; if (!$this->errorMessage) { $this->errorMessage = $e->getMessage(); } } } } else { $this->errorMessage = "Error when preparing a UPDATE statement!"; } } else { $this->errorMessage = "Data posted does not match update fields!"; } if ($this->debug) { $this->debugout(); } if ($this->showError && !$result) { $this->sendErrorHeader(); } return $result; }
public static function _mongoSearch($mongoquery, $GridParams = array(), $encoding = 'utf-8', $datearray = array(), $mongointegers = array()) { $s = ''; $v = array(); $sopt = array('eq' => '===', 'ne' => '!==', 'lt' => '<', 'le' => '<=', 'gt' => '>', 'ge' => '>=', 'bw' => "", 'bn' => "", 'in' => '==', 'ni' => '!=', 'ew' => '', 'en' => '', 'cn' => '', 'nc' => ''); $filters = jqGridUtils::GetParam($GridParams["filter"], ""); $rules = ""; // multiple filter if ($filters) { if (function_exists('json_decode') && strtolower(trim($encoding)) == "utf-8") { $jsona = json_decode($filters, true); } else { $jsona = jqGridUtils::decode($filters); } if (is_array($jsona)) { $gopr = strtolower(trim($jsona['groupOp'])); $rules = $jsona['rules']; } // single filter } else { if (jqGridUtils::GetParam($GridParams['searchField'], '')) { $gopr = 'or'; $rules[0]['field'] = jqGridUtils::GetParam($GridParams['searchField'], ''); $rules[0]['op'] = jqGridUtils::GetParam($GridParams['searchOper'], ''); $rules[0]['data'] = jqGridUtils::GetParam($GridParams['searchString'], ''); } } if ($gopr == 'or') { $gopr = ' || '; } else { $gopr = ' && '; } $i = 0; if (!is_array($mongoquery)) { $mongoquery = array(); } foreach ($rules as $key => $val) { $field = $val['field']; $op = $val['op']; $v = $val['data']; if (strlen($v) != 0 && $op) { $string = true; if (in_array($field, $datearray)) { $av = explode(",", jqGridUtils::parseDate('d/m/Y H:i:s', $v, 'Y,m,d,H,i,s')); $av[1] = (int) $av[1] - 1; $v = "new Date(" . implode(",", $av) . ")"; $string = false; } if (in_array($field, $mongointegers)) { $string = false; } $i++; if ($i > 1) { $s .= $gopr; } switch ($op) { case 'bw': $s .= "this." . $field . ".match(/^{$v}.*\$/i)"; break; case 'bn': $s .= "!this." . $field . ".match(/^{$v}.*\$/i)"; break; case 'ew': $s .= "this." . $field . ".match(/^.*{$v}\$/i)"; break; case 'en': $s .= "!this." . $field . ".match(/^.*{$v}\$/i)"; break; case 'cn': $s .= "this." . $field . ".match(/^.*{$v}.*\$/i)"; break; case 'nc': $s .= "!this." . $field . ".match(/^.*{$v}.*\$/i)"; break; default: if ($string) { $v = "'" . $v . "'"; } $s .= " this." . $field . " " . $sopt[$op] . $v; break; } } } if (isset($mongoquery) && is_array($mongoquery)) { $mongoquery = jqGridUtils::array_extend($mongoquery, array('$where' => "function(){ return " . $s . ";}")); } else { $mongoquery = array('$where' => "function(){ return " . $s . ";}"); } return $mongoquery; }