- <?php
-
-
- /**
- * Class file.
- *
- * @license http://opensource.org/licenses/mozilla1.1.php Mozilla Public License
- * @copyright 2005, diemeisterei GmbH. All rights reserved.
- * @author $Author: schmunk $
- * @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 $
- * @package Lithron.LContainer
- * @subpackage WebControls
- */
-
- /**
- * Brick Base Class
- *
- * A brick provides functionality for displaying data of a package.
- * Bricks have also an admin panel for setting parameters (i.e. different layouts).
- *
- * @package System.Web.UI.WebControls
- * @subpackage BrickSupport
- */
-
- class LBrick extends TTemplateControl
- {
- protected $_brickobject;
- protected $_adminSnippets;
- protected $AdminControls;
-
- private $_parameters= array ();
-
- protected $_uniqueValidationGroup; // valdation group id
-
-
- /**
- * sets the master class for consistent layout appearance
- */
- public function __construct()
- {
- parent :: __construct();
- $this->setUniqueValidationGroup(uniqid("brick"));
- $this->setMasterClass("LBaseBrick");
- }
-
- /**
- * @param object: brick propel data
- */
- public function setBrickObject($obj)
- {
- $this->_brickobject= $obj;
- }
- /**
- * @return object: brick propel data
- */
- public function getBrickObject()
- {
- return $this->_brickobject;
- }
-
- /**
- * @param string Admin|Normal which panel to show
- */
- public function setPanel($mode)
- {
- $this->setViewState("Panel", $mode);
- }
-
- /**
- * @return active panel
- */
- public function getPanel()
- {
- return $this->getViewState("Panel");
- }
-
- /**
- * @return boolean: wheter to show the admin panel or not
- */
- public function getShowAdminPanel()
- {
- $p= $this->getParameters();
- return (($this->Panel == "Admin") || ($p === null)) && $this->User->isInRole("admin");
- }
-
- /**
- * @return TMap: parameters for brick
- */
- public function getParameters()
- {
- $return= new TMap;
- if (isset ($this->_brickobject)) # TODO performance vs. stability? && method_exists("getParameters", $this->_brickobject))
- {
- $data= unserialize(base64_decode($this->_brickobject->getParameters()));
- if (is_array($data))
- $return->copyFrom($data);
- else
- return null;
- }
- else
- return null;
- return $return;
- }
-
- /**
- * @param array: sets the parameters
- */
- public function setParameters($arr)
- {
- $this->_brickobject->setParameters(base64_encode(serialize($arr)));
- $this->_brickobject->save();
- }
-
-
- /**
- * @param string
- * @tofo not working :(
- */
- public function setUniqueValidationGroup($value)
- {
- $this->_uniqueValidationGroup= $value;
- }
- /**
- * @return string
- */
- public function getUniqueValidationGroup()
- {
- return $this->_uniqueValidationGroup;
- }
-
- /**
- * @return boolean wheter the brick has an administration page, usually in 'packages'
- */
- public function getIsVisible()
- {
- return $this->getBrickObject()->getStatus();
- }
-
- /**
- * @return boolean wheter the brick has an administration page, usually in 'packages'
- */
- public function getHasAdministrationPage()
- {
- return false;
- }
-
- /**
- * @return string main admin page
- */
- public function getAdministrationPage()
- {
- return "packages.Administration";
- }
-
- /**
- * @return string the class name
- */
- public function getDescriptiveName()
- {
- return get_class($this);
- }
-
-
- /**
- * Prepares controls, sets slave
- */
- public function onInit($param)
- {
- parent :: onInit(new TEventParameter);
- $this->getMaster()->setSlave($this);
-
- $this->AdminControls= $this->Master->AdminPlaceholder;
-
- if ($this->User->isInRole("admin"))
- $this->renderAdminControls();
- }
-
- /**
- * Prepares layout panel
- */
- public function onPreRender($param)
- {
- parent :: onPreRender($param);
-
- $showAdmin= $this->getShowAdminPanel();
- $this->Master->AdminPanel->Visible= $showAdmin;
- $this->Master->ContentPanel->Visible= !$showAdmin;
- if ($showAdmin)
- $this->autoselectAdmin();
- }
-
- /**
- * selects admin form fields, depending on parameter name and contol id
- */
- protected function autoselectAdmin()
- {
- $p= $this->getParameters();
- if ($p instanceof TMap)
- foreach ($p->toArray() as $param => $value)
- {
- try
- {
- Prado :: log("Autoselecting $param with $value in " . get_class($this), 1, "Lithron.LContainer");
-
- if (($control= $this->AdminControls->findControl($param)) !== null)
- {
- if ($control instanceof TTextBox)
- $control->setText($value);
- else
- if ($control instanceof TListControl)
- $control->setSelectedValue($value);
- }
- else
- {
- Prado :: log("Unable to find control $param in " . get_class($this), 1, "Lithron.LContainer");
- }
- }
- catch (Exception $e)
- {
- Prado :: log("autoSelectAdmin(): Error, problems with " . get_class($this) . " " . $param . " (errors follow)", 8, "Lithron.LContent");
- Prado :: log($e->getErrorMessage(), 8, "Lithron.LContent");
- }
- }
- }
-
- /**
- * is opposite of autoselectAdmin
- */
- protected function autocollectAdmin()
- {
- foreach ($this->AdminControls->Controls as $control)
- {
- if (!$control instanceof TComponent)
- continue;
- if ($control instanceof TTextBox)
- $this->configureParameter($control->getId(), $_v= $control->getText());
- else
- if ($control instanceof TListControl)
- $this->configureParameter($control->getId(), $_v= $control->getSelectedValue());
- Prado :: log("Autocollecting param " . $control->getId() . " with value " . $_v, 1, "Lithron.LContainer");
- }
- }
-
- /**
- * prepares a string for the admin panel
- */
- public function addAdminSnippet($admin)
- {
- if (!$this->_adminSnippets instanceof TMap)
- {
- $this->_adminSnippets= new TMap;
- }
- #Prado :: log("Adding admin #" . $this->_adminSnippets->getCount() . " snippet for " . get_class($this), 1, "Lithron.LContainer");
- $this->_adminSnippets->add($this->_adminSnippets->getCount(), $admin);
- #return $param;
- }
-
- /**
- * returns all admin snippets
- */
- public function getAdminTemplate()
- {
- if (!$this->_adminSnippets instanceof TMap)
- {
- Prado :: log("No Admin snippets for " . get_class($this), 4, "Lithron.LContainer");
- return;
- }
- $return= implode("", $this->_adminSnippets->toArray());
- Prado :: log("Returning " . $this->_adminSnippets->getCount() . " admin snippet(s) for " . get_class($this), 1, "Lithron.LContainer");
- return $return;
- }
-
- /**
- * prepares parameters when for applyClicked()
- */
- public function configureParameter($name, $setter)
- {
- $this->_parameters[$name]['setter']= $setter;
- }
-
- /**
- * manages the commands of LBrickAdminButtons.php
- */
- public function brickControl($sender, $param)
- {
- switch ($param->getCommandName())
- {
- case "togglePanel" :
- $this->setPanel($this->Panel == "Admin" ? null : "Admin");
- break;
-
- case "deleteBrick" :
- //$this->Controls[] = "wanna delete brick<br>";
- $this->_brickobject->delete();
- $this->Visible= false;
- break;
-
- case "brickUp" :
- $cell= $this->Parent;
- while ($cell && !($cell instanceof LCell))
- $cell= $cell->Parent;
- if (!$cell)
- break;
- $cell->brickUp($this->_brickobject->getRank());
- break;
-
- case "brickDown" :
- $cell= $this->Parent;
- while ($cell && !($cell instanceof LCell))
- $cell= $cell->Parent;
- if (!$cell)
- break;
- $cell->brickDown($this->_brickobject->getRank());
- break;
-
- case "editBrick" :
- $url= $this->Service->constructUrl($this->getAdministrationPage());
- $this->Application->Response->redirect($url);
- break;
-
- case "enableBrick" :
- $this->getBrickObject()->setStatus(1);
- $this->getBrickObject()->save();
- break;
-
- case "disableBrick" :
- $this->getBrickObject()->setStatus(0);
- $this->getBrickObject()->save();
- break;
- }
- }
-
- /**
- * is invoked, when changes are applied
- */
- public function applyClicked()
- {
- Prado :: log("Apply clicked", 1, "Lithron.LContainer");
- $this->autocollectAdmin();
-
- $array= array ();
-
- foreach ($this->_parameters AS $name => $p)
- {
- #echo "return '".$p['setter']."';";
- $array[$name]= eval ("return '" . $p['setter'] . "';");
- }
- $this->setParameters($array);
- $this->setPanel(null);
-
- }
-
- /**
- * is invoked, when the admin task is cancelled
- */
- public function cancelClicked()
- {
- $this->setPanel(null);
- }
-
- /**
- * @ignore
- */
- private function renderAdminControls()
- {
- // TODO: windows path compatibility
- $base_arr= explode("/", $this->Application->getBasePath());
- array_pop($base_arr);
- $base= implode("/", $base_arr);
- $this->AdminControls->Controls->clear();
- try
- {
- $cooked= new TTemplate($this->getAdminTemplate(), $base);
- $cooked->instantiateIn($this->AdminControls);
- Prado :: log("Admin control created: " . get_class($this), 1, "Lithron.LContainer");
- }
- catch (Exception $e)
- {
- $this->AdminControls->Controls[]= '<div style="color: red;">' . $e->getErrorMessage() . '</div>';
- }
-
- }
-
- }
- ?>