syncparams.controller.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. api_protect_admin_script();
  4. $sql = "SELECT * FROM vchamilo";
  5. $result = Database::query($sql);
  6. $vchamilos = Database::store_result($result, 'ASSOC');
  7. // propagate in all known vchamilos a setting
  8. switch ($action) {
  9. case 'syncall':
  10. exit;
  11. $keys = array_keys($_REQUEST);
  12. $selection = preg_grep('/sel_.*/', $keys);
  13. foreach ($selection as $selkey) {
  14. $settingId = str_replace('sel_', '', $selkey);
  15. if (!is_numeric($settingId)) {
  16. continue;
  17. }
  18. $value = $_REQUEST[$selkey];
  19. $setting = api_get_settings_params_simple(['id = ?' => $settingId]);
  20. $params = [
  21. 'title' => $setting['title'],
  22. 'variable' => $setting['variable'],
  23. 'subkey' => $setting['subkey'],
  24. 'category' => $setting['category'],
  25. 'access_url' => $setting['access_url'],
  26. ];
  27. foreach ($vchamilos as $chm) {
  28. $table = $chm['main_database'].".settings_current ";
  29. $sql = " SELECT * FROM $table
  30. WHERE
  31. variable = '{{$setting['variable']}}' AND
  32. access_url = '{$setting['access_url']}'
  33. ";
  34. $result = Database::query($sql);
  35. if (Database::num_rows($result)) {
  36. $sql = "UPDATE $table SET
  37. selected_value = '$value'
  38. WHERE id = $settingId";
  39. Database::query($sql);
  40. }
  41. }
  42. }
  43. break;
  44. case 'syncthis':
  45. $settingId = isset($_GET['settingid']) ? (int) $_GET['settingid'] : '';
  46. if (!empty($settingId) && is_numeric($settingId)) {
  47. $deleteIfEmpty = isset($_REQUEST['del']) ? $_REQUEST['del'] : '';
  48. $value = $_REQUEST['value'];
  49. // Getting the local setting record.
  50. $setting = api_get_settings_params_simple(['id = ?' => $settingId]);
  51. if (empty($setting)) {
  52. return 0;
  53. }
  54. $params = [
  55. 'access_url_changeable' => $setting['access_url_changeable'],
  56. 'title' => $setting['title'],
  57. 'variable' => $setting['variable'],
  58. 'subkey' => $setting['subkey'],
  59. 'category' => $setting['category'],
  60. 'type' => $setting['type'],
  61. 'comment' => $setting['comment'],
  62. 'access_url' => $setting['access_url'],
  63. ];
  64. $errors = '';
  65. foreach ($vchamilos as $instance) {
  66. $table = 'settings_current';
  67. $config = new \Doctrine\DBAL\Configuration();
  68. $connectionParams = [
  69. 'dbname' => $instance['main_database'],
  70. 'user' => $instance['db_user'],
  71. 'password' => $instance['db_password'],
  72. 'host' => $instance['db_host'],
  73. 'driver' => 'pdo_mysql',
  74. ];
  75. $connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
  76. try {
  77. $variable = $setting['variable'];
  78. $subKey = $setting['subkey'];
  79. $category = $setting['category'];
  80. $accessUrl = $setting['access_url'];
  81. if ($deleteIfEmpty && empty($value)) {
  82. $sql = "DELETE FROM $table
  83. WHERE
  84. selected_value = '$value' AND
  85. variable = '$variable' AND
  86. access_url = '$accessUrl'
  87. ";
  88. $connection->executeQuery($sql);
  89. $case = 'delete';
  90. } else {
  91. $sql = "SELECT * FROM $table
  92. WHERE
  93. variable = '$variable' AND
  94. access_url = '$accessUrl'
  95. ";
  96. $result = $connection->fetchAll($sql);
  97. if (!empty($result)) {
  98. //$sql = "UPDATE $table SET selected_value = '$value' WHERE id = $settingId";
  99. $sql = "UPDATE $table SET selected_value = '$value' WHERE variable = '$variable'";
  100. if (!empty($subKey)) {
  101. $sql .= " AND subkey = '$subKey' ";
  102. }
  103. if (!empty($category)) {
  104. $sql .= " AND category = '$category'";
  105. }
  106. $connection->executeQuery($sql);
  107. } else {
  108. $connection->insert($table, $params);
  109. }
  110. }
  111. } catch (Exception $e) {
  112. echo $e->getMessage();
  113. }
  114. }
  115. return $errors;
  116. } else {
  117. return "Bad ID. Non numeric";
  118. }
  119. break;
  120. }
  121. return 0;