Skip to content

Commit

Permalink
admin controller to edit, delete projects and missions
Browse files Browse the repository at this point in the history
  • Loading branch information
appliedrd committed Aug 4, 2016
1 parent 19f7a8c commit dd6f0b2
Show file tree
Hide file tree
Showing 12 changed files with 990 additions and 1 deletion.
134 changes: 134 additions & 0 deletions src/Controller/AdminsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php
namespace App\Controller;

use App\Controller\AppController;
use Cake\ORM\TableRegistry;

/**
* Projects Controller
*
* @property \App\Model\Table\ProjectsTable $Projects
*/
class AdminsController extends AppController
{
public function initialize()
{
parent::initialize();
$this->Projects = TableRegistry::get('Projects');
$this->Users = TableRegistry::get('Users');
//$this->loadComponent('RequestHandler');
}
public function isAuthorized($user)
{
$user = $this->Users->findById($user['id'])->first();
if ($user && ($user->hasRoleName(['Administrator']))
) {
return true;
}
}

/**
* Index method
*
* @return \Cake\Network\Response|null
*/
public function index()
{
$this->paginate = [
'limit' => 1000,
'order' => ['Projects.name' => 'asc'],
'contain' => ['Organizations', 'Missions','Missions.Users','Missions.Professors']
];
$projects = $this->paginate($this->Projects);
$this->set(compact('projects'));
$this->set('_serialize', ['projects']);
}

/**
* View method
*
* @param string|null $id Project id.
* @return \Cake\Network\Response|null
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function view($id = null)
{
$project = $this->Projects->get($id, [
'contain' => ['Organizations', 'Contributors', 'Mentors', 'Missions', 'Applications']
]);

$this->set('project', $project);
$this->set('_serialize', ['project']);
}

/**
* Add method
*
* @return \Cake\Network\Response|void Redirects on successful add, renders view otherwise.
*/
public function add()
{
$project = $this->Projects->newEntity();
if ($this->request->is('post')) {
$project = $this->Projects->patchEntity($project, $this->request->data);
if ($this->Projects->save($project)) {
$this->Flash->success(__('The project has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The project could not be saved. Please, try again.'));
}
}
$organizations = $this->Projects->Organizations->find('list', ['limit' => 200]);
$contributors = $this->Projects->Contributors->find('list', ['limit' => 200]);
$mentors = $this->Projects->Mentors->find('list', ['limit' => 200]);
$this->set(compact('project', 'organizations', 'contributors', 'mentors'));
$this->set('_serialize', ['project']);
}

/**
* Edit method
*
* @param string|null $id Project id.
* @return \Cake\Network\Response|void Redirects on successful edit, renders view otherwise.
* @throws \Cake\Network\Exception\NotFoundException When record not found.
*/
public function edit($id = null)
{
$project = $this->Projects->get($id, [
'contain' => ['Organizations', 'Contributors', 'Mentors']
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$project = $this->Projects->patchEntity($project, $this->request->data);
if ($this->Projects->save($project)) {
$this->Flash->success(__('The project has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The project could not be saved. Please, try again.'));
}
}
$organizations = $this->Projects->Organizations->find('list', ['limit' => 200]);
$contributors = $this->Projects->Contributors->find('list', ['limit' => 200]);
$mentors = $this->Projects->Mentors->find('list', ['limit' => 200]);
$this->set(compact('project', 'organizations', 'contributors', 'mentors'));
$this->set('_serialize', ['project']);
}

/**
* Delete method
*
* @param string|null $id Project id.
* @return \Cake\Network\Response|null Redirects to index.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$project = $this->Projects->get($id);
if ($this->Projects->delete($project)) {
$this->Flash->success(__('The project has been deleted.'));
} else {
$this->Flash->error(__('The project could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
}
20 changes: 19 additions & 1 deletion src/Controller/MissionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ class MissionsController extends AppController
public function isAuthorized($user)
{
$user = $this->Users->findById($user['id'])->first();

if ($user && ($user->hasRoleName(['Administrator']))
) {
return true;
}
if (isset($this->_permissions[$this->request->action])) {
if ($user->hasPermissionName($this->_permissions[$this->request->action])) {
return true;
Expand Down Expand Up @@ -121,6 +124,7 @@ public function index()
$this->_setFilter('session_select', $this->request->data['session_select']);
$this->_setFilter('studentUniversity', $this->request->data['studentUniversity']);
$this->_setFilter('professorUniversity', $this->request->data['professorUniversity']);
$this->_setFilter('date', $this->request->data['modifiedDate']);
}
// query builder
$query = $this->Missions->find()->contain(['Projects', 'Projects.Organizations', 'Applications', 'TypeMissions', 'Users', 'Professors']);
Expand Down Expand Up @@ -621,4 +625,18 @@ public function editProfessor($id = null)
$this->set(compact('currentProfessorId', 'professors', 'mission'));
$this->set('_serialize', ['mission']);
}

/* for admin view to delete...*/
public function projectindex($id = null)
{
$query = $this->Missions->find()->contain(['Projects', 'Projects.Organizations','Users', 'Professors', 'TypeMissions']);
$query->where(['Projects.id' => $id]);
$missions = $this->paginate($query);
/* $this->paginate = [
'contain' => ['Projects', 'Users', 'Professors', 'TypeMissions']
];
$missions = $this->paginate($this->Missions);*/
$this->set(compact('missions'));
$this->set('_serialize', ['missions']);
}
}
33 changes: 33 additions & 0 deletions src/Template/Admins/add.ctp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<nav class="large-3 medium-4 columns" id="actions-sidebar">
<ul class="side-nav">
<li class="heading"><?= __('Actions') ?></li>
<li><?= $this->Html->link(__('List Projects'), ['action' => 'index']) ?></li>
<li><?= $this->Html->link(__('List Missions'), ['controller' => 'Missions', 'action' => 'index']) ?></li>
<li><?= $this->Html->link(__('New Mission'), ['controller' => 'Missions', 'action' => 'add']) ?></li>
<li><?= $this->Html->link(__('List Applications'), ['controller' => 'Applications', 'action' => 'index']) ?></li>
<li><?= $this->Html->link(__('New Application'), ['controller' => 'Applications', 'action' => 'add']) ?></li>
<li><?= $this->Html->link(__('List Organizations'), ['controller' => 'Organizations', 'action' => 'index']) ?></li>
<li><?= $this->Html->link(__('New Organization'), ['controller' => 'Organizations', 'action' => 'add']) ?></li>
<li><?= $this->Html->link(__('List Contributors'), ['controller' => 'Users', 'action' => 'index']) ?></li>
<li><?= $this->Html->link(__('New Contributor'), ['controller' => 'Users', 'action' => 'add']) ?></li>
</ul>
</nav>
<div class="projects form large-9 medium-8 columns content">
<?= $this->Form->create($project) ?>
<fieldset>
<legend><?= __('Add Project') ?></legend>
<?php
echo $this->Form->input('name');
echo $this->Form->input('link');
echo $this->Form->input('description');
echo $this->Form->input('accepted');
echo $this->Form->input('archived');
echo $this->Form->input('repository_link');
echo $this->Form->input('organizations._ids', ['options' => $organizations]);
echo $this->Form->input('contributors._ids', ['options' => $contributors]);
echo $this->Form->input('mentors._ids', ['options' => $mentors]);
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
28 changes: 28 additions & 0 deletions src/Template/Admins/apply.ctp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<div class="row">
<?= $this->cell('Sidebar::project', [$project->id]); ?>

<div class="col-lg-9 col-md-9 col-sm-9 col-xs-12">
<?= $this->Form->create($application); ?>
<fieldset>
<legend><?= __('Apply on this project') ?></legend>
<?php
echo $this->Form->input('presentation', ['type' => 'textarea']);
echo $this->Form->input('type_application_id', ['options' => $typeApplications, 'type' => 'select']);
echo $this->Form->input('weeklyHours', ['min' => '0']);
echo $this->Form->input('startDate');
echo $this->Form->input('endDate');
?>
</fieldset>
<?= $this->Form->button(__('Submit'), ['class' => 'btn-success']) ?>
<?= $this->Form->button(__('Cancel'), [
'type' => 'button',
'class' => 'btn btn-default',
'onclick' => 'location.href=\'' . $this->url->build([
'controller' => 'Projects',
'action' => 'view',
$project->id
]) . '\''
]); ?>
<?= $this->Form->end() ?>
</div>
</div>
39 changes: 39 additions & 0 deletions src/Template/Admins/edit.ctp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<nav class="large-3 medium-4 columns" id="actions-sidebar">
<ul class="side-nav">
<li class="heading"><?= __('Actions') ?></li>
<li><?= $this->Form->postLink(
__('Delete'),
['action' => 'delete', $project->id],
['confirm' => __('Are you sure you want to delete # {0}?', $project->id)]
)
?></li>
<li><?= $this->Html->link(__('List Projects'), ['action' => 'index']) ?></li>
<li><?= $this->Html->link(__('List Missions'), ['controller' => 'Missions', 'action' => 'index']) ?></li>
<li><?= $this->Html->link(__('New Mission'), ['controller' => 'Missions', 'action' => 'add']) ?></li>
<li><?= $this->Html->link(__('List Applications'), ['controller' => 'Applications', 'action' => 'index']) ?></li>
<li><?= $this->Html->link(__('New Application'), ['controller' => 'Applications', 'action' => 'add']) ?></li>
<li><?= $this->Html->link(__('List Organizations'), ['controller' => 'Organizations', 'action' => 'index']) ?></li>
<li><?= $this->Html->link(__('New Organization'), ['controller' => 'Organizations', 'action' => 'add']) ?></li>
<li><?= $this->Html->link(__('List Contributors'), ['controller' => 'Users', 'action' => 'index']) ?></li>
<li><?= $this->Html->link(__('New Contributor'), ['controller' => 'Users', 'action' => 'add']) ?></li>
</ul>
</nav>
<div class="projects form large-9 medium-8 columns content">
<?= $this->Form->create($project) ?>
<fieldset>
<legend><?= __('Edit Project') ?></legend>
<?php
echo $this->Form->input('name');
echo $this->Form->input('link');
echo $this->Form->input('description');
echo $this->Form->input('accepted');
echo $this->Form->input('archived');
echo $this->Form->input('repository_link');
echo $this->Form->input('organizations._ids', ['options' => $organizations]);
echo $this->Form->input('contributors._ids', ['options' => $contributors]);
echo $this->Form->input('mentors._ids', ['options' => $mentors]);
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
61 changes: 61 additions & 0 deletions src/Template/Admins/edit_mentor.ctp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<div class="row">
<?= $this->cell('Sidebar::project', [$project->id]); ?>
<div class="col-lg-9 col-md-9 col-sm-9 col-xs-12">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<h1 class="page-header"><?=__('Edit mentors'); ?></h1>
<?php
$this->Html->addCrumb(__('Home'), '/');
$this->Html->addCrumb(__('Projects'), '/Projects');
$this->Html->addCrumb($project->getName(), '/projects/view/' . $project->id);
$this->Html->addCrumb(__('Edit mentors'));

echo $this->Html->getCrumbList(); ?>
</div>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="panel panel-default">
<div class="panel-body">
<h3 class="header-title"><?= __('Edit mentors') ?></h3>
<?= $this->Form->create($project); ?>
<fieldset>
<select multiple id="users" name="users[]">
<?php foreach ($members as $member) {
foreach ($mentors as $mentor) {
if ($mentor->getId() === $member->getId()) {
$selected = true;
break;
} else
$selected = false;
}

if ($selected) { ?>
<option value="<?= $member['id'] ?>"
selected><?= '(' . $member['username'] . ') ' . $member['firstName'] . ' ' . $member['lastName'] ?></option>
<?php } else { ?>
<option
value="<?= $member['id'] ?>"><?= '(' . $member['username'] . ') ' . $member['firstName'] . ' ' . $member['lastName'] ?></option>
<?php }
} ?>
</select>
</fieldset>
<br>
<?= $this->Form->button(__('Submit'), ['class' => 'btn-info']) ?>
<?= $this->Form->button(__('Cancel'), [
'type' => 'button',
'class' => 'btn btn-default',
'onclick' => 'location.href=\'' . $this->url->build([
'controller' => 'Projects',
'action' => 'view',
$project->id
]) . '\''
]); ?>
<?= $this->Form->end() ?>
</div>
</div>
</div>
</div>
</div>
</div>
<?= $this->Html->script(['projects/edit-mentor.js', 'duallistbox/dual-list-box.min.js', 'initial.min'], ['block' => 'scriptBottom']); ?>
48 changes: 48 additions & 0 deletions src/Template/Admins/index.ctp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<? //echo phpinfo(); ?>
<div class="projects index large-9 medium-8 columns content">
<h3><?= __('Administer Projects') ?></h3>
<div class="table-responsive">
<table id="projects" class="table table-striped table-bordered dataTable">

<thead>
<tr>
<th><?= $this->Paginator->sort('id') ?></th>
<th>Org</th>
<th><?= $this->Paginator->sort('name') ?></th>
<th><?= $this->Paginator->sort('created') ?></th>
<th class="actions"><?= __('Actions') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($projects as $project): ?>
<tr>
<td><?= $this->Number->format($project->id) ?></td>
<td><? if (isset($project->organizations[0]['name'])) {
echo $project->organizations[0]['name']; }?></td>
<td><?= h($project->name) ?></td>
<td><?= h($project->created) ?></td>
<td class="actions">
<?= $this->Html->link('<i class="fa fa-eye"></i>',
['action' => 'view', $project->id],['escape' => false]) ?>
<?= $this->Form->postLink('<i class="fa fa-trash"></i>',
['action' => 'delete', $project->id],
['escape' => false],
['confirm' => __('Are you sure you want to delete # {0}? and all it missions?',
$project->id)]
) ?>
<?= $this->Html->link(__('missions'), ['controller' => 'Missions','action' => 'projectindex', $project->id]) ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<div class="paginator">
<ul class="pagination">
<?= $this->Paginator->prev('< ' . __('previous')) ?>
<?= $this->Paginator->numbers() ?>
<?= $this->Paginator->next(__('next') . ' >') ?>
</ul>
<p><?= $this->Paginator->counter() ?></p>
</div>
</div>
Loading

0 comments on commit dd6f0b2

Please sign in to comment.