public function __invoke($_id = false, $action = false) { // save the container, sometimes gets unset with toSelf function.. or some other recursion.. $container = $this->_container; // load the head to include css etc. so the 'preview' works properly if ($_id != false) { // do some more sanitization ... get keys from this and compare? (with default values and pull the $_r out // and see if all those fields exist.. // this could cause problems just to avoid looking up a record.. if we // do it this way we always get the most 'latest' edition and not whatever changes // the user made... but then we need to gracefully handle conflicts... // to avoid can avoid the second post lookup ... $r = isset($_POST['_id']) && $_POST['_id'] == $_id ? $_POST : $this->get($_id); if ($action == false) { $this->arrayToSelf($r); return $this; } else { $_POST = $r; } } elseif ($_id == false && $action != false) { !class_exists('poform') and (require 'poform.php'); $this->editor = poform::auto($this); // load the head to include stylesheets in the container // for when the record cannot be inserted (due to invalid fields) foreach ($container['head'] as $key => $value) { $this->head .= self::$key($value); } return $this; } elseif (!$action && $_id) { // check post object for an $_ID and do something else >> $this->arrayToSelf($r); // simply show the object if we are displaying ... return $this; } elseif ($_id && $action && isset($_POST['_rev']) || isset($_POST['_id'])) { $this->footer = $this->update(true, $_POST); } // load poform only if we need a web editor !class_exists('poform') and (require 'poform.php'); if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] != "POST") { // set the $_POST to $r so poform properly populates field values isset($r) and $_POST = $r; // dont have to worry about reloading perform, because if !$action we return $this $this->editor = poform::auto($this); } if ($action != 1 || $action == NULL) { // server request method is post ... // force an insert with whatever keys exist ? this may not be nessary if (isset($r['_rev'])) { $_POST['_rev'] = $r['_rev']; // probably dont need this .. unset($_POST['_r']); } $no_update = false; if (isset($this->_r)) { foreach ($this->_r as $key) { !array_key_exists($key, $_POST) || $_POST[$key] == '' and $no_update = true; } // throws weird error (end const not found assumed end) notice // end; if (!$no_update) { if ($_id != false) { // $this->footer .= $this->update(true,$_POST ); $this->update(true, $_POST); } else { // maybe even filter empty values? //unset($_POST['rev']); $id_check = str_replace(array(' ', '_'), '', $_POST['_id']); // this check will fail on spaces anyway ... if (ctype_alnum($id_check)) { $the_id = str_replace(' ', '_', $_POST['_id']); unset($_POST['_id']); } else { // handle this better... die('ID is not alpha-numeric'); } $put = $this->put($_POST, $the_id); /* DO THIS BETTER .. probably let $this->put return false */ // or just return a $put['error'] in the abstract class ? if (isset($put['error']) || $put == false) { $r = $this->get($the_id); $_POST['_rev'] = $r['_rev']; $this->update($the_id, $_POST); } $_POST['_id'] = $the_id; // puts just fine ... how to then return the proper values ? return $this($the_id, 1); } } else { // show errors and return $this .. return $this(isset($the_id) ? $the_id : '', 1); } } } // hmm probably make a form ... ? $this->_container = $container; if ($action != false) { // weird hack to get the record to display in the editor (as you are editing it, and // as new records are created to automatically go into 'edit' mode until the page is reloaded // is this call needed ? sometimes... in the event that we are attempting to not include a field // in an existing record that is required // could i make this easier ? $this->arrayToSelf($r); $called_class = get_called_class(); $called_class = new $called_class(); $this->editor = poform::auto($called_class); $called_class = $called_class($_id); $called_class->editor = $this->editor; $called_class->_container = $this->_container; return $called_class; } return $this; }
/* Example shows a custom class, use of load and make, coded string syntax for checkbox, radio and select options (which require assoc. arrays). To Do : Allow more control over labels/ tag attributes by using html5_core tag objects */ require 'poform.php'; class form_class { public $setting = array('form_type' => 'form', array('setting3' => 'select 1-Setting 1:2-Setting 2:3-Setting 3', 'setting2' => '')); // A blank parameter, will default to a text input w/o value. public $param; // to define a selection list set its value to something similar to below // a- b- etc refers to the value, while the text after it and before the ':' defines // what appears in the selection list, or use helper function to convert assoc. arrays public $selection = 'select a-Value A:b-value B:c-value C'; public $a = array('select:numbers' => array('1' => 'Value 1', '2' => 'Value 2', '3' => 'value 3')); public $checkbox = array('checkbox:fav_number' => array('1' => 'Value 1', '2' => 'Value 2', '3' => 'value 3')); public $radio = array('radio:preference' => array('y' => 'No', 'n' => 'Yes', 'm' => 'Maybe')); // this syntax needs improvement, but works with existing structures for special input tag types public $email = array('email:email_address' => array('' => 'email')); } // or create custom coded arrays that specify the action of the array (checkbox/radio/select) $a = new form_class(); print_r($a); $b = poform::load($a); print_r($b); echo poform::make($b);