Skip to content

Commit ccc9c32

Browse files
Merge remote-tracking branch 'real/develop' into fix_URI_resolveRelativeURI
2 parents 283bc52 + fd56585 commit ccc9c32

10 files changed

Lines changed: 145 additions & 55 deletions

File tree

system/Database/BaseConnection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,9 +1054,9 @@ public function showLastQuery(): string
10541054
*
10551055
* Used by the Debug Toolbar's timeline.
10561056
*
1057-
* @return float
1057+
* @return float|null
10581058
*/
1059-
public function getConnectStart(): float
1059+
public function getConnectStart(): ?float
10601060
{
10611061
return $this->connectTime;
10621062
}

system/Model.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,14 +1122,13 @@ public function chunk(int $size, Closure $userFunc)
11221122
*/
11231123
public function paginate(int $perPage = 20, string $group = 'default', int $page = 0)
11241124
{
1125-
// Get the necessary parts.
1126-
$page = $page >= 1 ? $page : (ctype_digit($_GET['page'] ?? '') && $_GET['page'] > 1 ? $_GET['page'] : 1);
1127-
1125+
$pager = \Config\Services::pager(null, null, false);
1126+
$page = $page >= 1 ? $page : $pager->getCurrentPage($group);
1127+
11281128
$total = $this->countAllResults(false);
11291129

11301130
// Store it in the Pager library so it can be
11311131
// paginated in the views.
1132-
$pager = \Config\Services::pager();
11331132
$this->pager = $pager->store($group, $page, $perPage, $total);
11341133

11351134
$offset = ($page - 1) * $perPage;

system/Pager/Pager.php

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ public function getPageURI(int $page = null, string $group = 'default', bool $re
348348
}
349349
else
350350
{
351-
$uri->addQuery('page', $page);
351+
$uri->addQuery($this->groups[$group]['pageSelector'], $page);
352352
}
353353

354354
if ($this->only)
@@ -357,7 +357,7 @@ public function getPageURI(int $page = null, string $group = 'default', bool $re
357357

358358
if (! $segment)
359359
{
360-
$query['page'] = $page;
360+
$query[$this->groups[$group]['pageSelector']] = $page;
361361
}
362362

363363
$uri->setQueryArray($query);
@@ -503,13 +503,31 @@ protected function ensureGroup(string $group)
503503
}
504504

505505
$this->groups[$group] = [
506-
'uri' => clone Services::request()->uri,
507-
'hasMore' => false,
508-
'total' => null,
509-
'perPage' => $this->config->perPage,
510-
'pageCount' => 1,
506+
'uri' => clone Services::request()->uri,
507+
'hasMore' => false,
508+
'total' => null,
509+
'perPage' => $this->config->perPage,
510+
'pageCount' => 1,
511+
'pageSelector' => $group === 'default' ? 'page' : 'page_' . $group,
511512
];
512513

514+
$this->calculateCurrentPage($group);
515+
516+
if ($_GET)
517+
{
518+
$this->groups[$group]['uri'] = $this->groups[$group]['uri']->setQueryArray($_GET);
519+
}
520+
}
521+
522+
//--------------------------------------------------------------------
523+
524+
/**
525+
* Calculating the current page
526+
*
527+
* @param string $group
528+
*/
529+
protected function calculateCurrentPage(string $group)
530+
{
513531
if (array_key_exists($group, $this->segment))
514532
{
515533
try
@@ -523,12 +541,11 @@ protected function ensureGroup(string $group)
523541
}
524542
else
525543
{
526-
$this->groups[$group]['currentPage'] = $_GET['page_' . $group] ?? $_GET['page'] ?? 1;
527-
}
544+
$pageSelector = $this->groups[$group]['pageSelector'];
528545

529-
if ($_GET)
530-
{
531-
$this->groups[$group]['uri'] = $this->groups[$group]['uri']->setQueryArray($_GET);
546+
$page = (int) ($_GET[$pageSelector] ?? 1);
547+
548+
$this->groups[$group]['currentPage'] = $page < 1 ? 1 : $page;
532549
}
533550
}
534551

system/Pager/PagerRenderer.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,13 @@ class PagerRenderer
9393
* @var integer
9494
*/
9595
protected $segment;
96-
96+
/**
97+
* Name of $_GET parameter
98+
*
99+
* @var integer
100+
*/
101+
protected $pageSelector;
102+
97103
//--------------------------------------------------------------------
98104

99105
/**
@@ -103,13 +109,14 @@ class PagerRenderer
103109
*/
104110
public function __construct(array $details)
105111
{
106-
$this->first = 1;
107-
$this->last = $details['pageCount'];
108-
$this->current = $details['currentPage'];
109-
$this->total = $details['total'];
110-
$this->uri = $details['uri'];
111-
$this->pageCount = $details['pageCount'];
112-
$this->segment = $details['segment'] ?? 0;
112+
$this->first = 1;
113+
$this->last = $details['pageCount'];
114+
$this->current = $details['currentPage'];
115+
$this->total = $details['total'];
116+
$this->uri = $details['uri'];
117+
$this->pageCount = $details['pageCount'];
118+
$this->segment = $details['segment'] ?? 0;
119+
$this->pageSelector = $details['pageSelector'] ?? 'page';
113120
}
114121

115122
//--------------------------------------------------------------------
@@ -164,7 +171,7 @@ public function getPrevious()
164171

165172
if ($this->segment === 0)
166173
{
167-
$uri->addQuery('page', $this->first - 1);
174+
$uri->addQuery($this->pageSelector, $this->first - 1);
168175
}
169176
else
170177
{
@@ -208,7 +215,7 @@ public function getNext()
208215

209216
if ($this->segment === 0)
210217
{
211-
$uri->addQuery('page', $this->last + 1);
218+
$uri->addQuery($this->pageSelector, $this->last + 1);
212219
}
213220
else
214221
{
@@ -231,7 +238,7 @@ public function getFirst(): string
231238

232239
if ($this->segment === 0)
233240
{
234-
$uri->addQuery('page', 1);
241+
$uri->addQuery($this->pageSelector, 1);
235242
}
236243
else
237244
{
@@ -254,7 +261,7 @@ public function getLast(): string
254261

255262
if ($this->segment === 0)
256263
{
257-
$uri->addQuery('page', $this->pageCount);
264+
$uri->addQuery($this->pageSelector, $this->pageCount);
258265
}
259266
else
260267
{
@@ -277,7 +284,7 @@ public function getCurrent(): string
277284

278285
if ($this->segment === 0)
279286
{
280-
$uri->addQuery('page', $this->current);
287+
$uri->addQuery($this->pageSelector, $this->current);
281288
}
282289
else
283290
{
@@ -306,7 +313,7 @@ public function links(): array
306313
for ($i = $this->first; $i <= $this->last; $i ++)
307314
{
308315
$links[] = [
309-
'uri' => (string) ($this->segment === 0 ? $uri->addQuery('page', $i) : $uri->setSegment($this->segment, $i)),
316+
'uri' => (string) ($this->segment === 0 ? $uri->addQuery($this->pageSelector, $i) : $uri->setSegment($this->segment, $i)),
310317
'title' => (int) $i,
311318
'active' => ($i === $this->current),
312319
];

system/View/Cell.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use CodeIgniter\Cache\CacheInterface;
4242
use CodeIgniter\View\Exceptions\ViewException;
4343
use ReflectionMethod;
44+
use Config\Services;
4445

4546
/**
4647
* Class Cell
@@ -119,6 +120,11 @@ public function render(string $library, $params = null, int $ttl = 0, string $ca
119120
// Not cached - so grab it...
120121
$instance = new $class();
121122

123+
if (method_exists($instance, 'initController'))
124+
{
125+
$instance->initController(Services::request(), Services::response(), Services::logger());
126+
}
127+
122128
if (! method_exists($instance, $method))
123129
{
124130
throw ViewException::forInvalidCellMethod($class, $method);

system/View/Parser.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,10 @@ protected function parsePair(string $variable, array $data, string $template): a
414414
$str .= $out;
415415
}
416416

417-
$replace['#' . $match[0] . '#s'] = $str;
417+
//Escape | character from filters as it's handled as OR in regex
418+
$escaped_match = preg_replace('/(?<!\\\\)\\|/', '\\|', $match[0]);
419+
420+
$replace['#' . $escaped_match . '#s'] = $str;
418421
}
419422

420423
return $replace;
@@ -766,7 +769,7 @@ protected function parsePlugins(string $template)
766769
foreach ($matchesParams[0] as $item)
767770
{
768771
$keyVal = explode('=', $item);
769-
if (count($keyVal) == 2)
772+
if (count($keyVal) === 2)
770773
{
771774
$params[$keyVal[0]] = str_replace('"', '', $keyVal[1]);
772775
}

system/View/View.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@ public function render(string $view, array $options = null, bool $saveData = nul
247247
$output = $this->render($layoutView, $options, $saveData);
248248
}
249249

250+
// Reset sections
251+
$this->sections = [];
252+
250253
$this->logPerformance($this->renderVars['start'], microtime(true), $this->renderVars['view']);
251254

252255
if ($this->debug && (! isset($options['debug']) || $options['debug'] === true))

tests/system/Pager/PagerTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,12 @@ public function testGetTotalPagesCalcsCorrectValue()
203203

204204
public function testGetNextURIUsesCurrentURI()
205205
{
206-
$_GET['page'] = 2;
206+
$_GET['page_foo'] = 2;
207207

208208
$this->pager->store('foo', 2, 12, 70);
209209

210210
$expected = current_url(true);
211-
$expected = (string)$expected->setQuery('page=3');
211+
$expected = (string)$expected->setQuery('page_foo=3');
212212

213213
$this->assertEquals((string)$expected, $this->pager->getNextPageURI('foo'));
214214
}
@@ -225,19 +225,19 @@ public function testGetNextURICorrectOnFirstPage()
225225
$this->pager->store('foo', 1, 12, 70);
226226

227227
$expected = current_url(true);
228-
$expected = (string)$expected->setQuery('page=2');
228+
$expected = (string)$expected->setQuery('page_foo=2');
229229

230230
$this->assertEquals($expected, $this->pager->getNextPageURI('foo'));
231231
}
232232

233233
public function testGetPreviousURIUsesCurrentURI()
234234
{
235-
$_GET['page'] = 2;
235+
$_GET['page_foo'] = 2;
236236

237237
$this->pager->store('foo', 2, 12, 70);
238238

239239
$expected = current_url(true);
240-
$expected = (string)$expected->setQuery('page=1');
240+
$expected = (string)$expected->setQuery('page_foo=1');
241241

242242
$this->assertEquals((string)$expected, $this->pager->getPreviousPageURI('foo'));
243243
}
@@ -252,28 +252,28 @@ public function testGetNextURIReturnsNullOnFirstPage()
252252
public function testGetNextURIWithQueryStringUsesCurrentURI()
253253
{
254254
$_GET = [
255-
'page' => 3,
256-
'status' => 1,
255+
'page_foo' => 3,
256+
'status' => 1,
257257
];
258258

259259
$expected = current_url(true);
260260
$expected = (string)$expected->setQueryArray($_GET);
261261

262-
$this->pager->store('foo', $_GET['page'] - 1, 12, 70);
262+
$this->pager->store('foo', $_GET['page_foo'] - 1, 12, 70);
263263

264264
$this->assertEquals((string)$expected, $this->pager->getNextPageURI('foo'));
265265
}
266266

267267
public function testGetPreviousURIWithQueryStringUsesCurrentURI()
268268
{
269269
$_GET = [
270-
'page' => 1,
271-
'status' => 1,
270+
'page_foo' => 1,
271+
'status' => 1,
272272
];
273273
$expected = current_url(true);
274274
$expected = (string)$expected->setQueryArray($_GET);
275275

276-
$this->pager->store('foo', $_GET['page'] + 1, 12, 70);
276+
$this->pager->store('foo', $_GET['page_foo'] + 1, 12, 70);
277277

278278
$this->assertEquals((string)$expected, $this->pager->getPreviousPageURI('foo'));
279279
}
@@ -383,12 +383,12 @@ public function testBasedURI()
383383
$this->config = new Pager();
384384
$this->pager = new \CodeIgniter\Pager\Pager($this->config, Services::renderer());
385385

386-
$_GET['page'] = 2;
386+
$_GET['page_foo'] = 2;
387387

388388
$this->pager->store('foo', 2, 12, 70);
389389

390390
$expected = current_url(true);
391-
$expected = (string)$expected->setQuery('page=1');
391+
$expected = (string)$expected->setQuery('page_foo=1');
392392

393393
$this->assertEquals((string)$expected, $this->pager->getPreviousPageURI('foo'));
394394
}

tests/system/View/ParserFilterTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,4 +409,59 @@ public function testLocalCurrency()
409409
$this->assertEquals('1.234.567,89 €', $parser->renderString($template));
410410
}
411411

412+
public function testParsePairWithAbs()
413+
{
414+
$parser = new Parser($this->config, $this->viewsDir, $this->loader);
415+
416+
$data = [
417+
'value1' => -1,
418+
'value2' => 1,
419+
'single' => [
420+
[
421+
'svalue1' => -2,
422+
'svalue2' => 2,
423+
],
424+
],
425+
'loop' => [
426+
[
427+
'lvalue' => -3,
428+
],
429+
[
430+
'lvalue' => 3,
431+
],
432+
],
433+
'nested' => [
434+
[
435+
'nvalue1' => -4,
436+
'nvalue2' => 4,
437+
'nsingle' => [
438+
[
439+
'nsvalue1' => -5,
440+
'nsvalue2' => 5,
441+
],
442+
],
443+
'nsloop' => [
444+
[
445+
'nlvalue' => -6,
446+
],
447+
[
448+
'nlvalue' => 6,
449+
],
450+
],
451+
],
452+
],
453+
];
454+
455+
$template = '{ value1|abs }{ value2|abs }'
456+
. '{single}{ svalue1|abs }{ svalue2|abs }{/single}'
457+
. '{loop}{ lvalue|abs }{/loop}'
458+
. '{nested}'
459+
. '{ nvalue1|abs }{ nvalue2|abs }'
460+
. '{nsingle}{ nsvalue1|abs }{ nsvalue2|abs }{/nsingle}'
461+
. '{nsloop}{ nlvalue|abs }{/nsloop}'
462+
. '{/nested}';
463+
464+
$parser->setData($data);
465+
$this->assertEquals('112233445566', $parser->renderString($template));
466+
}
412467
}

0 commit comments

Comments
 (0)