]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Administration/Common.php
Release 6.1.4
[Github/sugarcrm.git] / modules / Administration / Common.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
41
42 require_once('include/utils/array_utils.php');
43
44 /**
45  * @return bool
46  * @desc Creates the include language directory under the custom directory.
47  */
48 function create_include_lang_dir()
49 {
50    
51         if(!is_dir("custom/include/language"))
52            return sugar_mkdir("custom/include/language", null, true);
53
54    return true;
55 }
56
57 /**
58  * @return bool
59  * @param module string
60  * @desc Creates the module's language directory under the custom directory.
61  */
62 function create_module_lang_dir($module)
63 {
64    
65         if(!is_dir("custom/modules/$module/language"))
66        return sugar_mkdir("custom/modules/$module/language", null, true);
67        
68    return true;
69 }
70
71 /**
72  * @return string&
73  * @param the_array array, language string, module string
74  * @desc Returns the contents of the customized language pack.
75  */
76 function &create_field_lang_pak_contents($old_contents, $key, $value, $language, $module)
77 {
78         if(!empty($old_contents))
79         { 
80                 
81                 $old_contents = preg_replace("'[^\[\n\r]+\[\'{$key}\'\][^\;]+;[\ \r\n]*'i", '', $old_contents);
82                 $contents = str_replace("\n?>","\n\$mod_strings['{$key}'] = '$value';\n?>", $old_contents);
83                 
84         
85         }
86         else
87         {
88         $contents = "<?php\n"
89                         . '// Creation date: ' . date('Y-m-d H:i:s') . "\n"
90                         . "// Module: $module\n"
91                         . "// Language: $language\n\n"
92                         . "\$mod_strings['$key'] = '$value';"
93                         . "\n?>";
94         }
95
96    return $contents;
97 }
98
99 /**
100  * @return string&
101  * @param the_array array, language string
102  * @desc Returns the contents of the customized language pack.
103  */
104 function &create_dropdown_lang_pak_contents(&$the_array, $language)
105 {
106    $contents = "<?php\n" .
107                '// ' . date('Y-m-d H:i:s') . "\n" .
108                "// Language: $language\n\n" .
109                '$app_list_strings = ' .
110                var_export($the_array, true) .
111                ";\n?>";
112
113    return $contents;
114 }
115
116 /**
117  * @return bool
118  * @param module string, key string, value string
119  * @desc Wrapper function that will create a field label for every language.
120  */
121 function create_field_label_all_lang($module, $key, $value, $overwrite = false)
122 {
123    $languages = get_languages();
124    $return_value = false;
125
126    foreach($languages as $lang_key => $lang_value)
127    {
128       $return_value = create_field_label($module, $lang_key, $key, $value, $overwrite);
129       if(!$return_value)
130       {
131          break;
132       }
133    }
134
135    return $return_value;
136 }
137
138 /**
139  * @return bool
140  * @param module string, language string, key string, value string
141  * @desc Returns true if new field label can be created, false otherwise.
142  *       Probable reason for returning false: new_field_key already exists.
143  */
144 function create_field_label($module, $language, $key, $value, $overwrite=false)
145 {
146    $return_value = false;
147    $mod_strings = return_module_language($language, $module);
148
149    if(isset($mod_strings[$key]) && !$overwrite)
150    {
151       $GLOBALS['log']->info("Tried to create a key that already exists: $key");
152    }
153    else
154    {
155       $mod_strings = array_merge($mod_strings, array($key => $value));
156       $dirname = "custom/modules/$module/language";
157       $dir_exists = is_dir($dirname);
158
159       if(!$dir_exists)
160       {
161         
162          $dir_exists = create_module_lang_dir($module);
163       }
164
165       if($dir_exists)
166       {
167          $filename = "$dirname/$language.lang.php";
168                 if(is_file($filename) && filesize($filename) > 0){
169                         $handle = sugar_fopen($filename, 'rb');
170                                 $old_contents = fread($handle, filesize($filename));
171                                 fclose($handle);
172                 }else{
173                         $old_contents = '';     
174                 }
175                         $handle = sugar_fopen($filename, 'wb');
176          
177
178          if($handle)
179          {
180             $contents =create_field_lang_pak_contents($old_contents, $key,
181                                         $value, $language, $module);
182
183             if(fwrite($handle, $contents))
184             {
185                $return_value = true;
186                $GLOBALS['log']->info("Successful write to: $filename");
187             }
188
189             fclose($handle);
190          }
191          else
192          {
193             $GLOBALS['log']->info("Unable to write edited language pak to file: $filename");
194          }
195       }
196       else
197       {
198           $GLOBALS['log']->info("Unable to create dir: $dirname");
199       }
200    }
201
202    return $return_value;
203 }
204
205 /**
206  * @return bool
207  * @param dropdown_name string
208  * @desc Wrapper function that creates a dropdown type for all languages.
209  */
210 function create_dropdown_type_all_lang($dropdown_name)
211 {
212    $languages = get_languages();
213    $return_value = false;
214
215    foreach($languages as $lang_key => $lang_value)
216    {
217       $return_value = create_dropdown_type($dropdown_name, $lang_key);
218       if(!$return_value)
219       {
220          break;
221       }
222    }
223
224    return $return_value;
225 }
226
227 /**
228  * @return bool
229  * @param app_list_strings array
230  * @desc Saves the app_list_strings to file in the 'custom' dir.
231  */
232 function save_custom_app_list_strings_contents(&$contents, $language, $custom_dir_name = '')
233 {
234         $return_value = false;
235    $dirname = 'custom/include/language';
236    if(!empty($custom_dir_name))
237                 $dirname = $custom_dir_name;
238
239    $dir_exists = is_dir($dirname);
240
241    if(!$dir_exists)
242    {
243       $dir_exists = create_include_lang_dir($dirname);
244    }
245
246    if($dir_exists)
247    {
248       $filename = "$dirname/$language.lang.php";
249       $handle = @sugar_fopen($filename, 'wt');
250
251       if($handle)
252       {
253          if(fwrite($handle, $contents))
254          {
255             $return_value = true;
256             $GLOBALS['log']->info("Successful write to: $filename");
257          }
258
259          fclose($handle);
260       }
261       else
262       {
263          $GLOBALS['log']->info("Unable to write edited language pak to file: $filename");
264       }
265    }
266    else
267    {
268       $GLOBALS['log']->info("Unable to create dir: $dirname");
269    }
270 if($return_value){
271         $cache_key = 'app_list_strings.'.$language;
272         sugar_cache_clear($cache_key);
273    }
274
275         return $return_value;
276 }
277
278 /**
279  * @return bool
280  * @param app_list_strings array
281  * @desc Saves the app_list_strings to file in the 'custom' dir.
282  */
283 function save_custom_app_list_strings(&$app_list_strings, $language)
284 {
285         $return_value = false;
286         $dirname = 'custom/include/language';
287         
288    $dir_exists = is_dir($dirname);
289
290    if(!$dir_exists)
291    {
292       $dir_exists = create_include_lang_dir($dirname);
293    }
294
295    if($dir_exists)
296    {
297       $filename = "$dirname/$language.lang.php";
298       $handle = @sugar_fopen($filename, 'wt');
299
300       if($handle)
301       {
302          $contents =create_dropdown_lang_pak_contents($app_list_strings,
303                                                 $language);
304
305          if(fwrite($handle, $contents))
306          {
307             $return_value = true;
308             $GLOBALS['log']->info("Successful write to: $filename");
309          }
310
311          fclose($handle);
312       }
313       else
314       {
315          $GLOBALS['log']->info("Unable to write edited language pak to file: $filename");
316       }
317    }
318    else
319    {
320       $GLOBALS['log']->info("Unable to create dir: $dirname");
321    }
322 if($return_value){
323         $cache_key = 'app_list_strings.'.$language;
324         sugar_cache_clear($cache_key);
325    }
326
327         return $return_value;
328 }
329
330 function return_custom_app_list_strings_file_contents($language, $custom_filename = '')
331 {
332         $contents = '';
333         
334         $filename = "custom/include/language/$language.lang.php";
335         if(!empty($custom_filename))
336                 $filename = $custom_filename;
337         
338         if (is_file($filename))
339         {
340                 $contents = file_get_contents($filename);
341         }
342
343         return $contents;
344 }
345
346 /**
347  * @return bool
348  * @param dropdown_name string, language string
349  * @desc Creates a new dropdown type.
350  */
351 function create_dropdown_type($dropdown_name, $language)
352 {
353    $return_value = false;
354    $app_list_strings = return_app_list_strings_language($language);
355
356    if(isset($app_list_strings[$dropdown_name]))
357    {
358       $GLOBALS['log']->info("Tried to create a dropdown list key that already exists: $dropdown_name");
359    }
360    else
361    {
362                 // get the contents of the custom app list strings file
363                 $contents = return_custom_app_list_strings_file_contents($language);
364
365                 // add the new dropdown_name to it
366                 if($contents == '')
367                 {
368                         $new_contents = "<?php\n\$app_list_strings['$dropdown_name'] = array(''=>'');\n?>";
369                 }
370                 else
371                 {
372                         $new_contents = str_replace('?>', "\$app_list_strings['$dropdown_name'] = array(''=>'');\n?>", $contents);
373                 }
374
375                 // save the new contents to file
376                 $return_value = save_custom_app_list_strings_contents($new_contents, $language);
377    }
378
379    return $return_value;
380 }
381
382 /**
383  * @return string&
384  * @param identifier string, pairs array, first_entry string, selected_key string
385  * @desc Generates the HTML for a dropdown list.
386  */
387 function &create_dropdown_html($identifier, &$pairs, $first_entry='', $selected_key='')
388 {
389    $html = "<select name=\"$identifier\">\n";
390
391    if('' != $first_entry)
392    {
393       $html .= "<option name=\"\">$first_entry</option>\n";
394    }
395
396    foreach($pairs as $key => $value)
397    {
398       $html .= $selected_key == $key ?
399                "<option name=\"$key\" selected=\"selected\">$value</option>\n" :
400                "<option name=\"$key\">$value</option>\n";
401    }
402
403    $html .= "</select>\n";
404
405    return $html;
406 }
407
408
409 function dropdown_item_delete($dropdown_type, $language, $index)
410 {
411         $app_list_strings_to_edit = return_app_list_strings_language($language);
412    $dropdown_array =$app_list_strings_to_edit[$dropdown_type];
413         helper_dropdown_item_delete($dropdown_array, $index);
414
415         $contents = return_custom_app_list_strings_file_contents($language);
416         $new_contents = replace_or_add_dropdown_type($dropdown_type, $dropdown_array,
417                 $contents);
418
419    save_custom_app_list_strings_contents($new_contents, $language);
420 }
421
422 function helper_dropdown_item_delete(&$dropdown_array, $index)
423 {
424    // perform the delete from the array
425         $sliced_off_array = array_splice($dropdown_array, $index);
426    array_shift($sliced_off_array);
427         $dropdown_array = array_merge($dropdown_array, $sliced_off_array);
428 }
429
430 function dropdown_item_move_up($dropdown_type, $language, $index)
431 {
432         $app_list_strings_to_edit = return_app_list_strings_language($language);
433         $dropdown_array =$app_list_strings_to_edit[$dropdown_type];
434
435         if($index > 0 && $index < count($dropdown_array))
436         {
437                 $key = '';
438                 $value = '';
439                 $i = 0;
440
441                 reset($dropdown_array);
442                 while(list($k, $v) = each($dropdown_array))
443                 {
444                         if($i == $index)
445                         {
446                                 $key = $k;
447                                 $value = $v;
448                                 break;
449                         }
450
451                         $i++;
452                 }
453
454                 helper_dropdown_item_delete($dropdown_array, $index);
455                 helper_dropdown_item_insert($dropdown_array, $index - 1, $key, $value);
456
457                 // get the contents of the custom app list strings file
458                 $contents = return_custom_app_list_strings_file_contents($language);
459                 $new_contents = replace_or_add_dropdown_type($dropdown_type,
460                         $dropdown_array, $contents);
461
462                 save_custom_app_list_strings_contents($new_contents, $language);
463         }
464 }
465
466 function dropdown_item_move_down($dropdown_type, $language, $index)
467 {
468         $app_list_strings_to_edit = return_app_list_strings_language($language);
469         $dropdown_array =$app_list_strings_to_edit[$dropdown_type];
470
471         if($index >= 0 && $index < count($dropdown_array) - 1)
472         {
473                 $key = '';
474                 $value = '';
475                 $i = 0;
476
477                 reset($dropdown_array);
478                 while(list($k, $v) = each($dropdown_array))
479                 {
480                         if($i == $index)
481                         {
482                                 $key = $k;
483                                 $value = $v;
484                                 break;
485                         }
486
487                         $i++;
488                 }
489
490                 helper_dropdown_item_delete($dropdown_array, $index);
491                 helper_dropdown_item_insert($dropdown_array, $index + 1, $key, $value);
492
493                 // get the contents of the custom app list strings file
494                 $contents = return_custom_app_list_strings_file_contents($language);
495                 $new_contents = replace_or_add_dropdown_type($dropdown_type,
496                         $dropdown_array, $contents);
497         
498                 save_custom_app_list_strings_contents($new_contents, $language);
499         }
500 }
501
502 function dropdown_item_insert($dropdown_type, $language, $index, $key, $value)
503 {
504         $app_list_strings_to_edit = return_app_list_strings_language($language);
505         $dropdown_array =$app_list_strings_to_edit[$dropdown_type];
506         helper_dropdown_item_insert($dropdown_array, $index, $key, $value);
507
508         // get the contents of the custom app list strings file
509         $contents = return_custom_app_list_strings_file_contents($language);
510         $new_contents = replace_or_add_dropdown_type($dropdown_type,
511                 $dropdown_array, $contents);
512
513    save_custom_app_list_strings_contents($new_contents, $language);
514 }
515
516 function helper_dropdown_item_insert(&$dropdown_array, $index, $key, $value)
517 {
518         $pair = array($key => $value);
519         if($index <= 0)
520         {
521                 $dropdown_array = array_merge($pair, $dropdown_array);
522         }
523         if($index >= count($dropdown_array))
524         {
525                 $dropdown_array = array_merge($dropdown_array, $pair);
526         }
527         else
528         {
529                 $sliced_off_array = array_splice($dropdown_array, $index);
530                 $dropdown_array = array_merge($dropdown_array, $pair);
531                 $dropdown_array = array_merge($dropdown_array, $sliced_off_array);
532         }
533 }
534
535 function dropdown_item_edit($dropdown_type, $language, $key, $value)
536 {
537         $app_list_strings_to_edit = return_app_list_strings_language($language);
538         $dropdown_array =$app_list_strings_to_edit[$dropdown_type];
539
540         $dropdown_array[$key] = $value;
541
542         $contents = return_custom_app_list_strings_file_contents($language);
543
544         // get the contents of the custom app list strings file
545         $new_contents = replace_or_add_dropdown_type($dropdown_type,
546                 $dropdown_array, $contents);
547
548    save_custom_app_list_strings_contents($new_contents, $language);
549 }
550
551 function replace_or_add_dropdown_type($dropdown_type, &$dropdown_array,
552    &$file_contents)
553 {
554         $new_contents = "<?php\n?>";
555         $new_entry = override_value_to_string('app_list_strings',
556                 $dropdown_type, $dropdown_array);
557
558         if(empty($file_contents))
559         {
560                 // empty file, must create the php tags
561         $new_contents = "<?php\n$new_entry\n?>";
562         }
563         else
564         {
565                 // existing file, try to replace
566                 $new_contents = replace_dropdown_type($dropdown_type,
567                         $dropdown_array, $file_contents);
568
569                 $new_contents = dropdown_duplicate_check($dropdown_type, $new_contents);
570
571                 if($new_contents == $file_contents)
572                 {
573                         // replace failed, append to end of file
574                         $new_contents = str_replace("?>", '', $file_contents); 
575                         $new_contents .= "\n$new_entry\n?>";
576                 }
577         }
578
579         return $new_contents;
580 }
581
582 function replace_or_add_app_string($name, $value,
583    &$file_contents)
584 {
585         $new_contents = "<?php\n?>";
586         $new_entry = override_value_to_string('app_strings',
587                 $name, $value);
588
589         if(empty($file_contents))
590         {
591                 // empty file, must create the php tags
592         $new_contents = "<?php\n$new_entry\n?>";
593         }
594         else
595         {
596                 // existing file, try to replace
597                 $new_contents = replace_app_string($name,
598                         $value, $file_contents);
599
600                 $new_contents = app_string_duplicate_check($name, $new_contents);
601
602                 if($new_contents == $file_contents)
603                 {
604                         // replace failed, append to end of file
605                         $new_contents = str_replace("?>", '', $file_contents);
606                         $new_contents .= "\n$new_entry\n?>";
607                 }
608         }
609
610         return $new_contents;
611 }
612
613
614 function dropdown_duplicate_check($dropdown_type, &$file_contents)
615 {
616
617         if(!empty($dropdown_type) &&
618                 !empty($file_contents))
619         {
620                 $pattern = '/\$app_list_strings\[\''. $dropdown_type .
621                         '\'\][\ ]*=[\ ]*array[\ ]*\([^\)]*\)[\ ]*;/';
622
623                 $result = array();
624                 preg_match_all($pattern, $file_contents, $result);
625                 
626                 if(count($result[0]) > 1)
627                 {
628                         $new_entry = $result[0][0];
629                         $new_contents = preg_replace($pattern, '', $file_contents);
630                         
631                         // Append the new entry.
632                         $new_contents = str_replace("?>", '', $new_contents);
633                         $new_contents .= "\n$new_entry\n?>";
634                         return $new_contents;
635                 }
636                 
637                 return $file_contents;
638         }
639         
640         return $file_contents;
641         
642 }
643
644 function replace_dropdown_type($dropdown_type, &$dropdown_array,
645         &$file_contents)
646 {
647         $new_contents = $file_contents;
648
649         if(!empty($dropdown_type) &&
650                 is_array($dropdown_array) &&
651                 !empty($file_contents))
652         {
653                 $pattern = '/\$app_list_strings\[\''. $dropdown_type .
654                         '\'\][\ ]*=[\ ]*array[\ ]*\([^\)]*\)[\ ]*;/';
655                 $replacement = override_value_to_string('app_list_strings',
656                         $dropdown_type, $dropdown_array);
657                 $new_contents = preg_replace($pattern, $replacement, $file_contents, 1);
658         }
659
660         return $new_contents;
661 }
662
663 function replace_app_string($name, $value,
664         &$file_contents)
665 {
666         $new_contents = $file_contents;
667
668         if(!empty($name) &&
669                 is_string($value) &&
670                 !empty($file_contents))
671         {
672                 $pattern = '/\$app_strings\[\''. $name .'\'\][\ ]*=[\ ]*\'[^\']*\'[\ ]*;/';
673                 $replacement = override_value_to_string('app_strings',
674                         $name, $value);
675                 $new_contents = preg_replace($pattern, $replacement, $file_contents, 1);
676         }
677
678         return $new_contents;
679 }
680
681 function app_string_duplicate_check($name, &$file_contents)
682 {
683
684         if(!empty($name) &&
685                 !empty($file_contents))
686         {
687                 $pattern = '/\$app_strings\[\''. $name .'\'\][\ ]*=[\ ]*\'[^\']*\'[\ ]*;/';
688                 
689                 $result = array();
690                 preg_match_all($pattern, $file_contents, $result);
691         
692                 if(count($result[0]) > 1)
693                 {
694                         $new_entry = $result[0][0];
695                         $new_contents = preg_replace($pattern, '', $file_contents);
696
697                         // Append the new entry.
698                         $new_contents = str_replace("?>", '', $new_contents);
699                         $new_contents .= "\n$new_entry\n?>";
700                         return $new_contents;
701                 }
702                 return $file_contents;
703         }
704         
705         return $file_contents;
706         
707 }
708
709 ?>