homepage-functions.php 2.3 KB

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