Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by PhpStorm.
- * User: Stixlink
- * Date: 30.07.14
- * Time: 15:30
- */
- class AccountsController extends Controller {
- public $defaultAction = 'index';
- public function accessRules() {
- return array(
- array('allow',
- 'actions' => array('logout', 'error'),
- 'users' => array('*'),
- ),
- array('allow',
- 'actions' => array('login'),
- 'users' => array('?'),
- ),
- array('deny',
- 'users' => array('*'),
- ),
- );
- }
- public function actionCreateAccount() {
- $account = new Account();
- $account->setScenario('create');
- if (!empty($_POST)) {
- $account->setAttributes($_POST);
- if ($account->save()) {
- $this->redirect(array('accounts', 'serial' => $account->serial));
- }
- }
- $this->render('create', array('account' => $account));
- }
- public function actionTransfer() {
- $accountFrom = $accountTo = null;
- $transaction = new Transaction();
- $transaction->scenario = "transfer";
- if (Yii::app()->request->isPostRequest) {
- $transaction->attributes = $_POST;
- if ($transaction->validate(array('from', 'to', 'amount'))) {
- if (isset($_POST['from']) && !empty($_POST['from'])) {
- $accountFrom = Account::model()->findBySerial((int)$_POST['from']);
- if ($accountFrom === null) {
- $transaction->addError('from', 'Счет ' . (int)$_POST['from'] . ' не найден');
- }
- }
- if (isset($_POST['to']) && !empty($_POST['to']) | strval($_POST['to']) === strval(Yii::app()->params['serialSystemAccount'])) {
- $accountTo = Account::model()->findBySerial((int)$_POST['to']);
- if ($accountTo === null) {
- $transaction->addError('to', 'Счет ' . (int)$_POST['to'] . ' не найден');
- }
- }
- $amount = isset($_POST['amount']) && !empty($_POST['amount']) ? (float)$_POST['amount'] : null;
- if ($accountFrom !== null && $accountTo !== null && $amount !== null) {
- $resultTransactions = Transaction::executeTransaction($accountFrom, $accountTo, $amount);
- if ($resultTransactions) {
- Yii::app()->user->setFlash('transfer', 'Операция успешно выполнена!');
- $this->redirect('transfer');
- }
- }
- if (!empty($accountFrom->errors)) {
- $transaction->addErrors($accountFrom->errors);
- }
- if (!empty($accountTo->errors)) {
- $transaction->addErrors($accountTo->errors);
- }
- }
- }
- $this->render('transfer', array('accountTo' => $accountTo,
- 'accountFrom' => $accountFrom,
- 'transaction' => $transaction));
- }
- public function actionOperations($serial) {
- $transaction = new Transaction();
- $transaction->unsetAttributes();
- $transaction->setScenario('search');
- if (isset($_GET['Transaction'])) {
- $transaction->attributes = $_GET['Transaction'];
- }
- $this->render('operations', array(
- 'transaction' => $transaction,
- 'serial' => $serial
- ));
- }
- public function actionAccounts() {
- $account = new Account();
- $account->unsetAttributes();
- $account->setScenario('search');
- if (isset($_GET['Account'])) {
- $account->attributes = $_GET['Account'];
- }
- $this->render('index', array(
- 'account' => $account,
- ));
- }
- public function loadAccount($id) {
- $account = new Account();
- $account->findBySerial($id);
- if ($account === null) {
- throw new CHttpException(404, 'Счет не найден.');
- }
- return $account;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement