]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/formbase.php
Release 6.5.6
[Github/sugarcrm.git] / include / formbase.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 /*********************************************************************************
39
40  * Description:  is a form helper
41  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42  * All Rights Reserved.
43  * Contributor(s): ______________________________________..
44  ********************************************************************************/
45
46 /**
47  * Check for null or zero for list of values
48  * @param $prefix the prefix of value to be checked
49  * @param $required array of value to be checked
50  * @return boolean true if all values are set in the array
51  */
52 function checkRequired($prefix, $required)
53 {
54         foreach($required as $key)
55         {
56                 if(!isset($_POST[$prefix.$key]) || number_empty($_POST[$prefix.$key]))
57                 {
58                         return false;
59                 }
60         }
61         return true;
62 }
63
64 function populateFromPost($prefix, &$focus, $skipRetrieve=false) {
65         global $current_user;
66
67         if(!empty($_REQUEST[$prefix.'record']) && !$skipRetrieve)
68                 $focus->retrieve($_REQUEST[$prefix.'record']);
69
70         if(!empty($_POST['assigned_user_id']) && 
71             ($focus->assigned_user_id != $_POST['assigned_user_id']) && 
72             ($_POST['assigned_user_id'] != $current_user->id)) {
73                 $GLOBALS['check_notify'] = true;
74         }
75     require_once('include/SugarFields/SugarFieldHandler.php');
76     $sfh = new SugarFieldHandler();
77    
78         foreach($focus->field_defs as $field=>$def) {
79         if ( $field == 'id' && !empty($focus->id) ) {
80             // Don't try and overwrite the ID
81             continue;
82         }
83             $type = !empty($def['custom_type']) ? $def['custom_type'] : $def['type'];
84                 $sf = $sfh->getSugarField($type);
85         if($sf != null){
86             $sf->save($focus, $_POST, $field, $def, $prefix);
87         } else {
88             $GLOBALS['log']->fatal("Field '$field' does not have a SugarField handler");
89         }
90
91 /*
92         if(isset($_POST[$prefix.$field])) {
93                         if(is_array($_POST[$prefix.$field]) && !empty($focus->field_defs[$field]['isMultiSelect'])) {
94                                 if($_POST[$prefix.$field][0] === '' && !empty($_POST[$prefix.$field][1]) ) {
95                                         unset($_POST[$prefix.$field][0]);
96                                 }
97                                 $_POST[$prefix.$field] = encodeMultienumValue($_POST[$prefix.$field]);  
98                         }
99
100                         $focus->$field = $_POST[$prefix.$field];
101                         /* 
102                          * overrides the passed value for booleans.
103                          * this will be fully deprecated when the change to binary booleans is complete.
104                          /
105                         if(isset($focus->field_defs[$prefix.$field]) && $focus->field_defs[$prefix.$field]['type'] == 'bool' && isset($focus->field_defs[$prefix.$field]['options'])) {
106                                 $opts = explode("|", $focus->field_defs[$prefix.$field]['options']);
107                                 $bool = $_POST[$prefix.$field];
108
109                                 if(is_int($bool) || ($bool === "0" || $bool === "1" || $bool === "2")) {
110                                         // 1=on, 2=off
111                                         $selection = ($_POST[$prefix.$field] == "0") ? 1 : 0;
112                                 } elseif(is_bool($_POST[$prefix.$field])) {
113                                         // true=on, false=off
114                                         $selection = ($_POST[$prefix.$field]) ? 0 : 1;
115                                 }
116                                 $focus->$field = $opts[$selection];
117                         }
118                 } else if(!empty($focus->field_defs[$field]['isMultiSelect']) && !isset($_POST[$prefix.$field]) && isset($_POST[$prefix.$field . '_multiselect'])) {
119                         $focus->$field = '';
120                 }
121 */
122         }
123
124         foreach($focus->additional_column_fields as $field) {
125                 if(isset($_POST[$prefix.$field])) {
126                         $value = $_POST[$prefix.$field];
127                         $focus->$field = $value;
128                 }
129         }
130
131         return $focus;
132 }
133
134
135 function add_hidden_elements($key, $value) {
136
137     $elements = '';
138
139     // if it's an array, we need to loop into the array and use square brackets []
140     if (is_array($value)) {
141         foreach ($value as $k=>$v) {
142             $elements .= "<input type='hidden' name='$key"."[$k]' value='$v'>\n";
143         }
144     } else {
145         $elements = "<input type='hidden' name='$key' value='$value'>\n";
146     }
147
148     return $elements;
149 }
150
151
152 function getPostToForm($ignore='', $isRegularExpression=false)
153 {
154         $fields = '';
155         if(!empty($ignore) && $isRegularExpression) {
156                 foreach ($_POST as $key=>$value){
157                         if(!preg_match($ignore, $key)) {
158                                 $fields .= add_hidden_elements($key, $value);
159                         }
160                 }       
161         } else {
162                 foreach ($_POST as $key=>$value){
163                         if($key != $ignore) {
164                                 $fields .= add_hidden_elements($key, $value);
165                         }
166                 }
167         }
168         return $fields;
169 }
170
171 function getGetToForm($ignore='', $usePostAsAuthority = false)
172 {
173         $fields = '';
174         foreach ($_GET as $key=>$value)
175         {
176                 if($key != $ignore){
177                         if(!$usePostAsAuthority || !isset($_POST[$key])){
178                                 $fields.= "<input type='hidden' name='$key' value='$value'>";
179                         }
180                 }
181         }
182         return $fields;
183
184 }
185 function getAnyToForm($ignore='', $usePostAsAuthority = false)
186 {
187         $fields = getPostToForm($ignore);
188         $fields .= getGetToForm($ignore, $usePostAsAuthority);
189         return $fields;
190
191 }
192
193 function handleRedirect($return_id='', $return_module='', $additionalFlags = false)
194 {
195         if(isset($_REQUEST['return_url']) && $_REQUEST['return_url'] != "")
196         {
197                 header("Location: ". $_REQUEST['return_url']);
198                 exit;
199         }
200
201         $url = buildRedirectURL($return_id, $return_module);
202         header($url);
203         exit;   
204 }
205
206 //eggsurplus: abstract to simplify unit testing
207 function buildRedirectURL($return_id='', $return_module='') 
208 {
209     if(isset($_REQUEST['return_module']) && $_REQUEST['return_module'] != "")
210         {
211                 $return_module = $_REQUEST['return_module'];
212         }
213         else
214         {
215                 $return_module = $return_module;
216         }
217         if(isset($_REQUEST['return_action']) && $_REQUEST['return_action'] != "")
218         {
219             
220            //if we are doing a "Close and Create New"
221         if(isCloseAndCreateNewPressed())
222         {
223             $return_action = "EditView";    
224             $isDuplicate = "true";        
225             $status = "";
226             
227             // Meeting Integration
228             if(isset($_REQUEST['meetingIntegrationFlag']) && $_REQUEST['meetingIntegrationFlag'] == 1) {
229                 $additionalFlags = array('meetingIntegrationShowForm' => '1');
230             }
231             // END Meeting Integration
232         } 
233                 // if we create a new record "Save", we want to redirect to the DetailView
234                 else if(isset($_REQUEST['action']) && $_REQUEST['action'] == "Save" 
235                         && $_REQUEST['return_module'] != 'Activities'
236                         && $_REQUEST['return_module'] != 'Home' 
237                         && $_REQUEST['return_module'] != 'Forecasts' 
238                         && $_REQUEST['return_module'] != 'Calendar'
239                         && $_REQUEST['return_module'] != 'MailMerge'
240                         ) 
241                         {
242                             $return_action = 'DetailView';
243                         } elseif($_REQUEST['return_module'] == 'Activities' || $_REQUEST['return_module'] == 'Calendar') {
244                         $return_module = $_REQUEST['module'];
245                         $return_action = $_REQUEST['return_action']; 
246                         // wp: return action needs to be set for one-click close in task list
247                 } 
248                 else 
249                 {
250                         // if we "Cancel", we go back to the list view.
251                         $return_action = $_REQUEST['return_action'];
252                 }
253         }
254         else
255         {
256                 $return_action = "DetailView";
257         }
258         
259         if(isset($_REQUEST['return_id']) && $_REQUEST['return_id'] != "")
260         {
261                 $return_id = $_REQUEST['return_id'];
262         }
263
264     $add = "";
265     if(isset($additionalFlags) && !empty($additionalFlags)) {
266         foreach($additionalFlags as $k => $v) {
267             $add .= "&{$k}={$v}";
268         }
269     }
270     
271     if (!isset($isDuplicate) || !$isDuplicate)
272     {
273         $url="index.php?action=$return_action&module=$return_module&record=$return_id&return_module=$return_module&return_action=$return_action{$add}";
274         if(isset($_REQUEST['offset']) && empty($_REQUEST['duplicateSave'])) {
275             $url .= "&offset=".$_REQUEST['offset'];
276         }
277         if(!empty($_REQUEST['ajax_load']))
278         {
279             $ajax_ret = array(
280                 'content' => "<script>SUGAR.ajaxUI.loadContent('$url');</script>\n",
281                 'menu' => array(
282                     'module' => $return_module,
283                     'label' => translate($return_module),
284                 ),
285             );
286             $json = getJSONobj();
287             echo $json->encode($ajax_ret);
288         } else {
289             return "Location: $url";
290         }
291     } else {
292         $standard = "action=$return_action&module=$return_module&record=$return_id&isDuplicate=true&return_module=$return_module&return_action=$return_action&status=$status";
293         $url="index.php?{$standard}{$add}";
294         if(!empty($_REQUEST['ajax_load']))
295         {
296             $ajax_ret = array(
297                  'content' => "<script>SUGAR.ajaxUI.loadContent('$url');</script>\n",
298                  'menu' => array(
299                      'module' => $return_module,
300                      'label' => translate($return_module),
301                  ),
302             );
303             $json = getJSONobj();
304             echo $json->encode($ajax_ret);
305         } else {
306             return "Location: $url";
307         }
308     }
309 }
310
311 function getLikeForEachWord($fieldname, $value, $minsize=4)
312 {
313         $value = trim($value);
314         $values = explode(' ',$value);
315         $ret = '';
316         foreach($values as $val)
317         {
318                 if(strlen($val) >= $minsize)
319                 {
320                         if(!empty($ret))
321                         {
322                                 $ret .= ' or';
323                         }
324                         $ret .= ' '. $fieldname . ' LIKE %'.$val.'%';
325                 }
326
327         }
328
329
330 }
331
332 function isCloseAndCreateNewPressed() {
333     return isset($_REQUEST['action']) && 
334            $_REQUEST['action'] == "Save" &&
335            isset($_REQUEST['isSaveAndNew']) && 
336            $_REQUEST['isSaveAndNew'] == 'true'; 
337 }
338
339
340 /**
341  * Functions from Save2.php
342  * @see include/generic/Save2.php
343  */
344
345 function add_prospects_to_prospect_list($parent_id,$child_id)
346 {
347     $focus=BeanFactory::getBean('Prospects');
348     if(is_array($child_id)){
349         $uids = $child_id;
350     }
351     else{
352         $uids = array($child_id);
353     }
354
355     $relationship = '';
356     foreach($focus->get_linked_fields() as $field => $def) {
357         if ($focus->load_relationship($field)) {
358             if ( $focus->$field->getRelatedModuleName() == 'ProspectLists' ) {
359                 $relationship = $field;
360                 break;
361             }
362         }
363     }
364
365     if ( $relationship != '' ) {
366         foreach ( $uids as $id) {
367             $focus->retrieve($id);
368             $focus->load_relationship($relationship);
369             $focus->prospect_lists->add( $parent_id );
370         }
371     }
372 }
373
374 function add_to_prospect_list($query_panel,$parent_module,$parent_type,$parent_id,$child_id,$link_attribute,$link_type,$parent)
375 {
376     $GLOBALS['log']->debug('add_prospects_to_prospect_list:parameters:'.$query_panel);
377     $GLOBALS['log']->debug('add_prospects_to_prospect_list:parameters:'.$parent_module);
378     $GLOBALS['log']->debug('add_prospects_to_prospect_list:parameters:'.$parent_type);
379     $GLOBALS['log']->debug('add_prospects_to_prospect_list:parameters:'.$parent_id);
380     $GLOBALS['log']->debug('add_prospects_to_prospect_list:parameters:'.$child_id);
381     $GLOBALS['log']->debug('add_prospects_to_prospect_list:parameters:'.$link_attribute);
382     $GLOBALS['log']->debug('add_prospects_to_prospect_list:parameters:'.$link_type);
383     require_once('include/SubPanel/SubPanelTiles.php');
384
385
386     if (!class_exists($parent_type)) {
387         require_once('modules/'.cleanDirName($parent_module).'/'.cleanDirName($parent_type).'.php');
388     }
389     $focus = new $parent_type();
390     $focus->retrieve($parent_id);
391     if(empty($focus->id)) {
392         return false;
393     }
394     if(empty($parent)) {
395         return false;
396     }
397
398     //if link_type is default then load relationship once and add all the child ids.
399     $relationship_attribute=$link_attribute;
400
401     //find all prospects based on the query
402
403     $subpanel = new SubPanelTiles($parent, $parent->module_dir);
404     $thisPanel=$subpanel->subpanel_definitions->load_subpanel($query_panel);
405     if(empty($thisPanel)) {
406         return false;
407     }
408     $result = SugarBean::get_union_related_list($parent, '', '', '', 0, -99,-99,'', $thisPanel);
409
410     if(!empty($result['list'])) {
411         foreach($result['list'] as $object) {
412             if ($link_type != 'default') {
413                 $relationship_attribute=strtolower($object->$link_attribute);
414             }
415             $GLOBALS['log']->debug('add_prospects_to_prospect_list:relationship_attribute:'.$relationship_attribute);
416             // load relationship for the first time or on change of relationship atribute.
417             if (empty($focus->$relationship_attribute)) {
418                 $focus->load_relationship($relationship_attribute);
419             }
420             //add
421             $focus->$relationship_attribute->add($object->$child_id);
422         }
423     }
424 }
425
426 //Link rows returned by a report to parent record.
427 function save_from_report($report_id,$parent_id, $module_name, $relationship_attr_name) {
428     global $beanFiles;
429     global $beanList;
430
431     $GLOBALS['log']->debug("Save2: Linking with report output");
432     $GLOBALS['log']->debug("Save2:Report ID=".$report_id);
433     $GLOBALS['log']->debug("Save2:Parent ID=".$parent_id);
434     $GLOBALS['log']->debug("Save2:Module Name=".$module_name);
435     $GLOBALS['log']->debug("Save2:Relationship Attribute Name=".$relationship_attr_name);
436
437     $bean_name = $beanList[$module_name];
438     $GLOBALS['log']->debug("Save2:Bean Name=".$bean_name);
439     require_once($beanFiles[$bean_name]);
440     $focus = new $bean_name();
441
442     $focus->retrieve($parent_id);
443     $focus->load_relationship($relationship_attr_name);
444
445     //fetch report definition.
446     global $current_language, $report_modules, $modules_report;
447
448     $mod_strings = return_module_language($current_language,"Reports");
449
450
451     $saved = new SavedReport();
452     $saved->disable_row_level_security = true;
453     $saved->retrieve($report_id, false);
454
455     //initiailize reports engine with the report definition.
456     require_once('modules/Reports/SubpanelFromReports.php');
457     $report = new SubpanelFromReports($saved);
458     $report->run_query();
459
460     $sql = $report->query_list[0];
461     $GLOBALS['log']->debug("Save2:Report Query=".$sql);
462     $result = $report->db->query($sql);
463     while($row = $report->db->fetchByAssoc($result))
464     {
465         $focus->$relationship_attr_name->add($row['primaryid']);
466     }
467 }
468
469 ?>