From 4d625f87ebca0a64518750289b5b71f09bc0d858 Mon Sep 17 00:00:00 2001 From: Jakub Onderka Date: Fri, 23 Sep 2022 17:37:32 +0200 Subject: [PATCH] fix: [UI] Communities --- app/Controller/CommunitiesController.php | 8 +-- app/Model/Community.php | 72 +++++++--------------- app/View/Communities/index.ctp | 12 ++-- app/View/Communities/view.ctp | 11 ++-- app/files/community-metadata/defaults.json | 14 ++--- 5 files changed, 46 insertions(+), 71 deletions(-) diff --git a/app/Controller/CommunitiesController.php b/app/Controller/CommunitiesController.php index 162d7162a..5b7a651bf 100644 --- a/app/Controller/CommunitiesController.php +++ b/app/Controller/CommunitiesController.php @@ -1,15 +1,13 @@ 60, 'maxLimit' => 9999 diff --git a/app/Model/Community.php b/app/Model/Community.php index 60429a0b7..870657685 100644 --- a/app/Model/Community.php +++ b/app/Model/Community.php @@ -4,37 +4,20 @@ class Community extends AppModel { public $useTable = false; - public $recursive = -1; - - public $actsAs = array( - 'Containable', - ); - - public $validate = array( - ); - - public function beforeValidate($options = array()) - { - parent::beforeValidate(); - return true; - } - + /** + * @param string $context + * @param string|null $value + * @return array + */ public function getCommunityList($context, $value) { - $community_file = new File(APP . 'files/community-metadata/defaults.json'); - if (!$community_file->exists()) { - throw new NotFoundException(__('Default community list not found.')); - } - $community_list = $community_file->read(); - if (empty($community_list)) { - throw new NotFoundException(__('Default community list empty.')); - } try { - $community_list = json_decode($community_list, true); + $community_list = FileAccessTool::readJsonFromFile(APP . 'files/community-metadata/defaults.json'); } catch (Exception $e) { throw new NotFoundException(__('Default community list not in the expected format.')); } - $fieldsToCheck = array('name', 'uuid', 'description', 'url', 'sector', 'nationality', 'type', 'org_uuid', 'org_name'); + + $fieldsToCheck = ['name', 'uuid', 'description', 'url', 'sector', 'nationality', 'type', 'org_uuid', 'org_name']; foreach ($community_list as $k => $v) { if ($v['misp_project_vetted'] === ($context === 'vetted')) { $community_list[$k]['id'] = $k + 1; @@ -44,11 +27,12 @@ class Community extends AppModel continue; } if (!empty($value)) { + $value = mb_strtolower($value); $found = false; foreach ($fieldsToCheck as $field) { - if (strpos(strtolower($v[$field]), $value) !== false) { + if (strpos(mb_strtolower($v[$field]), $value) !== false) { $found = true; - continue; + break; } } if (!$found) { @@ -56,42 +40,32 @@ class Community extends AppModel } } } - $community_list = array_values($community_list); - return $community_list; + return array_values($community_list); } + /** + * @param int|string $id Community ID or UUID + * @return array + */ public function getCommunity($id) { - $community_file = new File(APP . 'files/community-metadata/defaults.json'); - if (!$community_file->exists()) { - throw new NotFoundException(__('Default community list not found.')); - } - $community_list = $community_file->read(); - if (empty($community_list)) { - throw new NotFoundException(__('Default community list empty.')); - } try { - $community_list = json_decode($community_list, true); + $community_list = FileAccessTool::readJsonFromFile(APP . 'files/community-metadata/defaults.json'); } catch (Exception $e) { throw new NotFoundException(__('Default community list not in the expected format.')); } + foreach ($community_list as $k => $v) { $community_list[$k]['id'] = $k + 1; $community_list[$k]['Org'] = array('uuid' => $v['org_uuid'], 'name' => $v['org_name']); } - $community = false; - $lookupField = 'id'; - if (Validation::uuid($id)) { - $lookupField = 'uuid'; - } + + $lookupField = Validation::uuid($id) ? 'uuid' : 'id'; foreach ($community_list as $s) { - if ($s[$lookupField === 'uuid' ? 'uuid' : 'id'] === $id) { - $community = $s; + if ($s[$lookupField === 'uuid' ? 'uuid' : 'id'] == $id) { + return $s; } } - if (empty($community)) { - throw new NotFoundException(__('Community not found.')); - } - return $community; + throw new NotFoundException(__('Community not found.')); } } diff --git a/app/View/Communities/index.ctp b/app/View/Communities/index.ctp index 4b1498fcd..9280af2cf 100644 --- a/app/View/Communities/index.ctp +++ b/app/View/Communities/index.ctp @@ -31,7 +31,7 @@ ), 'fields' => array( array( - 'name' => __('Id'), + 'name' => __('ID'), 'sort' => 'id', 'class' => 'short', 'data_path' => 'id', @@ -76,14 +76,16 @@ 'url_params_data_paths' => array( 'uuid' ), - 'icon' => 'eye' + 'icon' => 'eye', + 'title' => __('View'), ), array( 'url' => $baseurl . '/communities/requestAccess', 'url_params_data_paths' => array( 'uuid' ), - 'icon' => 'comments' + 'icon' => 'comments', + 'title' => __('Request access'), ) ) ) @@ -91,12 +93,12 @@ echo ''; echo $this->element('/genericElements/SideMenu/side_menu', array('menuList' => 'sync', 'menuItem' => 'list_communities')); ?> -