/**
  * Authenticates the all inputs and there relations with each other
  * returning true false does not stops proceeding to action. to stop add error to attribute.
  */
 public function customInputValidator($attribute, $params)
 {
     //check if address has been passed
     if (isset($this->address) && !AppCommon::isEmpty($this->address)) {
         //if passed check if address passed is allowed for the given user
         //array( addrId => array( locality, addrText ) )
         $allowedAddresses = AAddress::model()->getAllowedAddressForUser(AppCommon::getUserDetailsId());
         if (!isset($allowedAddresses[$this->address])) {
             $this->addError("address", CHtml::encode("Kindly select a valid address."));
             return;
         }
         //continue to check more errors only if address selected is valid
         //as cart depends on a valid address
         //set customerLocality a valid value taken from selected address this will override
         //upon the value passed by user and thus will be more reliable value
         $this->customerLocality = $allowedAddresses[$this->address][0];
         $this->getAddressData[$this->address] = $allowedAddresses[$this->address];
         $this->getCartArray = ACart::getCart($this->customerLocality, 0);
         //check if cart has rows > 0 for the selected locality
         if (isset($this->getCartArray) && isset($this->getCartArray["total_items_at_locality"]) && $this->getCartArray["total_items_at_locality"] <= 0) {
             $this->addError("extraErrorMessages", CHtml::encode("Error: The cart contains no tiffins which are available at the selected address's locality. Kindly, either add tiffins available at selected address's locality to the cart or select addresses with different locality."));
         }
         //check if for selected locality cart has not changed
         if (AppCommon::hasCartChanged($this->getCartArray)) {
             $this->addError("extraErrorMessages", CHtml::encode("Notification: The cart has changed. Kindly retry."));
         }
     }
 }
 /**
  * gets cart values
  * $location to select tiffins at a particular location
  * $doeditcart edit cart if needed.
  * return
  */
 public static function getCart($location = null, $doeditcart = 1)
 {
     /*Yii::ankFileSave( var_export( AppCommon::modifyInputTiffinsQuantity( 
     		array(
     		1=>2,
     		9=>1,
     		10=>20,
     		11=>0,
     		16=>1,
     		)
     		, "Embassy Tech Village" ) ,true));
     		echo "wahh wahh";
     		return;*/
     $cartData = null;
     if (Yii::app()->user->isGuest) {
         $cartCookiesArr = array();
         $allCookies = Yii::app()->request->getCookies();
         if (isset($allCookies[Yii::app()->params['cartCookieName']])) {
             $cartCookiesArr = CJSON::decode($allCookies[Yii::app()->params['cartCookieName']]->value, true);
         }
         $cartArr = array();
         if (isset($cartCookiesArr)) {
             foreach ($cartCookiesArr as $key => $value) {
                 $cartArr[$key] = $value[0];
                 //array(tiffinid=>quantity,...)
             }
         }
         $cartData = AppCommon::modifyInputTiffinsQuantity($cartArr, $location);
         if ($cartData["has_changed"]) {
             if (isset($cartCookiesArr)) {
                 foreach ($cartCookiesArr as $key1 => $value1) {
                     if (isset($cartData[$key1]) and $cartData[$key1]["has_changed"]) {
                         if ($cartData[$key1]["error_msg"] == AppCommon::$cartErrorMsgTiffinNotExist) {
                             unset($cartCookiesArr[$key1]);
                         } else {
                             if ($cartData[$key1]["error_msg"] == AppCommon::$cartErrorMsgTiffinNotAvailable and $doeditcart == 1) {
                                 unset($cartCookiesArr[$key1]);
                             } else {
                                 if ($cartData[$key1]["error_msg"] == AppCommon::$cartErrorMsgTiffinQuanCappedToLimits and $doeditcart == 1) {
                                     $cartCookiesArr[$key1][0] = $cartData[$key1]["quantity"];
                                 }
                             }
                         }
                     }
                 }
             }
             //update the cookie
             //TODO:: check CJSON::encode( $cartCookiesArr ) is not null
             $allCookies[Yii::app()->params['cartCookieName']] = new CHttpCookie(Yii::app()->params['cartCookieName'], CJSON::encode($cartCookiesArr), array('expire' => time() + 60 * 60 * 24 * 100, 'httpOnly' => true));
         }
     } else {
         $alreadyPresentRecord = ACart::model()->findAll(array('condition' => 'is_deleted = "no" AND cart2user = ' . AppCommon::getUserDetailsId()));
         $cartArr = array();
         foreach ($alreadyPresentRecord as $rec) {
             $cartArr[$rec->cart2tiffin] = $rec->quantity;
             //array(tiffinid=>quantity,...)
         }
         $cartData = AppCommon::modifyInputTiffinsQuantity($cartArr, $location);
         try {
             if ($cartData["has_changed"]) {
                 foreach ($alreadyPresentRecord as $rec1) {
                     if (isset($cartData[$rec1->cart2tiffin]) and $cartData[$rec1->cart2tiffin]["has_changed"]) {
                         if ($cartData[$rec1->cart2tiffin]["error_msg"] == AppCommon::$cartErrorMsgTiffinNotExist) {
                             $rec1->is_deleted = AppCommon::getUserDetailsId();
                             $rec1->save();
                             //update the record
                         } else {
                             if ($cartData[$rec1->cart2tiffin]["error_msg"] == AppCommon::$cartErrorMsgTiffinNotAvailable and $doeditcart == 1) {
                                 $rec1->is_deleted = AppCommon::getUserDetailsId();
                                 $rec1->save();
                                 //update the record
                             } else {
                                 if ($cartData[$rec1->cart2tiffin]["error_msg"] == AppCommon::$cartErrorMsgTiffinQuanCappedToLimits and $doeditcart == 1) {
                                     $rec1->quantity = $cartData[$rec1->cart2tiffin]["quantity"];
                                     $rec1->save();
                                     //update the record
                                 }
                             }
                         }
                     }
                 }
             }
         } catch (Exception $e) {
             Yii::ankFileSave(" get cart exception while saving cart in db");
             //added to ignore exception
         }
     }
     $cartData["total_items"] = AppCommon::cartItemCount();
     return $cartData;
 }
 /**
  * Returns the data model based on the primary key given in the GET variable.
  * If the data model is not found, an HTTP exception will be raised.
  * @param integer $id the ID of the model to be loaded
  * @return ACart the loaded model
  * @throws CHttpException
  */
 public function loadModel($id)
 {
     $model = ACart::model()->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     return $model;
 }
 /**
  * merges cookies and db cart if logged in.
  */
 public static function mergeCookieAndDbCart()
 {
     $allCookies = Yii::app()->request->getCookies();
     if (!Yii::app()->user->isGuest) {
         if (isset($allCookies[Yii::app()->params['cartCookieName']])) {
             $cartCookiesArr = array();
             $cartCookiesArr = CJSON::decode($allCookies[Yii::app()->params['cartCookieName']]->value, true);
             $alreadyPresentRecord = ACart::model()->findAll(array('condition' => 'is_deleted = "no" AND cart2user = '******'cartCookieName']]);
         }
     }
 }