コード例 #1
0
 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";
     }
 }