| | 59 | public function admin() |
| | 60 | { |
| | 61 | $valid = ! empty($_POST); |
| | 62 | |
| | 63 | $_POST = Validation::factory($_POST) |
| | 64 | ->pre_filter('trim') |
| | 65 | ->add_rules('title', 'required', 'length[4,32]') |
| | 66 | ->add_rules('description', 'required', 'length[4,127]') |
| | 67 | ->add_rules('link', 'length[6,127]', 'valid::url') |
| | 68 | ->add_rules('address', 'required', 'length[4,127]') |
| | 69 | ->add_callbacks('address', array($this, '_admin_check_address')); |
| | 70 | |
| | 71 | if ($_POST->validate()) |
| | 72 | { |
| | 73 | // Create a new location |
| | 74 | $location = ORM::factory('location'); |
| | 75 | |
| | 76 | // |
| | 77 | foreach ($_POST->as_array() as $key => $val) |
| | 78 | { |
| | 79 | $location->$key = $val; |
| | 80 | } |
| | 81 | |
| | 82 | echo Kohana::debug($_POST->as_array()); |
| | 83 | } |
| | 84 | |
| | 85 | if ($errors = $_POST->errors()) |
| | 86 | { |
| | 87 | foreach ($errors as $input => $rule) |
| | 88 | { |
| | 89 | // Add the errors |
| | 90 | $_POST->message($input, Kohana::lang("gmaps.form.$input")); |
| | 91 | } |
| | 92 | } |
| | 93 | |
| | 94 | View::factory('gmaps/admin')->render(TRUE); |
| | 95 | } |
| | 96 | |
| | 97 | public function _admin_check_address(Validation $array, $input) |
| | 98 | { |
| | 99 | if ($array[$input] == '') |
| | 100 | return; |
| | 101 | |
| | 102 | // Fetch the lat and lon via Gmap |
| | 103 | list ($lat, $lon) = Gmap::address_to_ll($array[$input]); |
| | 104 | |
| | 105 | if ($lat === NULL OR $lon === NULL) |
| | 106 | { |
| | 107 | // Add an error |
| | 108 | $array->add_error($input, 'address'); |
| | 109 | } |
| | 110 | else |
| | 111 | { |
| | 112 | // Set the latitude and longitude |
| | 113 | $_POST['lat'] = $lat; |
| | 114 | $_POST['lon'] = $lon; |
| | 115 | } |
| | 116 | } |
| | 117 | |