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