PageRenderTime 48ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/upgrade/scripts/post/7_FixDropdownData.php

https://bitbucket.org/hatim_alam/sugar-8
PHP | 132 lines | 101 code | 10 blank | 21 comment | 22 complexity | 951ba61f4d359cb233c1a49b2e7e2e21 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause, Apache-2.0, MIT, BSD-2-Clause
  1. <?php
  2. /*
  3. * Your installation or use of this SugarCRM file is subject to the applicable
  4. * terms available at
  5. * http://support.sugarcrm.com/Resources/Master_Subscription_Agreements/.
  6. * If you do not agree to all of the applicable terms or do not have the
  7. * authority to bind the entity as an authorized representative, then do not
  8. * install or use this SugarCRM file.
  9. *
  10. * Copyright (C) SugarCRM Inc. All rights reserved.
  11. */
  12. /**
  13. * Move custom dropdown data from custom/include/language to custom/Extension/application/Ext/Language
  14. */
  15. class SugarUpgradeFixDropdownData extends UpgradeScript
  16. {
  17. public $order = 7100;
  18. public $type = self::UPGRADE_CUSTOM;
  19. public function run()
  20. {
  21. $originalPath = 'custom/include/language';
  22. $newPath = 'custom/Extension/application/Ext/Language';
  23. if (!version_compare($this->from_version, '7.6.1', "<")) {
  24. return;
  25. }
  26. if (!is_dir($originalPath)) {
  27. return;
  28. }
  29. if (!is_dir($newPath)) {
  30. sugar_mkdir($newPath, null, true);
  31. }
  32. foreach (glob($originalPath . '/*.lang.php') as $file) {
  33. require $file;
  34. $fileParts = explode('/', $file);
  35. $fileName = $fileParts[3];
  36. $nameParts = explode('.', $fileName);
  37. $lang = $nameParts[0];
  38. $strings = array();
  39. // if $app_list_strings exists and it's a custom DD, we want to move it to the new directory
  40. if (!empty($app_list_strings)) {
  41. foreach($app_list_strings as $name => $fields) {
  42. // $app_list_strings DD strings
  43. if (is_array($fields) && $this->isCustomField($name, $fields, $file)) {
  44. $newFileName = $newPath . '/' . $lang . '.sugar_' . $name . '.php';
  45. $this->write_to_file($newFileName, 'app_list_strings', $name, $fields, true);
  46. }
  47. // $app_list_strings non-DD strings
  48. else {
  49. $strings[$name] = $fields;
  50. }
  51. }
  52. // if it is $app_list_strings but not a DD, we want to persist that back where it was
  53. if (!empty($strings)) {
  54. unlink($file);
  55. foreach($strings as $name => $fields) {
  56. if (!file_exists($file)) {
  57. $this->write_to_file($file, 'app_list_strings', $name, $fields, true);
  58. }
  59. else {
  60. $this->write_to_file($file, 'app_list_strings', $name, $fields);
  61. }
  62. }
  63. }
  64. }
  65. // if $app_strings exists, we want to persist that back where it was
  66. if (!empty($app_strings)) {
  67. if (!empty($strings)) {
  68. foreach($app_strings as $name => $field) {
  69. $this->write_to_file($file, 'app_strings', $name, $field);
  70. }
  71. }
  72. else {
  73. unlink($file);
  74. foreach($app_strings as $name => $field) {
  75. if (!file_exists($file)) {
  76. $this->write_to_file($file, 'app_strings', $name, $field, true);
  77. }
  78. else {
  79. $this->write_to_file($file, 'app_strings', $name, $field);
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86. public function isCustomField($name, $fields, $file) {
  87. $OOBLangFile = substr($file, 7); // remove 'custom/' from the file path
  88. // OOB equivalent doesn't exist, it is a custom field
  89. if (!file_exists($OOBLangFile)) {
  90. return true;
  91. }
  92. include ($OOBLangFile);
  93. if (isset($app_list_strings)) {
  94. if (array_key_exists($name, $app_list_strings)) {
  95. if (is_array($app_list_strings[$name])) {
  96. // Check if top level key exists in $app_list_strings[$name]
  97. // it is a custom field if the key doesn't exist
  98. foreach ($fields as $key => $value) {
  99. if (!array_key_exists($key, $app_list_strings[$name])) {
  100. return true;
  101. }
  102. }
  103. }
  104. }
  105. else {
  106. return true;
  107. }
  108. }
  109. return false;
  110. }
  111. public function write_to_file($file, $string_name, $name, $field, $timeHeader = false) {
  112. $output = "";
  113. if ($timeHeader) {
  114. $output = "<?php\n" .
  115. "// created: " . date('Y-m-d H:i:s') . "\n";
  116. }
  117. $output .= "\$" . $string_name . "['" . $name . "'] = " . (is_array($field) ? var_export_helper($field) : "'" . str_replace("'", "\'", $field) . "'") . ";\n";
  118. $fp = fopen($file, 'a');
  119. fputs($fp, $output);
  120. fclose($fp);
  121. }
  122. }