syncparams.controller.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. api_protect_admin_script();
  3. $sql = "SELECT * FROM vchamilo";
  4. $result = Database::query($sql);
  5. $vchamilos = Database::store_result($result);
  6. // propagate in all known vchamilos a setting
  7. if ($action == 'syncall') {
  8. $keys = array_keys($_REQUEST);
  9. $selection = preg_grep('/sel_.*/', $keys);
  10. foreach ($selection as $selkey) {
  11. $settingId = str_replace('sel_', '', $selkey);
  12. if (!is_numeric($settingId)) {
  13. continue;
  14. }
  15. $value = $_REQUEST[$selkey];
  16. $setting = api_get_settings_params_simple(array('id' => $settingId));
  17. $params = array(
  18. 'title' => $setting['title'],
  19. 'variable' => $setting['variable'],
  20. 'subkey' => $setting['subkey'],
  21. 'category' => $setting['category'],
  22. 'access_url' => $setting['access_url'],
  23. );
  24. foreach ($vchamilos as $vcid => $chm) {
  25. $table = $chm['main_database'].".settings_current ";
  26. $sql = " SELECT * FROM $table
  27. WHERE
  28. variable = '{{$setting['variable']}}' AND
  29. access_url = '{$setting['access_url']}'
  30. ";
  31. $result = Database::query($sql);
  32. if (Database::num_rows($result)) {
  33. $sql = "UPDATE $table SET
  34. selected_value = '$value'
  35. WHERE id = $settingId";
  36. Database::query($sql);
  37. }
  38. //$DB->set_field('settings_current', 'selected_value', $value, $params, 'id', $chm->main_database);
  39. }
  40. }
  41. }
  42. if ($action == 'syncthis') {
  43. $settingId = isset($_GET['settingid']) ? $_GET['settingid'] : '';
  44. if (is_numeric($settingId)) {
  45. $delifempty = isset($_REQUEST['del']) ? $_REQUEST['del'] : '';
  46. $value = $_REQUEST['value'];
  47. // Getting the local setting record.
  48. $setting = api_get_settings_params_simple(array('id' => $settingId));
  49. $params = array(
  50. 'access_url_changeable' => $setting['access_url_changeable'],
  51. 'title' => $setting['title'],
  52. 'variable' => $setting['variable'],
  53. 'subkey' => $setting['subkey'],
  54. 'category' => $setting['category'],
  55. 'type' => $setting['type'],
  56. 'comment' => $setting['comment'],
  57. 'access_url' => $setting['access_url'],
  58. );
  59. $errors = '';
  60. foreach ($vchamilos as $vcid => $chm) {
  61. $table = $chm['main_database'].".settings_current";
  62. if ($delifempty && empty($value)) {
  63. $sql = "DELETE FROM $table
  64. WHERE
  65. selected_value = '$value' AND
  66. variable = '{{$setting['variable']}}' AND
  67. access_url = '{$setting['access_url']}'
  68. ";
  69. Database::query($sql);
  70. //$res = $DB->delete_records('settings_current', $params, $chm->main_database);
  71. $case = "delete";
  72. } else {
  73. $sql = " SELECT * FROM $table
  74. WHERE
  75. variable = '".$setting['variable']."' AND
  76. access_url = '{$setting['access_url']}'
  77. ";
  78. $result = Database::query($sql);
  79. if (Database::num_rows($result)) {
  80. $sql = "UPDATE $table SET
  81. selected_value = '$value'
  82. WHERE id = $settingId";
  83. Database::query($sql);
  84. } else {
  85. Database::insert($table, $params, true);
  86. }
  87. }
  88. }
  89. return $errors;
  90. } else {
  91. return "Bad ID. Non numeric";
  92. }
  93. }
  94. return 0;