Browse Source

saving categories preferences

master
n0m1s 10 years ago
parent
commit
02102cb665
3 changed files with 77 additions and 1 deletions
  1. +63
    -0
      categoriesHandling.php
  2. +5
    -1
      index.php
  3. +9
    -0
      update-categories.php

+ 63
- 0
categoriesHandling.php View File

@ -0,0 +1,63 @@
<?php
/**
* @brief inits the categories from cookies and URL params
*
* @param $db: a PDO connection to the database
*
* @warning session_start() MUST have been called before calling this function
*/
function initCategories($db)
{
if(!isset($_SESSION['categorieStatus']))
{
//init all categories
$categories = getCategories($db);
foreach($categories as $cat)
$_SESSION['categorieStatus'][$cat['cat_id']] = true;
//loading from preferences
loadCategoriesFromCookie();
}
}
/**
* @brief loads the categories preferences from the cookie
*/
function loadCategoriesFromCookie()
{
if(isset($_COOKIE['categorieStatus']))
{
$categories = unserialize($_COOKIE['categorieStatus']);
if(is_array($categories))
for($i = 0; $i < count($categories); ++$i)
if(is_bool($categories[$i]))
$_SESSION[$i] = $categories[$i];
}
}
/**
* @brief saves the categories preferences from $_POST to $_SESSION
*
* @warning $_SESSION['categorieStatus] MUST have been populated before calling this function
*/
function saveCategoriesPostToSession()
{
foreach($_SESSION['categorieStatus'] as $cat_id => $cat_value)
{
$catPostName = 'cat_'.$cat_id;
if(isset($_POST[$catPostName]))
$_SESSION['categorieStatus'][$cat_id] = ($_POST[$catPostName] == 'on');
else
$_SESSION['categorieStatus'][$cat_id] = false;
}
}
/**
* @brief saves the categories preferences from $_SESSION to a cookie
*/
function saveCategoriesSessionToCookie()
{
setcookie('categorieStatus', serialize($_SESSION['categorieStatus']), strtotime('+6 months'), null, null, false, true);
}
?>

+ 5
- 1
index.php View File

@ -2,8 +2,11 @@
session_start(); session_start();
include_once('databaseOperations.php'); include_once('databaseOperations.php');
include_once('datetimeOperations.php'); include_once('datetimeOperations.php');
include_once('categoriesHandling.php');
$db = connect(); $db = connect();
initCategories($db);
if(isset($_GET['w'])&&is_numeric($_GET['w'])) if(isset($_GET['w'])&&is_numeric($_GET['w']))
$weekOffset = $_GET['w']; $weekOffset = $_GET['w'];
else else
@ -61,7 +64,7 @@ else
$cat_id = $categories[$cat_it]['cat_id']; $cat_id = $categories[$cat_it]['cat_id'];
$cat_title = $categories[$cat_it]['cat_title']; $cat_title = $categories[$cat_it]['cat_title'];
?> ?>
<input type="checkbox" id="cat_<?php echo $cat_id; ?>" name="cat_<?php echo $cat_id; ?>" />
<input type="checkbox" id="cat_<?php echo $cat_id; ?>" name="cat_<?php echo $cat_id; ?>" <?php if($_SESSION['categorieStatus'][$cat_id]) echo 'checked'; ?> />
<label for="cat_<?php echo $cat_id; ?>"><span></span><?php echo $cat_title;?></label><br /> <label for="cat_<?php echo $cat_id; ?>"><span></span><?php echo $cat_title;?></label><br />
<?php <?php
} }
@ -71,6 +74,7 @@ else
<?php <?php
} }
?> ?>
<input type="submit" id="change" value="sauvegarder cat&eacute;gories" />
</form> </form>
<a id="ancherEvent" href="./addEvent.php"> <a id="ancherEvent" href="./addEvent.php">
<div class="Button" id="AddEvent"> <div class="Button" id="AddEvent">


+ 9
- 0
update-categories.php View File

@ -0,0 +1,9 @@
<?php
session_start();
include_once('categoriesHandling.php');
saveCategoriesPostToSession();
saveCategoriesSessionToCookie();
header('Location:./');
?>

Loading…
Cancel
Save