based on an object */ namespace HalfMoon; require_once("form_common.php"); class FormHelper extends FormTagHelper { public function form_for($obj, $url_or_obj, $options = array(), \Closure $form_content) { if (!$obj) throw new HalfMoonException("invalid object passed to form_for()"); $this->form_object = $obj; return $this->output_form_around_closure($url_or_obj, $options, $form_content); } public function form_content_for($obj, $url_or_obj, $options = array(), \Closure $form_content) { if (!$obj) throw new HalfMoonException("invalid object passed to form_for()"); $this->form_object = $obj; $html = Utils::to_s($this, $form_content); $this->form_object = null; return $html; } /* the prefix to use for class and id names for fields */ protected function form_prefix() { if (empty($this->form_prefix)) return strtolower(get_class($this->form_object)); else return $this->form_prefix; } /* provide an html object id to use for a given field name */ public function prefixed_field_id($field) { return preg_replace("/[^a-z0-9]/", "_", $this->form_prefix() . "_" . $field); } /* provide an html object name to use for a given field name */ public function prefixed_field_name($field) { return $this->form_prefix() . "[" . $field . "]"; } /* return the value of the particular field for the form object */ protected function value_for_field($field, $options) { /* value is being statically overridden */ if (isset($options["value"])) return $options["value"]; /* if we have an array-looking name of field[var], use an AR getter of * field(var) */ elseif (preg_match("/^(.+)\[([^\]]+)\]$/", $field, $m)) return $this->form_object->{"get_" . $m[1]}($m[2]); else return $this->form_object->$field; } /* honor names/ids passed through as options */ private function set_field_id_and_name($field, $options) { if (!isset($options["id"])) $options["id"] = $this->prefixed_field_id($field); if (!isset($options["name"])) $options["name"] = $this->prefixed_field_name($field); return $options; } /* create an with a hidden input field to detect * when the checkbox has been unchecked (see rails docs for check_box, but * since php presents $_GET and $_POST with the last seen value, we have to * reverse the order of the checkbox and hidden input field) */ public function check_box($field, $options = array(), $checked_value = 1, $unchecked_value = 0) { if (!$this->form_object) throw new HalfMoonException("no form object; you probably wanted " . "check_box_tag"); $options = $this->set_field_id_and_name($field, $options); return "" . $this->wrap_field_with_errors($field, $this->check_box_tag($field, $checked_value, (bool)$this->value_for_field($field, $options), $options) ); } /* create an file upload field */ public function file_field($field, $options = array()) { if (!$this->form_object) throw new HalfMoonException("no form object; you probably wanted " . "file_field_tag"); $options = $this->set_field_id_and_name($field, $options); return $this->wrap_field_with_errors($field, $this->file_field_tag($field, $options) ); } /* create a hidden field */ public function hidden_field($field, $options = array()) { if (!$this->form_object) throw new HalfMoonException("no form object; you probably wanted " . "hidden_field_tag"); $options = $this->set_field_id_and_name($field, $options); return $this->hidden_field_tag($field, $this->value_for_field($field, $options), $options); } /* create a