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