/** * @param array $opts Array of options * @param object $user \User * @param string $action (Optional argument) * @return boolean true (success), false (failure) */ public static function save($opts, $user, $action = 'add') { $cart = \Cart::first(array("service_id = ?" => $opts['service_id'], "centre_id = ?" => $opts['centre_id'], "user_id = ?" => $user->id)); switch ($action) { case 'add': if (!$cart) { $cart = \Cart::saveRecord($user, $opts); return !is_array($cart); } return false; case 'remove': if ($cart) { $cart->delete(); return true; } return false; } }
/** * @before _secure */ public function checkout() { $this->seo(array("title" => "Cart", "view" => $this->getLayoutView())); $view = $this->getActionView(); $session = Registry::get("session"); $items = Cart::all(array("user_id = ?" => $this->user->id), array("service_id", "centre_id", "id")); $stored = $session->get('User\\Cart:$cart', array()); if (count($items) == 0 && count($stored) == 0) { $this->redirect("/profile"); } if (count($items) != count($stored)) { foreach ($stored as $s) { CartService::save($s, $this->user); } $items = Cart::all(array("user_id = ?" => $this->user->id), array("service_id", "centre_id", "id")); } $family = Shared\Services\Patient::findFamily($this->user); $locations = Location::all(array("user_id = ?" => $this->user->id), array("user_id", "id", "street", "area_id", "city_id")); $flocations = Shared\Services\Patient::familyLoc($family); $locations = array_merge($locations, $flocations); $view->set("items", $items); $view->set("family", $family); $view->set("locations", $locations); }