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);
 }
Пример #2
0
 /**
  * Builde order params
  */
 function buildOrderParams()
 {
     //orderlinkattrs for fields
     $this->orderLinksAttr();
     $order_ind = -1;
     //Array fields for sort
     $this->arrFieldForSort = array();
     //Array how fields sort
     $this->arrHowFieldSort = array();
     $arrImplicitSortFields = array();
     $key = array();
     //Session field for sort
     if (@$_SESSION[$this->sessionPrefix . "_arrFieldForSort"]) {
         $this->arrFieldForSort = $_SESSION[$this->sessionPrefix . "_arrFieldForSort"];
     }
     //Session how field sort
     if (@$_SESSION[$this->sessionPrefix . "_arrHowFieldSort"]) {
         $this->arrHowFieldSort = $_SESSION[$this->sessionPrefix . "_arrHowFieldSort"];
     }
     //Session key fields for sort
     if (@$_SESSION[$this->sessionPrefix . "_key"]) {
         $key = $_SESSION[$this->sessionPrefix . "_key"];
     } else {
         $tKeys = GetTableKeys($this->tName);
         for ($i = 0; $i < count($tKeys); $i++) {
             if (GetFieldIndex($tKeys[$i])) {
                 $key[] = GetFieldIndex($tKeys[$i]);
             }
         }
         $_SESSION[$this->sessionPrefix . "_key"] = $key;
     }
     $lenkey = count($key);
     if (!isset($_SESSION[$this->sessionPrefix . "_order"])) {
         $this->arrFieldForSort = array();
         $this->arrHowFieldSort = array();
         if (count($this->gOrderIndexes)) {
             for ($i = 0; $i < count($this->gOrderIndexes); $i++) {
                 $this->arrFieldForSort[] = $this->gOrderIndexes[$i][0];
                 $this->arrHowFieldSort[] = $this->gOrderIndexes[$i][1];
             }
         } elseif ($this->gstrOrderBy != '') {
             $_SESSION[$this->sessionPrefix . "_noNextPrev"] = 1;
         }
         //add sorting on key fields
         if (count($key) && $this->moveNext) {
             for ($i = 0; $i < $lenkey; $i++) {
                 $idsearch = array_search($key[$i], $this->arrFieldForSort);
                 if ($idsearch === false) {
                     $this->arrFieldForSort[] = $key[$i];
                     $this->arrHowFieldSort[] = "ASC";
                     $arrImplicitSortFields[] = $key[$i];
                 }
             }
         }
     }
     $lenArr = count($this->arrFieldForSort);
     //Sorting defined on the sheet
     if (@$_SESSION[$this->sessionPrefix . "_orderby"]) {
         $order_field = GetFieldByGoodFieldName(substr($_SESSION[$this->sessionPrefix . "_orderby"], 1));
         $order_dir = substr($_SESSION[$this->sessionPrefix . "_orderby"], 0, 1);
         $order_ind = GetFieldIndex($order_field);
         if ($order_ind) {
             if (!@$_REQUEST["a"] and !@$_REQUEST["goto"] and !@$_REQUEST["pagesize"]) {
                 if (@$_REQUEST["ctrl"]) {
                     $idsearch = array_search($order_ind, $this->arrFieldForSort);
                     if ($idsearch === false) {
                         if ($order_dir == "a") {
                             $this->arrFieldForSort[] = $order_ind;
                             $this->arrHowFieldSort[] = "ASC";
                         } else {
                             $this->arrFieldForSort[] = $order_ind;
                             $this->arrHowFieldSort[] = "DESC";
                         }
                     } else {
                         $this->arrHowFieldSort[$idsearch] = $order_dir == "a" ? "ASC" : "DESC";
                     }
                 } else {
                     $this->arrFieldForSort = array();
                     $this->arrHowFieldSort = array();
                     if (!empty($_SESSION[$this->sessionPrefix . "_orderNo"])) {
                         unset($_SESSION[$this->sessionPrefix . "_orderNo"]);
                     }
                     $_SESSION[$this->sessionPrefix . "_noNextPrev"] = 0;
                     if ($order_dir == "a") {
                         $this->arrFieldForSort[] = $order_ind;
                         $this->arrHowFieldSort[] = "ASC";
                     } else {
                         $this->arrFieldForSort[] = $order_ind;
                         $this->arrHowFieldSort[] = "DESC";
                     }
                 }
             }
         }
     }
     $lenArr = count($this->arrFieldForSort);
     //Draw pictures of fields for sorting
     $condition = true;
     if (!count($this->arrFieldForSort)) {
         $condition = false;
     } elseif (!$this->arrFieldForSort[0]) {
         $condition = false;
     }
     if ($condition) {
         for ($i = 0; $i < $lenArr; $i++) {
             $order_field = GetFieldByIndex($this->arrFieldForSort[$i]);
             $order_dir = $this->arrHowFieldSort[$i] == "ASC" ? "a" : "d";
             $idsearch = array_search($this->arrFieldForSort[$i], $arrImplicitSortFields);
             if ($idsearch === false) {
                 $this->xt->assign_section(GoodFieldName($order_field) . "_fieldheader", "", "<img " . ($this->is508 == true ? "alt=\" \" " : "") . "src=\"images/" . ($order_dir == "a" ? "up" : "down") . ".gif\" border=0>");
             }
             // default ASC for key fields
             if ($idsearch !== false && in_array(GetFieldIndex($order_field), $arrImplicitSortFields)) {
                 $orderlinkattrs = $this->setLinksAttr(GoodFieldName($order_field));
             } else {
                 $orderlinkattrs = $this->setLinksAttr(GoodFieldName($order_field), $order_dir);
             }
             $this->xt->assign(GoodFieldName($order_field) . "_orderlinkattrs", $orderlinkattrs);
         }
     }
     //Shape sorting line for a request
     if ($lenArr > 0) {
         for ($i = 0; $i < $lenArr; $i++) {
             $this->strOrderBy .= GetFieldByIndex($this->arrFieldForSort[$i]) ? ($this->strOrderBy != "" ? ", " : " ORDER BY ") . $this->arrFieldForSort[$i] . " " . $this->arrHowFieldSort[$i] : "";
         }
     }
     if ($_SESSION[$this->sessionPrefix . "_noNextPrev"] == 1) {
         $this->strOrderBy = $this->gstrOrderBy;
     }
 }