Пример #1
0
 public function execute($method, $url, array $post = array())
 {
     $redirects_count = 1;
     \Request::$initial = NULL;
     $this->_request = \Request::factory($url)->method($method)->post($post)->body(http_build_query($post));
     if ($this->_previous_url) {
         $this->_request->referrer($this->_previous_url);
     }
     $this->_previous_url = $this->current_url() . \URL::query($this->_request->query(), FALSE);
     \Request::$initial = $this->_request;
     $this->_response = $this->_request->execute();
     while ($this->_response->status() >= 300 and $this->_response->status() < 400) {
         $redirects_count++;
         if ($redirects_count >= $this->max_redirects()) {
             throw new Exception_Toomanyredirects('Maximum Number of redirects (5) for url :url', array(':url' => $url));
         }
         $url_parts = parse_url($this->_response->headers('location'));
         $query = isset($url_parts['query']) ? $url_parts['query'] : '';
         parse_str($query, $query);
         $_GET = $query;
         $url = $url_parts['path'];
         \Request::$initial = NULL;
         $this->_request = \Request::factory($url);
         \Request::$initial = $this->_request;
         $this->_response = $this->_request->execute();
     }
     return $this->_response->body();
 }
Пример #2
0
 public function action_index($options = array("max" => 10, "current" => 1, "count" => 5))
 {
     $model = array();
     $pages = array();
     $max_page = $options["max"];
     // Максимальное количество страниц
     $current_page = $options["current"];
     // Текущая страница
     $pages_length = $options["count"];
     // Количество номеров страниц (нечётное число)
     $next_prev_pages = floor($pages_length / 2);
     // Количество доп. страниц
     $pages[] = array("href" => "#", "active" => "active", "num" => $current_page);
     // Создание доп. страниц
     for ($i = 1; $i <= $next_prev_pages; $i++) {
         if ($current_page - $i > 1) {
             array_unshift($pages, array("href" => URL::query(array("page" => $current_page - $i)), "num" => $current_page - $i));
         }
         if ($current_page + $i < $max_page) {
             array_push($pages, array("href" => URL::query(array("page" => $current_page + $i)), "num" => $current_page + $i));
         }
     }
     if ($current_page > 1) {
         $model["min_page"] = array("num" => 1, "href" => URL::query(array("page" => 1)));
         $model["prev_href"] = URL::query(array("page" => $current_page - 1));
     }
     if ($current_page < $max_page) {
         $model["max_page"] = array("num" => $max_page, "href" => URL::query(array("page" => $max_page)));
         $model["next_href"] = URL::query(array("page" => $current_page + 1));
     }
     $model["pages"] = $pages;
     $this->set_template("/widgets/twig_pagination.php", "twig")->render($model)->body();
 }
Пример #3
0
 public function action_login()
 {
     if ($this->_auth->logged_in()) {
         // redirect to the user account
         $this->request->redirect(Route::get('admin')->uri(), 200);
     }
     // Disable sidebars on login page
     $this->_sidebars = FALSE;
     $this->title = __('Sign In');
     $user = ORM::factory('user');
     // Create form action
     $destination = isset($_GET['destination']) ? $_GET['destination'] : 'admin';
     $params = array('action' => 'login');
     $action = Route::get('admin/login')->uri($params) . URL::query(array('destination' => $destination));
     if ($layout = kohana::find_file('views', 'layouts/login')) {
         $this->template->set_filename('layouts/login');
     }
     $view = View::factory('admin/login')->set('use_username', Config::get('auth.username'))->set('post', $user)->set('action', $action)->bind('errors', $this->_errors);
     if ($this->valid_post('login')) {
         try {
             // Check Auth
             $user->login($this->request->post());
             // If the post data validates using the rules setup in the user model
             Message::success(__('Welcome, %title!', array('%title' => $user->nick)));
             Log::info('User :name logged in.', array(':name' => $user->name));
             // redirect to the user account
             $this->request->redirect(isset($_GET['destination']) ? $_GET['destination'] : 'admin', 200);
         } catch (Validation_Exception $e) {
             $this->_errors = $e->array->errors('login', TRUE);
         }
     }
     $this->response->body($view);
 }
Пример #4
0
 /**
  * 
  * @param Datasource_Section $ds
  * @param Datasource_Document $doc
  */
 public function action_post()
 {
     $id = (int) $this->request->post('id');
     $doc = $this->_get_document($id);
     Session::instance()->set('post_data', $this->request->post());
     try {
         $doc->read_values($this->request->post())->read_files($_FILES)->validate();
     } catch (Validation_Exception $e) {
         Messages::errors($e->errors('validation'));
         $this->go_back();
     } catch (DataSource_Exception_Document $e) {
         Messages::errors($e->getMessage());
         $this->go_back();
     }
     if ($doc->loaded()) {
         $this->section()->update_document($doc);
     } else {
         $doc = $this->section()->create_document($doc);
     }
     Messages::success(__('Document saved'));
     Session::instance()->delete('post_data');
     // save and quit or save and continue editing?
     if ($this->request->post('commit') !== NULL) {
         $this->go(Route::get('datasources')->uri(array('directory' => 'datasources', 'controller' => 'data')) . URL::query(array('ds_id' => $this->section()->id()), FALSE));
     } else {
         $this->go(Route::get('datasources')->uri(array('directory' => $this->section()->type(), 'controller' => 'document', 'action' => 'view')) . URL::query(array('ds_id' => $this->section()->id(), 'id' => $doc->id), FALSE));
     }
 }
Пример #5
0
 public function action_delete()
 {
     $id = (int) $this->request->param('id', 0);
     $comment = ORM::factory('comment', $id)->access('delete');
     $this->title = __('Are you absolutely sure?');
     $destination = empty($this->redirect) ? array() : array('destination' => $this->redirect);
     $post = $this->request->post();
     $route = Route::get('comment')->uri(array('action' => 'view', 'id' => $comment->id));
     $view = View::factory('form/confirm')->set('action', Route::get('comment')->uri(array('action' => 'delete', 'id' => $comment->id)) . URL::query($destination))->set('title', $comment->title);
     // If deletion is not desired, redirect to post
     if (isset($post['no']) and $this->valid_post()) {
         $this->request->redirect(empty($this->redirect) ? $route : $this->redirect);
     }
     // If deletion is confirmed
     if (isset($post['yes']) and $this->valid_post()) {
         $redirect = $comment->post->url;
         $title = $comment->title;
         try {
             $comment->delete();
             Log::info('Comment: :title deleted.', array(':title' => $title));
             Message::success(__('Comment %title deleted successful!', array('%title' => $title)));
         } catch (Exception $e) {
             Log::error('Error occurred deleting comment id: :id, :msg', array(':id' => $comment->id, ':msg' => $e->getMessage()));
             Message::error('An error occurred deleting comment %post.', array('%post' => $title));
             $this->_errors = array('An error occurred deleting comment %post.', array('%post' => $title));
         }
         $redirect = empty($destination) ? $redirect : $this->redirect;
         $this->request->redirect($redirect);
     }
     $this->response->body($view);
 }
Пример #6
0
 public function action_index($supplychain_id)
 {
     if (!is_numeric($supplychain_id)) {
         $supplychain_id = $this->_match_alias($supplychain_id);
     }
     $supplychain = ORM::factory('supplychain', $supplychain_id);
     $sc = $supplychain->kitchen_sink($supplychain_id);
     if ($supplychain->loaded()) {
         $current_user_id = Auth::instance()->logged_in() ? (int) Auth::instance()->get_user()->id : 0;
         $owner_id = (int) $supplychain->user_id;
         if ($supplychain->user_can($current_user_id, Sourcemap::READ)) {
             $this->layout->supplychain_id = $supplychain_id;
             // pass supplychain metadeta to template
             $this->template->supplychain_id = $supplychain_id;
             $this->template->supplychain_date = date('F j, Y', $sc->created);
             $this->template->supplychain_name = isset($sc->attributes->name) ? $sc->attributes->name : "";
             $this->template->supplychain_owner = isset($sc->owner->name) ? $sc->owner->name : "";
             $this->template->supplychain_ownerid = isset($sc->owner->id) ? $sc->owner->id : "";
             $this->template->supplychain_avatar = isset($sc->owner->avatar) ? $sc->owner->avatar : "";
             $this->template->supplychain_desc = isset($sc->attributes->description) ? $sc->attributes->description : "";
             $this->layout->scripts = array('blog-view');
             $this->layout->styles = array('sites/default/assets/styles/reset.css', 'assets/styles/base.less', 'assets/styles/general.less');
             // qrcode url
             $qrcode_query = URL::query(array('q' => URL::site('view/' . $supplychain->id, true), 'sz' => 8));
             $this->template->qrcode_url = URL::site('services/qrencode', true) . $qrcode_query;
         } else {
             Message::instance()->set('That map is private.');
             $this->request->redirect('browse');
         }
     } else {
         Message::instance()->set('That map could not be found.');
         $this->request->redirect('browse');
     }
 }
Пример #7
0
 /**
  * Render a sortable <th> tag based on current request
  *
  * @param string $column
  * @param string $name
  * @param string|array $attributes
  * @return string
  */
 public static function th($column, $name, $attributes = null)
 {
     static $_query;
     // Only get one per request
     if ($_query === null) {
         $_query = Request::current()->query();
         // Default
         $_query['sort'] = isset($_query['sort']) ? strtolower($_query['sort']) : 'id';
         $_query['order'] = isset($_query['order']) ? strtolower($_query['order']) : 'desc';
     }
     // Attributes
     if (!is_array($attributes)) {
         $attributes = array('class' => $attributes);
     }
     // Init
     $class = 'sorting';
     $order = 'asc';
     // This column is selected
     if ($column == $_query['sort']) {
         $class .= '_' . $_query['order'];
         $order = $_query['order'] == 'asc' ? 'desc' : 'asc';
     }
     // Add class to element
     $attributes['class'] = trim($class . ' ' . $attributes['class'], ' ');
     // Build URL query
     $url = URL::query(array('sort' => $column, 'order' => $order));
     // Return HTML
     return strtr('<th:attrs><a href=":url">:name</a></th>', array(':attrs' => HTML::attributes($attributes), ':url' => $url, ':name' => $name));
 }
Пример #8
0
 /**
  * 
  * @param string $action
  * @param integer|string $ds_id
  * @return string
  */
 public static function uri($action = 'view', $ds_id = NULL)
 {
     if ($action == 'view') {
         $uri = Route::get('datasources')->uri(array('controller' => 'data', 'directory' => 'datasources'));
         return $ds_id !== NULL ? $uri . URL::query(array('ds_id' => (int) $ds_id)) : $uri;
     }
     return Route::get('datasources')->uri(array('controller' => 'section', 'directory' => 'datasources', 'action' => $action, 'id' => $ds_id));
 }
Пример #9
0
 public function action_code()
 {
     $query = URL::query();
     $template = new View('oauth');
     $view = new View('oauth-server-authorize', array('authorized' => TRUE, 'query' => $query));
     $template->content = $view->render();
     $this->request->response = $template;
 }
Пример #10
0
 public function url($page = 1)
 {
     $page = max(1, (int) $page);
     if (isset($this->config['source']) && $this->config['source'] == 'route') {
         return URL::site(Request::instance()->uri(array($this->config['current_page_key'] => $page))) . URL::query();
     } else {
         return URL::site(Request::instance()->uri()) . URL::query(array($this->config['current_page_key'] => $page));
     }
 }
Пример #11
0
 /**
  * Добавление раздела в меню Backend
  * 
  * @param Datasource_Section $section
  * @param Model_Navigation_Section $parent_section
  * return Model_Navigation_Section;
  */
 public static function add_section_to_menu(Datasource_Section $section, Model_Navigation_Section $parent_section = NULL)
 {
     if ($parent_section === NULL) {
         $parent_section = Model_Navigation::get_root_section();
     }
     if (!$section->has_access_view()) {
         return $parent_section;
     }
     return $parent_section->add_page(new Model_Navigation_Page(array('name' => $section->name, 'url' => Route::get('datasources')->uri(array('controller' => 'data', 'directory' => 'datasources')) . URL::query(array('ds_id' => $section->id())), 'icon' => $section->icon(), 'permissions' => 'ds_id.' . $section->id() . '.section.view')), 999);
 }
Пример #12
0
 /**
  * Save referer to session
  * @param string $name
  * @param bool $referer
  */
 protected function save_referer($name, $referer = true)
 {
     if ($referer === true) {
         $referer = $this->request->uri();
     } else {
         $referer = (string) $referer;
     }
     $referer = URL::base() . $referer . URL::query();
     $this->session->set($name, $referer);
 }
Пример #13
0
 public function next()
 {
     return Tart::html($this, function ($h, $self) {
         if ($self->offset() < $self->total() - $self->per_page()) {
             $h->anchor(Tart::uri($self->controller()) . URL::query(array('offset' => $self->offset() + $self->per_page())), __('Next &raquo;'));
         } else {
             $h('span', __('Next &raquo;'));
         }
     });
 }
Пример #14
0
 public function action_index()
 {
     // todo: try/catch OAuthClientException
     $server = service('oauth.server.auth');
     $params = $server->getGrantType('authorization_code')->checkAuthoriseParams();
     $this->session->set('oauth', $params);
     if (!$this->user) {
         $this->redirect('user/login' . URL::query(array('from_url' => 'oauth/authorize' . URL::query()), FALSE));
     }
     $this->redirect('oauth/authorize' . URL::query(Arr::extract($params, $this->oauth_params)));
 }
Пример #15
0
 public function fetch_headline_value($value, $document_id)
 {
     if (empty($value)) {
         return parent::fetch_headline_value($value, $document_id);
     }
     $header = DataSource_Hybrid_Field_Utils::get_document_header($this->from_ds, $value);
     if (!empty($header)) {
         return HTML::anchor(Route::get('datasources')->uri(array('directory' => 'hybrid', 'controller' => 'document', 'action' => 'view')) . URL::query(array('ds_id' => $this->from_ds, 'id' => $value), FALSE), $header, array('class' => ' popup fancybox.iframe'));
     }
     return parent::fetch_headline_value($value, $document_id);
 }
Пример #16
0
 public function render_active()
 {
     $self = $this;
     return Tart::to_sentence(array_values(array_filter(array_map(function ($entry) use($self) {
         if (!$entry->active()) {
             return NULL;
         }
         $data = $self->data();
         unset($data[$entry->name()]);
         return HTML::anchor(Tart::uri($self->controller()) . URL::query(array_filter($data), FALSE), $entry->active(), array('class' => 'label'));
     }, $this->entries()))));
 }
Пример #17
0
 /**
  * Constructor for init widget
  *
  * @param string $module
  * @param string $controller
  * @param string $action
  * @param array $query
  */
 private function __construct($module, $controller, $action, $query)
 {
     if ($action === null) {
         $action = 'index';
     }
     if ($query !== null) {
         $this->_query = URL::query($query);
     }
     $this->_params['module'] = $module;
     $this->_params['controller'] = $controller;
     $this->_params['action'] = $action;
 }
Пример #18
0
 public function before()
 {
     //$this->redirect('http://ehistory.kz/manage');
     parent::before();
     $this->response->headers('cache-control', 'private');
     // creating and attaching page metadata
     $this->metadata = new Model_Metadata();
     $this->metadata->title(__(Application::instance()->get('title')), false);
     $this->set('_metadata', $this->metadata);
     Auth::instance()->auto_login();
     if (!Auth::instance()->logged_in()) {
         $this->redirect('manage/auth/login');
     } else {
         $id = Auth::instance()->get_user()->id;
         $user = ORM::factory('user', $id);
         $input = $user->has('roles', ORM::factory('role', array('name' => 'admin'))) || $user->has('roles', ORM::factory('Role', array('name' => 'moderator')));
         $input_redactor = $user->has('roles', ORM::factory('Role', array('name' => 'redactor')));
         if (!$input && !$input_redactor) {
             $this->redirect('/manage/auth/logout');
         }
         if (!$input && (strtolower($this->request->controller()) != 'ehistory' && strtolower($this->request->controller()) != 'language')) {
             $this->redirect('manage/ehistory');
         }
     }
     $this->user = Auth::instance()->get_user();
     if (Request::$initial === Request::$current) {
         $messages = Notify::instance()->get_all_once();
         $this->set('_notifications', $messages);
     }
     $language = Session::instance()->get('_language', 'ru');
     $this->language = in_array($language, array('ru', 'en', 'kz')) ? $language : 'ru';
     I18n::lang($this->language);
     $rr = Request::initial()->uri() . urlencode(URL::query(null, true));
     $rr = trim($rr, '/');
     //$this->metadata->title('Sharua.kz', false);
     $countcomm = ORM::factory('Comment')->where('status', '=', '0')->count_all();
     //смотрим сколько новых коментов
     $this->set('_user', $this->user)->set('_language', $this->language)->set('_return_url', $rr)->set('_countcomm', $countcomm);
     //вносим в переменную количество новых коментов
     $knigi = ORM::factory('Book')->where('category_id', '=', '0')->find_all();
     //смотрим сколько книг без категории
     if ($knigi) {
         if (count($knigi) > 0) {
             $this->set('_uncatcount', count($knigi));
             //вносим в переменную количество книг без категории
         }
     }
     $this->referrer = Request::initial()->referrer();
     if (Message::get()) {
         $this->set('basic_message', Message::display('/message/basic'));
     }
 }
Пример #19
0
 /**
  * Generates the full URL for a certain page.
  *
  * @param   integer  page number
  * @return  string   page URL
  */
 public function url($page = 1)
 {
     if ($this->config['current_page']['source'] == 'query_string' && ($url = Arr::get($this->config, 'url'))) {
         // Clean the page number
         $page = max(1, (int) $page);
         // No page number in URLs to first page
         if ($page === 1) {
             $page = NULL;
         }
         return URL::site($url) . URL::query(array($this->config['current_page']['key'] => $page));
     }
     return parent::url($page);
 }
Пример #20
0
 public function url($page = 1, $opts = array())
 {
     $page = max(1, (int) $page);
     $params = array($this->config['current_page_key'] => $page);
     if (isset($this->config['source']) && $this->config['source'] == 'route') {
         return URL::site(Request::instance()->uri($params)) . URL::query();
     } else {
         if (!empty($opts)) {
             $params = $opts + $params;
         }
         return URL::site(Request::instance()->uri()) . URL::query($params);
     }
 }
Пример #21
0
 public function getPaging()
 {
     // Get paging parameters, ensuring all values are set
     $params = $this->search->getSorting(true);
     $prev_params = $next_params = $params;
     $next_params['offset'] = $params['offset'] + $params['limit'];
     $prev_params['offset'] = $params['offset'] - $params['limit'];
     $prev_params['offset'] = $prev_params['offset'] > 0 ? $prev_params['offset'] : 0;
     $request = Request::current();
     $curr = URL::site($request->uri() . URL::query($params), $request);
     $next = URL::site($request->uri() . URL::query($next_params), $request);
     $prev = URL::site($request->uri() . URL::query($prev_params), $request);
     return ['limit' => $params['limit'], 'offset' => $params['offset'], 'order' => $params['order'], 'orderby' => $params['orderby'], 'curr' => $curr, 'next' => $next, 'prev' => $prev, 'total_count' => $this->total];
 }
Пример #22
0
 /**
  * 
  * @param string $email
  * @param integer $size
  * @param string $default
  * @param array $attributes
  * @return string
  */
 public static function load($email, $size = 40, $default = NULL, array $attributes = NULL)
 {
     if (empty($email)) {
         $email = '*****@*****.**';
     }
     if ($default === NULL) {
         $default = 'mm';
     }
     $hash = md5(strtolower(trim($email)));
     $query_params = URL::query(array('d' => $default, 's' => (int) $size));
     if (!isset(self::$_cache[$email][$size])) {
         self::$_cache[$email][$size] = HTML::image('http://www.gravatar.com/avatar/' . $hash . $query_params, $attributes);
     }
     return self::$_cache[$email][$size];
 }
Пример #23
0
 /**
  * Returns src part of img HTML tag // Usefull with lightboxes and inline stlyes for background images
  *
  * @param string $file
  * @param string $type
  * @param string $default
  * @param array $attributes img attributes
  * @return unknown
  */
 public static function img_src($file, $type, $default = NULL)
 {
     $filepath = Spot_Image::find_file($file);
     if (is_file($filepath)) {
         if (is_string($type)) {
             return URL::base() . Kohana::config("spot_image.prefix") . "/" . $type . "/" . $file;
         } elseif (is_array($type)) {
             $type = URL::query($type);
             return URL::base() . Kohana::config("spot_image.prefix") . "/" . $file . "/" . $type;
         }
     } else {
         return $default;
     }
     return FALSE;
 }
Пример #24
0
 public function login()
 {
     $auth = Auth::instance();
     $request = Request::current();
     // If user already signed-in / don't show the widget on user controller.
     if ($auth->logged_in() or $request->controller() === 'user') {
         return;
     }
     Assets::css('user', 'media/css/user.css', array('weight' => 2));
     // Create form action
     $destination = isset($_GET['destination']) ? $_GET['destination'] : Request::initial()->uri();
     $params = array('action' => 'login');
     $action = Route::get('user')->uri($params) . URL::query(array('destination' => $destination));
     return View::factory('widget/login')->set('register', Config::get('auth.register'))->set('use_username', Config::get('auth.username'))->set('providers', array_filter(Auth::providers()))->set('action', $action)->set('post', ORM::factory('user'))->render();
 }
Пример #25
0
 public function go($url = NULL, $code = 302)
 {
     $route_params = array('controller' => strtolower($this->request->controller()));
     if (is_array($url) or $url === NULL) {
         if (is_array($url)) {
             $route_params = Arr::merge($route_params, $url);
         }
         $url = Route::get('backend')->uri($route_params);
     }
     if (is_array($this->query_params)) {
         $url = preg_replace('/\\?.*/', '', $url);
         $url .= URL::query($this->query_params, TRUE);
     }
     $this->redirect($url, $code);
 }
Пример #26
0
 public function render()
 {
     return Tart::form($this, function ($h, $self) {
         $h('label', __($self->label()));
         $h('p', function ($h, $self) {
             $h('div', array('class' => 'btn-group' . ($self->vertical() ? ' btn-group-vertical' : ''), 'data-toggle' => 'buttons-radio'), function ($h, $self) {
                 if ($self->all()) {
                     $h('a', array('class' => 'btn ' . ($self->parent()->form()->object()->{$self->name()} ? '' : ' active'), 'href' => Request::current()->url() . URL::query(array($self->name() => ''))), __('All'));
                 }
                 foreach ($self->params() as $value => $label) {
                     $h('a', array('class' => 'btn ' . ($self->parent()->form()->object()->{$self->name()} == $value ? ' active' : ''), 'href' => Request::current()->url() . URL::query(array($self->name() => $value))), $label);
                 }
             });
         });
     });
 }
Пример #27
0
 public function test4()
 {
     $url = "http://url.com/doc/index.php?query=1&query2=2#abc=3";
     $domain = URL::domain($url);
     //url.com
     $queryString = URL::queryString($url);
     //query=1&query=2
     $query = URL::query($url);
     //['query'=>1,'query2'=>2]
     $dir = URL::dir($url);
     //http://url.com/doc
     echo $domain;
     echo $queryString;
     print_r($query);
     echo $dir;
 }
Пример #28
0
 /**
  * Make an API call.
  *
  * @param   string  $call
  * @param   array   $params
  * @return  array
  */
 public function api_call($call, array $params = null)
 {
     if (!$params) {
         $params = array('access_token' => $this->_token['access_token']);
     } else {
         $params['access_token'] = $this->_token['access_token'];
     }
     $api_call = $this->base_uri() . $call . URL::query($params, false);
     if (substr_count($api_call, '?') > 1) {
         $api_call = str_replace('?access_token', '&access_token', $api_call);
     }
     $request = Request::factory($api_call)->method(Request::GET);
     Kohana::$log->add(Log::DEBUG, 'OAuth2: Making an API call: :call', array(':call' => $api_call));
     $response = $this->execute($request);
     return (array) json_decode($response->body());
 }
Пример #29
0
 public function url($page = 1)
 {
     // Clean the page number
     $page = max(1, (int) $page);
     // No page number in URLs to first page
     if ($page === 1 and !$this->config['first_page_in_url']) {
         $page = NULL;
     }
     $params = isset($this->config['url_params']) && is_array($this->config['url_params']) ? $this->config['url_params'] : array();
     switch ($this->config['current_page']['source']) {
         case 'query_string':
             $params[$this->config['current_page']['key']] = $page;
             return URL::site(Request::current()->uri) . URL::query($params);
         case 'route':
             return URL::site(Request::current()->uri(array($this->config['current_page']['key'] => $page))) . URL::query($params);
     }
     return '#';
 }
Пример #30
0
 /**
  * Sending mails
  *
  * @since 1.0.0  First time this method was introduced
  * @since 1.1.0  Added jQuery Textarea Characters Counter Plugin
  *
  * @link  http://roy-jin.appspot.com/jsp/textareaCounter.jsp
  *
  * @uses  Request::query
  * @uses  Route::get
  * @uses  Route::uri
  * @uses  URL::query
  * @uses  URL::site
  * @uses  Validation::rule
  * @uses  Config::get
  * @uses  Config::load
  * @uses  Assets::js
  */
 public function action_mail()
 {
     $this->title = __('Contact us');
     $config = Config::load('contact');
     Assets::js('textareaCounter', 'media/js/jquery.textareaCounter.plugin.js', array('jquery'), FALSE, array('weight' => 10));
     Assets::js('greet/form', 'media/js/greet.form.js', array('textareaCounter'), FALSE, array('weight' => 15));
     //Add schema.org support
     $this->schemaType = 'ContactPage';
     // Set form destination
     $destination = !is_null($this->request->query('destination')) ? array('destination' => $this->request->query('destination')) : array();
     // Set form action
     $action = Route::get('contact')->uri(array('action' => $this->request->action())) . URL::query($destination);
     // Get user
     $user = User::active_user();
     // Set mail types
     $types = $config->get('types', array());
     $view = View::factory('contact/form')->set('destination', $destination)->set('action', $action)->set('config', $config)->set('types', $types)->set('user', $user)->bind('post', $post)->bind('errors', $this->_errors);
     // Initiate Captcha
     if ($config->get('use_captcha', FALSE) and !$this->_auth->logged_in()) {
         $captcha = Captcha::instance();
         $view->set('captcha', $captcha);
     }
     if ($this->valid_post('contact')) {
         $post = Validation_Contact::factory($this->request->post());
         if ($post->check()) {
             // Create the email subject
             $subject = __('[:category] :subject', array(':category' => $types[$post['category']], ':subject' => Text::plain($post['subject'])));
             // Create the email body
             $body = View::factory('email/contact')->set('name', $post['name'])->set('body', $post['body'])->set('config', Config::load('site'))->render();
             // Create an email message
             $email = Email::factory()->to(Text::plain($this->_config->get('site_email', '*****@*****.**')), __('Webmaster :site', array(':site' => Template::getSiteName())))->subject($subject)->from($post['email'], Text::plain($post['name']))->message($body, 'text/html');
             // @todo message type should be configurable
             // Send the message
             $email->send();
             Log::info(':name sent an e-mail regarding :cat', array(':name' => Text::plain($post['name']), ':cat' => $types[$post['category']]));
             Message::success(__('Your message has been sent.'));
             // Always redirect after a successful POST to prevent refresh warnings
             $this->request->redirect(Route::get('contact')->uri(), 200);
         } else {
             $this->_errors = $post->errors('contact', TRUE);
         }
     }
     $this->response->body($view);
 }