diff --git a/google_analytics_views/google_analytics_views.info b/google_analytics_views/google_analytics_views.info index 86f578b..199c4f3 100644 --- a/google_analytics_views/google_analytics_views.info +++ b/google_analytics_views/google_analytics_views.info @@ -7,10 +7,11 @@ dependencies[] = google_analytics_api files[] = google_analytics_views.module files[] = google_analytics_views.views.inc files[] = plugins/google_analytics_plugin_query_google_analytics.inc +files[] = handlers/google_analytics_handler_filter.inc files[] = handlers/google_analytics_handler_filter_startend.inc files[] = handlers/google_analytics_handler_filter_string.inc files[] = handlers/google_analytics_handler_filter_numeric.inc -files[] = handlers/google_analytics_handler_field_string.inc -files[] = handlers/google_analytics_handler_argument_string.inc +files[] = handlers/google_analytics_handler_field.inc +files[] = handlers/google_analytics_handler_argument.inc diff --git a/google_analytics_views/google_analytics_views.module b/google_analytics_views/google_analytics_views.module index 0344ef2..37722bf 100644 --- a/google_analytics_views/google_analytics_views.module +++ b/google_analytics_views/google_analytics_views.module @@ -1251,3 +1251,24 @@ function google_analytics_views_get_fields() { ); } + +/** + * Determines if a field is custom or not. + */ +function google_analytics_views_is_custom($field) { + return preg_match('/\(n\)/', $field) ? TRUE : FALSE; +} + +/** + * Converts a base custom field name and number into a specific field name. + */ +function google_analytics_views_custom_to_variable_field($field, $number) { + return preg_replace('/\(n\)/', $number, $field); +} + +/** + * Converts a specific field name into a base custom field name. + */ +function google_analytics_views_variable_to_custom_field($field) { + return preg_replace('/\d/', '(n)', $field); +} diff --git a/google_analytics_views/google_analytics_views.views.inc b/google_analytics_views/google_analytics_views.views.inc index bb46ab2..44ad0d4 100644 --- a/google_analytics_views/google_analytics_views.views.inc +++ b/google_analytics_views/google_analytics_views.views.inc @@ -43,14 +43,14 @@ function google_analytics_views_views_data() { 'help' => $field['description'], 'group' => sprintf('%s %ss', $field['group'], ucfirst($field['type'])), 'field' => array( - 'handler' => 'google_analytics_handler_field_string', + 'handler' => 'google_analytics_handler_field', 'click sortable' => TRUE, ), 'sort' => array( 'handler' => 'views_handler_sort', ), 'argument' => array( - 'handler' => ($field['type'] == 'metric') ? 'google_analytics_handler_argument_string' : 'google_analytics_handler_argument_string', + 'handler' => 'google_analytics_handler_argument', ), 'filter' => array( 'handler' => ($field['type'] == 'metric') ? 'google_analytics_handler_filter_numeric' : 'google_analytics_handler_filter_string', diff --git a/google_analytics_views/handlers/google_analytics_handler_argument.inc b/google_analytics_views/handlers/google_analytics_handler_argument.inc new file mode 100644 index 0000000..3960c75 --- /dev/null +++ b/google_analytics_views/handlers/google_analytics_handler_argument.inc @@ -0,0 +1,74 @@ +is_custom = google_analytics_views_is_custom($this->real_field); + } + + /** + * {@inheritdoc} + */ + function query($group_by = FALSE) { + if ($this->is_custom) { + $this->real_field = google_analytics_views_custom_to_variable_field($this->real_field, $this->options['custom_field_number']); + } + + $this->operator = '=='; + $this->query->add_where(1, $this->real_field, $this->argument, $this->operator); + } + + /** + * {@inheritdoc} + */ + function option_definition() { + $options = parent::option_definition(); + + if ($this->is_custom) { + $options['custom_field_number'] = array('default' => 1); + } + + return $options; + } + + /** + * {@inheritdoc} + */ + function options_form(&$form, &$form_state) { + if ($this->is_custom) { + $form['custom_field_number'] = array( + '#type' => 'textfield', + '#title' => t('Custom field number'), + '#default_value' => isset($this->options['custom_field_number']) ? $this->options['custom_field_number'] : 1, + '#size' => 2, + '#maxlength' => 2, + '#required' => TRUE, + '#element_validate' => array('element_validate_integer_positive'), + ); + } + + parent::options_form($form, $form_state); + } + +} diff --git a/google_analytics_views/handlers/google_analytics_handler_argument_string.inc b/google_analytics_views/handlers/google_analytics_handler_argument_string.inc deleted file mode 100644 index 614e558..0000000 --- a/google_analytics_views/handlers/google_analytics_handler_argument_string.inc +++ /dev/null @@ -1,22 +0,0 @@ -operator = '=@'; - $this->query->add_where(1, $this->real_field, $this->argument, $this->operator); - } - -} diff --git a/google_analytics_views/handlers/google_analytics_handler_field.inc b/google_analytics_views/handlers/google_analytics_handler_field.inc new file mode 100644 index 0000000..9eac374 --- /dev/null +++ b/google_analytics_views/handlers/google_analytics_handler_field.inc @@ -0,0 +1,73 @@ +is_custom = google_analytics_views_is_custom($this->real_field); + } + + /** + * {@inheritdoc} + */ + function query() { + if ($this->is_custom) { + $this->real_field = google_analytics_views_custom_to_variable_field($this->real_field, $this->options['custom_field_number']); + } + + parent::query(); + } + + /** + * {@inheritdoc} + */ + function option_definition() { + $options = parent::option_definition(); + + if ($this->is_custom) { + $options['custom_field_number'] = array('default' => 1); + } + + return $options; + } + + /** + * {@inheritdoc} + */ + function options_form(&$form, &$form_state) { + if ($this->is_custom) { + $form['custom_field_number'] = array( + '#type' => 'textfield', + '#title' => t('Custom field number'), + '#default_value' => isset($this->options['custom_field_number']) ? $this->options['custom_field_number'] : 1, + '#size' => 2, + '#maxlength' => 2, + '#required' => TRUE, + '#element_validate' => array('element_validate_integer_positive'), + ); + } + + parent::options_form($form, $form_state); + } + +} diff --git a/google_analytics_views/handlers/google_analytics_handler_field_string.inc b/google_analytics_views/handlers/google_analytics_handler_field_string.inc deleted file mode 100644 index 81ffac3..0000000 --- a/google_analytics_views/handlers/google_analytics_handler_field_string.inc +++ /dev/null @@ -1,17 +0,0 @@ -is_custom = google_analytics_views_is_custom($this->real_field); + } + + /** + * {@inheritdoc} + */ + function option_definition() { + $options = parent::option_definition(); + + if ($this->is_custom) { + $options['custom_field_number'] = array('default' => 1); + } + + return $options; + } + + /** + * {@inheritdoc} + */ + function options_form(&$form, &$form_state) { + parent::options_form($form, $form_state); + + if ($this->is_custom) { + $form['custom_field_number'] = array( + '#type' => 'textfield', + '#title' => t('Custom field number'), + '#default_value' => isset($this->options['custom_field_number']) ? $this->options['custom_field_number'] : 1, + '#size' => 2, + '#maxlength' => 2, + '#required' => TRUE, + '#element_validate' => array('element_validate_integer_positive'), + ); + } + } + + /** + * {@inheritdoc} + */ + function operator_options($which = 'title') { + $options = array(); + foreach ($this->operators() as $id => $info) { + $options[$id] = $info[$which]; + } + + return $options; + } + + /** + * {@inheritdoc} + */ + function query() { + if ($this->is_custom) { + $this->real_field = google_analytics_views_custom_to_variable_field($this->real_field, $this->options['custom_field_number']); + } + + $info = $this->operators(); + if (!empty($info[$this->operator]['method'])) { + $this->{$info[$this->operator]['method']}($this->real_field); + } + } + +} diff --git a/google_analytics_views/handlers/google_analytics_handler_filter_numeric.inc b/google_analytics_views/handlers/google_analytics_handler_filter_numeric.inc index ee7364c..45c5962 100644 --- a/google_analytics_views/handlers/google_analytics_handler_filter_numeric.inc +++ b/google_analytics_views/handlers/google_analytics_handler_filter_numeric.inc @@ -5,8 +5,8 @@ * * @ingroup views_filter_handlers */ -class google_analytics_handler_filter_numeric extends views_handler_filter { - var $always_multiple = TRUE; +class google_analytics_handler_filter_numeric extends google_analytics_handler_filter { + function option_definition() { $options = parent::option_definition(); @@ -64,18 +64,6 @@ class google_analytics_handler_filter_numeric extends views_handler_filter { return $operators; } - /** - * Provide a list of all the numeric operators - */ - function operator_options($which = 'title') { - $options = array(); - foreach ($this->operators() as $id => $info) { - $options[$id] = $info[$which]; - } - - return $options; - } - function operator_values($values = 1) { $options = array(); foreach ($this->operators() as $id => $info) { @@ -176,14 +164,6 @@ class google_analytics_handler_filter_numeric extends views_handler_filter { } } - function query() { - $info = $this->operators(); - if (!empty($info[$this->operator]['method'])) { - $this->{$info[$this->operator]['method']}($this->real_field); - } - } - - function op_simple($field) { $this->query->add_where($this->options['group'], $field, $this->value['value'], $this->operator); } diff --git a/google_analytics_views/handlers/google_analytics_handler_filter_string.inc b/google_analytics_views/handlers/google_analytics_handler_filter_string.inc index badaa6d..46d5aa5 100644 --- a/google_analytics_views/handlers/google_analytics_handler_filter_string.inc +++ b/google_analytics_views/handlers/google_analytics_handler_filter_string.inc @@ -5,9 +5,7 @@ * * @ingroup views_filter_handlers */ -class google_analytics_handler_filter_string extends views_handler_filter { - // exposed filter options - var $always_multiple = TRUE; +class google_analytics_handler_filter_string extends google_analytics_handler_filter { function option_definition() { $options = parent::option_definition(); @@ -65,18 +63,6 @@ class google_analytics_handler_filter_string extends views_handler_filter { return $operators; } - /** - * Build strings from the operators() for 'select' options - */ - function operator_options($which = 'title') { - $options = array(); - foreach ($this->operators() as $id => $info) { - $options[$id] = $info[$which]; - } - - return $options; - } - function admin_summary() { if (!empty($this->options['exposed'])) { return t('exposed'); @@ -155,13 +141,6 @@ class google_analytics_handler_filter_string extends views_handler_filter { } } - function query() { - $info = $this->operators(); - if (!empty($info[$this->operator]['method'])) { - $this->{$info[$this->operator]['method']}($this->real_field); - } - } - function op_equal($field) { $this->query->add_where($this->options['group'], $field, $this->value, '=='); } diff --git a/google_analytics_views/plugins/google_analytics_plugin_query_google_analytics.inc b/google_analytics_views/plugins/google_analytics_plugin_query_google_analytics.inc index b6f4d68..4e6c6ce 100644 --- a/google_analytics_views/plugins/google_analytics_plugin_query_google_analytics.inc +++ b/google_analytics_views/plugins/google_analytics_plugin_query_google_analytics.inc @@ -147,8 +147,10 @@ class google_analytics_plugin_query_google_analytics extends views_plugin_query $query = array(); foreach ($this->fields as $field) { - if ($available_fields[$field['field']]) { - $type = $available_fields[$field['field']]['type']; + $field_name = google_analytics_views_variable_to_custom_field($field['field']); + + if ($available_fields[$field_name]) { + $type = $available_fields[$field_name]['type']; $type = ($type == 'dimension') ? 'dimensions' : 'metrics'; $query[$type][] = 'ga:' . $field['field']; } @@ -158,27 +160,19 @@ class google_analytics_plugin_query_google_analytics extends views_plugin_query foreach ($this->where as $where_group => $where) { foreach ($where['conditions'] as $condition) { - if ($condition['field'] == 'start_date') { - $query['start_date'] = $condition['value']; - continue; - } - if ($condition['field'] == 'end_date') { - $query['end_date'] = $condition['value']; - continue; - } - if ($condition['field'] == 'profile_id') { - $query['profile_id'] = $condition['value']; - continue; - } + $field_name = google_analytics_views_variable_to_custom_field($condition['field']); - if ($available_fields[$condition['field']]) { + if ($field_name == 'start_date' || $field_name == 'end_date' || $field_name == 'profile_id') { + $query[$field_name] = $condition['value']; + } + elseif ($available_fields[$field_name]) { $filters[$where_group][] = 'ga:' . $condition['field'] . $condition['operator'] . $condition['value']; } } $glue = ($where['type'] == 'AND') ? ';' : ','; $filters[$where_group] = implode($glue, $filters[$where_group]); } - + if (!empty($filters)) { $glue = ($this->group_operator == 'AND') ? ';' : ','; $query['filters'] = implode($glue, $filters);