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-2013 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
39 require_once ('modules/DynamicFields/DynamicField.php');
40 require_once ('modules/DynamicFields/FieldCases.php');
44 $db = DBManagerFactory:: getInstance();
47 $result = $db->query('SELECT * FROM fields_meta_data WHERE deleted = 0 ORDER BY custom_module');
50 * get the real field_meta_data
52 while ($row = $db->fetchByAssoc($result)) {
53 $the_modules = $row['custom_module'];
54 if (!isset ($modules[$the_modules])) {
55 $modules[$the_modules] = array ();
57 $modules[$the_modules][$row['name']] = $row['name'];
61 if (!isset ($_REQUEST['run'])) {
63 echo "SIMULATION MODE - NO CHANGES WILL BE MADE EXCEPT CLEARING CACHE";
66 foreach ($modules as $the_module => $fields) {
67 $class_name = $beanList[$the_module];
68 echo "<br><br>Scanning $the_module <br>";
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();
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'];
89 } elseif(!empty($row['data_length']) && (strtolower($row['type'])=='varchar' or strtolower($row['type'])=='varchar2')) {
90 $type.='(' . $row['data_length'] . ')';
92 if (!isset ($fields[$col]) && $col != 'id_c') {
94 $db->query("ALTER TABLE $mod->table_name"."_cstm DROP COLUMN $col");
96 unset ($fields[$col]);
97 echo "Dropping Column $col from $mod->table_name"."_cstm for module $the_module<br>";
100 $db_data_type = strtolower(str_replace(' ' , '', $the_field->get_db_type()));
102 $type = strtolower(str_replace(' ' , '', $type));
103 if (strcmp($db_data_type,$type) != 0) {
105 echo "Fixing Column Type for $col changing $type to ".$db_data_type."<br>";
107 $db->query($the_field->get_db_modify_alter_table($mod->table_name.'_cstm'));
112 unset ($fields[$col]);
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>";
121 $mod->custom_fields->add_existing_custom_field($field);
126 DynamicField :: deleteCache();
129 echo '<a href="index.php?module=Administration&action=UpgradeFields&run=true">Execute non-simulation mode</a>';