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.
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]))
65 * Populating bean from $_POST
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
73 function populateFromPost($prefix, &$focus, $skipRetrieve = false, $checkACL = false)
77 if(!empty($_REQUEST[$prefix.'record']) && !$skipRetrieve)
78 $focus->retrieve($_REQUEST[$prefix.'record']);
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;
85 require_once('include/SugarFields/SugarFieldHandler.php');
86 $sfh = new SugarFieldHandler();
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') {
94 if (empty($def['source']) || $def['source'] != 'non-db') {
97 if (empty($def['id_name']) || $def['id_name'] == $field) {
100 $relatedFields[$def['id_name']] = $field;
103 foreach($focus->field_defs as $field=>$def) {
104 if ( $field == 'id' && !empty($focus->id) ) {
105 // Don't try and overwrite the ID
110 $type = !empty($def['custom_type']) ? $def['custom_type'] : $def['type'];
111 $sf = $sfh->getSugarField($type);
113 $sf->save($focus, $_POST, $field, $def, $prefix);
115 $GLOBALS['log']->fatal("Field '$field' does not have a SugarField handler");
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]);
124 $_POST[$prefix.$field] = encodeMultienumValue($_POST[$prefix.$field]);
127 $focus->$field = $_POST[$prefix.$field];
129 * overrides the passed value for booleans.
130 * this will be fully deprecated when the change to binary booleans is complete.
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];
136 if(is_int($bool) || ($bool === "0" || $bool === "1" || $bool === "2")) {
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;
143 $focus->$field = $opts[$selection];
145 } else if(!empty($focus->field_defs[$field]['isMultiSelect']) && !isset($_POST[$prefix.$field]) && isset($_POST[$prefix.$field . '_multiselect'])) {
151 foreach($focus->additional_column_fields as $field) {
152 if(isset($_POST[$prefix.$field])) {
153 $value = $_POST[$prefix.$field];
154 $focus->$field = $value;
160 function add_hidden_elements($key, $value) {
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";
170 $elements = "<input type='hidden' name='$key' value='$value'>\n";
177 function getPostToForm($ignore='', $isRegularExpression=false)
180 if(!empty($ignore) && $isRegularExpression) {
181 foreach ($_POST as $key=>$value){
182 if(!preg_match($ignore, $key)) {
183 $fields .= add_hidden_elements($key, $value);
187 foreach ($_POST as $key=>$value){
188 if($key != $ignore) {
189 $fields .= add_hidden_elements($key, $value);
196 function getGetToForm($ignore='', $usePostAsAuthority = false)
199 foreach ($_GET as $key=>$value)
202 if(!$usePostAsAuthority || !isset($_POST[$key])){
203 $fields.= "<input type='hidden' name='$key' value='$value'>";
210 function getAnyToForm($ignore='', $usePostAsAuthority = false)
212 $fields = getPostToForm($ignore);
213 $fields .= getGetToForm($ignore, $usePostAsAuthority);
218 function handleRedirect($return_id='', $return_module='', $additionalFlags = false)
220 if(isset($_REQUEST['return_url']) && $_REQUEST['return_url'] != "")
222 header("Location: ". $_REQUEST['return_url']);
226 $url = buildRedirectURL($return_id, $return_module);
231 //eggsurplus: abstract to simplify unit testing
232 function buildRedirectURL($return_id='', $return_module='')
234 if(isset($_REQUEST['return_module']) && $_REQUEST['return_module'] != "")
236 $return_module = $_REQUEST['return_module'];
240 $return_module = $return_module;
242 if(isset($_REQUEST['return_action']) && $_REQUEST['return_action'] != "")
245 //if we are doing a "Close and Create New"
246 if(isCloseAndCreateNewPressed())
248 $return_action = "EditView";
249 $isDuplicate = "true";
252 // Meeting Integration
253 if(isset($_REQUEST['meetingIntegrationFlag']) && $_REQUEST['meetingIntegrationFlag'] == 1) {
254 $additionalFlags = array('meetingIntegrationShowForm' => '1');
256 // END Meeting Integration
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'
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
275 // if we "Cancel", we go back to the list view.
276 $return_action = $_REQUEST['return_action'];
281 $return_action = "DetailView";
284 if(isset($_REQUEST['return_id']) && $_REQUEST['return_id'] != "")
286 $return_id = $_REQUEST['return_id'];
290 if(isset($additionalFlags) && !empty($additionalFlags)) {
291 foreach($additionalFlags as $k => $v) {
292 $add .= "&{$k}={$v}";
296 if (!isset($isDuplicate) || !$isDuplicate)
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'];
302 if(!empty($_REQUEST['ajax_load']))
305 'content' => "<script>SUGAR.ajaxUI.loadContent('$url');</script>\n",
307 'module' => $return_module,
308 'label' => translate($return_module),
311 $json = getJSONobj();
312 echo $json->encode($ajax_ret);
314 return "Location: $url";
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']))
322 'content' => "<script>SUGAR.ajaxUI.loadContent('$url');</script>\n",
324 'module' => $return_module,
325 'label' => translate($return_module),
328 $json = getJSONobj();
329 echo $json->encode($ajax_ret);
331 return "Location: $url";
336 function getLikeForEachWord($fieldname, $value, $minsize=4)
338 $value = trim($value);
339 $values = explode(' ',$value);
341 foreach($values as $val)
343 if(strlen($val) >= $minsize)
349 $ret .= ' '. $fieldname . ' LIKE %'.$val.'%';
357 function isCloseAndCreateNewPressed() {
358 return isset($_REQUEST['action']) &&
359 $_REQUEST['action'] == "Save" &&
360 isset($_REQUEST['isSaveAndNew']) &&
361 $_REQUEST['isSaveAndNew'] == 'true';
366 * Functions from Save2.php
367 * @see include/generic/Save2.php
370 function add_prospects_to_prospect_list($parent_id,$child_id)
372 $focus=BeanFactory::getBean('Prospects');
373 if(is_array($child_id)){
377 $uids = array($child_id);
381 foreach($focus->get_linked_fields() as $field => $def) {
382 if ($focus->load_relationship($field)) {
383 if ( $focus->$field->getRelatedModuleName() == 'ProspectLists' ) {
384 $relationship = $field;
390 if ( $relationship != '' ) {
391 foreach ( $uids as $id) {
392 $focus->retrieve($id);
393 $focus->load_relationship($relationship);
394 $focus->prospect_lists->add( $parent_id );
399 function add_to_prospect_list($query_panel,$parent_module,$parent_type,$parent_id,$child_id,$link_attribute,$link_type,$parent)
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');
411 if (!class_exists($parent_type)) {
412 require_once('modules/'.cleanDirName($parent_module).'/'.cleanDirName($parent_type).'.php');
414 $focus = new $parent_type();
415 $focus->retrieve($parent_id);
416 if(empty($focus->id)) {
423 //if link_type is default then load relationship once and add all the child ids.
424 $relationship_attribute=$link_attribute;
426 //find all prospects based on the query
428 $subpanel = new SubPanelTiles($parent, $parent->module_dir);
429 $thisPanel=$subpanel->subpanel_definitions->load_subpanel($query_panel);
430 if(empty($thisPanel)) {
434 // bugfix #57850 filter prospect list based on marketing_id (if it's present)
435 if (isset($_REQUEST['marketing_id']) && $_REQUEST['marketing_id'] != 'all')
437 $thisPanel->_instance_properties['function_parameters']['EMAIL_MARKETING_ID_VALUE'] = $_REQUEST['marketing_id'];
440 $result = SugarBean::get_union_related_list($parent, '', '', '', 0, -99,-99,'', $thisPanel);
442 if(!empty($result['list'])) {
443 foreach($result['list'] as $object) {
444 if ($link_type != 'default') {
445 $relationship_attribute=strtolower($object->$link_attribute);
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);
453 $focus->$relationship_attribute->add($object->$child_id);
458 //Link rows returned by a report to parent record.
459 function save_from_report($report_id,$parent_id, $module_name, $relationship_attr_name) {
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);
469 $GLOBALS['log']->debug("Save2:Bean Name=" . $module_name);
470 $focus = BeanFactory::newBean($module_name);
472 $focus->retrieve($parent_id);
473 $focus->load_relationship($relationship_attr_name);
475 //fetch report definition.
476 global $current_language, $report_modules, $modules_report;
478 $mod_strings = return_module_language($current_language,"Reports");
481 $saved = new SavedReport();
482 $saved->disable_row_level_security = true;
483 $saved->retrieve($report_id, false);
485 //initiailize reports engine with the report definition.
486 require_once('modules/Reports/SubpanelFromReports.php');
487 $report = new SubpanelFromReports($saved);
488 $report->run_query();
490 $sql = $report->query_list[0];
491 $GLOBALS['log']->debug("Save2:Report Query=".$sql);
492 $result = $report->db->query($sql);
494 $reportBean = BeanFactory::newBean($saved->module);
495 while($row = $report->db->fetchByAssoc($result))
497 $reportBean->id = $row['primaryid'];
498 $focus->$relationship_attr_name->add($reportBean);