/include/ListView/ListViewSession.php

https://github.com/vtiger-jp/vtigercrm-5.1.x-ja · PHP · 199 lines · 170 code · 16 blank · 13 comment · 40 complexity · 1c3bc99a52725dfcfe56fe9ed232e7fe MD5 · raw file

  1. <?php
  2. /*********************************************************************************
  3. ** The contents of this file are subject to the vtiger CRM Public License Version 1.0
  4. * ("License"); You may not use this file except in compliance with the License
  5. * The Original Code is: vtiger CRM Open Source
  6. * The Initial Developer of the Original Code is vtiger.
  7. * Portions created by vtiger are Copyright (C) vtiger.
  8. * All Rights Reserved.
  9. *
  10. ********************************************************************************/
  11. require_once('include/logging.php');
  12. require_once('modules/CustomView/CustomView.php');
  13. class ListViewSession {
  14. var $module = null;
  15. var $viewname = null;
  16. var $start = null;
  17. var $sorder = null;
  18. var $sortby = null;
  19. var $page_view = null;
  20. /**initializes ListViewSession
  21. * Portions created by vtigerCRM are Copyright (C) vtigerCRM.
  22. * All Rights Reserved.
  23. */
  24. function ListViewSession()
  25. {
  26. global $log,$currentModule;
  27. $log->debug("Entering ListViewSession() method ...");
  28. $this->module = $currentModule;
  29. $this->sortby = 'ASC';
  30. $this->start =1;
  31. }
  32. function getCurrentPage($currentModule,$viewId){
  33. if(!empty($_SESSION['lvs'][$currentModule][$viewId]['start'])){
  34. return $_SESSION['lvs'][$currentModule][$viewId]['start'];
  35. }
  36. return 1;
  37. }
  38. function getRequestStartPage(){
  39. $start = $_REQUEST['start'];
  40. if(!is_numeric($start)){
  41. $start = 1;
  42. }
  43. if($start < 1){
  44. $start = 1;
  45. }
  46. $start = ceil($start);
  47. return $start;
  48. }
  49. function getListViewNavigation($currentRecordId){
  50. global $currentModule,$current_user,$adb,$log,$list_max_entries_per_page;
  51. Zend_Json::$useBuiltinEncoderDecoder = true;
  52. $reUseData = false;
  53. $displayBufferRecordCount = 10;
  54. $bufferRecordCount = 15;
  55. if($currentModule == 'Documents'){
  56. $sql = "select folderid from vtiger_notes where notesid=?";
  57. $params = array($currentRecordId);
  58. $result = $adb->pquery($sql,$params);
  59. $folderId = $adb->query_result($result,0,'folderid');
  60. }
  61. $cv = new CustomView();
  62. $viewId = $cv->getViewId($currentModule);
  63. if(!empty($_SESSION[$currentModule.'_DetailView_Navigation'.$viewId])){
  64. $recordNavigationInfo = Zend_Json::decode($_SESSION[$currentModule.'_DetailView_Navigation'.$viewId]);
  65. $pageNumber =0;
  66. if(count($recordNavigationInfo) == 1){
  67. foreach ($recordNavigationInfo as $recordIdList) {
  68. if(in_array($currentRecordId,$recordIdList)){
  69. $reUseData = true;
  70. }
  71. }
  72. }else{
  73. $recordList = array();
  74. $recordPageMapping = array();
  75. foreach ($recordNavigationInfo as $start=>$recordIdList){
  76. foreach ($recordIdList as $index=>$recordId) {
  77. $recordList[] = $recordId;
  78. $recordPageMapping[$recordId] = $start;
  79. if($recordId == $currentRecordId){
  80. $searchKey = count($recordList)-1;
  81. }
  82. }
  83. }
  84. if($searchKey > $displayBufferRecordCount -1 && $searchKey < count($recordList)-$displayBufferRecordCount){
  85. $reUseData= true;
  86. }
  87. }
  88. }
  89. if($reUseData === false){
  90. $recordNavigationInfo = array();
  91. if(!empty($_REQUEST['start'])){
  92. $start = ListViewSession::getRequestStartPage();
  93. }else{
  94. $start = ListViewSession::getCurrentPage($currentModule,$viewId);
  95. }
  96. $startRecord = (($start - 1) * $list_max_entries_per_page) - $bufferRecordCount;
  97. if($startRecord < 0){
  98. $startRecord = 0;
  99. }
  100. $list_query = $_SESSION[$currentModule.'_listquery'];
  101. if($currentModule=='Documents' && !empty($folderId)){
  102. $list_query = preg_replace("/[\n\r\s]+/"," ",$list_query);
  103. $findOrderByPosition = stripos($list_query,' ORDER BY ');
  104. if ($findOrderByPosition > 0) {
  105. $orderByClause = substr($list_query, $findOrderByPosition, strlen($list_query));
  106. $list_query = substr($list_query, 0, $findOrderByPosition). " AND vtiger_notes.folderid=$folderId " . $orderByClause;
  107. } else {
  108. $list_query .= " AND vtiger_notes.folderid=$folderId";
  109. }
  110. }
  111. if($start !=1){
  112. $recordCount = ($list_max_entries_per_page+2 * $bufferRecordCount);
  113. }else{
  114. $recordCount = ($list_max_entries_per_page+ $bufferRecordCount);
  115. }
  116. if( $adb->dbType == "pgsql"){
  117. $list_query .= " OFFSET $startRecord LIMIT $recordCount";
  118. }else{
  119. $list_query .= " LIMIT $startRecord, $recordCount";
  120. }
  121. $resultAllCRMIDlist_query=$adb->pquery($list_query,array());
  122. $navigationRecordList = array();
  123. while($forAllCRMID = $adb->fetch_array($resultAllCRMIDlist_query)) {
  124. $navigationRecordList[] = $forAllCRMID['crmid'];
  125. }
  126. $pageCount = 0;
  127. $current = $start;
  128. if($start ==1){
  129. $firstPageRecordCount = $list_max_entries_per_page;
  130. }else{
  131. $firstPageRecordCount = $bufferRecordCount;
  132. $current -=1;
  133. }
  134. $searchKey = array_search($currentRecordId,$navigationRecordList);
  135. $recordNavigationInfo = array();
  136. if($searchKey !== false){
  137. foreach ($navigationRecordList as $index => $recordId) {
  138. if(!is_array($recordNavigationInfo[$current])){
  139. $recordNavigationInfo[$current] = array();
  140. }
  141. if($index == $firstPageRecordCount || $index == ($firstPageRecordCount+$pageCount * $list_max_entries_per_page)){
  142. $current++;
  143. $pageCount++;
  144. }
  145. $recordNavigationInfo[$current][] = $recordId;
  146. }
  147. }
  148. $_SESSION[$currentModule.'_DetailView_Navigation'.$viewId] =
  149. Zend_Json::encode($recordNavigationInfo);
  150. }
  151. return $recordNavigationInfo;
  152. }
  153. function getRequestCurrentPage($currentModule, $query, $viewid, $queryMode = false) {
  154. global $list_max_entries_per_page, $adb;
  155. $start = 1;
  156. if(isset($_REQUEST['query']) && $_REQUEST['query'] == 'true'){
  157. return ListViewSession::getRequestStartPage();
  158. }
  159. if(!empty($_REQUEST['start'])){
  160. $start = $_REQUEST['start'];
  161. if($start == 'last'){
  162. $count_result = $adb->query( mkCountQuery( $query));
  163. $noofrows = $adb->query_result($count_result,0,"count");
  164. if($noofrows > 0){
  165. $start = ceil($noofrows/$list_max_entries_per_page);
  166. }
  167. }
  168. if(!is_numeric($start)){
  169. $start = 1;
  170. }elseif($start < 1){
  171. $start = 1;
  172. }
  173. $start = ceil($start);
  174. }else if(!empty($_SESSION['lvs'][$currentModule][$viewid]['start'])){
  175. $start = $_SESSION['lvs'][$currentModule][$viewid]['start'];
  176. }
  177. if(!$queryMode) {
  178. $_SESSION['lvs'][$currentModule][$viewid]['start'] = intval($start);
  179. }
  180. return $start;
  181. }
  182. }
  183. ?>