2 if (! defined ( 'sugarEntry' ) || ! sugarEntry)
3 die ( 'Not A Valid Entry Point' ) ;
5 /*********************************************************************************
6 * SugarCRM Community Edition is a customer relationship management program developed by
7 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU Affero General Public License version 3 as published by the
11 * Free Software Foundation with the addition of the following permission added
12 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
13 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
14 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
21 * You should have received a copy of the GNU Affero General Public License along with
22 * this program; if not, see http://www.gnu.org/licenses or write to the Free
23 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
27 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
29 * The interactive user interfaces in modified source and object code versions
30 * of this program must display Appropriate Legal Notices, as required under
31 * Section 5 of the GNU Affero General Public License version 3.
33 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
34 * these Appropriate Legal Notices must retain the display of the "Powered by
35 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
36 * technical reasons, the Appropriate Legal Notices must display the words
37 * "Powered by SugarCRM".
38 ********************************************************************************/
43 var $use_existing_labels = false; // this value is set to true by install_custom_fields() in ModuleInstaller.php; everything else expects it to be false
46 function DynamicField($module = '') {
47 $this->module = (! empty ( $module )) ? $module :( (isset($_REQUEST['module']) && ! empty($_REQUEST['module'])) ? $_REQUEST ['module'] : '');
48 $this->base_path = "custom/Extension/modules/{$this->module}/Ext/Vardefs";
51 function getModuleName()
53 return $this->module ;
57 * As DynamicField has a counterpart in MBModule, provide the MBModule function getPackagename() here also
59 function getPackageName()
64 function deleteCache(){
69 * This will add the bean as a reference in this object as well as building the custom field cache if it has not been built
70 * LOADS THE BEAN IF THE BEAN IS NOT BEING PASSED ALONG IN SETUP IT SHOULD BE SET PRIOR TO SETUP
72 * @param SUGARBEAN $bean
74 function setup($bean = null) {
78 if (isset ( $this->bean->module_dir )) {
79 $this->module = $this->bean->module_dir;
81 if(!isset($GLOBALS['dictionary'][$this->bean->object_name]['custom_fields'])){
82 $this->buildCache ( $this->module );
86 function setLabel( $language='en_us' , $key , $value )
88 $params [ "label_" . $key ] = $value;
89 require_once 'modules/ModuleBuilder/parsers/parser.label.php' ;
90 $parser = new ParserLabel ( $this->module ) ;
91 $parser->handleSave( $params , $language);
95 * Builds the cache for custom fields based on the vardefs
97 * @param STRING $module
100 function buildCache($module = false) {
101 //We can't build the cache while installing as the required database tables may not exist.
102 if (!empty($GLOBALS['installing']) && $GLOBALS['installing'] == true|| !$GLOBALS['db'])
104 if($module == '../data')return false;
106 static $results = array ( ) ;
109 if (! empty ( $module )) {
110 $where .= " custom_module='$module' AND ";
111 unset( $results[ $module ] ) ; // clear out any old results for the module as $results is declared static
115 $results = array ( ) ; // clear out results - if we remove a module we don't want to have its old vardefs hanging around
118 $GLOBALS['log']->debug('rebuilding cache for ' . $module);
119 $query = "SELECT * FROM fields_meta_data WHERE $where deleted = 0";
121 $result = $GLOBALS['db']->query ( $query );
122 require_once ('modules/DynamicFields/FieldCases.php');
124 // retrieve the field definition from the fields_meta_data table
125 // using 'encode'=false to fetchByAssoc to prevent any pre-formatting of the base metadata
126 // for immediate use in HTML. This metadata will be further massaged by get_field_def() and so should not be pre-formatted
127 while ( $row = $GLOBALS['db']->fetchByAssoc ( $result , -1 ,false ) ) {
128 $field = get_widget ( $row ['type'] );
130 foreach ( $row as $key => $value ) {
131 $field->$key = $value;
133 $field->default = $field->default_value;
134 $vardef = $field->get_field_def ();
135 $vardef ['id'] = $row ['id'];
136 $vardef ['custom_module'] = $row ['custom_module'];
137 if (empty ( $vardef ['source'] ))
138 $vardef ['source'] = 'custom_fields';
139 if (empty ( $results [$row ['custom_module']] ))
140 $results [$row ['custom_module']] = array ( );
141 $results [$row ['custom_module']] [$row ['name']] = $vardef;
143 if (empty ( $module )) {
144 foreach ( $results as $module => $result ) {
145 $this->saveToVardef ( $module, $result );
148 if (! empty ( $results [$module] )) {
149 $this->saveToVardef ( $module, $results [$module] );
151 $this->saveToVardef ( $module, false );
159 * Returns the widget for a custom field from the fields_meta_data table.
161 function getFieldWidget($module, $fieldName) {
162 if (empty($module) || empty($fieldName)){
163 sugar_die("Unable to load widget for '$module' : '$fieldName'");
165 $query = "SELECT * FROM fields_meta_data WHERE custom_module='$module' AND name='$fieldName' AND deleted = 0";
166 $result = $GLOBALS['db']->query ( $query );
167 require_once ('modules/DynamicFields/FieldCases.php');
168 if ( $row = $GLOBALS['db']->fetchByAssoc ( $result ) ) {
169 $field = get_widget ( $row ['type'] );
170 $field->populateFromRow($row);
177 * Updates the cached vardefs with the custom field information stored in result
179 * @param string $module
180 * @param array $result
182 function saveToVardef($module,$result) {
185 if (! empty ( $beanList [$module] )) {
186 $object = $beanList [$module];
188 if ($object == 'aCase') {
192 if(empty($GLOBALS['dictionary'][$object]['fields'])){
193 //if the vardef isn't loaded let's try loading it.
194 VardefManager::refreshVardefs($module,$object, null, false);
195 //if it's still not loaded we really don't have anything useful to cache
196 if(empty($GLOBALS['dictionary'][$object]['fields']))return;
198 $GLOBALS ['dictionary'] [$object] ['custom_fields'] = false;
199 if (! empty ( $GLOBALS ['dictionary'] [$object] )) {
200 if (! empty ( $result )) {
203 foreach ( $result as $field ) {
204 foreach($field as $k=>$v){
205 //allows values for custom fields to be defined outside of the scope of studio
206 if(!isset($GLOBALS ['dictionary'] [$object] ['fields'] [$field ['name']][$k])){
207 $GLOBALS ['dictionary'] [$object] ['fields'] [$field ['name']][$k] = $v;
212 // Second loop to remove
213 foreach ( $GLOBALS ['dictionary'] [$object] ['fields'] as $name => $fieldDef ) {
215 if (isset ( $fieldDef ['custom_module'] )) {
216 if (! isset ( $result [$name] )) {
217 unset ( $GLOBALS ['dictionary'] [$object] ['fields'] [$name] );
219 $GLOBALS ['dictionary'] [$object] ['custom_fields'] = true;
226 $manager = new VardefManager ( );
227 $manager->saveCache ( $this->module, $object );
232 * returns either false or an array containing the select and join parameter for a query using custom fields
233 * @param $expandedList boolean If true, return a list of all fields with source=custom_fields in the select instead of the standard _cstm.*
234 * This is required for any downstream construction of a SQL statement where we need to manipulate the select list,
235 * for example, listviews with custom relate fields where the value comes from join rather than from the custom table
237 * @return array select=>select columns, join=>prebuilt join statement
239 function getJOIN( $expandedList = false , $includeRelates = false, &$where = false){
240 if(!$this->bean->hasCustomFields()){
244 if (empty($expandedList) )
246 $select = ",{$this->bean->table_name}_cstm.*" ;
251 $isList = is_array($expandedList);
252 foreach($this->bean->field_defs as $name=>$field)
254 if (!empty($field['source']) && $field['source'] == 'custom_fields' && (!$isList || !empty($expandedList[$name]))){
255 // assumption: that the column name in _cstm is the same as the field name. Currently true.
256 // however, two types of dynamic fields do not have columns in the custom table - html fields (they're readonly) and flex relates (parent_name doesn't exist)
257 if ( $field['type'] != 'html' && $name != 'parent_name')
258 $select .= ",{$this->bean->table_name}_cstm.{$name}" ;
262 $join = " LEFT JOIN " .$this->bean->table_name. "_cstm ON " .$this->bean->table_name. ".id = ". $this->bean->table_name. "_cstm.id_c ";
264 if ($includeRelates) {
265 $jtAlias = "relJoin";
267 foreach($this->bean->field_defs as $name=>$field)
269 if ($field['type'] == 'relate' && isset($field['custom_module'])) {
270 $relateJoinInfo = $this->getRelateJoin($field, $jtAlias.$jtCount);
271 $select .= $relateJoinInfo['select'];
272 $join .= $relateJoinInfo['from'];
276 $pattern = '/'.$field['name'].'\slike/i';
277 $replacement = $relateJoinInfo['name_field'].' like';
278 $where = preg_replace($pattern,$replacement,$where);
285 return array('select'=>$select, 'join'=>$join);
289 function getRelateJoin($field_def, $joinTableAlias) {
290 if (empty($field_def['type']) || $field_def['type'] != "relate") {
293 global $beanFiles, $beanList, $module;
294 $rel_module = $field_def['module'];
295 if(empty($beanFiles[$beanList[$rel_module]])) {
299 require_once($beanFiles[$beanList[$rel_module]]);
300 $rel_mod = new $beanList[$rel_module]();
301 $rel_table = $rel_mod->table_name;
302 if (isset($rel_mod->field_defs['name']))
304 $name_field_def = $rel_mod->field_defs['name'];
305 if(isset($name_field_def['db_concat_fields']))
307 $name_field = db_concat($joinTableAlias, $name_field_def['db_concat_fields']);
309 //If the name field is non-db, we need to find another field to display
310 else if(!empty($rel_mod->field_defs['name']['source']) && $rel_mod->field_defs['name']['source'] == "non-db" && !empty($field_def['rname']))
312 $name_field = "$joinTableAlias." . $field_def['rname'];
316 $name_field = "$joinTableAlias.name";
319 $tableName = isset($field_def['custom_module']) ? "{$this->bean->table_name}_cstm" : $this->bean->table_name ;
320 $relID = $field_def['id_name'];
321 $ret_array['rel_table'] = $rel_table;
322 $ret_array['name_field'] = $name_field;
323 $ret_array['select'] = ", $name_field {$field_def['name']} ";
324 $ret_array['from'] = " LEFT JOIN $rel_table $joinTableAlias ON $tableName.$relID = $joinTableAlias.id"
325 . " AND $joinTableAlias.deleted=0 ";
330 * Fills in all the custom fields of type relate relationships for an object
333 function fill_relationships(){
334 global $beanList, $beanFiles;
335 if(!empty($this->bean->relDepth)) {
336 if($this->bean->relDepth > 1)return;
338 $this->bean->relDepth = 0;
340 foreach($this->bean->field_defs as $field){
341 if(empty($field['source']) || $field['source'] != 'custom_fields')continue;
342 if($field['type'] == 'relate'){
343 $related_module =$field['ext2'];
344 $name = $field['name'];
345 if (empty($this->bean->$name)) { //Don't load the relationship twice
346 $id_name = $field['id_name'];
347 if(isset($beanList[ $related_module])){
348 $class = $beanList[$related_module];
350 if(file_exists($beanFiles[$class]) && isset($this->bean->$name)){
351 require_once($beanFiles[$class]);
353 $mod->relDepth = $this->bean->relDepth + 1;
354 $mod->retrieve($this->bean->$id_name);
355 $this->bean->$name = $mod->name;
364 * Process the save action for sugar bean custom fields
366 * @param boolean $isUpdate
368 function save($isUpdate){
370 if($this->bean->hasCustomFields() && isset($this->bean->id)){
373 $query = "UPDATE ". $this->bean->table_name. "_cstm SET ";
375 $queryInsert = "INSERT INTO ". $this->bean->table_name. "_cstm (id_c";
376 $values = "('".$this->bean->id."'";
378 foreach($this->bean->field_defs as $name=>$field){
380 if(empty($field['source']) || $field['source'] != 'custom_fields')continue;
381 if($field['type'] == 'html' || $field['type'] == 'parent')continue;
382 if(isset($this->bean->$name)){
385 if(in_array($field['type'], array('int', 'float', 'double', 'uint', 'ulong', 'long', 'short', 'tinyint', 'currency', 'decimal'))) {
387 if(!isset($this->bean->$name) || !is_numeric($this->bean->$name) ){
388 if($field['required']){
389 $this->bean->$name = 0;
391 $this->bean->$name = 'NULL';
395 if ( $field['type'] == 'bool' ) {
396 if ( $this->bean->$name === FALSE )
397 $this->bean->$name = '0';
398 elseif ( $this->bean->$name === TRUE )
399 $this->bean->$name = '1';
402 $val = $this->bean->$name;
403 if(($field['type'] == 'date' || $field['type'] == 'datetimecombo') && (empty($this->bean->$name )|| $this->bean->$name == '1900-01-01')){
406 $this->bean->$name = ''; // do not set it to string 'NULL'
410 $query .= " $name=$quote".$GLOBALS['db']->quote($val)."$quote";
413 $query .= " ,$name=$quote".$GLOBALS['db']->quote($val)."$quote";
417 $queryInsert .= " ,$name";
418 $values .= " ,$quote". $GLOBALS['db']->quote($val). "$quote";
422 $query.= " WHERE id_c='" . $this->bean->id ."'";
426 $queryInsert .= " ) VALUES $values )";
429 $GLOBALS['db']->query($queryInsert);
431 $checkquery = "SELECT id_c FROM {$this->bean->table_name}_cstm WHERE id_c = '{$this->bean->id}'";
432 if ( $GLOBALS['db']->getOne($checkquery) )
433 $result = $GLOBALS['db']->query($query);
435 $GLOBALS['db']->query($queryInsert);
442 * Deletes the field from fields_meta_data and drops the database column then it rebuilds the cache
443 * Use the widgets get_db_modify_alter_table() method to get the table sql - some widgets do not need any custom table modifications
444 * @param STRING $name - field name
446 function deleteField($widget){
447 require_once('modules/DynamicFields/templates/Fields/TemplateField.php');
449 if (!($widget instanceof TemplateField)) {
450 $field_name = $widget;
451 $widget = new TemplateField();
452 $widget->name = $field_name;
454 $object_name = $beanList[$this->module];
456 if ($object_name == 'aCase') {
457 $object_name = 'Case';
460 $GLOBALS['db']->query("DELETE FROM fields_meta_data WHERE id='" . $this->module . $widget->name . "'");
461 $sql = $widget->get_db_delete_alter_table( $this->bean->table_name . "_cstm" ) ;
462 if (! empty( $sql ) )
463 $GLOBALS['db']->query( $sql );
465 $this->removeVardefExtension($widget);
466 VardefManager::clearVardef();
467 VardefManager::refreshVardefs($this->module, $object_name);
472 * Method required by the TemplateRelatedTextField->save() method
473 * Taken from MBModule's implementation
475 function fieldExists($name = '', $type = ''){
476 // must get the vardefs from the GLOBAL array as $bean->field_defs does not contain the values from the cache at this point
477 // TODO: fix this - saveToVardefs() updates GLOBAL['dictionary'] correctly, obtaining its information directly from the fields_meta_data table via buildCache()...
478 $vardefs = $GLOBALS['dictionary'][$this->bean->object_name]['fields'];
479 if(!empty($vardefs)){
480 if(empty($type) && empty($name))
482 else if(empty($type))
483 return !empty($vardefs[$name]);
484 else if(empty($name)){
485 foreach($vardefs as $def){
486 if($def['type'] == $type)
491 return (!empty($vardefs[$name]) && ($vardefs[$name]['type'] == $type));
499 * Adds a custom field using a field object
501 * @param Field Object $field
504 function addFieldObject(&$field){
505 $GLOBALS['log']->debug('adding field');
506 $object_name = $this->module;
507 $db_name = $field->name;
509 $fmd = new FieldsMetaData();
510 $id = $fmd->retrieve($object_name.$db_name,true, false);
512 $label = $field->label;
516 $db_name = $this->getDBName($field->name);
517 $field->name = $db_name;
519 $this->createCustomTable();
520 $fmd->id = $object_name.$db_name;
521 $fmd->custom_module= $object_name;
522 $fmd->name = $db_name;
523 $fmd->vname = $label;
524 $fmd->type = $field->type;
525 $fmd->help = $field->help;
526 if (!empty($field->len))
527 $fmd->len = $field->len; // tyoung bug 15407 - was being set to $field->size so changes weren't being saved
528 $fmd->required = ($field->required ? 1 : 0);
529 $fmd->default_value = $field->default;
530 $fmd->ext1 = $field->ext1;
531 $fmd->ext2 = $field->ext2;
532 $fmd->ext3 = $field->ext3;
533 $fmd->ext4 = (isset($field->ext4) ? $field->ext4 : '');
534 $fmd->comments = $field->comment;
535 $fmd->massupdate = $field->massupdate;
536 $fmd->importable = ( isset ( $field->importable ) ) ? $field->importable : null ;
537 $fmd->duplicate_merge = $field->duplicate_merge;
538 $fmd->audited =$field->audited;
539 $fmd->reportable = ($field->reportable ? 1 : 0);
541 $fmd->new_with_id=true;
544 $this->buildCache($this->module);
547 $query = $field->get_db_add_alter_table($this->bean->table_name . '_cstm');
549 $query = $field->get_db_modify_alter_table($this->bean->table_name . '_cstm');
552 $GLOBALS['db']->query($query);
554 $this->saveExtendedAttributes($field, array_keys($fmd->field_defs));
560 function saveExtendedAttributes($field, $column_fields)
562 require_once ('modules/ModuleBuilder/parsers/StandardField.php') ;
563 require_once ('modules/DynamicFields/FieldCases.php') ;
567 $base_field = get_widget ( $field->type) ;
568 foreach ($field->vardef_map as $property => $fmd_col){
570 if (!isset($field->$property) || in_array($fmd_col, $column_fields) || in_array($property, $column_fields)
571 || $this->isDefaultValue($property, $field->$property, $base_field)
572 || $property == "action" || $property == "label_value" || $property == "label"
573 || (substr($property, 0,3) == 'ext' && strlen($property) == 4))
577 $to_save[$property] =
578 is_string($field->$property) ? htmlspecialchars_decode($field->$property, ENT_QUOTES) : $field->$property;
580 $bean_name = $beanList[$this->module];
582 $this->writeVardefExtension($bean_name, $field, $to_save);
585 protected function isDefaultValue($property, $value, $baseField)
589 return ( $value === 'true' || $value === '1' || $value === true || $value === 1 ); break;
593 return ( $value === 'false' || $value === '0' || $value === false || $value === 0); break;
594 case "default_value":
598 return ($value == "");
599 case "duplicate_merge":
600 return ( $value === 'false' || $value === '0' || $value === false || $value === 0 || $value === "disabled"); break;
603 if (isset($baseField->$property))
605 return $baseField->$property == $value;
611 protected function writeVardefExtension($bean_name, $field, $def_override)
613 //Hack for the broken cases module
614 $vBean = $bean_name == "aCase" ? "Case" : $bean_name;
615 $file_loc = "$this->base_path/sugarfield_{$field->name}.php";
617 $out = "<?php\n // created: " . date('Y-m-d H:i:s') . "\n";
618 foreach ($def_override as $property => $val)
620 $out .= override_value_to_string_recursive(array($vBean, "fields", $field->name, $property), "dictionary", $val) . "\n";
625 if (!file_exists($this->base_path))
626 mkdir_recursive($this->base_path);
628 if( $fh = @sugar_fopen( $file_loc, 'w' ) )
640 protected function removeVardefExtension($field)
642 $file_loc = "$this->base_path/sugarfield_{$field->name}.php";
644 if (is_file($file_loc))
652 * DEPRECIATED: Use addFieldObject instead.
653 * Adds a Custom Field using parameters
655 * @param unknown_type $name
656 * @param unknown_type $label
657 * @param unknown_type $type
658 * @param unknown_type $max_size
659 * @param unknown_type $required_option
660 * @param unknown_type $default_value
661 * @param unknown_type $ext1
662 * @param unknown_type $ext2
663 * @param unknown_type $ext3
664 * @param unknown_type $audited
665 * @param unknown_type $mass_update
666 * @param unknown_type $ext4
667 * @param unknown_type $help
668 * @param unknown_type $duplicate_merge
669 * @param unknown_type $comment
672 function addField($name,$label='', $type='Text',$max_size='255',$required_option='optional', $default_value='', $ext1='', $ext2='', $ext3='',$audited=0, $mass_update = 0 , $ext4='', $help='',$duplicate_merge=0, $comment=''){
673 require_once('modules/DynamicFields/templates/Fields/TemplateField.php');
674 $field = new TemplateField();
675 $field->label = $label;
676 if(empty($field->label)){
677 $field->label = $name;
679 $field->name = $name;
680 $field->type = $type;
681 $field->len = $max_size;
682 $field->required = (!empty($required_option) && $required_option != 'optional');
683 $field->default = $default_value;
684 $field->ext1 = $ext1;
685 $field->ext2 = $ext2;
686 $field->ext3 = $ext3;
687 $field->ext4 = $ext4;
688 $field->help = $help;
689 $field->comments = $comment;
690 $field->massupdate = $mass_update;
691 $field->duplicate_merge = $duplicate_merge;
692 $field->audited = $audited;
693 $field->reportable = 1;
694 return $this->addFieldObject($field);
698 * Creates the custom table with an id of id_c
701 function createCustomTable($execute = true){
703 if (!$GLOBALS['db']->tableExists($this->bean->table_name."_cstm")) {
704 $GLOBALS['log']->debug('creating custom table for '. $this->bean->table_name);
705 $query = 'CREATE TABLE '.$this->bean->table_name.'_cstm ( ';
706 $query .='id_c ' . $this->bean->dbManager->helper->getColumnType('id') .' NOT NULL';
707 $query .=', PRIMARY KEY ( id_c ) )';
708 if($GLOBALS['db']->dbType == 'mysql'){
709 $query .= ' CHARACTER SET utf8 COLLATE utf8_general_ci';
711 $out = $query . "\n";
713 $GLOBALS['db']->query($query);
714 $out .= $this->add_existing_custom_fields($execute);
721 * Updates the db schema and adds any custom fields we have used if the custom table was dropped
724 function add_existing_custom_fields($execute = true){
726 if($this->bean->hasCustomFields()){
727 foreach($this->bean->field_defs as $name=>$data){
728 if(empty($data['source']) || $data['source'] != 'custom_fields')
730 $out .= $this->add_existing_custom_field($data, $execute);
736 function add_existing_custom_field($data, $execute = true)
739 $field = get_widget ( $data ['type'] );
740 $field->populateFromRow($data);
741 $query = "/*MISSING IN DATABASE - {$data['name']} - ROW*/\n"
742 . $field->get_db_add_alter_table($this->bean->table_name . '_cstm');
743 $out = $query . "\n";
745 $GLOBALS['db']->query($query);
750 public function repairCustomFields($execute = true)
752 $out = $this->createCustomTable($execute);
753 //If the table didn't exist, createCustomTable will have returned all the SQL to create and populate it
755 return "/*Checking Custom Fields for module : {$this->module} */\n$out";
756 //Otherwise make sure all the custom fields defined in the vardefs exist in the custom table.
757 //We aren't checking for data types, just that the column exists.
758 $db = $GLOBALS['db'];
759 $tablename = $this->bean->table_name."_cstm";
760 $compareFieldDefs = $db->getHelper()->get_columns($tablename);
761 foreach($this->bean->field_defs as $name=>$data){
762 if(empty($data['source']) || $data['source'] != 'custom_fields')
764 if(!empty($compareFieldDefs[$name])) {
767 $out .= $this->add_existing_custom_field($data, $execute);
770 $out = "/*Checking Custom Fields for module : {$this->module} */\n$out";
776 * Adds a label to the module's mod_strings for the current language
777 * Note that the system label name
779 * @param string $displayLabel The label value to be displayed
780 * @return string The core of the system label name - returns currency_id5 for example, when the full label would then be LBL_CURRENCY_ID5
781 * TODO: Only the core is returned for historical reasons - switch to return the real system label
783 function addLabel ( $displayLabel )
785 $mod_strings = return_module_language($GLOBALS[ 'current_language' ], $this->module);
788 $field_key = $this->getDBName($displayLabel, false);
789 $systemLabel = $field_key;
790 if(!$this->use_existing_labels){ // use_existing_labels defaults to false in this module; as of today, only set to true by ModuleInstaller.php
791 while( isset( $mod_strings [ $systemLabel ] ) && $count <= $limit )
793 $systemLabel = $field_key. "_$count";
797 $selMod = (!empty($_REQUEST['view_module'])) ? $_REQUEST['view_module'] : $this->module;
798 require_once 'modules/ModuleBuilder/parsers/parser.label.php' ;
799 $parser = new ParserLabel ( $selMod , isset ( $_REQUEST [ 'view_package' ] ) ? $_REQUEST [ 'view_package' ] : null ) ;
800 $parser->handleSave ( array('label_'. $systemLabel => $displayLabel ) , $GLOBALS [ 'current_language' ] ) ;
806 * Returns a Database Safe Name
808 * @param STRING $name
809 * @param BOOLEAN $_C do we append _c to the name
812 function getDBName($name, $_C= true){
813 static $cached_results = array();
814 if(!empty($cached_results[$name]))
816 return $cached_results[$name];
818 $exclusions = array('parent_type', 'parent_id', 'currency_id', 'parent_name');
819 // Remove any non-db friendly characters
820 $return_value = preg_replace("/[^\w]+/","_",$name);
821 if($_C == true && !in_array($return_value, $exclusions) && substr($return_value, -2) != '_c'){
822 $return_value .= '_c';
824 $cached_results[$name] = $return_value;
825 return $return_value;
828 function setWhereClauses(&$where_clauses){
829 if (isset($this->avail_fields)) {
830 foreach($this->avail_fields as $name=>$value){
831 if(!empty($_REQUEST[$name])){
832 $where_clauses[] = $this->bean->table_name . "_cstm.$name LIKE '". $GLOBALS['db']->quote($_REQUEST[$name]). "%'";
839 /////////////////////////BACKWARDS COMPATABILITY MODE FOR PRE 5.0 MODULES\\\\\\\\\\\\\\\\\\\\\\\\\\\
840 ////////////////////////////END BACKWARDS COMPATABILITY MODE FOR PRE 5.0 MODULES\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
845 loads fields into the bean
846 This used to be called during the retrieve process now it is done through a join
847 Restored from pre-r30895 to maintain support for custom code that may have called retrieve() directly
852 if(!isset($this->bean)){
853 $GLOBALS['log']->fatal("DynamicField retrieve, bean not instantiated: ".var_export(debug_print_backtrace(), true));
857 if(!$this->bean->hasCustomFields()){
861 $query = "SELECT * FROM ".$this->bean->table_name."_cstm WHERE id_c='".$this->bean->id."'";
862 $result = $GLOBALS['db']->query($query);
863 $row = $GLOBALS['db']->fetchByAssoc($result);
867 foreach($row as $name=>$value)
869 // originally in pre-r30895 we checked if this field was in avail_fields i.e., in fields_meta_data and not deleted
870 // with the removal of avail_fields post-r30895 we have simplified this - we now retrieve every custom field even if previously deleted
871 // this is considered harmless as the value although set in the bean will not otherwise be used (nothing else works off the list of fields in the bean)
872 $this->bean->$name = $value;
878 function populateXTPL(&$xtpl, $view){
879 if($this->bean->hasCustomFields()){
880 $results = $this->getAllFieldsView($view, 'xtpl');
881 foreach($results as $name=>$value){
882 if(is_array($value['xtpl'])){
883 foreach($value['xtpl'] as $xName=>$xValue){
884 $xtpl->assign(strtoupper($xName), $xValue);
888 $xtpl->assign(strtoupper($name), $value['xtpl']);
895 function populateAllXTPL(&$xtpl, $view){
896 $this->populateXTPL($xtpl, $view);
900 function getAllFieldsView($view, $type){
902 foreach($this->bean->field_defs as $name=>$data){
903 if(empty($data['source']) || $data['source'] != 'custom_fields')continue;
904 require_once ('modules/DynamicFields/FieldCases.php');
905 $field = get_widget ( $data ['type'] );
906 $field->populateFromRow($data);
907 $field->view = $view;
908 $field->bean =& $this->bean;
909 switch(strtolower($type)){
911 $results[$name] = array('xtpl'=>$field->get_xtpl());
914 $results[$name] = array('html'=> $field->get_html(), 'label'=> $field->get_html_label(), 'fieldType'=>$field->data_type, 'isCustom' =>true);
923 ////////////////////////////END BACKWARDS COMPATABILITY MODE FOR PRE 5.0 MODULES\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\