public function add($client = NULL) { $client_id = $this->input->post('client') ? $this->input->post('client') : $client; if ($client_id == '' and $client_id != '-') { url::redirect('clients/new?client=' . urlencode($this->input->post('client_new'))); } $invoices = ORM::factory('invoice'); $clients = $this->cache->get('client') ? $this->cache->get('client') : ORM::factory('client')->find_all_as_array(); // $client_list = array(); foreach ($clients as $client) { $client_list[$client['id']] = $client['company']; } $data = array('hour', 'day', 'service', 'product'); $form = Formo::factory('invoice_add')->set('class', 'simple-form')->add('invoice_id', array('class' => 'size', 'label' => 'Invoice ID'))->add('po_number', array('class' => 'size', 'label' => 'P.O. Number', 'required' => FALSE))->add_select('client', $client_list, array('class' => 'size', 'value' => $client_id))->add_textarea('notes', array('class' => 'size', 'required' => FALSE))->add('qty', array('class' => 'qty'))->add_select('type', $data)->add_textarea('description')->add('unit_price', array('class' => 'qty'))->add('submit', 'Submit'); if ($form->validate()) { // echo Kohana::debug($form); die; $invoice = ORM::factory('invoice'); $invoice->invoice_no = $form->invoice_id->value; $invoice->po_number = $form->po_number->value; $invoice->client_id = $form->client->value; $invoice->notes = $form->notes->value; $invoice->save(); $item = ORM::factory('item'); $item->qty = $form->qty->value; $item->description = $form->description->value; $item->unit_price = $form->unit_price->value; $item->type = $form->type->value; $item->invoice_id = $invoice->id; $item->save() and $this->cache->delete_tag('clients'); url::redirect('invoices'); } $this->template->content = new View('invoices/add', $form->get(TRUE)); $this->template->content->client_name = $client_list[$client_id]; $this->template->content->item_view = new View('invoices/items', $form->get(TRUE)); }
public function article($uri) { $this->template->content = View::factory('blog/view')->bind('post', $post)->bind('comments', $comments)->bind('form', $form); $post = ORM::factory('blog_post', (string) $uri); // Show 404 if we don't find posts if (!$post->loaded) { Event::run('system.404'); } $comments = $post->blog_comments; $this->head->title->prepend($post->title); if (!($post->comment_status === 'open' and config::get('blog.comment_status') === 'open')) { return; } $form = Formo::factory()->plugin('csrf')->add('text', 'author', array('label' => __('Name')))->add('text', 'email', array('label' => __('Email')))->add('text', 'url', array('label' => __('Homepage')))->add('textarea', 'content', array('label' => __('Comment')))->add('submit', 'submit', array('label' => __('Submit')))->pre_filter('all', 'trim')->pre_filter('author', 'security::xss_clean')->pre_filter('content', 'security::xss_clean')->pre_filter('url', 'security::xss_clean')->pre_filter('url', 'format::url')->add_rule('author', 'required', __('You must provide your name'))->add_rule('author', 'length[2,40]', __('Your Name is too long'))->add_rule('email', 'valid::email', __('Email address is not valid'))->add_rule('content', 'required', __('You must enter a comment')); if (config::get('blog.enable_captcha') === 'yes') { $form->add('captcha', 'security', array('label' => __('Security code'))); $form->security->error_msg = __('Invalid security code'); } if ($form->validate()) { $comment = ORM::factory('blog_comment'); $comment->author = $form->author->value; $comment->email = $form->email->value; $comment->content = $form->content->value; $comment->url = $form->url->value; $comment->ip = $this->input->ip_address(); $comment->agent = Kohana::$user_agent; $comment->date = date("Y-m-d H:i:s", time()); $post->add_comment($comment); Event::run('blog.comment_added', $comment); Cache::instance()->delete('s7n_blog_feed'); Cache::instance()->delete('s7n_blog_feed_comments'); url::redirect($post->url()); } $form = View::factory('blog/form_comment', $form->get(TRUE)); }
public function index() { $this->head->title->append(__('Settings')); $this->template->title = __('Settings'); $form = Formo::factory()->plugin('csrf')->add('text', 'site_title', array('label' => __('Site title'), 'value' => config::get('s7n.site_title')))->add_select('theme', theme::available(), array('label' => __('Theme'), 'value' => config::get('s7n.theme')))->add('submit', 'submit', array('label' => __('Save'))); if ($form->validate()) { config::set('s7n.site_title', $form->site_title->value); config::set('s7n.theme', $form->theme->value); message::info(__('Settings edited successfully'), 'admin/settings'); } $this->template->content = View::factory('settings/settings', $form->get(TRUE)); }
/** * @dataProvider provider_attr */ public function test_attr(array $construct, $attr, $val, array $checks) { $expected = TRUE; $result = TRUE; $field = Formo::factory($construct); if (is_array($attr)) { $field->attr($attr); } else { $field->attr($attr, $val); } foreach ($checks as $key => $value) { if ($field->attr($key) !== $value) { $result = FALSE; } } $this->assertSame($expected, $result); }
public function edit($id) { $client = ORM::factory('client', (int) $id); $form = Formo::factory('client_edit')->set('class', 'smart-form')->add('company', array('class' => 'size', 'value' => $client->company))->add('address', array('class' => 'size', 'value' => $client->address))->add('address1', array('class' => 'size', 'value' => $client->address1, 'required' => FALSE))->add('city', array('class' => 'size', 'value' => $client->city))->add('postcode', array('class' => 'size', 'value' => $client->postcode))->add('phone', array('class' => 'size', 'value' => $client->phone))->add('fax', array('class' => 'size', 'value' => $client->fax, 'required' => FALSE))->add('url', array('label' => 'Website', 'class' => 'size', 'value' => $client->url, 'required' => FALSE))->add_rule('url', 'valid::url')->add('submit', 'Submit'); if ($form->validate()) { $client->company = $form->company->value; $client->address = $form->address->value; $client->address1 = $form->address1->value; $client->city = $form->city->value; $client->postcode = $form->postcode->value; $client->phone = $form->phone->value; $client->fax = $form->fax->value; $client->url = $form->url->value; $client->save() and $this->cache->delete_tag('clients') and url::redirect('clients'); } $this->template->content = new View('clients/add'); $this->template->content->form = $form; }
public function database_setup() { $form = Formo::factory('database_setup')->set('class', 'smart-form')->add('host', array('class' => 'size'))->add('username', array('class' => 'size'))->add('password', array('class' => 'size', 'required' => FALSE))->type('password')->add('database', array('class' => 'size'))->add('prefix', array('class' => 'size', 'value' => 'bc_'))->add('checkbox', 'drop', array('label' => 'Drop Tables', 'required' => FALSE))->add('checkbox', 'data', array('label' => 'Insert Data', 'required' => FALSE))->add('submit', 'submit', array('value' => __('Install'), 'class' => 'button')); if ($form->validate()) { try { setup::check_db($form->username->value, $form->password->value, $form->host->value, $form->database->value, $form->prefix->value); $data = array('username' => $form->username->value, 'password' => $form->password->value, 'host' => $form->host->value, 'database' => $form->database->value, 'prefix' => $form->prefix->value, 'data' => $form->data->checked); Session::instance()->set('database_data', $data); $redirect = $form->drop->checked ? 'install/step_drop_tables' : 'install/step_create_structure'; url::redirect($redirect); } catch (Exception $e) { $error = $e->getMessage(); Session::instance()->delete('conn_status'); switch ($error) { case 'access': $conn_error = __('Wrong username or password.'); break; case 'unknown_host': $conn_error = __('Could not find the host.'); break; case 'connect_to_host': $conn_error = __('Could not connect to host.'); break; case 'create': $conn_error = __('Could not create the database.'); break; case 'select': $conn_error = __('Could not select the database.'); break; case 'prefix': $conn_error = __('The Table Prefix you chose is already in use.'); break; default: $conn_error = $error; } } } $this->template->page_title = __('Database Setup'); $data = $form->get(TRUE); $this->template->content = new View('install/database_setup', $data); $this->template->content->success = __('%bc will work correctly with your environment', array('%bc' => Kohana::config('bc.bc'))); $this->template->content->error = isset($conn_error) ? $conn_error : ''; $this->template->content->passed = Session::instance()->get('conn_status'); }
public function edit($id) { $this->head->title->append(__('New User')); $this->template->title .= __('New User'); $user = ORM::factory('user', (int) $id); if (!$user->loaded) { Event::run('system.404'); } $form = Formo::factory()->plugin('csrf')->add('text', 'username', array('label' => __('Username'), 'value' => $user->username))->add('text', 'email', array('label' => __('Email'), 'value' => $user->email))->add('password', 'password', array('label' => __('Password')))->add('password', 'password_confirm', array('label' => __('Confirm password')))->add('submit', 'submit', array('label' => __('Save')))->add_rule('email', 'required', __('You must enter an email'))->add_rule('email', 'valid::email', __('Email address is not valid'))->add_rule('password', 'matches[password_confirm]', __('The passwords doesn\'t match')); if ($form->validate()) { $user->username = $form->username->value; $user->email = $form->email->value; if (!empty($form->password->value)) { $user->password = $form->password->value; } $user->save(); message::info(__('User edited successfully'), 'admin/user'); } $this->template->content = View::factory('user/edit', $form->get(TRUE)); }
public function change_client($id = NULL) { $contact = ORM::factory('contact', $id); if ($contact->id == '') { $this->session->set_flash('contact', 'There is no contact associated with the id provided.'); url::redirect(); } $clients = $this->cache->get('client') ? $this->cache->get('client') : ORM::factory('client')->find_all_as_array(); // $client_list = array(); foreach ($clients as $client) { $client_list[$client['id']] = $client['company']; } $form = Formo::factory('contact_edit')->set('class', 'smart-form')->add_select('client', $client_list, array('value' => $contact->client_id))->add('submit', 'Submit'); if ($form->validate()) { $contact->client_id = $form->client->value; $contact->save() and $this->cache->delete_tag('contacts') and url::redirect('clients'); } $this->template->content = new View('contacts/add'); $this->template->content->form = $form; }
public function edit($id) { $comment = ORM::factory('blog_comment', (int) $id); if (!$comment->loaded) { message::error(__('Invalid ID'), 'admin/blog'); } $this->head->title->append(__('Edit comment #%id', array('%id' => $comment->id))); $this->template->title .= __('Edit comment #%id', array('%id' => $comment->id)); $form = Formo::factory()->plugin('csrf')->add('text', 'author', array('label' => __('Author'), 'value' => $comment->author))->add('text', 'email', array('label' => __('Email'), 'value' => $comment->email))->add('text', 'url', array('label' => __('Homepage'), 'value' => $comment->url))->add('textarea', 'content', array('label' => __('Comment'), 'value' => $comment->content))->add('submit', 'submit', array('label' => __('Save'))); if ($form->validate()) { $comment->author = $form->author->value; $comment->email = $form->email->value; $comment->url = $form->url->value; $comment->content = $form->content->value; $comment->save(); Cache::instance()->delete('s7n_blog_feed_comments'); message::info(__('Comment edited successfully'), 'admin/blog/comments/view/' . $comment->blog_post_id); } $this->template->content = View::factory('blog/editcomment', $form->get(TRUE)); }
public function index() { // If remember me is set the autologin if ($this->auth->auto_login() || $this->auth->logged_in()) { url::redirect('dashboard'); } $form = Formo::factory('login')->set('class', 'smart-form')->add('username', array('class' => 'size hasDefault'))->add_rule('username', 'required', __('You must enter a username'))->add('password', array('class' => 'size hasDefault'))->type('password')->add_rule('password', 'required', __('You must enter a password'))->add('checkbox', 'remember_me', array('label' => 'remember me', 'tabindex' => 3, 'required' => FALSE))->add('submit', 'submit', array('value' => __('Login'), 'class' => 'button')); if ($form->validate()) { // Remember me check $remember = isset($form->remember_me) ? TRUE : FALSE; // Load the user $user = ORM::factory('user', $form->username->value); // Attempt a login if ($this->auth->login($user, $form->password->value, $remember)) { url::redirect('dashboard'); } $error = __('Invalid username or password'); } $this->template->content = View::factory('login/index', $form->get(TRUE)); $this->template->content->error = isset($error) ? $error : ''; }
public function login() { if (Auth::instance()->logged_in()) { if (Auth::instance()->logged_in('admin')) { url::redirect('admin'); } else { url::redirect(); } } $form = Formo::factory()->add('text', 'username', array('label' => __('Username')))->add('password', 'password', array('label' => __('Password')))->add('submit', 'submit', array('label' => __('Login')))->add_rule('username', 'required', __('You must enter a username'))->add_rule('password', 'required', __('You must enter a password')); if ($form->validate()) { // Load the user $user = ORM::factory('user', $form->username->value); // Attempt a login if ($user->loaded and Auth::instance()->login($user, $form->password->value)) { $url = Session::instance()->get_once('redirect_me_to'); url::redirect(empty($url) ? 'admin' : $url); } $error = __('Invalid username or password'); } $view = View::factory('login', $form->get(TRUE))->bind('error', $error); echo $view; }
/** * Edit a $this->model_name * @Developer brandon * @Date Apr 21, 2010 */ public function edit($id = NULL) { $item = ORM::factory($this->model_name, (string) $id); $form = Formo::factory()->plugin('orm')->plugin('habtm')->orm($this->model_name, $id)->set('action', $item->update_path()); // Add the related objects if ($this->show_habtm) { foreach ($item->has_and_belongs_to_many as $relationship) { $form->habtm($this->model_name, inflector::singular($relationship)); } } // Add the submit button $form->add('submit'); if (!$item->loaded) { flash::set_error('The ' . format::friendly_model_name($this->model_name) . ' you were looking for was not found'); url::redirect($this->directory . '/index'); } $this->template->set('content', View::factory($this->directory . '/edit')->set('form', $form)->set($this->model_name, $item)); }
/** * Edit a $this->model_name * @Developer brandon * @Date Apr 21, 2010 */ public function edit($id = NULL) { $item = ORM::factory($this->model_name, $id); $form = Formo::factory()->plugin('orm')->orm($this->model_name, $id)->set('action', $item->update_path())->add('submit'); if (!$item->loaded) { flash::set_error('The ' . format::friendly_model_name($this->model_name) . ' you were looking for was not found'); url::redirect($this->directory . '/index'); } $this->template->set('content', View::factory($this->directory . '/edit')->set('form', $form)->set($this->model_name, $item)); }
public function demo8() { $hobbies = array('_blank_' => '', 'Run' => 'run', 'Jump' => 'jump', 'Swim' => 'swim'); $skills = array(1 => 'poet', 25 => 'artist', 3 => 'television'); $months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); $form = Formo::factory()->plugin('ajaxval')->add('autocomplete', 'month', array('data' => implode('|', $months)))->add_html('<div style="height:20px"></div>', 'space1')->add('username')->add('password', 'password')->add('password', 'password2')->label('Confirm Password')->rule('matches[password]', 'Does not match')->add_html('<div style="height:20px"></div>', 'space2')->add('email')->add('phone')->add_select('hobbies', $hobbies)->add_group('skills[]', $skills)->add('textarea', 'notes')->add_submit('submit'); $this->content = $form; $form->validate() and $this->_success(); }
public function settings() { $this->head->title->append(__('Settings')); $this->template->title .= __('Settings'); $form = Formo::factory()->plugin('csrf')->add('text', 'items_per_page', array('label' => __('Blog entries per page'), 'value' => config::get('blog.items_per_page')))->add('checkbox', 'enable_captcha', array('label' => __('Enable captcha'), 'checked' => config::get('blog.enable_captcha') === 'yes'))->add('checkbox', 'enable_tagcloud', array('label' => __('Enable tag cloud'), 'checked' => config::get('blog.enable_tagcloud') === 'yes'))->add('checkbox', 'comment_status', array('label' => __('Enable comments'), 'checked' => config::get('blog.comment_status') === 'open'))->add('submit', 'submit', array('label' => __('Save')))->add_rule('items_per_page', 'required', __('Please enter a number'))->add_rule('items_per_page', 'digit', __('This must be a number')); if ($form->validate()) { config::set('blog.enable_captcha', $form->enable_captcha->checked ? 'yes' : 'no'); config::set('blog.enable_tagcloud', $form->enable_tagcloud->checked ? 'yes' : 'no'); config::set('blog.comment_status', $form->comment_status->checked ? 'open' : 'closed'); config::set('blog.items_per_page', $form->items_per_page->value); message::info(__('Settings changed successfully'), 'admin/blog/settings'); } $this->template->content = new View('blog/settings', $form->get(TRUE)); }
public function create_sub($alias, $driver, array $fields, $order = NULL) { // Create the empty subform object $subform = Formo::factory($alias, $driver); foreach ($fields as $field) { // Find each field $field = $this->find($field); // Remember the field's original parent $last_parent = $field->parent(); // Add the field to the new subform $subform->append($field); // Remove the field from its original parent $last_parent->remove($field->alias()); } // If the parent has a model, copy it to the new subform $subform->set('model', $this->get('model')); $order and $subform->set('order', $order); // Append the new subform $this->append($subform); return $this; }
public function demo8() { $this->scripts = TRUE; $hobbies = array('_blank_' => '', 'Run' => 'run', 'Jump' => 'jump', 'Swim' => 'swim'); $form = Formo::factory()->plugin('ajaxval')->add('username')->add('password', 'password')->add('password', 'password2')->label('Confirm Password')->rule('matches[password]', 'Does not match')->add_html('<div style="height:20px"></div>')->add('email')->add('phone')->add_select('hobbies', $hobbies)->add('textarea', 'notes')->add_submit('submit'); $this->header = new View('headers/demo8'); $this->content = $form; if ($form->validate()) { $this->content = $this->msg . $this->content; } }
/** * Create a subform from an array list of fields already in a form * * @access public * @param mixed $alias * @param array $fields * @param array $order (default: NULL) * @param string $driver (default: 'group') * @return Formo obj */ public function subform($alias, array $fields, array $order = NULL, $driver = 'group') { $subform = Formo::factory(array('alias' => $alias, 'driver' => $driver)); foreach ($fields as $field_alias) { $field = $this->find($field_alias, TRUE); $subform->add($field); $this->remove($field_alias); } $this->add($subform); if ($order !== NULL) { $this->order($alias, $order[0], Arr::get($order, 1)); } return $this; }
public function edit($id) { $page = ORM::factory('page', (int) $id); if (!$page->loaded) { message::error(__('Invalid ID'), 'admin/page'); } $form = Formo::factory()->plugin('csrf')->add_group('type', array('none' => __('Do nothing'), 'module' => __('Load module'), 'redirect' => __('Redirect to')))->add_select('module', module::installed_as_array(), array('value' => $page->target))->add_select('redirect', $page->paths(), array('value' => $page->target))->add('submit', 'submit', array('label' => __('Save'))); foreach (Kohana::config('locale.languages') as $key => $value) { $page_content = ORM::factory('page_content')->where(array('page_id' => $page->id, 'language' => $key))->find(); $form->add('text', 'title_' . $key, array('label' => __('Title'), 'value' => $page_content->title))->add('text', 'content_' . $key, array('label' => __('Content'), 'value' => $page_content->content))->add_rule('title_' . $key, 'required', __('Please choose a title')); } if ($form->validate()) { $title = array(); foreach (Kohana::config('locale.languages') as $key => $value) { $page_content = ORM::factory('page_content')->where(array('page_id' => $page->id, 'language' => $key))->find(); if (!$page_content->loaded) { $page_content->page_id = $page->id; $page_content->language = $key; $page_content->date = date("Y-m-d H:i:s"); } $page_content->title = $form->{'title_' . $key}->value; $page_content->uri = url::title($form->{'title_' . $key}->value); $page_content->content = $form->{'content_' . $key}->value; $page_content->modified = date("Y-m-d H:i:s"); $page_content->save(); $title[] = $page_content->title; } $type = NULL; $target = NULL; /* * TODO workaround for: * http://projects.kohanaphp.com/boards/5/topics/114 * and * http://projects.kohanaphp.com/issues/1697 * * fixed in Formo 1.2 * */ $_type = NULL; foreach ($form->type->elements as $key => $value) { if ($form->type->{$key}->checked) { $_type = $value; break; } } if ($_type == 'redirect') { $redirect = trim($form->redirect->value); if (!empty($redirect)) { $type = 'redirect'; $target = $redirect; } } elseif ($_type == 'module') { $module = trim($form->module->value); if (!empty($module)) { $type = 'module'; $target = $module; } } $page->type = $type; $page->target = $target; $page->title = implode(' / ', $title); $page->save(); Cache::instance()->delete_tag('menu'); Cache::instance()->delete_tag('route'); message::info(__('Page edited successfully'), 'admin/page'); } $this->template->content = View::factory('page/edit', $form->get(TRUE)); $this->template->content->page = $page; $this->template->content->modules = module::installed(); }