]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Import/Forms.php
Release 6.1.4
[Github/sugarcrm.git] / modules / Import / Forms.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3
4 /*********************************************************************************
5  * SugarCRM 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 /*********************************************************************************
40
41  * Description:  Contains a variety of utility functions for the Import module
42  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
43  * All Rights Reserved.
44  ********************************************************************************/
45
46 /**
47  * Returns the bean object of the given module
48  *
49  * @param  string $module
50  * @return object
51  */
52 function loadImportBean(
53     $module
54     )
55 {
56     $focus = SugarModule::get($module)->loadBean();
57     if ( $focus ) {
58         if ( !$focus->importable )
59             return false;
60         if ( $module == 'Users' && !is_admin($GLOBALS['current_user'])
61              && !is_admin_for_module($GLOBALS['current_user'],'Users')
62            )
63             return false;
64         if ( $focus->bean_implements('ACL')){
65             if(!ACLController::checkAccess($focus->module_dir, 'import', true)){
66                 ACLController::displayNoAccess();
67                 sugar_die('');
68             }
69         }
70     }
71     else {
72         return false;
73     }
74     
75     return $focus;
76 }
77
78 /**
79  * Displays the Smarty template for an error
80  *
81  * @param string $message error message to show
82  * @param string $module what module we were importing into
83  * @param string $action what page we should go back to
84  */
85 function showImportError(
86     $message, 
87     $module,
88     $action = 'Step1'
89     )
90 {
91     $ss = new Sugar_Smarty();
92     
93         $ss->assign("MESSAGE",$message);
94     $ss->assign("ACTION",$action);
95     $ss->assign("IMPORT_MODULE",$module);
96     $ss->assign("MOD", $GLOBALS['mod_strings']);
97     $ss->assign("SOURCE","");
98     if ( isset($_REQUEST['source']) )
99         $ss->assign("SOURCE", $_REQUEST['source']);
100     
101     echo $ss->fetch('modules/Import/tpls/error.tpl');
102 }
103
104 /**
105  * Replaces PHP error handler in Step4
106  *
107  * @param int    $errno
108  * @param string $errstr
109  * @param string $errfile
110  * @param string $errline
111  */
112 function handleImportErrors(
113     $errno, 
114     $errstr, 
115     $errfile, 
116     $errline
117     )
118 {
119     if ( !defined('E_DEPRECATED') )
120         define('E_DEPRECATED','8192');
121     if ( !defined('E_USER_DEPRECATED') )
122         define('E_USER_DEPRECATED','16384');
123     
124     // check to see if current reporting level should be included based upon error_reporting() setting, if not
125     // then just return
126     if ( !(error_reporting() & $errno) )
127         return true;
128     
129     switch ($errno) {
130     case E_USER_ERROR:
131         $err_str = "ERROR: [$errno] $errstr on line $errline in file $errfile<br />\n";
132         $GLOBALS['log']->fatal('IMPORT ERROR:: '.$err_str);
133         echo $err_str;
134         exit(1);
135         break;
136     case E_USER_WARNING:
137     case E_WARNING:
138         $GLOBALS['log']->fatal("IMPORT WARNING::  [$errno] $errstr on line $errline in file $errfile<br />\n");
139         break;
140     case E_USER_NOTICE:
141     case E_NOTICE:
142         $GLOBALS['log']->fatal("IMPORT NOTICE::   [$errno] $errstr on line $errline in file $errfile<br />\n");
143         break;
144     case E_STRICT:
145     case E_DEPRECATED:
146     case E_USER_DEPRECATED:
147         // don't worry about these
148         //echo "STRICT ERROR: [$errno] $errstr on line $errline in file $errfile<br />\n";
149         break;
150     default:
151         $err_str = "Unknown error type: [$errno] $errstr on line $errline in file $errfile<br />\n";
152         echo $err_str;
153         $GLOBALS['log']->fatal('IMPORT ERROR:: '.$err_str);
154         break;
155     }
156
157     return true;
158 }
159
160
161 /**
162  * Returns an input control for this fieldname given
163  *
164  * @param  string $module
165  * @param  string $fieldname
166  * @param  string $vardef
167  * @param  string $value
168  * @return string html for input element for this control
169  */
170 function getControl(
171     $module,
172     $fieldname,
173     $vardef = null,
174     $value = ''
175     )
176 {
177     global $current_language, $app_strings, $dictionary, $app_list_strings;
178     
179     // use the mod_strings for this module
180     $mod_strings = return_module_language($current_language,$module);
181     
182         // set the filename for this control
183     $file = create_cache_directory('modules/Import/') . $module . $fieldname . '.tpl';
184
185     if ( !is_file($file) 
186             || !empty($GLOBALS['sugar_config']['developerMode']) 
187             || !empty($_SESSION['developerMode']) ) {
188         
189         if ( !isset($vardef) ) {
190             $focus = loadBean($module);
191             $vardef = $focus->getFieldDefinition($fieldname);
192         }
193         
194         // if this is the id relation field, then don't have a pop-up selector.
195         if( $vardef['type'] == 'relate' && $vardef['id_name'] == $vardef['name']) { 
196             $vardef['type'] = 'varchar'; 
197         }
198         
199         // create the dropdowns for the parent type fields
200         if ( $vardef['type'] == 'parent_type' ) {
201             $vardef['type'] = 'enum';
202         }
203         
204         // remove the special text entry field function 'getEmailAddressWidget'
205         if ( isset($vardef['function']) 
206                 && ( $vardef['function'] == 'getEmailAddressWidget' 
207                     || $vardef['function']['name'] == 'getEmailAddressWidget' ) )
208             unset($vardef['function']);
209         
210         // load SugarFieldHandler to render the field tpl file
211         static $sfh;
212         
213         if(!isset($sfh)) {
214             require_once('include/SugarFields/SugarFieldHandler.php');
215             $sfh = new SugarFieldHandler();
216         }
217         
218         $displayParams = array();
219         $displayParams['formName'] = 'importstep3';  
220
221         $contents = $sfh->displaySmarty('fields', $vardef, 'ImportView', $displayParams);
222         
223         // Remove all the copyright comments
224         $contents = preg_replace('/\{\*[^\}]*?\*\}/', '', $contents);
225         
226         // hack to disable one of the js calls in this control
227         if ( isset($vardef['function']) 
228                 && ( $vardef['function'] == 'getCurrencyDropDown' 
229                     || $vardef['function']['name'] == 'getCurrencyDropDown' ) )
230         $contents .= "{literal}<script>function CurrencyConvertAll() { return; }</script>{/literal}";
231         
232         // Save it to the cache file
233         if($fh = @sugar_fopen($file, 'w')) {
234             fputs($fh, $contents);
235             fclose($fh);
236         }
237     }
238     
239     // Now render the template we received
240     $ss = new Sugar_Smarty();
241     
242     // Create Smarty variables for the Calendar picker widget
243     global $timedate;
244     $time_format = $timedate->get_user_time_format();
245     $date_format = $timedate->get_cal_date_format();
246     $ss->assign('USER_DATEFORMAT', $timedate->get_user_date_format());
247         $ss->assign('TIME_FORMAT', $time_format);
248     $time_separator = ":";
249     $match = array();
250     if(preg_match('/\d+([^\d])\d+([^\d]*)/s', $time_format, $match)) {
251         $time_separator = $match[1];
252     }
253     $t23 = strpos($time_format, '23') !== false ? '%H' : '%I';
254     if(!isset($match[2]) || $match[2] == '') {
255         $ss->assign('CALENDAR_FORMAT', $date_format . ' ' . $t23 . $time_separator . "%M");
256     } 
257     else {
258         $pm = $match[2] == "pm" ? "%P" : "%p";
259         $ss->assign('CALENDAR_FORMAT', $date_format . ' ' . $t23 . $time_separator . "%M" . $pm);
260     }
261     
262     // populate the fieldlist from the vardefs
263     $fieldlist = array();
264     if ( !isset($focus) || !($focus instanceof SugarBean) )
265         $focus = loadBean($module);
266     // create the dropdowns for the parent type fields
267     if ( $vardef['type'] == 'parent_type' ) {
268         $focus->field_defs[$vardef['name']]['options'] = $focus->field_defs[$vardef['group']]['options'];
269     }
270     $vardefFields = $focus->getFieldDefinitions();
271     foreach ( $vardefFields as $name => $properties ) {
272         $fieldlist[$name] = $properties;
273         // fill in enums
274         if(isset($fieldlist[$name]['options']) && is_string($fieldlist[$name]['options']) && isset($app_list_strings[$fieldlist[$name]['options']]))
275             $fieldlist[$name]['options'] = $app_list_strings[$fieldlist[$name]['options']];
276         // Bug 32626: fall back on checking the mod_strings if not in the app_list_strings
277         elseif(isset($fieldlist[$name]['options']) && is_string($fieldlist[$name]['options']) && isset($mod_strings[$fieldlist[$name]['options']]))
278             $fieldlist[$name]['options'] = $mod_strings[$fieldlist[$name]['options']];
279         // Bug 22730: make sure all enums have the ability to select blank as the default value.
280         if(!isset($fieldlist[$name]['options']['']))
281             $fieldlist[$name]['options'][''] = '';
282     }
283     // fill in function return values
284     if ( !in_array($fieldname,array('email1','email2')) ) {
285         if (!empty($fieldlist[$fieldname]['function']['returns']) && $fieldlist[$fieldname]['function']['returns'] == 'html'){
286             $function = $fieldlist[$fieldname]['function']['name'];
287             // include various functions required in the various vardefs
288             if ( isset($fieldlist[$fieldname]['function']['include']) && is_file($fieldlist[$fieldname]['function']['include']))
289                 require_once($fieldlist[$fieldname]['function']['include']);
290             $value = $function($focus, $fieldname, $value, 'EditView');
291             // Bug 22730 - add a hack for the currency type dropdown, since it's built by a function.
292             if ( preg_match('/getCurrency.*DropDown/s',$function)  )
293                 $value = str_ireplace('</select>','<option value="">'.$app_strings['LBL_NONE'].'</option></select>',$value);
294         }
295     }
296     $fieldlist[$fieldname]['value'] = $value;
297     $ss->assign("fields",$fieldlist);
298     $ss->assign("form_name",'importstep3');
299     $ss->assign("bean",$focus);
300     
301     // add in any additional strings
302     $ss->assign("MOD", $mod_strings);
303     $ss->assign("APP", $app_strings);
304     return $ss->fetch($file);
305 }