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