예제 #1
0
 public function actionChangeLocation()
 {
     $user = Yii::app()->user;
     if ($user->isGuest) {
         $BoxoCart = new BoxoCart();
         $BoxoCart->setLocation_id(null);
         $this->redirect(array('/shop/index'));
     } else {
         $this->redirect(array('/user/update', 'id' => $user->id));
     }
 }
예제 #2
0
 /**
  * Updates a particular model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id the ID of the model to be updated
  */
 public function actionUpdate($id)
 {
     $Customer = $this->loadModel($id);
     if ($Customer->id !== Yii::app()->user->id) {
         throw new CHttpException(403, 'You are not authorized to perform this action.');
     }
     if (isset($_POST['role']) && $_POST['role'] == 'customer') {
         $UserLoc = new UserLocation();
         $UserLoc->user_id = $Customer->user_id;
         $UserLoc->location_id = $Customer->location_id;
         $UserLoc->address = $Customer->user_address;
         $UserLoc->address2 = $Customer->user_address2;
         $UserLoc->suburb = $Customer->user_suburb;
         $UserLoc->state = $Customer->user_state;
         $UserLoc->postcode = $Customer->user_postcode;
         $UserLoc->phone = !empty($Customer->user_phone) ? $Customer->user_phone : $Customer->user_mobile;
         $UserLoc->save();
         $Customer->user_id = $Customer->user_id;
         $Customer->update(array('user_id'));
     }
     $allSaved = true;
     if (isset($_POST['Supplier'])) {
         $Supplier = $Customer->Supplier;
         $Supplier->attributes = $_POST['Supplier'];
         if (!$Supplier->update()) {
             $allSaved = false;
         }
     }
     if (isset($_POST['role'])) {
         $Customer->setRole($_POST['role']);
     }
     if (isset($_POST['BoxomaticUser'])) {
         $oldLocation = $Customer->location_id;
         $oldDeliveryDay = $Customer->delivery_day;
         $Customer->attributes = $_POST['BoxomaticUser'];
         $locationId = $_POST['BoxomaticUser']['delivery_location_key'];
         $custLocationId = new CDbExpression('NULL');
         if (strpos($locationId, '-')) {
             //has a customer location
             $parts = explode('-', $locationId);
             $locationId = $parts[1];
             $custLocationId = $parts[0];
         }
         $Customer->location_id = $locationId;
         $Customer->user_location_id = $custLocationId;
         $Customer->validate();
         if (!$Customer->update()) {
             $allSaved = false;
         }
         //Update the cart to prevent ordering on an unavailable day
         $BoxoCart = new BoxoCart();
         $BoxoCart->delivery_day = $Customer->delivery_day;
         $BoxoCart->setLocation_id($Customer->location_id);
         $BoxoCart->setDelivery_date_id($BoxoCart->getNextDeliveryDate()->id);
         //The frontend system currently doesn't handle ordering from multiple locations
         //so delete all orders if changing location
         if ($Customer->location_id != $oldLocation || $Customer->delivery_day != $oldDeliveryDay) {
             $deleted = false;
             foreach ($Customer->getFutureOrders() as $Order) {
                 $Order->delete();
                 $deleted = true;
             }
             $BoxoCart->emptyCart();
             if ($deleted) {
                 Yii::app()->user->setFlash('warning', 'All future orders removed.');
             }
         }
         if ($allSaved) {
             $this->redirect(array('user/update', 'id' => $Customer->id));
         }
     }
     $custLocDataProvider = null;
     $custLocDataProvider = new CActiveDataProvider('UserLocation', array('criteria' => array('condition' => 'user_id=' . $Customer->id)));
     $this->render('update', array('model' => $Customer, 'custLocDataProvider' => $custLocDataProvider));
 }