diff --git a/CoreVersions/Baikal_0.1/Frameworks/Baikal/Core/ClassLoader.php b/CoreVersions/Baikal_0.1/Frameworks/Baikal/Core/ClassLoader.php index ba0e905..af95e8b 100644 --- a/CoreVersions/Baikal_0.1/Frameworks/Baikal/Core/ClassLoader.php +++ b/CoreVersions/Baikal_0.1/Frameworks/Baikal/Core/ClassLoader.php @@ -5,7 +5,7 @@ namespace Baikal\Core; class ClassLoader { public static function register() { - return spl_autoload_register(array(get_called_class(), 'loadClass')); + return spl_autoload_register(array(__CLASS__, 'loadClass')); } public static function loadClass($sFullClassName) { diff --git a/CoreVersions/Baikal_0.1/Frameworks/BaikalAdmin/Controler/User/Form.php b/CoreVersions/Baikal_0.1/Frameworks/BaikalAdmin/Controler/User/Form.php index f409368..1919837 100644 --- a/CoreVersions/Baikal_0.1/Frameworks/BaikalAdmin/Controler/User/Form.php +++ b/CoreVersions/Baikal_0.1/Frameworks/BaikalAdmin/Controler/User/Form.php @@ -7,10 +7,21 @@ class Form extends \Flake\Core\Controler { const BASEPATH = "/admin/"; protected $aMessages = array(); - function execute() { + public function __construct() { + parent::__construct(); if(($iUser = self::editRequested()) !== FALSE) { - if(self::editSubmitted()) { - + $this->oModel = new \BaikalAdmin\Model\User($iUser); + } + + $this->initForm(); + } + + function execute() { + + if(($iUser = self::editRequested()) !== FALSE) { + if($this->oForm->submitted()) { + $this->oForm->execute(); +/* $aPost = \Flake\Util\Tools::POST(); $aErrors = array(); @@ -44,7 +55,7 @@ class Form extends \Flake\Core\Controler { "Changes on " . $oUser->get("username") . " have been saved." ); } - +*/ } } @@ -75,7 +86,21 @@ class Form extends \Flake\Core\Controler { self::BASEPATH ); } - } + } + } + + function initForm() { + $aOptions = array( + "closeurl" => $this::BASEPATH + ); + + if($this->editRequested()) { + $this->oForm = $this->oModel->formForInstance($aOptions); + } else { + $this->oForm = \BaikalAdmin\Model\User::formEmpty(array( + "closeurl" => $this::BASEPATH + )); + } } public static function editRequested() { @@ -86,12 +111,6 @@ class Form extends \Flake\Core\Controler { return FALSE; } - public static function editSubmitted() { - return self::editRequested() && ( - intval(\Flake\Util\Tools::POST("formedit-submitted")) === 1 - ); - } - public static function deleteRequested() { if(($iUser = intval(\Flake\Util\Tools::GET("userdel"))) > 0) { return $iUser; @@ -124,7 +143,9 @@ class Form extends \Flake\Core\Controler { $oView->setData("user", $oUser); $oView->setData("messages", $sMessages); + $sHtml .= $this->oForm->render(); $sHtml .= $oView->render(); + } else { $sHtml .= $sMessages; } diff --git a/CoreVersions/Baikal_0.1/Frameworks/BaikalAdmin/Core/Bootstrap.php b/CoreVersions/Baikal_0.1/Frameworks/BaikalAdmin/Core/Bootstrap.php index 37ec3da..a0304a9 100644 --- a/CoreVersions/Baikal_0.1/Frameworks/BaikalAdmin/Core/Bootstrap.php +++ b/CoreVersions/Baikal_0.1/Frameworks/BaikalAdmin/Core/Bootstrap.php @@ -8,6 +8,9 @@ require_once(dirname(dirname(dirname(__FILE__))) . "/Baikal/Core/Bootstrap.php") # Bootstrap Flake require_once(dirname(dirname(dirname(__FILE__))) . "/Flake/Core/Bootstrap.php"); +# Bootstrap Formal +require_once(dirname(dirname(dirname(__FILE__))) . "/Formal/Core/Bootstrap.php"); + # Registering BaikalAdmin classloader require_once(dirname(__FILE__) . '/ClassLoader.php'); \BaikalAdmin\Core\ClassLoader::register(); diff --git a/CoreVersions/Baikal_0.1/Frameworks/BaikalAdmin/Core/ClassLoader.php b/CoreVersions/Baikal_0.1/Frameworks/BaikalAdmin/Core/ClassLoader.php index e229bf7..c5fe065 100644 --- a/CoreVersions/Baikal_0.1/Frameworks/BaikalAdmin/Core/ClassLoader.php +++ b/CoreVersions/Baikal_0.1/Frameworks/BaikalAdmin/Core/ClassLoader.php @@ -5,7 +5,7 @@ namespace BaikalAdmin\Core; class ClassLoader { public static function register() { - return spl_autoload_register(array(get_called_class(), 'loadClass')); + return spl_autoload_register(array(__CLASS__, 'loadClass')); } public static function loadClass($sFullClassName) { diff --git a/CoreVersions/Baikal_0.1/Frameworks/BaikalAdmin/Model/User.php b/CoreVersions/Baikal_0.1/Frameworks/BaikalAdmin/Model/User.php index a031e95..083a4ba 100644 --- a/CoreVersions/Baikal_0.1/Frameworks/BaikalAdmin/Model/User.php +++ b/CoreVersions/Baikal_0.1/Frameworks/BaikalAdmin/Model/User.php @@ -5,6 +5,7 @@ namespace BaikalAdmin\Model; class User extends \Flake\Core\Model\Db { const DATATABLE = "users"; const PRIMARYKEY = "id"; + const LABELFIELD = "username"; protected $oIdentityPrincipal = null; @@ -56,4 +57,37 @@ class User extends \Flake\Core\Model\Db { public function getMailtoURI() { return "mailto:" . rawurlencode($this->get("displayname") . " <" . $this->get("email") . ">"); } + + # Empty form, + public static function formEmpty($options = array()) { + $sClass = get_called_class(); + $oForm = new \Formal\Core\Form($sClass, $options); + + $oForm->add(new \Formal\Element\Text(array( + "prop" => "username", + "label" => "Username", + "validation" => "required" + ))); + + $oForm->add(new \Formal\Element\Text(array( + "prop" => "displayname", + "label" => "Display name", + "validation" => "required" + ))); + + $oForm->add(new \Formal\Element\Text(array( + "prop" => "email", + "label" => "Email", + "validation" => "required,email" + ))); + + return $oForm; + } + + public function formForInstance($options = array()) { + $oForm = self::formEmpty($options)->setModelInstance($this); + $oForm->element("username")->setOption("readonly", true); + + return $oForm; + } } \ No newline at end of file diff --git a/CoreVersions/Baikal_0.1/Frameworks/BaikalAdmin/Resources/Templates/User/Form.html b/CoreVersions/Baikal_0.1/Frameworks/BaikalAdmin/Resources/Templates/User/Form.html index 97c770f..c958697 100644 --- a/CoreVersions/Baikal_0.1/Frameworks/BaikalAdmin/Resources/Templates/User/Form.html +++ b/CoreVersions/Baikal_0.1/Frameworks/BaikalAdmin/Resources/Templates/User/Form.html @@ -1,30 +1 @@ -
- -
- Editing user get("username") ?> - -
- -
- " disabled="" /> - -
-
-
- -
- "/> -
-
-
- -
- "/> -
-
-
- - Close -
-
-
\ No newline at end of file + diff --git a/CoreVersions/Baikal_0.1/Frameworks/Flake/Core/Bootstrap.php b/CoreVersions/Baikal_0.1/Frameworks/Flake/Core/Bootstrap.php index f1f89f9..547d9a8 100755 --- a/CoreVersions/Baikal_0.1/Frameworks/Flake/Core/Bootstrap.php +++ b/CoreVersions/Baikal_0.1/Frameworks/Flake/Core/Bootstrap.php @@ -29,6 +29,7 @@ require_once(FLAKE_PATH_ROOT . 'Core/ClassLoader.php'); require_once(FLAKE_PATH_ROOT . "config.php"); if(!\Flake\Util\Tools::isCliPhp()) { + ini_set("html_errors", TRUE); session_start(); \Flake\Util\Tools::decode_GET(); } diff --git a/CoreVersions/Baikal_0.1/Frameworks/Flake/Core/ClassLoader.php b/CoreVersions/Baikal_0.1/Frameworks/Flake/Core/ClassLoader.php index d6b968d..bdd1366 100755 --- a/CoreVersions/Baikal_0.1/Frameworks/Flake/Core/ClassLoader.php +++ b/CoreVersions/Baikal_0.1/Frameworks/Flake/Core/ClassLoader.php @@ -5,7 +5,7 @@ namespace Flake\Core; class ClassLoader { public static function register() { - return spl_autoload_register(array(get_called_class(), 'loadClass')); + return spl_autoload_register(array(__CLASS__, 'loadClass')); } public static function loadClass($sFullClassName) { diff --git a/CoreVersions/Baikal_0.1/Frameworks/Flake/Core/Collection.php b/CoreVersions/Baikal_0.1/Frameworks/Flake/Core/Collection.php index ed91998..572a3d8 100755 --- a/CoreVersions/Baikal_0.1/Frameworks/Flake/Core/Collection.php +++ b/CoreVersions/Baikal_0.1/Frameworks/Flake/Core/Collection.php @@ -26,6 +26,15 @@ class Collection extends \Flake\Core\FLObject implements \Iterator { $key = key($this->aCollection); return ($key !== NULL && $key !== FALSE); } + + public function getForKey($sKey) { + $aKeys = $this->keys(); + if(!in_array($sKey, $aKeys)) { + throw new \Exception("\Flake\Core\Collection->getForKey(): key '" . $sKey . "' not found in Collection"); + } + + return $this->aCollection[$sKey]; + } public function &each() { list($key, $val) = each($this->aCollection); @@ -126,6 +135,14 @@ class Collection extends \Flake\Core\FLObject implements \Iterator { public function toArray() { return $this->aCollection; } + + # Create a new collection like this one + # This abstraction is useful because of CollectionTyped + + protected function newCollectionLikeThisOne() { + $oCollection = \Flake\Core\Collection(); + return $oCollection; + } /* * Méthode magique __call diff --git a/CoreVersions/Baikal_0.1/Frameworks/Flake/Core/CollectionTyped.php b/CoreVersions/Baikal_0.1/Frameworks/Flake/Core/CollectionTyped.php index 9879777..735274f 100755 --- a/CoreVersions/Baikal_0.1/Frameworks/Flake/Core/CollectionTyped.php +++ b/CoreVersions/Baikal_0.1/Frameworks/Flake/Core/CollectionTyped.php @@ -30,4 +30,12 @@ class CollectionTyped extends \Flake\Core\Collection { parent::push($mMixed); } + + # Create a new collection like this one + # This abstraction is useful because of CollectionTyped + + protected function newCollectionLikeThisOne() { + $oCollection = \Flake\Core\CollectionTyped($this->sTypeClassOrProtocol); + return $oCollection; + } } \ No newline at end of file diff --git a/CoreVersions/Baikal_0.1/Frameworks/Flake/Core/FLObject.php b/CoreVersions/Baikal_0.1/Frameworks/Flake/Core/FLObject.php index 06f2490..42098a7 100755 --- a/CoreVersions/Baikal_0.1/Frameworks/Flake/Core/FLObject.php +++ b/CoreVersions/Baikal_0.1/Frameworks/Flake/Core/FLObject.php @@ -7,8 +7,16 @@ class FLObject { return print_r($this, TRUE); } - public static function getClass() { - return get_called_class(); + public function getClass() { +// throw new \Exception("getClass() is deprecated"); + if(!isset($this)) { +# echo "STATIC
"; + return get_called_class(); + } else { +# echo "INSTANCE
"; +# debug($this); + return get_class($this); + } } public function isA($sClassOrProtocolName) { diff --git a/CoreVersions/Baikal_0.1/Frameworks/Flake/Core/Model.php b/CoreVersions/Baikal_0.1/Frameworks/Flake/Core/Model.php index d2f1056..3bbc189 100755 --- a/CoreVersions/Baikal_0.1/Frameworks/Flake/Core/Model.php +++ b/CoreVersions/Baikal_0.1/Frameworks/Flake/Core/Model.php @@ -26,4 +26,8 @@ abstract class Model extends \Flake\Core\FLObject { throw new \Exception("\Flake\Core\Model->set(): property " . htmlspecialchars($sPropName) . " does not exist on " . self::getClass()); } + + public function getLabel() { + return $this->get($this::LABELFIELD); + } } \ No newline at end of file diff --git a/CoreVersions/Baikal_0.1/Frameworks/Flake/Util/Tools.php b/CoreVersions/Baikal_0.1/Frameworks/Flake/Util/Tools.php index a9ccba4..613bcbc 100755 --- a/CoreVersions/Baikal_0.1/Frameworks/Flake/Util/Tools.php +++ b/CoreVersions/Baikal_0.1/Frameworks/Flake/Util/Tools.php @@ -428,16 +428,16 @@ TEST; public static function is_a($object, $class) { if(is_object($object)) return $object instanceof $class; if(is_string($object)){ - if(is_object($class)) $class=get_class($class); + if(is_object($class)) $class=get_class($class); - if(class_exists($class)) return is_subclass_of($object, $class) || $object==$class; - if(interface_exists($class)) { - $reflect = new \ReflectionClass($object); - return $reflect->implementsInterface($class); - } - - } - return false; + if(class_exists($class)) return is_subclass_of($object, $class) || $object==$class; + if(interface_exists($class)) { + $reflect = new \ReflectionClass($object); + return $reflect->implementsInterface($class); + } + + } + return false; } public static function HTTPStatus($iCode, $sMessage) { diff --git a/CoreVersions/Baikal_0.1/Frameworks/Formal/Core.php b/CoreVersions/Baikal_0.1/Frameworks/Formal/Core.php new file mode 100644 index 0000000..83a8df4 --- /dev/null +++ b/CoreVersions/Baikal_0.1/Frameworks/Formal/Core.php @@ -0,0 +1,9 @@ +PHP Autoload Error. Cannot find ' . $sFullClassName . ''; + echo "
" . print_r(debug_backtrace(), TRUE) . "
"; + die(); + } + } +} diff --git a/CoreVersions/Baikal_0.1/Frameworks/Formal/Core/Form.php b/CoreVersions/Baikal_0.1/Frameworks/Formal/Core/Form.php new file mode 100644 index 0000000..467e699 --- /dev/null +++ b/CoreVersions/Baikal_0.1/Frameworks/Formal/Core/Form.php @@ -0,0 +1,139 @@ +sModelClass = $sModelClass; + $this->aOptions = array_merge($this->aOptions, $aOptions); + $this->oElements = new \Flake\Core\CollectionTyped("\Formal\Element"); + } + + public function option($sName) { + return $this->aOptions[$sName]; + } + + public function setModelInstance($oModelInstance) { + if(!\Flake\Util\Tools::is_a($oModelInstance, $this->sModelClass)) { + throw new \Exception("\Formal\Core->setModelInstance(): Given instance is not of class '" . $this->sModelClass . "'"); + } + + $this->oModelInstance = $oModelInstance; + + $this->oElements->reset(); + foreach($this->oElements as $oElement) { + $oElement->setValue( + $this->getModelInstance()->get( + $oElement->option("prop") + ) + ); + } + + return $this; + } + + public function getModelInstance() { + return $this->oModelInstance; + } + + public function hasModelInstance() { + return !is_null($this->getModelInstance()); + } + + public function add(\Formal\Element $oElement) { + $this->oElements->push($oElement); + } + + public function element($sPropName) { + $this->oElements->reset(); + foreach($this->oElements as $oElement) { + if($oElement->option("prop") === $sPropName) { + return $oElement; + } + } + + throw new \Exception("\Formal\Core\Form->element(): Element prop='" . $sPropName . "' not found"); + } + + public function execute() { + if(!$this->hasModelInstance()) { + return; + } + + $this->oElements->reset(); + foreach($this->oElements as $oElement) { + # If element is readonly, skip process + if($oElement->option("readonly")) { + continue; + } + + $sPropName = $oElement->option("prop"); + + # posted value is fetched, then passes to element before persistance + $sPostValue = $this->postValue($sPropName); + $oElement->setValue($sPostValue); + + $this->getModelInstance()->set( + $sPropName, + $oElement->value() + ); + } + + $this->getModelInstance()->persist(); + } + + public function postValue($sPropName) { + # could be as well \Flake\Util\Tools::POST($sPropName) + return \Flake\Util\Tools::POST($this->element($sPropName)->option("prop")); + } + + public function render() { + $aHtml = array(); + + $this->oElements->reset(); + foreach($this->oElements as $oElement) { + $aHtml[] = $oElement->render(); + } + + $elements = implode("\n", $aHtml); + + if($this->hasModelInstance()) { + $sTitle = "Editing " . $this->getHumanModelName() . " " . $this->getModelInstance()->getLabel() . ""; + } else { + $sTitle = "Creating new " . $this->getHumanModelName(); + } + + $sSubmittedFlagName = $this->sModelClass . "::submitted"; + $sCloseUrl = $this->option("closeurl"); + + $sHtml =<< + +
+ {$sTitle} + {$elements} +
+ + Close +
+
+ +HTML; + + return $sHtml; + } + + public function submitted() { + return intval(\Flake\Util\Tools::POST($this->sModelClass . "::submitted")) === 1; + } + + public function getHumanModelName() { + return array_pop(explode("\\", $this->sModelClass)); + } +} \ No newline at end of file diff --git a/CoreVersions/Baikal_0.1/Frameworks/Formal/Element.php b/CoreVersions/Baikal_0.1/Frameworks/Formal/Element.php new file mode 100644 index 0000000..1c240a2 --- /dev/null +++ b/CoreVersions/Baikal_0.1/Frameworks/Formal/Element.php @@ -0,0 +1,35 @@ + FALSE, + ); + + protected $sValue = ""; + + public function __construct($aOptions) { + $this->aOptions = array_merge($aOptions, $this->aOptions); + } + + public function option($sName) { + if(array_key_exists($sName, $this->aOptions)) { + return $this->aOptions[$sName]; + } + + throw new \Exception("\Formal\Element->option(): Option '" . htmlspecialchars($sName) . "' not found."); + } + + public function setOption($sOptionName, $sOptionValue) { + $this->aOptions[$sOptionName] = $sOptionValue; + } + + public function value() { + return $this->sValue; + } + + public function setValue($sValue) { + $this->sValue = $sValue; + } +} \ No newline at end of file diff --git a/CoreVersions/Baikal_0.1/Frameworks/Formal/Element/Text.php b/CoreVersions/Baikal_0.1/Frameworks/Formal/Element/Text.php new file mode 100644 index 0000000..49882ac --- /dev/null +++ b/CoreVersions/Baikal_0.1/Frameworks/Formal/Element/Text.php @@ -0,0 +1,29 @@ +value()); + $label = $this->option("label"); + $prop = $this->option("prop"); + $disabled = ""; + $class = ""; + + if($this->option("readonly") === TRUE) { + $class = " disabled"; + $disabled = " disabled"; + } + + $sHtml =<< + +
+ +
+ +HTML; + return $sHtml; + } +} \ No newline at end of file diff --git a/Specific/db/baikal.sqlite b/Specific/db/baikal.sqlite index 2a55238..d71204f 100755 Binary files a/Specific/db/baikal.sqlite and b/Specific/db/baikal.sqlite differ