]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/javascript/javascript.php
Release 6.1.5
[Github/sugarcrm.git] / include / javascript / javascript.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6  * 
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.
13  * 
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
17  * details.
18  * 
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
22  * 02110-1301 USA.
23  * 
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.
26  * 
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.
30  * 
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  ********************************************************************************/
37
38 /*********************************************************************************
39
40  * Description:  Creates the runtime database connection.
41  ********************************************************************************/
42 class javascript{
43         var $formname = 'form';
44         var $script = '';
45         var $sugarbean = null;
46         function setFormName($name){
47                 $this->formname = $name;
48         }
49
50         function javascript(){
51                 global $app_strings, $current_user, $sugar_config;
52
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 = '';
57         }
58         
59         function setSugarBean($sugar){
60                 $this->sugarbean = $sugar;
61         }
62
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);
67                                 }
68                         }
69         }
70
71     function addSpecialField($dispField, $realField, $type, $required, $prefix = '') {
72         $this->addFieldGeneric($dispField, 'date', $this->sugarbean->field_name_map[$realField]['vname'], $required, $prefix );
73     }
74     
75         function addField($field,$required, $prefix='', $displayField='', $translate = false){
76                 if(isset($this->sugarbean->field_name_map[$field]['vname'])){
77             $vname = $this->sugarbean->field_name_map[$field]['vname'];
78             if ( $translate )
79                 $vname = $this->buildStringToTranslateInSmarty($this->sugarbean->field_name_map[$field]['vname']);
80                         if(empty($required)){
81                                 if(isset($this->sugarbean->field_name_map[$field]['required']) && $this->sugarbean->field_name_map[$field]['required']){
82                                         $required = 'true';
83                                 }else{
84                                         $required = 'false';    
85                                 }
86                                 if(isset($this->sugarbean->required_fields[$field]) && $this->sugarbean->required_fields[$field]){
87                                         $required = 'true';
88                                 }
89                                 if($field == 'id'){
90                                         $required = 'false';    
91                                 }       
92                                                 
93                         }
94                         if(isset($this->sugarbean->field_name_map[$field]['validation'])){
95                                 switch($this->sugarbean->field_name_map[$field]['validation']['type']){
96                                         case 'range': 
97                                                 $min = 0;
98                                                 $max = 100;
99                                                 if(isset($this->sugarbean->field_name_map[$field]['validation']['min'])){
100                                                         $min = $this->sugarbean->field_name_map[$field]['validation']['min'];
101                                                 }
102                                                 if(isset($this->sugarbean->field_name_map[$field]['validation']['max'])){
103                                                         $max = $this->sugarbean->field_name_map[$field]['validation']['max'];
104                                                 }
105                                                 if($min > $max){
106                                                         $max = $min;
107                                                 }
108                                                 if(!empty($displayField)){
109                                                         $dispField = $displayField;
110                                                 }
111                                                 else{
112                                                         $dispField = $field;
113                                                 }
114                                                 $this->addFieldRange($dispField,$this->sugarbean->field_name_map[$field]['type'],$vname,$required,$prefix, $min, $max );        
115                                                 break;
116                                         case 'isbefore':
117                                                 $compareTo = $this->sugarbean->field_name_map[$field]['validation']['compareto'];
118                                                 if(!empty($displayField)){
119                                                         $dispField = $displayField;
120                                                 }
121                                                 else{
122                                                         $dispField = $field;
123                                                 }
124                                                 if(!empty($this->sugarbean->field_name_map[$field]['validation']['blank']) && $this->sugarbean->field_name_map[$field]['validation']['blank']) 
125                                                 $this->addFieldDateBeforeAllowBlank($dispField,$this->sugarbean->field_name_map[$field]['type'],$vname,$required,$prefix, $compareTo );
126                                                 else $this->addFieldDateBefore($dispField,$this->sugarbean->field_name_map[$field]['type'],$vname,$required,$prefix, $compareTo );
127                                                 break;
128                                         default: 
129                                                 if(!empty($displayField)){
130                                                         $dispField = $displayField;
131                                                 }
132                                                 else{
133                                                         $dispField = $field;
134                                                 }
135                                                 
136                                                 $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'];
137                                                 
138                                                 $this->addFieldGeneric($dispField,$type,$vname,$required,$prefix );     
139                                                 break;
140                                 }
141                         }else{
142                                 if(!empty($displayField)){
143                                                         $dispField = $displayField;
144                                                 }
145                                                 else{
146                                                         $dispField = $field;
147                                                 }
148                                         $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'];
149                                         if(!empty($this->sugarbean->field_name_map[$dispField]['isMultiSelect']))$dispField .='[]';
150                                         $this->addFieldGeneric($dispField,$type,$vname,$required,$prefix );
151                         }
152                 }else{
153                         $GLOBALS['log']->debug('No VarDef Label For ' . $field . ' in module ' . $this->sugarbean->module_dir );        
154                 }
155
156         }
157
158
159         function stripEndColon($modString)
160         {
161                 if(substr($modString, -1, 1) == ":")
162                         $modString = substr($modString, 0, (strlen($modString) - 1));
163                 if(substr($modString, -2, 2) == ": ")
164                         $modString = substr($modString, 0, (strlen($modString) - 2));
165                 return $modString;
166                 
167         }
168         
169         function addFieldGeneric($field, $type,$displayName, $required, $prefix=''){
170
171                 $this->script .= "addToValidate('".$this->formname."', '".$prefix.$field."', '".$type . "', {$this->getRequiredString($required)},'"
172                        . $this->stripEndColon(translate($displayName,$this->sugarbean->module_dir)) . "' );\n";
173         }
174
175         function addFieldRange($field, $type,$displayName, $required, $prefix='',$min, $max){
176                 $this->script .= "addToValidateRange('".$this->formname."', '".$prefix.$field."', '".$type . "', {$this->getRequiredString($required)},'"
177                        . $this->stripEndColon(translate($displayName,$this->sugarbean->module_dir)) . "', $min, $max );\n";
178         }
179         
180         function addFieldIsValidDate($field, $type, $displayName, $msg, $required, $prefix='') {
181                 $name = $prefix.$field;
182                 $req = $this->getRequiredString($required);
183                 $this->script .= "addToValidateIsValidDate('{$this->formname}', '{$name}', '{$type}', {$req}, '{$msg}');\n";
184         }
185
186         function addFieldIsValidTime($field, $type, $displayName, $msg, $required, $prefix='') {
187                 $name = $prefix.$field;
188                 $req = $this->getRequiredString($required);
189                 $this->script .= "addToValidateIsValidTime('{$this->formname}', '{$name}', '{$type}', {$req}, '{$msg}');\n";
190         }
191
192         function addFieldDateBefore($field, $type,$displayName, $required, $prefix='',$compareTo){
193                 $this->script .= "addToValidateDateBefore('".$this->formname."', '".$prefix.$field."', '".$type . "', {$this->getRequiredString($required)},'"
194                        . $this->stripEndColon(translate($displayName,$this->sugarbean->module_dir)) . "', '$compareTo' );\n";
195         }
196
197         function addFieldDateBeforeAllowBlank($field, $type, $displayName, $required, $prefix='', $compareTo, $allowBlank='true'){
198                 $this->script .= "addToValidateDateBeforeAllowBlank('".$this->formname."', '".$prefix.$field."', '".$type . "', {$this->getRequiredString($required)},'"
199                        . $this->stripEndColon(translate($displayName,$this->sugarbean->module_dir)) . "', '$compareTo', '$allowBlank' );\n";
200         }
201         
202         function addToValidateBinaryDependency($field, $type, $displayName, $required, $prefix='',$compareTo){
203                 $this->script .= "addToValidateBinaryDependency('".$this->formname."', '".$prefix.$field."', '".$type . "', {$this->getRequiredString($required)},'"
204                        . $this->stripEndColon(translate($displayName,$this->sugarbean->module_dir)) . "', '$compareTo' );\n";
205         }
206     
207     function addToValidateComparison($field, $type, $displayName, $required, $prefix='',$compareTo){
208         $this->script .= "addToValidateComparison('".$this->formname."', '".$prefix.$field."', '".$type . "', {$this->getRequiredString($required)},'"
209                        . $this->stripEndColon(translate($displayName,$this->sugarbean->module_dir)) . "', '$compareTo' );\n";
210     }
211     
212     function addFieldIsInArray($field, $type, $displayName, $required, $prefix, $arr, $operator){
213         $name = $prefix.$field;
214                 $req = $this->getRequiredString($required);
215                 $json = getJSONobj();
216                 $arr = $json->encode($arr);
217                 $this->script .= "addToValidateIsInArray('{$this->formname}', '{$name}', '{$type}', {$req}, '".$this->stripEndColon(translate($displayName,$this->sugarbean->module_dir))."', '{$arr}', '{$operator}');\n";
218     }
219
220         function addAllFields($prefix,$skip_fields=null, $translate = false){
221                 if (!isset($skip_fields))
222                 {
223                         $skip_fields = array();
224                 }
225                 foreach($this->sugarbean->field_name_map as $field=>$value){
226                         if (!isset($skip_fields[$field]))
227                         {
228                             if(isset($value['type']) && ($value['type'] == 'datetimecombo' || $value['type'] == 'datetime')) {
229                                 $isRequired = (isset($value['required']) && $value['required']) ? 'true' : 'false';
230                                 $this->addSpecialField($value['name'] . '_date', $value['name'], 'datetime', $isRequired);
231                     if ($value['type'] != 'link'  && isset($this->sugarbean->field_name_map[$field]['validation'])) {
232                         //datetime should also support the isbefore or other type of validate
233                         $this->addField($field, '', $prefix,'',$translate);
234                     }
235                             } else if (isset($value['type'])) {
236                                         if ($value['type'] != 'link') {                                         
237                                                 $this->addField($field, '', $prefix,'',$translate);
238                                         }
239                                 }
240                         }
241                 }
242         }
243
244         function getScript($showScriptTag = true){
245                 $tempScript = $this->script;
246                 $this->script = "";
247                 if($showScriptTag){
248                         $this->script = "<script type=\"text/javascript\">\n";
249                 }
250                 
251                 $this->script .= $tempScript;
252
253                 if($showScriptTag){
254                         $this->script .= "</script>";
255                 }
256                 return $this->script;
257         }
258     
259     function buildStringToTranslateInSmarty(
260         $string
261         )
262     {
263         if ( is_array($string) ) {
264             $returnstring = '';
265             foreach ( $string as $astring )
266                 $returnstring .= $this->buildStringToTranslateInSmarty($astring);
267             return $returnstring;
268         }
269             
270         return "{/literal}{sugar_translate label='$string' module='{$this->sugarbean->module_dir}' for_js=true}{literal}";
271     }
272
273     protected function getRequiredString($required)
274     {
275         if (is_string($required) && strtolower($required) == "false")
276         {
277             return "false";
278         }
279         return empty($required) ? "false" : "true";
280     }
281 }
282 ?>