Skip to content

Commit

Permalink
#20 - Add NextPrev when using Grid view
Browse files Browse the repository at this point in the history
Needs testing!
  • Loading branch information
Moc committed Sep 25, 2022
1 parent f0bf433 commit 9946a5d
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 20 deletions.
14 changes: 14 additions & 0 deletions admin_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,13 @@ class cookbook_recipes_ui extends e_admin_ui
'help' => 'Grid view or Datatables',
'tab' => 0,
),
'gridview_itemspp' => array(
'title' => 'Grid overview: items per page',
'type' => 'hidden',
'data' => 'int',
'help' => 'Number of recipes that are shown per page (when using Grid view)',
'tab' => 0,
),
'date_format' => array(
'title' => 'Date format',
'type' => 'dropdown',
Expand Down Expand Up @@ -563,6 +570,13 @@ public function init()
"overview_datatable" => "Datatable overview", // TODO LAN
);

// Grid overview: items per page
if($pref['overview_format'] == "overview_grid")
{
// Change type from 'hidden' to 'number'
$this->prefs['gridview_itemspp']['type'] = 'number';
}

}

// Make some adjustments before storing the new data in the database
Expand Down
39 changes: 28 additions & 11 deletions cookbook_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,10 @@ private function loadRecipeInfo($data)
return e107::getParser()->parseTemplate($RECIPE_INFO, true, $sc);
}

public function renderOverviewTable($recipes)
public function renderOverviewTable($recipes, $parm = array())
{
$text = '';
$vars = array();

$key = e107::getPlugPref('cookbook', 'overview_format', 'overview_grid');

Expand All @@ -249,19 +250,35 @@ public function renderOverviewTable($recipes)

$text .= e107::getParser()->parseTemplate($template['start'], true, $sc);

// Nextprev when using Grid overview
if($key == 'overview_grid' && isset($parm['from']))
{
$count = count($recipes);
$page = $parm['from'];
$perPage = e107::getPlugPref('cookbook', 'gridview_itemspp', 10);
$from = ($page - 1) * $perPage;
$total = ceil($count / $perPage);

$recipes = array_slice($recipes, $from, $perPage);

$vars['recipecount'] = $count;
}

foreach($recipes as $recipe)
{
// Pass query values onto the shortcodes
$sc->setVars($recipe);
$text .= e107::getParser()->parseTemplate($template['items'], true, $sc);
}

// Pass 'recipecount' to nextprev shortcode
$sc->setVars($vars);
$text .= e107::getParser()->parseTemplate($template['end'], true, $sc);

return $text;
}

public function renderLatestRecipes()
public function renderLatestRecipes($parm = array())
{
$sql = e107::getDb();
$text = '';
Expand All @@ -281,7 +298,7 @@ public function renderLatestRecipes()
// Check if there are recipes in this category
if($recipes)
{
$text .= $this->renderOverviewTable($recipes);
$text .= $this->renderOverviewTable($recipes, $parm);
}
// No recipes in this category yet
else
Expand All @@ -295,7 +312,7 @@ public function renderLatestRecipes()
return $text;
}

public function renderCategory($data)
public function renderCategory($data, $parm = array())
{
$sql = e107::getDb();
$text = '';
Expand Down Expand Up @@ -334,7 +351,7 @@ public function renderCategory($data)
// Check if there are recipes in this category
if($recipes)
{
$text .= $this->renderOverviewTable($recipes);
$text .= $this->renderOverviewTable($recipes, $parm);
}
// No recipes in this category yet
else
Expand Down Expand Up @@ -401,7 +418,7 @@ public function renderCategories()
return $text;
}

public function renderKeyword($keyword)
public function renderKeyword($keyword, $parm = array())
{
$sql = e107::getDb();
$tp = e107::getParser();
Expand All @@ -427,7 +444,7 @@ public function renderKeyword($keyword)
// Check if there are recipes with this keyword
if($recipes)
{
$text .= $this->renderOverviewTable($recipes);
$text .= $this->renderOverviewTable($recipes, $parm);
}
// No recipes with this keyword
else
Expand Down Expand Up @@ -461,7 +478,7 @@ public function renderKeywordOverview()
return $text;
}

public function renderBookmarks()
public function renderBookmarks($parm = array())
{
// If not logged in, redirect to Cookbook index
if(!USERID)
Expand Down Expand Up @@ -494,7 +511,7 @@ public function renderBookmarks()
$recipes[] = e107::getDb()->retrieve('cookbook_recipes', '*', 'r_id = '.$r_id);
}

$text = $this->renderOverviewTable($recipes);
$text = $this->renderOverviewTable($recipes, $parm);
}
else
{
Expand All @@ -507,7 +524,7 @@ public function renderBookmarks()
return $text;
}

public function renderRecipeOverview()
public function renderRecipeOverview($parm = array())
{
$sql = e107::getDb();
$tp = e107::getParser();
Expand All @@ -522,7 +539,7 @@ public function renderRecipeOverview()
// Check if there are recipes
if($recipes)
{
$text .= $this->renderOverviewTable($recipes);
$text .= $this->renderOverviewTable($recipes, $parm);
}
// No recipes yet
else
Expand Down
14 changes: 14 additions & 0 deletions cookbook_shortcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -510,4 +510,18 @@ function sc_cookbook_related($parm = array())

return e107::getForm()->renderRelated($parm, $this->var['r_keywords'], array('cookbook' => $this->var['r_id']), $template);
}

function sc_grid_nextprev($parm = array())
{
$count = $this->var['recipecount'];

$page = empty($_GET['from']) ? 1 : (int) $_GET['from'];
$perPage = e107::getPlugPref('cookbook', 'gridview_itemspp', 10);

$from = ($page - 1) * $perPage;
$total = ceil($count / $perPage);
$options = array('type' => 'page', 'navcount' => 4);

return e107::getForm()->pagination(e_REQUEST_SELF.'?from=[FROM]', $total, $page, $perPage, $options);
}
}
30 changes: 21 additions & 9 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@

require_once(HEADERF);

// NEXTPREV - SET "FROM" to 1 IF NOT SET
// TODO CLEAN THIS UP
$overview_format = e107::getPlugPref('cookbook', 'overview_format', 'overview_grid');
if($overview_format == "overview_grid")
{
$parm = array();
$from = empty($_GET['from']) ? 1 : (int) $_GET['from'];

$parm['from'] = $from;
}

// Individual recipe
if(isset($_GET['p']) && $_GET['p'] == 'id' && $_GET['id'])
{
Expand All @@ -68,11 +79,11 @@
e107::getRender()->tablerender(LAN_CB_RECIPE.$cookbook_class->caption, $text, "recipe-item");
}
// Individual category
elseif(isset($_GET['p']) && $_GET['p'] == 'category' && $_GET['category'])
elseif(isset($_GET['p']) && $_GET['p'] == 'category' && isset($_GET['category']))
{
e107::route('cookbook/category');
$categoryid = (int) $_GET['category'];
$text = $cookbook_class->renderCategory($categoryid);
$category = (int) $_GET['category'];
$text = $cookbook_class->renderCategory($category, $parm);

e107::getRender()->tablerender($cookbook_class->caption, $text);
}
Expand All @@ -88,10 +99,11 @@
elseif(isset($_GET['p']) && $_GET['p'] == 'keyword' && $_GET['keyword'])
{
e107::route('cookbook/keyword');
$keyword = e107::getParser()->toDb($_GET['keyword']);
$text = $cookbook_class->renderKeyword($keyword);
$keyword = e107::getParser()->filter($_GET['keyword']);
$keyword = preg_split("#/#", $keyword);
$text = $cookbook_class->renderKeyword($keyword[0], $parm);

e107::getRender()->tablerender(LAN_KEYWORDS." - ".$keyword, $text);
e107::getRender()->tablerender(LAN_KEYWORDS." - ".$keyword[0], $text);
}
// Keyword overview (tagcloud)
elseif(isset($_GET['p']) && $_GET['p'] == 'keywords')
Expand All @@ -105,23 +117,23 @@
elseif(isset($_GET['p']) && $_GET['p'] == 'latest')
{
e107::route('cookbook/latest');
$text = $cookbook_class->renderLatestRecipes();
$text = $cookbook_class->renderLatestRecipes($parm);

e107::getRender()->tablerender(LAN_CB_RECIPE_LATEST, $text);
}
// Bookmarks
elseif(isset($_GET['p']) && $_GET['p'] == 'bookmarks')
{
e107::route('cookbook/bookmarks');
$text = $cookbook_class->renderBookmarks();
$text = $cookbook_class->renderBookmarks($parm);

e107::getRender()->tablerender(LAN_CB_BOOKMARKS, $text);
}
// Recipe overview (home)
else
{
e107::route('cookbook/index');
$text = $cookbook_class->renderRecipeOverview();
$text = $cookbook_class->renderRecipeOverview($parm);

e107::getRender()->tablerender(LAN_CB_RECIPE_OVERVIEW, $text);
}
Expand Down
3 changes: 3 additions & 0 deletions templates/cookbook_template.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
';

$COOKBOOK_TEMPLATE['overview_grid']['end'] = '
</div>
<div class="row">
{GRID_NEXTPREV}
</div>';


Expand Down

0 comments on commit 9946a5d

Please sign in to comment.