Created
November 26, 2015 17:10
-
-
Save antoniovassell/9e24aa16ceb5b74f4977 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Controller; | |
use App\Controller\AppController; | |
use Cake\ORM\TableRegistry; | |
/** | |
* TransactionItems Controller | |
* | |
* @property \App\Model\Table\TransactionItemsTable $TransactionItems | |
*/ | |
class TransactionItemsController extends AppController | |
{ | |
/** | |
* Index method | |
* | |
*/ | |
public function index() | |
{ | |
$userId = $this->Auth->user('id'); | |
$this->paginate = [ | |
'contain' => ['TransactionTypes', 'TransactionFrequencies', 'Accounts'], | |
'conditions' => [ | |
'TransactionItems.user_id' => $userId | |
] | |
]; | |
$this->set('transactionItems', $this->paginate($this->TransactionItems)); | |
$this->set('_serialize', ['transactionItems']); | |
} | |
/** | |
* View method | |
* | |
* @param string|null $id Transaction Item id. | |
* @return void | |
* @throws \Cake\Network\Exception\NotFoundException When record not found. | |
*/ | |
public function view($id = null) | |
{ | |
$transactionItem = $this->TransactionItems->get($id, [ | |
'contain' => [ | |
'TransactionTypes', | |
'Accounts', | |
'Transactions' => function ($q) { | |
return $q | |
->order(['Transactions.priority', 'Transactions.date']); | |
} | |
] | |
]); | |
$this->set('transactionItem', $transactionItem); | |
$this->set('_serialize', ['transactionItem']); | |
} | |
/** | |
* Add method | |
* | |
* @return void Redirects on successful add, renders view otherwise. | |
*/ | |
public function add() | |
{ | |
$transactionItem = $this->TransactionItems->newEntity(); | |
if ($this->request->is('post')) { | |
$data = $this->request->data; | |
$data['user_id'] = $this->Auth->user('id'); | |
$transactionItem = $this->TransactionItems->patchEntity($transactionItem, $data); | |
if ($this->TransactionItems->add($transactionItem)) { | |
if (isset($data['default_transaction_item_id'])) { | |
if (TableRegistry::get('DefaultTransactionItems') | |
->markAsRemove($data['default_transaction_item_id'], $data['user_id'])) { | |
$this->set('transaction_item_id', $transactionItem->id); | |
} | |
} | |
} | |
$errors = $transactionItem->errors(); | |
} | |
$this->set( | |
compact( | |
'transactionItem', | |
'errors' | |
) | |
); | |
$this->set('_serialize', ['transactionItem', 'errors', 'transaction_item_id']); | |
} | |
/** | |
* Edit method | |
* | |
* @param string|null $id Transaction Item id. | |
* @return void Redirects on successful edit, renders view otherwise. | |
* @throws \Cake\Network\Exception\NotFoundException When record not found. | |
*/ | |
public function edit($id = null) | |
{ | |
$transactionItem = $this->TransactionItems->get($id); | |
if ($this->request->is(['put', 'patch', 'post'])) { | |
$data = $this->request->data; | |
$data['user_id'] = $this->Auth->user('id'); | |
$data['date'] = date('Y-m-d', strtotime($data['date'])); | |
$data['end_date'] = date('Y-m-d', strtotime($data['end_date'])); | |
$transactionItem = $this->TransactionItems->patchEntity($transactionItem, $data); | |
if ($this->TransactionItems->edit($transactionItem)) { | |
} | |
$errors = $transactionItem->errors(); | |
} | |
$this->set( | |
compact( | |
'transactionItem', | |
'errors' | |
) | |
); | |
$this->set('_serialize', ['transactionItem', 'errors']); | |
} | |
/** | |
* Delete method | |
* | |
* @param string|null $id Transaction Item id. | |
* @return void Redirects to index. | |
* @throws \Cake\Network\Exception\NotFoundException When record not found. | |
*/ | |
public function delete($id = null) | |
{ | |
$this->request->allowMethod(['post', 'delete']); | |
$transactionItem = $this->TransactionItems->get($id); | |
if ($this->TransactionItems->delete($transactionItem)) { | |
$this->set([ | |
'success' => true, | |
'_serialize' => ['success'] | |
]); | |
} else { | |
$this->set([ | |
'success' => false, | |
'_serialize' => ['success'] | |
]); | |
} | |
} | |
public function summarized() | |
{ | |
$userId = $this->Auth->user('id'); | |
$result = $this->TransactionItems->getItemsPerMonth($userId); | |
$this->set([ | |
'transactionItems' => $result, | |
'_serialize' => ['transactionItems'] | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment