Skip to content

Commit

Permalink
Merge 0830f86 into a80579e
Browse files Browse the repository at this point in the history
  • Loading branch information
fxleblanc committed Mar 30, 2016
2 parents a80579e + 0830f86 commit 6e76cf5
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 149 deletions.
108 changes: 32 additions & 76 deletions src/Controller/ProjectsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,92 +84,48 @@ public function initialize()
$this->loadComponent('RequestHandler');
}

/**
* SetFilter method
*
* @param string $key key
* @param string $value value
*
* @return void
*/
private function _setFilter($key, $value)
{
if (!empty($value)) {
$this->request->session()->write('filter.project.' . $key, $value);
} else {
$this->request->session()->delete('filter.project.' . $key);
}
}

/**
* Index method
*
* @return void
*/
public function index()
{
$user = $this->loadModel("Users")->findById($this->request->session()->read('Auth.User.id'))->first();
$session = $this->request->session();
if ($this->request->is(['post', 'put', 'patch'])) {
$this->_setFilter('name', $this->request->data['name']);
}

$orgUser = (!is_null($user) ? array_map(
function ($o) {
return $o->getId();
},
$user->getOrganizationsJoined()
) : []);
$name = $session->read('filter.project.name');
$query = $this->Projects->find();
$query->contain(['Organizations']);
$query->where(['archived' => 0]);
$query->where(['accepted' => 1]);
if ($name) {
$query->where(['name' => $name]);
}

if (!is_null($user) && $user->hasPermissionName(['list_projects_all'])) {
$this->adminIndex();
} else {
$data = $this->DataTables
->find(
'Projects',
[
'contain' => [
'Organizations' =>
[
'fields' =>
[
'id', 'name', 'OrganizationsProjects.project_id'
]
]
],
'fields' =>
[
'id', 'name', 'link', 'accepted'
],
'join' =>
[
[
'table' => 'projects_mentors',
'alias' => 'm',
'type' => 'LEFT',
'conditions' => 'm.project_id = Projects.id'
],
[
'table' => 'projects_contributors',
'alias' => 'c',
'type' => 'LEFT',
'conditions' => 'c.project_id = Projects.id'
],
[
'table' => 'organizations_projects',
'alias' => 'o',
'type' => 'LEFT',
'conditions' => 'o.project_id = Projects.id'
]
],
'conditions' =>
[
'OR' =>
[
[
'archived' => 0,
'accepted' => 1,
],
[
'archived' => 0,
'm.user_id' => (!is_null($user) ? $user->getId() : ' '),
],
[
'archived' => 0,
'o.organization_id IN' => (!empty($orgUser) ? $orgUser : ' ')
]
]
],
'group' => 'Projects.id'
]
);
$projects = $this->paginate($query);

$this->set(
[
'data' => $data,
'_serialize' => array_merge($this->viewVars['_serialize'], ['data'])
]
);
}
$this->set(compact('projects'));
$this->set('_serialize', ['projects']);
}

/**
Expand Down
106 changes: 35 additions & 71 deletions src/Template/Projects/index.ctp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?= $this->Html->css('dataTables.bootstrap.min', ['block' => 'cssTop']); ?>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<h1 class="page-header"><?= __('List of projects'); ?> <?= $this->Wiki->addHelper('Projects'); ?></h1>
Expand All @@ -10,12 +9,16 @@
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="alert alert-info alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<?=__("A project can be of development or system administration and it can be owned by zero, one or more organizations. It may provide mission to students.")?>
<div class="panel panel-primary">
<div class="panel-body">
<?= $this->Form->create($projects); ?>
<div class="col-xs-12 col-md-12 col-sm-12">
<?= $this->Form->input('name', ['required' => false]); ?>
</div>
<div class="col-xs-12">
<?= $this->Form->button(__('Search for a project'), ['class' => 'btn btn-info']) ?>
</div>
<?= $this->Form->end(); ?>
</div>
</div>
<div class="row">
Expand All @@ -26,80 +29,41 @@
<div class="table-responsive">
<table id="projects" class="table table-striped table-bordered dataTable">
<thead>
<tr>
<th></th>
<th><?= __('Name'); ?></th>
<th><?= __('Link'); ?></th>
<th><?= __('Organizations'); ?> <?= $this->Wiki->addHelper('Organizations'); ?></th>
</tr>
<tr>
<th><?= __('Name'); ?></th>
<th><?= __('Link'); ?></th>
<th><?= __('Organizations'); ?> <?= $this->Wiki->addHelper('Organizations'); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($projects as $project): ?>
<tr>
<td><?= $this->Html->link($project->name, ['controller' => 'Projects', 'action' => 'view', $project->id]); ?></td>
<td><?= $this->Html->link($project->link, $project->link); ?></td>
<td class="hide-mobile">
<?php foreach ($project->organizations as $org): ?>
<?php if ($org !== reset($project->organizations)) echo ','; ?>
<?php echo $org['name']; ?>
<?php endforeach; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
<tfoot>
<tr class="table-search info">
<td></td>
<td><input type="text" placeholder="<?= __('Search ...'); ?>"
class="form-control input-sm input-block-level"/></td>
<td><input type="text" placeholder="<?= __('Search ...'); ?>"
class="form-control input-sm input-block-level"/></td>
<td><input type="text" placeholder="<?= __('Search ...'); ?>"
class="form-control input-sm input-block-level"/></td>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
</div>
<div class="paginator">
<ul class="pagination col-lg-12 col-md-12 col-xs-12">
<?= $this->Paginator->prev('< ' . __('previous')) ?>
<?= $this->Paginator->next(__('next') . ' >') ?>
</ul>
</div>
</div>
<!-- Add DataTables scripts -->
<?= $this->Html->script(
[
'datatables/jquery.dataTables.min',
'datatables/dataTables.bootstrap.min',
'DataTables.cakephp.dataTables',
],
['block' => 'scriptBottom']);
?>
<?php
$this->Html->scriptStart(['block' => 'scriptBottom']);
echo $this->DataTables->init([
'ajax' => [
'url' => $this->Url->build(['action' => 'index']),
],
'deferLoading' => $recordsTotal,
'delay' => 600,
"sDom" => "<'row'<'col-xs-6'l>r>t<'row'<'col-xs-6'i><'col-xs-6'p>>",
"autoWidth" => false,
'columns' => [
[
'name' => 'Projects.id',
'data' => 'id',
'searchable' => false,
'visible' => false
],
[
'name' => 'Projects.name',
'data' => 'name',
'searchable' => true
],
[
'name' => 'Projects.link',
'data' => 'link',
'searchable' => true
],
[
'name' => 'Organizations.name',
'data' => 'organizations',
'searchable' => true,
'orderable' => false
]
],
'pageLength' => 50
])->draw('.dataTable');
echo 'var orgUrl="' . $this->Url->Build(['controller' => 'organizations', 'action' => 'view']) . '";';
echo 'var projectUrl="' . $this->Url->Build(['action' => 'view']) . '";';
echo 'var validationTxt="' . __('Pending Validation') . '";';
$this->Html->scriptEnd();
'jquery-2.1.4.min.js',
'bootstrap.min.js'
]);
?>
<?= $this->Html->script('projects/index.js', ['block' => 'scriptBottom']); ?>
4 changes: 2 additions & 2 deletions tests/Fixture/ProjectsFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ class ProjectsFixture extends TestFixture
],
[
'id' => 4,
'name' => 'projet3',
'name' => 'projet4',
'link' => 'www.website.com',
'description' => 'bla bla',
'accepted' => 0,
'archived' => 1
],
[
'id' => 5,
'name' => 'projet3',
'name' => 'projet5',
'link' => 'www.website.com',
'description' => 'bla bla',
'accepted' => 0,
Expand Down
18 changes: 18 additions & 0 deletions tests/TestCase/Controller/ProjectsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@ public function testIndexOk()
$this->assertResponseSuccess();
}

/**
*
* Test index - With name in request
*
* @return void
*/
public function testIndexWithName()
{
$data = [
'name' => 'projet5'
];

$this->post('/projects/index', $data);

$this->assertResponseSuccess();
$this->assertResponseContains('projet5');
}

/**
* Test index - No Authentification
*
Expand Down

0 comments on commit 6e76cf5

Please sign in to comment.