]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/formbase.php
Release 6.2.0
[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-2011 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 getPostToForm($ignore='', $isRegularExpression=false)
136 {
137         $fields = '';
138         if(!empty($ignore) && $isRegularExpression) {
139                 foreach ($_POST as $key=>$value){
140                         if(!preg_match($ignore, $key)) {
141                                 $fields.= "<input type='hidden' name='$key' value='$value'>\n";
142                         }
143                 }       
144         } else {
145                 foreach ($_POST as $key=>$value){
146                         if($key != $ignore) {
147                            $fields.= "<input type='hidden' name='$key' value='$value'>\n";
148                         }
149                 }
150         }
151         return $fields;
152 }
153
154 function getGetToForm($ignore='', $usePostAsAuthority = false)
155 {
156         $fields = '';
157         foreach ($_GET as $key=>$value)
158         {
159                 if($key != $ignore){
160                         if(!$usePostAsAuthority || !isset($_POST[$key])){
161                                 $fields.= "<input type='hidden' name='$key' value='$value'>";
162                         }
163                 }
164         }
165         return $fields;
166
167 }
168 function getAnyToForm($ignore='', $usePostAsAuthority = false)
169 {
170         $fields = getPostToForm($ignore);
171         $fields .= getGetToForm($ignore, $usePostAsAuthority);
172         return $fields;
173
174 }
175
176 function handleRedirect($return_id='', $return_module='')
177 {
178         if(isset($_REQUEST['return_url']) && $_REQUEST['return_url'] != "")
179         {
180                 header("Location: ". $_REQUEST['return_url']);
181                 exit;
182         }
183
184         if(isset($_REQUEST['return_module']) && $_REQUEST['return_module'] != "")
185         {
186                 $return_module = $_REQUEST['return_module'];
187         }
188         else
189         {
190                 $return_module = $return_module;
191         }
192         if(isset($_REQUEST['return_action']) && $_REQUEST['return_action'] != "")
193         {
194             
195            //if we are doing a "Close and Create New"
196         if(isCloseAndCreateNewPressed())
197         {
198             $return_action = "EditView";    
199             $isDuplicate = "true";        
200             $status = "";
201             
202             // Meeting Integration
203             if(isset($_REQUEST['meetingIntegrationFlag']) && $_REQUEST['meetingIntegrationFlag'] == 1) {
204                 $additionalFlags = array('meetingIntegrationShowForm' => '1');
205             }
206             // END Meeting Integration
207         } 
208                 // if we create a new record "Save", we want to redirect to the DetailView
209                 else if($_REQUEST['action'] == "Save" 
210                         && $_REQUEST['return_module'] != 'Activities'
211                         && $_REQUEST['return_module'] != 'Home' 
212                         && $_REQUEST['return_module'] != 'Forecasts' 
213                         && $_REQUEST['return_module'] != 'Calendar'
214                         && $_REQUEST['return_module'] != 'MailMerge'
215                         ) 
216                         {
217                             $return_action = 'DetailView';
218                         } elseif($_REQUEST['return_module'] == 'Activities' || $_REQUEST['return_module'] == 'Calendar') {
219                         $return_module = $_REQUEST['module'];
220                         $return_action = $_REQUEST['return_action']; 
221                         // wp: return action needs to be set for one-click close in task list
222                 } 
223                 else 
224                 {
225                         // if we "Cancel", we go back to the list view.
226                         $return_action = $_REQUEST['return_action'];
227                 }
228         }
229         else
230         {
231                 $return_action = "DetailView";
232         }
233         
234         if(isset($_REQUEST['return_id']) && $_REQUEST['return_id'] != "")
235         {
236                 $return_id = $_REQUEST['return_id'];
237         }
238     
239     if (!isset($isDuplicate) || !$isDuplicate)
240     {
241         header("Location: index.php?action=$return_action&module=$return_module&record=$return_id&return_module=$return_module&return_action=$return_action");
242     } else {
243         $standard = "action=$return_action&module=$return_module&record=$return_id&isDuplicate=true&return_module=$return_module&return_action=$return_action&status=$status";
244                 $add = '';
245
246         if(isset($additionalFlags) && !empty($additionalFlags)) {
247                 foreach($additionalFlags as $k => $v) {
248                         if(!empty($add)) {
249                                 $add .= "&";
250                         }
251                         $add .= "{$k}={$v}";
252                 }
253         }
254         if(!empty($add)) {
255                 $add = "&" . $add;
256         }
257         header("Location: index.php?{$standard}{$add}");
258     }
259         exit;
260 }
261
262 function getLikeForEachWord($fieldname, $value, $minsize=4)
263 {
264         $value = trim($value);
265         $values = explode(' ',$value);
266         $ret = '';
267         foreach($values as $val)
268         {
269                 if(strlen($val) >= $minsize)
270                 {
271                         if(!empty($ret))
272                         {
273                                 $ret .= ' or';
274                         }
275                         $ret .= ' '. $fieldname . ' LIKE %'.$val.'%';
276                 }
277
278         }
279
280
281 }
282
283 function isCloseAndCreateNewPressed() {
284     return isset($_REQUEST['action']) && 
285            $_REQUEST['action'] == "Save" &&
286            isset($_REQUEST['isSaveAndNew']) && 
287            $_REQUEST['isSaveAndNew'] == 'true'; 
288 }
289
290
291 ?>