a tiny mvc framework for php using php-activerecord
at master 133 lines 3.5 kB view raw
1<?php 2/* 3 <form>-building helper functions 4*/ 5 6namespace HalfMoon; 7 8class FormHelperCommon extends Helper { 9 public $controller = null; 10 public $form_object = null; 11 public $html_helper = null; 12 public $form_prefix = null; 13 14 public function __construct() { 15 $this->html_helper = new HtmlHelper; 16 } 17 18 /* the guts of form_for() and form_tag() */ 19 protected function output_form_around_closure($url_or_obj, 20 $options = array(), \Closure $form_content) { 21 if (!isset($options["method"])) 22 $options["method"] = "post"; 23 24 if (!empty($options["multipart"])) { 25 unset($options["multipart"]); 26 $options["enctype"] = "multipart/form-data"; 27 } 28 29 if (!empty($options["form_prefix"])) { 30 $this->form_prefix = $options["form_prefix"]; 31 unset($options["form_prefix"]); 32 } 33 34 print "<form" 35 . $this->options_to_s($options) 36 . " action=\"" 37 . h($this->html_helper->link_from_obj_or_string($url_or_obj)) 38 . "\"" 39 . ">"; 40 41 if (strtolower($options["method"]) != "get") 42 print "<input" 43 . " name=\"authenticity_token\"" 44 . " type=\"hidden\"" 45 . " value=\"" . h($this->controller->form_authenticity_token()) 46 . "\"" 47 . " />"; 48 49 print Utils::to_s($this, $form_content, $this->controller); 50 51 print "</form>"; 52 53 $this->form_object = null; 54 55 return $this; 56 } 57 58 /* generate <option> tags for an array of options for a <select> */ 59 protected function options_for_select($choices, $selected) { 60 $str = ""; 61 $is_assoc = Utils::is_assoc($choices); 62 $array_of_arrays = (bool)(!$is_assoc && is_array($choices[0])); 63 64 foreach ($choices as $key => $val) { 65 if ($array_of_arrays) { 66 if (Utils::is_assoc($val)) { 67 $key = Utils::A(array_keys($val), 0); 68 $val = Utils::A(array_values($val), 0); 69 } else { 70 $key = $val[0]; 71 $val = $val[1]; 72 } 73 } elseif (!$is_assoc) 74 $key = $val; 75 76 if ($selected === $key) 77 $is_selected = true; 78 elseif (is_numeric($selected) && is_numeric($key) && 79 ($selected == $key)) 80 $is_selected = true; 81 /* TODO: handle 0=false, 1=true, true="true", etc. */ 82 else 83 $is_selected = false; 84 85 $str .= "<option value=\"" . raw_or_h($key) . "\"" 86 . ($is_selected ? " selected" : "") 87 . ">" . raw_or_h($val) . "</option>"; 88 } 89 90 return $str; 91 } 92 93 /* convert an array of options to html element options */ 94 protected function options_to_s($options) { 95 if (!is_array($options)) 96 throw new HalfMoonException("invalid argument passed; expected " 97 . "options array"); 98 99 $opts_s = ""; 100 foreach ($options as $k => $v) 101 $opts_s .= " " . $k . "=\"" . raw_or_h($v) . "\""; 102 103 return $opts_s; 104 } 105 106 /* generate <option> tags for each identifier in php's timezone list */ 107 protected function time_zone_options_for_select($selected) { 108 $str = ""; 109 110 foreach (\DateTimeZone::listIdentifiers() as $tz) 111 $str .= "<option value=\"" . $tz . "\"" 112 . (strtolower($selected) === strtolower($tz) ? 113 " selected" : "") . ">" . h($tz) . "</option>"; 114 115 return $str; 116 } 117 118 /* for each attribute in a form_for() object that has errors, output a div 119 * around it that should be styled to stand out */ 120 protected function wrap_field_with_errors($field, $field_html) { 121 if (!$this->form_object) 122 throw new HalfMoonExceptions("wrap_field_with_errors called when " 123 . "no form object"); 124 125 if ($this->form_object->errors && 126 $this->form_object->errors->on($field)) 127 return "<div class=\"fieldWithErrors\">" . $field_html . "</div>"; 128 else 129 return $field_html; 130 } 131} 132 133?>