]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/SavedSearch/UpgradeSavedSearch.php
Release 6.5.0
[Github/sugarcrm.git] / modules / SavedSearch / UpgradeSavedSearch.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38 class UpgradeSavedSearch {
39
40         function UpgradeSavedSearch() {
41                 
42                 $result = $GLOBALS['db']->query("SELECT id FROM saved_search");
43                 while($row = $GLOBALS['db']->fetchByAssoc($result)) {
44                       $focus = new SavedSearch();
45                           $focus->retrieve($row['id']);
46                           $contents = unserialize(base64_decode($focus->contents));
47               $has_team_name_saved = isset($contents['team_name_advanced']) || isset($contents['team_name_basic']) ? true : false;
48                           //If $contents['searchFormTab'] is set then this is coming from a 4.x saved search
49                           if(isset($contents['searchFormTab']) && $contents['searchFormTab'] == 'saved_views') {
50                                  $new_contents = array();
51                                  $module = $contents['search_module'];
52                                  $advanced = !empty($contents['advanced']);
53                                  $field_map = array();
54                                  
55                                  if(file_exists("custom/modules/{$module}/metadata/searchdefs.php")) {
56                                         require("custom/modules/{$module}/metadata/searchdefs.php");
57                                         $field_map = $advanced ? $searchdefs[$module]['layout']['advanced_search'] : $searchdefs[$module]['layout']['basic_search'];                             
58                              }else if(file_exists("modules/{$module}/metadata/SearchFields.php")) {
59                                         require("modules/{$module}/metadata/SearchFields.php");
60                                         $field_map = $searchFields[$module];
61                                  } else {
62                                         
63                                         $bean = loadBean($module);
64                                         $field_map = $bean->field_name_map;              
65                                  }
66
67                                  //Special case for team_id field (from 4.5.x)
68                                  if(isset($contents['team_id'])) {
69                                     $contents['team_name'] = $contents['team_id'];
70                                     unset($contents['team_id']);        
71                                  }
72                                  
73                                  foreach($contents as $key=>$value) {
74                                          if(isset($field_map[$key])) {
75                                                 $new_key = $key . ($advanced ? '_advanced' : '_basic');
76                                             if(preg_match('/^team_name_(advanced|basic)$/', $new_key)) {
77                                                    
78                                                if(!is_array($value)) {
79                                                   $temp_value = array();
80                                                   $teap_value[] = $value;
81                                                   $value = $temp_value;
82                                                }
83                                             
84                                                $team_results = $GLOBALS['db']->query("SELECT id, name FROM teams where id in ('" . implode("','", $value) . "')");
85                                                if(!empty($team_results)) {
86                                                   $count = 0;
87                                                   while($team_row = $GLOBALS['db']->fetchByAssoc($team_results)) {
88                                                                 $team_key = $new_key . '_collection_' . $count;
89                                                                 $new_contents[$team_key] = $team_row['name'];
90                                                                 $new_contents['id_' . $team_key] = $team_row['id'];
91                                                                 $count++;
92                                                   } //while
93                                                } //if
94                                                
95                                                
96                                                //Unset the original key
97                                                unset($new_contents[$key]);
98                                                
99                                                //Add the any switch
100                                                $new_contents[$new_key . '_type'] = 'any';
101                                                 } else {                                                
102                                                    $new_contents[$new_key] = $value;
103                                                 }
104                                          } else {
105                                                 $new_contents[$key] = $value;
106                                          }
107                                  }
108                                  $new_contents['searchFormTab'] = $advanced ? 'advanced_search' : 'basic_search';
109                                  $content = base64_encode(serialize($new_contents));
110                                  $GLOBALS['db']->query("UPDATE saved_search SET contents = '{$content}' WHERE id = '{$row['id']}'");
111                         } else if($has_team_name_saved) {
112                              //Otherwise, if the boolean has_team_name_saved is set to true, we also need to parse (coming from 5.x)
113                                  if(isset($contents['team_name_advanced'])) {
114                                         $team_results = $GLOBALS['db']->query("SELECT name FROM teams where id = '{$contents['team_name_advanced']}'");
115                                         if(!empty($team_results)) {
116                                                 $team_row = $GLOBALS['db']->fetchByAssoc($team_results);
117                                                 $contents['team_name_advanced_collection_0'] = $team_row['name'];
118                                                 $contents['id_team_name_advanced_collection_0'] = $contents['team_name_advanced'];
119                                                 $contents['team_name_advanced_type'] = 'any';
120                                                 unset($contents['team_name_advanced']);
121                                                 $content = base64_encode(serialize($contents));
122                                                 $GLOBALS['db']->query("UPDATE saved_search SET contents = '{$content}' WHERE id = '{$row['id']}'");                                             
123                                         }
124                                  }                              
125                         }
126                 } //while
127         }
128 }
129 ?>