d26ea94cf44d9d6d18e5c6ce37b6dd6706a9329c

Author: k-kishida

Date: 2010-02-11 18:36:42 +0900

Can save filter query is compatible redmine format.

diff --git a/app/controllers/app_controller.php b/app/controllers/app_controller.php index a105225..d26c3c4 100644 --- a/app/controllers/app_controller.php +++ b/app/controllers/app_controller.php @@ -347,6 +347,8 @@ class AppController extends Controller { $value = $this->params['url'][$name]; } elseif(is_array($this->data) && array_key_exists($this->{$this->modelClass}->name, $this->data) && array_key_exists($name, $this->data[$this->{$this->modelClass}->name])) { $value = $this->data[$this->{$this->modelClass}->name][$name]; + } elseif(array_key_exists('form', $this->params) && array_key_exists($name, $this->params['form'])) { + $value = $this->params['form'][$name]; } else { $value = null; } @@ -411,8 +413,9 @@ class AppController extends Controller { function _findProject() { - if ( isset($this->params['project_id']) ) { - if ($this->_project = $this->Project->findMainProject($this->params['project_id'])) { + $project_id = $this->_get_param('project_id'); + if ( !empty($project_id) ) { + if ($this->_project = $this->Project->findMainProject($project_id)) { $this->set(array('main_project'=> $this->_project)); $this->set('main_project', $this->_project); } else { diff --git a/app/controllers/components/queries.php b/app/controllers/components/queries.php index 0ed01fb..d180af3 100755 --- a/app/controllers/components/queries.php +++ b/app/controllers/components/queries.php @@ -14,7 +14,7 @@ class QueriesComponent extends Object function retrieve_query($forse_set_filter = null) { $self = $this->controller; - $Query =& ClassRegistry::getObject('Query'); + $Query = $self->Query; $self->set('force_show_filters', $force_show_filters = $Query->show_filters()); $show_filters = isset($self->params['url']['set_filter']) || $forse_set_filter ? a() : $force_show_filters; $available_filters = $Query->available_filters($self->_project, $self->current_user); diff --git a/app/controllers/queries_controller.php b/app/controllers/queries_controller.php index 3519b0e..b26d4e6 100644 --- a/app/controllers/queries_controller.php +++ b/app/controllers/queries_controller.php @@ -43,7 +43,7 @@ class QueriesController extends AppController # menu_item :issues function beforeFilter() { - $this->params['project_id'] = $this->params['form']['project_id']; + $this->MenuManager->menu_item('issues'); return parent::beforeFilter(); } @@ -59,33 +59,28 @@ class QueriesController extends AppController $query['user'] = $this->current_user; $query['is_public'] = $query['project'] && $this->User->is_allowed_to($this->current_user, ':manage_public_queries', $this->_project) || $this->current_user['admin'] ? true : false; $query['default_columns'] = true; + + if(!empty($this->data) && $this->RequestHandler->isPost() && $this->_get_param('confirm') && !$this->RequestHandler->isAjax()) { + $this->data['Query']['project_id'] = $this->_project['Project']['id']; + $this->data['Query']['user_id'] = $this->current_user['id']; + + foreach($this->params['form']['fields'] as $field) { + $this->Query->add_filter($field, $this->params['form']['operators'][$field], $this->params['form']['values'][$field]); + } + $this->Query->save($this->data); + if(empty($this->Query->validationErrors)) { + $this->Session->setFlash(__('Successful creation.', true), 'default', array('class'=>'flash flash_notice')); + $this->redirect(array('controller'=>'issues', 'action'=>'index', 'project_id'=>$this->_project['Project']['identifier'])); + } + } + if (isset($this->data['Query'])) $query = am($query, $this->data['Query']); $this->data = am($this->data, array( 'Query' => $query, - )); - $this->set('params', $this->params); + )); if ($this->RequestHandler->isAjax()) $this->layout = 'ajax'; } -# def new -# @query = Query.new(params[:query]) -# @query.project = params[:query_is_for_all] ? nil : @project -# @query.user = User.current -# @query.is_public = false unless (@query.project && current_role.allowed_to?(:manage_public_queries)) || User.current.admin? -# @query.column_names = nil if params[:default_columns] -# -# params[:fields].each do |field| -# @query.add_filter(field, params[:operators][field], params[:values][field]) -# end if params[:fields] -# -# if request.post? && params[:confirm] && @query.save -# flash[:notice] = l(:notice_successful_create) -# redirect_to :controller => 'issues', :action => 'index', :project_id => @project, :query_id => @query -# return -# end -# render :layout => false if request.xhr? -# end -# function edit() { } diff --git a/app/models/query.php b/app/models/query.php index e46426c..e6e4884 100644 --- a/app/models/query.php +++ b/app/models/query.php @@ -4,7 +4,7 @@ ## ## This program is free software; you can redistribute it and/or ## modify it under the terms of the GNU General Public License -## as published by the Free Software Foundation; either version 2 +## as published by the Free Software Foundation; either version 2008 ## of the License, or (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, @@ -60,11 +60,23 @@ class Query extends AppModel 'Candy', ); # serialize :filters + var $validate = array( + 'name' => array( + 'validates_presence_of'=>array('rule'=>array('notEmpty')), + 'validates_length_of'=>array('rule'=>array('maxLength', 255)), + ), + 'filters' => array( + 'validates_presence_of'=>array('rule'=>array('validate_filters')) + ) + ); + var $column_names; var $operators; var $operators_by_filter_type; var $default_show_filters; var $available_filters; + var $filetes = array(); + function __construct() { if (!$this->operators) { @@ -115,6 +127,9 @@ class Query extends AppModel function available_filters($project = array(), $currentuser = array()) { + if(!empty($this->available_filters)) { + return $this->available_filters; + } $Status = & ClassRegistry::init('Status'); $IssueStatus = & ClassRegistry::init('IssueStatus'); $Enumeration = & ClassRegistry::init('Enumeration'); @@ -205,6 +220,7 @@ class Query extends AppModel $available_filters[$k]['operators'][$operator] = $this->operators[$operator]; } } + $this->available_filters = $available_filters; return $available_filters; } function show_filters($options = array()) @@ -282,15 +298,19 @@ class Query extends AppModel # @is_for_all = project.nil? # end # -# def validate -# filters.each_key do |field| -# errors.add label_for(field), :activerecord_error_blank unless -# # filter requires one or more values -# (values_for(field) and !values_for(field).first.blank?) or -# # filter doesn't require any value -# ["o", "c", "!*", "*", "t", "w"].include? operator_for(field) -# end if filters -# end + function validate_filters() { + foreach($this->filters as $field => $values) { + # filter requires one or more values + # filter doesn't require any value + $value = $this->values_for($field); + if(!(!is_null($value) && !empty($value[0]) || + in_array($this->operator_for($field), array("o", "c", "!*", "*", "t", "w")) + )) { + $this->invalidate($this->label_for($field), 'Please be sure to input.'); + } + } + return true; + } # # def editable_by?(user) # return false unless user @@ -475,44 +495,57 @@ class Query extends AppModel # end } -# def add_filter(field, operator, values) -# # values must be an array -# return unless values and values.is_a? Array # and !values.first.empty? -# # check if field is defined as an available filter -# if available_filters.has_key? field -# filter_options = available_filters[field] -# # check if operator is allowed for that filter -# #if @@operators_by_filter_type[filter_options[:type]].include? operator -# # allowed_values = values & ([""] + (filter_options[:values] || []).collect {|val| val[1]}) -# # filters[field] = {:operator => operator, :values => allowed_values } if (allowed_values.first and !allowed_values.first.empty?) or ["o", "c", "!*", "*", "t"].include? operator -# #end -# filters[field] = {:operator => operator, :values => values } -# end -# end -# -# def add_short_filter(field, expression) -# return unless expression -# parms = expression.scan(/^(o|c|\!|\*)?(.*)$/).first -# add_filter field, (parms[0] || "="), [parms[1] || ""] -# end -# -# def has_filter?(field) -# filters and filters[field] -# end -# -# def operator_for(field) -# has_filter?(field) ? filters[field][:operator] : nil -# end -# -# def values_for(field) -# has_filter?(field) ? filters[field][:values] : nil -# end -# -# def label_for(field) -# label = available_filters[field][:name] if available_filters.has_key?(field) -# label ||= field.gsub(/\_id$/, "") -# end -# + function add_filter($field, $operator, $values) { + if(empty($values)) { + return; + } + # values must be an array + if(!is_array($values)) { + $values = array($values); + } + # check if field is defined as an available filter + if (array_key_exists($field, $this->available_filters)) { + $this->filter_options = $this->available_filters[$field]; + # check if operator is allowed for that filter + #if @@operators_by_filter_type[filter_options[:type]].include? operator + # allowed_values = values & ([""] + (filter_options[:values] || []).collect {|val| val[1]}) + # filters[field] = {:operator => operator, :values => allowed_values } if (allowed_values.first and !allowed_values.first.empty?) or ["o", "c", "!*", "*", "t"].include? operator + #end + $this->filters[$field] = compact('operator', 'values'); + } + } + + function add_short_filter($field, $expression) { + if (empry($expression)) return; + preg_match('/^(o|c|\!|\*)?(.*)$/', $expression, $matches); + $parms = $matches[0]; + $operator = empty($parms[0]) ? "=" : $parms[0]; + $values = empty($parms[1]) ? "" : $parms[1]; + $this->add_filter($field, $operator, $values); + } + + function has_filter($field) { + return !empty($this->filters[$field]); + } + + function operator_for($field) { + return $this->has_filter($field) ? $this->filters[$field]['operator'] : null; + } + + function values_for($field) { + return $this->has_filter($field) ? $this->filters[$field]['values'] : null; + } + + function label_for($field) { + if (array_key_exists($field, $this->available_filters)) { + $label = $this->available_filters[$field]['name']; + } + if(empty($label)) { + $label = preg_replace('/\_id$/', "", $field); + } + return $label; + } + function available_columns() { # return @available_columns if @available_columns @@ -739,4 +772,39 @@ class Query extends AppModel # s.join(' AND ') # end #end + function beforeSave() + { + if (isset($this->filters)) { + $rb_filters = array(); + // Convert for ruby serialize format + foreach($this->filters as $field=>$filter) { + $rb_operator = '"'.$filter['operator'].'"'; + $rb_values = array(); + foreach($filter['values'] as $value) { + $rb_values[] = is_numeric($value) ? '"'.$value.'"' : $value; + } + $rb_filters[$field] = array(':operator'=>$rb_operator, ':values'=>$rb_values); + } + // To YAML format + $this->data[$this->name]['filters'] = Spyc::YAMLDump($rb_filters); + } + return true; + } + + function afterFind($results, $primary = false) + { + if (isset($results['id'])) { + $rb_filters = Spyc::YAMLLoad($results['filters']); + foreach($rb_filters as $field=>$filter) { + $operator = trim($filter[':operator'], '"'); + $values = array(); + foreach($filter[':values'] as $value) { + $values[] = trim($value, '"'); + } + $this->filters[$field] = compact('operator', 'values'); + } + } + return $results; + } + } \ No newline at end of file diff --git a/app/views/elements/queries/columns.ctp b/app/views/elements/queries/columns.ctp index 9db24a1..208f3d2 100755 --- a/app/views/elements/queries/columns.ctp +++ b/app/views/elements/queries/columns.ctp @@ -24,6 +24,4 @@ <!--<% end %>--> </fieldset> -<% content_for :header_tags do %> -<%= javascript_include_tag 'select_list_move' %> -<% end %> +<?php $javascript->link('select_list_move', false);?> diff --git a/app/views/elements/queries/filters.ctp b/app/views/elements/queries/filters.ctp index 0fd0a3b..73fb865 100755 --- a/app/views/elements/queries/filters.ctp +++ b/app/views/elements/queries/filters.ctp @@ -79,7 +79,7 @@ function toggle_multi_select(field) { case 'list_status': case 'list_subprojects': ?> - <?php echo $form->select('Filter.values_' . $field, $filter['values'], null, am(count($filter['values']) > 1 ? array('multiple' => 'true'): a(), array('name' => 'values[' . $field . ']', 'class' => 'select-small', 'style' => 'vertical-align: top;', 'id' => 'values_' . $field)), false) ?> + <?php echo $form->select('Filter.values_' . $field, $filter['values'], '1', am(count($filter['values']) > 1 ? array('multiple' => 'true'): a(), array('name' => 'values[' . $field . ']', 'class' => 'select-small', 'style' => 'vertical-align: top;', 'id' => 'values_' . $field)), false) ?> <?php echo $html->link($html->image('bullet_toggle_plus.png'), '#', array('onclick' => "toggle_multi_select('" . $javascript->escapeString($field) . "')", 'style' => 'vertical-align: bottom'), null, false) ?> <?php break; diff --git a/app/views/queries/add.ctp b/app/views/queries/add.ctp index 7bd91ba..d3d8107 100644 --- a/app/views/queries/add.ctp +++ b/app/views/queries/add.ctp @@ -1,7 +1,8 @@ <h2><?php __('New query') ?></h2> -<form action="<?php $html->url(array('controller' => 'queries', 'action' => 'add', 'project_id' => $main_project['Project']['identifier'])) ?>" onsubmit="selectAllOptions('selected_columns');"> +<form action="<?php echo $html->url(array('controller' => 'queries', 'action' => 'add', '?'=>array('project_id' => $main_project['Project']['identifier']))) ?>" onsubmit="selectAllOptions('selected_columns');" method="POST"> <!--<% form_tag({:action => 'new', :project_id => @query.project}, :onsubmit => 'selectAllOptions("selected_columns");') do %>--> +<?php echo $this->renderElement('error_explanation'); ?> <?php echo $this->renderElement('queries/form', array('query' => array('Query' => $this->data['Query']))) ?> <!--<%= render :partial => 'form', :locals => {:query => @query} %>--> <?php echo $form->submit(__('Save', true)) ?>