]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Administration/UpgradeFields.php
Release 6.5.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-2012 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
47 $result = $db->query('SELECT * FROM fields_meta_data WHERE deleted = 0 ORDER BY custom_module');
48 $modules = array ();
49 /*
50  * get the real field_meta_data
51  */
52 while ($row = $db->fetchByAssoc($result)) {
53         $the_modules = $row['custom_module'];
54         if (!isset ($modules[$the_modules])) {
55                 $modules[$the_modules] = array ();
56         }
57         $modules[$the_modules][$row['name']] = $row['name'];
58 }
59
60 $simulate = false;
61 if (!isset ($_REQUEST['run'])) {
62         $simulate = true;
63         echo "SIMULATION MODE - NO CHANGES WILL BE MADE EXCEPT CLEARING CACHE";
64 }
65
66 foreach ($modules as $the_module => $fields) {
67         $class_name = $beanList[$the_module];
68         echo "<br><br>Scanning $the_module <br>";
69
70         require_once ($beanFiles[$class_name]);
71         $mod = new $class_name ();
72         if (!$db->tableExists($mod->table_name."_cstm")) {
73                 $mod->custom_fields = new DynamicField();
74                 $mod->custom_fields->setup($mod);
75                 $mod->custom_fields->createCustomTable();
76         }
77
78         $table = $db->getTableDescription($mod->table_name."_cstm");
79         foreach($table as $row) {
80                 $col = strtolower(empty ($row['Field']) ? $row['field'] : $row['Field']);
81                 $the_field = $mod->custom_fields->getField($col);
82                 $type = strtolower(empty ($row['Type']) ? $row['type'] : $row['Type']);
83                 if (!empty($row['data_precision']) && !empty($row['data_scale'])) {
84                         $type.='(' . $row['data_precision'];
85                         if (!empty($row['data_scale'])) {
86                                 $type.=',' . $row['data_scale'];
87                         }
88                         $type.=')';
89                 } elseif(!empty($row['data_length']) && (strtolower($row['type'])=='varchar' or strtolower($row['type'])=='varchar2')) {
90                         $type.='(' . $row['data_length'] . ')';
91                 }
92                 if (!isset ($fields[$col]) && $col != 'id_c') {
93                         if (!$simulate) {
94                                 $db->query("ALTER TABLE $mod->table_name"."_cstm DROP COLUMN $col");
95                         }
96                         unset ($fields[$col]);
97                         echo "Dropping Column $col from $mod->table_name"."_cstm for module $the_module<br>";
98                 } else {
99                         if ($col != 'id_c') {
100                                 $db_data_type = strtolower(str_replace(' ' , '', $the_field->get_db_type()));
101
102                                 $type = strtolower(str_replace(' ' , '', $type));
103                                 if (strcmp($db_data_type,$type) != 0) {
104
105                                         echo "Fixing Column Type for $col changing $type to ".$db_data_type."<br>";
106                                         if (!$simulate) {
107                                                 $db->query($the_field->get_db_modify_alter_table($mod->table_name.'_cstm'));
108                     }
109                                 }
110                         }
111
112                         unset ($fields[$col]);
113                 }
114
115         }
116
117         echo sizeof($fields)." field(s) missing from $mod->table_name"."_cstm<br>";
118         foreach ($fields as $field) {
119                 echo "Adding Column $field to $mod->table_name"."_cstm<br>";
120                 if (!$simulate)
121                         $mod->custom_fields->add_existing_custom_field($field);
122         }
123
124 }
125
126 DynamicField :: deleteCache();
127 echo '<br>Done<br>';
128 if ($simulate) {
129         echo '<a href="index.php?module=Administration&action=UpgradeFields&run=true">Execute non-simulation mode</a>';
130 }
131 ?>