WidgetsService.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. namespace Elgg;
  3. /**
  4. * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
  5. *
  6. * Use the elgg_* versions instead.
  7. *
  8. * @access private
  9. *
  10. * @package Elgg.Core
  11. * @subpackage Widgets
  12. * @since 1.9.0
  13. */
  14. class WidgetsService {
  15. /**
  16. * @var \stdClass
  17. */
  18. private $widgets;
  19. /**
  20. * @see \Elgg\WidgetsService::getWidgets()
  21. * @var array
  22. */
  23. private $widgetCache = array();
  24. /**
  25. * @see elgg_get_widgets
  26. * @access private
  27. * @since 1.9.0
  28. */
  29. public function getWidgets($owner_guid, $context) {
  30. $widget_cache_key = "$context-$owner_guid";
  31. if (isset($this->widgetCache[$widget_cache_key])) {
  32. return $this->widgetCache[$widget_cache_key];
  33. }
  34. $options = array(
  35. 'type' => 'object',
  36. 'subtype' => 'widget',
  37. 'owner_guid' => $owner_guid,
  38. 'private_setting_name' => 'context',
  39. 'private_setting_value' => $context,
  40. 'limit' => 0,
  41. );
  42. $widgets = elgg_get_entities_from_private_settings($options);
  43. if (!$widgets) {
  44. return array();
  45. }
  46. $sorted_widgets = array();
  47. foreach ($widgets as $widget) {
  48. if (!isset($sorted_widgets[(int)$widget->column])) {
  49. $sorted_widgets[(int)$widget->column] = array();
  50. }
  51. $sorted_widgets[(int)$widget->column][$widget->order] = $widget;
  52. }
  53. foreach ($sorted_widgets as $col => $widgets) {
  54. ksort($sorted_widgets[$col]);
  55. }
  56. $this->widgetCache[$widget_cache_key] = $sorted_widgets;
  57. return $sorted_widgets;
  58. }
  59. /**
  60. * @see elgg_create_widget
  61. * @access private
  62. * @since 1.9.0
  63. */
  64. public function createWidget($owner_guid, $handler, $context, $access_id = null) {
  65. if (empty($owner_guid) || empty($handler) || !$this->validateType($handler)) {
  66. return false;
  67. }
  68. $owner = get_entity($owner_guid);
  69. if (!$owner) {
  70. return false;
  71. }
  72. $widget = new \ElggWidget;
  73. $widget->owner_guid = $owner_guid;
  74. $widget->container_guid = $owner_guid; // @todo - will this work for group widgets?
  75. if (isset($access_id)) {
  76. $widget->access_id = $access_id;
  77. } else {
  78. $widget->access_id = get_default_access();
  79. }
  80. if (!$widget->save()) {
  81. return false;
  82. }
  83. // private settings cannot be set until \ElggWidget saved
  84. $widget->handler = $handler;
  85. $widget->context = $context;
  86. return $widget->getGUID();
  87. }
  88. /**
  89. * @see elgg_can_edit_widget_layout
  90. * @access private
  91. * @since 1.9.0
  92. */
  93. public function canEditLayout($context, $user_guid = 0) {
  94. $user = get_entity((int)$user_guid);
  95. if (!$user) {
  96. $user = _elgg_services()->session->getLoggedInUser();
  97. }
  98. $return = false;
  99. if (_elgg_services()->session->isAdminLoggedIn()) {
  100. $return = true;
  101. }
  102. if (elgg_get_page_owner_guid() == $user->guid) {
  103. $return = true;
  104. }
  105. $params = array(
  106. 'user' => $user,
  107. 'context' => $context,
  108. 'page_owner' => elgg_get_page_owner_entity(),
  109. );
  110. return _elgg_services()->hooks->trigger('permissions_check', 'widget_layout', $params, $return);
  111. }
  112. /**
  113. * @see elgg_register_widget_type
  114. * @access private
  115. * @since 1.9.0
  116. */
  117. public function registerType($handler, $name, $description, array $context = array('all'), $multiple = false) {
  118. if (!$handler || !$name) {
  119. return false;
  120. }
  121. if (!isset($this->widgets)) {
  122. $this->widgets = new \stdClass;
  123. }
  124. if (!isset($this->widgets->handlers)) {
  125. $this->widgets->handlers = array();
  126. }
  127. $handlerobj = new \stdClass;
  128. $handlerobj->name = $name;
  129. $handlerobj->description = $description;
  130. $handlerobj->context = $context;
  131. $handlerobj->multiple = $multiple;
  132. $this->widgets->handlers[$handler] = $handlerobj;
  133. return true;
  134. }
  135. /**
  136. * @param string $handler
  137. * @return bool
  138. * @see elgg_unregister_widget_type
  139. * @access private
  140. * @since 1.9.0
  141. */
  142. public function unregisterType($handler) {
  143. if (!isset($this->widgets)) {
  144. return false;
  145. }
  146. if (!isset($this->widgets->handlers)) {
  147. return false;
  148. }
  149. if (isset($this->widgets->handlers[$handler])) {
  150. unset($this->widgets->handlers[$handler]);
  151. return true;
  152. }
  153. return false;
  154. }
  155. /**
  156. * @see elgg_is_widget_type
  157. * @access private
  158. * @since 1.9.0
  159. */
  160. public function validateType($handler) {
  161. if (!empty($this->widgets) &&
  162. !empty($this->widgets->handlers) &&
  163. is_array($this->widgets->handlers) &&
  164. array_key_exists($handler, $this->widgets->handlers)) {
  165. return true;
  166. }
  167. return false;
  168. }
  169. /**
  170. * @access private
  171. * @since 1.9.0
  172. */
  173. public function getAllTypes() {
  174. if (empty($this->widgets) ||
  175. empty($this->widgets->handlers) ||
  176. !is_array($this->widgets->handlers)) {
  177. // no widgets
  178. return array();
  179. }
  180. $widgets = array();
  181. foreach ($this->widgets->handlers as $key => $handler) {
  182. $widgets[$key] = $handler;
  183. }
  184. return $widgets;
  185. }
  186. /**
  187. * @access private
  188. * @since 1.9.0
  189. */
  190. public function getNameByType($handler) {
  191. if (isset($this->widgets->handlers[$handler])) {
  192. return $this->widgets->handlers[$handler]->name;
  193. }
  194. return false;
  195. }
  196. /**
  197. * @see elgg_get_widget_types
  198. * @access private
  199. * @since 1.9.0
  200. */
  201. public function getTypes($context = "", $exact = false) {
  202. if (empty($this->widgets) ||
  203. empty($this->widgets->handlers) ||
  204. !is_array($this->widgets->handlers)) {
  205. // no widgets
  206. return array();
  207. }
  208. if (!$context) {
  209. $context = elgg_get_context();
  210. }
  211. $widgets = array();
  212. foreach ($this->widgets->handlers as $key => $handler) {
  213. if ($exact) {
  214. if (in_array($context, $handler->context)) {
  215. $widgets[$key] = $handler;
  216. }
  217. } else {
  218. if (in_array('all', $handler->context) || in_array($context, $handler->context)) {
  219. $widgets[$key] = $handler;
  220. }
  221. }
  222. }
  223. return $widgets;
  224. }
  225. }