2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM Community Edition is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
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: Creates the runtime database connection.
41 ********************************************************************************/
43 var $formname = 'form';
45 var $sugarbean = null;
46 function setFormName($name){
47 $this->formname = $name;
50 function javascript(){
51 global $app_strings, $current_user, $sugar_config;
53 // Bug 24730 - default initialize the bean object in case we never set it to the current bean object
54 $this->sugarbean = new stdClass;
55 $this->sugarbean->field_name_map = array();
56 $this->sugarbean->module_dir = '';
59 function setSugarBean($sugar){
60 $this->sugarbean = $sugar;
63 function addRequiredFields($prefix=''){
64 if(isset($this->sugarbean->required_fields)){
65 foreach($this->sugarbean->required_fields as $field=>$value){
66 $this->addField($field,'true', $prefix);
71 function addSpecialField($dispField, $realField, $type, $required, $prefix = '') {
72 if (isset($this->sugarbean->field_name_map[$realField]['vname'])) {
73 $this->addFieldGeneric($dispField, 'date', $this->sugarbean->field_name_map[$realField]['vname'], $required, $prefix );
77 function addField($field,$required, $prefix='', $displayField='', $translate = false){
78 if(isset($this->sugarbean->field_name_map[$field]['vname'])){
79 $vname = $this->sugarbean->field_name_map[$field]['vname'];
81 $vname = $this->buildStringToTranslateInSmarty($this->sugarbean->field_name_map[$field]['vname']);
83 if(isset($this->sugarbean->field_name_map[$field]['required']) && $this->sugarbean->field_name_map[$field]['required']){
88 if(isset($this->sugarbean->required_fields[$field]) && $this->sugarbean->required_fields[$field]){
96 if(isset($this->sugarbean->field_name_map[$field]['validation'])){
97 switch($this->sugarbean->field_name_map[$field]['validation']['type']){
101 if(isset($this->sugarbean->field_name_map[$field]['validation']['min'])){
102 $min = $this->sugarbean->field_name_map[$field]['validation']['min'];
104 if(isset($this->sugarbean->field_name_map[$field]['validation']['max'])){
105 $max = $this->sugarbean->field_name_map[$field]['validation']['max'];
110 if(!empty($displayField)){
111 $dispField = $displayField;
116 $this->addFieldRange($dispField,$this->sugarbean->field_name_map[$field]['type'],$vname,$required,$prefix, $min, $max );
119 $compareTo = $this->sugarbean->field_name_map[$field]['validation']['compareto'];
120 if(!empty($displayField)){
121 $dispField = $displayField;
126 if(!empty($this->sugarbean->field_name_map[$field]['validation']['blank']) && $this->sugarbean->field_name_map[$field]['validation']['blank'])
127 $this->addFieldDateBeforeAllowBlank($dispField,$this->sugarbean->field_name_map[$field]['type'],$vname,$required,$prefix, $compareTo );
128 else $this->addFieldDateBefore($dispField,$this->sugarbean->field_name_map[$field]['type'],$vname,$required,$prefix, $compareTo );
131 if(!empty($displayField)){
132 $dispField = $displayField;
138 $type = (!empty($this->sugarbean->field_name_map[$field]['custom_type']))?$this->sugarbean->field_name_map[$field]['custom_type']:$this->sugarbean->field_name_map[$field]['type'];
140 $this->addFieldGeneric($dispField,$type,$vname,$required,$prefix );
144 if(!empty($displayField)){
145 $dispField = $displayField;
150 $type = (!empty($this->sugarbean->field_name_map[$field]['custom_type']))?$this->sugarbean->field_name_map[$field]['custom_type']:$this->sugarbean->field_name_map[$field]['type'];
151 if(!empty($this->sugarbean->field_name_map[$dispField]['isMultiSelect']))$dispField .='[]';
152 $this->addFieldGeneric($dispField,$type,$vname,$required,$prefix );
155 $GLOBALS['log']->debug('No VarDef Label For ' . $field . ' in module ' . $this->sugarbean->module_dir );
161 function stripEndColon($modString)
163 if(substr($modString, -1, 1) == ":")
164 $modString = substr($modString, 0, (strlen($modString) - 1));
165 if(substr($modString, -2, 2) == ": ")
166 $modString = substr($modString, 0, (strlen($modString) - 2));
171 function addFieldGeneric($field, $type,$displayName, $required, $prefix=''){
172 $this->script .= "addToValidate('".$this->formname."', '".$prefix.$field."', '".$type . "', {$this->getRequiredString($required)},'"
173 . $this->stripEndColon(translate($displayName,$this->sugarbean->module_dir)) . "' );\n";
176 function addFieldRange($field, $type,$displayName, $required, $prefix='',$min, $max){
177 $this->script .= "addToValidateRange('".$this->formname."', '".$prefix.$field."', '".$type . "', {$this->getRequiredString($required)},'"
178 . $this->stripEndColon(translate($displayName,$this->sugarbean->module_dir)) . "', $min, $max );\n";
181 function addFieldIsValidDate($field, $type, $displayName, $msg, $required, $prefix='') {
182 $name = $prefix.$field;
183 $req = $this->getRequiredString($required);
184 $this->script .= "addToValidateIsValidDate('{$this->formname}', '{$name}', '{$type}', {$req}, '{$msg}');\n";
187 function addFieldIsValidTime($field, $type, $displayName, $msg, $required, $prefix='') {
188 $name = $prefix.$field;
189 $req = $this->getRequiredString($required);
190 $this->script .= "addToValidateIsValidTime('{$this->formname}', '{$name}', '{$type}', {$req}, '{$msg}');\n";
193 function addFieldDateBefore($field, $type,$displayName, $required, $prefix='',$compareTo){
194 $this->script .= "addToValidateDateBefore('".$this->formname."', '".$prefix.$field."', '".$type . "', {$this->getRequiredString($required)},'"
195 . $this->stripEndColon(translate($displayName,$this->sugarbean->module_dir)) . "', '$compareTo' );\n";
198 function addFieldDateBeforeAllowBlank($field, $type, $displayName, $required, $prefix='', $compareTo, $allowBlank='true'){
199 $this->script .= "addToValidateDateBeforeAllowBlank('".$this->formname."', '".$prefix.$field."', '".$type . "', {$this->getRequiredString($required)},'"
200 . $this->stripEndColon(translate($displayName,$this->sugarbean->module_dir)) . "', '$compareTo', '$allowBlank' );\n";
203 function addToValidateBinaryDependency($field, $type, $displayName, $required, $prefix='',$compareTo){
204 $this->script .= "addToValidateBinaryDependency('".$this->formname."', '".$prefix.$field."', '".$type . "', {$this->getRequiredString($required)},'"
205 . $this->stripEndColon(translate($displayName,$this->sugarbean->module_dir)) . "', '$compareTo' );\n";
208 function addToValidateComparison($field, $type, $displayName, $required, $prefix='',$compareTo){
209 $this->script .= "addToValidateComparison('".$this->formname."', '".$prefix.$field."', '".$type . "', {$this->getRequiredString($required)},'"
210 . $this->stripEndColon(translate($displayName,$this->sugarbean->module_dir)) . "', '$compareTo' );\n";
213 function addFieldIsInArray($field, $type, $displayName, $required, $prefix, $arr, $operator){
214 $name = $prefix.$field;
215 $req = $this->getRequiredString($required);
216 $json = getJSONobj();
217 $arr = $json->encode($arr);
218 $this->script .= "addToValidateIsInArray('{$this->formname}', '{$name}', '{$type}', {$req}, '".$this->stripEndColon(translate($displayName,$this->sugarbean->module_dir))."', '{$arr}', '{$operator}');\n";
221 function addAllFields($prefix,$skip_fields=null, $translate = false){
222 if (!isset($skip_fields))
224 $skip_fields = array();
226 foreach($this->sugarbean->field_name_map as $field=>$value){
227 if (!isset($skip_fields[$field]))
229 if(isset($value['type']) && ($value['type'] == 'datetimecombo' || $value['type'] == 'datetime')) {
230 $isRequired = (isset($value['required']) && $value['required']) ? 'true' : 'false';
231 $this->addSpecialField($value['name'] . '_date', $value['name'], 'datetime', $isRequired);
232 if ($value['type'] != 'link' && isset($this->sugarbean->field_name_map[$field]['validation'])) {
233 //datetime should also support the isbefore or other type of validate
234 $this->addField($field, '', $prefix,'',$translate);
236 } else if (isset($value['type'])) {
237 if ($value['type'] != 'link') {
238 $this->addField($field, '', $prefix,'',$translate);
245 function getScript($showScriptTag = true){
246 $tempScript = $this->script;
249 $this->script = "<script type=\"text/javascript\">\n";
252 $this->script .= $tempScript;
255 $this->script .= "</script>";
257 return $this->script;
260 function buildStringToTranslateInSmarty(
264 if ( is_array($string) ) {
266 foreach ( $string as $astring )
267 $returnstring .= $this->buildStringToTranslateInSmarty($astring);
268 return $returnstring;
270 return "{/literal}{sugar_translate label='$string' module='{$this->sugarbean->module_dir}' for_js=true}{literal}";
273 protected function getRequiredString($required)
275 if (is_string($required) && strtolower($required) == "false")
279 return empty($required) ? "false" : "true";