From 8d8526977d30abaafe6be898795b37de7d751099 Mon Sep 17 00:00:00 2001 From: mokaddem Date: Fri, 16 Aug 2019 16:52:36 +0200 Subject: [PATCH] chg: [decaying] Slightly improved `Model/DecayingModel` with shortcuts code quality and options --- app/Controller/DecayingModelController.php | 6 +-- app/Model/DecayingModel.php | 55 +++++++++++++--------- 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/app/Controller/DecayingModelController.php b/app/Controller/DecayingModelController.php index cf509b27e..7111f0811 100644 --- a/app/Controller/DecayingModelController.php +++ b/app/Controller/DecayingModelController.php @@ -329,7 +329,7 @@ class DecayingModelController extends AppController }); $types = array_merge($types, $objectTypes); ksort($types); - $savedDecayingModels = $this->DecayingModel->fetchAllowedModels($this->Auth->user()); + $savedDecayingModels = $this->DecayingModel->fetchAllAllowedModels($this->Auth->user()); $available_formulas = $this->DecayingModel->listAvailableFormulas(); $this->set('available_formulas', $available_formulas); @@ -350,7 +350,7 @@ class DecayingModelController extends AppController public function getAllDecayingModels() { if ($this->request->is('get') && $this->request->is('ajax')) { - $savedDecayingModels = $this->DecayingModel->fetchAllowedModels($this->Auth->user()); + $savedDecayingModels = $this->DecayingModel->fetchAllAllowedModels($this->Auth->user()); $associated_models = $this->DecayingModel->DecayingModelMapping->getAssociatedModels($this->Auth->user()); $associated_types = array(); foreach ($associated_models as $type => $models) { @@ -385,7 +385,7 @@ class DecayingModelController extends AppController } $this->set('user', $this->Auth->user()); $this->set('decaying_model', $decaying_model); - $allowed_models = $this->DecayingModel->fetchAllowedModels($this->Auth->user()); + $allowed_models = $this->DecayingModel->fetchAllAllowedModels($this->Auth->user()); $this->set('all_models', $allowed_models); } diff --git a/app/Model/DecayingModel.php b/app/Model/DecayingModel.php index fb699a071..6bdea5a10 100644 --- a/app/Model/DecayingModel.php +++ b/app/Model/DecayingModel.php @@ -166,7 +166,7 @@ class DecayingModel extends AppModel return !is_null($decaying_model['DecayingModel']['uuid']); } - public function fetchAllowedModels($user, $full=true) + public function fetchAllAllowedModels($user, $full=true) { $conditions = array(); if (!$user['Role']['perm_site_admin']) { @@ -189,10 +189,24 @@ class DecayingModel extends AppModel return $decayingModels; } + public function fetchModels($user, $ids, $full=true, $conditions=array()) + { + $models = array(); + foreach ($ids as $id) { + try { + $model = $this->fetchModel($user, $id, $full, $conditions); + $models[] = $model; + } catch (MethodNotAllowedException $e) { + // Just don't add the model to the result + } + } + return $models; + } + // Method that fetches decayingModel // very flexible, it's basically a replacement for find, with the addition that it restricts access based on user // - full attach Attribute types associated to the requested model - public function fetchModel($user, $id, $full=true, $conditions = array()) + public function fetchModel($user, $id, $full=true, $conditions=array()) { $conditions['id'] = $id; $searchOptions = array( @@ -434,31 +448,26 @@ class DecayingModel extends AppModel public function attachScoresToAttribute($user, &$attribute, $model_id=false, $model_overrides=array()) { - if ($model_id !== false) { - $model = $this->fetchModel($user, $model_id, false); - if ($model !== false) { - if (!empty($model_overrides)) { - $this->overrideModelParameters($model, $model_overrides); - } - $score = $this->getScore($attribute, $model, $user); - $decayed = $this->isDecayed($attribute, $model, $score); - $attribute['decay_score'][] = array('DecayingModel' => $model['DecayingModel'], 'score' => $score, 'decayed' => $decayed); - } else { - throw new NotFoundException(__('Model not found or you are not authorized to view it')); - } - } else { // get score for all activated models + $models = array(); + if ($model_id === false) { // fetch all allowed and associated models $associated_model_ids = $this->DecayingModelMapping->getAssociatedModels($user, $attribute['type'], true); $associated_model_ids = array_values($associated_model_ids[$attribute['type']]); if (!empty($associated_model_ids)) { - foreach ($associated_model_ids as $model_id) { - $model = $this->fetchModel($user, $model_id, false); - if ($model !== false && $model['DecayingModel']['enabled']) { - $score = $this->getScore($attribute, $model, $user); - $decayed = $this->isDecayed($attribute, $model, $score); - $attribute['decay_score'][] = array('DecayingModel' => $model['DecayingModel'], 'score' => $score, 'decayed' => $decayed); - } - } + $models = $this->fetchModels($user, $associated_model_ids, false, array('enabled' => true)); } + } elseif (is_array($model_id)) { + $models = $this->fetchModels($user, $model_id, false, array('enabled' => true)); + } else { + $models[] = $this->fetchModel($user, $model_id, false, array('enabled' => true)); + } + + foreach ($models as $i => $model) { + if (!empty($model_overrides)) { + $this->overrideModelParameters($model, $model_overrides); + } + $score = $this->getScore($attribute, $model, $user); + $decayed = $this->isDecayed($attribute, $model, $score); + $attribute['decay_score'][] = array('DecayingModel' => $model['DecayingModel'], 'score' => $score, 'decayed' => $decayed); } }