syncparams.controller.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. if ($action == 'syncall') {
  9. exit;
  10. $keys = array_keys($_REQUEST);
  11. $selection = preg_grep('/sel_.*/', $keys);
  12. foreach ($selection as $selkey) {
  13. $settingId = str_replace('sel_', '', $selkey);
  14. if (!is_numeric($settingId)) {
  15. continue;
  16. }
  17. $value = $_REQUEST[$selkey];
  18. $setting = api_get_settings_params_simple(array('id' => $settingId));
  19. $params = array(
  20. 'title' => $setting['title'],
  21. 'variable' => $setting['variable'],
  22. 'subkey' => $setting['subkey'],
  23. 'category' => $setting['category'],
  24. 'access_url' => $setting['access_url'],
  25. );
  26. foreach ($vchamilos as $vcid => $chm) {
  27. $table = $chm['main_database'].".settings_current ";
  28. $sql = " SELECT * FROM $table
  29. WHERE
  30. variable = '{{$setting['variable']}}' AND
  31. access_url = '{$setting['access_url']}'
  32. ";
  33. $result = Database::query($sql);
  34. if (Database::num_rows($result)) {
  35. $sql = "UPDATE $table SET
  36. selected_value = '$value'
  37. WHERE id = $settingId";
  38. Database::query($sql);
  39. }
  40. //$DB->set_field('settings_current', 'selected_value', $value, $params, 'id', $chm->main_database);
  41. }
  42. }
  43. }
  44. if ($action == 'syncthis') {
  45. $settingId = isset($_GET['settingid']) ? $_GET['settingid'] : '';
  46. if (is_numeric($settingId)) {
  47. $delifempty = isset($_REQUEST['del']) ? $_REQUEST['del'] : '';
  48. $value = $_REQUEST['value'];
  49. // Getting the local setting record.
  50. $setting = api_get_settings_params_simple(array('id' => $settingId));
  51. $params = array(
  52. 'access_url_changeable' => $setting['access_url_changeable'],
  53. 'title' => $setting['title'],
  54. 'variable' => $setting['variable'],
  55. 'subkey' => $setting['subkey'],
  56. 'category' => $setting['category'],
  57. 'type' => $setting['type'],
  58. 'comment' => $setting['comment'],
  59. 'access_url' => $setting['access_url'],
  60. );
  61. $errors = '';
  62. foreach ($vchamilos as $vcid => $chm) {
  63. $table = "settings_current";
  64. $config = new \Doctrine\DBAL\Configuration();
  65. $connectionParams = array(
  66. 'dbname' => $chm['main_database'],
  67. 'user' => $chm['db_user'],
  68. 'password' => $chm['db_password'],
  69. 'host' => $chm['db_host'],
  70. 'driver' => 'pdo_mysql',
  71. );
  72. $connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
  73. try {
  74. if ($delifempty && empty($value)) {
  75. $sql = "DELETE FROM $table
  76. WHERE
  77. selected_value = '$value' AND
  78. variable = '{{$setting['variable']}}' AND
  79. access_url = '{$setting['access_url']}'
  80. ";
  81. $connection->executeQuery($sql);
  82. $case = "delete";
  83. } else {
  84. $sql = " SELECT * FROM $table
  85. WHERE
  86. variable = '".$setting['variable']."' AND
  87. access_url = '{$setting['access_url']}'
  88. ";
  89. $result = $connection->fetchAll($sql);
  90. if (!empty($result)) {
  91. $sql = "UPDATE $table SET selected_value = '$value' WHERE id = $settingId";
  92. $connection->executeQuery($sql);
  93. } else {
  94. $connection->insert($table, $params);
  95. }
  96. }
  97. } catch (Exception $e) {
  98. echo $e->getMessage();
  99. }
  100. }
  101. return $errors;
  102. } else {
  103. return "Bad ID. Non numeric";
  104. }
  105. }
  106. return 0;