Example #1
0
 public function actionApply()
 {
     $data = array();
     if (Rays::isPost()) {
         $rules = array(array('field' => 'ads-title', 'label' => 'Ads title', 'rules' => 'trim|required|min_length[5]|max_length[255]'), array('field' => 'ads-content', 'label' => 'Ads content', 'rules' => 'required'), array('field' => 'paid-price', 'label' => 'Paid price', 'rules' => 'trim|required|number'));
         $validation = new RValidation($rules);
         if ($validation->run()) {
             //Money cannot exceed wallet remaining
             $money = Rays::user()->getWallet()->money;
             if ($money < (int) $_POST['paid-price']) {
                 $this->flash('error', 'You cannot overdraft your ' . Wallet::COIN_NAME . ' to publish advertisements.');
                 $data['applyForm'] = $_POST;
             } else {
                 Rays::user()->getWallet()->cutMoney((int) $_POST['paid-price']);
                 //Pay the price
                 $ads = new Ads();
                 $result = $ads->apply(Rays::user()->id, $_POST['ads-title'], RHtml::encode($_POST['ads-content']), $_POST['paid-price']);
                 if ($result == true) {
                     $this->flash('message', 'Your ads was applied successfully.');
                 } else {
                     $data['applyForm'] = $_POST;
                     $this->flash('message', 'Apply failed.');
                 }
             }
         } else {
             $data['applyForm'] = $_POST;
             $data['validation_errors'] = $validation->getErrors();
         }
     }
     $this->setHeaderTitle("Ads application");
     $this->render('apply', $data, false);
 }