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