]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/DynamicFields/UpgradeFields.php
Release 6.5.6
[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          if(isset($beanList[$the_module]))
65          {
66         $class_name = $beanList[$the_module];
67         echo "<br><br>Scanning $the_module <br>";
68                 
69                 require_once($beanFiles[$class_name]);
70                 $mod = new $class_name();
71                 if(!$db->tableExists($mod->table_name . "_cstm")){
72                         $mod->custom_fields = new DynamicField();
73                         $mod->custom_fields->setup($mod);
74                 $mod->custom_fields->createCustomTable();
75                 }
76
77                 $result = $db->query("DESCRIBE $mod->table_name" . "_cstm");
78                 
79                 while($row = $db->fetchByAssoc($result)){
80                         
81                         
82                         $col = $row['Field'];
83                         $type = $row['Type'];
84                         $fieldDef = $mod->getFieldDefinition($col);
85                         $the_field = get_widget($fieldDef['type']);
86                         $the_field->set($fieldDef);
87                                 
88                                 if(!isset($fields[$col]) && $col != 'id_c'){
89                                         if(!$simulate)$db->query("ALTER TABLE $mod->table_name" . "_cstm DROP COLUMN $col");
90                                         unset($fields[$col]);
91                                         echo "Dropping Column $col from $mod->table_name" . "_cstm for module $the_module<br>";
92                                 }       else{
93                                         if($col != 'id_c'){
94                                         if(trim($the_field->get_db_type()) != trim($type)){
95                                                 
96                                         echo "Fixing Column Type for $col changing $type to ". $the_field->get_db_type() . "<br>";
97                                         if(!$simulate)$db->query($the_field->get_db_modify_alter_table($mod->table_name . '_cstm'));            
98                                         }
99                                         }
100                                         
101                                         unset($fields[$col]);
102                                 }
103                         
104                 }
105                         
106                         echo sizeof($fields) . " field(s) missing from $mod->table_name" . "_cstm<br>";
107                         foreach($fields as $field){
108                                 echo "Adding Column $field to $mod->table_name" . "_cstm<br>";
109                                 if(!$simulate){
110                                         $the_field = $mod->getFieldDefinition($field);
111                                         $field = get_widget($the_field['type']);
112                                         $field->set($the_field);
113                                         $query = $field->get_db_add_alter_table($mod->table_name . '_cstm');
114                                         echo $query;
115                         if(!empty($query)){
116                                 $mod->db->query($query);
117                         }
118                                 }
119                         }
120          }
121         }
122         
123         
124         DynamicField::deleteCache();    
125         echo '<br>Done<br>';
126         if($simulate){
127                 echo '<a href="index.php?module=Administration&action=UpgradeFields&run=true">Execute non-simulation mode</a>'; 
128         }
129                 
130                 
131  
132  
133         
134  
135  
136  
137  ?>