/** * GetKeysArray * Form aray of primary keys and their values for audit * @param {array} $arr array of inserting values * @param {bool} $searchId - find last inserted id or not * @return {array} array of keys and their values */ function GetKeysArray($arr, $searchId = false) { global $conn; $keyfields = GetTableKeys(); $aKeys = array(); if (count($keyfields)) { foreach ($keyfields as $kfield) { if (array_key_exists($kfield, $arr)) { $aKeys[$kfield] = $arr[$kfield]; } } if (count($aKeys) == 0 && searchId) { $lastId = db_insertid($conn); if ($lastId > 0) { $aKeys[$keyfields[0]] = $lastId; } } } return $aKeys; }
function DoInsertRecordSQL($table, &$avalues, &$blobfields, $pageid, &$pageObject) { global $error_happened, $conn, $inlineadd, $usermessage, $message, $failed_inline_add, $keys, $strTableName; // make SQL string $strSQL = "insert into " . AddTableWrappers($table) . " "; $strFields = "("; $strValues = "("; $blobs = PrepareBlobs($avalues, $blobfields); foreach ($avalues as $akey => $value) { $strFields .= GetFullFieldName($akey) . ", "; if (in_array($akey, $blobfields)) { $strValues .= $value . ", "; } else { $strValues .= add_db_quotes($akey, $value) . ", "; } } if (substr($strFields, -2) == ", ") { $strFields = substr($strFields, 0, strlen($strFields) - 2); } if (substr($strValues, -2) == ", ") { $strValues = substr($strValues, 0, strlen($strValues) - 2); } $strSQL .= $strFields . ") values " . $strValues . ")"; if (!ExecuteUpdate($strSQL, $blobs, true)) { return false; } if ($error_happened) { return false; } $pageObject->ProcessFiles(); if ($inlineadd == ADD_INLINE) { $status = "ADDED"; $message = "" . mlang_message("RECORD_ADDED") . ""; $IsSaved = true; } else { $message = "<<< " . mlang_message("RECORD_ADDED") . " >>>"; } if ($usermessage != "") { $message = $usermessage; } $auditObj = GetAuditObject($table); if ($inlineadd == ADD_SIMPLE || $inlineadd == ADD_INLINE || $inlineadd == ADD_ONTHEFLY || $inlineadd == ADD_POPUP || $inlineadd == ADD_MASTER || tableEventExists("AfterAdd", $strTableName) || $auditObj) { $failed_inline_add = false; $keyfields = GetTableKeys(); foreach ($keyfields as $k) { if (array_key_exists($k, $avalues)) { $keys[$k] = $avalues[$k]; } elseif (IsAutoincField($k)) { $lastrs = db_query("select @@IDENTITY", $conn); if ($lastdata = db_fetch_numarray($lastrs)) { $keys[$k] = $lastdata[0]; } } else { $failed_inline_add = true; } } } return true; }
function getNextPrevRecordKeys(&$data, $securityMode, &$next, &$prev) { global $conn; $next = array(); $prev = array(); if (@$_SESSION[$this->sessionPrefix . "_noNextPrev"]) { return; } $prevExpr = ""; $nextExpr = ""; $where_next = ""; $where_prev = ""; $order_next = ""; $order_prev = ""; $arrFieldForSort = array(); $arrHowFieldSort = array(); $query = GetQueryObject($this->tName); $where = $_SESSION[$this->sessionPrefix . "_where"]; if (!strlen($where)) { $where = SecuritySQL($securityMode); } $having = $_SESSION[$this->sessionPrefix . "_having"]; $orderindexes = GetTableData($this->tName, ".orderindexes", array()); $strOrderBy = GetTableData($this->tName, ".strOrderBy", ''); // calc indexes of the key fields $keyIndexes = array(); $tKeys = GetTableKeys($this->tName); foreach ($tKeys as $k) { $ki = GetFieldIndex($k, $this->tName); if ($ki) { $keyIndexes[] = $ki; } } if (!count($keyIndexes)) { $_SESSION[$this->sessionPrefix . "_noNextPrev"] = 1; return; } // get current ordering info if (@$_SESSION[$this->sessionPrefix . "_arrFieldForSort"] && @$_SESSION[$this->sessionPrefix . "_arrHowFieldSort"]) { $arrFieldForSort = $_SESSION[$this->sessionPrefix . "_arrFieldForSort"]; $arrHowFieldSort = $_SESSION[$this->sessionPrefix . "_arrHowFieldSort"]; } else { // get default order if (count($orderindexes)) { for ($i = 0; $i < count($orderindexes); $i++) { $arrFieldForSort[] = $orderindexes[$i][0]; $arrHowFieldSort[] = $orderindexes[$i][1]; } } elseif ($strOrderBy != '') { $_SESSION[$this->sessionPrefix . "_noNextPrev"] = 1; return; } // add key fields for ($i = 0; $i < count($keyIndexes); $i++) { if (array_search($keyIndexes[$i], $arrFieldForSort) === false) { $arrFieldForSort[] = $keyIndexes[$i]; $arrHowFieldSort[] = "ASC"; } } $_SESSION[$this->sessionPrefix . "_arrFieldForSort"] = $arrFieldForSort; $_SESSION[$this->sessionPrefix . "_arrHowFieldSort"] = $arrHowFieldSort; } if (!count($arrFieldForSort)) { $_SESSION[$this->sessionPrefix . "_noNextPrev"] = 1; return; } // make next & prev ORDER BY strings for ($i = 0; $i < count($arrFieldForSort); $i++) { if (!GetFieldByIndex($arrFieldForSort[$i], $this->tName)) { continue; } if ($order_next == "") { $order_next = " ORDER BY "; $order_prev = " ORDER BY "; } else { $order_next .= ","; $order_prev .= ","; } $order_next .= $arrFieldForSort[$i] . " " . $arrHowFieldSort[$i]; $order_prev .= $arrFieldForSort[$i] . " " . ($arrHowFieldSort[$i] == "DESC" ? "ASC" : "DESC"); } // make next & prev where expressions $tail = ""; for ($i = 0; $i < count($arrFieldForSort); $i++) { $fieldName = GetFieldByIndex($arrFieldForSort[$i], $this->tName); if (!$fieldName) { continue; } if (!$query->HasGroupBy()) { $fullName = GetFullFieldName($fieldName, $this->tName); } else { $fullName = $fieldName; } $asc = $arrHowFieldSort[$i] == "ASC"; if (!is_null($data[$fieldName])) { // current field value is not null $value = make_db_value($fieldName, $data[$fieldName], "", "", $this->tName); $nextop = $asc ? ">" : "<"; $prevop = $asc ? "<" : ">"; $nextExpr = $fullName . $nextop . $value; $prevExpr = $fullName . $prevop . $value; if ($nextop == "<") { $nextExpr .= " or " . $fullName . " IS NULL"; } else { $prevExpr .= " or " . $fullName . " IS NULL"; } if ($i < count($arrFieldForSort) - 1) { $nextExpr .= " or " . $fullName . "=" . $value; $prevExpr .= " or " . $fullName . "=" . $value; } } else { // current field value is null if ($asc) { $nextExpr = $fullName . " IS NOT NULL"; } else { $prevExpr = $fullName . " IS NOT NULL"; } if ($i < count($arrFieldForSort) - 1) { if ($nextExpr != "") { $nextExpr .= " or "; } $nextExpr .= $fullName . " IS NULL"; if ($prevExpr != "") { $prevExpr .= " or "; } $prevExpr .= $fullName . " IS NULL"; } } if ($nextExpr == "") { $nextExpr = " 1=0 "; } if ($prevExpr == "") { $prevExpr = " 1=0 "; } // append expression to where clause if ($i > 0) { $where_next .= " AND "; $where_prev .= " AND "; } $where_next .= "(" . $nextExpr; $where_prev .= "(" . $prevExpr; $tail .= ")"; } $where_next = $where_next . $tail; $where_prev = $where_prev . $tail; if ($where_next == "" or $order_next == "" or $where_prev == "" or $order_prev == "") { $_SESSION[$this->sessionPrefix . "_noNextPrev"] = 1; return; } // make the resulting query if ($query === null) { return; } if (!$query->HasGroupBy()) { $oWhere = $query->Where(); $where = whereAdd($where, $oWhere->toSql($query)); $where_next = whereAdd($where_next, $where); $where_prev = whereAdd($where_prev, $where); $query->ReplaceFieldsWithDummies(GetBinaryFieldsIndices($this->tName)); $sql_next = $query->toSql($where_next, $order_next); $sql_prev = $query->toSql($where_prev, $order_prev); } else { $oWhere = $query->Where(); $oHaving = $query->Having(); $where = whereAdd($where, $oWhere->toSql($query)); $having = whereAdd($having, $oHaving->toSql($query)); $query->ReplaceFieldsWithDummies(GetBinaryFieldsIndices($this->tName)); $sql = "select * from (" . $query->toSql($where, "", $having) . ") prevnextquery"; $sql_next = $sql . " WHERE " . $where_next . $order_next; $sql_prev = $sql . " WHERE " . $where_prev . $order_prev; } // add record count options $sql_next = AddTop($sql_next, 1); $sql_prev = AddTop($sql_prev, 1); $res_next = db_query($sql_next, $conn); if ($res_next) { if ($row_next = db_fetch_array($res_next)) { foreach ($tKeys as $i => $k) { $next[$i + 1] = $row_next[$k]; } } db_closequery($res_next); } $res_prev = db_query($sql_prev, $conn); if ($row_prev = db_fetch_array($res_prev)) { foreach ($tKeys as $i => $k) { $prev[$i + 1] = $row_prev[$k]; } } db_closequery($res_prev); }
/** * Fills list grid.This method use many other methods * */ function fillGridData() { $totals = array(); // fill $rowinfo array $rowinfo = array(); $this->fillGridShowInfo($rowinfo); // add grid data $rowClass = false; $data = $this->beforeProccessRow(); $lockRecIds = array(); $this->googleMapCfg['viewLinkBase'] = $this->shortTableName . "_view.php?"; $tKeys = GetTableKeys($this->tName); $this->controlsMap['gridRows'] = array(); while ($data && ($this->recNo <= $this->pageSize || $this->pageSize == -1)) { $row = array(); if (!$this->isVerLayout) { $row["rowclass"] = ""; if (!$rowClass) { $row["rowclass"] .= "interlaced"; $rowClass = true; } else { $rowClass = false; } } $row["grid_record"] = array(); $row["grid_record"]["data"] = array(); $this->rowId++; for ($col = 1; $data && ($this->recNo <= $this->pageSize || $this->pageSize == -1) && $col <= $this->colsOnPage; $col++) { $this->countTotals($totals, $data); $record = array(); $this->genId(); $row["rowattrs"] = " id=\"gridRow" . $this->recId . "\""; $gridRowInd = count($this->controlsMap['gridRows']); $this->controlsMap['gridRows'][$gridRowInd]['id'] = $this->recId; $this->controlsMap['gridRows'][$gridRowInd]['rowInd'] = $gridRowInd; $this->controlsMap['gridRows'][$gridRowInd]['isEditOwnRow'] = $this->permis[$this->tName]['edit'] && CheckSecurity($data[$this->mainTableOwnerID], "Edit"); for ($i = 0; $i < count($tKeys); $i++) { $this->controlsMap['gridRows'][$gridRowInd]['keys'][$tKeys[$i]] = $data[$tKeys[$i]]; } $record["edit_link"] = $this->permis[$this->tName]['edit'] && CheckSecurity($data[$this->mainTableOwnerID], "Edit"); $record["inlineedit_link"] = $this->permis[$this->tName]['edit'] && CheckSecurity($data[$this->mainTableOwnerID], "Edit"); $record["view_link"] = $this->permis[$this->tName]['search']; $record["copy_link"] = $this->permis[$this->tName]['add']; //for list icons instead of list links if ($col == 1) { $this->countWidthListIcons(''); } //get record id for locking record if ($this->lockingObj) { if ($this->mode == LIST_SIMPLE && !count($this->lockDelRec) && isset($_SESSION[$this->sessionPrefix . "_lockDelRec"])) { $this->lockDelRec = $_SESSION[$this->sessionPrefix . "_lockDelRec"]; unset($_SESSION[$this->sessionPrefix . "_lockDelRec"]); } for ($i = 0; $i < count($this->lockDelRec); $i++) { $lockDelRec = true; foreach ($this->lockDelRec[$i] as $key => $val) { if ($data[$key] != $val) { $lockDelRec = false; break; } } if ($lockDelRec) { $lockRecIds[] = $this->recId; break; } } } // detail tables $this->proccessDetailGridInfo($record, $data, $gridRowInd); // key fields $keyblock = ""; $editlink = ""; $copylink = ""; $keylink = ""; for ($i = 0; $i < count($tKeys); $i++) { if ($i != 0) { $keyblock .= "&"; $editlink .= "&"; $copylink .= "&"; } $keyblock .= rawurlencode($data[$tKeys[$i]]); $editlink .= "editid" . ($i + 1) . "=" . htmlspecialchars(rawurlencode($data[$tKeys[$i]])); $copylink .= "copyid" . ($i + 1) . "=" . htmlspecialchars(rawurlencode($data[$tKeys[$i]])); $keylink .= "&key" . ($i + 1) . "=" . htmlspecialchars(rawurlencode(@$data[$tKeys[$i]])); } $record["editlink_attrs"] = "id=\"editLink" . $this->recId . "\" name=\"editLink" . $this->recId . "\" href='" . $this->shortTableName . "_edit.php?" . $editlink . "'"; $record["inlineeditlink_attrs"] = "id=\"iEditLink" . $this->recId . "\" name=\"iEditLink" . $this->recId . "\" href='" . $this->shortTableName . "_edit.php?" . $editlink . "'"; $record["copylink_attrs"] = "id=\"copyLink" . $this->recId . "\" name=\"copyLink" . $this->recId . "\" href='" . $this->shortTableName . "_add.php?" . $copylink . "'"; $record["viewlink_attrs"] = "id=\"viewLink" . $this->recId . "\" name=\"viewLink" . $this->recId . "\" href='" . $this->shortTableName . "_view.php?" . $editlink . "'"; $viewLink = $this->shortTableName . "_view.php?" . $editlink; $this->fillCheckAttr($record, $data, $keyblock); if ($this->googleMapCfg['isUseMainMaps']) { $this->addBigGoogleMapMarkers($data, $viewLink); } for ($i = 0; $i < count($this->listFields); $i++) { // call addGoogleMapData before call proccessRecordValue!!! if (in_array($i, $this->gMapFields)) { $this->addGoogleMapData($this->listFields[$i]['fName'], $data, $viewLink); } $record[$this->listFields[$i]['valueFieldName']] = $this->proccessRecordValue($data, $keylink, $this->listFields[$i]); } if ($this->eventExists("BeforeMoveNextList")) { $this->eventsObject->BeforeMoveNextList($data, $row, $record); } $this->recIds[] = $this->recId; $this->addSpansForGridCells('edit', $record, $data); if ($col < $this->colsOnPage) { $record["endrecord_block"] = true; } $record["grid_recordheader"] = true; $record["grid_vrecord"] = true; $row["grid_record"]["data"][] = $record; $data = $this->beforeProccessRow(); $this->recNo++; } while ($col <= $this->colsOnPage) { $record = array(); if ($col < $this->colsOnPage) { $record["endrecord_block"] = true; } $row["grid_record"]["data"][] = $record; $col++; } // assign row spacings for vertical layout $row["grid_rowspace"] = true; $row["grid_recordspace"] = array("data" => array()); for ($i = 0; $i < $this->colsOnPage * 2 - 1; $i++) { $row["grid_recordspace"]["data"][] = true; } $rowinfo["data"][] = $row; } if ($this->lockingObj) { $this->jsSettings['tableSettings'][$this->tName]['lockRecIds'] = $lockRecIds; } if (count($rowinfo["data"])) { $rowinfo["data"][count($rowinfo["data"]) - 1]["grid_rowspace"] = false; if ($this->isVerLayout && $this->is508) { $rowinfo["begin"] = "<caption style=\"display:none\">Table data</caption>"; } $this->xt->assignbyref("grid_row", $rowinfo); } $this->buildTotals($totals); }