Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Requests
- if($_GET['action']){
- $action = $_GET['action'];
- }
- if($_GET['id']){
- $id = $_GET['id'];
- }
- // Some settings (you can chnage this when you call snippet like [[!ajaxCookieList? &cookieName=`wishlist`]])
- $cookieTime = 3600;
- $duration = time() + (int) $cookieTime;
- $cookieName = 'favorites';
- $parents = '6';
- $tpl = 'popup.favorites.item';
- $includeTVs = 'header.bgImage,franchise.logo,franchise.price,title,subtitle';
- // Here we will be store our resource ids
- $values = [];
- // Let's check if we already have cookies and convert it to array
- if(isset($_COOKIE[$cookieName])){
- $values = explode(',', $_COOKIE[$cookieName]);
- }
- // If user clicked add button
- if($action == 'add'){
- // Check if we don't have this resource in array
- if(!in_array($id, $values)){
- $values[] = $id;
- }
- }
- // If user mistaked or changed his mind
- if($action == 'remove'){
- $values = explode(',', $_COOKIE[$cookieName]);
- // If we have removing value in cookies
- if(in_array($id, $values)){
- // find it
- $index = array_search($id, $values);
- // delete it
- unset($values[$index]);
- }
- }
- // Separate with comma resources
- $cookieValues = implode(',', $values);
- // Now we will update cookies
- setcookie($cookieName, $cookieValues, $duration, '/');
- // Final result of cookies, like "1,2,3"
- // We check if there any fresh values in our array and if not we grab data from cookie
- $ids = isset($cookieValues) ? $cookieValues : $_COOKIE[$cookieName];
- $result = array(
- 'parents' => (!empty($ids)) ? $parents : '',
- 'resources' => $ids,
- 'tpl' => $tpl,
- 'includeTVs' => $includeTVs,
- 'prepareTVs' => '1',
- 'hideContainers' => '1'
- );
- return $modx->runSnippet('pdoResources', $result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement