Advertisement
stixlink

controller yii

Sep 1st, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.31 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Created by PhpStorm.
  5.  * User: Stixlink
  6.  * Date: 30.07.14
  7.  * Time: 15:30
  8.  */
  9. class AccountsController extends Controller {
  10.  
  11.     public $defaultAction = 'index';
  12.  
  13.     public function accessRules() {
  14.  
  15.         return array(
  16.             array('allow',
  17.                   'actions' => array('logout', 'error'),
  18.                   'users' => array('*'),
  19.             ),
  20.             array('allow',
  21.                   'actions' => array('login'),
  22.                   'users' => array('?'),
  23.             ),
  24.  
  25.             array('deny',
  26.                   'users' => array('*'),
  27.             ),
  28.         );
  29.     }
  30.  
  31.     public function actionCreateAccount() {
  32.  
  33.         $account = new Account();
  34.         $account->setScenario('create');
  35.  
  36.         if (!empty($_POST)) {
  37.             $account->setAttributes($_POST);
  38.             if ($account->save()) {
  39.                 $this->redirect(array('accounts', 'serial' => $account->serial));
  40.             }
  41.         }
  42.         $this->render('create', array('account' => $account));
  43.  
  44.     }
  45.  
  46.  
  47.     public function actionTransfer() {
  48.  
  49.         $accountFrom = $accountTo = null;
  50.  
  51.         $transaction = new Transaction();
  52.         $transaction->scenario = "transfer";
  53.         if (Yii::app()->request->isPostRequest) {
  54.             $transaction->attributes = $_POST;
  55.             if ($transaction->validate(array('from', 'to', 'amount'))) {
  56.  
  57.  
  58.                 if (isset($_POST['from']) && !empty($_POST['from'])) {
  59.                     $accountFrom = Account::model()->findBySerial((int)$_POST['from']);
  60.  
  61.                     if ($accountFrom === null) {
  62.                         $transaction->addError('from', 'Счет ' . (int)$_POST['from'] . ' не найден');
  63.                     }
  64.  
  65.                 }
  66.  
  67.                 if (isset($_POST['to']) && !empty($_POST['to']) | strval($_POST['to']) === strval(Yii::app()->params['serialSystemAccount'])) {
  68.                     $accountTo = Account::model()->findBySerial((int)$_POST['to']);
  69.  
  70.                     if ($accountTo === null) {
  71.                         $transaction->addError('to', 'Счет ' . (int)$_POST['to'] . ' не найден');
  72.                     }
  73.                 }
  74.  
  75.                 $amount = isset($_POST['amount']) && !empty($_POST['amount']) ? (float)$_POST['amount'] : null;
  76.  
  77.                 if ($accountFrom !== null && $accountTo !== null && $amount !== null) {
  78.  
  79.                     $resultTransactions = Transaction::executeTransaction($accountFrom, $accountTo, $amount);
  80.                     if ($resultTransactions) {
  81.  
  82.                         Yii::app()->user->setFlash('transfer', 'Операция успешно выполнена!');
  83.                         $this->redirect('transfer');
  84.                     }
  85.                 }
  86.                 if (!empty($accountFrom->errors)) {
  87.                     $transaction->addErrors($accountFrom->errors);
  88.                 }
  89.                 if (!empty($accountTo->errors)) {
  90.                     $transaction->addErrors($accountTo->errors);
  91.                 }
  92.             }
  93.         }
  94.         $this->render('transfer', array('accountTo' => $accountTo,
  95.                                         'accountFrom' => $accountFrom,
  96.                                         'transaction' => $transaction));
  97.  
  98.     }
  99.  
  100.     public function actionOperations($serial) {
  101.  
  102.         $transaction = new Transaction();
  103.         $transaction->unsetAttributes();
  104.         $transaction->setScenario('search');
  105.  
  106.  
  107.         if (isset($_GET['Transaction'])) {
  108.             $transaction->attributes = $_GET['Transaction'];
  109.         }
  110.  
  111.         $this->render('operations', array(
  112.             'transaction' => $transaction,
  113.             'serial' => $serial
  114.         ));
  115.     }
  116.  
  117.     public function actionAccounts() {
  118.  
  119.         $account = new Account();
  120.         $account->unsetAttributes();
  121.         $account->setScenario('search');
  122.  
  123.         if (isset($_GET['Account'])) {
  124.             $account->attributes = $_GET['Account'];
  125.         }
  126.  
  127.         $this->render('index', array(
  128.             'account' => $account,
  129.         ));
  130.     }
  131.  
  132.     public function loadAccount($id) {
  133.  
  134.         $account = new Account();
  135.         $account->findBySerial($id);
  136.  
  137.         if ($account === null) {
  138.             throw new CHttpException(404, 'Счет не найден.');
  139.         }
  140.  
  141.         return $account;
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement