From dd6f0b2030cdd628df9d5635d9287bf0e4ed463b Mon Sep 17 00:00:00 2001 From: appliedrd Date: Thu, 4 Aug 2016 13:07:04 -0400 Subject: [PATCH] admin controller to edit, delete projects and missions --- src/Controller/AdminsController.php | 134 ++++++++++ src/Controller/MissionsController.php | 20 +- src/Template/Admins/add.ctp | 33 +++ src/Template/Admins/apply.ctp | 28 +++ src/Template/Admins/edit.ctp | 39 +++ src/Template/Admins/edit_mentor.ctp | 61 +++++ src/Template/Admins/index.ctp | 48 ++++ src/Template/Admins/my_projects.ctp | 104 ++++++++ src/Template/Admins/submit.ctp | 114 +++++++++ src/Template/Admins/view.ctp | 328 +++++++++++++++++++++++++ src/Template/Missions/index.ctp | 9 + src/Template/Missions/projectindex.ctp | 73 ++++++ 12 files changed, 990 insertions(+), 1 deletion(-) create mode 100644 src/Controller/AdminsController.php create mode 100644 src/Template/Admins/add.ctp create mode 100644 src/Template/Admins/apply.ctp create mode 100644 src/Template/Admins/edit.ctp create mode 100644 src/Template/Admins/edit_mentor.ctp create mode 100644 src/Template/Admins/index.ctp create mode 100644 src/Template/Admins/my_projects.ctp create mode 100644 src/Template/Admins/submit.ctp create mode 100644 src/Template/Admins/view.ctp create mode 100644 src/Template/Missions/projectindex.ctp diff --git a/src/Controller/AdminsController.php b/src/Controller/AdminsController.php new file mode 100644 index 00000000..7e3b3a3d --- /dev/null +++ b/src/Controller/AdminsController.php @@ -0,0 +1,134 @@ +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']); + } +} diff --git a/src/Controller/MissionsController.php b/src/Controller/MissionsController.php index f6b853bb..c0f95d0e 100644 --- a/src/Controller/MissionsController.php +++ b/src/Controller/MissionsController.php @@ -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; @@ -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']); @@ -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']); + } } diff --git a/src/Template/Admins/add.ctp b/src/Template/Admins/add.ctp new file mode 100644 index 00000000..e57a22a2 --- /dev/null +++ b/src/Template/Admins/add.ctp @@ -0,0 +1,33 @@ + +
+ Form->create($project) ?> +
+ + 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]); + ?> +
+ Form->button(__('Submit')) ?> + Form->end() ?> +
diff --git a/src/Template/Admins/apply.ctp b/src/Template/Admins/apply.ctp new file mode 100644 index 00000000..1d02c67c --- /dev/null +++ b/src/Template/Admins/apply.ctp @@ -0,0 +1,28 @@ +
+ cell('Sidebar::project', [$project->id]); ?> + +
+ Form->create($application); ?> +
+ + 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'); + ?> +
+ Form->button(__('Submit'), ['class' => 'btn-success']) ?> + Form->button(__('Cancel'), [ + 'type' => 'button', + 'class' => 'btn btn-default', + 'onclick' => 'location.href=\'' . $this->url->build([ + 'controller' => 'Projects', + 'action' => 'view', + $project->id + ]) . '\'' + ]); ?> + Form->end() ?> +
+
diff --git a/src/Template/Admins/edit.ctp b/src/Template/Admins/edit.ctp new file mode 100644 index 00000000..ee249da7 --- /dev/null +++ b/src/Template/Admins/edit.ctp @@ -0,0 +1,39 @@ + +
+ Form->create($project) ?> +
+ + 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]); + ?> +
+ Form->button(__('Submit')) ?> + Form->end() ?> +
diff --git a/src/Template/Admins/edit_mentor.ctp b/src/Template/Admins/edit_mentor.ctp new file mode 100644 index 00000000..42ef7980 --- /dev/null +++ b/src/Template/Admins/edit_mentor.ctp @@ -0,0 +1,61 @@ +
+ cell('Sidebar::project', [$project->id]); ?> +
+
+
+

+ 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(); ?> +
+
+
+
+
+
+

+ Form->create($project); ?> +
+ +
+
+ Form->button(__('Submit'), ['class' => 'btn-info']) ?> + Form->button(__('Cancel'), [ + 'type' => 'button', + 'class' => 'btn btn-default', + 'onclick' => 'location.href=\'' . $this->url->build([ + 'controller' => 'Projects', + 'action' => 'view', + $project->id + ]) . '\'' + ]); ?> + Form->end() ?> +
+
+
+
+
+
+Html->script(['projects/edit-mentor.js', 'duallistbox/dual-list-box.min.js', 'initial.min'], ['block' => 'scriptBottom']); ?> diff --git a/src/Template/Admins/index.ctp b/src/Template/Admins/index.ctp new file mode 100644 index 00000000..db1fe1f8 --- /dev/null +++ b/src/Template/Admins/index.ctp @@ -0,0 +1,48 @@ + +
+

+
+ + + + + + + + + + + + + + + + + + + + + + +
Paginator->sort('id') ?>OrgPaginator->sort('name') ?>Paginator->sort('created') ?>
Number->format($project->id) ?>organizations[0]['name'])) { + echo $project->organizations[0]['name']; }?>name) ?>created) ?> + Html->link('', + ['action' => 'view', $project->id],['escape' => false]) ?> + Form->postLink('', + ['action' => 'delete', $project->id], + ['escape' => false], + ['confirm' => __('Are you sure you want to delete # {0}? and all it missions?', + $project->id)] + ) ?> + Html->link(__('missions'), ['controller' => 'Missions','action' => 'projectindex', $project->id]) ?> +
+
+
+
    + Paginator->prev('< ' . __('previous')) ?> + Paginator->numbers() ?> + Paginator->next(__('next') . ' >') ?> +
+

Paginator->counter() ?>

+
+
diff --git a/src/Template/Admins/my_projects.ctp b/src/Template/Admins/my_projects.ctp new file mode 100644 index 00000000..3f0fa5e6 --- /dev/null +++ b/src/Template/Admins/my_projects.ctp @@ -0,0 +1,104 @@ +Html->css('dataTables.bootstrap.min', ['block' => 'cssTop']); ?> +
+
+

Wiki->addHelper('Projects'); ?>

+ Html->addCrumb(__('Home'), '/'); + $this->Html->addCrumb(__('Projects'), '/Projects'); + $this->Html->addCrumb(__('My projects')); + + echo $this->Html->getCrumbList(); ?> +
+
+
+
+
+
+

Wiki->addHelper('Projects'); ?>

+ +
+ + + + + + + + + + + + + + + + + + + +
Wiki->addHelper('Organizations'); ?>
+
+
+
+
+
+ +Html->script( + [ + 'datatables/jquery.dataTables.min', + 'datatables/dataTables.bootstrap.min', + 'DataTables.cakephp.dataTables', + ], + ['block' => 'scriptBottom']); +?> +Html->scriptStart(['block' => 'scriptBottom']); +echo $this->DataTables->init([ + 'ajax' => [ + 'url' => $this->Url->build(['action' => 'myProjects']), + ], + 'deferLoading' => $recordsTotal, + 'delay' => 600, + "sDom" => "<'row'<'col-xs-6'l>r>t<'row'<'col-xs-6'i><'col-xs-6'p>>", + '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 + ] + ], + 'lengthMenu' => '' +])->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(); +?> +Html->script('projects/index.js', ['block' => 'scriptBottom']); ?> \ No newline at end of file diff --git a/src/Template/Admins/submit.ctp b/src/Template/Admins/submit.ctp new file mode 100644 index 00000000..1d8f2389 --- /dev/null +++ b/src/Template/Admins/submit.ctp @@ -0,0 +1,114 @@ +Html->css('bootstrap-markdown.min', ['block' => 'cssTop']); ?> +
+
+

+ Html->addCrumb(__('Home'), '/'); + $this->Html->addCrumb(__('Projects'), '/Projects'); + $this->Html->addCrumb(__('Submit a project')); + + echo $this->Html->getCrumbList(); ?> +
+
+
+ +
+
+
+
+
+
+

+ +
+ +
+
+
+ +
+
+ Form->create($project, ['name' => 'project', 'id' => 'createProject']); ?> + Form->input('name', ['label' => __('Name of the project ')]); + echo $this->Form->input('link', ['pattern' => '^(https?):\/\/(.*)\.(.+)', 'title' => 'http://website.ca', 'label' => __('Website of the project'), 'placeholder' => __("http(s)://website.com")]); + echo $this->Form->input('description', + [ + 'label' => __('Description of the project'), + 'data-provide' => 'markdown', + 'data-iconlibrary' => 'fa', + 'data-hidden-buttons' => 'cmdImage', + 'data-language' => ($this->request->session()->read('lang') == 'fr_CA' ? 'fr' : ''), + 'data-footer' => '' . __('Markdown Cheatsheet') . '' + ] + ); + if (empty($organizations->toArray())) : ?> +

+ Html->link(__('You\'re not part of an organization. Add yours now!'), ['controller' => 'Organizations', 'action' => 'submit']); ?> +

+ + Form->input('organizations._ids', ['options' => $organizations, 'label' => __('Select organizations associated with the project. Leave blank if no organizations')]); ?> +

+ Html->link(__('here.'), ['controller' => 'Organizations', 'action' => 'submit']) ?> +

+ toArray(); + $missionsPost = array_intersect_key($projectArray, array_flip(preg_grep('/^mission-/', array_keys($projectArray)))); + if (!is_null($missionsPost)) : + foreach ($missionsPost as $i => $mission) : + ?> + + Form->end() + ?> +
+
+
+ +
+
+ element('Missions/addForm'); + echo $this->fetch('form'); + ?> +
+
+
+ Form->button(__('Submit the project'), ['id' => 'submitProject', 'class' => 'btn-info ']) ?> + +
+
+
+
+Html->script( + [ + 'markdown/markdown', + 'markdown/to-markdown', + 'bootstrap/bootstrap-markdown', + 'bootstrap.min', + 'projects/submit' + ], + ['block' => 'scriptBottom']); +if ($this->request->session()->read('lang') == 'fr_CA') + echo $this->Html->script('locale/bootstrap-markdown.fr', ['block' => 'scriptBottom']); +$this->Html->scriptStart(['block' => 'scriptBottom']); +echo 'var btnSubmitTxt="' . __('Submit the project') . '";'; +echo 'var errorMsg="' . __('All tabs must be valid before submitting the project.') . '";'; +echo 'var multiselectTr="' . __('You must select at least one item.') . '";'; +echo 'var infoIntern="' . __('By selecting an intern, you acknowledge that you will pay for your intern (typically $12000 to $14000 per semester). We will then forward your posting to the CO-OP department of all our university partners.') . '";'; +echo 'var infoMaster="' . __('This project type requires a professor and student(s) from the same university. Ensure the project is sufficiently complex for a graduate student (guidelines coming in 2016).') . '";'; +echo 'var infoCapstone=\'' . __('A PFE/Capstone project requires a professor and student(s) from the same university. The length is 1 to 2 sessions, depending on the university. For example guidelines see here: (guidelines coming in 2016). {0} for more information', $this->Html->link(__('Contact us'), ['controller' => 'Pages', 'action' => 'contact'])) . '\';'; +$this->Html->scriptEnd(); +?> \ No newline at end of file diff --git a/src/Template/Admins/view.ctp b/src/Template/Admins/view.ctp new file mode 100644 index 00000000..27a68ef8 --- /dev/null +++ b/src/Template/Admins/view.ctp @@ -0,0 +1,328 @@ + +
+

name) ?>

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
name) ?>
link) ?>
repository_link) ?>
Number->format($project->id) ?>
Number->format($project->accepted) ?>
Number->format($project->archived) ?>
created) ?>
modified) ?>
+
+

+ Text->autoParagraph(h($project->description)); ?> +
+ + + + + +
diff --git a/src/Template/Missions/index.ctp b/src/Template/Missions/index.ctp index 30a905b9..a435a61b 100644 --- a/src/Template/Missions/index.ctp +++ b/src/Template/Missions/index.ctp @@ -67,6 +67,15 @@ $paginatorParams = $this->Paginator->params();
+
+
+

+ Form->templates( + ['dateWidget' => '{{day}}{{month}}{{year}}'] + ); + echo $this->Form->input('modifiedDate', ['type'=>'date','label'=>false]); ?> +
+

diff --git a/src/Template/Missions/projectindex.ctp b/src/Template/Missions/projectindex.ctp new file mode 100644 index 00000000..c65bdfd5 --- /dev/null +++ b/src/Template/Missions/projectindex.ctp @@ -0,0 +1,73 @@ +
+ first(); + $projectName = $firstMission->project['name']; + if (isset($firstMission->organizations[0])) { + $org = $firstMission->organizations[0]['name]']; + } else { + $org = 'no org'; + } + ?> +

+
+
+ Project: +
+
+ +
+
+
+
+ Organization: +
+
+ +
+
+
+ + + + + + + + + + + + + + + + + + + + + +
Paginator->sort('id') ?>Paginator->sort('mission') ?>Paginator->sort('mentor_id') ?>Paginator->sort('session') ?>
Number->format($mission->id) ?>name) ?>has('user') ? + $this->Html->link($mission->user->lastName, ['controller' => 'Users', 'action' => 'view', $mission->user->id]) : '' ?>Number->format($mission->session) ?> + Html->link('', + ['action' => 'view', $mission->id],['escape' => false]) ?> + Html->link('', + ['action' => 'edit', $mission->id],['escape' => false]) ?> + Form->postLink('', + ['action' => 'delete', $mission->id], + ['escape' => false], + ['confirm' => __('Are you sure you want to delete # {0}?', $mission->id)]) ?> +
+
+
    + Paginator->prev('< ' . __('previous')) ?> + Paginator->numbers() ?> + Paginator->next(__('next') . ' >') ?> +
+

Paginator->counter() ?>

+
+
+
+