Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect $config['cur_page'] variable for pagination #4384

Merged
merged 1 commit into from
Jan 20, 2016

Conversation

jekkos
Copy link
Contributor

@jekkos jekkos commented Jan 18, 2016

After upgrading to CI3 I noticed that developers are able to determine the current page counter for pagination based on

  • Explicit query string parameters
  • URI segment configuration

In earlier versions a developer could still set the current page counter in the pagination lib directly which is useful if you want to use pagination with HTTP POST instead of GET. This could be done by passing

$config['cur_page']  = '10';

to the pagination function for link generation. Currently this code has changed and will always try to check whether the uri segment is a valid number or not, even if the cur_page variable was passed in the associative array, and fallback to zero if it fails to validate that result.

This can be easily resolved by checking whether the counter was already set with a valid number from the $config array before trying to resolve it from the uri segment.

This fix give a developer more flexibility and stop CI from overwriting the externally set value with an incorrect one.

Signed-off-by: jekkos jeroen.peelaerts@gmail.com

@@ -497,7 +497,7 @@ public function create_links()
{
$this->cur_page = $this->CI->input->get($this->query_string_segment);
}
else
else if (! ctype_digit($this->cur_page))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail if the cur_page value is an integer.
It also doesn't fit with our styleguide.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe is_numeric would be more appropriate here. I took this check from the same source file, so then I suppose I'll have to change https://github.com/bcit-ci/CodeIgniter/blob/develop/system/libraries/Pagination.php#L518 to validate integers as well. Otherwise pagination would fallback to the default page again.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding style guide, I copied the statement from the aformentioned code line, just to be certain that the change would be compliant. Regarding the styleguide I suppose there should be one space before and after the negation sign.

Will send a corrected version later today.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_numeric() won't be sufficient either, because it accepts floats (and IIRC some other weird stuff). The problem is that you're trying to satisfy multiple conditions by adding just one line of code ... it can't work that way. I'd do this:

elseif (empty($this->cur_page))
{
    // the current auto-detection from uri
}
else
{
    $this->cur_page = (string) $this->cur_page;
}

The string cast is necessary to keep the following checks working.

In addition to spaces around operators, we also write elseif no space.

@jekkos jekkos force-pushed the pagination-fix branch 3 times, most recently from 6ab80b8 to 626d1d7 Compare January 19, 2016 19:01
@jekkos
Copy link
Contributor Author

jekkos commented Jan 19, 2016

Hope the patch is compliant with the styleguide now.

@narfbg
Copy link
Contributor

narfbg commented Jan 20, 2016

Sadly not, we put braces on separate lines ...

After upgrading to CI3 I noticed that developers are able to determine the current page counter for pagination based on

* Explicit query string parameters
* URI segment configuration

In earlier versions a developer could still set the current page counter in the pagination lib directly which is useful if you want to use pagination with HTTP POST instead of GET. This could be done by passing

    $config['cur_page']  = '10';

to the pagination function for link generation. Currently this code has changed and will always try to check whether the uri segment is a valid number or not, even if the cur_page variable was passed in the associative array, and fallback to zero if it fails to validate that result.

This can be easily resolved by checking whether the counter was already set with a valid number from the $config array before trying to resolve it from the uri segment.

This fix give a developer more flexibility and stop CI from overwriting the externally set value with an incorrect one.

Signed-off-by: jekkos <jeroen.peelaerts@gmail.com>
@jekkos
Copy link
Contributor Author

jekkos commented Jan 20, 2016

Ok should be good now.

narfbg added a commit that referenced this pull request Jan 20, 2016
Respect $config['cur_page'] variable for pagination
@narfbg narfbg merged commit 4159996 into bcit-ci:develop Jan 20, 2016
narfbg added a commit that referenced this pull request Jan 20, 2016
@narfbg
Copy link
Contributor

narfbg commented Jan 20, 2016

For future reference - please don't put that whole description in the commit message. I just now noticed that you did this.

Stating just that cur_page was erroneously ignored would be fine, but personal thoughts should be avoided as they can be misleading and/or not actually in line with why the patch was merged. For example, utilizing this feature for POST requests is something that I'd rather discourage, but a user reading your commit message may take it as a "cool" idea instead.

I do like that you've overwritten previous commits and it's just one that appears in the log - I wish more people did that. :)

@narfbg narfbg added this to the 3.0.5 milestone Jan 20, 2016
@narfbg narfbg added the Bug label Jan 20, 2016
@jekkos
Copy link
Contributor Author

jekkos commented Jan 20, 2016

Thanks for the merge, using POST surely has some disadvantages. In this comment it was only used to clarify the need for more flexibility.

I also like the fact that commit history shows only what is relevant, thats why I rewrote the first commits afterwards :)

@jekkos jekkos deleted the pagination-fix branch January 21, 2016 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants