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.
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.
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
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
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.
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.
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 ********************************************************************************/
38 /*********************************************************************************
40 * Description: is a form helper
41 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42 * All Rights Reserved.
43 * Contributor(s): ______________________________________..
44 ********************************************************************************/
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
52 function checkRequired($prefix, $required)
54 foreach($required as $key)
56 if(!isset($_POST[$prefix.$key]) || number_empty($_POST[$prefix.$key]))
64 function populateFromPost($prefix, &$focus, $skipRetrieve=false) {
67 if(!empty($_REQUEST[$prefix.'record']) && !$skipRetrieve)
68 $focus->retrieve($_REQUEST[$prefix.'record']);
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;
75 require_once('include/SugarFields/SugarFieldHandler.php');
76 $sfh = new SugarFieldHandler();
78 foreach($focus->field_defs as $field=>$def) {
79 if ( $field == 'id' && !empty($focus->id) ) {
80 // Don't try and overwrite the ID
83 $type = !empty($def['custom_type']) ? $def['custom_type'] : $def['type'];
84 $sf = $sfh->getSugarField($type);
86 $sf->save($focus, $_POST, $field, $def, $prefix);
88 $GLOBALS['log']->fatal("Field '$field' does not have a SugarField handler");
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]);
97 $_POST[$prefix.$field] = encodeMultienumValue($_POST[$prefix.$field]);
100 $focus->$field = $_POST[$prefix.$field];
102 * overrides the passed value for booleans.
103 * this will be fully deprecated when the change to binary booleans is complete.
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];
109 if(is_int($bool) || ($bool === "0" || $bool === "1" || $bool === "2")) {
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;
116 $focus->$field = $opts[$selection];
118 } else if(!empty($focus->field_defs[$field]['isMultiSelect']) && !isset($_POST[$prefix.$field]) && isset($_POST[$prefix.$field . '_multiselect'])) {
124 foreach($focus->additional_column_fields as $field) {
125 if(isset($_POST[$prefix.$field])) {
126 $value = $_POST[$prefix.$field];
127 $focus->$field = $value;
133 function add_hidden_elements($key, $value) {
137 // if it's an array, we need to loop into the array and use square brackets []
138 if (is_array($value)) {
139 foreach ($value as $k=>$v) {
140 $elements .= "<input type='hidden' name='$key"."[$k]' value='$v'>\n";
143 $elements = "<input type='hidden' name='$key' value='$value'>\n";
150 function getPostToForm($ignore='', $isRegularExpression=false)
153 if(!empty($ignore) && $isRegularExpression) {
154 foreach ($_POST as $key=>$value){
155 if(!preg_match($ignore, $key)) {
156 $fields .= add_hidden_elements($key, $value);
160 foreach ($_POST as $key=>$value){
161 if($key != $ignore) {
162 $fields .= add_hidden_elements($key, $value);
169 function getGetToForm($ignore='', $usePostAsAuthority = false)
172 foreach ($_GET as $key=>$value)
175 if(!$usePostAsAuthority || !isset($_POST[$key])){
176 $fields.= "<input type='hidden' name='$key' value='$value'>";
183 function getAnyToForm($ignore='', $usePostAsAuthority = false)
185 $fields = getPostToForm($ignore);
186 $fields .= getGetToForm($ignore, $usePostAsAuthority);
191 function handleRedirect($return_id='', $return_module='', $additionalFlags = false)
193 if(isset($_REQUEST['return_url']) && $_REQUEST['return_url'] != "")
195 header("Location: ". $_REQUEST['return_url']);
199 $url = buildRedirectURL($return_id, $return_module);
204 //eggsurplus: abstract to simplify unit testing
205 function buildRedirectURL($return_id='', $return_module='')
207 if(isset($_REQUEST['return_module']) && $_REQUEST['return_module'] != "")
209 $return_module = $_REQUEST['return_module'];
213 $return_module = $return_module;
215 if(isset($_REQUEST['return_action']) && $_REQUEST['return_action'] != "")
218 //if we are doing a "Close and Create New"
219 if(isCloseAndCreateNewPressed())
221 $return_action = "EditView";
222 $isDuplicate = "true";
225 // Meeting Integration
226 if(isset($_REQUEST['meetingIntegrationFlag']) && $_REQUEST['meetingIntegrationFlag'] == 1) {
227 $additionalFlags = array('meetingIntegrationShowForm' => '1');
229 // END Meeting Integration
231 // if we create a new record "Save", we want to redirect to the DetailView
232 else if(isset($_REQUEST['action']) && $_REQUEST['action'] == "Save"
233 && $_REQUEST['return_module'] != 'Activities'
234 && $_REQUEST['return_module'] != 'Home'
235 && $_REQUEST['return_module'] != 'Forecasts'
236 && $_REQUEST['return_module'] != 'Calendar'
237 && $_REQUEST['return_module'] != 'MailMerge'
240 $return_action = 'DetailView';
241 } elseif($_REQUEST['return_module'] == 'Activities' || $_REQUEST['return_module'] == 'Calendar') {
242 $return_module = $_REQUEST['module'];
243 $return_action = $_REQUEST['return_action'];
244 // wp: return action needs to be set for one-click close in task list
248 // if we "Cancel", we go back to the list view.
249 $return_action = $_REQUEST['return_action'];
254 $return_action = "DetailView";
257 if(isset($_REQUEST['return_id']) && $_REQUEST['return_id'] != "")
259 $return_id = $_REQUEST['return_id'];
263 if(isset($additionalFlags) && !empty($additionalFlags)) {
264 foreach($additionalFlags as $k => $v) {
265 $add .= "&{$k}={$v}";
269 if (!isset($isDuplicate) || !$isDuplicate)
271 $url="index.php?action=$return_action&module=$return_module&record=$return_id&return_module=$return_module&return_action=$return_action{$add}";
272 if(isset($_REQUEST['offset']) && empty($_REQUEST['duplicateSave'])) {
273 $url .= "&offset=".$_REQUEST['offset'];
275 if(!empty($_REQUEST['ajax_load']))
278 'content' => "<script>SUGAR.ajaxUI.loadContent('$url');</script>\n",
280 'module' => $return_module,
281 'label' => translate($return_module),
284 $json = getJSONobj();
285 echo $json->encode($ajax_ret);
287 return "Location: $url";
290 $standard = "action=$return_action&module=$return_module&record=$return_id&isDuplicate=true&return_module=$return_module&return_action=$return_action&status=$status";
291 $url="index.php?{$standard}{$add}";
292 if(!empty($_REQUEST['ajax_load']))
295 'content' => "<script>SUGAR.ajaxUI.loadContent('$url');</script>\n",
297 'module' => $return_module,
298 'label' => translate($return_module),
301 $json = getJSONobj();
302 echo $json->encode($ajax_ret);
304 return "Location: $url";
309 function getLikeForEachWord($fieldname, $value, $minsize=4)
311 $value = trim($value);
312 $values = explode(' ',$value);
314 foreach($values as $val)
316 if(strlen($val) >= $minsize)
322 $ret .= ' '. $fieldname . ' LIKE %'.$val.'%';
330 function isCloseAndCreateNewPressed() {
331 return isset($_REQUEST['action']) &&
332 $_REQUEST['action'] == "Save" &&
333 isset($_REQUEST['isSaveAndNew']) &&
334 $_REQUEST['isSaveAndNew'] == 'true';
339 * Functions from Save2.php
340 * @see include/generic/Save2.php
343 function add_prospects_to_prospect_list($parent_id,$child_id)
345 $focus=BeanFactory::getBean('Prospects');
346 if(is_array($child_id)){
350 $uids = array($child_id);
354 foreach($focus->get_linked_fields() as $field => $def) {
355 if ($focus->load_relationship($field)) {
356 if ( $focus->$field->getRelatedModuleName() == 'ProspectLists' ) {
357 $relationship = $field;
363 if ( $relationship != '' ) {
364 foreach ( $uids as $id) {
365 $focus->retrieve($id);
366 $focus->load_relationship($relationship);
367 $focus->prospect_lists->add( $parent_id );
372 function add_to_prospect_list($query_panel,$parent_module,$parent_type,$parent_id,$child_id,$link_attribute,$link_type,$parent)
374 $GLOBALS['log']->debug('add_prospects_to_prospect_list:parameters:'.$query_panel);
375 $GLOBALS['log']->debug('add_prospects_to_prospect_list:parameters:'.$parent_module);
376 $GLOBALS['log']->debug('add_prospects_to_prospect_list:parameters:'.$parent_type);
377 $GLOBALS['log']->debug('add_prospects_to_prospect_list:parameters:'.$parent_id);
378 $GLOBALS['log']->debug('add_prospects_to_prospect_list:parameters:'.$child_id);
379 $GLOBALS['log']->debug('add_prospects_to_prospect_list:parameters:'.$link_attribute);
380 $GLOBALS['log']->debug('add_prospects_to_prospect_list:parameters:'.$link_type);
381 require_once('include/SubPanel/SubPanelTiles.php');
384 if (!class_exists($parent_type)) {
385 require_once('modules/'.cleanDirName($parent_module).'/'.cleanDirName($parent_type).'.php');
387 $focus = new $parent_type();
388 $focus->retrieve($parent_id);
389 if(empty($focus->id)) {
396 //if link_type is default then load relationship once and add all the child ids.
397 $relationship_attribute=$link_attribute;
399 //find all prospects based on the query
401 $subpanel = new SubPanelTiles($parent, $parent->module_dir);
402 $thisPanel=$subpanel->subpanel_definitions->load_subpanel($query_panel);
403 if(empty($thisPanel)) {
406 $result = SugarBean::get_union_related_list($parent, '', '', '', 0, -99,-99,'', $thisPanel);
408 if(!empty($result['list'])) {
409 foreach($result['list'] as $object) {
410 if ($link_type != 'default') {
411 $relationship_attribute=strtolower($object->$link_attribute);
413 $GLOBALS['log']->debug('add_prospects_to_prospect_list:relationship_attribute:'.$relationship_attribute);
414 // load relationship for the first time or on change of relationship atribute.
415 if (empty($focus->$relationship_attribute)) {
416 $focus->load_relationship($relationship_attribute);
419 $focus->$relationship_attribute->add($object->$child_id);
424 //Link rows returned by a report to parent record.
425 function save_from_report($report_id,$parent_id, $module_name, $relationship_attr_name) {
429 $GLOBALS['log']->debug("Save2: Linking with report output");
430 $GLOBALS['log']->debug("Save2:Report ID=".$report_id);
431 $GLOBALS['log']->debug("Save2:Parent ID=".$parent_id);
432 $GLOBALS['log']->debug("Save2:Module Name=".$module_name);
433 $GLOBALS['log']->debug("Save2:Relationship Attribute Name=".$relationship_attr_name);
435 $bean_name = $beanList[$module_name];
436 $GLOBALS['log']->debug("Save2:Bean Name=".$bean_name);
437 require_once($beanFiles[$bean_name]);
438 $focus = new $bean_name();
440 $focus->retrieve($parent_id);
441 $focus->load_relationship($relationship_attr_name);
443 //fetch report definition.
444 global $current_language, $report_modules, $modules_report;
446 $mod_strings = return_module_language($current_language,"Reports");
449 $saved = new SavedReport();
450 $saved->disable_row_level_security = true;
451 $saved->retrieve($report_id, false);
453 //initiailize reports engine with the report definition.
454 require_once('modules/Reports/SubpanelFromReports.php');
455 $report = new SubpanelFromReports($saved);
456 $report->run_query();
458 $sql = $report->query_list[0];
459 $GLOBALS['log']->debug("Save2:Report Query=".$sql);
460 $result = $report->db->query($sql);
461 while($row = $report->db->fetchByAssoc($result))
463 $focus->$relationship_attr_name->add($row['primaryid']);