Skip to content

Instantly share code, notes, and snippets.

@manhtlu
Created June 15, 2017 03:15
Show Gist options
  • Save manhtlu/6ea4955962066674ab04f24479b4a912 to your computer and use it in GitHub Desktop.
Save manhtlu/6ea4955962066674ab04f24479b4a912 to your computer and use it in GitHub Desktop.
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Event\Event;
use Cake\Routing\Router;
use Cake\ORM\TableRegistry;
use Cake\Filesystem\Folder;
use Cake\Filesystem\File;
use Cake\Log\Log;
use Cake\Auth\DefaultPasswordHasher;
use Cake\Core\Configure;
use Cake\I18n\Time;
use Cake\Network\Request;
use App\Lib\ProjectsLib;
use Cake\Network\Http\Client;
/**
* Requests Controller
*
* @property \App\Model\Table\RequestsTable $Requests
*
* @method \App\Model\Entity\Request[] paginate($object = null, array $settings = [])
*/
class RequestsController extends AppController
{
public $paginate = [
'limit' => 6,
'order' => [
'created' => 'ASC',
'id' => 'ASC'
]
];
public function index()
{
$this->loadModel('Requests');
$userId = $this->Auth->user('id');
$this->loadModel('Users');
$adminType = $this->Users->getAdminType($userId);
if($adminType==1||$adminType==2||$adminType==3||$adminType==4||$adminType==5){
$requests = $this->Requests->getListRequestByAdminSME($adminType);
/*Draw chart*/
//QL
$manage = $this->Requests->getCountRequests(['type'=>1,'status'=>0]);
$manageProcessing = $this->Requests->getCountRequests(['type'=>1,'status'=>1]);
$manageProcessed = $this->Requests->getCountRequests(['type'=>1,'status'=>2]);
//Phapche
$juridical = $this->Requests->getCountRequests(['type'=>2,'status'=>0]);
$juridicalProcessing = $this->Requests->getCountRequests(['type'=>2,'status'=>1]);
$juridicalProcessed = $this->Requests->getCountRequests(['type'=>2,'status'=>2]);
//XTTM
$commerce = $this->Requests->getCountRequests(['type'=>3,'status'=>0]);
$commerceProcessing = $this->Requests->getCountRequests(['type'=>3,'status'=>1]);
$commerceProcessed = $this->Requests->getCountRequests(['type'=>3,'status'=>2]);
//VV
$loans = $this->Requests->getCountRequests(['type'=>4,'status'=>0]);
$loansProcessing = $this->Requests->getCountRequests(['type'=>4,'status'=>1]);
$loansProcessed = $this->Requests->getCountRequests(['type'=>4,'status'=>2]);
$this->set(compact('manage','manageProcessing','manageProcessed','juridical','juridicalProcessing','juridicalProcessed','commerce'
,'commerceProcessing','commerceProcessed','loans','loansProcessing','loansProcessed'));
/*End Draw chart*/
$this->set([
'requests' => $this->paginate($requests),
'_serialize' => ['requests']
]);
// $this->set('_serialize', ['requests']);
}else{
$requests = $this->Requests->getListRequestUser($userId);
$this->set(compact('requests'));
// $this->set('_serialize', ['requests']);
$this->render('list_request');
}
$this->set('title', __('Danh sách yêu cầu/kiến nghị'));
}
/**
* View method
*
* @param string|null $id Request id.
* @return \Cake\Http\Response|null
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function view($id = null)
{
$request = $this->Requests->get($id,['contains'=>['Projects']]);
//$this->loadModel('Requests');
//$project = $this->Requests->getProjectByRequest($id);
//$this->set(compact('request','project'));
$this->set('request', $request);
$this->set('_serialize', ['request']);
$this->set('title', __('Xem yêu cầu/kiến nghị'));
}
public function add()
{
$request = $this->Requests->newEntity();
if ($this->request->is('post')) {
$request = $this->Requests->patchEntity($request, $this->request->data);
$request->project_id = 1;
$request->user_id = $this->Auth->user('id');
// $this->Request->data['project_id'] = 1;
// $this->Request->data['user_id'] = $this->Auth->user('id');
$this->uploadFiles('img/uploads', $this->request->data['attach1']);
if ($this->Requests->save($request)) {
$this->Flash->success(__('Yêu cầu thêm thành công.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('Không thể thêm yêu cầu, vui lòng thử lại !'));
}
$users = $this->Requests->Users->find('list', ['limit' => 200]);
$userID = $this->Auth->user('id');
$ownerProjects = $this->Requests->Projects->find('all')->where(['user_id' => $userID]);
$ProjectsTable = TableRegistry::get('Projects');
$projects = $ProjectsTable->find('list', ['fields' => ['id', 'title'], 'keyField' => 'id', 'valueField' => 'title'])->toArray();
// $scales = $ScalesTable->find('list', ['fields' => ['id', 'name'], 'keyField' => 'id', 'valueField' => 'name'])->toArray();
$this->set(compact('request', 'projects', 'users','ownerProjects'));
$this->set('_serialize', ['request']);
$this->set('title', __('Thêm yêu cầu/kiến nghị'));
}
/**
* Edit method
*
* @param string|null $id Request id.
* @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise.
* @throws \Cake\Network\Exception\NotFoundException When record not found.
*/
public function edit($id = null)
{
$request = $this->Requests->get($id, [
'contain' => []
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$request = $this->Requests->patchEntity($request, $this->request->getData());
if ($this->Requests->save($request)) {
$this->Flash->success(__('The request has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The request could not be saved. Please, try again.'));
}
$projects = $this->Requests->Projects->find('list', ['limit' => 200]);
$users = $this->Requests->Users->find('list', ['limit' => 200]);
$this->set(compact('request', 'projects', 'users'));
$this->set('_serialize', ['request']);
$this->set('title', __('Sửa yêu cầu/kiến nghị'));
}
public function listRequest(){
$userId = $this->Auth->user('id');
$this->loadModel('Requests');
$requests = $this->Requests->getListRequestUser($userId);
$this->set(compact('requests'));
$this->set('_serialize', ['requests']);
$this->set('title', __('Danh sách yêu cầu/kiến nghị'));}
/**
* Delete method
*
* @param string|null $id Request id.
* @return \Cake\Http\Response|null Redirects to index.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$request = $this->Requests->get($id);
if ($this->Requests->delete($request)) {
$this->Flash->success(__('The request has been deleted.'));
} else {
$this->Flash->error(__('The request could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment