Skip to content

Commit

Permalink
Dev: Database fixes for quick-menu plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
olleharstedt committed May 25, 2016
1 parent ec2629c commit 7fc800b
Showing 1 changed file with 28 additions and 33 deletions.
61 changes: 28 additions & 33 deletions application/core/plugins/QuickMenu/QuickMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,38 +131,30 @@ public function init()
*/
public function beforeActivate()
{
$aFields = array(
'uid' => 'integer NOT NULL',
'button_name' => 'string(64)',
'sort_order' => 'integer',
'PRIMARY KEY (button_name, uid)'
);
try {
$this->api->createTable($this, 'plugin_extraquickmenuitems_sortorder', $aFields);
//$oDB->createCommand()->createTable('{{plugin_extraquickmenuitems_sortorder}}', $aFields);
/* Carsten says no to foreign keys because of different db
$oDB->createCommand()->addForeignKey(
'fk_survey_id',
'{{plugin_extraquickmenuitems_sortorder}}',
'uid',
'{{users}}',
'uid',
'CASCADE',
'CASCADE'
);
*/
}
catch(Exception $e)
// Create sort order table if it doesn't exist
if (!$this->api->tableExists($this, 'sortorder'))
{
$event = $this->getEvent();
$event->set('success', false);
$event->set(
'message',
gT('An non-recoverable error happened during the update. Error details:')
. "<p>"
. htmlspecialchars($e->getMessage())
. "</p>"
$aFields = array(
'uid' => 'integer NOT NULL',
'button_name' => 'string(64)',
'sort_order' => 'integer',
'PRIMARY KEY (button_name, uid)'
);
try {
$this->api->createTable($this, 'sortorder', $aFields);
}
catch(Exception $e)
{
$event = $this->getEvent();
$event->set('success', false);
$event->set(
'message',
gT('An non-recoverable error happened during the update. Error details:')
. "<p>"
. htmlspecialchars($e->getMessage())
. "</p>"
);
}
}
}

Expand Down Expand Up @@ -479,10 +471,11 @@ public function saveOrder(LSHttpRequest $request)
protected function deleteOldSortings($userId)
{
// Delete all old sortings
$tableName = '{{quickmenu_sortorder}}'; // TODO: Should not be hard-coded
$db = Yii::app()->db;
$db->createCommand()
->delete(
'{{plugin_extraquickmenuitems_sortorder}}',
$tableName,
'uid=:uid',
array(':uid' => $userId)
);
Expand All @@ -498,10 +491,11 @@ protected function deleteOldSortings($userId)
protected function insertNewSortings($userId, $buttons)
{
$db = Yii::app()->db;
$tableName = '{{quickmenu_sortorder}}'; // TODO: Should not be hard-coded
foreach ($buttons as $buttonName => $buttonIndex)
{
$db->createCommand()->insert(
'{{plugin_extraquickmenuitems_sortorder}}',
$tableName,
array(
'uid' => $userId,
'button_name' => $buttonName,
Expand All @@ -519,10 +513,11 @@ protected function insertNewSortings($userId, $buttons)
*/
public static function getOrder($userId)
{
$tableName = '{{quickmenu_sortorder}}'; // TODO: Should not be hard-coded
$db = Yii::app()->db;
$orders = $db->createCommand()
->select(array('button_name', 'sort_order'))
->from('{{plugin_extraquickmenuitems_sortorder}}')
->from($tableName)
->where('uid=:uid', array(':uid' => $userId))
->order('sort_order')
->queryAll();
Expand Down

0 comments on commit 7fc800b

Please sign in to comment.