]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Administration/RepairFieldCasing.php
Release 6.4.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                    $GLOBALS['db']->query($GLOBALS['db']->renameColumnSQL($table_name, $entry['name'], strtolower($entry['name'])));
76            }
77        }
78     }
79
80     //If we have metadata files to alter
81     if(!empty($module_entries)) {
82             $modules = array_keys($module_entries);
83             $views = array('basic_search', 'advanced_search', 'detailview', 'editview', 'quickcreate');
84             $class_names = array();
85
86         require_once ('include/TemplateHandler/TemplateHandler.php') ;
87             require_once('modules/ModuleBuilder/parsers/ParserFactory.php');
88
89             foreach($modules as $module) {
90                if(isset($GLOBALS['beanList'][$module])) {
91                   $class_names[] = $GLOBALS['beanList'][$module];
92                }
93
94                $repairClass->module_list[] = $module;
95                foreach($views as $view) {
96                 try{
97                     $parser = ParserFactory::getParser($view, $module);
98                 }
99                 catch(Exception $e){
100                     $GLOBALS['log']->fatal("Caught exception in RepairFieldCasing script: ".$e->getMessage());
101                     continue;
102                 }
103                         if(isset($parser->_viewdefs['panels'])) {
104                            foreach($parser->_viewdefs['panels'] as $panel_id=>$panel) {
105                                   foreach($panel as $row_id=>$row) {
106                                          foreach($row as $entry_id=>$entry) {
107                                                 if(is_array($entry) && isset($entry['name'])) {
108                                                    $parser->_viewdefs['panels'][$panel_id][$row_id][$entry_id]['name'] = strtolower($entry['name']);
109                                                 }
110                                          }
111                                   }
112                            }
113                         } else {
114                           //For basic_search and advanced_search views, just process the fields
115                           foreach($parser->_viewdefs as $entry_id=>$entry) {
116                                  if(is_array($entry) && isset($entry['name'])) {
117                                         $parser->_viewdefs[$entry_id]['name'] = strtolower($entry['name']);
118                                  }
119                           }
120                         }
121
122                         //Save the changes
123                         $parser->handleSave(false);
124                } //foreach
125
126                //Now clear the cache of the .tpl files
127                TemplateHandler::clearCache($module);
128
129
130             } //foreach
131
132             echo '<br>'.$mod_strings['LBL_CLEAR_VARDEFS_DATA_CACHE_TITLE'];
133             require_once('modules/Administration/QuickRepairAndRebuild.php');
134         $repair = new RepairAndClear();
135         $repair->show_output = false;
136         $repair->module_list = array($class_names);
137         $repair->clearVardefs();
138     }
139
140     echo '<br>'.$mod_strings['LBL_DIAGNOSTIC_DONE'];
141
142 }
143 ?>