]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Administration/RepairFieldCasing.php
Release 6.2.0
[Github/sugarcrm.git] / modules / Administration / RepairFieldCasing.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-2011 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 //Check if current user has admin access
39 if(is_admin($current_user)) {
40     global $mod_strings; 
41     
42     //echo out processing message
43     echo '<br>'.$mod_strings['LBL_REPAIR_FIELD_CASING_PROCESSING'];
44     
45     //store the affected entries
46     $database_entries = array();
47     $module_entries = array();
48     
49     $query = "SELECT * FROM fields_meta_data";
50     $result = $GLOBALS['db']->query($query);      
51     while($row = $GLOBALS['db']->fetchByAssoc($result)) {
52           $name = $row['name'];
53           $id = $row['id'];
54           $module_entries[$row['custom_module']] = true;
55           
56           //Only run database SQL where the name or id casing does is not lowercased
57           if($name != strtolower($row['name'])) {
58                  $database_entries[$row['custom_module']][$name] = $row;         
59           }
60     }    
61     
62     //If we have database entries to process
63     if(!empty($database_entries)) {
64        
65        foreach($database_entries as $module=>$entries) {
66            $table_name = strtolower($module) . '_cstm';
67            
68            foreach($entries as $original_col_name=>$entry) {
69                echo '<br>'. string_format($mod_strings['LBL_REPAIR_FIELD_CASING_SQL_FIELD_META_DATA'], array($entry['name']));
70                    $update_sql = "UPDATE fields_meta_data SET id = '" . $entry['custom_module'] . strtolower($entry['name']) . "', name = '" . strtolower($entry['name']) . "' WHERE id = '" . $entry['id'] . "'";
71                    $GLOBALS['db']->query($update_sql); 
72
73                    echo '<br>'. string_format($mod_strings['LBL_REPAIR_FIELD_CASING_SQL_CUSTOM_TABLE'], array($entry['name'], $table_name));
74                    
75                    if($GLOBALS['db']->dbType == 'mssql') {
76                                 $sql = "SP_RENAME '{$table_name}.{$entry['name']}', '" . strtolower($entry['name']) . "', 'COLUMN'";
77                                 $GLOBALS['db']->query($sql);
78                    } else if($GLOBALS['db']->dbType == 'mysql') {
79                             $entry['name'] = strtolower($entry['name']);
80                             $GLOBALS['db']->alterColumn($table_name, $entry);
81                    } else if($GLOBALS['db']->dbType == 'oci8') {
82                                 $sql = "ALTER TABLE \"" . strtoupper($table_name) ."\" RENAME COLUMN \"" . $entry['name'] . "\" TO \"" . strtolower($entry['name']) . "\""; 
83                                 $GLOBALS['db']->query($sql);                            
84                    }
85            }
86        }
87     }
88     
89     //If we have metadata files to alter
90     if(!empty($module_entries)) {
91             $modules = array_keys($module_entries);
92             $views = array('basic_search', 'advanced_search', 'detailview', 'editview', 'quickcreate');
93             $class_names = array();
94             
95         require_once ('include/TemplateHandler/TemplateHandler.php') ;      
96             require_once('modules/ModuleBuilder/parsers/ParserFactory.php');
97             
98             foreach($modules as $module) {      
99                if(isset($GLOBALS['beanList'][$module])) {
100                   $class_names[] = $GLOBALS['beanList'][$module];
101                }
102                 
103                $repairClass->module_list[] = $module;
104                foreach($views as $view) {
105                 try{
106                     $parser = ParserFactory::getParser($view, $module);
107                 }
108                 catch(Exception $e){
109                     $GLOBALS['log']->fatal("Caught exception in RepairFieldCasing script: ".$e->getMessage());
110                     continue;
111                 }
112                         if(isset($parser->_viewdefs['panels'])) {
113                            foreach($parser->_viewdefs['panels'] as $panel_id=>$panel) {
114                                   foreach($panel as $row_id=>$row) {
115                                          foreach($row as $entry_id=>$entry) {
116                                                 if(is_array($entry) && isset($entry['name'])) {
117                                                    $parser->_viewdefs['panels'][$panel_id][$row_id][$entry_id]['name'] = strtolower($entry['name']);    
118                                                 }
119                                          }
120                                   }
121                            }
122                         } else {
123                           //For basic_search and advanced_search views, just process the fields
124                           foreach($parser->_viewdefs as $entry_id=>$entry) {
125                                  if(is_array($entry) && isset($entry['name'])) {
126                                         $parser->_viewdefs[$entry_id]['name'] = strtolower($entry['name']);     
127                                  }                                      
128                           }
129                         }
130                 
131                         //Save the changes
132                         $parser->handleSave(false);
133                } //foreach
134                
135                //Now clear the cache of the .tpl files
136                TemplateHandler::clearCache($module);
137                
138                
139             } //foreach
140             
141             echo '<br>'.$mod_strings['LBL_CLEAR_VARDEFS_DATA_CACHE_TITLE'];
142             require_once('modules/Administration/QuickRepairAndRebuild.php');
143         $repair = new RepairAndClear();
144         $repair->show_output = false;
145         $repair->module_list = array($class_names);
146         $repair->clearVardefs();            
147     }
148     
149     echo '<br>'.$mod_strings['LBL_DIAGNOSTIC_DONE'];
150     
151 }
152 ?>