public function newEvent($start, $end, $title, $description, $location, $categories, $access, $allDay) { if (!empty($this->user_id) && !empty($this->table)) { jqGridDB::beginTransaction($this->db); $query = jqGridDB::prepare($this->db, "INSERT INTO " . $this->table . "\t(user_id, start, end, title, description, location, categories, access, all_day)\n\t\t\tVALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", array($this->user_id, $start, $end, $title, $description, $location, $categories, $access, $allDay)); jqGridDB::execute($query); $lastid = jqGridDB::lastInsertId($this->db, $this->table, 'event_id', $this->dbtype); jqGridDB::commit($this->db); jqGridDB::closeCursor($query); return $lastid; } else { return false; } }
/** * * Insert the data array into the database according to the table element. * A primaryKey should be set. If the key is not set It can be obtained * from jqGridDB::getPrimaryKey * Return true on succes, false otherwiese. * @todo in the future we should return the last insert id from the table * @param array $data associative array which key values correspond to the * names in the table. * @return boolean */ public function insert($data) { if (!$this->add) { return false; } if (!$this->_buildFields()) { return false; } if (!$this->checkPrimary()) { return false; } $datefmt = $this->userdateformat; $timefmt = $this->usertimeformat; if ($this->serialKey) { unset($data[$this->getPrimaryKeyId()]); } $tableFields = array_keys($this->fields); $rowFields = array_intersect($tableFields, array_keys($data)); // Get "col = :col" pairs for the update query $insertFields = array(); $binds = array(); $types = array(); $v = ''; foreach ($rowFields as $key => $val) { $insertFields[] = "?"; //$field; $t = $this->fields[$val]["type"]; $value = $data[$val]; 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); } } $types[] = $t; $binds[] = $v; unset($v); } $result = false; if (count($insertFields) > 0) { // build the statement $sql = "INSERT INTO " . $this->table . " (" . implode(', ', $rowFields) . ")" . " VALUES( " . implode(', ', $insertFields) . ")"; // Prepare insert query $stmt = $this->parseSql($sql, $binds, false); if ($stmt) { // Bind values to columns jqGridDB::bindValues($stmt, $binds, $types); // Execute if ($this->trans) { try { jqGridDB::beginTransaction($this->pdo); $result = $this->_actionsCRUDGrid('add', 'before'); if ($this->debug) { $this->logQuery($sql, $binds, $types, $data, $this->fields, $this->primaryKey); } if ($result) { $result = jqGridDB::execute($stmt, $binds); } if ($result) { if ($this->serialKey && $this->getLastInsert) { $this->lastId = jqGridDB::lastInsertId($this->pdo, $this->table, $this->primaryKey, $this->dbtype); if (!is_numeric($this->lastId)) { $result = false; } } } if ($result) { $saver = $this->showError; $this->showError = false; $result = $this->_actionsCRUDGrid('add', 'after'); $this->showError = $saver; } if ($result) { $result = jqGridDB::commit($this->pdo); } jqGridDB::closeCursor($stmt); if (!$result) { $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('add', '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 ($this->serialKey && $this->getLastInsert && $result) { $this->lastId = jqGridDB::lastInsertId($this->pdo, $this->table, $this->primaryKey, $this->dbtype); if (!is_numeric($this->lastId)) { $result = false; } } if ($result) { $result = $this->_actionsCRUDGrid('add', '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 INSERT statement!"; $result = false; } } else { $this->errorMessage = "Data posted does not match insert fields!"; $result = false; } if ($this->debug) { $this->debugout(); } if ($this->showError && !$result) { $this->sendErrorHeader(); } return $result; }
public function newEvent($data = array()) { //$start, $end, $title, $description, $location, $categories, $access, $allDay) { if (!empty($this->user_id) && !empty($this->table)) { if (!isset($data['user_id'])) { return false; } if (is_array($this->user_id)) { if (!in_array($data['user_id'], $this->user_id)) { return false; } } else { if ($data['user_id'] != $this->user_id) { return false; } } $tableFields = array_keys($this->dbmap); $binds = array(); unset($tableFields['event_id']); $rowFields = array_intersect($tableFields, array_keys($data)); foreach ($rowFields as $key => $val) { $insertFields[] = "?"; //$field; $value = $data[$val]; if (strtolower($this->encoding) != 'utf-8') { $value = iconv("utf-8", $this->encoding . "//TRANSLIT", $value); } $binds[] = $value; } $sql = ""; if (count($insertFields) > 0) { $sql = "INSERT INTO " . $this->table . " (" . implode(', ', $rowFields) . ")" . " VALUES( " . implode(', ', $insertFields) . ")"; } if (!$sql) { return false; } jqGridDB::beginTransaction($this->db); $query = jqGridDB::prepare($this->db, $sql, $binds); jqGridDB::execute($query); $lastid = jqGridDB::lastInsertId($this->db, $this->table, 'event_id', $this->dbtype); jqGridDB::commit($this->db); jqGridDB::closeCursor($query); return $lastid; } else { return false; } }