Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 4fe3e49

Browse files
author
Jamie Snape
committed
Tidy scalar model and controller
1 parent 473a21d commit 4fe3e49

File tree

3 files changed

+131
-104
lines changed

3 files changed

+131
-104
lines changed

modules/tracker/controllers/ScalarController.php

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,12 @@
2222
* Scalar controller for the tracker module.
2323
*
2424
* @property Tracker_ScalarModel $Tracker_Scalar
25-
* @property Tracker_TrendModel $Tracker_Trend
2625
* @package Modules\Tracker\Controller
2726
*/
2827
class Tracker_ScalarController extends Tracker_AppController
2928
{
3029
/** @var array */
31-
public $_models = array('Community');
32-
33-
/** @var array */
34-
public $_moduleModels = array('Scalar', 'Trend');
30+
public $_moduleModels = array('Scalar');
3531

3632
/**
3733
* Display the dialog of scalar details, including associated result items with thumbnails.
@@ -47,38 +43,39 @@ public function detailsAction()
4743

4844
/** @var int $scalarId */
4945
$scalarId = $this->getParam('scalarId');
46+
5047
if (!isset($scalarId)) {
51-
throw new Zend_Exception('Must set scalarId parameter');
48+
throw new Zend_Exception('The required scalarId parameter is missing');
5249
}
5350

54-
/** @var Tracker_ScalarDao $scalar */
55-
$scalar = $this->Tracker_Scalar->load($scalarId);
56-
if (!$scalar) {
57-
throw new Zend_Exception('Scalar with that id does not exist', 404);
58-
}
59-
$producer = $scalar->getTrend()->getProducer();
60-
$comm = $producer->getCommunity();
61-
if (!$this->Community->policyCheck($comm, $this->userSession->Dao, MIDAS_POLICY_READ)
51+
/** @var Tracker_ScalarDao $scalarDao */
52+
$scalarDao = $this->Tracker_Scalar->load($scalarId);
53+
54+
if ($this->Tracker_Scalar->policyCheck($scalarDao, $this->userSession->Dao, MIDAS_POLICY_READ) === false
6255
) {
63-
throw new Zend_Exception('Permission denied', 403);
56+
throw new Zend_Exception('The scalar does not exist or you do not have the necessary permission', 403);
6457
}
65-
$this->view->isAdmin = $this->Community->policyCheck($comm, $this->userSession->Dao, MIDAS_POLICY_ADMIN);
66-
$this->view->scalar = $scalar;
67-
$this->view->extraParams = json_decode($scalar->getParams(), true);
68-
$this->view->extraUrls = json_decode($scalar->getExtraUrls(), true);
69-
$rev = $scalar->getProducerRevision();
70-
$repoBrowserUrl = $producer->getRevisionUrl();
71-
if ($repoBrowserUrl) {
72-
$repoBrowserUrl = preg_replace('/%revision/', $rev, $repoBrowserUrl);
73-
$this->view->revisionHtml = '<a target="_blank" href="'.$repoBrowserUrl.'">'.$rev.'</a>';
58+
59+
$this->view->isAdmin = $this->Tracker_Scalar->policyCheck($scalarDao, $this->userSession->Dao, MIDAS_POLICY_ADMIN);
60+
$this->view->scalar = $scalarDao;
61+
$this->view->extraParams = json_decode($scalarDao->getParams(), true);
62+
$this->view->extraUrls = json_decode($scalarDao->getExtraUrls(), true);
63+
64+
$revisionUrl = $scalarDao->getTrend()->getProducer()->getRevisionUrl();
65+
$producerRevision = $scalarDao->getProducerRevision();
66+
67+
if (!is_null($revisionUrl)) {
68+
$producerRevisionUrl = preg_replace('/%revision/', $producerRevision, $revisionUrl);
69+
$this->view->revisionHtml = '<a target="_blank" href="'.$producerRevisionUrl.'">'.$producerRevision.'</a>';
7470
} else {
75-
$this->view->revisionHtml = $rev;
71+
$this->view->revisionHtml = $producerRevision;
7672
}
77-
$this->view->resultItems = $this->Tracker_Scalar->getAssociatedItems($scalar);
78-
$this->view->otherValues = $this->Tracker_Scalar->getOtherValuesFromSubmission($scalar);
7973

80-
if ($scalar->getUserId() != -1) {
81-
$this->view->submittedBy = $scalar->getUser();
74+
$this->view->resultItems = $this->Tracker_Scalar->getAssociatedItems($scalarDao);
75+
$this->view->otherValues = $this->Tracker_Scalar->getOtherValuesFromSubmission($scalarDao);
76+
77+
if ($scalarDao->getUserId() !== -1) {
78+
$this->view->submittedBy = $scalarDao->getUser();
8279
} else {
8380
$this->view->submittedBy = null;
8481
}
@@ -99,21 +96,20 @@ public function deleteAction()
9996

10097
/** @var int $scalarId */
10198
$scalarId = $this->getParam('scalarId');
99+
102100
if (!isset($scalarId)) {
103-
throw new Zend_Exception('Must set scalarId parameter');
101+
throw new Zend_Exception('The required scalarId parameter is missing');
104102
}
105103

106-
/** @var Tracker_ScalarDao $scalar */
107-
$scalar = $this->Tracker_Scalar->load($scalarId);
108-
if (!$scalar) {
109-
throw new Zend_Exception('Scalar with that id does not exist', 404);
110-
}
111-
$comm = $scalar->getTrend()->getProducer()->getCommunity();
112-
if (!$this->Community->policyCheck($comm, $this->userSession->Dao, MIDAS_POLICY_ADMIN)
104+
/** @var Tracker_ScalarDao $scalarDao */
105+
$scalarDao = $this->Tracker_Scalar->load($scalarId);
106+
107+
if ($this->Tracker_Scalar->policyCheck($scalarDao, $this->userSession->Dao, MIDAS_POLICY_READ) === false
113108
) {
114-
throw new Zend_Exception('Permission denied', 403);
109+
throw new Zend_Exception('The scalar does not exist or you do not have the necessary permission', 403);
115110
}
116-
$this->Tracker_Scalar->delete($scalar);
111+
112+
$this->Tracker_Scalar->delete($scalarDao);
117113
echo JsonComponent::encode(array('status' => 'ok', 'message' => 'Scalar deleted'));
118114
}
119115
}

modules/tracker/models/base/ScalarModelBase.php

Lines changed: 65 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ abstract class Tracker_ScalarModelBase extends Tracker_AppModel
2929
public function __construct()
3030
{
3131
parent::__construct();
32+
3233
$this->_name = 'tracker_scalar';
3334
$this->_key = 'scalar_id';
3435
$this->_mainData = array(
@@ -57,41 +58,42 @@ public function __construct()
5758
'child_column' => 'user_id',
5859
),
5960
);
61+
6062
$this->initialize();
6163
}
6264

6365
/**
6466
* Associate the given scalar and item.
6567
*
66-
* @param Tracker_ScalarDao $scalar scalar DAO
67-
* @param ItemDao $item item DAO
68+
* @param Tracker_ScalarDao $scalarDao scalar DAO
69+
* @param ItemDao $itemDao item DAO
6870
* @param string $label label
6971
*/
70-
abstract public function associateItem($scalar, $item, $label);
72+
abstract public function associateItem($scalarDao, $itemDao, $label);
7173

7274
/**
7375
* Return the items associated with the given scalar.
7476
*
75-
* @param Tracker_ScalarDao $scalar scalar DAO
77+
* @param Tracker_ScalarDao $scalarDao scalar DAO
7678
* @return array array of associative arrays with keys "item" and "label"
7779
*/
78-
abstract public function getAssociatedItems($scalar);
80+
abstract public function getAssociatedItems($scalarDao);
7981

8082
/**
8183
* Return any other scalars from the same submission as the given scalar.
8284
*
83-
* @param Tracker_ScalarDao $scalar scalar DAO
85+
* @param Tracker_ScalarDao $scalarDao scalar DAO
8486
* @return array scalar DAOs
8587
*/
86-
abstract public function getOtherScalarsFromSubmission($scalar);
88+
abstract public function getOtherScalarsFromSubmission($scalarDao);
8789

8890
/**
8991
* Return any other values from the same submission as the given scalar.
9092
*
91-
* @param Tracker_ScalarDao $scalar scalar DAO
93+
* @param Tracker_ScalarDao $scalarDao scalar DAO
9294
* @return array associative array with keys equal to the metric names
9395
*/
94-
abstract public function getOtherValuesFromSubmission($scalar);
96+
abstract public function getOtherValuesFromSubmission($scalarDao);
9597

9698
/**
9799
* Return a scalar given a trend id, submit time, and user id.
@@ -114,11 +116,11 @@ abstract public function getDistinctBranches();
114116
* Add a new scalar to the trend. If overwrite is true, and a scalar already exists on the trend with the same
115117
* submit time and user, then this will replace that scalar.
116118
*
117-
* @param Tracker_TrendDao $trend trend DAO
119+
* @param Tracker_TrendDao $trendDao trend DAO
118120
* @param string $submitTime submit time
119121
* @param string $producerRevision producer revision
120122
* @param float $value scalar value
121-
* @param UserDao $user user DAO
123+
* @param UserDao $userDao user DAO
122124
* @param bool $overwrite true if a scalar with the same trend, submit time, and user should be overwritten
123125
* @param bool $official true if the submission containing the scalar should be official
124126
* @param string $buildResultsUrl build results URL
@@ -128,47 +130,75 @@ abstract public function getDistinctBranches();
128130
* @return Tracker_ScalarDao scalar DAO
129131
*/
130132
public function addToTrend(
131-
$trend,
133+
$trendDao,
132134
$submitTime,
133135
$producerRevision,
134136
$value,
135-
$user,
137+
$userDao,
136138
$overwrite = true,
137139
$official = true,
138140
$buildResultsUrl = '',
139141
$branch = '',
140142
$params = null,
141143
$extraUrls = null
142144
) {
143-
if ($overwrite) {
144-
$dao = $this->getByTrendAndTimestamp($trend->getKey(), $submitTime, $user->getKey());
145-
if ($dao) {
146-
$this->delete($dao);
145+
if ($overwrite === true) {
146+
$scalarDao = $this->getByTrendAndTimestamp($trendDao->getKey(), $submitTime, $userDao->getKey());
147+
148+
if ($scalarDao !== false) {
149+
$this->delete($scalarDao);
147150
}
148151
}
149152

150-
if (is_array($params)) {
153+
if (empty($params)) {
154+
$params = null;
155+
} elseif (is_array($params)) {
151156
$params = json_encode($params);
152157
}
153-
if (is_array($extraUrls)) {
158+
159+
if (empty($extraUrls)) {
160+
$extraUrls = null;
161+
} elseif (is_array($extraUrls)) {
154162
$extraUrls = json_encode($extraUrls);
155163
}
156164

157-
/** @var Tracker_ScalarDao $scalar */
158-
$scalar = MidasLoader::newDao('ScalarDao', $this->moduleName);
159-
$scalar->setTrendId($trend->getKey());
160-
$scalar->setSubmitTime($submitTime);
161-
$scalar->setProducerRevision($producerRevision);
162-
$scalar->setValue($value);
163-
$scalar->setUserId($user instanceof UserDao ? $user->getKey() : -1);
164-
$scalar->setOfficial($official ? 1 : 0);
165-
$scalar->setBuildResultsUrl($buildResultsUrl);
166-
$scalar->setBranch(trim($branch));
167-
$scalar->setParams($params);
168-
$scalar->setExtraUrls($extraUrls);
169-
170-
$this->save($scalar);
171-
172-
return $scalar;
165+
$userId = (is_null($userDao) || $userDao === false) ? -1 : $userDao->getKey();
166+
167+
/** @var Tracker_ScalarDao $scalarDao */
168+
$scalarDao = MidasLoader::newDao('ScalarDao', $this->moduleName);
169+
$scalarDao->setTrendId($trendDao->getKey());
170+
$scalarDao->setSubmitTime($submitTime);
171+
$scalarDao->setProducerRevision($producerRevision);
172+
$scalarDao->setValue($value);
173+
$scalarDao->setUserId($userId);
174+
$scalarDao->setOfficial((int) $official);
175+
$scalarDao->setBuildResultsUrl($buildResultsUrl);
176+
$scalarDao->setBranch(trim($branch));
177+
$scalarDao->setParams($params);
178+
$scalarDao->setExtraUrls($extraUrls);
179+
$this->save($scalarDao);
180+
181+
return $scalarDao;
182+
}
183+
184+
/**
185+
* Check whether the given policy is valid for the given scalar and user.
186+
*
187+
* @param Tracker_ScalarDao $scalarDao scalar DAO
188+
* @param null|UserDao $userDao user DAO
189+
* @param int $policy policy
190+
* @return bool true if the given policy is valid for the given scalar and user
191+
*/
192+
public function policyCheck($scalarDao, $userDao = null, $policy = MIDAS_POLICY_READ)
193+
{
194+
if (is_null($scalarDao) || $scalarDao === false) {
195+
return false;
196+
}
197+
198+
/** @var Tracker_TrendModel $trendModel */
199+
$trendModel = MidasLoader::loadModel('Trend', $this->moduleName);
200+
$trendDao = $scalarDao->getTrend();
201+
202+
return $trendModel->policyCheck($trendDao, $userDao, $policy);
173203
}
174204
}

0 commit comments

Comments
 (0)