/control.php

https://github.com/tomaszeman/ASPPHP · PHP · 303 lines · 151 code · 55 blank · 97 comment · 18 complexity · 2f7760412f27f8e59f0b73b805ba5bfe MD5 · raw file

  1. <?php
  2. class Control
  3. {
  4. private $id;
  5. public function getId() { return $this->id; }
  6. public function setId($id) { $this->id= $id; }
  7. private $lastIdIndex= -1;
  8. const DEFAULT_ID_SEPARATOR= "_";
  9. protected function getClientIdSeparator() { return self::DEFAULT_ID_SEPARATOR; }
  10. protected function getIdSeparator() { return self::DEFAULT_ID_SEPARATOR; }
  11. private $clientId;
  12. public function getClientId()
  13. {
  14. $mode= $this->clientIdMode;
  15. $id= $this->id;
  16. if($mode == ClientIDMode::Hidden)
  17. return null;
  18. if($mode == ClientIDMode::Fixed)
  19. {
  20. if($id == null)
  21. throw new Exception("Id is required in fixed mode.");
  22. return $id;
  23. }
  24. //Legacy mode
  25. $id= $this->getUniqueId();
  26. $csep= $this->getClientIdSeparator();
  27. $sep= $this->getIdSeparator();
  28. if($csep == $sep)
  29. return $id;
  30. //translate
  31. return strtr($id, $sep, $csep);
  32. }
  33. public function getUniqueId()
  34. {
  35. $id= $this->id;
  36. $separator= $this->getIdSeparator();
  37. $nc= $this->namingContainer;
  38. //create dynamic id
  39. $page= $this->page;
  40. while($nc !== $page)
  41. {
  42. $id= "{$nc->getId()}$separator$id";
  43. $nc= $nc->getNamingContainer();
  44. }
  45. return $id;
  46. }
  47. public function setClientId($clientId) { $this->clientId= $clientId; }
  48. private $clientIdMode;
  49. public function getClientIdMode() { return $this->clientIdMode; }
  50. public function setClientIdMode($clientIdMode) { $this->clientIdMode= $clientIdMode; }
  51. private $page;
  52. public function getPage() { return $this->page; }
  53. public function setPage(Page $page) { $this->page= $page; }
  54. private $namingContainer;
  55. public function getNamingContainer() { return $this->namingContainer; }
  56. public function setNamingContainer(INamingContainer $namingContainer) { $this->namingContainer= $namingContainer; }
  57. private $parent;
  58. public function getParent() { return $this->parent; }
  59. public function setParent(Control $parent) { $this->parent= $parent; }
  60. private $controls;
  61. public function getControls() { return $this->controls; }
  62. public function __construct()
  63. {
  64. $this->clientId= ClientIDMode::Legacy;
  65. $this->controls= $this->createControlCollection();
  66. }
  67. private $visible= true;
  68. public function isVisible() { return $this->visible; }
  69. public function setVisible($visible= null) { $this->visible= $visible === null || $visible; }
  70. protected function renderChildren()
  71. {
  72. foreach($this->controls as $control)
  73. $control->Render();
  74. }
  75. protected function createControlCollection() { return new ControlCollection($this); }
  76. //protected function onInit() { $this->ensureChildControlsCreated(); }
  77. protected function onInit() {}
  78. protected function onLoad() {}
  79. protected function generateAutomaticId() { return "c". ++$this->lastIdIndex; }
  80. /*protected function ensureId()
  81. {
  82. $nc= $this->namingContainer;
  83. if($this->id == null && $nc != null)
  84. $this->id= $nc->generateAutomaticId();
  85. }*/
  86. public function render()
  87. {
  88. if($this->visible)
  89. $this->renderChildren();
  90. }
  91. protected function saveControlState() { return null; }
  92. protected function loadControlState($state) {}
  93. public function controlAdded(Control $control)
  94. {
  95. $control->setParent($this);
  96. //$page= $this->getPage();
  97. //is it possible to get page and namingcontainers?
  98. /*$nc= $this instanceof INamingContainer
  99. ? $this
  100. : $this->getNamingContainer();
  101. if($nc != null)
  102. $this->updateNamingContainer($control, $nc);*/
  103. /*if($page != null)
  104. {
  105. $page->addedControlToPage($control);
  106. self::traverseControlStructure(
  107. $control->getControls(),
  108. function(Control $c, $page) { $page->addedControlToPage($c); },
  109. $page);
  110. }*/
  111. }
  112. /*private $childControlsCreated= false;
  113. protected function createChildControls() { $this->childControlsCreated= true; }
  114. protected function getChildControlsCreated() { return $this->$childControlsCreated; }
  115. protected function ensureChildControlsCreated()
  116. {
  117. if(!$this->childControlsCreated)
  118. {
  119. $this->createChildControls();
  120. $this->childControlsCreated= true;
  121. }
  122. }*/
  123. protected function initRecursive(Control $control, INamingContainer $nc)
  124. {
  125. $page= $nc->getPage();
  126. $control->setPage($page);
  127. $page->addedControlToPage($control);
  128. $control->setNamingContainer($nc);
  129. if($control->getId() === null)
  130. $control->setId($nc->generateAutomaticId());
  131. $control->onInit();
  132. if($control instanceof INamingContainer)
  133. $nc= $control;
  134. foreach($control->getControls() as $child)
  135. $this->initRecursive($child, $nc);
  136. }
  137. protected function loadRecursive(Control $control)
  138. {
  139. foreach($control->getControls() as $child)
  140. $this->loadRecursive($child);
  141. $control->onLoad();
  142. }
  143. /*protected function initRecursive(Control $control, INamingContainer $nc)
  144. {
  145. /*if($control instanceof INamingContainer)
  146. $nc= $control;
  147. foreach($control->getControls() as $child)
  148. {
  149. $page->addedControlToPage($control->getPage());
  150. $control->setNamingContainer= $nc;
  151. if($control->id == null)
  152. $control->id= $nc->generateAutomaticId();
  153. $this->initRecursive($child, $nc);
  154. }
  155. $control.OnInit();
  156. }*/
  157. /*protected function updateNamingContainer(Control $control, INamingContainer $nc)
  158. {
  159. if($control->id === null)
  160. $control->id= $nc->generateAutomaticId();
  161. $control->setNamingContainer($nc);
  162. if($control instanceof INamingContainer)
  163. return;
  164. //$nc= $control;
  165. foreach($control->getControls() as $child)
  166. $this->updateNamingContainer($child, $nc);
  167. }*/
  168. /*
  169. public function findControl($id)
  170. {
  171. }*/
  172. /*public function findControl($id)
  173. {
  174. if((!$this instanceof INamingContainer))
  175. return ($this->getNamingConainer()->findControl($id);
  176. return $this->findControlRecursive($this->controls, $id);
  177. }
  178. public function findControlRecursive(ControlCollection &$controls, $id)
  179. {
  180. foreach($controls as $child)
  181. {
  182. $curId= $child.getId();
  183. if($id == $curId)
  184. return $id;
  185. if((!$child instanceof INamingContainer))
  186. {
  187. $curId= $this->findControlRecursive($child);
  188. if($curId != null)
  189. return $curId;
  190. }
  191. }
  192. return null;
  193. }*/
  194. static function traverseControlStructure(ControlCollection $controls, $apply, $params= null)
  195. {
  196. foreach($controls as $child)
  197. {
  198. $apply($child, $params);
  199. self::traverseControlStructure($child->getControls(), $apply, $params);
  200. }
  201. }
  202. public function populate($data) { $this->populateRecursive($this, $data); }
  203. protected function populateRecursive(Control $control, $data)
  204. {
  205. if($control instanceof IBindableControl)
  206. {
  207. $expression= $control->getExpression();
  208. if($expression === null)
  209. return;
  210. if(!strpos($expression, "->"))
  211. $control->setValue($data->$expression);
  212. else
  213. $control->setValue(eval("return \$data->$expression;"));
  214. return;
  215. }
  216. foreach($control->getControls() as $child)
  217. $this->populateRecursive($child, $data);
  218. }
  219. public function persist($data) { $this->persistRecursive($this, $data); }
  220. protected function persistRecursive(Control $control, $data)
  221. {
  222. if($control instanceof IBindableControl)
  223. {
  224. $expression= $control->getExpression();
  225. if($expression === null)
  226. return;
  227. $value= $control->getValue();
  228. if(!strpos($expression, "->"))
  229. $data->$expression= $value;
  230. else
  231. eval("\$data->$expression= \$value;");
  232. return;
  233. }
  234. foreach($control->getControls() as $child)
  235. $this->persistRecursive($child, $data);
  236. }
  237. }
  238. ?>