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: Defines the base class for all data entities used throughout the
41 * application. The base class including its methods and variables is designed to
42 * be overloaded with module-specific methods and variables particular to the
43 * module's base entity class.
44 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
45 * All Rights Reserved.
46 *******************************************************************************/
48 require_once('modules/DynamicFields/DynamicField.php');
49 require_once("data/Relationships/RelationshipFactory.php");
56 * SugarBean is the base class for all business objects in Sugar. It implements
57 * the primary functionality needed for manipulating business objects: create,
58 * retrieve, update, delete. It allows for searching and retrieving list of records.
59 * It allows for retrieving related objects (e.g. contacts related to a specific account).
61 * In the current implementation, there can only be one bean per folder.
62 * Naming convention has the bean name be the same as the module and folder name.
63 * All bean names should be singular (e.g. Contact). The primary table name for
64 * a bean should be plural (e.g. contacts).
70 * A pointer to the database object
77 * Unique object identifier
84 * When createing a bean, you can specify a value in the id column as
85 * long as that value is unique. During save, if the system finds an
86 * id, it assumes it is an update. Setting new_with_id to true will
87 * make sure the system performs an insert instead of an update.
89 * @var BOOL -- default false
91 var $new_with_id = false;
95 * How deep logic hooks can go
98 protected $max_logic_depth = 10;
101 * Disble vardefs. This should be set to true only for beans that do not have varders. Tracker is an example
103 * @var BOOL -- default false
105 var $disable_vardefs = false;
109 * holds the full name of the user that an item is assigned to. Only used if notifications
110 * are turned on and going to be sent out.
114 var $new_assigned_user_name;
117 * An array of booleans. This array is cleared out when data is loaded.
118 * As date/times are converted, a "1" is placed under the key, the field is converted.
120 * @var Array of booleans
122 var $processed_dates_times = array();
125 * Whether to process date/time fields for storage in the database in GMT
129 var $process_save_dates =true;
132 * This signals to the bean that it is being saved in a mass mode.
133 * Examples of this kind of save are import and mass update.
134 * We turn off notificaitons of this is the case to make things more efficient.
138 var $save_from_post = true;
141 * When running a query on related items using the method: retrieve_by_string_fields
142 * this value will be set to true if more than one item matches the search criteria.
146 var $duplicates_found = false;
149 * true if this bean has been deleted, false otherwise.
156 * Should the date modified column of the bean be updated during save?
157 * This is used for admin level functionality that should not be updating
158 * the date modified. This is only used by sync to allow for updates to be
159 * replicated in a way that will not cause them to be replicated back.
163 var $update_date_modified = true;
166 * Should the modified by column of the bean be updated during save?
167 * This is used for admin level functionality that should not be updating
168 * the modified by column. This is only used by sync to allow for updates to be
169 * replicated in a way that will not cause them to be replicated back.
173 var $update_modified_by = true;
176 * Setting this to true allows for updates to overwrite the date_entered
180 var $update_date_entered = false;
183 * This allows for seed data to be created without using the current uesr to set the id.
184 * This should be replaced by altering the current user before the call to save.
188 //TODO This should be replaced by altering the current user before the call to save.
189 var $set_created_by = true;
194 * The database table where records of this Bean are stored.
198 var $table_name = '';
201 * This is the singular name of the bean. (i.e. Contact).
205 var $object_name = '';
207 /** Set this to true if you query contains a sub-select and bean is converting both select statements
208 * into count queries.
210 var $ungreedy_count=false;
213 * The name of the module folder for this type of bean.
217 var $module_dir = '';
218 var $module_name = '';
222 var $column_fields = array();
223 var $list_fields = array();
224 var $additional_column_fields = array();
225 var $relationship_fields = array();
226 var $current_notify_user;
227 var $fetched_row=false;
228 var $fetched_rel_row = array();
230 var $force_load_details = false;
231 var $optimistic_lock = false;
232 var $disable_custom_fields = false;
233 var $number_formatting_done = false;
234 var $process_field_encrypted=false;
236 * The default ACL type
238 var $acltype = 'module';
241 var $additional_meta_fields = array();
244 * Set to true in the child beans if the module supports importing
246 var $importable = false;
249 * Set to true in the child beans if the module use the special notification template
251 var $special_notification = false;
254 * Set to true if the bean is being dealt with in a workflow
256 var $in_workflow = false;
260 * By default it will be true but if any module is to be kept non visible
261 * to tracker, then its value needs to be overriden in that particular module to false.
264 var $tracker_visibility = true;
267 * Used to pass inner join string to ListView Data.
269 var $listview_inner_join = array();
272 * Set to true in <modules>/Import/views/view.step4.php if a module is being imported
274 var $in_import = false;
276 * A way to keep track of the loaded relationships so when we clone the object we can unset them.
280 protected $loaded_relationships = array();
283 * set to true if dependent fields updated
285 protected $is_updated_dependent_fields = false;
288 * Blowfish encryption key
291 static protected $field_key;
294 * Cache of fields which can contain files
298 static protected $fileFields = array();
301 * Constructor for the bean, it performs following tasks:
303 * 1. Initalized a database connections
304 * 2. Load the vardefs for the module implemeting the class. cache the entries
306 * 3. Setup row-level security preference
307 * All implementing classes must call this constructor using the parent::SugarBean() class.
312 global $dictionary, $current_user;
313 static $loaded_defs = array();
314 $this->db = DBManagerFactory::getInstance();
315 if (empty($this->module_name))
316 $this->module_name = $this->module_dir;
317 if((false == $this->disable_vardefs && empty($loaded_defs[$this->object_name])) || !empty($GLOBALS['reload_vardefs']))
319 VardefManager::loadVardef($this->module_dir, $this->object_name);
321 // build $this->column_fields from the field_defs if they exist
322 if (!empty($dictionary[$this->object_name]['fields'])) {
323 foreach ($dictionary[$this->object_name]['fields'] as $key=>$value_array) {
324 $column_fields[] = $key;
325 if(!empty($value_array['required']) && !empty($value_array['name'])) {
326 $this->required_fields[$value_array['name']] = 1;
329 $this->column_fields = $column_fields;
332 //setup custom fields
333 if(!isset($this->custom_fields) &&
334 empty($this->disable_custom_fields))
336 $this->setupCustomFields($this->module_dir);
338 //load up field_arrays from CacheHandler;
339 if(empty($this->list_fields))
340 $this->list_fields = $this->_loadCachedArray($this->module_dir, $this->object_name, 'list_fields');
341 if(empty($this->column_fields))
342 $this->column_fields = $this->_loadCachedArray($this->module_dir, $this->object_name, 'column_fields');
343 if(empty($this->required_fields))
344 $this->required_fields = $this->_loadCachedArray($this->module_dir, $this->object_name, 'required_fields');
346 if(isset($GLOBALS['dictionary'][$this->object_name]) && !$this->disable_vardefs)
348 $this->field_name_map = $dictionary[$this->object_name]['fields'];
349 $this->field_defs = $dictionary[$this->object_name]['fields'];
351 if(!empty($dictionary[$this->object_name]['optimistic_locking']))
353 $this->optimistic_lock=true;
356 $loaded_defs[$this->object_name]['column_fields'] =& $this->column_fields;
357 $loaded_defs[$this->object_name]['list_fields'] =& $this->list_fields;
358 $loaded_defs[$this->object_name]['required_fields'] =& $this->required_fields;
359 $loaded_defs[$this->object_name]['field_name_map'] =& $this->field_name_map;
360 $loaded_defs[$this->object_name]['field_defs'] =& $this->field_defs;
364 $this->column_fields =& $loaded_defs[$this->object_name]['column_fields'] ;
365 $this->list_fields =& $loaded_defs[$this->object_name]['list_fields'];
366 $this->required_fields =& $loaded_defs[$this->object_name]['required_fields'];
367 $this->field_name_map =& $loaded_defs[$this->object_name]['field_name_map'];
368 $this->field_defs =& $loaded_defs[$this->object_name]['field_defs'];
369 $this->added_custom_field_defs = true;
371 if(!isset($this->custom_fields) &&
372 empty($this->disable_custom_fields))
374 $this->setupCustomFields($this->module_dir, false);
376 if(!empty($dictionary[$this->object_name]['optimistic_locking']))
378 $this->optimistic_lock=true;
382 if($this->bean_implements('ACL') && !empty($GLOBALS['current_user'])){
383 $this->acl_fields = (isset($dictionary[$this->object_name]['acl_fields']) && $dictionary[$this->object_name]['acl_fields'] === false)?false:true;
385 $this->populateDefaultValues();
390 * Returns the object name. If object_name is not set, table_name is returned.
392 * All implementing classes must set a value for the object_name variable.
394 * @param array $arr row of data fetched from the database.
398 function getObjectName()
400 if ($this->object_name)
401 return $this->object_name;
403 // This is a quick way out. The generated metadata files have the table name
404 // as the key. The correct way to do this is to override this function
405 // in bean and return the object name. That requires changing all the beans
406 // as well as put the object name in the generator.
407 return $this->table_name;
411 * Returns a list of fields with their definitions that have the audited property set to true.
412 * Before calling this function, check whether audit has been enabled for the table/module or not.
413 * You would set the audit flag in the implemting module's vardef file.
416 * @see is_AuditEnabled
418 * Internal function, do not override.
420 function getAuditEnabledFieldDefinitions()
422 $aclcheck = $this->bean_implements('ACL');
423 $is_owner = $this->isOwner($GLOBALS['current_user']->id);
424 if (!isset($this->audit_enabled_fields))
427 $this->audit_enabled_fields=array();
428 foreach ($this->field_defs as $field => $properties)
433 !empty($properties['Audited']) || !empty($properties['audited']))
437 $this->audit_enabled_fields[$field]=$properties;
442 return $this->audit_enabled_fields;
446 * Return true if auditing is enabled for this object
447 * You would set the audit flag in the implemting module's vardef file.
451 * Internal function, do not override.
453 function is_AuditEnabled()
456 if (isset($dictionary[$this->getObjectName()]['audited']))
458 return $dictionary[$this->getObjectName()]['audited'];
469 * Returns the name of the audit table.
470 * Audit table's name is based on implementing class' table name.
472 * @return String Audit table name.
474 * Internal function, do not override.
476 function get_audit_table_name()
478 return $this->getTableName().'_audit';
482 * Returns the name of the custom table.
483 * Custom table's name is based on implementing class' table name.
485 * @return String Custom table name.
487 * Internal function, do not override.
489 public function get_custom_table_name()
491 return $this->getTableName().'_cstm';
495 * If auditing is enabled, create the audit table.
497 * Function is used by the install scripts and a repair utility in the admin panel.
499 * Internal function, do not override.
501 function create_audit_table()
504 $table_name=$this->get_audit_table_name();
506 require('metadata/audit_templateMetaData.php');
508 // Bug: 52583 Need ability to customize template for audit tables
509 $custom = 'custom/metadata/audit_templateMetaData_' . $this->getTableName() . '.php';
510 if (file_exists($custom))
515 $fieldDefs = $dictionary['audit']['fields'];
516 $indices = $dictionary['audit']['indices'];
518 // Renaming template indexes to fit the particular audit table (removed the brittle hard coding)
519 foreach($indices as $nr => $properties){
520 $indices[$nr]['name'] = 'idx_' . strtolower($this->getTableName()) . '_' . $properties['name'];
524 if(isset($dictionary['audit']['engine'])) {
525 $engine = $dictionary['audit']['engine'];
526 } else if(isset($dictionary[$this->getObjectName()]['engine'])) {
527 $engine = $dictionary[$this->getObjectName()]['engine'];
530 $this->db->createTableParams($table_name, $fieldDefs, $indices, $engine);
534 * Returns the implementing class' table name.
536 * All implementing classes set a value for the table_name variable. This value is returned as the
537 * table name. If not set, table name is extracted from the implementing module's vardef.
539 * @return String Table name.
541 * Internal function, do not override.
543 public function getTableName()
545 if(isset($this->table_name))
547 return $this->table_name;
550 return $dictionary[$this->getObjectName()]['table'];
554 * Returns field definitions for the implementing module.
556 * The definitions were loaded in the constructor.
558 * @return Array Field definitions.
560 * Internal function, do not override.
562 function getFieldDefinitions()
564 return $this->field_defs;
568 * Returns index definitions for the implementing module.
570 * The definitions were loaded in the constructor.
572 * @return Array Index definitions.
574 * Internal function, do not override.
576 function getIndices()
579 if(isset($dictionary[$this->getObjectName()]['indices']))
581 return $dictionary[$this->getObjectName()]['indices'];
587 * Returns field definition for the requested field name.
589 * The definitions were loaded in the constructor.
591 * @param string field name,
592 * @return Array Field properties or boolean false if the field doesn't exist
594 * Internal function, do not override.
596 function getFieldDefinition($name)
598 if ( !isset($this->field_defs[$name]) )
601 return $this->field_defs[$name];
605 * Returnss definition for the id field name.
607 * The definitions were loaded in the constructor.
609 * @return Array Field properties.
611 * Internal function, do not override.
613 function getPrimaryFieldDefinition()
615 $def = $this->getFieldDefinition("id");
617 $def = $this->getFieldDefinition(0);
620 $defs = $this->field_defs;
622 $def = current($defs);
627 * Returns the value for the requested field.
629 * When a row of data is fetched using the bean, all fields are created as variables in the context
630 * of the bean and then fetched values are set in these variables.
632 * @param string field name,
633 * @return varies Field value.
635 * Internal function, do not override.
637 function getFieldValue($name)
639 if (!isset($this->$name)){
642 if($this->$name === TRUE){
645 if($this->$name === FALSE){
652 * Basically undoes the effects of SugarBean::populateDefaultValues(); this method is best called right after object
655 public function unPopulateDefaultValues()
657 if ( !is_array($this->field_defs) )
660 foreach ($this->field_defs as $field => $value) {
661 if( !empty($this->$field)
662 && ((isset($value['default']) && $this->$field == $value['default']) || (!empty($value['display_default']) && $this->$field == $value['display_default']))
664 $this->$field = null;
667 if(!empty($this->$field) && !empty($value['display_default']) && in_array($value['type'], array('date', 'datetime', 'datetimecombo')) &&
668 $this->$field == $this->parseDateDefault($value['display_default'], ($value['type'] != 'date'))) {
669 $this->$field = null;
675 * Create date string from default value
677 * @param string $value
678 * @param bool $time Should be expect time set too?
681 protected function parseDateDefault($value, $time = false)
685 $dtAry = explode('&', $value, 2);
686 $dateValue = $timedate->getNow(true)->modify($dtAry[0]);
687 if(!empty($dtAry[1])) {
688 $timeValue = $timedate->fromString($dtAry[1]);
689 $dateValue->setTime($timeValue->hour, $timeValue->min, $timeValue->sec);
691 return $timedate->asUser($dateValue);
693 return $timedate->asUserDate($timedate->getNow(true)->modify($value));
697 function populateDefaultValues($force=false){
698 if ( !is_array($this->field_defs) )
700 foreach($this->field_defs as $field=>$value){
701 if((isset($value['default']) || !empty($value['display_default'])) && ($force || empty($this->$field))){
702 $type = $value['type'];
706 if(!empty($value['display_default'])){
707 $this->$field = $this->parseDateDefault($value['display_default']);
711 case 'datetimecombo':
712 if(!empty($value['display_default'])){
713 $this->$field = $this->parseDateDefault($value['display_default'], true);
717 if(empty($value['default']) && !empty($value['display_default']))
718 $this->$field = $value['display_default'];
720 $this->$field = $value['default'];
723 if(isset($this->$field)){
727 if ( isset($value['default']) && $value['default'] !== '' ) {
728 $this->$field = htmlentities($value['default'], ENT_QUOTES, 'UTF-8');
739 * Removes relationship metadata cache.
741 * Every module that has relationships defined with other modules, has this meta data cached. The cache is
742 * stores in 2 locations: relationships table and file system. This method clears the cache from both locations.
744 * @param string $key module whose meta cache is to be cleared.
745 * @param string $db database handle.
746 * @param string $tablename table name
747 * @param string $dictionary vardef for the module
748 * @param string $module_dir name of subdirectory where module is installed.
753 * Internal function, do not override.
755 function removeRelationshipMeta($key,$db,$tablename,$dictionary,$module_dir)
757 //load the module dictionary if not supplied.
758 if ((!isset($dictionary) or empty($dictionary)) && !empty($module_dir))
760 $filename='modules/'. $module_dir . '/vardefs.php';
761 if(file_exists($filename))
766 if (!is_array($dictionary) or !array_key_exists($key, $dictionary))
768 $GLOBALS['log']->fatal("removeRelationshipMeta: Metadata for table ".$tablename. " does not exist");
769 display_notice("meta data absent for table ".$tablename." keyed to $key ");
773 if (isset($dictionary[$key]['relationships']))
775 $RelationshipDefs = $dictionary[$key]['relationships'];
776 foreach ($RelationshipDefs as $rel_name)
778 Relationship::delete($rel_name,$db);
786 * This method has been deprecated.
788 * @see removeRelationshipMeta()
789 * @deprecated 4.5.1 - Nov 14, 2006
792 function remove_relationship_meta($key,$db,$log,$tablename,$dictionary,$module_dir)
794 SugarBean::removeRelationshipMeta($key,$db,$tablename,$dictionary,$module_dir);
799 * Populates the relationship meta for a module.
801 * It is called during setup/install. It is used statically to create relationship meta data for many-to-many tables.
803 * @param string $key name of the object.
804 * @param object $db database handle.
805 * @param string $tablename table, meta data is being populated for.
806 * @param array dictionary vardef dictionary for the object. *
807 * @param string module_dir name of subdirectory where module is installed.
808 * @param boolean $iscustom Optional,set to true if module is installed in a custom directory. Default value is false.
811 * Internal function, do not override.
813 function createRelationshipMeta($key,$db,$tablename,$dictionary,$module_dir,$iscustom=false)
815 //load the module dictionary if not supplied.
816 if (empty($dictionary) && !empty($module_dir))
820 $filename='custom/modules/' . $module_dir . '/Ext/Vardefs/vardefs.ext.php';
826 // a very special case for the Employees module
827 // this must be done because the Employees/vardefs.php does an include_once on
829 $filename='modules/Users/vardefs.php';
833 $filename='modules/'. $module_dir . '/vardefs.php';
837 if(file_exists($filename))
840 // cn: bug 7679 - dictionary entries defined as $GLOBALS['name'] not found
841 if(empty($dictionary) || !empty($GLOBALS['dictionary'][$key]))
843 $dictionary = $GLOBALS['dictionary'];
848 $GLOBALS['log']->debug("createRelationshipMeta: no metadata file found" . $filename);
853 if (!is_array($dictionary) or !array_key_exists($key, $dictionary))
855 $GLOBALS['log']->fatal("createRelationshipMeta: Metadata for table ".$tablename. " does not exist");
856 display_notice("meta data absent for table ".$tablename." keyed to $key ");
860 if (isset($dictionary[$key]['relationships']))
863 $RelationshipDefs = $dictionary[$key]['relationships'];
867 $beanList_ucase=array_change_key_case ( $beanList ,CASE_UPPER);
868 foreach ($RelationshipDefs as $rel_name=>$rel_def)
870 if (isset($rel_def['lhs_module']) and !isset($beanList_ucase[strtoupper($rel_def['lhs_module'])])) {
871 $GLOBALS['log']->debug('skipping orphaned relationship record ' . $rel_name . ' lhs module is missing ' . $rel_def['lhs_module']);
874 if (isset($rel_def['rhs_module']) and !isset($beanList_ucase[strtoupper($rel_def['rhs_module'])])) {
875 $GLOBALS['log']->debug('skipping orphaned relationship record ' . $rel_name . ' rhs module is missing ' . $rel_def['rhs_module']);
880 //check whether relationship exists or not first.
881 if (Relationship::exists($rel_name,$db))
883 $GLOBALS['log']->debug('Skipping, reltionship already exists '.$rel_name);
887 $seed = BeanFactory::getBean("Relationships");
888 $keys = array_keys($seed->field_defs);
890 foreach($keys as $key)
894 $toInsert[$key] = create_guid();
896 else if ($key == "relationship_name")
898 $toInsert[$key] = $rel_name;
900 else if (isset($rel_def[$key]))
902 $toInsert[$key] = $rel_def[$key];
904 //todo specify defaults if meta not defined.
908 $column_list = implode(",", array_keys($toInsert));
909 $value_list = "'" . implode("','", array_values($toInsert)) . "'";
911 //create the record. todo add error check.
912 $insert_string = "INSERT into relationships (" .$column_list. ") values (".$value_list.")";
913 $db->query($insert_string, true);
920 //log informational message stating no relationships meta was set for this bean.
926 * This method has been deprecated.
927 * @see createRelationshipMeta()
928 * @deprecated 4.5.1 - Nov 14, 2006
931 function create_relationship_meta($key,&$db,&$log,$tablename,$dictionary,$module_dir)
933 SugarBean::createRelationshipMeta($key,$db,$tablename,$dictionary,$module_dir);
938 * Handle the following when a SugarBean object is cloned
940 * Currently all this does it unset any relationships that were created prior to cloning the object
944 public function __clone()
946 if(!empty($this->loaded_relationships)) {
947 foreach($this->loaded_relationships as $rel) {
955 * Loads the request relationship. This method should be called before performing any operations on the related data.
957 * This method searches the vardef array for the requested attribute's definition. If the attribute is of the type
958 * link then it creates a similary named variable and loads the relationship definition.
960 * @param string $rel_name relationship/attribute name.
963 function load_relationship($rel_name)
965 $GLOBALS['log']->debug("SugarBean[{$this->object_name}].load_relationships, Loading relationship (".$rel_name.").");
967 if (empty($rel_name))
969 $GLOBALS['log']->error("SugarBean.load_relationships, Null relationship name passed.");
972 $fieldDefs = $this->getFieldDefinitions();
974 //find all definitions of type link.
975 if (!empty($fieldDefs[$rel_name]))
977 //initialize a variable of type Link
978 require_once('data/Link2.php');
979 $class = load_link_class($fieldDefs[$rel_name]);
980 if (isset($this->$rel_name) && $this->$rel_name instanceof $class) {
983 //if rel_name is provided, search the fieldef array keys by name.
984 if (isset($fieldDefs[$rel_name]['type']) && $fieldDefs[$rel_name]['type'] == 'link')
986 if ($class == "Link2")
987 $this->$rel_name = new $class($rel_name, $this);
989 $this->$rel_name = new $class($fieldDefs[$rel_name]['relationship'], $this, $fieldDefs[$rel_name]);
991 if (empty($this->$rel_name) ||
992 (method_exists($this->$rel_name, "loadedSuccesfully") && !$this->$rel_name->loadedSuccesfully()))
994 unset($this->$rel_name);
997 // keep track of the loaded relationships
998 $this->loaded_relationships[] = $rel_name;
1002 $GLOBALS['log']->debug("SugarBean.load_relationships, Error Loading relationship (".$rel_name.")");
1007 * Loads all attributes of type link.
1009 * DO NOT CALL THIS FUNCTION IF YOU CAN AVOID IT. Please use load_relationship directly instead.
1011 * Method searches the implmenting module's vardef file for attributes of type link, and for each attribute
1012 * create a similary named variable and load the relationship definition.
1016 * Internal function, do not override.
1018 function load_relationships()
1020 $GLOBALS['log']->debug("SugarBean.load_relationships, Loading all relationships of type link.");
1021 $linked_fields=$this->get_linked_fields();
1022 foreach($linked_fields as $name=>$properties)
1024 $this->load_relationship($name);
1029 * Returns an array of beans of related data.
1031 * For instance, if an account is related to 10 contacts , this function will return an array of contacts beans (10)
1032 * with each bean representing a contact record.
1033 * Method will load the relationship if not done so already.
1035 * @param string $field_name relationship to be loaded.
1036 * @param string $bean name class name of the related bean.
1037 * @param array $sort_array optional, unused
1038 * @param int $begin_index Optional, default 0, unused.
1039 * @param int $end_index Optional, default -1
1040 * @param int $deleted Optional, Default 0, 0 adds deleted=0 filter, 1 adds deleted=1 filter.
1041 * @param string $optional_where, Optional, default empty.
1043 * Internal function, do not override.
1045 function get_linked_beans($field_name,$bean_name, $sort_array = array(), $begin_index = 0, $end_index = -1,
1046 $deleted=0, $optional_where="")
1048 //if bean_name is Case then use aCase
1049 if($bean_name=="Case")
1050 $bean_name = "aCase";
1052 if($this->load_relationship($field_name)) {
1053 if ($this->$field_name instanceof Link) {
1054 // some classes are still based on Link, e.g. TeamSetLink
1055 return array_values($this->$field_name->getBeans(new $bean_name(), $sort_array, $begin_index, $end_index, $deleted, $optional_where));
1058 if ($end_index != -1 || !empty($deleted) || !empty($optional_where))
1059 return array_values($this->$field_name->getBeans(array(
1060 'where' => $optional_where,
1061 'deleted' => $deleted,
1062 'limit' => ($end_index - $begin_index)
1065 return array_values($this->$field_name->getBeans());
1073 * Returns an array of fields that are of type link.
1075 * @return array List of fields.
1077 * Internal function, do not override.
1079 function get_linked_fields()
1082 $linked_fields=array();
1084 // require_once('data/Link.php');
1086 $fieldDefs = $this->getFieldDefinitions();
1088 //find all definitions of type link.
1089 if (!empty($fieldDefs))
1091 foreach ($fieldDefs as $name=>$properties)
1093 if (array_search('link',$properties) === 'type')
1095 $linked_fields[$name]=$properties;
1100 return $linked_fields;
1104 * Returns an array of fields that are able to be Imported into
1105 * i.e. 'importable' not set to 'false'
1107 * @return array List of fields.
1109 * Internal function, do not override.
1111 function get_importable_fields()
1113 $importableFields = array();
1115 $fieldDefs= $this->getFieldDefinitions();
1117 if (!empty($fieldDefs)) {
1118 foreach ($fieldDefs as $key=>$value_array) {
1119 if ( (isset($value_array['importable'])
1120 && (is_string($value_array['importable']) && $value_array['importable'] == 'false'
1121 || is_bool($value_array['importable']) && $value_array['importable'] == false))
1122 || (isset($value_array['type']) && $value_array['type'] == 'link')
1123 || (isset($value_array['auto_increment'])
1124 && ($value_array['type'] == true || $value_array['type'] == 'true')) ) {
1125 // only allow import if we force it
1126 if (isset($value_array['importable'])
1127 && (is_string($value_array['importable']) && $value_array['importable'] == 'true'
1128 || is_bool($value_array['importable']) && $value_array['importable'] == true)) {
1129 $importableFields[$key]=$value_array;
1134 //Expose the cooresponding id field of a relate field if it is only defined as a link so that users can relate records by id during import
1135 if( isset($value_array['type']) && ($value_array['type'] == 'relate') && isset($value_array['id_name']) )
1137 $idField = $value_array['id_name'];
1138 if( isset($fieldDefs[$idField]) && isset($fieldDefs[$idField]['type'] ) && $fieldDefs[$idField]['type'] == 'link' )
1140 $tmpFieldDefs = $fieldDefs[$idField];
1141 $tmpFieldDefs['vname'] = translate($value_array['vname'], $this->module_dir) . " " . $GLOBALS['app_strings']['LBL_ID'];
1142 $importableFields[$idField]=$tmpFieldDefs;
1146 $importableFields[$key]=$value_array;
1151 return $importableFields;
1155 * Returns an array of fields that are of type relate.
1157 * @return array List of fields.
1159 * Internal function, do not override.
1161 function get_related_fields()
1164 $related_fields=array();
1166 // require_once('data/Link.php');
1168 $fieldDefs = $this->getFieldDefinitions();
1170 //find all definitions of type link.
1171 if (!empty($fieldDefs))
1173 foreach ($fieldDefs as $name=>$properties)
1175 if (array_search('relate',$properties) === 'type')
1177 $related_fields[$name]=$properties;
1182 return $related_fields;
1186 * Returns an array of fields that are required for import
1190 function get_import_required_fields()
1192 $importable_fields = $this->get_importable_fields();
1193 $required_fields = array();
1195 foreach ( $importable_fields as $name => $properties ) {
1196 if ( isset($properties['importable']) && is_string($properties['importable']) && $properties['importable'] == 'required' ) {
1197 $required_fields[$name] = $properties;
1201 return $required_fields;
1205 * Iterates through all the relationships and deletes all records for reach relationship.
1207 * @param string $id Primary key value of the parent reocrd
1209 function delete_linked($id)
1211 $linked_fields=$this->get_linked_fields();
1212 foreach ($linked_fields as $name => $value)
1214 if ($this->load_relationship($name))
1216 $this->$name->delete($id);
1220 $GLOBALS['log']->fatal("error loading relationship $name");
1226 * Creates tables for the module implementing the class.
1227 * If you override this function make sure that your code can handles table creation.
1230 function create_tables()
1234 $key = $this->getObjectName();
1235 if (!array_key_exists($key, $dictionary))
1237 $GLOBALS['log']->fatal("create_tables: Metadata for table ".$this->table_name. " does not exist");
1238 display_notice("meta data absent for table ".$this->table_name." keyed to $key ");
1242 if(!$this->db->tableExists($this->table_name))
1244 $this->db->createTable($this);
1245 if($this->bean_implements('ACL')){
1246 if(!empty($this->acltype)){
1247 ACLAction::addActions($this->getACLCategory(), $this->acltype);
1249 ACLAction::addActions($this->getACLCategory());
1255 echo "Table already exists : $this->table_name<br>";
1257 if($this->is_AuditEnabled()){
1258 if (!$this->db->tableExists($this->get_audit_table_name())) {
1259 $this->create_audit_table();
1267 * Delete the primary table for the module implementing the class.
1268 * If custom fields were added to this table/module, the custom table will be removed too, along with the cache
1269 * entries that define the custom fields.
1272 function drop_tables()
1275 $key = $this->getObjectName();
1276 if (!array_key_exists($key, $dictionary))
1278 $GLOBALS['log']->fatal("drop_tables: Metadata for table ".$this->table_name. " does not exist");
1279 echo "meta data absent for table ".$this->table_name."<br>\n";
1281 if(empty($this->table_name))return;
1282 if ($this->db->tableExists($this->table_name))
1284 $this->db->dropTable($this);
1285 if ($this->db->tableExists($this->table_name. '_cstm'))
1287 $this->db->dropTableName($this->table_name. '_cstm');
1288 DynamicField::deleteCache();
1290 if ($this->db->tableExists($this->get_audit_table_name())) {
1291 $this->db->dropTableName($this->get_audit_table_name());
1300 * Loads the definition of custom fields defined for the module.
1301 * Local file system cache is created as needed.
1303 * @param string $module_name setting up custom fields for this module.
1304 * @param boolean $clean_load Optional, default true, rebuilds the cache if set to true.
1306 function setupCustomFields($module_name, $clean_load=true)
1308 $this->custom_fields = new DynamicField($module_name);
1309 $this->custom_fields->setup($this);
1314 * Cleans char, varchar, text, etc. fields of XSS type materials
1316 function cleanBean() {
1317 foreach($this->field_defs as $key => $def) {
1319 if (isset($def['type'])) {
1322 if(isset($def['dbType']))
1323 $type .= $def['dbType'];
1325 if($def['type'] == 'html' || $def['type'] == 'longhtml') {
1326 $this->$key = SugarCleaner::cleanHtml($this->$key, true);
1327 } elseif((strpos($type, 'char') !== false ||
1328 strpos($type, 'text') !== false ||
1332 $this->$key = SugarCleaner::cleanHtml($this->$key);
1338 * Implements a generic insert and update logic for any SugarBean
1339 * This method only works for subclasses that implement the same variable names.
1340 * This method uses the presence of an id field that is not null to signify and update.
1341 * The id field should not be set otherwise.
1343 * @param boolean $check_notify Optional, default false, if set to true assignee of the record is notified via email.
1344 * @todo Add support for field type validation and encoding of parameters.
1346 function save($check_notify = FALSE)
1348 $this->in_save = true;
1349 // cn: SECURITY - strip XSS potential vectors
1351 // This is used so custom/3rd-party code can be upgraded with fewer issues, this will be removed in a future release
1352 $this->fixUpFormatting();
1354 global $current_user, $action;
1357 if(empty($this->id))
1362 if ( $this->new_with_id == true )
1366 if(empty($this->date_modified) || $this->update_date_modified)
1368 $this->date_modified = $GLOBALS['timedate']->nowDb();
1371 $this->_checkOptimisticLocking($action, $isUpdate);
1373 if(!empty($this->modified_by_name)) $this->old_modified_by_name = $this->modified_by_name;
1374 if($this->update_modified_by)
1376 $this->modified_user_id = 1;
1378 if (!empty($current_user))
1380 $this->modified_user_id = $current_user->id;
1381 $this->modified_by_name = $current_user->user_name;
1384 if ($this->deleted != 1)
1388 if (empty($this->date_entered))
1390 $this->date_entered = $this->date_modified;
1392 if($this->set_created_by == true)
1394 // created by should always be this user
1395 $this->created_by = (isset($current_user)) ? $current_user->id : "";
1397 if( $this->new_with_id == false)
1399 $this->id = create_guid();
1405 require_once("data/BeanFactory.php");
1406 BeanFactory::registerBean($this->module_name, $this);
1408 if (empty($GLOBALS['updating_relationships']) && empty($GLOBALS['saving_relationships']) && empty ($GLOBALS['resavingRelatedBeans']))
1410 $GLOBALS['saving_relationships'] = true;
1411 // let subclasses save related field changes
1412 $this->save_relationship_changes($isUpdate);
1413 $GLOBALS['saving_relationships'] = false;
1415 if($isUpdate && !$this->update_date_entered)
1417 unset($this->date_entered);
1419 // call the custom business logic
1420 $custom_logic_arguments['check_notify'] = $check_notify;
1423 $this->call_custom_logic("before_save", $custom_logic_arguments);
1424 unset($custom_logic_arguments);
1426 // If we're importing back semi-colon separated non-primary emails
1427 if ($this->hasEmails() && !empty($this->email_addresses_non_primary) && is_array($this->email_addresses_non_primary))
1429 // Add each mail to the account
1430 foreach ($this->email_addresses_non_primary as $mail)
1432 $this->emailAddress->addAddress($mail);
1434 $this->emailAddress->save($this->id, $this->module_dir);
1437 if(isset($this->custom_fields))
1439 $this->custom_fields->bean = $this;
1440 $this->custom_fields->save($isUpdate);
1443 // use the db independent query generator
1444 $this->preprocess_fields_on_save();
1446 //construct the SQL to create the audit record if auditing is enabled.
1447 $auditDataChanges=array();
1448 if ($this->is_AuditEnabled()) {
1449 if ($isUpdate && !isset($this->fetched_row)) {
1450 $GLOBALS['log']->debug('Auditing: Retrieve was not called, audit record will not be created.');
1452 $auditDataChanges=$this->db->getAuditDataChanges($this);
1456 $this->_sendNotifications($check_notify);
1459 $this->db->update($this);
1461 $this->db->insert($this);
1464 if (!empty($auditDataChanges) && is_array($auditDataChanges))
1466 foreach ($auditDataChanges as $change)
1468 $this->db->save_audit_records($this,$change);
1473 if (empty($GLOBALS['resavingRelatedBeans'])){
1474 SugarRelationship::resaveRelatedBeans();
1477 // populate fetched row with current bean values
1478 foreach ($auditDataChanges as $change) {
1479 $this->fetched_row[$change['field_name']] = $change['after'];
1483 //If we aren't in setup mode and we have a current user and module, then we track
1484 if(isset($GLOBALS['current_user']) && isset($this->module_dir))
1486 $this->track_view($current_user->id, $this->module_dir, 'save');
1489 $this->call_custom_logic('after_save', '');
1491 //Now that the record has been saved, we don't want to insert again on further saves
1492 $this->new_with_id = false;
1493 $this->in_save = false;
1499 * Performs a check if the record has been modified since the specified date
1501 * @param date $date Datetime for verification
1502 * @param string $modified_user_id User modified by
1504 function has_been_modified_since($date, $modified_user_id)
1506 global $current_user;
1507 $date = $this->db->convert($this->db->quoted($date), 'datetime');
1508 if (isset($current_user))
1510 $query = "SELECT date_modified FROM $this->table_name WHERE id='$this->id' AND modified_user_id != '$current_user->id'
1511 AND (modified_user_id != '$modified_user_id' OR date_modified > $date)";
1512 $result = $this->db->query($query);
1514 if($this->db->fetchByAssoc($result))
1523 * Determines which users receive a notification
1525 function get_notification_recipients() {
1526 $notify_user = new User();
1527 $notify_user->retrieve($this->assigned_user_id);
1528 $this->new_assigned_user_name = $notify_user->full_name;
1530 $GLOBALS['log']->info("Notifications: recipient is $this->new_assigned_user_name");
1532 $user_list = array($notify_user);
1535 //send notifications to followers, but ensure to not query for the assigned_user.
1536 return SugarFollowing::getFollowers($this, $notify_user);
1541 * Handles sending out email notifications when items are first assigned to users
1543 * @param string $notify_user user to notify
1544 * @param string $admin the admin user that sends out the notification
1546 function send_assignment_notifications($notify_user, $admin)
1548 global $current_user;
1550 if(($this->object_name == 'Meeting' || $this->object_name == 'Call') || $notify_user->receive_notifications)
1552 $sendToEmail = $notify_user->emailAddress->getPrimaryAddress($notify_user);
1554 if(empty($sendToEmail)) {
1555 $GLOBALS['log']->warn("Notifications: no e-mail address set for user {$notify_user->user_name}, cancelling send");
1559 $notify_mail = $this->create_notification_email($notify_user);
1560 $notify_mail->setMailerForSystem();
1562 if(empty($admin->settings['notify_send_from_assigning_user'])) {
1563 $notify_mail->From = $admin->settings['notify_fromaddress'];
1564 $notify_mail->FromName = (empty($admin->settings['notify_fromname'])) ? "" : $admin->settings['notify_fromname'];
1566 // Send notifications from the current user's e-mail (if set)
1567 $fromAddress = $current_user->emailAddress->getReplyToAddress($current_user);
1568 $fromAddress = !empty($fromAddress) ? $fromAddress : $admin->settings['notify_fromaddress'];
1569 $notify_mail->From = $fromAddress;
1570 //Use the users full name is available otherwise default to system name
1571 $from_name = !empty($admin->settings['notify_fromname']) ? $admin->settings['notify_fromname'] : "";
1572 $from_name = !empty($current_user->full_name) ? $current_user->full_name : $from_name;
1573 $notify_mail->FromName = $from_name;
1576 $oe = new OutboundEmail();
1577 $oe = $oe->getUserMailerSettings($current_user);
1578 //only send if smtp server is defined
1580 $smtpVerified = false;
1582 //first check the user settings
1583 if(!empty($oe->mail_smtpserver)){
1584 $smtpVerified = true;
1587 //if still not verified, check against the system settings
1588 if (!$smtpVerified){
1589 $oe = $oe->getSystemMailerSettings();
1590 if(!empty($oe->mail_smtpserver)){
1591 $smtpVerified = true;
1594 //if smtp was not verified against user or system, then do not send out email
1595 if (!$smtpVerified){
1596 $GLOBALS['log']->fatal("Notifications: error sending e-mail, smtp server was not found ");
1601 if(!$notify_mail->Send()) {
1602 $GLOBALS['log']->fatal("Notifications: error sending e-mail (method: {$notify_mail->Mailer}), (error: {$notify_mail->ErrorInfo})");
1604 $GLOBALS['log']->info("Notifications: e-mail successfully sent");
1612 * This function handles create the email notifications email.
1613 * @param string $notify_user the user to send the notification email to
1615 function create_notification_email($notify_user) {
1616 global $sugar_version;
1617 global $sugar_config;
1618 global $app_list_strings;
1619 global $current_user;
1622 $OBCharset = $locale->getPrecedentPreference('default_email_charset');
1625 require_once("include/SugarPHPMailer.php");
1627 $notify_address = $notify_user->emailAddress->getPrimaryAddress($notify_user);
1628 $notify_name = $notify_user->full_name;
1629 $GLOBALS['log']->debug("Notifications: user has e-mail defined");
1631 $notify_mail = new SugarPHPMailer();
1632 $notify_mail->AddAddress($notify_address,$locale->translateCharsetMIME(trim($notify_name), 'UTF-8', $OBCharset));
1634 if(empty($_SESSION['authenticated_user_language'])) {
1635 $current_language = $sugar_config['default_language'];
1637 $current_language = $_SESSION['authenticated_user_language'];
1639 $xtpl = new XTemplate(get_notify_template_file($current_language));
1640 if($this->module_dir == "Cases") {
1641 $template_name = "Case"; //we should use Case, you can refer to the en_us.notify_template.html.
1644 $template_name = $beanList[$this->module_dir]; //bug 20637, in workflow this->object_name = strange chars.
1647 $this->current_notify_user = $notify_user;
1649 if(in_array('set_notification_body', get_class_methods($this))) {
1650 $xtpl = $this->set_notification_body($xtpl, $this);
1652 $xtpl->assign("OBJECT", translate('LBL_MODULE_NAME'));
1653 $template_name = "Default";
1655 if(!empty($_SESSION["special_notification"]) && $_SESSION["special_notification"]) {
1656 $template_name = $beanList[$this->module_dir].'Special';
1658 if($this->special_notification) {
1659 $template_name = $beanList[$this->module_dir].'Special';
1661 $xtpl->assign("ASSIGNED_USER", $this->new_assigned_user_name);
1662 $xtpl->assign("ASSIGNER", $current_user->name);
1665 if(isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443) {
1666 $port = $_SERVER['SERVER_PORT'];
1669 if (!isset($_SERVER['HTTP_HOST'])) {
1670 $_SERVER['HTTP_HOST'] = '';
1673 $httpHost = $_SERVER['HTTP_HOST'];
1675 if($colon = strpos($httpHost, ':')) {
1676 $httpHost = substr($httpHost, 0, $colon);
1679 $parsedSiteUrl = parse_url($sugar_config['site_url']);
1680 $host = $parsedSiteUrl['host'];
1681 if(!isset($parsedSiteUrl['port'])) {
1682 $parsedSiteUrl['port'] = 80;
1685 $port = ($parsedSiteUrl['port'] != 80) ? ":".$parsedSiteUrl['port'] : '';
1686 $path = !empty($parsedSiteUrl['path']) ? $parsedSiteUrl['path'] : "";
1687 $cleanUrl = "{$parsedSiteUrl['scheme']}://{$host}{$port}{$path}";
1689 $xtpl->assign("URL", $cleanUrl."/index.php?module={$this->module_dir}&action=DetailView&record={$this->id}");
1690 $xtpl->assign("SUGAR", "Sugar v{$sugar_version}");
1691 $xtpl->parse($template_name);
1692 $xtpl->parse($template_name . "_Subject");
1694 $notify_mail->Body = from_html(trim($xtpl->text($template_name)));
1695 $notify_mail->Subject = from_html($xtpl->text($template_name . "_Subject"));
1697 // cn: bug 8568 encode notify email in User's outbound email encoding
1698 $notify_mail->prepForOutbound();
1700 return $notify_mail;
1704 * This function is a good location to save changes that have been made to a relationship.
1705 * This should be overridden in subclasses that have something to save.
1707 * @param boolean $is_update true if this save is an update.
1708 * @param array $exclude a way to exclude relationships
1710 public function save_relationship_changes($is_update, $exclude = array())
1712 list($new_rel_id, $new_rel_link) = $this->set_relationship_info($exclude);
1714 $new_rel_id = $this->handle_preset_relationships($new_rel_id, $new_rel_link, $exclude);
1716 $this->handle_remaining_relate_fields($exclude);
1718 $this->update_parent_relationships($exclude);
1720 $this->handle_request_relate($new_rel_id, $new_rel_link);
1724 * Look in the bean for the new relationship_id and relationship_name if $this->not_use_rel_in_req is set to true,
1725 * otherwise check the $_REQUEST param for a relate_id and relate_to field. Once we have that make sure that it's
1726 * not excluded from the passed in array of relationships to exclude
1728 * @param array $exclude any relationship's to exclude
1729 * @return array The relationship_id and relationship_name in an array
1731 protected function set_relationship_info($exclude = array())
1734 $new_rel_id = false;
1735 $new_rel_link = false;
1736 // check incoming data
1737 if (isset($this->not_use_rel_in_req) && $this->not_use_rel_in_req == true) {
1738 // if we should use relation data from properties (for REQUEST-independent calls)
1739 $rel_id = isset($this->new_rel_id) ? $this->new_rel_id : '';
1740 $rel_link = isset($this->new_rel_relname) ? $this->new_rel_relname : '';
1744 // if we should use relation data from REQUEST
1745 $rel_id = isset($_REQUEST['relate_id']) ? $_REQUEST['relate_id'] : '';
1746 $rel_link = isset($_REQUEST['relate_to']) ? $_REQUEST['relate_to'] : '';
1749 // filter relation data
1750 if ($rel_id && $rel_link && !in_array($rel_link, $exclude) && $rel_id != $this->id) {
1751 $new_rel_id = $rel_id;
1752 $new_rel_link = $rel_link;
1753 // Bug #53223 : wrong relationship from subpanel create button
1754 // if LHSModule and RHSModule are same module use left link to add new item b/s of:
1755 // $rel_id and $rel_link are not emty - request is from subpanel
1756 // $rel_link contains relationship name - checked by call load_relationship
1757 $isRelationshipLoaded = $this->load_relationship($rel_link);
1758 if ($isRelationshipLoaded && !empty($this->$rel_link) && $this->$rel_link->getRelationshipObject() && $this->$rel_link->getRelationshipObject()->getLHSModule() == $this->$rel_link->getRelationshipObject()->getRHSModule() )
1760 $new_rel_link = $this->$rel_link->getRelationshipObject()->getLHSLink();
1764 //Try to find the link in this bean based on the relationship
1765 foreach ($this->field_defs as $key => $def)
1767 if (isset($def['type']) && $def['type'] == 'link' && isset($def['relationship']) && $def['relationship'] == $rel_link)
1769 $new_rel_link = $key;
1775 return array($new_rel_id, $new_rel_link);
1779 * Handle the preset fields listed in the fixed relationship_fields array hardcoded into the OOB beans
1781 * TODO: remove this mechanism and replace with mechanism exclusively based on the vardefs
1784 * @see save_relationship_changes
1785 * @param string|boolean $new_rel_id String of the ID to add
1786 * @param string Relationship Name
1787 * @param array $exclude any relationship's to exclude
1788 * @return string|boolean Return the new_rel_id if it was not used. False if it was used.
1790 protected function handle_preset_relationships($new_rel_id, $new_rel_link, $exclude = array())
1792 if (isset($this->relationship_fields) && is_array($this->relationship_fields)) {
1793 foreach ($this->relationship_fields as $id => $rel_name)
1796 if (in_array($id, $exclude)) continue;
1798 if(!empty($this->$id))
1800 // Bug #44930 We do not need to update main related field if it is changed from sub-panel.
1801 if ($rel_name == $new_rel_link && $this->$id != $new_rel_id)
1805 $GLOBALS['log']->debug('save_relationship_changes(): From relationship_field array - adding a relationship record: '.$rel_name . ' = ' . $this->$id);
1806 //already related the new relationship id so let's set it to false so we don't add it again using the _REQUEST['relate_i'] mechanism in a later block
1807 $this->load_relationship($rel_name);
1808 $rel_add = $this->$rel_name->add($this->$id);
1809 // move this around to only take out the id if it was save successfully
1810 if ($this->$id == $new_rel_id && $rel_add == true) {
1811 $new_rel_id = false;
1814 //if before value is not empty then attempt to delete relationship
1815 if (!empty($this->rel_fields_before_value[$id])) {
1816 $GLOBALS['log']->debug('save_relationship_changes(): From relationship_field array - attempting to remove the relationship record, using relationship attribute' . $rel_name);
1817 $this->load_relationship($rel_name);
1818 $this->$rel_name->delete($this->id, $this->rel_fields_before_value[$id]);
1828 * Next, we'll attempt to update all of the remaining relate fields in the vardefs that have 'save' set in their field_def
1829 * Only the 'save' fields should be saved as some vardef entries today are not for display only purposes and break the application if saved
1830 * If the vardef has entries for field <a> of type relate, where a->id_name = <b> and field <b> of type link
1831 * then we receive a value for b from the MVC in the _REQUEST, and it should be set in the bean as $this->$b
1834 * @see save_relationship_changes
1835 * @param array $exclude any relationship's to exclude
1836 * @return array the list of relationships that were added or removed successfully or if they were a failure
1838 protected function handle_remaining_relate_fields($exclude = array())
1841 $modified_relationships = array(
1842 'add' => array('success' => array(), 'failure' => array()),
1843 'remove' => array('success' => array(), 'failure' => array()),
1846 foreach ($this->field_defs as $def)
1848 if ($def ['type'] == 'relate' && isset ($def ['id_name']) && isset ($def ['link']) && isset ($def['save'])) {
1849 if (in_array($def['id_name'], $exclude) || in_array($def['id_name'], $this->relationship_fields))
1850 continue; // continue to honor the exclude array and exclude any relationships that will be handled by the relationship_fields mechanism
1852 $linkField = $def ['link'];
1853 if (isset($this->field_defs[$linkField])) {
1854 if ($this->load_relationship($linkField)) {
1855 $idName = $def['id_name'];
1857 if (!empty($this->rel_fields_before_value[$idName]) && empty($this->$idName)) {
1858 //if before value is not empty then attempt to delete relationship
1859 $GLOBALS['log']->debug("save_relationship_changes(): From field_defs - attempting to remove the relationship record: {$def [ 'link' ]} = {$this->rel_fields_before_value[$def [ 'id_name' ]]}");
1860 $success = $this->$def ['link']->delete($this->id, $this->rel_fields_before_value[$def ['id_name']]);
1861 // just need to make sure it's true and not an array as it's possible to return an array
1862 if($success == true) {
1863 $modified_relationships['remove']['success'][] = $def['link'];
1865 $modified_relationships['remove']['failure'][] = $def['link'];
1867 $GLOBALS['log']->debug("save_relationship_changes(): From field_defs - attempting to remove the relationship record returned " . var_export($success, true));
1870 if (!empty($this->$idName) && is_string($this->$idName)) {
1871 $GLOBALS['log']->debug("save_relationship_changes(): From field_defs - attempting to add a relationship record - {$def [ 'link' ]} = {$this->$def [ 'id_name' ]}");
1873 $success = $this->$linkField->add($this->$idName);
1875 // just need to make sure it's true and not an array as it's possible to return an array
1876 if($success == true) {
1877 $modified_relationships['add']['success'][] = $linkField;
1879 $modified_relationships['add']['failure'][] = $linkField;
1882 $GLOBALS['log']->debug("save_relationship_changes(): From field_defs - add a relationship record returned " . var_export($success, true));
1885 $GLOBALS['log']->fatal("Failed to load relationship {$linkField} while saving {$this->module_dir}");
1891 return $modified_relationships;
1896 * Updates relationships based on changes to fields of type 'parent' which
1897 * may or may not have links associated with them
1899 * @param array $exclude
1901 protected function update_parent_relationships($exclude = array())
1903 foreach ($this->field_defs as $def)
1905 if (!empty($def['type']) && $def['type'] == "parent")
1907 if (empty($def['type_name']) || empty($def['id_name']))
1909 $typeField = $def['type_name'];
1910 $idField = $def['id_name'];
1911 if (in_array($idField, $exclude))
1913 //Determine if the parent field has changed.
1915 //First check if the fetched row parent existed and now we no longer have one
1916 (!empty($this->fetched_row[$typeField]) && !empty($this->fetched_row[$idField])
1917 && (empty($this->$typeField) || empty($this->$idField))
1919 //Next check if we have one now that doesn't match the fetch row
1920 (!empty($this->$typeField) && !empty($this->$idField) &&
1921 (empty($this->fetched_row[$typeField]) || empty($this->fetched_row[$idField])
1922 || $this->fetched_row[$idField] != $this->$idField)
1924 // Check if we are deleting the bean, should remove the bean from any relationships
1927 $parentLinks = array();
1928 //Correlate links to parent field module types
1929 foreach ($this->field_defs as $ldef)
1931 if (!empty($ldef['type']) && $ldef['type'] == "link" && !empty($ldef['relationship']))
1933 $relDef = SugarRelationshipFactory::getInstance()->getRelationshipDef($ldef['relationship']);
1934 if (!empty($relDef['relationship_role_column']) && $relDef['relationship_role_column'] == $typeField)
1936 $parentLinks[$relDef['lhs_module']] = $ldef;
1941 //If we used to have a parent, call remove on that relationship
1942 if (!empty($this->fetched_row[$typeField]) && !empty($this->fetched_row[$idField])
1943 && !empty($parentLinks[$this->fetched_row[$typeField]])
1944 && ($this->fetched_row[$idField] != $this->$idField))
1946 $oldParentLink = $parentLinks[$this->fetched_row[$typeField]]['name'];
1947 //Load the relationship
1948 if ($this->load_relationship($oldParentLink))
1950 $this->$oldParentLink->delete($this->fetched_row[$idField]);
1951 // Should resave the old parent
1952 SugarRelationship::addToResaveList(BeanFactory::getBean($this->fetched_row[$typeField], $this->fetched_row[$idField]));
1956 // If both parent type and parent id are set, save it unless the bean is being deleted
1957 if (!empty($this->$typeField) && !empty($this->$idField) && !empty($parentLinks[$this->$typeField]['name']) && $this->deleted != 1)
1959 //Now add the new parent
1960 $parentLink = $parentLinks[$this->$typeField]['name'];
1961 if ($this->load_relationship($parentLink))
1963 $this->$parentLink->add($this->$idField);
1972 * Finally, we update a field listed in the _REQUEST['%/relate_id']/_REQUEST['relate_to'] mechanism (if it has not already been updated)
1975 * @see save_relationship_changes
1976 * @param string|boolean $new_rel_id
1977 * @param string $new_rel_link
1980 protected function handle_request_relate($new_rel_id, $new_rel_link)
1982 if (!empty($new_rel_id)) {
1984 if ($this->load_relationship($new_rel_link)) {
1985 return $this->$new_rel_link->add($new_rel_id);
1987 $lower_link = strtolower($new_rel_link);
1988 if ($this->load_relationship($lower_link)) {
1989 return $this->$lower_link->add($new_rel_id);
1992 require_once('data/Link2.php');
1993 $rel = Relationship::retrieve_by_modules($new_rel_link, $this->module_dir, $this->db, 'many-to-many');
1996 foreach ($this->field_defs as $field => $def) {
1997 if ($def['type'] == 'link' && !empty($def['relationship']) && $def['relationship'] == $rel) {
1998 $this->load_relationship($field);
1999 return $this->$field->add($new_rel_id);
2003 //ok so we didn't find it in the field defs let's save it anyway if we have the relationshp
2005 $this->$rel = new Link2($rel, $this, array());
2006 return $this->$rel->add($new_rel_id);
2012 // nothing was saved so just return false;
2017 * This function retrieves a record of the appropriate type from the DB.
2018 * It fills in all of the fields from the DB into the object it was called on.
2020 * @param $id - If ID is specified, it overrides the current value of $this->id. If not specified the current value of $this->id will be used.
2021 * @return this - The object that it was called apon or null if exactly 1 record was not found.
2025 function check_date_relationships_load()
2027 global $disable_date_format;
2029 if (empty($timedate))
2030 $timedate=TimeDate::getInstance();
2032 if(empty($this->field_defs))
2036 foreach($this->field_defs as $fieldDef)
2038 $field = $fieldDef['name'];
2039 if(!isset($this->processed_dates_times[$field]))
2041 $this->processed_dates_times[$field] = '1';
2042 if(empty($this->$field)) continue;
2043 if($field == 'date_modified' || $field == 'date_entered')
2045 $this->$field = $this->db->fromConvert($this->$field, 'datetime');
2046 if(empty($disable_date_format)) {
2047 $this->$field = $timedate->to_display_date_time($this->$field);
2050 elseif(isset($this->field_name_map[$field]['type']))
2052 $type = $this->field_name_map[$field]['type'];
2054 if($type == 'relate' && isset($this->field_name_map[$field]['custom_module']))
2056 $type = $this->field_name_map[$field]['type'];
2061 if($this->$field == '0000-00-00')
2064 } elseif(!empty($this->field_name_map[$field]['rel_field']))
2066 $rel_field = $this->field_name_map[$field]['rel_field'];
2068 if(!empty($this->$rel_field))
2070 if(empty($disable_date_format)) {
2071 $mergetime = $timedate->merge_date_time($this->$field,$this->$rel_field);
2072 $this->$field = $timedate->to_display_date($mergetime);
2073 $this->$rel_field = $timedate->to_display_time($mergetime);
2079 if(empty($disable_date_format)) {
2080 $this->$field = $timedate->to_display_date($this->$field, false);
2083 } elseif($type == 'datetime' || $type == 'datetimecombo')
2085 if($this->$field == '0000-00-00 00:00:00')
2091 if(empty($disable_date_format)) {
2092 $this->$field = $timedate->to_display_date_time($this->$field, true, true);
2095 } elseif($type == 'time')
2097 if($this->$field == '00:00:00')
2102 //$this->$field = from_db_convert($this->$field, 'time');
2103 if(empty($this->field_name_map[$field]['rel_field']) && empty($disable_date_format))
2105 $this->$field = $timedate->to_display_time($this->$field,true, false);
2108 } elseif($type == 'encrypt' && empty($disable_date_format)){
2109 $this->$field = $this->decrypt_after_retrieve($this->$field);
2117 * This function processes the fields before save.
2118 * Interal function, do not override.
2120 function preprocess_fields_on_save()
2122 $GLOBALS['log']->deprecated('SugarBean.php: preprocess_fields_on_save() is deprecated');
2126 * Removes formatting from values posted from the user interface.
2127 * It only unformats numbers. Function relies on user/system prefernce for format strings.
2129 * Internal Function, do not override.
2131 function unformat_all_fields()
2133 $GLOBALS['log']->deprecated('SugarBean.php: unformat_all_fields() is deprecated');
2137 * This functions adds formatting to all number fields before presenting them to user interface.
2139 * Internal function, do not override.
2141 function format_all_fields()
2143 $GLOBALS['log']->deprecated('SugarBean.php: format_all_fields() is deprecated');
2146 function format_field($fieldDef)
2148 $GLOBALS['log']->deprecated('SugarBean.php: format_field() is deprecated');
2152 * Function corrects any bad formatting done by 3rd party/custom code
2154 * This function will be removed in a future release, it is only here to assist upgrading existing code that expects formatted data in the bean
2156 function fixUpFormatting()
2159 static $boolean_false_values = array('off', 'false', '0', 'no');
2162 foreach($this->field_defs as $field=>$def)
2164 if ( !isset($this->$field) ) {
2167 if ( (isset($def['source'])&&$def['source']=='non-db') || $field == 'deleted' ) {
2170 if ( isset($this->fetched_row[$field]) && $this->$field == $this->fetched_row[$field] ) {
2171 // Don't hand out warnings because the field was untouched between retrieval and saving, most database drivers hand pretty much everything back as strings.
2174 $reformatted = false;
2175 switch($def['type']) {
2177 case 'datetimecombo':
2178 if(empty($this->$field)) break;
2179 if ($this->$field == 'NULL') {
2183 if ( ! preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$/',$this->$field) ) {
2184 // This appears to be formatted in user date/time
2185 $this->$field = $timedate->to_db($this->$field);
2186 $reformatted = true;
2190 if(empty($this->$field)) break;
2191 if ($this->$field == 'NULL') {
2195 if ( ! preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/',$this->$field) ) {
2196 // This date appears to be formatted in the user's format
2197 $this->$field = $timedate->to_db_date($this->$field, false);
2198 $reformatted = true;
2202 if(empty($this->$field)) break;
2203 if ($this->$field == 'NULL') {
2207 if ( preg_match('/(am|pm)/i',$this->$field) ) {
2208 // This time appears to be formatted in the user's format
2209 $this->$field = $timedate->fromUserTime($this->$field)->format(TimeDate::DB_TIME_FORMAT);
2210 $reformatted = true;
2217 if ( $this->$field === '' || $this->$field == NULL || $this->$field == 'NULL') {
2220 if ( is_string($this->$field) ) {
2221 $this->$field = (float)unformat_number($this->$field);
2222 $reformatted = true;
2231 if ( $this->$field === '' || $this->$field == NULL || $this->$field == 'NULL') {
2234 if ( is_string($this->$field) ) {
2235 $this->$field = (int)unformat_number($this->$field);
2236 $reformatted = true;
2240 if (empty($this->$field)) {
2241 $this->$field = false;
2242 } else if(true === $this->$field || 1 == $this->$field) {
2243 $this->$field = true;
2244 } else if(in_array(strval($this->$field), $boolean_false_values)) {
2245 $this->$field = false;
2246 $reformatted = true;
2248 $this->$field = true;
2249 $reformatted = true;
2253 $this->$field = $this->encrpyt_before_save($this->$field);
2256 if ( $reformatted ) {
2257 $GLOBALS['log']->deprecated('Formatting correction: '.$this->module_dir.'->'.$field.' had formatting automatically corrected. This will be removed in the future, please upgrade your external code');
2264 * Function fetches a single row of data given the primary key value.
2266 * The fetched data is then set into the bean. The function also processes the fetched data by formattig
2267 * date/time and numeric values.
2269 * @param string $id Optional, default -1, is set to -1 id value from the bean is used, else, passed value is used
2270 * @param boolean $encode Optional, default true, encodes the values fetched from the database.
2271 * @param boolean $deleted Optional, default true, if set to false deleted filter will not be added.
2273 * Internal function, do not override.
2275 function retrieve($id = -1, $encode=true,$deleted=true)
2278 $custom_logic_arguments['id'] = $id;
2279 $this->call_custom_logic('before_retrieve', $custom_logic_arguments);
2285 $custom_join = $this->getCustomJoin();
2287 $query = "SELECT $this->table_name.*". $custom_join['select']. " FROM $this->table_name ";
2289 $query .= $custom_join['join'];
2290 $query .= " WHERE $this->table_name.id = ".$this->db->quoted($id);
2291 if ($deleted) $query .= " AND $this->table_name.deleted=0";
2292 $GLOBALS['log']->debug("Retrieve $this->object_name : ".$query);
2293 $result = $this->db->limitQuery($query,0,1,true, "Retrieving record by id $this->table_name:$id found ");
2299 $row = $this->db->fetchByAssoc($result, $encode);
2305 //make copy of the fetched row for construction of audit record and for business logic/workflow
2306 $row = $this->convertRow($row);
2307 $this->fetched_row=$row;
2308 $this->populateFromRow($row);
2310 // fix defect #52438. implement the same logic as sugar_currency_format
2311 // Smarty modifier does.
2312 $this->populateCurrencyFields();
2314 global $module, $action;
2315 //Just to get optimistic locking working for this release
2316 if($this->optimistic_lock && $module == $this->module_dir && $action =='EditView' )
2318 $_SESSION['o_lock_id']= $id;
2319 $_SESSION['o_lock_dm']= $this->date_modified;
2320 $_SESSION['o_lock_on'] = $this->object_name;
2322 $this->processed_dates_times = array();
2323 $this->check_date_relationships_load();
2325 if(isset($this->custom_fields))
2327 $this->custom_fields->fill_relationships();
2330 $this->is_updated_dependent_fields = false;
2331 $this->fill_in_additional_detail_fields();
2332 $this->fill_in_relationship_fields();
2333 // save related fields values for audit
2334 foreach ($this->get_related_fields() as $rel_field_name)
2336 if (! empty($this->$rel_field_name['name']))
2338 $this->fetched_rel_row[$rel_field_name['name']] = $this->$rel_field_name['name'];
2341 //make a copy of fields in the relationship_fields array. These field values will be used to
2342 //clear relationship.
2343 foreach ( $this->field_defs as $key => $def )
2345 if ($def [ 'type' ] == 'relate' && isset ( $def [ 'id_name'] ) && isset ( $def [ 'link'] ) && isset ( $def[ 'save' ])) {
2346 if (isset($this->$key)) {
2347 $this->rel_fields_before_value[$key]=$this->$key;
2348 if (isset($this->$def [ 'id_name']))
2349 $this->rel_fields_before_value[$def [ 'id_name']]=$this->$def [ 'id_name'];
2352 $this->rel_fields_before_value[$key]=null;
2355 if (isset($this->relationship_fields) && is_array($this->relationship_fields))
2357 foreach ($this->relationship_fields as $rel_id=>$rel_name)
2359 if (isset($this->$rel_id))
2360 $this->rel_fields_before_value[$rel_id]=$this->$rel_id;
2362 $this->rel_fields_before_value[$rel_id]=null;
2366 // call the custom business logic
2367 $custom_logic_arguments['id'] = $id;
2368 $custom_logic_arguments['encode'] = $encode;
2369 $this->call_custom_logic("after_retrieve", $custom_logic_arguments);
2370 unset($custom_logic_arguments);
2375 * Sets value from fetched row into the bean.
2377 * @param array $row Fetched row
2378 * @todo loop through vardefs instead
2379 * @internal runs into an issue when populating from field_defs for users - corrupts user prefs
2381 * Internal function, do not override.
2383 function populateFromRow($row)
2386 foreach($this->field_defs as $field=>$field_value)
2388 if($field == 'user_preferences' && $this->module_dir == 'Users')
2390 if(isset($row[$field]))
2392 $this->$field = $row[$field];
2393 $owner = $field . '_owner';
2394 if(!empty($row[$owner])){
2395 $this->$owner = $row[$owner];
2400 $this->$field = $nullvalue;
2408 * Add any required joins to the list count query. The joins are required if there
2409 * is a field in the $where clause that needs to be joined.
2411 * @param string $query
2412 * @param string $where
2414 * Internal Function, do Not override.
2416 function add_list_count_joins(&$query, $where)
2418 $custom_join = $this->getCustomJoin();
2419 $query .= $custom_join['join'];
2424 * Changes the select expression of the given query to be 'count(*)' so you
2425 * can get the number of items the query will return. This is used to
2426 * populate the upper limit on ListViews.
2428 * @param string $query Select query string
2429 * @return string count query
2431 * Internal function, do not override.
2433 function create_list_count_query($query)
2435 // remove the 'order by' clause which is expected to be at the end of the query
2436 $pattern = '/\sORDER BY.*/is'; // ignores the case
2438 $query = preg_replace($pattern, $replacement, $query);
2439 //handle distinct clause
2441 if(substr_count(strtolower($query), 'distinct')){
2442 if (!empty($this->seed) && !empty($this->seed->table_name ))
2443 $star = 'DISTINCT ' . $this->seed->table_name . '.id';
2445 $star = 'DISTINCT ' . $this->table_name . '.id';
2449 // change the select expression to 'count(*)'
2450 $pattern = '/SELECT(.*?)(\s){1}FROM(\s){1}/is'; // ignores the case
2451 $replacement = 'SELECT count(' . $star . ') c FROM ';
2453 //if the passed query has union clause then replace all instances of the pattern.
2454 //this is very rare. I have seen this happening only from projects module.
2455 //in addition to this added a condition that has union clause and uses
2457 if (strstr($query," UNION ALL ") !== false) {
2459 //separate out all the queries.
2460 $union_qs=explode(" UNION ALL ", $query);
2461 foreach ($union_qs as $key=>$union_query) {
2463 preg_match($pattern, $union_query, $matches);
2464 if (!empty($matches)) {
2465 if (stristr($matches[0], "distinct")) {
2466 if (!empty($this->seed) && !empty($this->seed->table_name ))
2467 $star = 'DISTINCT ' . $this->seed->table_name . '.id';
2469 $star = 'DISTINCT ' . $this->table_name . '.id';
2472 $replacement = 'SELECT count(' . $star . ') c FROM ';
2473 $union_qs[$key] = preg_replace($pattern, $replacement, $union_query,1);
2475 $modified_select_query=implode(" UNION ALL ",$union_qs);
2477 $modified_select_query = preg_replace($pattern, $replacement, $query,1);
2481 return $modified_select_query;
2485 * This function returns a paged list of the current object type. It is intended to allow for
2486 * hopping back and forth through pages of data. It only retrieves what is on the current page.
2488 * @internal This method must be called on a new instance. It trashes the values of all the fields in the current one.
2489 * @param string $order_by
2490 * @param string $where Additional where clause
2491 * @param int $row_offset Optaional,default 0, starting row number
2492 * @param init $limit Optional, default -1
2493 * @param int $max Optional, default -1
2494 * @param boolean $show_deleted Optional, default 0, if set to 1 system will show deleted records.
2495 * @return array Fetched data.
2497 * Internal function, do not override.
2500 function get_list($order_by = "", $where = "", $row_offset = 0, $limit=-1, $max=-1, $show_deleted = 0, $singleSelect=false, $select_fields = array())
2502 $GLOBALS['log']->debug("get_list: order_by = '$order_by' and where = '$where' and limit = '$limit'");
2503 if(isset($_SESSION['show_deleted']))
2507 $order_by=$this->process_order_by($order_by);
2509 if($this->bean_implements('ACL') && ACLController::requireOwner($this->module_dir, 'list') )
2511 global $current_user;
2512 $owner_where = $this->getOwnerWhere($current_user->id);
2514 //rrs - because $this->getOwnerWhere() can return '' we need to be sure to check for it and
2515 //handle it properly else you could get into a situation where you are create a where stmt like
2517 if(!empty($owner_where)){
2519 $where = $owner_where;
2521 $where .= ' AND '. $owner_where;
2525 $query = $this->create_new_list_query($order_by, $where,$select_fields,array(), $show_deleted,'',false,null,$singleSelect);
2526 return $this->process_list_query($query, $row_offset, $limit, $max, $where);
2530 * Prefixes column names with this bean's table name.
2532 * @param string $order_by Order by clause to be processed
2533 * @param SugarBean $submodule name of the module this order by clause is for
2534 * @return string Processed order by clause
2536 * Internal function, do not override.
2538 function process_order_by ($order_by, $submodule = null)
2540 if (empty($order_by))
2542 //submodule is empty,this is for list object in focus
2543 if (empty($submodule))
2545 $bean_queried = $this;
2549 //submodule is set, so this is for subpanel, use submodule
2550 $bean_queried = $submodule;
2552 $elements = explode(',',$order_by);
2553 foreach ($elements as $key=>$value)
2555 if (strchr($value,'.') === false)
2557 //value might have ascending and descending decorations
2558 $list_column = explode(' ',trim($value));
2559 if (isset($list_column[0]))
2561 $list_column_name=trim($list_column[0]);
2562 if (isset($bean_queried->field_defs[$list_column_name]))
2564 $source=isset($bean_queried->field_defs[$list_column_name]['source']) ? $bean_queried->field_defs[$list_column_name]['source']:'db';
2565 if (empty($bean_queried->field_defs[$list_column_name]['table']) && $source=='db')
2567 $list_column[0] = $bean_queried->table_name .".".$list_column[0] ;
2569 if (empty($bean_queried->field_defs[$list_column_name]['table']) && $source=='custom_fields')
2571 $list_column[0] = $bean_queried->table_name ."_cstm.".$list_column[0] ;
2573 // Bug 38803 - Use CONVERT() function when doing an order by on ntext, text, and image fields
2574 if ($source != 'non-db' && $this->db->isTextType($this->db->getFieldType($bean_queried->field_defs[$list_column_name]))) {
2575 $list_column[0] = $this->db->convert($list_column[0], "text2char");
2577 $value = implode(' ',$list_column);
2579 $GLOBALS['log']->debug("process_order_by: ($list_column[0]) does not have a vardef entry.");
2583 $elements[$key]=$value;
2585 return implode(',', $elements);
2591 * Returns a detail object like retrieving of the current object type.
2593 * It is intended for use in navigation buttons on the DetailView. It will pass an offset and limit argument to the sql query.
2594 * @internal This method must be called on a new instance. It overrides the values of all the fields in the current one.
2596 * @param string $order_by
2597 * @param string $where Additional where clause
2598 * @param int $row_offset Optaional,default 0, starting row number
2599 * @param init $limit Optional, default -1
2600 * @param int $max Optional, default -1
2601 * @param boolean $show_deleted Optioanl, default 0, if set to 1 system will show deleted records.
2602 * @return array Fetched data.
2604 * Internal function, do not override.
2606 function get_detail($order_by = "", $where = "", $offset = 0, $row_offset = 0, $limit=-1, $max=-1, $show_deleted = 0)
2608 $GLOBALS['log']->debug("get_detail: order_by = '$order_by' and where = '$where' and limit = '$limit' and offset = '$offset'");
2609 if(isset($_SESSION['show_deleted']))
2614 if($this->bean_implements('ACL') && ACLController::requireOwner($this->module_dir, 'list') )
2616 global $current_user;
2617 $owner_where = $this->getOwnerWhere($current_user->id);
2621 $where = $owner_where;
2625 $where .= ' AND '. $owner_where;
2628 $query = $this->create_new_list_query($order_by, $where,array(),array(), $show_deleted, $offset);
2630 //Add Limit and Offset to query
2631 //$query .= " LIMIT 1 OFFSET $offset";
2633 return $this->process_detail_query($query, $row_offset, $limit, $max, $where, $offset);
2637 * Fetches data from all related tables.
2639 * @param object $child_seed
2640 * @param string $related_field_name relation to fetch data for
2641 * @param string $order_by Optional, default empty
2642 * @param string $where Optional, additional where clause
2643 * @return array Fetched data.
2645 * Internal function, do not override.
2647 function get_related_list($child_seed,$related_field_name, $order_by = "", $where = "",
2648 $row_offset = 0, $limit=-1, $max=-1, $show_deleted = 0)
2650 global $layout_edit_mode;
2651 $query_array = array();
2653 if(isset($layout_edit_mode) && $layout_edit_mode)
2655 $response = array();
2656 $child_seed->assign_display_fields($child_seed->module_dir);
2657 $response['list'] = array($child_seed);
2658 $response['row_count'] = 1;
2659 $response['next_offset'] = 0;
2660 $response['previous_offset'] = 0;
2664 $GLOBALS['log']->debug("get_related_list: order_by = '$order_by' and where = '$where' and limit = '$limit'");
2665 if(isset($_SESSION['show_deleted']))
2670 $this->load_relationship($related_field_name);
2672 if ($this->$related_field_name instanceof Link) {
2674 $query_array = $this->$related_field_name->getQuery(true);
2677 $query_array = $this->$related_field_name->getQuery(array(
2678 "return_as_array" => true,
2679 'where' => '1=1' // hook for 'where' clause in M2MRelationship file
2683 $entire_where = $query_array['where'];
2686 if(empty($entire_where))
2688 $entire_where = ' WHERE ' . $where;
2692 $entire_where .= ' AND ' . $where;
2696 $query = 'SELECT '.$child_seed->table_name.'.* ' . $query_array['from'] . ' ' . $entire_where;
2697 if(!empty($order_by))
2699 $query .= " ORDER BY " . $order_by;
2702 return $child_seed->process_list_query($query, $row_offset, $limit, $max, $where);
2706 protected static function build_sub_queries_for_union($subpanel_list, $subpanel_def, $parentbean, $order_by)
2708 global $layout_edit_mode, $beanFiles, $beanList;
2709 $subqueries = array();
2710 foreach($subpanel_list as $this_subpanel)
2712 if(!$this_subpanel->isDatasourceFunction() || ($this_subpanel->isDatasourceFunction()
2713 && isset($this_subpanel->_instance_properties['generate_select'])
2714 && $this_subpanel->_instance_properties['generate_select']==true))
2716 //the custom query function must return an array with
2717 if ($this_subpanel->isDatasourceFunction()) {
2718 $shortcut_function_name = $this_subpanel->get_data_source_name();
2719 $parameters=$this_subpanel->get_function_parameters();
2720 if (!empty($parameters))
2722 //if the import file function is set, then import the file to call the custom function from
2723 if (is_array($parameters) && isset($parameters['import_function_file'])){
2724 //this call may happen multiple times, so only require if function does not exist
2725 if(!function_exists($shortcut_function_name)){
2726 require_once($parameters['import_function_file']);
2728 //call function from required file
2729 $query_array = $shortcut_function_name($parameters);
2731 //call function from parent bean
2732 $query_array = $parentbean->$shortcut_function_name($parameters);
2737 $query_array = $parentbean->$shortcut_function_name();
2740 $related_field_name = $this_subpanel->get_data_source_name();
2741 if (!$parentbean->load_relationship($related_field_name)){
2742 unset ($parentbean->$related_field_name);
2745 $query_array = $parentbean->$related_field_name->getSubpanelQuery(array(), true);
2747 $table_where = preg_replace('/^\s*WHERE/i', '', $this_subpanel->get_where());
2748 $where_definition = preg_replace('/^\s*WHERE/i', '', $query_array['where']);
2750 if(!empty($table_where))
2752 if(empty($where_definition))
2754 $where_definition = $table_where;
2758 $where_definition .= ' AND ' . $table_where;
2762 $submodulename = $this_subpanel->_instance_properties['module'];
2763 $submoduleclass = $beanList[$submodulename];
2764 //require_once($beanFiles[$submoduleclass]);
2766 /** @var SugarBean $submodule */
2767 $submodule = new $submoduleclass();
2768 $subwhere = $where_definition;
2772 $list_fields = $this_subpanel->get_list_fields();
2773 foreach($list_fields as $list_key=>$list_field)
2775 if(isset($list_field['usage']) && $list_field['usage'] == 'display_only')
2777 unset($list_fields[$list_key]);
2782 if(!$subpanel_def->isCollection() && isset($list_fields[$order_by]) && isset($submodule->field_defs[$order_by])&& (!isset($submodule->field_defs[$order_by]['source']) || $submodule->field_defs[$order_by]['source'] == 'db'))
2784 $order_by = $submodule->table_name .'.'. $order_by;
2786 $table_name = $this_subpanel->table_name;
2787 $panel_name=$this_subpanel->name;
2789 $params['distinct'] = $this_subpanel->distinct_query();
2791 $params['joined_tables'] = $query_array['join_tables'];
2792 $params['include_custom_fields'] = !$subpanel_def->isCollection();
2793 $params['collection_list'] = $subpanel_def->get_inst_prop_value('collection_list');
2795 // use single select in case when sorting by relate field
2796 $singleSelect = $submodule->is_relate_field($order_by);
2798 $subquery = $submodule->create_new_list_query('',$subwhere ,$list_fields,$params, 0,'', true,$parentbean, $singleSelect);
2800 $subquery['select'] = $subquery['select']." , '$panel_name' panel_name ";
2801 $subquery['from'] = $subquery['from'].$query_array['join'];
2802 $subquery['query_array'] = $query_array;
2803 $subquery['params'] = $params;
2805 $subqueries[] = $subquery;
2812 * Constructs a query to fetch data for supanels and list views
2814 * It constructs union queries for activities subpanel.
2816 * @param SugarBean $parentbean constructing queries for link attributes in this bean
2817 * @param string $order_by Optional, order by clause
2818 * @param string $sort_order Optional, sort order
2819 * @param string $where Optional, additional where clause
2821 * Internal Function, do not overide.
2823 function get_union_related_list($parentbean, $order_by = "", $sort_order='', $where = "",
2824 $row_offset = 0, $limit=-1, $max=-1, $show_deleted = 0, $subpanel_def)
2826 $secondary_queries = array();
2827 global $layout_edit_mode, $beanFiles, $beanList;
2829 if(isset($_SESSION['show_deleted']))
2834 $final_query_rows = '';
2835 $subpanel_list=array();
2836 if ($subpanel_def->isCollection())
2838 $subpanel_def->load_sub_subpanels();
2839 $subpanel_list=$subpanel_def->sub_subpanels;
2843 $subpanel_list[]=$subpanel_def;
2848 //Breaking the building process into two loops. The first loop gets a list of all the sub-queries.
2849 //The second loop merges the queries and forces them to select the same number of columns
2850 //All columns in a sub-subpanel group must have the same aliases
2851 //If the subpanel is a datasource function, it can't be a collection so we just poll that function for the and return that
2852 foreach($subpanel_list as $this_subpanel)
2854 if($this_subpanel->isDatasourceFunction() && empty($this_subpanel->_instance_properties['generate_select']))
2856 $shortcut_function_name = $this_subpanel->get_data_source_name();
2857 $parameters=$this_subpanel->get_function_parameters();
2858 if (!empty($parameters))
2860 //if the import file function is set, then import the file to call the custom function from
2861 if (is_array($parameters) && isset($parameters['import_function_file'])){
2862 //this call may happen multiple times, so only require if function does not exist
2863 if(!function_exists($shortcut_function_name)){
2864 require_once($parameters['import_function_file']);
2866 //call function from required file
2867 $tmp_final_query = $shortcut_function_name($parameters);
2869 //call function from parent bean
2870 $tmp_final_query = $parentbean->$shortcut_function_name($parameters);
2873 $tmp_final_query = $parentbean->$shortcut_function_name();
2877 $final_query_rows .= ' UNION ALL ( '.$parentbean->create_list_count_query($tmp_final_query, $parameters) . ' )';
2878 $final_query .= ' UNION ALL ( '.$tmp_final_query . ' )';
2880 $final_query_rows = '(' . $parentbean->create_list_count_query($tmp_final_query, $parameters) . ')';
2881 $final_query = '(' . $tmp_final_query . ')';
2886 //If final_query is still empty, its time to build the sub-queries
2887 if (empty($final_query))
2889 $subqueries = SugarBean::build_sub_queries_for_union($subpanel_list, $subpanel_def, $parentbean, $order_by);
2890 $all_fields = array();
2891 foreach($subqueries as $i => $subquery)
2893 $query_fields = $GLOBALS['db']->getSelectFieldsFromQuery($subquery['select']);
2894 foreach($query_fields as $field => $select)
2896 if (!in_array($field, $all_fields))
2897 $all_fields[] = $field;
2899 $subqueries[$i]['query_fields'] = $query_fields;
2902 //Now ensure the queries have the same set of fields in the same order.
2903 foreach($subqueries as $subquery)
2905 $subquery['select'] = "SELECT";
2906 foreach($all_fields as $field)
2908 if (!isset($subquery['query_fields'][$field]))
2910 $subquery['select'] .= " NULL $field,";
2914 $subquery['select'] .= " {$subquery['query_fields'][$field]},";
2917 $subquery['select'] = substr($subquery['select'], 0 , strlen($subquery['select']) - 1);
2918 //Put the query into the final_query
2919 $query = $subquery['select'] . " " . $subquery['from'] . " " . $subquery['where'];
2922 $query = ' UNION ALL ( '.$query . ' )';
2923 $final_query_rows .= " UNION ALL ";
2925 $query = '(' . $query . ')';
2928 $query_array = $subquery['query_array'];
2929 $select_position=strpos($query_array['select'],"SELECT");
2930 $distinct_position=strpos($query_array['select'],"DISTINCT");
2931 if (!empty($subquery['params']['distinct']) && !empty($subpanel_def->table_name))
2933 $query_rows = "( SELECT count(DISTINCT ". $subpanel_def->table_name . ".id)". $subquery['from_min'].$query_array['join']. $subquery['where'].' )';
2935 elseif ($select_position !== false && $distinct_position!= false)
2937 $query_rows = "( ".substr_replace($query_array['select'],"SELECT count(",$select_position,6). ")" . $subquery['from_min'].$query_array['join']. $subquery['where'].' )';
2941 //resort to default behavior.
2942 $query_rows = "( SELECT count(*)". $subquery['from_min'].$query_array['join']. $subquery['where'].' )';
2944 if(!empty($subquery['secondary_select']))
2947 $subquerystring= $subquery['secondary_select'] . $subquery['secondary_from'].$query_array['join']. $subquery['where'];
2948 if (!empty($subquery['secondary_where']))
2950 if (empty($subquery['where']))
2952 $subquerystring.=" WHERE " .$subquery['secondary_where'];
2956 $subquerystring.=" AND " .$subquery['secondary_where'];
2959 $secondary_queries[]=$subquerystring;
2961 $final_query .= $query;
2962 $final_query_rows .= $query_rows;
2966 if(!empty($order_by))
2969 if(!$subpanel_def->isCollection())
2971 $submodulename = $subpanel_def->_instance_properties['module'];
2972 $submoduleclass = $beanList[$submodulename];
2973 $submodule = new $submoduleclass();
2975 if(!empty($submodule) && !empty($submodule->table_name))
2977 $final_query .= " ORDER BY " .$parentbean->process_order_by($order_by, $submodule);
2982 $final_query .= " ORDER BY ". $order_by . ' ';
2984 if(!empty($sort_order))
2986 $final_query .= ' ' .$sort_order;
2991 if(isset($layout_edit_mode) && $layout_edit_mode)
2993 $response = array();
2994 if(!empty($submodule))
2996 $submodule->assign_display_fields($submodule->module_dir);
2997 $response['list'] = array($submodule);
3001 $response['list'] = array();
3003 $response['parent_data'] = array();
3004 $response['row_count'] = 1;
3005 $response['next_offset'] = 0;
3006 $response['previous_offset'] = 0;
3011 return $parentbean->process_union_list_query($parentbean, $final_query, $row_offset, $limit, $max, '',$subpanel_def, $final_query_rows, $secondary_queries);
3016 * Returns a full (ie non-paged) list of the current object type.
3018 * @param string $order_by the order by SQL parameter. defaults to ""
3019 * @param string $where where clause. defaults to ""
3020 * @param boolean $check_dates. defaults to false
3021 * @param int $show_deleted show deleted records. defaults to 0
3023 function get_full_list($order_by = "", $where = "", $check_dates=false, $show_deleted = 0)
3025 $GLOBALS['log']->debug("get_full_list: order_by = '$order_by' and where = '$where'");
3026 if(isset($_SESSION['show_deleted']))
3030 $query = $this->create_new_list_query($order_by, $where,array(),array(), $show_deleted);
3031 return $this->process_full_list_query($query, $check_dates);
3035 * Return the list query used by the list views and export button. Next generation of create_new_list_query function.
3037 * Override this function to return a custom query.
3039 * @param string $order_by custom order by clause
3040 * @param string $where custom where clause
3041 * @param array $filter Optioanal
3042 * @param array $params Optional *
3043 * @param int $show_deleted Optional, default 0, show deleted records is set to 1.
3044 * @param string $join_type
3045 * @param boolean $return_array Optional, default false, response as array
3046 * @param object $parentbean creating a subquery for this bean.
3047 * @param boolean $singleSelect Optional, default false.
3048 * @return String select query string, optionally an array value will be returned if $return_array= true.
3050 function create_new_list_query($order_by, $where,$filter=array(),$params=array(), $show_deleted = 0,$join_type='', $return_array = false,$parentbean=null, $singleSelect = false, $ifListForExport = false)
3052 global $beanFiles, $beanList;
3053 $selectedFields = array();
3054 $secondarySelectedFields = array();
3055 $ret_array = array();
3057 if($this->bean_implements('ACL') && ACLController::requireOwner($this->module_dir, 'list') )
3059 global $current_user;
3060 $owner_where = $this->getOwnerWhere($current_user->id);
3063 $where = $owner_where;
3067 $where .= ' AND '. $owner_where;
3070 if(!empty($params['distinct']))
3072 $distinct = ' DISTINCT ';
3076 $ret_array['select'] = " SELECT $distinct $this->table_name.* ";
3080 $ret_array['select'] = " SELECT $distinct $this->table_name.id ";
3082 $ret_array['from'] = " FROM $this->table_name ";
3083 $ret_array['from_min'] = $ret_array['from'];
3084 $ret_array['secondary_from'] = $ret_array['from'] ;
3085 $ret_array['where'] = '';
3086 $ret_array['order_by'] = '';
3087 //secondary selects are selects that need to be run after the primary query to retrieve additional info on main
3090 $ret_array['secondary_select']=& $ret_array['select'];
3091 $ret_array['secondary_from'] = & $ret_array['from'];
3095 $ret_array['secondary_select'] = '';
3097 $custom_join = $this->getCustomJoin( empty($filter)? true: $filter );
3098 if((!isset($params['include_custom_fields']) || $params['include_custom_fields']))
3100 $ret_array['select'] .= $custom_join['select'];
3102 $ret_array['from'] .= $custom_join['join'];
3103 // Bug 52490 - Captivea (Sve) - To be able to add custom fields inside where clause in a subpanel
3104 $ret_array['from_min'] .= $custom_join['join'];
3106 //LOOP AROUND FOR FIXIN VARDEF ISSUES
3107 require('include/VarDefHandler/listvardefoverride.php');
3108 if (file_exists('custom/include/VarDefHandler/listvardefoverride.php'))
3110 require('custom/include/VarDefHandler/listvardefoverride.php');
3113 $joined_tables = array();
3114 if(!empty($params['joined_tables']))
3116 foreach($params['joined_tables'] as $table)
3118 $joined_tables[$table] = 1;
3124 $filterKeys = array_keys($filter);
3125 if(is_numeric($filterKeys[0]))
3128 foreach($filter as $field)
3130 $field = strtolower($field);
3131 //remove out id field so we don't duplicate it
3132 if ( $field == 'id' && !empty($filter) ) {
3135 if(isset($this->field_defs[$field]))
3137 $fields[$field]= $this->field_defs[$field];
3141 $fields[$field] = array('force_exists'=>true);
3150 $fields = $this->field_defs;
3153 $used_join_key = array();
3155 foreach($fields as $field=>$value)
3157 //alias is used to alias field names
3159 if (isset($value['alias']))
3161 $alias =' as ' . $value['alias'] . ' ';
3164 if(empty($this->field_defs[$field]) || !empty($value['force_blank']) )
3166 if(!empty($filter) && isset($filter[$field]['force_exists']) && $filter[$field]['force_exists'])
3168 if ( isset($filter[$field]['force_default']) )
3169 $ret_array['select'] .= ", {$filter[$field]['force_default']} $field ";
3171 //spaces are a fix for length issue problem with unions. The union only returns the maximum number of characters from the first select statement.
3172 $ret_array['select'] .= ", ' ' $field ";
3178 $data = $this->field_defs[$field];
3181 //ignore fields that are a part of the collection and a field has been removed as a result of
3182 //layout customization.. this happens in subpanel customizations, use case, from the contacts subpanel
3183 //in opportunities module remove the contact_role/opportunity_role field.
3184 $process_field=true;
3185 if (isset($data['relationship_fields']) and !empty($data['relationship_fields']))
3187 foreach ($data['relationship_fields'] as $field_name)
3189 if (!isset($fields[$field_name]))
3191 $process_field=false;
3195 if (!$process_field)
3200 if( (!isset($data['source']) || $data['source'] == 'db') && (!empty($alias) || !empty($filter) ))
3202 $ret_array['select'] .= ", $this->table_name.$field $alias";
3203 $selectedFields["$this->table_name.$field"] = true;
3204 } else if( (!isset($data['source']) || $data['source'] == 'custom_fields') && (!empty($alias) || !empty($filter) )) {
3205 //add this column only if it has NOT already been added to select statement string
3206 $colPos = strpos($ret_array['select'],"$this->table_name"."_cstm".".$field");
3207 if(!$colPos || $colPos<0)
3209 $ret_array['select'] .= ", $this->table_name"."_cstm".".$field $alias";
3212 $selectedFields["$this->table_name.$field"] = true;
3215 if($data['type'] != 'relate' && isset($data['db_concat_fields']))
3217 $ret_array['select'] .= ", " . $this->db->concat($this->table_name, $data['db_concat_fields']) . " as $field";
3218 $selectedFields[$this->db->concat($this->table_name, $data['db_concat_fields'])] = true;
3220 //Custom relate field or relate fields built in module builder which have no link field associated.
3221 if ($data['type'] == 'relate' && (isset($data['custom_module']) || isset($data['ext2']))) {
3222 $joinTableAlias = 'jt' . $jtcount;
3223 $relateJoinInfo = $this->custom_fields->getRelateJoin($data, $joinTableAlias);
3224 $ret_array['select'] .= $relateJoinInfo['select'];
3225 $ret_array['from'] .= $relateJoinInfo['from'];
3226 //Replace any references to the relationship in the where clause with the new alias
3227 //If the link isn't set, assume that search used the local table for the field
3228 $searchTable = isset($data['link']) ? $relateJoinInfo['rel_table'] : $this->table_name;
3229 $field_name = $relateJoinInfo['rel_table'] . '.' . !empty($data['name'])?$data['name']:'name';
3230 $where = preg_replace('/(^|[\s(])' . $field_name . '/' , '${1}' . $relateJoinInfo['name_field'], $where);
3234 if ($data['type'] == 'parent') {
3235 //See if we need to join anything by inspecting the where clause
3236 $match = preg_match('/(^|[\s(])parent_(\w+)_(\w+)\.name/', $where, $matches);
3238 $joinTableAlias = 'jt' . $jtcount;
3239 $joinModule = $matches[2];
3240 $joinTable = $matches[3];
3241 $localTable = $this->table_name;
3242 if (!empty($data['custom_module'])) {
3243 $localTable .= '_cstm';
3245 global $beanFiles, $beanList, $module;
3246 require_once($beanFiles[$beanList[$joinModule]]);
3247 $rel_mod = new $beanList[$joinModule]();
3248 $nameField = "$joinTableAlias.name";
3249 if (isset($rel_mod->field_defs['name']))
3251 $name_field_def = $rel_mod->field_defs['name'];
3252 if(isset($name_field_def['db_concat_fields']))
3254 $nameField = $this->db->concat($joinTableAlias, $name_field_def['db_concat_fields']);
3257 $ret_array['select'] .= ", $nameField {$data['name']} ";
3258 $ret_array['from'] .= " LEFT JOIN $joinTable $joinTableAlias
3259 ON $localTable.{$data['id_name']} = $joinTableAlias.id";
3260 //Replace any references to the relationship in the where clause with the new alias
3261 $where = preg_replace('/(^|[\s(])parent_' . $joinModule . '_' . $joinTable . '\.name/', '${1}' . $nameField, $where);
3266 if ($this->is_relate_field($field))
3268 $this->load_relationship($data['link']);
3269 if(!empty($this->$data['link']))
3272 if(empty($join_type))
3274 $params['join_type'] = ' LEFT JOIN ';
3278 $params['join_type'] = $join_type;
3280 if(isset($data['join_name']))
3282 $params['join_table_alias'] = $data['join_name'];
3286 $params['join_table_alias'] = 'jt' . $jtcount;
3289 if(isset($data['join_link_name']))
3291 $params['join_table_link_alias'] = $data['join_link_name'];
3295 $params['join_table_link_alias'] = 'jtl' . $jtcount;
3297 $join_primary = !isset($data['join_primary']) || $data['join_primary'];
3299 $join = $this->$data['link']->getJoin($params, true);
3300 $used_join_key[] = $join['rel_key'];
3301 $rel_module = $this->$data['link']->getRelatedModuleName();
3302 $table_joined = !empty($joined_tables[$params['join_table_alias']]) || (!empty($joined_tables[$params['join_table_link_alias']]) && isset($data['link_type']) && $data['link_type'] == 'relationship_info');
3304 //if rname is set to 'name', and bean files exist, then check if field should be a concatenated name
3305 global $beanFiles, $beanList;
3306 if($data['rname'] && !empty($beanFiles[$beanList[$rel_module]])) {
3308 //create an instance of the related bean
3309 require_once($beanFiles[$beanList[$rel_module]]);
3310 $rel_mod = new $beanList[$rel_module]();
3311 //if bean has first and last name fields, then name should be concatenated
3312 if(isset($rel_mod->field_name_map['first_name']) && isset($rel_mod->field_name_map['last_name'])){
3313 $data['db_concat_fields'] = array(0=>'first_name', 1=>'last_name');
3318 if($join['type'] == 'many-to-many')
3320 if(empty($ret_array['secondary_select']))
3322 $ret_array['secondary_select'] = " SELECT $this->table_name.id ref_id ";
3324 if(!empty($beanFiles[$beanList[$rel_module]]) && $join_primary)
3326 require_once($beanFiles[$beanList[$rel_module]]);
3327 $rel_mod = new $beanList[$rel_module]();
3328 if(isset($rel_mod->field_defs['assigned_user_id']))
3330 $ret_array['secondary_select'].= " , ". $params['join_table_alias'] . ".assigned_user_id {$field}_owner, '$rel_module' {$field}_mod";
3334 if(isset($rel_mod->field_defs['created_by']))
3336 $ret_array['secondary_select'].= " , ". $params['join_table_alias'] . ".created_by {$field}_owner , '$rel_module' {$field}_mod";
3342 if(isset($data['db_concat_fields']))
3344 $ret_array['secondary_select'] .= ' , ' . $this->db->concat($params['join_table_alias'], $data['db_concat_fields']) . ' ' . $field;
3348 if(!isset($data['relationship_fields']))
3350 $ret_array['secondary_select'] .= ' , ' . $params['join_table_alias'] . '.' . $data['rname'] . ' ' . $field;
3355 $ret_array['select'] .= ", ' ' $field ";
3358 foreach($used_join_key as $used_key) {
3359 if($used_key == $join['rel_key']) $count_used++;
3361 if($count_used <= 1) {//27416, the $ret_array['secondary_select'] should always generate, regardless the dbtype
3362 // add rel_key only if it was not aready added
3365 $ret_array['select'] .= ", ' ' " . $join['rel_key'] . ' ';
3367 $ret_array['secondary_select'] .= ', ' . $params['join_table_link_alias'].'.'. $join['rel_key'] .' ' . $join['rel_key'];
3369 if(isset($data['relationship_fields']))
3371 foreach($data['relationship_fields'] as $r_name=>$alias_name)
3373 if(!empty( $secondarySelectedFields[$alias_name]))continue;
3374 $ret_array['secondary_select'] .= ', ' . $params['join_table_link_alias'].'.'. $r_name .' ' . $alias_name;
3375 $secondarySelectedFields[$alias_name] = true;
3380 $ret_array['secondary_from'] .= ' ' . $join['join']. ' AND ' . $params['join_table_alias'].'.deleted=0';
3381 if (isset($data['link_type']) && $data['link_type'] == 'relationship_info' && ($parentbean instanceOf SugarBean))
3383 $ret_array['secondary_where'] = $params['join_table_link_alias'] . '.' . $join['rel_key']. "='" .$parentbean->id . "'";
3389 if(isset($data['db_concat_fields']))
3391 $ret_array['select'] .= ' , ' . $this->db->concat($params['join_table_alias'], $data['db_concat_fields']) . ' ' . $field;
3395 $ret_array['select'] .= ' , ' . $params['join_table_alias'] . '.' . $data['rname'] . ' ' . $field;
3397 if(isset($data['additionalFields'])){
3398 foreach($data['additionalFields'] as $k=>$v){
3399 if (!empty($data['id_name']) && $data['id_name'] == $v && !empty($fields[$data['id_name']])) {
3402 $ret_array['select'] .= ' , ' . $params['join_table_alias'] . '.' . $k . ' ' . $v;
3407 $ret_array['from'] .= ' ' . $join['join']. ' AND ' . $params['join_table_alias'].'.deleted=0';
3408 if(!empty($beanList[$rel_module]) && !empty($beanFiles[$beanList[$rel_module]]))
3410 require_once($beanFiles[$beanList[$rel_module]]);
3411 $rel_mod = new $beanList[$rel_module]();
3412 if(isset($value['target_record_key']) && !empty($filter))
3414 $selectedFields[$this->table_name.'.'.$value['target_record_key']] = true;
3415 $ret_array['select'] .= " , $this->table_name.{$value['target_record_key']} ";
3417 if(isset($rel_mod->field_defs['assigned_user_id']))
3419 $ret_array['select'] .= ' , ' .$params['join_table_alias'] . '.assigned_user_id ' . $field . '_owner';
3423 $ret_array['select'] .= ' , ' .$params['join_table_alias'] . '.created_by ' . $field . '_owner';
3425 $ret_array['select'] .= " , '".$rel_module ."' " . $field . '_mod';
3430 // To fix SOAP stuff where we are trying to retrieve all the accounts data where accounts.id = ..
3431 // and this code changes accounts to jt4 as there is a self join with the accounts table.
3433 if(isset($data['db_concat_fields'])){
3434 $buildWhere = false;
3435 if(in_array('first_name', $data['db_concat_fields']) && in_array('last_name', $data['db_concat_fields']))
3437 $exp = '/\(\s*?'.$data['name'].'.*?\%\'\s*?\)/';
3438 if(preg_match($exp, $where, $matches))
3440 $search_expression = $matches[0];
3441 //Create three search conditions - first + last, first, last
3442 $first_name_search = str_replace($data['name'], $params['join_table_alias'] . '.first_name', $search_expression);
3443 $last_name_search = str_replace($data['name'], $params['join_table_alias'] . '.last_name', $search_expression);
3444 $full_name_search = str_replace($data['name'], $this->db->concat($params['join_table_alias'], $data['db_concat_fields']), $search_expression);
3446 $where = str_replace($search_expression, '(' . $full_name_search . ' OR ' . $first_name_search . ' OR ' . $last_name_search . ')', $where);
3452 $db_field = $this->db->concat($params['join_table_alias'], $data['db_concat_fields']);
3453 $where = preg_replace('/'.$data['name'].'/', $db_field, $where);
3456 $where = preg_replace('/(^|[\s(])' . $data['name'] . '/', '${1}' . $params['join_table_alias'] . '.'.$data['rname'], $where);
3460 $joined_tables[$params['join_table_alias']]=1;
3461 $joined_tables[$params['join_table_link_alias']]=1;
3470 if(isset($this->field_defs['assigned_user_id']) && empty($selectedFields[$this->table_name.'.assigned_user_id']))
3472 $ret_array['select'] .= ", $this->table_name.assigned_user_id ";
3474 else if(isset($this->field_defs['created_by']) && empty($selectedFields[$this->table_name.'.created_by']))
3476 $ret_array['select'] .= ", $this->table_name.created_by ";
3478 if(isset($this->field_defs['system_id']) && empty($selectedFields[$this->table_name.'.system_id']))
3480 $ret_array['select'] .= ", $this->table_name.system_id ";
3485 if ($ifListForExport) {
3486 if(isset($this->field_defs['email1'])) {
3487 $ret_array['select'].= " ,email_addresses.email_address email1";
3488 $ret_array['from'].= " LEFT JOIN email_addr_bean_rel on {$this->table_name}.id = email_addr_bean_rel.bean_id and email_addr_bean_rel.bean_module='{$this->module_dir}' and email_addr_bean_rel.deleted=0 and email_addr_bean_rel.primary_address=1 LEFT JOIN email_addresses on email_addresses.id = email_addr_bean_rel.email_address_id ";
3492 $where_auto = '1=1';
3493 if($show_deleted == 0)
3495 $where_auto = "$this->table_name.deleted=0";
3496 }else if($show_deleted == 1)
3498 $where_auto = "$this->table_name.deleted=1";
3501 $ret_array['where'] = " where ($where) AND $where_auto";
3503 $ret_array['where'] = " where $where_auto";
3504 if(!empty($order_by))
3506 //make call to process the order by clause
3507 $ret_array['order_by'] = " ORDER BY ". $this->process_order_by($order_by);
3511 unset($ret_array['secondary_where']);
3512 unset($ret_array['secondary_from']);
3513 unset($ret_array['secondary_select']);
3521 return $ret_array['select'] . $ret_array['from'] . $ret_array['where']. $ret_array['order_by'];
3524 * Returns parent record data for objects that store relationship information
3526 * @param array $type_info
3528 * Interal function, do not override.
3530 function retrieve_parent_fields($type_info)
3533 global $beanList, $beanFiles;
3534 $templates = array();
3535 $parent_child_map = array();
3536 foreach($type_info as $children_info)
3538 foreach($children_info as $child_info)
3540 if($child_info['type'] == 'parent')
3542 if(empty($templates[$child_info['parent_type']]))
3544 //Test emails will have an invalid parent_type, don't try to load the non-existent parent bean
3545 if ($child_info['parent_type'] == 'test') {
3548 $class = $beanList[$child_info['parent_type']];
3549 // Added to avoid error below; just silently fail and write message to log
3550 if ( empty($beanFiles[$class]) ) {
3551 $GLOBALS['log']->error($this->object_name.'::retrieve_parent_fields() - cannot load class "'.$class.'", skip loading.');
3554 require_once($beanFiles[$class]);
3555 $templates[$child_info['parent_type']] = new $class();
3558 if(empty($queries[$child_info['parent_type']]))
3560 $queries[$child_info['parent_type']] = "SELECT id ";
3561 $field_def = $templates[$child_info['parent_type']]->field_defs['name'];
3562 if(isset($field_def['db_concat_fields']))
3564 $queries[$child_info['parent_type']] .= ' , ' . $this->db->concat($templates[$child_info['parent_type']]->table_name, $field_def['db_concat_fields']) . ' parent_name';
3568 $queries[$child_info['parent_type']] .= ' , name parent_name';
3570 if(isset($templates[$child_info['parent_type']]->field_defs['assigned_user_id']))
3572 $queries[$child_info['parent_type']] .= ", assigned_user_id parent_name_owner , '{$child_info['parent_type']}' parent_name_mod";;
3573 }else if(isset($templates[$child_info['parent_type']]->field_defs['created_by']))
3575 $queries[$child_info['parent_type']] .= ", created_by parent_name_owner, '{$child_info['parent_type']}' parent_name_mod";
3577 $queries[$child_info['parent_type']] .= " FROM " . $templates[$child_info['parent_type']]->table_name ." WHERE id IN ('{$child_info['parent_id']}'";
3581 if(empty($parent_child_map[$child_info['parent_id']]))
3582 $queries[$child_info['parent_type']] .= " ,'{$child_info['parent_id']}'";
3584 $parent_child_map[$child_info['parent_id']][] = $child_info['child_id'];
3589 foreach($queries as $query)
3591 $result = $this->db->query($query . ')');
3592 while($row = $this->db->fetchByAssoc($result))
3594 $results[$row['id']] = $row;
3598 $child_results = array();
3599 foreach($parent_child_map as $parent_key=>$parent_child)
3601 foreach($parent_child as $child)
3603 if(isset( $results[$parent_key]))
3605 $child_results[$child] = $results[$parent_key];
3609 return $child_results;
3613 * Processes the list query and return fetched row.
3615 * Internal function, do not override.
3616 * @param string $query select query to be processed.
3617 * @param int $row_offset starting position
3618 * @param int $limit Optioanl, default -1
3619 * @param int $max_per_page Optional, default -1
3620 * @param string $where Optional, additional filter criteria.
3621 * @return array Fetched data
3623 function process_list_query($query, $row_offset, $limit= -1, $max_per_page = -1, $where = '')
3625 global $sugar_config;
3626 $db = DBManagerFactory::getInstance('listviews');
3628 * if the row_offset is set to 'end' go to the end of the list
3630 $toEnd = strval($row_offset) == 'end';
3631 $GLOBALS['log']->debug("process_list_query: ".$query);
3632 if($max_per_page == -1)
3634 $max_per_page = $sugar_config['list_max_entries_per_page'];
3636 // Check to see if we have a count query available.
3637 if(empty($sugar_config['disable_count_query']) || $toEnd)
3639 $count_query = $this->create_list_count_query($query);
3640 if(!empty($count_query) && (empty($limit) || $limit == -1))
3642 // We have a count query. Run it and get the results.
3643 $result = $db->query($count_query, true, "Error running count query for $this->object_name List: ");
3644 $assoc = $db->fetchByAssoc($result);
3645 if(!empty($assoc['c']))
3647 $rows_found = $assoc['c'];
3648 $limit = $sugar_config['list_max_entries_per_page'];
3652 $row_offset = (floor(($rows_found -1) / $limit)) * $limit;
3658 if((empty($limit) || $limit == -1))
3660 $limit = $max_per_page + 1;
3661 $max_per_page = $limit;
3666 if(empty($row_offset))
3670 if(!empty($limit) && $limit != -1 && $limit != -99)
3672 $result = $db->limitQuery($query, $row_offset, $limit,true,"Error retrieving $this->object_name list: ");
3676 $result = $db->query($query,true,"Error retrieving $this->object_name list: ");
3681 $previous_offset = $row_offset - $max_per_page;
3682 $next_offset = $row_offset + $max_per_page;
3684 $class = get_class($this);
3685 //FIXME: Bug? we should remove the magic number -99
3686 //use -99 to return all
3687 $index = $row_offset;
3688 while ($max_per_page == -99 || ($index < $row_offset + $max_per_page))
3690 $row = $db->fetchByAssoc($result);
3691 if (empty($row)) break;
3693 //instantiate a new class each time. This is because php5 passes
3694 //by reference by default so if we continually update $this, we will
3695 //at the end have a list of all the same objects
3696 /** @var SugarBean $temp */
3697 $temp = new $class();
3699 foreach($this->field_defs as $field=>$value)
3701 if (isset($row[$field]))
3703 $temp->$field = $row[$field];
3704 $owner_field = $field . '_owner';
3705 if(isset($row[$owner_field]))
3707 $temp->$owner_field = $row[$owner_field];
3710 $GLOBALS['log']->debug("$temp->object_name({$row['id']}): ".$field." = ".$temp->$field);
3711 }else if (isset($row[$this->table_name .'.'.$field]))
3713 $temp->$field = $row[$this->table_name .'.'.$field];
3721 $temp->check_date_relationships_load();
3722 $temp->fill_in_additional_list_fields();
3723 if($temp->hasCustomFields()) $temp->custom_fields->fill_relationships();
3724 $temp->call_custom_logic("process_record");
3726 // fix defect #44206. implement the same logic as sugar_currency_format
3727 // Smarty modifier does.
3728 $temp->populateCurrencyFields();
3733 if(!empty($sugar_config['disable_count_query']) && !empty($limit))
3736 $rows_found = $row_offset + count($list);
3743 } else if(!isset($rows_found)){
3744 $rows_found = $row_offset + count($list);
3747 $response = Array();
3748 $response['list'] = $list;
3749 $response['row_count'] = $rows_found;
3750 $response['next_offset'] = $next_offset;
3751 $response['previous_offset'] = $previous_offset;
3752 $response['current_offset'] = $row_offset ;
3757 * Returns the number of rows that the given SQL query should produce
3759 * Internal function, do not override.
3760 * @param string $query valid select query
3761 * @param boolean $is_count_query Optional, Default false, set to true if passed query is a count query.
3762 * @return int count of rows found
3764 function _get_num_rows_in_query($query, $is_count_query=false)
3766 $num_rows_in_query = 0;
3767 if (!$is_count_query)
3769 $count_query = SugarBean::create_list_count_query($query);
3771 $count_query=$query;
3773 $result = $this->db->query($count_query, true, "Error running count query for $this->object_name List: ");
3775 while($row = $this->db->fetchByAssoc($result, true))
3777 $num_rows_in_query += current($row);
3780 return $num_rows_in_query;
3784 * Applies pagination window to union queries used by list view and subpanels,
3785 * executes the query and returns fetched data.
3787 * Internal function, do not override.
3788 * @param object $parent_bean
3789 * @param string $query query to be processed.
3790 * @param int $row_offset
3791 * @param int $limit optional, default -1
3792 * @param int $max_per_page Optional, default -1
3793 * @param string $where Custom where clause.
3794 * @param array $subpanel_def definition of sub-panel to be processed
3795 * @param string $query_row_count
3796 * @param array $seconday_queries
3797 * @return array Fetched data.
3799 function process_union_list_query($parent_bean, $query,
3800 $row_offset, $limit= -1, $max_per_page = -1, $where = '', $subpanel_def, $query_row_count='', $secondary_queries = array())
3803 $db = DBManagerFactory::getInstance('listviews');
3805 * if the row_offset is set to 'end' go to the end of the list
3807 $toEnd = strval($row_offset) == 'end';
3808 global $sugar_config;
3809 $use_count_query=false;
3810 $processing_collection=$subpanel_def->isCollection();
3812 $GLOBALS['log']->debug("process_union_list_query: ".$query);
3813 if($max_per_page == -1)
3815 $max_per_page = $sugar_config['list_max_entries_per_subpanel'];
3817 if(empty($query_row_count))
3819 $query_row_count = $query;
3821 $distinct_position=strpos($query_row_count,"DISTINCT");
3823 if ($distinct_position!= false)
3825 $use_count_query=true;
3827 $performSecondQuery = true;
3828 if(empty($sugar_config['disable_count_query']) || $toEnd)
3830 $rows_found = $this->_get_num_rows_in_query($query_row_count,$use_count_query);
3833 $performSecondQuery = false;
3835 if(!empty($rows_found) && (empty($limit) || $limit == -1))
3837 $limit = $sugar_config['list_max_entries_per_subpanel'];
3841 $row_offset = (floor(($rows_found -1) / $limit)) * $limit;
3847 if((empty($limit) || $limit == -1))
3849 $limit = $max_per_page + 1;
3850 $max_per_page = $limit;
3854 if(empty($row_offset))
3859 $previous_offset = $row_offset - $max_per_page;
3860 $next_offset = $row_offset + $max_per_page;
3862 if($performSecondQuery)
3864 if(!empty($limit) && $limit != -1 && $limit != -99)
3866 $result = $db->limitQuery($query, $row_offset, $limit,true,"Error retrieving $parent_bean->object_name list: ");
3870 $result = $db->query($query,true,"Error retrieving $this->object_name list: ");
3872 //use -99 to return all
3874 // get the current row
3875 $index = $row_offset;
3876 $row = $db->fetchByAssoc($result);
3878 $post_retrieve = array();
3879 $isFirstTime = true;
3882 $function_fields = array();
3883 if(($index < $row_offset + $max_per_page || $max_per_page == -99))
3885 if ($processing_collection)
3887 $current_bean =$subpanel_def->sub_subpanels[$row['panel_name']]->template_instance;
3890 $class = get_class($subpanel_def->sub_subpanels[$row['panel_name']]->template_instance);
3891 $current_bean = new $class();
3894 $current_bean=$subpanel_def->template_instance;
3897 $class = get_class($subpanel_def->template_instance);
3898 $current_bean = new $class();
3901 $isFirstTime = false;
3902 //set the panel name in the bean instance.
3903 if (isset($row['panel_name']))
3905 $current_bean->panel_name=$row['panel_name'];
3907 foreach($current_bean->field_defs as $field=>$value)
3910 if (isset($row[$field]))
3912 $current_bean->$field = $this->convertField($row[$field], $value);
3913 unset($row[$field]);
3915 else if (isset($row[$this->table_name .'.'.$field]))
3917 $current_bean->$field = $this->convertField($row[$this->table_name .'.'.$field], $value);
3918 unset($row[$this->table_name .'.'.$field]);
3922 $current_bean->$field = "";
3923 unset($row[$field]);
3925 if(isset($value['source']) && $value['source'] == 'function')
3927 $function_fields[]=$field;
3930 foreach($row as $key=>$value)
3932 $current_bean->$key = $value;
3934 foreach($function_fields as $function_field)
3936 $value = $current_bean->field_defs[$function_field];
3937 $can_execute = true;
3938 $execute_params = array();
3939 $execute_function = array();
3940 if(!empty($value['function_class']))
3942 $execute_function[] = $value['function_class'];
3943 $execute_function[] = $value['function_name'];
3947 $execute_function = $value['function_name'];
3949 foreach($value['function_params'] as $param )
3951 if (empty($value['function_params_source']) or $value['function_params_source']=='parent')
3953 if(empty($this->$param))
3955 $can_execute = false;
3956 } else if($param == '$this') {
3957 $execute_params[] = $this;
3961 $execute_params[] = $this->$param;
3963 } else if ($value['function_params_source']=='this')
3965 if(empty($current_bean->$param))
3967 $can_execute = false;
3968 } else if($param == '$this') {
3969 $execute_params[] = $current_bean;
3973 $execute_params[] = $current_bean->$param;
3978 $can_execute = false;
3984 if(!empty($value['function_require']))
3986 require_once($value['function_require']);
3988 $current_bean->$function_field = call_user_func_array($execute_function, $execute_params);
3991 if(!empty($current_bean->parent_type) && !empty($current_bean->parent_id))
3993 if(!isset($post_retrieve[$current_bean->parent_type]))
3995 $post_retrieve[$current_bean->parent_type] = array();
3997 $post_retrieve[$current_bean->parent_type][] = array('child_id'=>$current_bean->id, 'parent_id'=> $current_bean->parent_id, 'parent_type'=>$current_bean->parent_type, 'type'=>'parent');
3999 //$current_bean->fill_in_additional_list_fields();
4000 $list[$current_bean->id] = $current_bean;
4002 // go to the next row
4004 $row = $db->fetchByAssoc($result);
4006 //now handle retrieving many-to-many relationships
4009 foreach($secondary_queries as $query2)
4011 $result2 = $db->query($query2);
4013 $row2 = $db->fetchByAssoc($result2);
4016 $id_ref = $row2['ref_id'];
4018 if(isset($list[$id_ref]))
4020 foreach($row2 as $r2key=>$r2value)
4022 if($r2key != 'ref_id')
4024 $list[$id_ref]->$r2key = $r2value;
4028 $row2 = $db->fetchByAssoc($result2);
4034 if(isset($post_retrieve))
4036 $parent_fields = $this->retrieve_parent_fields($post_retrieve);
4040 $parent_fields = array();
4042 if(!empty($sugar_config['disable_count_query']) && !empty($limit))
4044 //C.L. Bug 43535 - Use the $index value to set the $rows_found value here
4045 $rows_found = isset($index) ? $index : $row_offset + count($list);
4047 if(count($list) >= $limit)
4061 $parent_fields = array();
4063 $response = array();
4064 $response['list'] = $list;
4065 $response['parent_data'] = $parent_fields;
4066 $response['row_count'] = $rows_found;
4067 $response['next_offset'] = $next_offset;
4068 $response['previous_offset'] = $previous_offset;
4069 $response['current_offset'] = $row_offset ;
4070 $response['query'] = $query;
4076 * Applies pagination window to select queries used by detail view,
4077 * executes the query and returns fetched data.
4079 * Internal function, do not override.
4080 * @param string $query query to be processed.
4081 * @param int $row_offset
4082 * @param int $limit optional, default -1
4083 * @param int $max_per_page Optional, default -1
4084 * @param string $where Custom where clause.
4085 * @param int $offset Optional, default 0
4086 * @return array Fetched data.
4089 function process_detail_query($query, $row_offset, $limit= -1, $max_per_page = -1, $where = '', $offset = 0)
4091 global $sugar_config;
4092 $GLOBALS['log']->debug("process_detail_query: ".$query);
4093 if($max_per_page == -1)
4095 $max_per_page = $sugar_config['list_max_entries_per_page'];
4098 // Check to see if we have a count query available.
4099 $count_query = $this->create_list_count_query($query);
4101 if(!empty($count_query) && (empty($limit) || $limit == -1))
4103 // We have a count query. Run it and get the results.
4104 $result = $this->db->query($count_query, true, "Error running count query for $this->object_name List: ");
4105 $assoc = $this->db->fetchByAssoc($result);
4106 if(!empty($assoc['c']))
4108 $total_rows = $assoc['c'];
4112 if(empty($row_offset))
4117 $result = $this->db->limitQuery($query, $offset, 1, true,"Error retrieving $this->object_name list: ");
4119 $previous_offset = $row_offset - $max_per_page;
4120 $next_offset = $row_offset + $max_per_page;
4122 $row = $this->db->fetchByAssoc($result);
4123 $this->retrieve($row['id']);
4125 $response = Array();
4126 $response['bean'] = $this;
4127 if (empty($total_rows))
4129 $response['row_count'] = $total_rows;
4130 $response['next_offset'] = $next_offset;
4131 $response['previous_offset'] = $previous_offset;
4137 * Processes fetched list view data
4139 * Internal function, do not override.
4140 * @param string $query query to be processed.
4141 * @param boolean $check_date Optional, default false. if set to true date time values are processed.
4142 * @return array Fetched data.
4145 function process_full_list_query($query, $check_date=false)
4148 $GLOBALS['log']->debug("process_full_list_query: query is ".$query);
4149 $result = $this->db->query($query, false);
4150 $GLOBALS['log']->debug("process_full_list_query: result is ".print_r($result,true));
4151 $class = get_class($this);
4152 $isFirstTime = true;
4153 $bean = new $class();
4155 // We have some data.
4156 while (($row = $bean->db->fetchByAssoc($result)) != null)
4158 $row = $this->convertRow($row);
4161 $bean = new $class();
4163 $isFirstTime = false;
4165 foreach($bean->field_defs as $field=>$value)
4167 if (isset($row[$field]))
4169 $bean->$field = $row[$field];
4170 $GLOBALS['log']->debug("process_full_list: $bean->object_name({$row['id']}): ".$field." = ".$bean->$field);
4179 $bean->processed_dates_times = array();
4180 $bean->check_date_relationships_load();
4182 $bean->fill_in_additional_list_fields();
4183 $bean->call_custom_logic("process_record");
4184 $bean->fetched_row = $row;
4189 if (isset($list)) return $list;
4194 * Tracks the viewing of a detail record.
4195 * This leverages get_summary_text() which is object specific.
4197 * Internal function, do not override.
4198 * @param string $user_id - String value of the user that is viewing the record.
4199 * @param string $current_module - String value of the module being processed.
4200 * @param string $current_view - String value of the current view
4202 function track_view($user_id, $current_module, $current_view='')
4204 $trackerManager = TrackerManager::getInstance();
4205 if($monitor = $trackerManager->getMonitor('tracker')){
4206 $monitor->setValue('date_modified', $GLOBALS['timedate']->nowDb());
4207 $monitor->setValue('user_id', $user_id);
4208 $monitor->setValue('module_name', $current_module);
4209 $monitor->setValue('action', $current_view);
4210 $monitor->setValue('item_id', $this->id);
4211 $monitor->setValue('item_summary', $this->get_summary_text());
4212 $monitor->setValue('visible', $this->tracker_visibility);
4213 $trackerManager->saveMonitor($monitor);
4218 * Returns the summary text that should show up in the recent history list for this object.
4222 public function get_summary_text()
4224 return "Base Implementation. Should be overridden.";
4228 * This is designed to be overridden and add specific fields to each record.
4229 * This allows the generic query to fill in the major fields, and then targeted
4230 * queries to get related fields and add them to the record. The contact's
4231 * account for instance. This method is only used for populating extra fields
4234 function fill_in_additional_list_fields(){
4235 if(!empty($this->field_defs['parent_name']) && empty($this->parent_name)){
4236 $this->fill_in_additional_parent_fields();
4241 * This is designed to be overridden and add specific fields to each record.
4242 * This allows the generic query to fill in the major fields, and then targeted
4243 * queries to get related fields and add them to the record. The contact's
4244 * account for instance. This method is only used for populating extra fields
4245 * in the detail form
4247 function fill_in_additional_detail_fields()
4249 if(!empty($this->field_defs['assigned_user_name']) && !empty($this->assigned_user_id)){
4251 $this->assigned_user_name = get_assigned_user_name($this->assigned_user_id);
4254 if(!empty($this->field_defs['created_by']) && !empty($this->created_by))
4255 $this->created_by_name = get_assigned_user_name($this->created_by);
4256 if(!empty($this->field_defs['modified_user_id']) && !empty($this->modified_user_id))
4257 $this->modified_by_name = get_assigned_user_name($this->modified_user_id);
4259 if(!empty($this->field_defs['parent_name'])){
4260 $this->fill_in_additional_parent_fields();
4265 * This is desgined to be overridden or called from extending bean. This method
4266 * will fill in any parent_name fields.
4268 function fill_in_additional_parent_fields() {
4270 if(!empty($this->parent_id) && !empty($this->last_parent_id) && $this->last_parent_id == $this->parent_id){
4273 $this->parent_name = '';
4275 if(!empty($this->parent_type)) {
4276 $this->last_parent_id = $this->parent_id;
4277 $this->getRelatedFields($this->parent_type, $this->parent_id, array('name'=>'parent_name', 'document_name' => 'parent_document_name', 'first_name'=>'parent_first_name', 'last_name'=>'parent_last_name'));
4278 if(!empty($this->parent_first_name) || !empty($this->parent_last_name) ){
4279 $this->parent_name = $GLOBALS['locale']->getLocaleFormattedName($this->parent_first_name, $this->parent_last_name);
4280 } else if(!empty($this->parent_document_name)){
4281 $this->parent_name = $this->parent_document_name;
4287 * Fill in a link field
4290 function fill_in_link_field( $linkFieldName , $def)
4292 $idField = $linkFieldName;
4293 //If the id_name provided really was an ID, don't try to load it as a link. Use the normal link
4294 if (!empty($this->field_defs[$linkFieldName]['type']) && $this->field_defs[$linkFieldName]['type'] == "id" && !empty($def['link']))
4296 $linkFieldName = $def['link'];
4298 if ($this->load_relationship($linkFieldName))
4300 $list=$this->$linkFieldName->get();
4301 $this->$idField = '' ; // match up with null value in $this->populateFromRow()
4303 $this->$idField = $list[0];
4308 * Fill in fields where type = relate
4310 function fill_in_relationship_fields(){
4311 global $fill_in_rel_depth;
4312 if(empty($fill_in_rel_depth) || $fill_in_rel_depth < 0)
4313 $fill_in_rel_depth = 0;
4315 if($fill_in_rel_depth > 1)
4318 $fill_in_rel_depth++;
4320 foreach($this->field_defs as $field)
4322 if(0 == strcmp($field['type'],'relate') && !empty($field['module']))
4324 $name = $field['name'];
4325 if(empty($this->$name))
4327 // set the value of this relate field in this bean ($this->$field['name']) to the value of the 'name' field in the related module for the record identified by the value of $this->$field['id_name']
4328 $related_module = $field['module'];
4329 $id_name = $field['id_name'];
4331 if (empty($this->$id_name))
4333 $this->fill_in_link_field($id_name, $field);
4335 if(!empty($this->$id_name) && ( $this->object_name != $related_module || ( $this->object_name == $related_module && $this->$id_name != $this->id ))){
4336 if(isset($GLOBALS['beanList'][ $related_module])){
4337 $class = $GLOBALS['beanList'][$related_module];
4339 if(!empty($this->$id_name) && file_exists($GLOBALS['beanFiles'][$class]) && isset($this->$name)){
4340 require_once($GLOBALS['beanFiles'][$class]);
4341 $mod = new $class();
4343 // disable row level security in order to be able
4344 // to retrieve related bean properties (bug #44928)
4346 $mod->retrieve($this->$id_name);
4348 if (!empty($field['rname'])) {
4349 $this->$name = $mod->$field['rname'];
4350 } else if (isset($mod->name)) {
4351 $this->$name = $mod->name;
4356 if(!empty($this->$id_name) && isset($this->$name))
4358 if(!isset($field['additionalFields']))
4359 $field['additionalFields'] = array();
4360 if(!empty($field['rname']))
4362 $field['additionalFields'][$field['rname']]= $name;
4366 $field['additionalFields']['name']= $name;
4368 $this->getRelatedFields($related_module, $this->$id_name, $field['additionalFields']);
4373 $fill_in_rel_depth--;
4377 * This is a helper function that is used to quickly created indexes when creating tables.
4379 function create_index($query)
4381 $GLOBALS['log']->info("create_index: $query");
4383 $result = $this->db->query($query, true, "Error creating index:");
4387 * This function should be overridden in each module. It marks an item as deleted.
4389 * If it is not overridden, then marking this type of item is not allowed
4391 function mark_deleted($id)
4393 global $current_user;
4394 $date_modified = $GLOBALS['timedate']->nowDb();
4395 if(isset($_SESSION['show_deleted']))
4397 $this->mark_undeleted($id);
4401 // call the custom business logic
4402 $custom_logic_arguments['id'] = $id;
4403 $this->call_custom_logic("before_delete", $custom_logic_arguments);
4405 $this->mark_relationships_deleted($id);
4406 if ( isset($this->field_defs['modified_user_id']) ) {
4407 if (!empty($current_user)) {
4408 $this->modified_user_id = $current_user->id;
4410 $this->modified_user_id = 1;
4412 $query = "UPDATE $this->table_name set deleted=1 , date_modified = '$date_modified', modified_user_id = '$this->modified_user_id' where id='$id'";
4414 $query = "UPDATE $this->table_name set deleted=1 , date_modified = '$date_modified' where id='$id'";
4416 $this->db->query($query, true,"Error marking record deleted: ");
4418 SugarRelationship::resaveRelatedBeans();
4420 // Take the item off the recently viewed lists
4421 $tracker = new Tracker();
4422 $tracker->makeInvisibleForAll($id);
4425 $this->deleteFiles();
4427 // call the custom business logic
4428 $this->call_custom_logic("after_delete", $custom_logic_arguments);
4433 * Restores data deleted by call to mark_deleted() function.
4435 * Internal function, do not override.
4437 function mark_undeleted($id)
4439 // call the custom business logic
4440 $custom_logic_arguments['id'] = $id;
4441 $this->call_custom_logic("before_restore", $custom_logic_arguments);
4443 $date_modified = $GLOBALS['timedate']->nowDb();
4444 $query = "UPDATE $this->table_name set deleted=0 , date_modified = '$date_modified' where id='$id'";
4445 $this->db->query($query, true,"Error marking record undeleted: ");
4447 $this->restoreFiles();
4449 // call the custom business logic
4450 $this->call_custom_logic("after_restore", $custom_logic_arguments);
4454 * This function deletes relationships to this object. It should be overridden
4455 * to handle the relationships of the specific object.
4456 * This function is called when the item itself is being deleted.
4458 * @param int $id id of the relationship to delete
4460 function mark_relationships_deleted($id)
4462 $this->delete_linked($id);
4466 * Returns path for files of bean or false on error
4468 * @return bool|string
4470 public function deleteFileDirectory()
4472 if (empty($this->id)) {
4475 return preg_replace('/^(..)(..)(..)/', '$1/$2/$3/', $this->id);
4479 * Moves file to deleted folder
4481 * @return bool success of movement
4483 protected function deleteFiles()
4488 if (!$this->haveFiles()) {
4491 $files = $this->getFiles();
4492 if (empty($files)) {
4496 $directory = $this->deleteFileDirectory();
4498 $isCreated = sugar_is_dir('upload://deleted/' . $directory);
4500 sugar_mkdir('upload://deleted/' . $directory, 0777, true);
4501 $isCreated = sugar_is_dir('upload://deleted/' . $directory);
4507 foreach ($files as $file) {
4508 if (file_exists('upload://' . $file)) {
4509 if (!sugar_rename('upload://' . $file, 'upload://deleted/' . $directory . '/' . $file)) {
4510 $GLOBALS['log']->error('Could not move file ' . $file . ' to deleted directory');
4516 * @var DBManager $db
4520 'bean_id' => $db->quoted($this->id),
4521 'module' => $db->quoted($this->module_name),
4522 'date_modified' => $db->convert($db->quoted(date('Y-m-d H:i:s')), 'datetime')
4524 $recordDB = $db->fetchOne("SELECT id FROM cron_remove_documents WHERE module={$record['module']} AND bean_id={$record['bean_id']}");
4525 if (!empty($recordDB)) {
4526 $record['id'] = $db->quoted($recordDB['id']);
4528 if (empty($record['id'])) {
4529 $record['id'] = $db->quoted(create_guid());
4530 $db->query('INSERT INTO cron_remove_documents (' . implode(', ', array_keys($record)) . ') VALUES(' . implode(', ', $record) . ')');
4532 $db->query("UPDATE cron_remove_documents SET date_modified={$record['date_modified']} WHERE id={$record['id']}");
4539 * Restores files from deleted folder
4541 * @return bool success of operation
4543 protected function restoreFiles()
4548 if (!$this->haveFiles()) {
4551 $files = $this->getFiles();
4552 if (empty($files)) {
4556 $directory = $this->deleteFileDirectory();
4558 foreach ($files as $file) {
4559 if (sugar_is_file('upload://deleted/' . $directory . '/' . $file)) {
4560 if (!sugar_rename('upload://deleted/' . $directory . '/' . $file, 'upload://' . $file)) {
4561 $GLOBALS['log']->error('Could not move file ' . $directory . '/' . $file . ' from deleted directory');
4567 * @var DBManager $db
4570 $db->query('DELETE FROM cron_remove_documents WHERE bean_id=' . $db->quoted($this->id));
4576 * Method returns true if bean has files
4580 public function haveFiles()
4583 if ($this->bean_implements('FILE')) {
4585 } elseif ($this instanceof File) {
4587 } elseif (!empty(self::$fileFields[$this->module_name])) {
4589 } elseif (!empty($this->field_defs)) {
4590 foreach ($this->field_defs as $fieldDef) {
4591 if ($fieldDef['type'] != 'image') {
4602 * Method returns array of names of files for current bean
4606 public function getFiles() {
4608 foreach ($this->getFilesFields() as $field) {
4609 if (!empty($this->$field)) {
4610 $files[] = $this->$field;
4617 * Method returns array of name of fields which contain names of files
4619 * @param bool $resetCache do not use cache
4622 public function getFilesFields($resetCache = false)
4624 if (isset(self::$fileFields[$this->module_name]) && $resetCache == false) {
4625 return self::$fileFields[$this->module_name];
4628 self::$fileFields = array();
4629 if ($this->bean_implements('FILE') || $this instanceof File) {
4630 self::$fileFields[$this->module_name][] = 'id';
4632 foreach ($this->field_defs as $fieldName => $fieldDef) {
4633 if ($fieldDef['type'] != 'image') {
4636 self::$fileFields[$this->module_name][] = $fieldName;
4639 return self::$fileFields[$this->module_name];
4643 * This function is used to execute the query and create an array template objects
4644 * from the resulting ids from the query.
4645 * It is currently used for building sub-panel arrays.
4647 * @param string $query - the query that should be executed to build the list
4648 * @param object $template - The object that should be used to copy the records.
4649 * @param int $row_offset Optional, default 0
4650 * @param int $limit Optional, default -1
4653 function build_related_list($query, &$template, $row_offset = 0, $limit = -1)
4655 $GLOBALS['log']->debug("Finding linked records $this->object_name: ".$query);
4656 $db = DBManagerFactory::getInstance('listviews');
4658 if(!empty($row_offset) && $row_offset != 0 && !empty($limit) && $limit != -1)
4660 $result = $db->limitQuery($query, $row_offset, $limit,true,"Error retrieving $template->object_name list: ");
4664 $result = $db->query($query, true);
4668 $isFirstTime = true;
4669 $class = get_class($template);
4670 while($row = $this->db->fetchByAssoc($result))
4674 $template = new $class();
4676 $isFirstTime = false;
4677 $record = $template->retrieve($row['id']);
4681 // this copies the object into the array
4682 $list[] = $template;
4689 * This function is used to execute the query and create an array template objects
4690 * from the resulting ids from the query.
4691 * It is currently used for building sub-panel arrays. It supports an additional
4692 * where clause that is executed as a filter on the results
4694 * @param string $query - the query that should be executed to build the list
4695 * @param object $template - The object that should be used to copy the records.
4697 function build_related_list_where($query, &$template, $where='', $in='', $order_by, $limit='', $row_offset = 0)
4699 $db = DBManagerFactory::getInstance('listviews');
4700 // No need to do an additional query
4701 $GLOBALS['log']->debug("Finding linked records $this->object_name: ".$query);
4702 if(empty($in) && !empty($query))
4704 $idList = $this->build_related_in($query);
4705 $in = $idList['in'];
4707 // MFH - Added Support For Custom Fields in Searches
4708 $custom_join = $this->getCustomJoin();
4710 $query = "SELECT id ";
4712 $query .= $custom_join['select'];
4713 $query .= " FROM $this->table_name ";
4715 $query .= $custom_join['join'];
4717 $query .= " WHERE deleted=0 AND id IN $in";
4720 $query .= " AND $where";
4724 if(!empty($order_by))
4726 $query .= "ORDER BY $order_by";
4730 $result = $db->limitQuery($query, $row_offset, $limit,true,"Error retrieving $this->object_name list: ");
4734 $result = $db->query($query, true);
4738 $isFirstTime = true;
4739 $class = get_class($template);
4741 $disable_security_flag = ($template->disable_row_level_security) ? true : false;
4742 while($row = $db->fetchByAssoc($result))
4746 $template = new $class();
4747 $template->disable_row_level_security = $disable_security_flag;
4749 $isFirstTime = false;
4750 $record = $template->retrieve($row['id']);
4753 // this copies the object into the array
4754 $list[] = $template;
4762 * Constructs an comma separated list of ids from passed query results.
4764 * @param string @query query to be executed.
4767 function build_related_in($query)
4770 $result = $this->db->query($query, true);
4772 while($row = $this->db->fetchByAssoc($result))
4774 $idList[] = $row['id'];
4777 $ids = "('" . $row['id'] . "'";
4781 $ids .= ",'" . $row['id'] . "'";
4791 return array('list'=>$idList, 'in'=>$ids);
4795 * Optionally copies values from fetched row into the bean.
4797 * Internal function, do not override.
4799 * @param string $query - the query that should be executed to build the list
4800 * @param object $template - The object that should be used to copy the records
4801 * @param array $field_list List of fields.
4804 function build_related_list2($query, &$template, &$field_list)
4806 $GLOBALS['log']->debug("Finding linked values $this->object_name: ".$query);
4808 $result = $this->db->query($query, true);
4811 $isFirstTime = true;
4812 $class = get_class($template);
4813 while($row = $this->db->fetchByAssoc($result))
4815 // Create a blank copy
4819 $copy = new $class();
4821 $isFirstTime = false;
4822 foreach($field_list as $field)
4824 // Copy the relevant fields
4825 $copy->$field = $row[$field];
4829 // this copies the object into the array
4837 * Let implementing classes to fill in row specific columns of a list view form
4840 function list_view_parse_additional_sections(&$list_form)
4844 * Assigns all of the values into the template for the list view
4846 function get_list_view_array()
4848 static $cache = array();
4849 // cn: bug 12270 - sensitive fields being passed arbitrarily in listViews
4850 $sensitiveFields = array('user_hash' => '');
4852 $return_array = Array();
4853 global $app_list_strings, $mod_strings;
4854 foreach($this->field_defs as $field=>$value){
4856 if(isset($this->$field)){
4858 // cn: bug 12270 - sensitive fields being passed arbitrarily in listViews
4859 if(isset($sensitiveFields[$field]))
4861 if(!isset($cache[$field]))
4862 $cache[$field] = strtoupper($field);
4864 //Fields hidden by Dependent Fields
4865 if (isset($value['hidden']) && $value['hidden'] === true) {
4866 $return_array[$cache[$field]] = "";
4869 //cn: if $field is a _dom, detect and return VALUE not KEY
4870 //cl: empty function check for meta-data enum types that have values loaded from a function
4871 else if (((!empty($value['type']) && ($value['type'] == 'enum' || $value['type'] == 'radioenum') )) && empty($value['function'])){
4872 if(!empty($value['options']) && !empty($app_list_strings[$value['options']][$this->$field])){
4873 $return_array[$cache[$field]] = $app_list_strings[$value['options']][$this->$field];
4875 //nsingh- bug 21672. some modules such as manufacturers, Releases do not have a listing for select fields in the $app_list_strings. Must also check $mod_strings to localize.
4876 elseif(!empty($value['options']) && !empty($mod_strings[$value['options']][$this->$field]))
4878 $return_array[$cache[$field]] = $mod_strings[$value['options']][$this->$field];
4881 $return_array[$cache[$field]] = $this->$field;
4884 // tjy: no need to do this str_replace as the changes in 29994 for ListViewGeneric.tpl for translation handle this now
4885 // }elseif(!empty($value['type']) && $value['type'] == 'multienum'&& empty($value['function'])){
4886 // $return_array[strtoupper($field)] = str_replace('^,^', ', ', $this->$field );
4887 }elseif(!empty($value['custom_module']) && $value['type'] != 'currency'){
4888 // $this->format_field($value);
4889 $return_array[$cache[$field]] = $this->$field;
4891 $return_array[$cache[$field]] = $this->$field;
4893 // handle "Assigned User Name"
4894 if($field == 'assigned_user_name'){
4895 $return_array['ASSIGNED_USER_NAME'] = get_assigned_user_name($this->assigned_user_id);
4899 return $return_array;
4902 * Override this function to set values in the array used to render list view data.
4905 function get_list_view_data()
4907 return $this->get_list_view_array();
4911 * Construct where clause from a list of name-value pairs.
4912 * @param array $fields_array Name/value pairs for column checks
4913 * @param boolean $deleted Optional, default true, if set to false deleted filter will not be added.
4914 * @return string The WHERE clause
4916 function get_where($fields_array, $deleted=true)
4919 foreach ($fields_array as $name=>$value)
4921 if (!empty($where_clause)) {
4922 $where_clause .= " AND ";
4924 $name = $this->db->getValidDBName($name);
4926 $where_clause .= "$name = ".$this->db->quoted($value,false);
4928 if(!empty($where_clause)) {
4930 return "WHERE $where_clause AND deleted=0";
4932 return "WHERE $where_clause";
4941 * Constructs a select query and fetch 1 row using this query, and then process the row
4943 * Internal function, do not override.
4944 * @param array @fields_array array of name value pairs used to construct query.
4945 * @param boolean $encode Optional, default true, encode fetched data.
4946 * @param boolean $deleted Optional, default true, if set to false deleted filter will not be added.
4947 * @return object Instance of this bean with fetched data.
4949 function retrieve_by_string_fields($fields_array, $encode=true, $deleted=true)
4951 $where_clause = $this->get_where($fields_array, $deleted);
4952 $custom_join = $this->getCustomJoin();
4953 $query = "SELECT $this->table_name.*". $custom_join['select']. " FROM $this->table_name " . $custom_join['join'];
4954 $query .= " $where_clause";
4955 $GLOBALS['log']->debug("Retrieve $this->object_name: ".$query);
4956 //requireSingleResult has been deprecated.
4957 //$result = $this->db->requireSingleResult($query, true, "Retrieving record $where_clause:");
4958 $result = $this->db->limitQuery($query,0,1,true, "Retrieving record $where_clause:");
4965 $row = $this->db->fetchByAssoc($result, $encode);
4970 // Removed getRowCount-if-clause earlier and insert duplicates_found here as it seems that we have found something
4971 // if we didn't return null in the previous clause.
4972 $this->duplicates_found = true;
4973 $row = $this->convertRow($row);
4974 $this->fetched_row = $row;
4975 $this->fromArray($row);
4976 $this->is_updated_dependent_fields = false;
4977 $this->fill_in_additional_detail_fields();
4982 * This method is called during an import before inserting a bean
4983 * Define an associative array called $special_fields
4984 * the keys are user defined, and don't directly map to the bean's fields
4985 * the value is the method name within that bean that will do extra
4986 * processing for that field. example: 'full_name'=>'get_names_from_full_name'
4989 function process_special_fields()
4991 foreach ($this->special_functions as $func_name)
4993 if ( method_exists($this,$func_name) )
4995 $this->$func_name();
5001 * Override this function to build a where clause based on the search criteria set into bean .
5004 function build_generic_where_clause($value)
5008 function getRelatedFields($module, $id, $fields, $return_array = false){
5009 if(empty($GLOBALS['beanList'][$module]))return '';
5010 $object = BeanFactory::getObjectName($module);
5012 VardefManager::loadVardef($module, $object);
5013 if(empty($GLOBALS['dictionary'][$object]['table']))return '';
5014 $table = $GLOBALS['dictionary'][$object]['table'];
5015 $query = 'SELECT id';
5016 foreach($fields as $field=>$alias){
5017 if(!empty($GLOBALS['dictionary'][$object]['fields'][$field]['db_concat_fields'])){
5018 $query .= ' ,' .$this->db->concat($table, $GLOBALS['dictionary'][$object]['fields'][$field]['db_concat_fields']) . ' as ' . $alias ;
5019 }else if(!empty($GLOBALS['dictionary'][$object]['fields'][$field]) &&
5020 (empty($GLOBALS['dictionary'][$object]['fields'][$field]['source']) ||
5021 $GLOBALS['dictionary'][$object]['fields'][$field]['source'] != "non-db"))
5023 $query .= ' ,' .$table . '.' . $field . ' as ' . $alias;
5025 if(!$return_array)$this->$alias = '';
5027 if($query == 'SELECT id' || empty($id)){
5032 if(isset($GLOBALS['dictionary'][$object]['fields']['assigned_user_id']))
5034 $query .= " , ". $table . ".assigned_user_id owner";
5037 else if(isset($GLOBALS['dictionary'][$object]['fields']['created_by']))
5039 $query .= " , ". $table . ".created_by owner";
5042 $query .= ' FROM ' . $table . ' WHERE deleted=0 AND id=';
5043 $result = $GLOBALS['db']->query($query . "'$id'" );
5044 $row = $GLOBALS['db']->fetchByAssoc($result);
5048 $owner = (empty($row['owner']))?'':$row['owner'];
5049 foreach($fields as $alias){
5050 $this->$alias = (!empty($row[$alias]))? $row[$alias]: '';
5051 $alias = $alias .'_owner';
5052 $this->$alias = $owner;
5053 $a_mod = $alias .'_mod';
5054 $this->$a_mod = $module;
5061 function &parse_additional_headers(&$list_form, $xTemplateSection)
5066 function assign_display_fields($currentModule)
5069 foreach($this->column_fields as $field)
5071 if(isset($this->field_name_map[$field]) && empty($this->$field))
5073 if($this->field_name_map[$field]['type'] != 'date' && $this->field_name_map[$field]['type'] != 'enum')
5074 $this->$field = $field;
5075 if($this->field_name_map[$field]['type'] == 'date')
5077 $this->$field = $timedate->to_display_date('1980-07-09');
5079 if($this->field_name_map[$field]['type'] == 'enum')
5081 $dom = $this->field_name_map[$field]['options'];
5082 global $current_language, $app_list_strings;
5083 $mod_strings = return_module_language($current_language, $currentModule);
5085 if(isset($mod_strings[$dom]))
5087 $options = $mod_strings[$dom];
5088 foreach($options as $key=>$value)
5090 if(!empty($key) && empty($this->$field ))
5092 $this->$field = $key;
5096 if(isset($app_list_strings[$dom]))
5098 $options = $app_list_strings[$dom];
5099 foreach($options as $key=>$value)
5101 if(!empty($key) && empty($this->$field ))
5103 $this->$field = $key;
5115 * RELATIONSHIP HANDLING
5118 function set_relationship($table, $relate_values, $check_duplicates = true,$do_update=false,$data_values=null)
5122 // make sure there is a date modified
5123 $date_modified = $this->db->convert("'".$GLOBALS['timedate']->nowDb()."'", 'datetime');
5126 if($check_duplicates)
5128 $query = "SELECT * FROM $table ";
5129 $where = "WHERE deleted = '0' ";
5130 foreach($relate_values as $name=>$value)
5132 $where .= " AND $name = '$value' ";
5135 $result = $this->db->query($query, false, "Looking For Duplicate Relationship:" . $query);
5136 $row=$this->db->fetchByAssoc($result);
5139 if(!$check_duplicates || empty($row) )
5141 unset($relate_values['id']);
5142 if ( isset($data_values))
5144 $relate_values = array_merge($relate_values,$data_values);
5146 $query = "INSERT INTO $table (id, ". implode(',', array_keys($relate_values)) . ", date_modified) VALUES ('" . create_guid() . "', " . "'" . implode("', '", $relate_values) . "', ".$date_modified.")" ;
5148 $this->db->query($query, false, "Creating Relationship:" . $query);
5150 else if ($do_update)
5153 foreach($data_values as $key=>$value)
5155 array_push($conds,$key."='".$this->db->quote($value)."'");
5157 $query = "UPDATE $table SET ". implode(',', $conds).",date_modified=".$date_modified." ".$where;
5158 $this->db->query($query, false, "Updating Relationship:" . $query);
5162 function retrieve_relationships($table, $values, $select_id)
5164 $query = "SELECT $select_id FROM $table WHERE deleted = 0 ";
5165 foreach($values as $name=>$value)
5167 $query .= " AND $name = '$value' ";
5169 $query .= " ORDER BY $select_id ";
5170 $result = $this->db->query($query, false, "Retrieving Relationship:" . $query);
5172 while($row = $this->db->fetchByAssoc($result))
5179 // TODO: this function needs adjustment
5180 function loadLayoutDefs()
5182 global $layout_defs;
5183 if(empty( $this->layout_def) && file_exists('modules/'. $this->module_dir . '/layout_defs.php'))
5185 include_once('modules/'. $this->module_dir . '/layout_defs.php');
5186 if(file_exists('custom/modules/'. $this->module_dir . '/Ext/Layoutdefs/layoutdefs.ext.php'))
5188 include_once('custom/modules/'. $this->module_dir . '/Ext/Layoutdefs/layoutdefs.ext.php');
5190 if ( empty( $layout_defs[get_class($this)]))
5192 echo "\$layout_defs[" . get_class($this) . "]; does not exist";
5195 $this->layout_def = $layout_defs[get_class($this)];
5200 * Trigger custom logic for this module that is defined for the provided hook
5201 * The custom logic file is located under custom/modules/[CURRENT_MODULE]/logic_hooks.php.
5202 * That file should define the $hook_version that should be used.
5203 * It should also define the $hook_array. The $hook_array will be a two dimensional array
5204 * the first dimension is the name of the event, the second dimension is the information needed
5205 * to fire the hook. Each entry in the top level array should be defined on a single line to make it
5206 * easier to automatically replace this file. There should be no contents of this file that are not replacable.
5208 * $hook_array['before_save'][] = Array(1, testtype, 'custom/modules/Leads/test12.php', 'TestClass', 'lead_before_save_1');
5209 * This sample line creates a before_save hook. The hooks are procesed in the order in which they
5210 * are added to the array. The second dimension is an array of:
5211 * processing index (for sorting before exporting the array)
5214 * php file to include
5215 * php class the method is in
5216 * php method to call
5218 * The method signature for version 1 hooks is:
5219 * function NAME(&$bean, $event, $arguments)
5220 * $bean - $this bean passed in by reference.
5221 * $event - The string for the current event (i.e. before_save)
5222 * $arguments - An array of arguments that are specific to the event.
5224 function call_custom_logic($event, $arguments = null)
5226 if(!isset($this->processed) || $this->processed == false){
5227 //add some logic to ensure we do not get into an infinite loop
5228 if(!empty($this->logicHookDepth[$event])) {
5229 if($this->logicHookDepth[$event] > $this->max_logic_depth)
5232 $this->logicHookDepth[$event] = 0;
5234 //we have to put the increment operator here
5235 //otherwise we may never increase the depth for that event in the case
5236 //where one event will trigger another as in the case of before_save and after_save
5237 //Also keeping the depth per event allow any number of hooks to be called on the bean
5238 //and we only will return if one event gets caught in a loop. We do not increment globally
5239 //for each event called.
5240 $this->logicHookDepth[$event]++;
5242 //method defined in 'include/utils/LogicHook.php'
5244 $logicHook = new LogicHook();
5245 $logicHook->setBean($this);
5246 $logicHook->call_custom_logic($this->module_dir, $event, $arguments);
5247 $this->logicHookDepth[$event]--;
5252 /* When creating a custom field of type Dropdown, it creates an enum row in the DB.
5253 A typical get_list_view_array() result will have the *KEY* value from that drop-down.
5254 Since custom _dom objects are flat-files included in the $app_list_strings variable,
5255 We need to generate a key-key pair to get the true value like so:
5256 ([module]_cstm->fields_meta_data->$app_list_strings->*VALUE*)*/
5257 function getRealKeyFromCustomFieldAssignedKey($name)
5259 if ($this->custom_fields->avail_fields[$name]['ext1'])
5263 elseif ($this->custom_fields->avail_fields[$name]['ext2'])
5267 elseif ($this->custom_fields->avail_fields[$name]['ext3'])
5273 $GLOBALS['log']->fatal("SUGARBEAN: cannot find Real Key for custom field of type dropdown - cannot return Value.");
5278 return $this->custom_fields->avail_fields[$name][$realKey];
5282 function bean_implements($interface)
5287 * Check whether the user has access to a particular view for the current bean/module
5288 * @param $view string required, the view to determine access for i.e. DetailView, ListView...
5289 * @param $is_owner bool optional, this is part of the ACL check if the current user is an owner they will receive different access
5291 function ACLAccess($view,$is_owner='not_set')
5293 global $current_user;
5294 if($current_user->isAdminForModule($this->getACLCategory())) {
5298 if($is_owner == 'not_set')
5301 $is_owner = $this->isOwner($current_user->id);
5304 // If we don't implement ACLs, return true.
5305 if(!$this->bean_implements('ACL'))
5307 $view = strtolower($view);
5313 return ACLController::checkAccess($this->module_dir,'list', true);
5316 if( !$is_owner && $not_set && !empty($this->id)){
5317 $class = get_class($this);
5318 $temp = new $class();
5319 if(!empty($this->fetched_row) && !empty($this->fetched_row['id']) && !empty($this->fetched_row['assigned_user_id']) && !empty($this->fetched_row['created_by'])){
5320 $temp->populateFromRow($this->fetched_row);
5322 $temp->retrieve($this->id);
5324 $is_owner = $temp->isOwner($current_user->id);
5326 case 'popupeditview':
5328 return ACLController::checkAccess($this->module_dir,'edit', $is_owner, $this->acltype);
5332 return ACLController::checkAccess($this->module_dir,'view', $is_owner, $this->acltype);
5334 return ACLController::checkAccess($this->module_dir,'delete', $is_owner, $this->acltype);
5336 return ACLController::checkAccess($this->module_dir,'export', $is_owner, $this->acltype);
5338 return ACLController::checkAccess($this->module_dir,'import', true, $this->acltype);
5340 //if it is not one of the above views then it should be implemented on the page level
5349 function getOwnerField($returnFieldName = false)
5351 if (isset($this->field_defs['assigned_user_id']))
5353 return $returnFieldName? 'assigned_user_id': $this->assigned_user_id;
5356 if (isset($this->field_defs['created_by']))
5358 return $returnFieldName? 'created_by': $this->created_by;
5365 * Returns true of false if the user_id passed is the owner
5367 * @param GUID $user_id
5370 function isOwner($user_id)
5372 //if we don't have an id we must be the owner as we are creating it
5373 if(!isset($this->id))
5377 //if there is an assigned_user that is the owner
5378 if (!empty($this->fetched_row['assigned_user_id'])) {
5379 if ($this->fetched_row['assigned_user_id'] == $user_id) {
5383 } elseif (isset($this->assigned_user_id)) {
5384 if($this->assigned_user_id == $user_id) return true;
5389 //other wise if there is a created_by that is the owner
5390 if(isset($this->created_by) && $this->created_by == $user_id)
5398 * Gets there where statement for checking if a user is an owner
5400 * @param GUID $user_id
5403 function getOwnerWhere($user_id)
5405 if(isset($this->field_defs['assigned_user_id']))
5407 return " $this->table_name.assigned_user_id ='$user_id' ";
5409 if(isset($this->field_defs['created_by']))
5411 return " $this->table_name.created_by ='$user_id' ";
5418 * Used in order to manage ListView links and if they should
5419 * links or not based on the ACL permissions of the user
5421 * @return ARRAY of STRINGS
5423 function listviewACLHelper()
5425 $array_assign = array();
5426 if($this->ACLAccess('DetailView'))
5428 $array_assign['MAIN'] = 'a';
5432 $array_assign['MAIN'] = 'span';
5434 return $array_assign;
5438 * returns this bean as an array
5440 * @return array of fields with id, name, access and category
5442 function toArray($dbOnly = false, $stringOnly = false, $upperKeys=false)
5444 static $cache = array();
5447 foreach($this->field_defs as $field=>$data)
5449 if( !$dbOnly || !isset($data['source']) || $data['source'] == 'db')
5450 if(!$stringOnly || is_string($this->$field))
5453 if(!isset($cache[$field])){
5454 $cache[$field] = strtoupper($field);
5456 $arr[$cache[$field]] = $this->$field;
5460 if(isset($this->$field)){
5461 $arr[$field] = $this->$field;
5471 * Converts an array into an acl mapping name value pairs into files
5475 function fromArray($arr)
5477 foreach($arr as $name=>$value)
5479 $this->$name = $value;
5484 * Convert row data from DB format to internal format
5485 * Mostly useful for dates/times
5487 * @return array $row
5489 public function convertRow($row)
5491 foreach($this->field_defs as $name => $fieldDef)
5493 // skip empty fields and non-db fields
5494 if (isset($name) && !empty($row[$name])) {
5495 $row[$name] = $this->convertField($row[$name], $fieldDef);
5502 * Converts the field value based on the provided fieldDef
5503 * @param $fieldvalue
5507 public function convertField($fieldvalue, $fieldDef)
5509 if (!empty($fieldvalue)) {
5510 if (!(isset($fieldDef['source']) &&
5511 !in_array($fieldDef['source'], array('db', 'custom_fields', 'relate'))
5512 && !isset($fieldDef['dbType']))
5514 // fromConvert other fields
5515 $fieldvalue = $this->db->fromConvert($fieldvalue, $this->db->getFieldType($fieldDef));
5522 * Loads a row of data into instance of a bean. The data is passed as an array to this function
5524 * @param array $arr row of data fetched from the database.
5527 * Internal function do not override.
5529 function loadFromRow($arr)
5531 $this->populateFromRow($arr);
5532 $this->processed_dates_times = array();
5533 $this->check_date_relationships_load();
5535 $this->fill_in_additional_list_fields();
5537 if($this->hasCustomFields())$this->custom_fields->fill_relationships();
5538 $this->call_custom_logic("process_record");
5541 function hasCustomFields()
5543 return !empty($GLOBALS['dictionary'][$this->object_name]['custom_fields']);
5547 * Ensure that fields within order by clauses are properly qualified with
5548 * their tablename. This qualification is a requirement for sql server support.
5550 * @param string $order_by original order by from the query
5551 * @param string $qualify prefix for columns in the order by list.
5554 * Internal function do not override.
5556 function create_qualified_order_by( $order_by, $qualify)
5557 { // if the column is empty, but the sort order is defined, the value will throw an error, so do not proceed if no order by is given
5558 if (empty($order_by))
5562 $order_by_clause = " ORDER BY ";
5563 $tmp = explode(",", $order_by);
5565 foreach ( $tmp as $stmp)
5567 $stmp = (substr_count($stmp, ".") > 0?trim($stmp):"$qualify." . trim($stmp));
5568 $order_by_clause .= $comma . $stmp;
5571 return $order_by_clause;
5575 * Combined the contents of street field 2 thru 4 into the main field
5577 * @param string $street_field
5580 function add_address_streets(
5584 $street_field_2 = $street_field.'_2';
5585 $street_field_3 = $street_field.'_3';
5586 $street_field_4 = $street_field.'_4';
5587 if ( isset($this->$street_field_2)) {
5588 $this->$street_field .= "\n". $this->$street_field_2;
5589 unset($this->$street_field_2);
5591 if ( isset($this->$street_field_3)) {
5592 $this->$street_field .= "\n". $this->$street_field_3;
5593 unset($this->$street_field_3);
5595 if ( isset($this->$street_field_4)) {
5596 $this->$street_field .= "\n". $this->$street_field_4;
5597 unset($this->$street_field_4);
5599 if ( isset($this->$street_field)) {
5600 $this->$street_field = trim($this->$street_field, "\n");
5604 protected function getEncryptKey()
5606 if(empty(self::$field_key)) {
5607 self::$field_key = blowfishGetKey('encrypt_field');
5609 return self::$field_key;
5613 * Encrpyt and base64 encode an 'encrypt' field type in the bean using Blowfish. The default system key is stored in cache/Blowfish/{keytype}
5614 * @param STRING value -plain text value of the bean field.
5617 function encrpyt_before_save($value)
5619 require_once("include/utils/encryption_utils.php");
5620 return blowfishEncode($this->getEncryptKey(), $value);
5624 * Decode and decrypt a base 64 encoded string with field type 'encrypt' in this bean using Blowfish.
5625 * @param STRING value - an encrypted and base 64 encoded string.
5628 function decrypt_after_retrieve($value)
5630 if(empty($value)) return $value; // no need to decrypt empty
5631 require_once("include/utils/encryption_utils.php");
5632 return blowfishDecode($this->getEncryptKey(), $value);
5636 * Moved from save() method, functionality is the same, but this is intended to handle
5637 * Optimistic locking functionality.
5639 private function _checkOptimisticLocking($action, $isUpdate){
5640 if($this->optimistic_lock && !isset($_SESSION['o_lock_fs'])){
5641 if(isset($_SESSION['o_lock_id']) && $_SESSION['o_lock_id'] == $this->id && $_SESSION['o_lock_on'] == $this->object_name)
5643 if($action == 'Save' && $isUpdate && isset($this->modified_user_id) && $this->has_been_modified_since($_SESSION['o_lock_dm'], $this->modified_user_id))
5645 $_SESSION['o_lock_class'] = get_class($this);
5646 $_SESSION['o_lock_module'] = $this->module_dir;
5647 $_SESSION['o_lock_object'] = $this->toArray();
5648 $saveform = "<form name='save' id='save' method='POST'>";
5649 foreach($_POST as $key=>$arg)
5651 $saveform .= "<input type='hidden' name='". addslashes($key) ."' value='". addslashes($arg) ."'>";
5653 $saveform .= "</form><script>document.getElementById('save').submit();</script>";
5654 $_SESSION['o_lock_save'] = $saveform;
5655 header('Location: index.php?module=OptimisticLock&action=LockResolve');
5660 unset ($_SESSION['o_lock_object']);
5661 unset ($_SESSION['o_lock_id']);
5662 unset ($_SESSION['o_lock_dm']);
5668 if(isset($_SESSION['o_lock_object'])) { unset ($_SESSION['o_lock_object']); }
5669 if(isset($_SESSION['o_lock_id'])) { unset ($_SESSION['o_lock_id']); }
5670 if(isset($_SESSION['o_lock_dm'])) { unset ($_SESSION['o_lock_dm']); }
5671 if(isset($_SESSION['o_lock_fs'])) { unset ($_SESSION['o_lock_fs']); }
5672 if(isset($_SESSION['o_lock_save'])) { unset ($_SESSION['o_lock_save']); }
5677 * Send assignment notifications and invites for meetings and calls
5679 private function _sendNotifications($check_notify){
5680 if($check_notify || (isset($this->notify_inworkflow) && $this->notify_inworkflow == true) // cn: bug 5795 - no invites sent to Contacts, and also bug 25995, in workflow, it will set the notify_on_save=true.
5681 && !$this->isOwner($this->created_by) ) // cn: bug 42727 no need to send email to owner (within workflow)
5683 $admin = new Administration();
5684 $admin->retrieveSettings();
5685 $sendNotifications = false;
5687 if ($admin->settings['notify_on'])
5689 $GLOBALS['log']->info("Notifications: user assignment has changed, checking if user receives notifications");
5690 $sendNotifications = true;
5692 elseif(isset($_REQUEST['send_invites']) && $_REQUEST['send_invites'] == 1)
5694 // cn: bug 5795 Send Invites failing for Contacts
5695 $sendNotifications = true;
5699 $GLOBALS['log']->info("Notifications: not sending e-mail, notify_on is set to OFF");
5703 if($sendNotifications == true)
5705 $notify_list = $this->get_notification_recipients();
5706 foreach ($notify_list as $notify_user)
5708 $this->send_assignment_notifications($notify_user, $admin);
5716 * Called from ImportFieldSanitize::relate(), when creating a new bean in a related module. Will
5717 * copies fields over from the current bean into the related. Designed to be overriden in child classes.
5719 * @param SugarBean $newbean newly created related bean
5721 public function populateRelatedBean(
5728 * Called during the import process before a bean save, to handle any needed pre-save logic when
5729 * importing a record
5731 public function beforeImportSave()
5736 * Called during the import process after a bean save, to handle any needed post-save logic when
5737 * importing a record
5739 public function afterImportSave()
5744 * This function is designed to cache references to field arrays that were previously stored in the
5745 * bean files and have since been moved to separate files. Was previously in include/CacheHandler.php
5748 * @param $module_dir string the module directory
5749 * @param $module string the name of the module
5750 * @param $key string the type of field array we are referencing, i.e. list_fields, column_fields, required_fields
5752 private function _loadCachedArray(
5758 static $moduleDefs = array();
5760 $fileName = 'field_arrays.php';
5762 $cache_key = "load_cached_array.$module_dir.$module.$key";
5763 $result = sugar_cache_retrieve($cache_key);
5766 // Use SugarCache::EXTERNAL_CACHE_NULL_VALUE to store null values in the cache.
5767 if($result == SugarCache::EXTERNAL_CACHE_NULL_VALUE)
5775 if(file_exists('modules/'.$module_dir.'/'.$fileName))
5777 // If the data was not loaded, try loading again....
5778 if(!isset($moduleDefs[$module]))
5780 include('modules/'.$module_dir.'/'.$fileName);
5781 $moduleDefs[$module] = $fields_array;
5783 // Now that we have tried loading, make sure it was loaded
5784 if(empty($moduleDefs[$module]) || empty($moduleDefs[$module][$module][$key]))
5786 // It was not loaded.... Fail. Cache null to prevent future repeats of this calculation
5787 sugar_cache_put($cache_key, SugarCache::EXTERNAL_CACHE_NULL_VALUE);
5791 // It has been loaded, cache the result.
5792 sugar_cache_put($cache_key, $moduleDefs[$module][$module][$key]);
5793 return $moduleDefs[$module][$module][$key];
5796 // It was not loaded.... Fail. Cache null to prevent future repeats of this calculation
5797 sugar_cache_put($cache_key, SugarCache::EXTERNAL_CACHE_NULL_VALUE);
5802 * Returns the ACL category for this module; defaults to the SugarBean::$acl_category if defined
5803 * otherwise it is SugarBean::$module_dir
5807 public function getACLCategory()
5809 return !empty($this->acl_category)?$this->acl_category:$this->module_dir;
5813 * Returns the query used for the export functionality for a module. Override this method if you wish
5814 * to have a custom query to pull this data together instead
5816 * @param string $order_by
5817 * @param string $where
5818 * @return string SQL query
5820 public function create_export_query($order_by, $where)
5822 return $this->create_new_list_query($order_by, $where, array(), array(), 0, '', false, $this, true, true);
5826 * Determine whether the given field is a relate field
5828 * @param string $field Field name
5831 protected function is_relate_field($field)
5833 if (!isset($this->field_defs[$field]))
5838 $field_def = $this->field_defs[$field];
5840 return isset($field_def['type'])
5841 && $field_def['type'] == 'relate'
5842 && isset($field_def['link']);
5846 * Proxy method for DynamicField::getJOIN
5847 * @param bool $expandedList
5848 * @param bool $includeRelates
5849 * @param string|bool $where
5852 public function getCustomJoin($expandedList = false, $includeRelates = false, &$where = false)
5858 if(isset($this->custom_fields))
5860 $result = $this->custom_fields->getJOIN($expandedList, $includeRelates, $where);
5866 * Populates currency fields in case of currency is default and it's
5867 * attributes are not retrieved from database (bugs ##44206, 52438)
5869 protected function populateCurrencyFields()
5871 if (property_exists($this, 'currency_id') && $this->currency_id == -99) {
5872 // manually retrieve default currency object as long as it's
5873 // not stored in database and thus cannot be joined in query
5874 $currency = BeanFactory::getBean('Currencies', $this->currency_id);
5877 // walk through all currency-related fields
5878 foreach ($this->field_defs as $this_field) {
5879 if (isset($this_field['type']) && $this_field['type'] == 'relate'
5880 && isset($this_field['module']) && $this_field['module'] == 'Currencies'
5881 && isset($this_field['id_name']) && $this_field['id_name'] == 'currency_id') {
5882 // populate related properties manually
5883 $this_property = $this_field['name'];
5884 $currency_property = $this_field['rname'];
5885 $this->$this_property = $currency->$currency_property;
5893 * Checks if Bean has email defs
5897 public function hasEmails()
5899 if (!empty($this->field_defs['email_addresses']) && $this->field_defs['email_addresses']['type'] == 'link' &&
5900 !empty($this->field_defs['email_addresses_non_primary']) && $this->field_defs['email_addresses_non_primary']['type'] == 'email')