#
# This file is part of Helga.
#
# Helga is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Helga is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Helga. If not, see .
#
namespace eval ::helga::global {
#
# hset
#
# Add a new attribute to an asset, or change the value of a preexisting
# attribute.
#
proc hsetargs {} {
return [list \
[list attrName "" "" "The name of the attribute to set." ] \
[list newAttrValue "" "" "The value to set the attribute to." ] \
[list -name "" $::session(currentAsset) "The name of the asset or task to set the attribute on. Defaults to your current asset." ] \
]
}
proc hset args {
::helga::command::parseArgs $args "hset"
set thingInfo [::helga::assets::getThingIdAndType $name]
set thingId [lindex $thingInfo 0]
set thingType [lindex $thingInfo 1]
if { $thingType == "task" } {
set names [::helga::assets::getAssetNameAndSuffix $name]
set assetName [lindex $names 0]
::helga::user::checkPermissions advanced $assetName
switch $attrName {
taskId -
assetId -
startedDate -
completedDate -
createdDate {
error "Error: you can't change the attribute $attrName on tasks."
}
taskName {
if { ![::helga::tasks::isATask $newAttrValue] } {
error "Error: hset: that is not a valid task name."
}
set sql "UPDATE `tasks` SET `taskName` = '$newAttrValue' WHERE `taskId` = $thingId"
::helga::db::update $sql
return $newAttrValue
}
status {
if { $newAttrValue == "inprogress" } {
set sql "UPDATE `tasks` SET `startedDate` = [clock seconds] WHERE `taskId` = $thingId"
::helga::db::update $sql
} elseif { $newAttrValue == "complete" } {
set sql "UPDATE `tasks` SET `completedDate` = [clock seconds] WHERE `taskId` = $thingId"
::helga::db::update $sql
} elseif { $newAttrValue == "notstarted" } {
} else {
error "Error: invalid value given for status. Valid values are: notstarted, inprogress, complete."
}
set sql "UPDATE `tasks` SET `status` = '$newAttrValue' WHERE `taskId` = $thingId"
::helga::db::update $sql
return $newAttrValue
}
description -
dueDate -
difficulty -
priority {
set sql "UPDATE `tasks` SET `$attrName` = '$newAttrValue' WHERE `taskId` = $thingId"
::helga::db::update $sql
return $newAttrValue
}
default {
error "Error: tasks don't have that attribute. You can't add custom attributes to tasks."
}
}
} elseif { $thingType == "logEntry" } {
error "Error: you cannot modify log entries. Discussion is underway as to whether to allow this feature."
} else {
::helga::user::checkPermissions advanced $name
set assetId [::helga::assets::getAssetIdFromName $name]
switch $attrName {
assetId {
error "Error: You may not change an asset's ID."
}
assetName {
::helga::assets::checkAssetName $newAttrValue
set sql "UPDATE `assets` SET `$attrName` = '$newAttrValue' WHERE `assetId` = $assetId"
::helga::db::update $sql
}
typeId {
set sql "UPDATE `assets` SET `$attrName` = '$newAttrValue' WHERE `assetId` = $assetId"
::helga::db::update $sql
}
position {
if { ![string is integer $newAttrValue] } {
error "Error: you may only set \"position\" to an integer value. $newAttrValue did not pass the integer check."
}
set nameSplit [split $name "/"]
set listName [join [lrange $nameSplit 0 end-1] "/"]
set listId [::helga::assets::getAssetIdFromName $listName]
set sql "SELECT `assetId`,`position` FROM `assets` WHERE `parentId` = $listId AND `position` >= $newAttrValue"
set rowsToMove [::helga::db::select $sql]
foreach row $rowsToMove {
set memberId [lindex $row 0]
if { $memberId != $assetId } {
set newPosition [expr [lindex $row 1] + 1]
set sql "UPDATE `assets` SET `position` = $newPosition WHERE `assetId` = $memberId"
::helga::db::update $sql
}
}
set sql "UPDATE `assets` SET `position` = $newAttrValue WHERE `assetId` = $assetId"
::helga::db::update $sql
::helga::assets::fixListPositions $listName
}
default {
set sql "SELECT `attrId` FROM `attributes` WHERE `assetId` = $assetId AND `attrName` = '$attrName'"
set result [::helga::db::select $sql]
if { $result != "" } {
# if attr exists, do an update
set attrId [lindex [lindex $result 0] 0]
set sql "UPDATE `attributes` SET `attrVal` = '$newAttrValue' WHERE `attrId` = $attrId"
::helga::db::update $sql
} else {
# otherwise, do an insert
set sql "INSERT INTO `attributes` ( `attrId`, `assetId`, `attrName`, `attrVal` ) VALUES ( NULL, $assetId, '$attrName', '$newAttrValue' )"
::helga::db::insert $sql
}
}
}
}
# return the new value
return $newAttrValue
}
}