Advertisement
Guest User

ajaxCookieList

a guest
Jul 12th, 2017
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.71 KB | None | 0 0
  1. <?php
  2. // Requests
  3. if($_GET['action']){
  4.     $action = $_GET['action'];
  5. }
  6. if($_GET['id']){
  7.     $id = $_GET['id'];
  8. }
  9.  
  10. // Some settings (you can chnage this when you call snippet like [[!ajaxCookieList? &cookieName=`wishlist`]])
  11. $cookieTime = 3600;
  12. $duration = time() + (int) $cookieTime;
  13. $cookieName = 'favorites';
  14. $parents = '6';
  15. $tpl = 'popup.favorites.item';
  16. $includeTVs = 'header.bgImage,franchise.logo,franchise.price,title,subtitle';
  17.  
  18. // Here we will be store our resource ids
  19. $values = [];
  20.  
  21. // Let's check if we already have cookies and convert it to array
  22. if(isset($_COOKIE[$cookieName])){
  23.     $values = explode(',', $_COOKIE[$cookieName]);
  24. }
  25.  
  26. // If user clicked add button
  27. if($action == 'add'){
  28.     // Check if we don't have this resource in array
  29.     if(!in_array($id, $values)){
  30.         $values[] = $id;
  31.     }
  32. }
  33. // If user mistaked or changed his mind
  34. if($action == 'remove'){
  35.     $values = explode(',', $_COOKIE[$cookieName]);
  36.     // If we have removing value in cookies
  37.     if(in_array($id, $values)){
  38.         // find it
  39.         $index = array_search($id, $values);
  40.         // delete it
  41.         unset($values[$index]);
  42.     }
  43. }
  44.  
  45. // Separate with comma resources
  46. $cookieValues = implode(',', $values);
  47.  
  48. // Now we will update cookies
  49. setcookie($cookieName, $cookieValues, $duration, '/');
  50.  
  51. // Final result of cookies, like "1,2,3"
  52. // We check if there any fresh values in our array and if not we grab data from cookie
  53. $ids = isset($cookieValues) ? $cookieValues : $_COOKIE[$cookieName];
  54.  
  55. $result = array(
  56.     'parents' => (!empty($ids)) ? $parents : '',
  57.     'resources' => $ids,
  58.     'tpl' => $tpl,
  59.     'includeTVs' => $includeTVs,
  60.     'prepareTVs' => '1',
  61.     'hideContainers' => '1'
  62. );
  63.  
  64. return $modx->runSnippet('pdoResources', $result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement