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.
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 $old_contents = file_get_contents($filename);
173 $handle = sugar_fopen($filename, 'wb');
178 $contents =create_field_lang_pak_contents($old_contents, $key,
179 $value, $language, $module);
181 if(fwrite($handle, $contents))
183 $return_value = true;
184 $GLOBALS['log']->info("Successful write to: $filename");
191 $GLOBALS['log']->info("Unable to write edited language pak to file: $filename");
196 $GLOBALS['log']->info("Unable to create dir: $dirname");
200 return $return_value;
205 * @param dropdown_name string
206 * @desc Wrapper function that creates a dropdown type for all languages.
208 function create_dropdown_type_all_lang($dropdown_name)
210 $languages = get_languages();
211 $return_value = false;
213 foreach($languages as $lang_key => $lang_value)
215 $return_value = create_dropdown_type($dropdown_name, $lang_key);
222 return $return_value;
227 * @param app_list_strings array
228 * @desc Saves the app_list_strings to file in the 'custom' dir.
230 function save_custom_app_list_strings_contents(&$contents, $language, $custom_dir_name = '')
232 $return_value = false;
233 $dirname = 'custom/include/language';
234 if(!empty($custom_dir_name))
235 $dirname = $custom_dir_name;
237 $dir_exists = is_dir($dirname);
241 $dir_exists = create_include_lang_dir($dirname);
246 $filename = "$dirname/$language.lang.php";
247 $handle = @sugar_fopen($filename, 'wt');
251 if(fwrite($handle, $contents))
253 $return_value = true;
254 $GLOBALS['log']->info("Successful write to: $filename");
261 $GLOBALS['log']->info("Unable to write edited language pak to file: $filename");
266 $GLOBALS['log']->info("Unable to create dir: $dirname");
269 $cache_key = 'app_list_strings.'.$language;
270 sugar_cache_clear($cache_key);
273 return $return_value;
278 * @param app_list_strings array
279 * @desc Saves the app_list_strings to file in the 'custom' dir.
281 function save_custom_app_list_strings(&$app_list_strings, $language)
283 $return_value = false;
284 $dirname = 'custom/include/language';
286 $dir_exists = is_dir($dirname);
290 $dir_exists = create_include_lang_dir($dirname);
295 $filename = "$dirname/$language.lang.php";
296 $handle = @sugar_fopen($filename, 'wt');
300 $contents =create_dropdown_lang_pak_contents($app_list_strings,
303 if(fwrite($handle, $contents))
305 $return_value = true;
306 $GLOBALS['log']->info("Successful write to: $filename");
313 $GLOBALS['log']->info("Unable to write edited language pak to file: $filename");
318 $GLOBALS['log']->info("Unable to create dir: $dirname");
321 $cache_key = 'app_list_strings.'.$language;
322 sugar_cache_clear($cache_key);
325 return $return_value;
328 function return_custom_app_list_strings_file_contents($language, $custom_filename = '')
332 $filename = "custom/include/language/$language.lang.php";
333 if(!empty($custom_filename))
334 $filename = $custom_filename;
336 if (is_file($filename))
338 $contents = file_get_contents($filename);
346 * @param dropdown_name string, language string
347 * @desc Creates a new dropdown type.
349 function create_dropdown_type($dropdown_name, $language)
351 $return_value = false;
352 $app_list_strings = return_app_list_strings_language($language);
354 if(isset($app_list_strings[$dropdown_name]))
356 $GLOBALS['log']->info("Tried to create a dropdown list key that already exists: $dropdown_name");
360 // get the contents of the custom app list strings file
361 $contents = return_custom_app_list_strings_file_contents($language);
363 // add the new dropdown_name to it
366 $new_contents = "<?php\n\$app_list_strings['$dropdown_name'] = array(''=>'');\n?>";
370 $new_contents = str_replace('?>', "\$app_list_strings['$dropdown_name'] = array(''=>'');\n?>", $contents);
373 // save the new contents to file
374 $return_value = save_custom_app_list_strings_contents($new_contents, $language);
377 return $return_value;
382 * @param identifier string, pairs array, first_entry string, selected_key string
383 * @desc Generates the HTML for a dropdown list.
385 function &create_dropdown_html($identifier, &$pairs, $first_entry='', $selected_key='')
387 $html = "<select name=\"$identifier\">\n";
389 if('' != $first_entry)
391 $html .= "<option name=\"\">$first_entry</option>\n";
394 foreach($pairs as $key => $value)
396 $html .= $selected_key == $key ?
397 "<option name=\"$key\" selected=\"selected\">$value</option>\n" :
398 "<option name=\"$key\">$value</option>\n";
401 $html .= "</select>\n";
407 function dropdown_item_delete($dropdown_type, $language, $index)
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);
413 $contents = return_custom_app_list_strings_file_contents($language);
414 $new_contents = replace_or_add_dropdown_type($dropdown_type, $dropdown_array,
417 save_custom_app_list_strings_contents($new_contents, $language);
420 function helper_dropdown_item_delete(&$dropdown_array, $index)
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);
428 function dropdown_item_move_up($dropdown_type, $language, $index)
430 $app_list_strings_to_edit = return_app_list_strings_language($language);
431 $dropdown_array =$app_list_strings_to_edit[$dropdown_type];
433 if($index > 0 && $index < count($dropdown_array))
439 reset($dropdown_array);
440 while(list($k, $v) = each($dropdown_array))
452 helper_dropdown_item_delete($dropdown_array, $index);
453 helper_dropdown_item_insert($dropdown_array, $index - 1, $key, $value);
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);
460 save_custom_app_list_strings_contents($new_contents, $language);
464 function dropdown_item_move_down($dropdown_type, $language, $index)
466 $app_list_strings_to_edit = return_app_list_strings_language($language);
467 $dropdown_array =$app_list_strings_to_edit[$dropdown_type];
469 if($index >= 0 && $index < count($dropdown_array) - 1)
475 reset($dropdown_array);
476 while(list($k, $v) = each($dropdown_array))
488 helper_dropdown_item_delete($dropdown_array, $index);
489 helper_dropdown_item_insert($dropdown_array, $index + 1, $key, $value);
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);
496 save_custom_app_list_strings_contents($new_contents, $language);
500 function dropdown_item_insert($dropdown_type, $language, $index, $key, $value)
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);
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);
511 save_custom_app_list_strings_contents($new_contents, $language);
514 function helper_dropdown_item_insert(&$dropdown_array, $index, $key, $value)
516 $pair = array($key => $value);
519 $dropdown_array = array_merge($pair, $dropdown_array);
521 if($index >= count($dropdown_array))
523 $dropdown_array = array_merge($dropdown_array, $pair);
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);
533 function dropdown_item_edit($dropdown_type, $language, $key, $value)
535 $app_list_strings_to_edit = return_app_list_strings_language($language);
536 $dropdown_array =$app_list_strings_to_edit[$dropdown_type];
538 $dropdown_array[$key] = $value;
540 $contents = return_custom_app_list_strings_file_contents($language);
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);
546 save_custom_app_list_strings_contents($new_contents, $language);
549 function replace_or_add_dropdown_type($dropdown_type, &$dropdown_array,
552 $new_contents = "<?php\n?>";
553 $new_entry = override_value_to_string('app_list_strings',
554 $dropdown_type, $dropdown_array);
556 if(empty($file_contents))
558 // empty file, must create the php tags
559 $new_contents = "<?php\n$new_entry\n?>";
563 // existing file, try to replace
564 $new_contents = replace_dropdown_type($dropdown_type,
565 $dropdown_array, $file_contents);
567 $new_contents = dropdown_duplicate_check($dropdown_type, $new_contents);
569 if($new_contents == $file_contents)
571 // replace failed, append to end of file
572 $new_contents = str_replace("?>", '', $file_contents);
573 $new_contents .= "\n$new_entry\n?>";
577 return $new_contents;
580 function replace_or_add_app_string($name, $value,
583 $new_contents = "<?php\n?>";
584 $new_entry = override_value_to_string('app_strings',
587 if(empty($file_contents))
589 // empty file, must create the php tags
590 $new_contents = "<?php\n$new_entry\n?>";
594 // existing file, try to replace
595 $new_contents = replace_app_string($name,
596 $value, $file_contents);
598 $new_contents = app_string_duplicate_check($name, $new_contents);
600 if($new_contents == $file_contents)
602 // replace failed, append to end of file
603 $new_contents = str_replace("?>", '', $file_contents);
604 $new_contents .= "\n$new_entry\n?>";
608 return $new_contents;
612 function dropdown_duplicate_check($dropdown_type, &$file_contents)
615 if(!empty($dropdown_type) &&
616 !empty($file_contents))
618 $pattern = '/\$app_list_strings\[\''. $dropdown_type .
619 '\'\][\ ]*=[\ ]*array[\ ]*\([^\)]*\)[\ ]*;/';
622 preg_match_all($pattern, $file_contents, $result);
624 if(count($result[0]) > 1)
626 $new_entry = $result[0][0];
627 $new_contents = preg_replace($pattern, '', $file_contents);
629 // Append the new entry.
630 $new_contents = str_replace("?>", '', $new_contents);
631 $new_contents .= "\n$new_entry\n?>";
632 return $new_contents;
635 return $file_contents;
638 return $file_contents;
642 function replace_dropdown_type($dropdown_type, &$dropdown_array,
645 $new_contents = $file_contents;
647 if(!empty($dropdown_type) &&
648 is_array($dropdown_array) &&
649 !empty($file_contents))
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);
658 return $new_contents;
661 function replace_app_string($name, $value,
664 $new_contents = $file_contents;
668 !empty($file_contents))
670 $pattern = '/\$app_strings\[\''. $name .'\'\][\ ]*=[\ ]*\'[^\']*\'[\ ]*;/';
671 $replacement = override_value_to_string('app_strings',
673 $new_contents = preg_replace($pattern, $replacement, $file_contents, 1);
676 return $new_contents;
679 function app_string_duplicate_check($name, &$file_contents)
683 !empty($file_contents))
685 $pattern = '/\$app_strings\[\''. $name .'\'\][\ ]*=[\ ]*\'[^\']*\'[\ ]*;/';
688 preg_match_all($pattern, $file_contents, $result);
690 if(count($result[0]) > 1)
692 $new_entry = $result[0][0];
693 $new_contents = preg_replace($pattern, '', $file_contents);
695 // Append the new entry.
696 $new_contents = str_replace("?>", '', $new_contents);
697 $new_contents .= "\n$new_entry\n?>";
698 return $new_contents;
700 return $file_contents;
703 return $file_contents;