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

Commit 0629233

Browse files
author
Charles Ma
committed
ENH: refs #380 Added setting table. Changed assetstore's default name. Re-added assetstore default selection.
1 parent 996f381 commit 0629233

File tree

10 files changed

+293
-76
lines changed

10 files changed

+293
-76
lines changed

core/AppController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,11 @@ protected function t($text)
431431
* @var UserModelBase
432432
*/
433433
var $User;
434+
/**
435+
* Setting Model
436+
* @var SettingModelBase
437+
*/
438+
var $Setting;
434439

435440
/**end completion eclipse */
436441
}//end class

core/controllers/AssetstoreController.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class AssetstoreController extends AppController
1818
{
1919

20-
public $_models = array('Assetstore');
20+
public $_models = array('Assetstore', 'Setting');
2121
public $_daos = array('Assetstore');
2222
public $_components = array('Utility');
2323
public $_forms = array('Assetstore');
@@ -61,9 +61,7 @@ function defaultassetstoreAction()
6161
$assetstore = $this->Assetstore->load($element);
6262
if($assetstore != false)
6363
{
64-
$applicationConfig = parse_ini_file(BASE_PATH.'/core/configs/application.local.ini', true);
65-
$applicationConfig['global']['defaultassetstore.id'] = $assetstore->getKey();
66-
$this->Component->Utility->createInitFile(BASE_PATH.'/core/configs/application.local.ini', $applicationConfig);
64+
$this->Setting->setConfig('default_assetstore', $assetstore->getKey());
6765
echo JsonComponent::encode(array(true, $this->t('Changes saved')));
6866
return;
6967
}

core/controllers/InstallController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ function step2Action()
224224

225225
//create default assetstrore
226226
$assetstoreDao = new AssetstoreDao();
227-
$assetstoreDao->setName('Default');
227+
$assetstoreDao->setName('Local');
228228
$assetstoreDao->setPath(BASE_PATH.'/data/assetstore');
229229
$assetstoreDao->setType(MIDAS_ASSETSTORE_LOCAL);
230230
$this->Assetstore = new AssetstoreModel(); //reset Database adapter

core/database/upgrade/3.1.4.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
class Upgrade_3_1_4 extends MIDASUpgrade
4+
{
5+
public function preUpgrade()
6+
{
7+
}
8+
9+
public function mysql()
10+
{
11+
$sql = "CREATE TABLE IF NOT EXISTS `setting` (
12+
`setting_id` bigint(20) NOT NULL AUTO_INCREMENT,
13+
`name` varchar(255) NOT NULL,
14+
`module` varchar(255) NOT NULL,
15+
`value` text NULL DEFAULT NULL ,
16+
PRIMARY KEY (`setting_id`)
17+
) DEFAULT CHARSET=utf8;";
18+
$this->db->query($sql);
19+
}
20+
21+
22+
public function pgsql()
23+
{
24+
$sql = "CREATE TABLE community (
25+
setting_id serial PRIMARY KEY,
26+
name character varying(256) NOT NULL,
27+
module character varying(256) NOT NULL,
28+
value text NOT NULL
29+
) ;";
30+
$this->db->query($sql);
31+
}
32+
33+
public function postUpgrade()
34+
{
35+
}
36+
}
37+
?>

core/models/base/AssetstoreModelBase.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,40 @@ public function delete($dao)
150150
}// delete
151151

152152
/**
153-
* This function returns the default assetstore in the database. This
154-
* is assumed to be named "Default" and is selected as such. If there
155-
* is no assetsore we fail misserably, if there are more than one then
156-
* we only return the "first."
153+
* Check if there is an assetstore in the database. If not look for one called Default.
154+
* If Default doesn't exist, return the first assetstore found.
157155
* @return the default assetstore
158156
*/
159157
public function getDefault()
160158
{
161-
$found = $this->findBy('name', 'Default');
162-
return $found[0];
159+
$modelLoader = new MIDAS_ModelLoader();
160+
$settingModel = $modelLoader->loadModel('Setting');
161+
$assetstoreModel = $modelLoader->loadModel('Assetstore');
162+
$defaultAssetstoreId = $settingModel->getValueByName('default_assetstore');
163+
164+
$defaultAssetstore = false;
165+
166+
if(is_numeric($defaultAssetstoreId))
167+
{
168+
$defaultAssetstore = $assetstoreModel->load($defaultAssetstoreId);
169+
}
170+
171+
if($defaultAssetstoreId == null || $defaultAssetstore == false)
172+
{
173+
$found = $this->findBy('name', 'Default');
174+
if(empty($found))
175+
{
176+
$found = $this->getAll();
177+
if(empty($found))
178+
{
179+
throw new Zend_Exception("No assetstore found in the database");
180+
}
181+
}
182+
$defaultAssetstore = $found[0];
183+
$settingModel->setConfig('default_assetstore', $defaultAssetstore->getKey());
184+
}
185+
186+
return $defaultAssetstore;
163187
} // end getDefault
164188

165189
} // end class AssetstoreModelBase
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 20 rue de la Villette. All rights reserved.
5+
69328 Lyon, FRANCE.
6+
7+
See Copyright.txt for details.
8+
This software is distributed WITHOUT ANY WARRANTY; without even
9+
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10+
PURPOSE. See the above copyright notices for more information.
11+
=========================================================================*/
12+
require_once BASE_PATH.'/core/models/dao/SettingDao.php';
13+
14+
/** Setting Model Base*/
15+
abstract class SettingModelBase extends AppModel
16+
{
17+
/** Constructor*/
18+
public function __construct()
19+
{
20+
parent::__construct();
21+
$this->_name = 'setting';
22+
$this->_key = 'setting_id';
23+
24+
$this->_mainData = array(
25+
'setting_id' => array('type' => MIDAS_DATA),
26+
'name' => array('type' => MIDAS_DATA),
27+
'module' => array('type' => MIDAS_DATA),
28+
'value' => array('type' => MIDAS_DATA)
29+
);
30+
$this->initialize(); // required
31+
} // end __construct()
32+
33+
/** Abstract functions */
34+
abstract function getDaoByName($name, $module = 'core');
35+
36+
/** get value by name */
37+
public function getValueByName($name, $module = 'core')
38+
{
39+
$dao = $this->getDaoByName($name, $module);
40+
if($dao == false)
41+
{
42+
return null;
43+
}
44+
return $dao->getValue();
45+
}
46+
47+
/** Set Configuration value. Set value as null to delete */
48+
public function setConfig($name, $value, $module = 'core')
49+
{
50+
if(!is_string($name) || !is_string($value) || !is_string($module))
51+
{
52+
throw new Zend_Exception('Error Parameters');
53+
}
54+
$dao = $this->getDaoByName($name, $module);
55+
if($dao != false && $dao->getValue() == $value)
56+
{
57+
return;
58+
}
59+
if($dao != false && $value === null)
60+
{
61+
$this->delete($previousDao);
62+
}
63+
elseif($dao != false)
64+
{
65+
$dao->setValue($value);
66+
$this->save($dao);
67+
}
68+
else
69+
{
70+
$dao = new SettingDao ();
71+
$dao->setName($name);
72+
$dao->setModule($module);
73+
$dao->setValue($value);
74+
$this->save($dao);
75+
}
76+
return $dao;
77+
}
78+
79+
} // end class AssetstoreModelBase

core/models/dao/SettingDao.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 20 rue de la Villette. All rights reserved.
5+
69328 Lyon, FRANCE.
6+
7+
See Copyright.txt for details.
8+
This software is distributed WITHOUT ANY WARRANTY; without even
9+
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10+
PURPOSE. See the above copyright notices for more information.
11+
=========================================================================*/
12+
13+
/**
14+
* \class SettingDao
15+
* \brief DAO (table setting)
16+
*/
17+
class SettingDao extends AppDao
18+
{
19+
public $_model = 'Setting';
20+
}
21+
?>

core/models/pdo/SettingModel.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 20 rue de la Villette. All rights reserved.
5+
69328 Lyon, FRANCE.
6+
7+
See Copyright.txt for details.
8+
This software is distributed WITHOUT ANY WARRANTY; without even
9+
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10+
PURPOSE. See the above copyright notices for more information.
11+
=========================================================================*/
12+
13+
require_once BASE_PATH.'/core/models/base/SettingModelBase.php';
14+
15+
/**
16+
* SettingModel
17+
* Pdo Model
18+
*/
19+
class SettingModel extends SettingModelBase
20+
{
21+
/** get by name*/
22+
function getDaoByName($name, $module = 'core')
23+
{
24+
if(!is_string($name) || !is_string($module))
25+
{
26+
throw new Zend_Exception('Error Parameters');
27+
}
28+
$row = $this->database->fetchRow($this->database->select()->where('name = ?', $name)->where('module = ?', $module));
29+
$dao = $this->initDao(ucfirst($this->_name), $row);
30+
return $dao;
31+
}
32+
}// end class

0 commit comments

Comments
 (0)