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.
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.
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
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
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.
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.
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 ********************************************************************************/
42 require_once('include/utils/array_utils.php');
46 * @desc Creates the include language directory under the custom directory.
48 function create_include_lang_dir()
51 if(!is_dir("custom/include/language"))
52 return sugar_mkdir("custom/include/language", null, true);
59 * @param module string
60 * @desc Creates the module's language directory under the custom directory.
62 function create_module_lang_dir($module)
65 if(!is_dir("custom/modules/$module/language"))
66 return sugar_mkdir("custom/modules/$module/language", null, true);
73 * @param the_array array, language string, module string
74 * @desc Returns the contents of the customized language pack.
76 function &create_field_lang_pak_contents($old_contents, $key, $value, $language, $module)
78 if(!empty($old_contents))
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);
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';"
101 * @param the_array array, language string
102 * @desc Returns the contents of the customized language pack.
104 function &create_dropdown_lang_pak_contents(&$the_array, $language)
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) .
118 * @param module string, key string, value string
119 * @desc Wrapper function that will create a field label for every language.
121 function create_field_label_all_lang($module, $key, $value, $overwrite = false)
123 $languages = get_languages();
124 $return_value = false;
126 foreach($languages as $lang_key => $lang_value)
128 $return_value = create_field_label($module, $lang_key, $key, $value, $overwrite);
135 return $return_value;
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.
144 function create_field_label($module, $language, $key, $value, $overwrite=false)
146 $return_value = false;
147 $mod_strings = return_module_language($language, $module);
149 if(isset($mod_strings[$key]) && !$overwrite)
151 $GLOBALS['log']->info("Tried to create a key that already exists: $key");
155 $mod_strings = array_merge($mod_strings, array($key => $value));
156 $dirname = "custom/modules/$module/language";
157 $dir_exists = is_dir($dirname);
162 $dir_exists = create_module_lang_dir($module);
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));
175 $handle = sugar_fopen($filename, 'wb');
180 $contents =create_field_lang_pak_contents($old_contents, $key,
181 $value, $language, $module);
183 if(fwrite($handle, $contents))
185 $return_value = true;
186 $GLOBALS['log']->info("Successful write to: $filename");
193 $GLOBALS['log']->info("Unable to write edited language pak to file: $filename");
198 $GLOBALS['log']->info("Unable to create dir: $dirname");
202 return $return_value;
207 * @param dropdown_name string
208 * @desc Wrapper function that creates a dropdown type for all languages.
210 function create_dropdown_type_all_lang($dropdown_name)
212 $languages = get_languages();
213 $return_value = false;
215 foreach($languages as $lang_key => $lang_value)
217 $return_value = create_dropdown_type($dropdown_name, $lang_key);
224 return $return_value;
229 * @param app_list_strings array
230 * @desc Saves the app_list_strings to file in the 'custom' dir.
232 function save_custom_app_list_strings_contents(&$contents, $language, $custom_dir_name = '')
234 $return_value = false;
235 $dirname = 'custom/include/language';
236 if(!empty($custom_dir_name))
237 $dirname = $custom_dir_name;
239 $dir_exists = is_dir($dirname);
243 $dir_exists = create_include_lang_dir($dirname);
248 $filename = "$dirname/$language.lang.php";
249 $handle = @sugar_fopen($filename, 'wt');
253 if(fwrite($handle, $contents))
255 $return_value = true;
256 $GLOBALS['log']->info("Successful write to: $filename");
263 $GLOBALS['log']->info("Unable to write edited language pak to file: $filename");
268 $GLOBALS['log']->info("Unable to create dir: $dirname");
271 $cache_key = 'app_list_strings.'.$language;
272 sugar_cache_clear($cache_key);
275 return $return_value;
280 * @param app_list_strings array
281 * @desc Saves the app_list_strings to file in the 'custom' dir.
283 function save_custom_app_list_strings(&$app_list_strings, $language)
285 $return_value = false;
286 $dirname = 'custom/include/language';
288 $dir_exists = is_dir($dirname);
292 $dir_exists = create_include_lang_dir($dirname);
297 $filename = "$dirname/$language.lang.php";
298 $handle = @sugar_fopen($filename, 'wt');
302 $contents =create_dropdown_lang_pak_contents($app_list_strings,
305 if(fwrite($handle, $contents))
307 $return_value = true;
308 $GLOBALS['log']->info("Successful write to: $filename");
315 $GLOBALS['log']->info("Unable to write edited language pak to file: $filename");
320 $GLOBALS['log']->info("Unable to create dir: $dirname");
323 $cache_key = 'app_list_strings.'.$language;
324 sugar_cache_clear($cache_key);
327 return $return_value;
330 function return_custom_app_list_strings_file_contents($language, $custom_filename = '')
334 $filename = "custom/include/language/$language.lang.php";
335 if(!empty($custom_filename))
336 $filename = $custom_filename;
338 if (is_file($filename))
340 $contents = file_get_contents($filename);
348 * @param dropdown_name string, language string
349 * @desc Creates a new dropdown type.
351 function create_dropdown_type($dropdown_name, $language)
353 $return_value = false;
354 $app_list_strings = return_app_list_strings_language($language);
356 if(isset($app_list_strings[$dropdown_name]))
358 $GLOBALS['log']->info("Tried to create a dropdown list key that already exists: $dropdown_name");
362 // get the contents of the custom app list strings file
363 $contents = return_custom_app_list_strings_file_contents($language);
365 // add the new dropdown_name to it
368 $new_contents = "<?php\n\$app_list_strings['$dropdown_name'] = array(''=>'');\n?>";
372 $new_contents = str_replace('?>', "\$app_list_strings['$dropdown_name'] = array(''=>'');\n?>", $contents);
375 // save the new contents to file
376 $return_value = save_custom_app_list_strings_contents($new_contents, $language);
379 return $return_value;
384 * @param identifier string, pairs array, first_entry string, selected_key string
385 * @desc Generates the HTML for a dropdown list.
387 function &create_dropdown_html($identifier, &$pairs, $first_entry='', $selected_key='')
389 $html = "<select name=\"$identifier\">\n";
391 if('' != $first_entry)
393 $html .= "<option name=\"\">$first_entry</option>\n";
396 foreach($pairs as $key => $value)
398 $html .= $selected_key == $key ?
399 "<option name=\"$key\" selected=\"selected\">$value</option>\n" :
400 "<option name=\"$key\">$value</option>\n";
403 $html .= "</select>\n";
409 function dropdown_item_delete($dropdown_type, $language, $index)
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);
415 $contents = return_custom_app_list_strings_file_contents($language);
416 $new_contents = replace_or_add_dropdown_type($dropdown_type, $dropdown_array,
419 save_custom_app_list_strings_contents($new_contents, $language);
422 function helper_dropdown_item_delete(&$dropdown_array, $index)
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);
430 function dropdown_item_move_up($dropdown_type, $language, $index)
432 $app_list_strings_to_edit = return_app_list_strings_language($language);
433 $dropdown_array =$app_list_strings_to_edit[$dropdown_type];
435 if($index > 0 && $index < count($dropdown_array))
441 reset($dropdown_array);
442 while(list($k, $v) = each($dropdown_array))
454 helper_dropdown_item_delete($dropdown_array, $index);
455 helper_dropdown_item_insert($dropdown_array, $index - 1, $key, $value);
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);
462 save_custom_app_list_strings_contents($new_contents, $language);
466 function dropdown_item_move_down($dropdown_type, $language, $index)
468 $app_list_strings_to_edit = return_app_list_strings_language($language);
469 $dropdown_array =$app_list_strings_to_edit[$dropdown_type];
471 if($index >= 0 && $index < count($dropdown_array) - 1)
477 reset($dropdown_array);
478 while(list($k, $v) = each($dropdown_array))
490 helper_dropdown_item_delete($dropdown_array, $index);
491 helper_dropdown_item_insert($dropdown_array, $index + 1, $key, $value);
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);
498 save_custom_app_list_strings_contents($new_contents, $language);
502 function dropdown_item_insert($dropdown_type, $language, $index, $key, $value)
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);
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);
513 save_custom_app_list_strings_contents($new_contents, $language);
516 function helper_dropdown_item_insert(&$dropdown_array, $index, $key, $value)
518 $pair = array($key => $value);
521 $dropdown_array = array_merge($pair, $dropdown_array);
523 if($index >= count($dropdown_array))
525 $dropdown_array = array_merge($dropdown_array, $pair);
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);
535 function dropdown_item_edit($dropdown_type, $language, $key, $value)
537 $app_list_strings_to_edit = return_app_list_strings_language($language);
538 $dropdown_array =$app_list_strings_to_edit[$dropdown_type];
540 $dropdown_array[$key] = $value;
542 $contents = return_custom_app_list_strings_file_contents($language);
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);
548 save_custom_app_list_strings_contents($new_contents, $language);
551 function replace_or_add_dropdown_type($dropdown_type, &$dropdown_array,
554 $new_contents = "<?php\n?>";
555 $new_entry = override_value_to_string('app_list_strings',
556 $dropdown_type, $dropdown_array);
558 if(empty($file_contents))
560 // empty file, must create the php tags
561 $new_contents = "<?php\n$new_entry\n?>";
565 // existing file, try to replace
566 $new_contents = replace_dropdown_type($dropdown_type,
567 $dropdown_array, $file_contents);
569 $new_contents = dropdown_duplicate_check($dropdown_type, $new_contents);
571 if($new_contents == $file_contents)
573 // replace failed, append to end of file
574 $new_contents = str_replace("?>", '', $file_contents);
575 $new_contents .= "\n$new_entry\n?>";
579 return $new_contents;
582 function replace_or_add_app_string($name, $value,
585 $new_contents = "<?php\n?>";
586 $new_entry = override_value_to_string('app_strings',
589 if(empty($file_contents))
591 // empty file, must create the php tags
592 $new_contents = "<?php\n$new_entry\n?>";
596 // existing file, try to replace
597 $new_contents = replace_app_string($name,
598 $value, $file_contents);
600 $new_contents = app_string_duplicate_check($name, $new_contents);
602 if($new_contents == $file_contents)
604 // replace failed, append to end of file
605 $new_contents = str_replace("?>", '', $file_contents);
606 $new_contents .= "\n$new_entry\n?>";
610 return $new_contents;
614 function dropdown_duplicate_check($dropdown_type, &$file_contents)
617 if(!empty($dropdown_type) &&
618 !empty($file_contents))
620 $pattern = '/\$app_list_strings\[\''. $dropdown_type .
621 '\'\][\ ]*=[\ ]*array[\ ]*\([^\)]*\)[\ ]*;/';
624 preg_match_all($pattern, $file_contents, $result);
626 if(count($result[0]) > 1)
628 $new_entry = $result[0][0];
629 $new_contents = preg_replace($pattern, '', $file_contents);
631 // Append the new entry.
632 $new_contents = str_replace("?>", '', $new_contents);
633 $new_contents .= "\n$new_entry\n?>";
634 return $new_contents;
637 return $file_contents;
640 return $file_contents;
644 function replace_dropdown_type($dropdown_type, &$dropdown_array,
647 $new_contents = $file_contents;
649 if(!empty($dropdown_type) &&
650 is_array($dropdown_array) &&
651 !empty($file_contents))
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);
660 return $new_contents;
663 function replace_app_string($name, $value,
666 $new_contents = $file_contents;
670 !empty($file_contents))
672 $pattern = '/\$app_strings\[\''. $name .'\'\][\ ]*=[\ ]*\'[^\']*\'[\ ]*;/';
673 $replacement = override_value_to_string('app_strings',
675 $new_contents = preg_replace($pattern, $replacement, $file_contents, 1);
678 return $new_contents;
681 function app_string_duplicate_check($name, &$file_contents)
685 !empty($file_contents))
687 $pattern = '/\$app_strings\[\''. $name .'\'\][\ ]*=[\ ]*\'[^\']*\'[\ ]*;/';
690 preg_match_all($pattern, $file_contents, $result);
692 if(count($result[0]) > 1)
694 $new_entry = $result[0][0];
695 $new_contents = preg_replace($pattern, '', $file_contents);
697 // Append the new entry.
698 $new_contents = str_replace("?>", '', $new_contents);
699 $new_contents .= "\n$new_entry\n?>";
700 return $new_contents;
702 return $file_contents;
705 return $file_contents;