Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <? defined('SYSPATH') OR die('No direct access allowed.');
- /**
- * A Formo enhanced version of ORM.
- *
- * @todo - credits/license.
- *
- */
- class FOORM extends ORM {
- protected $_formo = null;
- /**
- * make it so that __set calls to the orm will automatically populate the formo fields. Also when trying to save, it assumes that the formo
- * fields are the most recent and saves them
- *
- * @var boolean
- */
- public $auto_formo = false;
- /**
- * Load the formo object when needed - for most ORM actions that will not need to set values, formo isn't needed.
- *
- * @param unknown_type $column
- * @return unknown
- */
- public function __get($column) {
- if ($column == 'formo') {
- if (!isset($this->_formo)) {
- $this->_formo = $this->formo();
- }
- return $this->_formo;
- }
- return parent::__get($column);
- }
- /**
- * if it's an formo field - set it there also. This is needed because formo is going to be the default model validation
- * so anytime a field is set in the ORM model, it needs to be set in the internal Formo object so the validation will work.
- * Otherwise - you could do
- * $user->name = $invalid_username
- * $user->validate()===true (since validate is now using the formo validation)
- *
- * @param unknown_type $column
- * @param unknown_type $value
- * @return unknown
- */
- public function __set($column,$value) {
- //@todo - should I be able to set this here?
- if ($column == 'formo') {
- $this->_formo = $value;
- return;
- }
- if ($this->auto_formo && isset($this->_formo->$column)) {
- $this->_formo->$column->value = $value;
- }
- return parent::__set($column,$value);
- }
- /**
- * In case the formo has been changed but the orm not - we copy the formo to the orm.
- *
- * @return unknown
- */
- public function save() {
- if ($this->auto_formo ) {
- // if the _formo object is null -
- // to validate need to create the formo object then map my orm values to it.
- if ($this->_formo===null) {
- $this->formo();
- }
- $this->map_formo_orm();
- if (!$this->_formo->validate()) {
- Kohana::log('info','formo validation failed');
- return false;
- }
- // $this->formo->errors will have the errors
- }
- return parent::save();
- }
- /**
- * Internal Formo object used for all the validation
- *
- * @return unknown
- */
- public function validate() {
- if ($this->_formo==null) {
- $this->formo();
- }
- return $this->_formo->validate();
- }
- /**
- * Create an empty formo object if one is not passed, otherwise create one.
- * Children will call this at the top to simply do this work.
- *
- * // example of child formo
- * public function formo($formo=null) {
- * parent::formo($formo);
- *
- * $this->_formo->add('fields');
- *
- * $this->map_orm_formo();
- * return $this->_formo;
- * }
- *
- *
- * @param Formo $formo
- * @return Formo
- */
- public function formo($formo = null) {
- // allow them to pass in a formo object with some things already set, like orm, defaults,etc.
- if ($formo == null) {
- $this->_formo = Formo::Factory();
- } elseif (!is_a($formo,'Formo')) {
- throw(new Exception('formo() expecting Formo'));
- } else {
- $this->_formo = $formo;
- }
- return $this->_formo;
- }
- /**
- * Map the orm variables to the formo object - used when first creating the formo - or reloading
- *
- */
- protected function map_orm_formo() {
- foreach ($this->_formo->get_fields() as $field=>$value) {
- // there maybe formo elements that are not ORM fields (eg csrf) - only use object elements
- if (isset($this->$field)) $this->_formo->$field->value = $this->$field;
- }
- }
- /**
- * copy the formo variables to the orm - used when a form has been posted and validated it's time to save.
- *
- */
- protected function map_formo_orm() {
- foreach ($this->_formo->get_values() as $field=>$value) {
- if (isset($this->table_columns[$field])) {
- $this->$field = $value;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment