Overview

Namespaces

  • cli_db
    • propel
      • map
      • om
  • cli_import
  • LoggedPDO
  • None
  • PHP
  • webservices
    • cart
    • combisearch
    • details
      • annotations
        • feature
    • graphs
      • barplot
      • genome
    • listing
    • queue

Classes

  • Sync
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: namespace webservices\cart;
  4: 
  5: require_once 'TranscriptDB//db.php';
  6: 
  7: 
  8: /**
  9:  * Web Service.
 10:  * Replicates cart functionality on server side, syncing client-side cart with server-side cart in SESSION.
 11:  * If the user is logged in, data is loaded/changed/stored to database.
 12:  * Always returns state of the current cart.
 13:  */
 14: class Sync extends \WebService {
 15: 
 16:     public static $regexCartName = '^[a-z0-9._ ]+$';
 17: 
 18: 
 19:     /**
 20:      * Loads cart from database, if logged in.
 21:      * Locks row for update to prevent concurrent changes.
 22:      * @global \PDO $db
 23:      * @return nothing
 24:      */
 25:     private function loadCart() {
 26:         if (!isset($_SESSION['OpenID']) || empty($_SESSION['OpenID']))
 27:             return;
 28: 
 29:         global $db;
 30:         if (false)
 31:             $db = new \PDO();
 32: 
 33:         $stm_retrieve_cart = $db->prepare('SELECT value FROM webuser_data WHERE identity=:identity AND type_id=:type_cart FOR UPDATE');
 34:         $stm_retrieve_cart->bindValue('type_cart', WEBUSER_CART);
 35:         $stm_retrieve_cart->bindValue('identity', $_SESSION['OpenID']);
 36: 
 37:         $stm_retrieve_cart->execute();
 38:         if ($stm_retrieve_cart->rowCount() == 1) {
 39:             $row = $stm_retrieve_cart->fetch(\PDO::FETCH_ASSOC);
 40:             $_SESSION['cart'] = unserialize($row['value']);
 41:         } else {
 42:             $this->saveCart();
 43:         }
 44:     }
 45: 
 46:     /**
 47:      * Saves cart if logged in & ends transaction, i.e. releases row lock.
 48:      * @global \PDO $db
 49:      * @return nothing
 50:      */
 51:     private function saveCart() {
 52:         if (!isset($_SESSION['OpenID']) || empty($_SESSION['OpenID']))
 53:             return;
 54: 
 55:         global $db;
 56:         if (false)
 57:             $db = new \PDO();
 58: 
 59:         $stm_save_cart = $db->prepare('UPDATE webuser_data SET value=:value WHERE  identity=:identity AND type_id=:type_cart');
 60:         $stm_save_cart->bindValue('value', serialize($_SESSION['cart']));
 61:         $stm_save_cart->bindValue('type_cart', WEBUSER_CART);
 62:         $stm_save_cart->bindValue('identity', $_SESSION['OpenID']);
 63:         $stm_save_cart->execute();
 64:         if ($stm_save_cart->rowCount() == 1)
 65:             return;
 66: 
 67:         $stm_insert_cart = $db->prepare('INSERT INTO webuser_data (identity, type_id, value) VALUES (:identity, :type_cart, :value)');
 68:         $stm_insert_cart->bindValue('value', serialize($_SESSION['cart']));
 69:         $stm_insert_cart->bindValue('type_cart', WEBUSER_CART);
 70:         $stm_insert_cart->bindValue('identity', $_SESSION['OpenID']);
 71:         $stm_insert_cart->execute();
 72:     }
 73: 
 74:     public function execute($querydata) {
 75:         global $db;
 76:         $db->beginTransaction();
 77:         
 78:         if (!isset($_SESSION))
 79:             session_start();
 80: 
 81:         $this->loadCart();
 82: 
 83:         $this->syncActions($querydata['action'], $querydata['currentContext']);
 84: 
 85:         //if we are logged in: save our cart back to the DB
 86:         $this->saveCart();
 87:         $db->commit();
 88:         return array('currentRequest' => isset($querydata['currentRequest']) ? $querydata['currentRequest'] : -1, 'cart' => $_SESSION['cart']);
 89:     }
 90: 
 91:     /**
 92:      * replicates client-side action in the session stored cart.
 93:      * @param type $parms contains action to be executed & parameters
 94:      * @param type $currentContext current context (release-organism-combination)
 95:      */
 96:     public function syncActions($parms, $currentContext) {
 97:         //prepare empty values
 98:         if (!isset($_SESSION['cart'])) {
 99:             $_SESSION['cart'] = array('cartitems' => array(), 'carts' => array());
100:         }
101:         if (!isset($_SESSION['cart']['carts'][$currentContext]))
102:             $_SESSION['cart']['carts'][$currentContext] = array('all' => array());
103: 
104:         //refs for quicker access
105:         $cartitems = &$_SESSION['cart']['cartitems'];
106:         $currentCart = &$_SESSION['cart']['carts'][$currentContext];
107: 
108:         //manipulation
109:         switch ($parms['action']) {
110:             case 'addItem':
111:                 foreach ($parms['ids'] as $key=>$id)
112:                     $parms['ids'][$key] = intval($id);
113: 
114:                 $missingIds = array_diff( $parms['ids'], array_keys($cartitems));
115:                 // add item to $cartitems
116:                 if (count($missingIds) > 0) {
117:                     list($service) = \WebService::factory('details/features');
118:                     $items = $service->execute(array('terms' => $missingIds));
119:                     foreach ($items['results'] as $item) {
120:                         $cartitems[intval($item['feature_id'])] = array_merge($item, array('metadata' => array()));
121:                     }
122:                 }
123:                 // add item to $currentCart
124:                 foreach ($parms['ids'] as $id) {
125:                     if (!in_array($id, $currentCart[$parms['groupname']]))
126:                             $currentCart[$parms['groupname']][] = $id;
127:                 }
128: 
129:                 break;
130:             case 'updateItem':
131:                 //enforce id to be int. might get interpreted as string otherwise, which will lead json_encode to enclose it in ""...
132:                 $parms['id'] = intval($parms['id']);
133:                 //update metadata
134:                 $cartitems[$parms['id']]['metadata'] = $parms['metadata'];
135:                 break;
136:             case 'removeItem':
137:                 if ($parms['groupname'] == 'all') {
138:                     //enforce id to be int. might get interpreted as string otherwise, which will lead json_encode to enclose it in ""...
139:                     $parms['id'] = intval($parms['id']);
140:                     //remove from all groups
141:                     foreach ($currentCart as &$group){
142:                         $pos = array_search($parms['id'], $group);
143:                         if ($pos !== FALSE)
144:                             array_splice($group, $pos, 1);
145:                     }
146:                     //remove from $cartitems
147:                     unset($cartitems[$parms['id']]);
148:                 } else {
149:                     //remove from group
150:                     $pos = array_search($parms['id'], $currentCart[$parms['groupname']]);
151:                     if ($pos !== FALSE)
152:                         array_splice($currentCart[$parms['groupname']], $pos, 1);
153:                 }
154:                 break;
155:             case 'addGroup':
156:                 $currentCart[$parms['groupname']] = array();
157:                 break;
158:             case 'renameGroup':
159:                 $currentCart[$parms['newname']] = $currentCart[$parms['groupname']];
160:                 unset($currentCart[$parms['groupname']]);
161:                 break;
162:             case 'removeGroup':
163:                 unset($currentCart[$parms['groupname']]);
164:                 break;
165:             case 'clear':
166:                 foreach ($currentCart['all'] as $id)
167:                     unset($cartitems[$id]);
168:                 $_SESSION['cart']['carts'][$currentContext] = array('all' => array());
169:                 break;
170:         }
171:     }
172: 
173: }
174: 
175: ?>
176: 
tbro API documentation generated by ApiGen 2.8.0