moddashboard.class.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /*
  3. * This file is part of MODX Revolution.
  4. *
  5. * Copyright (c) MODX, LLC. All Rights Reserved.
  6. *
  7. * For complete copyright and license information, see the COPYRIGHT and LICENSE
  8. * files found in the top-level directory of this distribution.
  9. */
  10. /**
  11. * @package modx
  12. * @subpackage mysql
  13. */
  14. class modDashboard extends xPDOSimpleObject {
  15. /**
  16. * Get the default MODX dashboard
  17. * @static
  18. * @param xPDO $xpdo A reference to an xPDO instance
  19. * @return An|null|object
  20. */
  21. public static function getDefaultDashboard(xPDO &$xpdo) {
  22. /** @var modDashboard $defaultDashboard */
  23. $defaultDashboard = $xpdo->getObject('modDashboard',array(
  24. 'id' => 1,
  25. ));
  26. if (empty($defaultDashboard)) {
  27. $defaultDashboard = $xpdo->getObject('modDashboard',array(
  28. 'name' => 'Default',
  29. ));
  30. }
  31. return $defaultDashboard;
  32. }
  33. /**
  34. * Override xPDOObject::remove() to revert to the default dashboard any user groups using this Dashboard
  35. *
  36. * @see xPDOObject::remove()
  37. * @param array $ancestors
  38. * @return boolean
  39. */
  40. public function remove(array $ancestors= array ()) {
  41. $dashboardId = $this->get('id');
  42. $removed = parent::remove($ancestors);
  43. if ($removed) {
  44. $defaultDashboard = modDashboard::getDefaultDashboard($this->xpdo);
  45. if (empty($defaultDashboard)) {
  46. /** @var modDashboard $defaultDashboard */
  47. $defaultDashboard = $this->xpdo->newObject('modDashboard');
  48. $defaultDashboard->set('id',0);
  49. }
  50. $userGroups = $this->xpdo->getCollection('modUserGroup',array(
  51. 'dashboard' => $dashboardId,
  52. ));
  53. /** @var modUserGroup $userGroup */
  54. foreach ($userGroups as $userGroup) {
  55. $userGroup->set('dashboard',$defaultDashboard->get('id'));
  56. $userGroup->save();
  57. }
  58. }
  59. return $removed;
  60. }
  61. /**
  62. * Render the Dashboard
  63. *
  64. * @param modManagerController $controller
  65. * @return string
  66. */
  67. public function render(modManagerController $controller) {
  68. $c = $this->xpdo->newQuery('modDashboardWidgetPlacement');
  69. $c->where(array(
  70. 'dashboard' => $this->get('id'),
  71. ));
  72. $c->sortby($this->xpdo->escape('rank'), 'ASC');
  73. $placements = $this->getMany('Placements', $c);
  74. $output = array();
  75. /** @var modDashboardWidgetPlacement $placement */
  76. foreach ($placements as $placement) {
  77. /** @var modDashboardWidget $widget */
  78. $widget = $placement->getOne('Widget');
  79. if ($widget) {
  80. $content = $widget->getContent($controller);
  81. if (!empty($content)) {
  82. $output[] = $content;
  83. }
  84. }
  85. }
  86. return implode("\n", $output);
  87. }
  88. }