/**
  * 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 only those addresses for a user for which both address2bangaloreLocalities and
  * address record are not deleted.
  *
  * Returns array( addrId => array( locality, addrText ) )
  */
 public static function getAllowedAddressForUser($userId = NULL)
 {
     $response = array();
     if (isset($userId)) {
         $selectedAddresses = AAddress::model()->findAll(array('with' => array('address2bangaloreLocalities' => array('on' => 'address2bangaloreLocalities.is_deleted = "no" AND 
     				t.is_deleted = "no" AND t.link_table = "user_details"  AND t.link = ' . $userId . ' '))));
         foreach ($selectedAddresses as $row) {
             $response[$row->id][] = $row->address2bangaloreLocalities->locality_name;
             $response[$row->id][] = $row->getAddressString();
         }
     }
     return $response;
 }
 /**
  * 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 AAddress the loaded model
  * @throws CHttpException
  */
 public function loadModel($id)
 {
     $model = AAddress::model()->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     return $model;
 }
 public static function getBaseAddressDataForUser($userDetailsId)
 {
     $response = array();
     if (isset($userDetailsId)) {
         $selectedAddresses = AAddress::model()->findAll(array('order' => 't.id DESC', 'with' => array('address2bangaloreLocalities' => array('on' => 'address2bangaloreLocalities.is_deleted = "no" AND 
     				t.is_deleted = "no" AND t.link_table = "user_details"  AND 
     				t.meta_data = "base_address" AND t.link = ' . $userDetailsId . ' '))));
         foreach ($selectedAddresses as $row) {
             $response[$row->id][] = $row->address2bangaloreLocalities->locality_name;
             $response[$row->id][] = $row->getAddressString();
             break;
             //get the last updated and exit
         }
     }
     if (AppCommon::isEmpty($response)) {
         $response = false;
     }
     return $response;
 }