PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/blog/core/model/modx/moddashboardwidget.class.php

https://bitbucket.org/orchdork10159/dnsman.ly
PHP | 310 lines | 163 code | 18 blank | 129 comment | 21 complexity | ca746113dbcf63ec5789ddcaea11a6b6 MD5 | raw file
  1. <?php
  2. /**
  3. * @package modx
  4. * @subpackage mysql
  5. */
  6. /**
  7. * Abstraction of a Dashboard Widget, which can be placed on Dashboards for welcome screen customization.
  8. *
  9. * @property string $name The name of this widget
  10. * @property string $description A short description, or lexicon key, of this widget
  11. * @property string $type The type of widget that this is: file/snippet/html
  12. * @property string $content The content, or location, of this widget
  13. * @property string $namespace The namespace key of this widget
  14. * @property string $lexicon The lexicon that will be loaded for this widget's description/execution
  15. *
  16. * @property modNamespace $Namespace The Namespace this widget is related to
  17. * @property array $Placements An array of Placements this widget has been put on in Dashboards
  18. * @package modx
  19. */
  20. class modDashboardWidget extends xPDOSimpleObject {
  21. public function toArray($keyPrefix = '',$rawValues = false,$excludeLazy = false,$includeRelated = false) {
  22. $array = parent::toArray($keyPrefix,$rawValues,$excludeLazy);
  23. if (!empty($this->xpdo->lexicon) && $this->xpdo->lexicon instanceof modLexicon) {
  24. if ($this->get('lexicon') != 'core:dashboards') {
  25. $this->xpdo->lexicon->load($this->get('lexicon'));
  26. }
  27. $array['name_trans'] = $this->xpdo->lexicon->exists($this->get('name')) ? $this->xpdo->lexicon($this->get('name')) : $this->get('name');
  28. $array['description_trans'] = $this->xpdo->lexicon->exists($this->get('description')) ? $this->xpdo->lexicon($this->get('description')) : $this->get('description');
  29. }
  30. return $array;
  31. }
  32. /**
  33. * Return the output for the widget, processed by type
  34. *
  35. * @param modManagerController $controller
  36. * @return mixed
  37. */
  38. public function getContent($controller) {
  39. /** @var string $lexicon */
  40. $lexicon = $this->get('lexicon');
  41. if (!empty($lexicon) && !empty($this->xpdo->lexicon)) {
  42. $this->xpdo->lexicon->load($lexicon);
  43. }
  44. /** @var modNamespace $namespace */
  45. $namespace = $this->getOne('Namespace');
  46. /** @var string $content */
  47. $content = $this->get('content');
  48. /** @var modDashboardWidgetInterface $widget */
  49. $widget = null;
  50. switch (strtolower($this->get('type'))) {
  51. /* file/class-based widget */
  52. case 'file':
  53. $content = str_replace(array(
  54. '[[++base_path]]',
  55. '[[++core_path]]',
  56. '[[++manager_path]]',
  57. '[[++assets_path]]',
  58. '[[++manager_theme]]',
  59. ),array(
  60. $this->xpdo->getOption('base_path',null,MODX_BASE_PATH),
  61. $this->xpdo->getOption('core_path',null,MODX_CORE_PATH),
  62. $this->xpdo->getOption('manager_path',null,MODX_MANAGER_PATH),
  63. $this->xpdo->getOption('assets_path',null,MODX_ASSETS_PATH),
  64. $this->xpdo->getOption('manager_theme',null,'default'),
  65. ),$content);
  66. if (file_exists($content)) {
  67. $modx =& $this->xpdo;
  68. $scriptProperties = $this->toArray();
  69. ob_start();
  70. $className = include_once $content;
  71. $buffer = ob_get_contents();
  72. ob_end_clean();
  73. if (class_exists($className)) { /* is a class-based widget */
  74. /** @var modDashboardWidgetInterface $widget */
  75. $widget = new $className($this->xpdo,$this,$controller);
  76. } else { /* just a standard file with a return */
  77. if (($className === 1 || $className === true) && !empty($buffer)) {
  78. $className = $buffer;
  79. }
  80. $widget = new modDashboardFileWidget($this->xpdo,$this,$controller);
  81. $widget->setContent($className);
  82. }
  83. }
  84. break;
  85. /* Snippet widget */
  86. case 'snippet':
  87. $widget = new modDashboardSnippetWidget($this->xpdo,$this,$controller);
  88. break;
  89. /* PHP (Snippet) widget */
  90. case 'php':
  91. $widget = new modDashboardPhpWidget($this->xpdo,$this,$controller);
  92. break;
  93. /* HTML/static content widget */
  94. case 'html':
  95. default:
  96. $widget = new modDashboardHtmlWidget($this->xpdo,$this,$controller);
  97. break;
  98. }
  99. if (!empty($namespace)) {
  100. $widget->setNamespace($namespace);
  101. }
  102. return $widget->process();
  103. }
  104. }
  105. /**
  106. * A file-based widget that returns only the content of its include.
  107. *
  108. * @package modx
  109. * @subpackage dashboard
  110. */
  111. class modDashboardFileWidget extends modDashboardWidgetInterface {
  112. public function render() {
  113. return $this->content;
  114. }
  115. }
  116. /**
  117. * A widget that contains only HTML.
  118. *
  119. * @package modx
  120. * @subpackage dashboard
  121. */
  122. class modDashboardHtmlWidget extends modDashboardWidgetInterface {
  123. public function render() {
  124. return $this->widget->get('content');
  125. }
  126. }
  127. /**
  128. * A widget that runs its content as PHP in Snippet-like format to get its content.
  129. * @package modx
  130. * @subpackage dashboard
  131. */
  132. class modDashboardPhpWidget extends modDashboardWidgetInterface {
  133. public function render() {
  134. return $this->renderAsSnippet();
  135. }
  136. }
  137. /**
  138. * A Snippet-based widget that loads a MODX Snippet to return its content.
  139. * @package modx
  140. * @subpackage dashboard
  141. */
  142. class modDashboardSnippetWidget extends modDashboardWidgetInterface {
  143. public function render() {
  144. /** @var modSnippet $snippet */
  145. $snippet = $this->modx->getObject('modSnippet',array(
  146. 'name' => $this->widget->get('content'),
  147. ));
  148. if ($snippet) {
  149. $snippet->setCacheable(false);
  150. $content = $snippet->process(array(
  151. 'controller' => $this->controller,
  152. ));
  153. } else {
  154. $content = '';
  155. }
  156. return $content;
  157. }
  158. }
  159. /**
  160. * Abstract class used for creating Dashboard Widgets. In your widget file, simply return the name of the class
  161. * at the end of the file, and MODX will instantiate your class as a widget, and run the render() method to get
  162. * the widget output.
  163. *
  164. * @abstract
  165. * @package modx
  166. * @subpackage dashboard
  167. */
  168. abstract class modDashboardWidgetInterface {
  169. /**
  170. * A reference to the modX|xPDO instance
  171. * @var xPDO|modX $modx
  172. */
  173. public $modx;
  174. /**
  175. * A reference to the currently loaded manager controller
  176. * @var modManagerController $controller
  177. */
  178. public $controller;
  179. /**
  180. * A reference to this class's widget
  181. * @var modDashboardWidget $widget
  182. */
  183. public $widget;
  184. /**
  185. * A reference to the Namespace that this widget is executing in
  186. * @var modNamespace $namespace
  187. */
  188. public $namespace;
  189. /**
  190. * Allows widgets to specify a CSS class to attach to the block
  191. *
  192. * @var string
  193. */
  194. public $cssBlockClass = '';
  195. /**
  196. * @var string
  197. */
  198. public $content = '';
  199. /**
  200. * @param xPDO|modX $modx A reference to the modX instance
  201. * @param modDashboardWidget $widget A reference to this class's widget
  202. * @param modManagerController $controller A reference to the currently loaded manager controller
  203. */
  204. function __construct(xPDO &$modx,modDashboardWidget &$widget,modManagerController &$controller) {
  205. $this->modx =& $modx;
  206. $this->widget =& $widget;
  207. $this->controller =& $controller;
  208. }
  209. /**
  210. * Set the widget content
  211. * @param string $content
  212. * @return void
  213. */
  214. public function setContent($content) {
  215. $this->content = $content;
  216. }
  217. /**
  218. * Renders the content of the block in the appropriate size
  219. * @return string
  220. */
  221. public function process() {
  222. $output = $this->render();
  223. if (!empty($output)) {
  224. $widgetArray = $this->widget->toArray();
  225. $widgetArray['content'] = $output;
  226. $widgetArray['class'] = $this->cssBlockClass;
  227. $output = $this->getFileChunk('dashboard/block.tpl',$widgetArray);
  228. $output = preg_replace('@\[\[(.[^\[\[]*?)\]\]@si','',$output);
  229. $output = preg_replace('@\[\[(.[^\[\[]*?)\]\]@si','',$output);
  230. }
  231. return $output;
  232. }
  233. /**
  234. * Sets the Namespace that this widget will execute in
  235. * @param modNamespace $namespace
  236. * @return void
  237. */
  238. public function setNamespace(modNamespace $namespace) {
  239. $this->namespace =& $namespace;
  240. }
  241. /**
  242. * Returns an array of placeholders for the block from the widget class. Override to add or change placeholders.
  243. * @return array
  244. */
  245. public function toArray() {
  246. return $this->widget->toArray();
  247. }
  248. /**
  249. * Must be declared in your derivative class. Must return the processed output of the widget.
  250. * @abstract
  251. * @return string
  252. */
  253. abstract public function render();
  254. /**
  255. * @param string $tpl
  256. * @param array $placeholders
  257. * @return string
  258. */
  259. public function getFileChunk($tpl,array $placeholders = array()) {
  260. $output = '';
  261. $file = $tpl;
  262. if (!file_exists($file)) {
  263. $file = $this->modx->getOption('manager_path').'templates/'.$this->modx->getOption('manager_theme',null,'default').'/'.$tpl;
  264. }
  265. if (!file_exists($file)) {
  266. $file = $this->modx->getOption('manager_path').'templates/default/'.$tpl;
  267. }
  268. if (file_exists($file)) {
  269. /** @var modChunk $chunk */
  270. $chunk = $this->modx->newObject('modChunk');
  271. $chunk->setCacheable(false);
  272. $tplContent = file_get_contents($file);
  273. $chunk->setContent($tplContent);
  274. $output = $chunk->process($placeholders);
  275. }
  276. return $output;
  277. }
  278. /**
  279. * Render the widget content as if it were a Snippet
  280. *
  281. * @param string $content
  282. * @return string
  283. */
  284. public function renderAsSnippet($content = '') {
  285. if (empty($content)) $content = $this->widget->get('content');
  286. $content = str_replace('<?php','',$content);
  287. $closure = create_function('$scriptProperties','global $modx;if (is_array($scriptProperties)) {extract($scriptProperties, EXTR_SKIP);}'.$content);
  288. return $closure(array(
  289. 'controller' => $this->controller,
  290. ));
  291. }
  292. }