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

Commit

Permalink
ENH: refs #380 Added setting table. Changed assetstore's default name…
Browse files Browse the repository at this point in the history
…. Re-added assetstore default selection.
  • Loading branch information
Charles Ma committed Dec 1, 2011
1 parent 996f381 commit 0629233
Show file tree
Hide file tree
Showing 10 changed files with 293 additions and 76 deletions.
5 changes: 5 additions & 0 deletions core/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,11 @@ protected function t($text)
* @var UserModelBase
*/
var $User;
/**
* Setting Model
* @var SettingModelBase
*/
var $Setting;

/**end completion eclipse */
}//end class
6 changes: 2 additions & 4 deletions core/controllers/AssetstoreController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class AssetstoreController extends AppController
{

public $_models = array('Assetstore');
public $_models = array('Assetstore', 'Setting');
public $_daos = array('Assetstore');
public $_components = array('Utility');
public $_forms = array('Assetstore');
Expand Down Expand Up @@ -61,9 +61,7 @@ function defaultassetstoreAction()
$assetstore = $this->Assetstore->load($element);
if($assetstore != false)
{
$applicationConfig = parse_ini_file(BASE_PATH.'/core/configs/application.local.ini', true);
$applicationConfig['global']['defaultassetstore.id'] = $assetstore->getKey();
$this->Component->Utility->createInitFile(BASE_PATH.'/core/configs/application.local.ini', $applicationConfig);
$this->Setting->setConfig('default_assetstore', $assetstore->getKey());
echo JsonComponent::encode(array(true, $this->t('Changes saved')));
return;
}
Expand Down
2 changes: 1 addition & 1 deletion core/controllers/InstallController.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ function step2Action()

//create default assetstrore
$assetstoreDao = new AssetstoreDao();
$assetstoreDao->setName('Default');
$assetstoreDao->setName('Local');
$assetstoreDao->setPath(BASE_PATH.'/data/assetstore');
$assetstoreDao->setType(MIDAS_ASSETSTORE_LOCAL);
$this->Assetstore = new AssetstoreModel(); //reset Database adapter
Expand Down
37 changes: 37 additions & 0 deletions core/database/upgrade/3.1.4.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

class Upgrade_3_1_4 extends MIDASUpgrade
{
public function preUpgrade()
{
}

public function mysql()
{
$sql = "CREATE TABLE IF NOT EXISTS `setting` (
`setting_id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`module` varchar(255) NOT NULL,
`value` text NULL DEFAULT NULL ,
PRIMARY KEY (`setting_id`)
) DEFAULT CHARSET=utf8;";
$this->db->query($sql);
}


public function pgsql()
{
$sql = "CREATE TABLE community (
setting_id serial PRIMARY KEY,
name character varying(256) NOT NULL,
module character varying(256) NOT NULL,
value text NOT NULL
) ;";
$this->db->query($sql);
}

public function postUpgrade()
{
}
}
?>
36 changes: 30 additions & 6 deletions core/models/base/AssetstoreModelBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,40 @@ public function delete($dao)
}// delete

/**
* This function returns the default assetstore in the database. This
* is assumed to be named "Default" and is selected as such. If there
* is no assetsore we fail misserably, if there are more than one then
* we only return the "first."
* Check if there is an assetstore in the database. If not look for one called Default.
* If Default doesn't exist, return the first assetstore found.
* @return the default assetstore
*/
public function getDefault()
{
$found = $this->findBy('name', 'Default');
return $found[0];
$modelLoader = new MIDAS_ModelLoader();
$settingModel = $modelLoader->loadModel('Setting');
$assetstoreModel = $modelLoader->loadModel('Assetstore');
$defaultAssetstoreId = $settingModel->getValueByName('default_assetstore');

$defaultAssetstore = false;

if(is_numeric($defaultAssetstoreId))
{
$defaultAssetstore = $assetstoreModel->load($defaultAssetstoreId);
}

if($defaultAssetstoreId == null || $defaultAssetstore == false)
{
$found = $this->findBy('name', 'Default');
if(empty($found))
{
$found = $this->getAll();
if(empty($found))
{
throw new Zend_Exception("No assetstore found in the database");
}
}
$defaultAssetstore = $found[0];
$settingModel->setConfig('default_assetstore', $defaultAssetstore->getKey());
}

return $defaultAssetstore;
} // end getDefault

} // end class AssetstoreModelBase
79 changes: 79 additions & 0 deletions core/models/base/SettingModelBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/*=========================================================================
MIDAS Server
Copyright (c) Kitware SAS. 20 rue de la Villette. All rights reserved.
69328 Lyon, FRANCE.
See Copyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
require_once BASE_PATH.'/core/models/dao/SettingDao.php';

/** Setting Model Base*/
abstract class SettingModelBase extends AppModel
{
/** Constructor*/
public function __construct()
{
parent::__construct();
$this->_name = 'setting';
$this->_key = 'setting_id';

$this->_mainData = array(
'setting_id' => array('type' => MIDAS_DATA),
'name' => array('type' => MIDAS_DATA),
'module' => array('type' => MIDAS_DATA),
'value' => array('type' => MIDAS_DATA)
);
$this->initialize(); // required
} // end __construct()

/** Abstract functions */
abstract function getDaoByName($name, $module = 'core');

/** get value by name */
public function getValueByName($name, $module = 'core')
{
$dao = $this->getDaoByName($name, $module);
if($dao == false)
{
return null;
}
return $dao->getValue();
}

/** Set Configuration value. Set value as null to delete */
public function setConfig($name, $value, $module = 'core')
{
if(!is_string($name) || !is_string($value) || !is_string($module))
{
throw new Zend_Exception('Error Parameters');
}
$dao = $this->getDaoByName($name, $module);
if($dao != false && $dao->getValue() == $value)
{
return;
}
if($dao != false && $value === null)
{
$this->delete($previousDao);
}
elseif($dao != false)
{
$dao->setValue($value);
$this->save($dao);
}
else
{
$dao = new SettingDao ();
$dao->setName($name);
$dao->setModule($module);
$dao->setValue($value);
$this->save($dao);
}
return $dao;
}

} // end class AssetstoreModelBase
21 changes: 21 additions & 0 deletions core/models/dao/SettingDao.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/*=========================================================================
MIDAS Server
Copyright (c) Kitware SAS. 20 rue de la Villette. All rights reserved.
69328 Lyon, FRANCE.
See Copyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/

/**
* \class SettingDao
* \brief DAO (table setting)
*/
class SettingDao extends AppDao
{
public $_model = 'Setting';
}
?>
32 changes: 32 additions & 0 deletions core/models/pdo/SettingModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/*=========================================================================
MIDAS Server
Copyright (c) Kitware SAS. 20 rue de la Villette. All rights reserved.
69328 Lyon, FRANCE.
See Copyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/

require_once BASE_PATH.'/core/models/base/SettingModelBase.php';

/**
* SettingModel
* Pdo Model
*/
class SettingModel extends SettingModelBase
{
/** get by name*/
function getDaoByName($name, $module = 'core')
{
if(!is_string($name) || !is_string($module))
{
throw new Zend_Exception('Error Parameters');
}
$row = $this->database->fetchRow($this->database->select()->where('name = ?', $name)->where('module = ?', $module));
$dao = $this->initDao(ucfirst($this->_name), $row);
return $dao;
}
}// end class
Loading

0 comments on commit 0629233

Please sign in to comment.