4
0

homepage-functions.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. trait HomepageFunctions
  3. {
  4. public function homepageCheckKeyPermissions($key, $permissions)
  5. {
  6. if (array_key_exists($key, $permissions)) {
  7. return $permissions[$key];
  8. } elseif ($key == 'all') {
  9. return $permissions;
  10. } else {
  11. return [];
  12. }
  13. }
  14. public function getHomepageSettingsList()
  15. {
  16. $methods = get_class_methods($this);
  17. $searchTerm = 'SettingsArray';
  18. return array_filter($methods, function ($k) use ($searchTerm) {
  19. return stripos($k, $searchTerm) !== false;
  20. }, 0);
  21. }
  22. public function getHomepageSettingsCombined()
  23. {
  24. $list = $this->getHomepageSettingsList();
  25. $combined = [];
  26. foreach ($list as $item) {
  27. $combined[] = $this->$item(true);
  28. }
  29. return $combined;
  30. }
  31. public function homepageItemPermissions($settings = false, $api = false)
  32. {
  33. if (!$settings) {
  34. if ($api) {
  35. $this->setAPIResponse('error', 'No settings were supplied', 422);
  36. }
  37. return false;
  38. }
  39. foreach ($settings as $type => $setting) {
  40. $settingsType = gettype($setting);
  41. switch ($type) {
  42. case 'enabled':
  43. if ($settingsType == 'string') {
  44. if (!$this->config[$setting]) {
  45. if ($api) {
  46. $this->setAPIResponse('error', $setting . ' module is not enabled', 409);
  47. }
  48. return false;
  49. }
  50. } else {
  51. foreach ($setting as $item) {
  52. if (!$this->config[$item]) {
  53. if ($api) {
  54. $this->setAPIResponse('error', $item . ' module is not enabled', 409);
  55. }
  56. return false;
  57. }
  58. }
  59. }
  60. break;
  61. case 'auth':
  62. if ($settingsType == 'string') {
  63. if (!$this->qualifyRequest($this->config[$setting])) {
  64. if ($api) {
  65. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  66. }
  67. return false;
  68. }
  69. } else {
  70. foreach ($setting as $item) {
  71. if (!$this->qualifyRequest($this->config[$item])) {
  72. if ($api) {
  73. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  74. }
  75. return false;
  76. }
  77. }
  78. }
  79. break;
  80. case 'not_empty':
  81. if ($settingsType == 'string') {
  82. if (empty($this->config[$setting])) {
  83. if ($api) {
  84. $this->setAPIResponse('error', $setting . ' was not supplied', 422);
  85. }
  86. return false;
  87. }
  88. } else {
  89. foreach ($setting as $item) {
  90. if (empty($this->config[$item])) {
  91. if ($api) {
  92. $this->setAPIResponse('error', $item . ' was not supplied', 422);
  93. }
  94. return false;
  95. }
  96. }
  97. }
  98. break;
  99. default:
  100. //return false;
  101. }
  102. }
  103. return true;
  104. }
  105. }