Lithron.LContainer
[ class tree: Lithron.LContainer ] [ index: Lithron.LContainer ] [ all elements ]

Source for file LBrick.php

Documentation is available at LBrick.php

  1. <?php
  2.  
  3.  
  4. /**
  5. * Class file.
  6. *
  7. * @license http://opensource.org/licenses/mozilla1.1.php Mozilla Public License
  8. * @copyright 2005, diemeisterei GmbH. All rights reserved.
  9. * @author $Author: schmunk $
  10. * @version $Revision: 259 $ $Date: 2006-06-07 01:03:06 +0200 (Mi, 07 Jun 2006) $ $HeadURL: https://svn.sourceforge.net/svnroot/lithron/trunk/packages/LContainer/LBrick.php $
  11. * @package Lithron.LContainer
  12. * @subpackage WebControls
  13. */
  14.  
  15. /**
  16. * Brick Base Class
  17. *
  18. * A brick provides functionality for displaying data of a package.
  19. * Bricks have also an admin panel for setting parameters (i.e. different layouts).
  20. *
  21. * @package System.Web.UI.WebControls
  22. * @subpackage BrickSupport
  23. */
  24.  
  25. class LBrick extends TTemplateControl
  26. {
  27. protected $_brickobject;
  28. protected $_adminSnippets;
  29. protected $AdminControls;
  30.  
  31. private $_parameters= array ();
  32.  
  33. protected $_uniqueValidationGroup; // valdation group id
  34.  
  35. /**
  36. * sets the master class for consistent layout appearance
  37. */
  38. public function __construct()
  39. {
  40. parent :: __construct();
  41. $this->setUniqueValidationGroup(uniqid("brick"));
  42. $this->setMasterClass("LBaseBrick");
  43. }
  44.  
  45. /**
  46. * @param object: brick propel data
  47. */
  48. public function setBrickObject($obj)
  49. {
  50. $this->_brickobject= $obj;
  51. }
  52. /**
  53. * @return object: brick propel data
  54. */
  55. public function getBrickObject()
  56. {
  57. return $this->_brickobject;
  58. }
  59.  
  60. /**
  61. * @param string Admin|Normal which panel to show
  62. */
  63. public function setPanel($mode)
  64. {
  65. $this->setViewState("Panel", $mode);
  66. }
  67.  
  68. /**
  69. * @return active panel
  70. */
  71. public function getPanel()
  72. {
  73. return $this->getViewState("Panel");
  74. }
  75.  
  76. /**
  77. * @return boolean: wheter to show the admin panel or not
  78. */
  79. public function getShowAdminPanel()
  80. {
  81. $p= $this->getParameters();
  82. return (($this->Panel == "Admin") || ($p === null)) && $this->User->isInRole("admin");
  83. }
  84.  
  85. /**
  86. * @return TMap: parameters for brick
  87. */
  88. public function getParameters()
  89. {
  90. $return= new TMap;
  91. if (isset ($this->_brickobject)) # TODO performance vs. stability? && method_exists("getParameters", $this->_brickobject))
  92. {
  93. $data= unserialize(base64_decode($this->_brickobject->getParameters()));
  94. if (is_array($data))
  95. $return->copyFrom($data);
  96. else
  97. return null;
  98. }
  99. else
  100. return null;
  101. return $return;
  102. }
  103.  
  104. /**
  105. * @param array: sets the parameters
  106. */
  107. public function setParameters($arr)
  108. {
  109. $this->_brickobject->setParameters(base64_encode(serialize($arr)));
  110. $this->_brickobject->save();
  111. }
  112.  
  113.  
  114. /**
  115. * @param string
  116. * @tofo not working :(
  117. */
  118. public function setUniqueValidationGroup($value)
  119. {
  120. $this->_uniqueValidationGroup= $value;
  121. }
  122. /**
  123. * @return string
  124. */
  125. public function getUniqueValidationGroup()
  126. {
  127. return $this->_uniqueValidationGroup;
  128. }
  129.  
  130. /**
  131. * @return boolean wheter the brick has an administration page, usually in 'packages'
  132. */
  133. public function getIsVisible()
  134. {
  135. return $this->getBrickObject()->getStatus();
  136. }
  137.  
  138. /**
  139. * @return boolean wheter the brick has an administration page, usually in 'packages'
  140. */
  141. public function getHasAdministrationPage()
  142. {
  143. return false;
  144. }
  145.  
  146. /**
  147. * @return string main admin page
  148. */
  149. public function getAdministrationPage()
  150. {
  151. return "packages.Administration";
  152. }
  153.  
  154. /**
  155. * @return string the class name
  156. */
  157. public function getDescriptiveName()
  158. {
  159. return get_class($this);
  160. }
  161.  
  162.  
  163. /**
  164. * Prepares controls, sets slave
  165. */
  166. public function onInit($param)
  167. {
  168. parent :: onInit(new TEventParameter);
  169. $this->getMaster()->setSlave($this);
  170.  
  171. $this->AdminControls= $this->Master->AdminPlaceholder;
  172.  
  173. if ($this->User->isInRole("admin"))
  174. $this->renderAdminControls();
  175. }
  176.  
  177. /**
  178. * Prepares layout panel
  179. */
  180. public function onPreRender($param)
  181. {
  182. parent :: onPreRender($param);
  183.  
  184. $showAdmin= $this->getShowAdminPanel();
  185. $this->Master->AdminPanel->Visible= $showAdmin;
  186. $this->Master->ContentPanel->Visible= !$showAdmin;
  187. if ($showAdmin)
  188. $this->autoselectAdmin();
  189. }
  190.  
  191. /**
  192. * selects admin form fields, depending on parameter name and contol id
  193. */
  194. protected function autoselectAdmin()
  195. {
  196. $p= $this->getParameters();
  197. if ($p instanceof TMap)
  198. foreach ($p->toArray() as $param => $value)
  199. {
  200. try
  201. {
  202. Prado :: log("Autoselecting $param with $value in " . get_class($this), 1, "Lithron.LContainer");
  203.  
  204. if (($control= $this->AdminControls->findControl($param)) !== null)
  205. {
  206. if ($control instanceof TTextBox)
  207. $control->setText($value);
  208. else
  209. if ($control instanceof TListControl)
  210. $control->setSelectedValue($value);
  211. }
  212. else
  213. {
  214. Prado :: log("Unable to find control $param in " . get_class($this), 1, "Lithron.LContainer");
  215. }
  216. }
  217. catch (Exception $e)
  218. {
  219. Prado :: log("autoSelectAdmin(): Error, problems with " . get_class($this) . " " . $param . " (errors follow)", 8, "Lithron.LContent");
  220. Prado :: log($e->getErrorMessage(), 8, "Lithron.LContent");
  221. }
  222. }
  223. }
  224.  
  225. /**
  226. * is opposite of autoselectAdmin
  227. */
  228. protected function autocollectAdmin()
  229. {
  230. foreach ($this->AdminControls->Controls as $control)
  231. {
  232. if (!$control instanceof TComponent)
  233. continue;
  234. if ($control instanceof TTextBox)
  235. $this->configureParameter($control->getId(), $_v= $control->getText());
  236. else
  237. if ($control instanceof TListControl)
  238. $this->configureParameter($control->getId(), $_v= $control->getSelectedValue());
  239. Prado :: log("Autocollecting param " . $control->getId() . " with value " . $_v, 1, "Lithron.LContainer");
  240. }
  241. }
  242.  
  243. /**
  244. * prepares a string for the admin panel
  245. */
  246. public function addAdminSnippet($admin)
  247. {
  248. if (!$this->_adminSnippets instanceof TMap)
  249. {
  250. $this->_adminSnippets= new TMap;
  251. }
  252. #Prado :: log("Adding admin #" . $this->_adminSnippets->getCount() . " snippet for " . get_class($this), 1, "Lithron.LContainer");
  253. $this->_adminSnippets->add($this->_adminSnippets->getCount(), $admin);
  254. #return $param;
  255. }
  256.  
  257. /**
  258. * returns all admin snippets
  259. */
  260. public function getAdminTemplate()
  261. {
  262. if (!$this->_adminSnippets instanceof TMap)
  263. {
  264. Prado :: log("No Admin snippets for " . get_class($this), 4, "Lithron.LContainer");
  265. return;
  266. }
  267. $return= implode("", $this->_adminSnippets->toArray());
  268. Prado :: log("Returning " . $this->_adminSnippets->getCount() . " admin snippet(s) for " . get_class($this), 1, "Lithron.LContainer");
  269. return $return;
  270. }
  271.  
  272. /**
  273. * prepares parameters when for applyClicked()
  274. */
  275. public function configureParameter($name, $setter)
  276. {
  277. $this->_parameters[$name]['setter']= $setter;
  278. }
  279.  
  280. /**
  281. * manages the commands of LBrickAdminButtons.php
  282. */
  283. public function brickControl($sender, $param)
  284. {
  285. switch ($param->getCommandName())
  286. {
  287. case "togglePanel" :
  288. $this->setPanel($this->Panel == "Admin" ? null : "Admin");
  289. break;
  290.  
  291. case "deleteBrick" :
  292. //$this->Controls[] = "wanna delete brick<br>";
  293. $this->_brickobject->delete();
  294. $this->Visible= false;
  295. break;
  296.  
  297. case "brickUp" :
  298. $cell= $this->Parent;
  299. while ($cell && !($cell instanceof LCell))
  300. $cell= $cell->Parent;
  301. if (!$cell)
  302. break;
  303. $cell->brickUp($this->_brickobject->getRank());
  304. break;
  305.  
  306. case "brickDown" :
  307. $cell= $this->Parent;
  308. while ($cell && !($cell instanceof LCell))
  309. $cell= $cell->Parent;
  310. if (!$cell)
  311. break;
  312. $cell->brickDown($this->_brickobject->getRank());
  313. break;
  314.  
  315. case "editBrick" :
  316. $url= $this->Service->constructUrl($this->getAdministrationPage());
  317. $this->Application->Response->redirect($url);
  318. break;
  319.  
  320. case "enableBrick" :
  321. $this->getBrickObject()->setStatus(1);
  322. $this->getBrickObject()->save();
  323. break;
  324.  
  325. case "disableBrick" :
  326. $this->getBrickObject()->setStatus(0);
  327. $this->getBrickObject()->save();
  328. break;
  329. }
  330. }
  331.  
  332. /**
  333. * is invoked, when changes are applied
  334. */
  335. public function applyClicked()
  336. {
  337. Prado :: log("Apply clicked", 1, "Lithron.LContainer");
  338. $this->autocollectAdmin();
  339.  
  340. $array= array ();
  341.  
  342. foreach ($this->_parameters AS $name => $p)
  343. {
  344. #echo "return '".$p['setter']."';";
  345. $array[$name]= eval ("return '" . $p['setter'] . "';");
  346. }
  347. $this->setParameters($array);
  348. $this->setPanel(null);
  349.  
  350. }
  351.  
  352. /**
  353. * is invoked, when the admin task is cancelled
  354. */
  355. public function cancelClicked()
  356. {
  357. $this->setPanel(null);
  358. }
  359.  
  360. /**
  361. * @ignore
  362. */
  363. private function renderAdminControls()
  364. {
  365. // TODO: windows path compatibility
  366. $base_arr= explode("/", $this->Application->getBasePath());
  367. array_pop($base_arr);
  368. $base= implode("/", $base_arr);
  369. $this->AdminControls->Controls->clear();
  370. try
  371. {
  372. $cooked= new TTemplate($this->getAdminTemplate(), $base);
  373. $cooked->instantiateIn($this->AdminControls);
  374. Prado :: log("Admin control created: " . get_class($this), 1, "Lithron.LContainer");
  375. }
  376. catch (Exception $e)
  377. {
  378. $this->AdminControls->Controls[]= '<div style="color: red;">' . $e->getErrorMessage() . '</div>';
  379. }
  380.  
  381. }
  382.  
  383. }
  384. ?>

Documentation generated on Tue, 20 Jun 2006 05:14:59 +0200 by phpDocumentor 1.3.0RC4