]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/javascript/javascript.php
Release 6.4.0
[Github/sugarcrm.git] / include / javascript / javascript.php
1 <?php
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.
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        if (isset($this->sugarbean->field_name_map[$realField]['vname'])) {
73         $this->addFieldGeneric($dispField, 'date', $this->sugarbean->field_name_map[$realField]['vname'], $required, $prefix );
74        }
75     }
76
77         function addField($field,$required, $prefix='', $displayField='', $translate = false){
78                 if ($field == "id") return;
79         if(isset($this->sugarbean->field_name_map[$field]['vname'])){
80             $vname = $this->sugarbean->field_name_map[$field]['vname'];
81             if ( $translate )
82                 $vname = $this->buildStringToTranslateInSmarty($this->sugarbean->field_name_map[$field]['vname']);
83                         if(empty($required)){
84                                 if(isset($this->sugarbean->field_name_map[$field]['required']) && $this->sugarbean->field_name_map[$field]['required']){
85                                         $required = 'true';
86                                 }else{
87                                         $required = 'false';
88                                 }
89                                 if(isset($this->sugarbean->required_fields[$field]) && $this->sugarbean->required_fields[$field]){
90                                         $required = 'true';
91                                 }
92                                 if($field == 'id'){
93                                         $required = 'false';
94                                 }
95
96                         }
97                         if(isset($this->sugarbean->field_name_map[$field]['validation'])){
98                                 switch($this->sugarbean->field_name_map[$field]['validation']['type']){
99                                         case 'range':
100                                                 $min = 0;
101                                                 $max = 100;
102                                                 if(isset($this->sugarbean->field_name_map[$field]['validation']['min'])){
103                                                         $min = $this->sugarbean->field_name_map[$field]['validation']['min'];
104                                                 }
105                                                 if(isset($this->sugarbean->field_name_map[$field]['validation']['max'])){
106                                                         $max = $this->sugarbean->field_name_map[$field]['validation']['max'];
107                                                 }
108                                                 if($min > $max){
109                                                         $max = $min;
110                                                 }
111                                                 if(!empty($displayField)){
112                                                         $dispField = $displayField;
113                                                 }
114                                                 else{
115                                                         $dispField = $field;
116                                                 }
117                                                 $this->addFieldRange($dispField,$this->sugarbean->field_name_map[$field]['type'],$vname,$required,$prefix, $min, $max );
118                                                 break;
119                                         case 'isbefore':
120                                                 $compareTo = $this->sugarbean->field_name_map[$field]['validation']['compareto'];
121                                                 if(!empty($displayField)){
122                                                         $dispField = $displayField;
123                                                 }
124                                                 else{
125                                                         $dispField = $field;
126                                                 }
127                                                 if(!empty($this->sugarbean->field_name_map[$field]['validation']['blank']) && $this->sugarbean->field_name_map[$field]['validation']['blank'])
128                                                 $this->addFieldDateBeforeAllowBlank($dispField,$this->sugarbean->field_name_map[$field]['type'],$vname,$required,$prefix, $compareTo );
129                                                 else $this->addFieldDateBefore($dispField,$this->sugarbean->field_name_map[$field]['type'],$vname,$required,$prefix, $compareTo );
130                                                 break;
131                     // Bug #47961 Adding new type of validation: through callback function
132                     case 'callback' :
133                         $dispField = $displayField ? $displayField : $field;
134                         $this->addFieldCallback($dispField, $this->sugarbean->field_name_map[$field]['type'], $vname, $required, $prefix, $this->sugarbean->field_name_map[$field]['validation']['callback']);
135                         break;
136                                         default:
137                                                 if(!empty($displayField)){
138                                                         $dispField = $displayField;
139                                                 }
140                                                 else{
141                                                         $dispField = $field;
142                                                 }
143
144                                                 $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'];
145
146                                                 $this->addFieldGeneric($dispField,$type,$vname,$required,$prefix );
147                                                 break;
148                                 }
149                         }else{
150                                 if(!empty($displayField)){
151                                                         $dispField = $displayField;
152                                                 }
153                                                 else{
154                                                         $dispField = $field;
155                                                 }
156                                         $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'];
157                                         if(!empty($this->sugarbean->field_name_map[$dispField]['isMultiSelect']))$dispField .='[]';
158                                         $this->addFieldGeneric($dispField,$type,$vname,$required,$prefix );
159                         }
160                 }else{
161                         $GLOBALS['log']->debug('No VarDef Label For ' . $field . ' in module ' . $this->sugarbean->module_dir );
162                 }
163
164         }
165
166
167         function stripEndColon($modString)
168         {
169                 if(substr($modString, -1, 1) == ":")
170                         $modString = substr($modString, 0, (strlen($modString) - 1));
171                 if(substr($modString, -2, 2) == ": ")
172                         $modString = substr($modString, 0, (strlen($modString) - 2));
173                 return $modString;
174
175         }
176
177         function addFieldGeneric($field, $type,$displayName, $required, $prefix=''){
178                 $this->script .= "addToValidate('".$this->formname."', '".$prefix.$field."', '".$type . "', {$this->getRequiredString($required)},'"
179                        . $this->stripEndColon(translate($displayName,$this->sugarbean->module_dir)) . "' );\n";
180         }
181
182     // Bug #47961 Generator of callback validator
183     function addFieldCallback($field, $type, $displayName, $required, $prefix, $callback)
184     {
185         $this->script .= 'addToValidateCallback("'
186             . $this->formname . '", "'
187             . $prefix.$field . '", "'
188             . $type . '", '
189             . $this->getRequiredString($required) . ', "'
190             . $this->stripEndColon(translate($displayName, $this->sugarbean->module_dir)).'", '
191             .$callback
192         .');'."\n";
193     }
194
195         function addFieldRange($field, $type,$displayName, $required, $prefix='',$min, $max){
196                 $this->script .= "addToValidateRange('".$this->formname."', '".$prefix.$field."', '".$type . "', {$this->getRequiredString($required)},'"
197                        . $this->stripEndColon(translate($displayName,$this->sugarbean->module_dir)) . "', $min, $max );\n";
198         }
199
200         function addFieldIsValidDate($field, $type, $displayName, $msg, $required, $prefix='') {
201                 $name = $prefix.$field;
202                 $req = $this->getRequiredString($required);
203                 $this->script .= "addToValidateIsValidDate('{$this->formname}', '{$name}', '{$type}', {$req}, '{$msg}');\n";
204         }
205
206         function addFieldIsValidTime($field, $type, $displayName, $msg, $required, $prefix='') {
207                 $name = $prefix.$field;
208                 $req = $this->getRequiredString($required);
209                 $this->script .= "addToValidateIsValidTime('{$this->formname}', '{$name}', '{$type}', {$req}, '{$msg}');\n";
210         }
211
212         function addFieldDateBefore($field, $type,$displayName, $required, $prefix='',$compareTo){
213                 $this->script .= "addToValidateDateBefore('".$this->formname."', '".$prefix.$field."', '".$type . "', {$this->getRequiredString($required)},'"
214                        . $this->stripEndColon(translate($displayName,$this->sugarbean->module_dir)) . "', '$compareTo' );\n";
215         }
216
217         function addFieldDateBeforeAllowBlank($field, $type, $displayName, $required, $prefix='', $compareTo, $allowBlank='true'){
218                 $this->script .= "addToValidateDateBeforeAllowBlank('".$this->formname."', '".$prefix.$field."', '".$type . "', {$this->getRequiredString($required)},'"
219                        . $this->stripEndColon(translate($displayName,$this->sugarbean->module_dir)) . "', '$compareTo', '$allowBlank' );\n";
220         }
221
222         function addToValidateBinaryDependency($field, $type, $displayName, $required, $prefix='',$compareTo){
223                 $this->script .= "addToValidateBinaryDependency('".$this->formname."', '".$prefix.$field."', '".$type . "', {$this->getRequiredString($required)},'"
224                        . $this->stripEndColon(translate($displayName,$this->sugarbean->module_dir)) . "', '$compareTo' );\n";
225         }
226
227     function addToValidateComparison($field, $type, $displayName, $required, $prefix='',$compareTo){
228         $this->script .= "addToValidateComparison('".$this->formname."', '".$prefix.$field."', '".$type . "', {$this->getRequiredString($required)},'"
229                        . $this->stripEndColon(translate($displayName,$this->sugarbean->module_dir)) . "', '$compareTo' );\n";
230     }
231
232     function addFieldIsInArray($field, $type, $displayName, $required, $prefix, $arr, $operator){
233         $name = $prefix.$field;
234                 $req = $this->getRequiredString($required);
235                 $json = getJSONobj();
236                 $arr = $json->encode($arr);
237                 $this->script .= "addToValidateIsInArray('{$this->formname}', '{$name}', '{$type}', {$req}, '".$this->stripEndColon(translate($displayName,$this->sugarbean->module_dir))."', '{$arr}', '{$operator}');\n";
238     }
239
240         function addAllFields($prefix,$skip_fields=null, $translate = false){
241                 if (!isset($skip_fields))
242                 {
243                         $skip_fields = array();
244                 }
245                 foreach($this->sugarbean->field_name_map as $field=>$value){
246                         if (!isset($skip_fields[$field]))
247                         {
248                             if(isset($value['type']) && ($value['type'] == 'datetimecombo' || $value['type'] == 'datetime')) {
249                                 $isRequired = (isset($value['required']) && $value['required']) ? 'true' : 'false';
250                                 $this->addSpecialField($value['name'] . '_date', $value['name'], 'datetime', $isRequired);
251                     if ($value['type'] != 'link'  && isset($this->sugarbean->field_name_map[$field]['validation'])) {
252                         //datetime should also support the isbefore or other type of validate
253                         $this->addField($field, '', $prefix,'',$translate);
254                     }
255                             } else if (isset($value['type'])) {
256                                         if ($value['type'] != 'link') {
257                                                 $this->addField($field, '', $prefix,'',$translate);
258                                         }
259                                 }
260                         }
261                 }
262         }
263
264         function getScript($showScriptTag = true, $clearValidateFields = true){
265                 $tempScript = $this->script;
266                 $this->script = "";
267                 if($showScriptTag){
268                         $this->script = "<script type=\"text/javascript\">\n";
269                 }
270
271         if($clearValidateFields){
272             $this->script .= "addForm('{$this->formname}');";
273         }
274   
275                 $this->script .= $tempScript;
276
277                 if($showScriptTag){
278                         $this->script .= "</script>";
279                 }
280                 return $this->script;
281         }
282
283     function buildStringToTranslateInSmarty(
284         $string
285         )
286     {
287         if ( is_array($string) ) {
288             $returnstring = '';
289             foreach ( $string as $astring )
290                 $returnstring .= $this->buildStringToTranslateInSmarty($astring);
291             return $returnstring;
292         }
293         return "{/literal}{sugar_translate label='$string' module='{$this->sugarbean->module_dir}' for_js=true}{literal}";
294     }
295
296     protected function getRequiredString($required)
297     {
298         if (is_string($required) && strtolower($required) == "false")
299         {
300             return "false";
301         }
302         return empty($required) ? "false" : "true";
303     }
304 }
305 ?>