PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/www/app/AdminModule/components/PageControl/Item.php

https://github.com/bazo/Mokuji
PHP | 147 lines | 59 code | 25 blank | 63 comment | 11 complexity | bdfbb36f4cf9df302de89700c3bd4530 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. <?php
  2. /**
  3. * This source file is subject to the "New BSD License".
  4. *
  5. * For more information please see http://nettephp.com
  6. *
  7. * @author Jan Kuchař
  8. * @copyright Copyright (c) 2009 Jan Kuchař (http://mujserver.net)
  9. * @license New BSD License
  10. * @link http://nettephp.com/extras/tabcontrol
  11. */
  12. /**
  13. * Tab class
  14. *
  15.  * show off @property, @property-read, @property-write
  16. * @property mixed $content Content of the tab
  17. */
  18. class Item extends Control {
  19. /**************************************************************************/
  20. /* Variables */
  21. /**************************************************************************/
  22. /**
  23. * Tab content factory
  24. * @var array
  25. */
  26. public $contentFactory = array();
  27. /**
  28. * Tab content renderer
  29. * @var array
  30. */
  31. public $contentRenderer;
  32. /**
  33. * Created component
  34. * @var IComponent
  35. */
  36. private $content;
  37. /**
  38. * Has content some snippets?
  39. * ! This parameter is very important when you use ajax!
  40. * @var bool
  41. */
  42. public $hasSnippets= false;
  43. function __construct(Page $parent,$name) {
  44. parent::__construct($parent, $name);
  45. $this->contentRenderer = array($this,"renderContent"); // Default renderer
  46. }
  47. /**************************************************************************/
  48. /* Main methods */
  49. /**************************************************************************/
  50. /**
  51. * Factory for components
  52. * @param string $name
  53. */
  54. function createComponent($name) {
  55. $this->getContent(); // This will also create content Components
  56. }
  57. /**
  58. * Default renderer
  59. */
  60. function renderContent() {
  61. $content = $this->getContent();
  62. if($content instanceof IComponent or $content instanceof ITemplate) {
  63. $content->render();
  64. }
  65. else
  66. echo (string)$content;
  67. }
  68. /**
  69. * Creates (if need) content and returns
  70. * @return IComponent
  71. */
  72. function getContent() {
  73. if($this->content === null) {
  74. if(is_callable($this->contentFactory, FALSE)) {
  75. // Callback
  76. $component = call_user_func_array($this->contentFactory, array($this->name, $this));
  77. if($component instanceof IComponent) {
  78. $name = $this->name;
  79. if($component->name !== null) $name = $component->name;
  80. if($component->parent === null)
  81. $this->addComponent($component, $name);
  82. if($component->parent !== $this)
  83. throw new InvalidStateException("Component must be registred to \"Item\" control");
  84. }
  85. $this->content = $component;
  86. }
  87. else
  88. throw new InvalidStateException("Factory callback is not callable!");
  89. }
  90. return $this->content;
  91. }
  92. function setContent($content) {
  93. $this->content = $content;
  94. }
  95. /**
  96. * Renders component
  97. */
  98. function render() {
  99. if (SnippetHelper::$outputAllowed OR $this->hasSnippets) {
  100. if(is_callable($this->contentRenderer, FALSE)) {
  101. call_user_func_array($this->contentRenderer, array($this));
  102. }
  103. else throw new InvalidStateException("Renderer callback is not callable!");
  104. }
  105. }
  106. /**
  107. * Generates URL to presenter, action or signal.
  108. *
  109. * This will try to generate URL on $this component. If fails try to generate url on handlerComponent.
  110. * @param string destination in format "[[module:]presenter:]action" or "signal!"
  111. * @param array|mixed
  112. * @return string
  113. * @throws InvalidLinkException
  114. */
  115. public function link($destination, $args = array()) {
  116. if (!is_array($args)) {
  117. $args = func_get_args();
  118. array_shift($args);
  119. }
  120. return $this->parent->handlerComponent->link($destination, $args);
  121. }
  122. }