]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Administration/UpgradeFields.php
Release 6.2.0
[Github/sugarcrm.git] / modules / Administration / UpgradeFields.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry)
3         die('Not A Valid Entry Point');
4 /*********************************************************************************
5  * SugarCRM Community Edition is a customer relationship management program developed by
6  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
7  * 
8  * This program is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Affero General Public License version 3 as published by the
10  * Free Software Foundation with the addition of the following permission added
11  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
12  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
13  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14  * 
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
18  * details.
19  * 
20  * You should have received a copy of the GNU Affero General Public License along with
21  * this program; if not, see http://www.gnu.org/licenses or write to the Free
22  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23  * 02110-1301 USA.
24  * 
25  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
26  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27  * 
28  * The interactive user interfaces in modified source and object code versions
29  * of this program must display Appropriate Legal Notices, as required under
30  * Section 5 of the GNU Affero General Public License version 3.
31  * 
32  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
33  * these Appropriate Legal Notices must retain the display of the "Powered by
34  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
35  * technical reasons, the Appropriate Legal Notices must display the words
36  * "Powered by SugarCRM".
37  ********************************************************************************/
38
39 require_once ('modules/DynamicFields/DynamicField.php');
40 require_once ('modules/DynamicFields/FieldCases.php');
41 global $db;
42
43 if (!isset ($db)) {
44         $db = DBManagerFactory:: getInstance();
45 }
46 global $dbManager;
47 if (!isset ($dbManager)) {
48         $dbManager = DBManagerFactory::getInstance();
49 }
50
51 $result = $db->query('SELECT * FROM fields_meta_data WHERE deleted = 0 ORDER BY custom_module');
52 $modules = array ();
53 /*
54  * get the real field_meta_data
55  */
56 while ($row = $db->fetchByAssoc($result)) {
57         $the_modules = $row['custom_module'];
58         if (!isset ($modules[$the_modules])) {
59                 $modules[$the_modules] = array ();
60         }
61         $modules[$the_modules][$row['name']] = $row['name'];
62 }
63
64 $simulate = false;
65 if (!isset ($_REQUEST['run'])) {
66         $simulate = true;
67         echo "SIMULATION MODE - NO CHANGES WILL BE MADE EXCEPT CLEARING CACHE";
68 }
69
70 foreach ($modules as $the_module => $fields) {
71         $class_name = $beanList[$the_module];
72         echo "<br><br>Scanning $the_module <br>";
73
74         require_once ($beanFiles[$class_name]);
75         $mod = new $class_name ();
76         if (!$db->tableExists($mod->table_name."_cstm")) {
77                 $mod->custom_fields = new DynamicField();
78                 $mod->custom_fields->setup($mod);
79                 $mod->custom_fields->createCustomTable();
80         }
81
82         if ($db->dbType == 'oci8') {
83         } elseif ($db->dbType == 'mssql') {
84         $def_query="SELECT col.name as field, col_type.name as type, col.is_nullable, col.precision as data_precision, col.max_length as data_length, col.scale data_scale";
85         $def_query.=" FROM sys.columns col";
86         $def_query.=" join sys.types col_type on col.user_type_id=col_type.user_type_id ";
87         $def_query.=" where col.object_id = (select object_id(sys.schemas.name + '.' + sys.tables.name)"; 
88         $def_query.=" from sys.tables  join sys.schemas on sys.schemas.schema_id = sys.tables.schema_id";
89         $def_query.=" where sys.tables.name='".$mod->table_name."_cstm')";        
90         $result = $db->query($def_query);    
91         }
92         else {
93                 $result = $db->query("DESCRIBE $mod->table_name"."_cstm");
94         }
95     
96         while ($row = $db->fetchByAssoc($result)) {
97                 $col = strtolower(empty ($row['Field']) ? $row['field'] : $row['Field']);
98                 $the_field = $mod->custom_fields->getField($col);
99                 $type = strtolower(empty ($row['Type']) ? $row['type'] : $row['Type']);
100                 if ($db->dbType == 'oci8' or $db->dbType == 'mssql') {
101                         if ($row['data_precision']!= '' and $row['data_scale']!='') {
102                                 $type.='(' . $row['data_precision'];
103                                 if (!empty($row['data_scale'])) {
104                                         $type.=',' . $row['data_scale'];                                
105                                 }               
106                                 $type.=')';
107                         }        else {
108                                 if ($row['data_length']!= '' && (strtolower($row['type'])=='varchar' or strtolower($row['type'])=='varchar2')) {
109                                         $type.='(' . $row['data_length'] . ')';
110                         
111                                 } 
112                         }       
113                 }       
114                 if (!isset ($fields[$col]) && $col != 'id_c') {
115                         if (!$simulate) {
116                                 $db->query("ALTER TABLE $mod->table_name"."_cstm DROP COLUMN $col");
117                         }
118                         unset ($fields[$col]);
119                         echo "Dropping Column $col from $mod->table_name"."_cstm for module $the_module<br>";
120                 } else {
121                         if ($col != 'id_c') {
122                                 //$db_data_type = $dbManager->helper->getColumnType(strtolower($the_field->data_type));
123                                 $db_data_type = strtolower(str_replace(' ' , '', $the_field->get_db_type()));
124                                 
125                                 $type = strtolower(str_replace(' ' , '', $type));
126                                 if (strcmp($db_data_type,$type) != 0) {
127
128                                         echo "Fixing Column Type for $col changing $type to ".$db_data_type."<br>";
129                                         if (!$simulate) {
130                                                 $db->query($the_field->get_db_modify_alter_table($mod->table_name.'_cstm'));
131                     }
132                                 }
133                         }
134
135                         unset ($fields[$col]);
136                 }
137
138         }
139
140         echo sizeof($fields)." field(s) missing from $mod->table_name"."_cstm<br>";
141         foreach ($fields as $field) {
142                 echo "Adding Column $field to $mod->table_name"."_cstm<br>";
143                 if (!$simulate)
144                         $mod->custom_fields->add_existing_custom_field($field);
145         }
146
147 }
148
149 DynamicField :: deleteCache();
150 echo '<br>Done<br>';
151 if ($simulate) {
152         echo '<a href="index.php?module=Administration&action=UpgradeFields&run=true">Execute non-simulation mode</a>';
153 }
154 ?>