]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/EditView/SugarVCR.php
Release 6.5.3
[Github/sugarcrm.git] / include / EditView / SugarVCR.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
5  * 
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  * 
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
16  * details.
17  * 
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  * 
23  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
26  * The interactive user interfaces in modified source and object code versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  * 
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the "Powered by
32  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33  * technical reasons, the Appropriate Legal Notices must display the words
34  * "Powered by SugarCRM".
35  ********************************************************************************/
36
37  define('VCREND', '50');
38  define('VCRSTART', '10');
39  /**
40   * @api
41   */
42  class SugarVCR{
43
44         /**
45          * records the query in the session for later retrieval
46          */
47         function store($module, $query){
48                 $_SESSION[$module .'2_QUERY'] = $query;
49         }
50
51         /**
52          * This function retrieves a query from the session
53          */
54         function retrieve($module){
55                 return (!empty($_SESSION[$module .'2_QUERY']) ? $_SESSION[$module .'2_QUERY'] : '');
56         }
57
58         /**
59          * return the start, prev, next, end
60          */
61         function play($module, $offset){
62                 //given some global offset try to determine if we have this
63                 //in our array.
64                 $ids = array();
65                 if(!empty($_SESSION[$module.'QUERY_ARRAY']))
66                         $ids = $_SESSION[$module.'QUERY_ARRAY'];
67                 if(empty($ids[$offset]) || empty($ids[$offset+1]) || empty($ids[$offset-1]))
68                         $ids = SugarVCR::record($module, $offset);
69                 $menu = array();
70                 if(!empty($ids[$offset])){
71                         //return the control given this id
72                         $menu['PREV'] = ($offset > 1) ? $ids[$offset-1] : '';
73                         $menu['CURRENT'] = $ids[$offset];
74                         $menu['NEXT'] = !empty($ids[$offset+1]) ? $ids[$offset+1] : '';
75                 }
76                 return $menu;
77         }
78
79     function menu($module, $offset, $isAuditEnabled, $saveAndContinue = false ){
80         $html_text = "";
81         if ($offset < 0)
82         {
83             $offset = 0;
84         }
85
86         //this check if require in cases when you visit the edit view before visiting that modules list view.
87         //you can do this easily either from home, activities or sitemap.
88         $stored_vcr_query = SugarVCR::retrieve($module);
89
90         // bug 15893 - only show VCR if called as an element in a set of records
91         if (!empty($_REQUEST['record']) and !empty($stored_vcr_query) and isset($_REQUEST['offset']) and (empty($_REQUEST['isDuplicate']) or $_REQUEST['isDuplicate'] == 'false'))
92         {
93             //syncing with display offset;
94             $offset ++;
95             $action = (!empty($_REQUEST['action']) ? $_REQUEST['action'] : 'EditView');
96
97             $menu = SugarVCR::play($module, $offset);
98
99             $list_link = '';
100             if ($saveAndContinue && !empty($menu['NEXT']))
101             {
102                 $list_link = ajaxLink('index.php?action=' . $action . '&module=' . $module . '&record=' . $menu['NEXT'] . '&offset=' . ($offset + 1));
103             }
104
105             $previous_link = "";
106             if (!empty($menu['PREV']))
107             {
108                 $previous_link = ajaxLink('index.php?module=' . $module . '&action=' . $action . '&offset=' . ($offset - 1) . '&record=' . $menu['PREV']);
109             }
110
111             $next_link = "";
112             if (!empty($menu['NEXT']))
113             {
114                 $next_link = ajaxLink('index.php?module=' . $module . '&action=' . $action . '&offset=' . ($offset + 1) . '&record=' . $menu['NEXT']);
115             }
116
117             $ss = new Sugar_Smarty();
118             $ss->assign('app_strings', $GLOBALS['app_strings']);
119             $ss->assign('module', $module);
120             $ss->assign('action', $action);
121             $ss->assign('menu', $menu);
122             $ss->assign('list_link', $list_link);
123             $ss->assign('previous_link', $previous_link);
124             $ss->assign('next_link', $next_link);
125
126             $ss->assign('offset', $offset);
127             $ss->assign('total', '');
128             $ss->assign('plus', '');
129
130             if (!empty($_SESSION[$module . 'total']))
131             {
132                 $ss->assign('total', $_SESSION[$module . 'total']);
133                 if (
134                     !empty($GLOBALS['sugar_config']['disable_count_query'])
135                     && (($_SESSION[$module. 'total']-1) % $GLOBALS['sugar_config']['list_max_entries_per_page'] == 0)
136                 )
137                 {
138                     $ss->assign('plus', '+');
139                 }
140             }
141
142             if (is_file('custom/include/EditView/SugarVCR.tpl'))
143             {
144                 $html_text .= $ss->fetch('custom/include/EditView/SugarVCR.tpl');
145             }
146             else
147             {
148                 $html_text .= $ss->fetch('include/EditView/SugarVCR.tpl');
149             }
150         }
151         return $html_text;
152     }
153
154         function record($module, $offset){
155                 $GLOBALS['log']->debug('SUGARVCR is recording more records');
156                 $start = max(0, $offset - VCRSTART);
157                 $index = $start;
158             $db = DBManagerFactory::getInstance();
159
160                 $result = $db->limitQuery(SugarVCR::retrieve($module),$start,($offset+VCREND),false);
161                 $index++;
162
163                 $ids = array();
164                 while(($row = $db->fetchByAssoc($result)) != null){
165                         $ids[$index] = $row['id'];
166                         $index++;
167                 }
168                 //now that we have the array of ids, store this in the session
169                 $_SESSION[$module.'QUERY_ARRAY'] = $ids;
170                 return $ids;
171         }
172
173         function recordIDs($module, $rids, $offset, $totalCount){
174                 $index = $offset;
175                 $index++;
176                 $ids = array();
177                 foreach($rids as $id){
178                         $ids[$index] = $id;
179                         $index++;
180                 }
181                 //now that we have the array of ids, store this in the session
182                 $_SESSION[$module.'QUERY_ARRAY'] = $ids;
183                 $_SESSION[$module.'total'] = $totalCount;
184         }
185
186         function erase($module){
187                 unset($_SESSION[$module. 'QUERY_ARRAY']);
188         }
189
190  }
191 ?>