예제 #1
0
 /**
  * Default action is product display. This action requires the product id to be passed.
  * If we do not receive this id, redirect back to the home page since something
  * went wrong.
  */
 public function actionView()
 {
     $id = Yii::app()->getRequest()->getQuery('id');
     if (empty($id)) {
         _xls_404();
     }
     //Load a product and display the information
     $model = $this->objProduct = Product::model()->findByPk($id);
     if (!$model || !$model->IsDisplayable) {
         _xls_404();
     }
     //If our request_url (based on description) has changed, redirect properly
     if ($model->request_url != Yii::app()->getRequest()->getQuery('name')) {
         _xls_301($model->Link);
     }
     //Set breadcrumbs
     $this->breadcrumbs = $model->Breadcrumbs;
     $this->pageImageUrl = $model->SmallImageAbsolute;
     $objWishlistAddForm = new WishlistAddForm();
     $objWishlistAddForm->id = $this->objProduct->id;
     $objWishlistAddForm->qty = 1;
     $objWishlistAddForm->lists = $objWishlistAddForm->getLists();
     $objWishlistAddForm->gift_code = Wishlist::LoadFirstCode();
     $this->setPageTitle($model->PageTitle);
     $this->pageDescription = $model->PageDescription;
     $this->canonicalUrl = $model->canonicalUrl;
     $this->returnUrl = $model->absoluteLink;
     $model->intQty = 1;
     //Raise any events first
     $objEvent = new CEventProduct(get_class($this), 'onActionProductView', $model);
     _xls_raise_events('CEventProduct', $objEvent);
     $this->widget('ext.wscartanimate.wscartanimate');
     $this->widget('ext.wsproduct.wsproduct');
     $this->render('index', array('model' => $model, 'WishlistAddForm' => $objWishlistAddForm));
 }
 public function actionAdd()
 {
     if (Yii::app()->user->isGuest) {
         echo Yii::t('wishlist', "You must be logged in to add to a wish list. Please sign in and try again.");
         return;
     }
     if (Yii::app()->request->isAjaxRequest) {
         $model = new WishlistAddForm();
         // collect user input data
         if (isset($_POST)) {
             if (isset($_POST['WishlistAddForm'])) {
                 $model->attributes = $_POST['WishlistAddForm'];
             } else {
                 $model->attributes = $_POST;
             }
             if ($model->validate()) {
                 $intProductId = $model->id;
                 $intQty = $model->qty;
                 $strSize = $model->size;
                 $strColor = $model->color;
                 $objProduct = Product::model()->findByPk($intProductId);
                 if (!$objProduct instanceof Product) {
                     return;
                 }
                 if ($objProduct->IsMaster) {
                     if (isset($strSize) || isset($strColor)) {
                         $objProduct = Product::LoadChildProduct($intProductId, $strSize, $strColor);
                         if (!$objProduct instanceof Product) {
                             echo Yii::t('wishlist', "Please choose options before selecting {button}", array('{button}' => Yii::t('product', 'Add to Wish List')));
                             return;
                         }
                         $model->id = $intProductId = $objProduct->id;
                     } else {
                         echo Yii::t('wishlist', "Please choose options before selecting {button}", array('{button}' => Yii::t('product', 'Add to Wish List')));
                         return;
                     }
                 }
                 //If we don't have a wish list, create one and add. If we only have one wish list, add it. If we have more than
                 //one, we need to prompt for which one to add
                 $objWishLists = Wishlist::LoadUserLists();
                 switch (count($objWishLists)) {
                     case 0:
                         //We don't have a wish list, so let's create one
                         $objWishList = new Wishlist();
                         $objWishList->registry_name = Yii::t('wishlist', 'My Wish List');
                         $objWishList->visibility = Wishlist::PERSONALLIST;
                         $objWishList->after_purchase = Wishlist::LEAVEINLIST;
                         $objWishList->customer_id = Yii::app()->user->id;
                         $objWishList->gift_code = md5(uniqid());
                         $objWishList->ship_option = 0;
                         if (!$objWishList->save()) {
                             Yii::log("Error creating Wish List " . print_r($objWishList->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__);
                         }
                         $objWishLists = array();
                         $objWishLists[0] = $objWishList;
                         // No break so we drop to the next set of instructions
                     // No break so we drop to the next set of instructions
                     case 1:
                         $objWishList = $objWishLists[0];
                         $objWishItem = new WishlistItem();
                         $objWishItem->registry_id = $objWishList->id;
                         $objWishItem->product_id = $intProductId;
                         $objWishItem->qty = $intQty;
                         if ($objWishItem->save()) {
                             echo Yii::t('wishlist', 'Item has been added to your Wish List.');
                         } else {
                             Yii::log("error saving wishlist item " . print_r($objWishItem->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__);
                             echo Yii::t('wishlist', 'An error occurred adding this item to your Wish List.');
                         }
                         break;
                     default:
                         $Wishlistadd = new WishlistAddForm();
                         if (isset($_POST['WishlistAddForm'])) {
                             $Wishlistadd->attributes = $_POST['WishlistAddForm'];
                         }
                         if ($Wishlistadd->validate()) {
                             $objWishList = Wishlist::model()->findByAttributes(array('gift_code' => $Wishlistadd->gift_code));
                             $objWishItem = new WishlistItem();
                             $objWishItem->registry_id = $objWishList->id;
                             $objWishItem->product_id = $intProductId;
                             $objWishItem->qty = $Wishlistadd->qty;
                             if ($objWishItem->save()) {
                                 echo Yii::t('global', 'Item has been added to your Wish List.');
                             } else {
                                 Yii::log("error saving wishlist item " . print_r($objWishItem->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__);
                                 echo Yii::t('global', 'An error occurred adding this item to your Wish List.');
                             }
                         } else {
                             echo "multiple";
                         }
                         break;
                 }
             } else {
                 echo print_r($model->getErrors(), true);
             }
         } else {
             echo "Error missing POST";
         }
     } else {
         echo "Error not AJAX";
     }
 }