public function up() { //we'll need some places where the events can take place $locations = array('Rīga', 'Valmiera', 'Ventspils'); foreach ($locations as $location) { $loc = \Model_Orm_Location::forge(); $loc->title = $location; $loc->save(); } //and let's create at least one event as well $demo_event = \Model_Orm_Event::forge(); $demo_event->title = "Notikums pēc nedēļas"; $demo_event->description = "Kaut kas, kam jānotiek nedēļu vēlāk nekā šī skripta izpilde."; $startdate = \Fuel\Core\Date::forge(time() + 7 * 24 * 60 * 60); $demo_event->start = $startdate->format('mysql', false); //'2013-11-27 07:00:00'; $demo_event->location_id = 1; //pieņemsim, ka Rīgā; $demo_event->save(); //the event shouldn't be empty - some agenda items $agenda_items = array('Notikuma pats, pats sākums', 'Kaut kad drusku vēlāk', 'Vēl mazliet vēlāk', 'Un nu jau arī beigas'); foreach ($agenda_items as $agenda_item) { $demo_agenda = \Model_Orm_Agenda::forge(); $demo_agenda->title = $agenda_item; $demo_agenda->event = $demo_event; $demo_agenda->save(); } //we also need some users. at least two. \Auth::instance()->create_user("*****@*****.**", "fuel_dev", "*****@*****.**", 100, array("verified" => true, "verification_key" => md5(mt_rand(0, mt_getrandmax())))); \Auth::instance()->create_user("*****@*****.**", "fuel_dev", "*****@*****.**", 1, array("verified" => true, "verification_key" => md5(mt_rand(0, mt_getrandmax())))); }
/** * A validation function which checks if the number passed * to it is a valid location ID. * @param type $val * @return boolean */ public static function _validation_valid_location($val) { if (Model_Orm_Location::find($val) == null) { Validation::active()->set_message('valid_location', 'The field "location" must refer to a valid location.'); return false; } return true; }
public function action_search($term = null) { if ($term == null) { $term = Input::get("term"); } //only ajax requests served here //(! Input::is_ajax()) and Response::redirect("location"); $clean_query = Security::clean($term); $data["locations"] = array(); if ($clean_query != "") { $data["locations"] = Model_Orm_Location::query()->where("title", "like", $clean_query . "%")->get(); } $response = Response::forge(View::forge("location/search", $data)); $response->set_header("Content-Type", "application/json"); return $response; }
/** * Automatic creation of input forms. * @return view data */ public function action_fieldset() { $output = ""; //the generated output will be collected here $eventinstance = new Model_Orm_Event(); //a new form which is identified as "eventform" //in current view $inputform = Fuel\Core\Fieldset::forge("eventform"); //the fieldset class loads field information from our model $inputform->add_model($eventinstance); //since the location field should be a drop-down select box, //we add it manually. //The actual list of locations is loaded by the Model_Orm_Location model $inputform->add("location", "Select a location", array("type" => "select", "name" => "location_id", "options" => Model_Orm_Location::get_locations())); //the Fieldset does not automatically add buttons, hence we do $inputform->add("submit", "", array("type" => "submit", "value" => "Add", "class" => "btn medium primary")); //this re-loads data from $_POST array if the form is re-submitted. $inputform->repopulate(); //execute validations described in the model class if ($inputform->validation()->run()) { //this is the good case! $new_event = Model_Orm_Event::forge(); $fields = $inputform->validated(); $new_event->title = $fields["title"]; $new_event->description = $fields["description"]; $new_event->start = time(); $new_event->save(); //always do a redirect after saving an item! Response::redirect("event/orm"); } else { //there have been some problems, output them to the client foreach ($inputform->validation()->error() as $error) { $output .= "<p>" . $error . "</p>"; } } //the 'build' actually generates all HTML code $output .= $inputform->build(); return Response::forge($output); }
public function action_edit($id = null) { //looks up the even in the database //if anything is not OK - redirects back to the list of events is_null($id) and Response::redirect('event'); $event = Model_Orm_Event::find($id, array("related" => array("location"))); is_null($event) and Response::redirect('Event'); //not POST = just read from database if (Input::method() == 'POST') { $val = Model_Orm_Event::validate("edit"); if ($val->run()) { //validation is OK! $event->title = $val->validated("title"); $event->description = $val->validated("description"); $event->start = $val->validated("start"); $event->location = Model_Orm_Location::find(Input::post("location")); if ($event->save()) { Session::set_flash("success", "Changes saved successfuly!"); } else { Session::set_flash("error", "Somehow could not save the item."); } Response::redirect("event/view/" . $event->id); } else { //POST data passed, but something wrong with validation Session::set_flash("error", $val->error()); } } $data["event"] = $event; $data["locations"] = Model_Orm_Location::get_locations(); $this->add_rich_form_scripts(); $this->template->title = "Editing the event " . $event->title; $this->template->page_content = View::forge("event/edit", $data); }