kdb = $kdb; //mida kuradit!!!
if ( is_numeric($rid)) {
$commData = $kdb->getCommonData($rid);
$this->setData($commData);
} else {
$this->newObject();
}
}
function setData($commData) {
$this->setResourceID($commData['resourceID']);
$this->resourceUID = $commData['resourceUID'];
if ($this->resourceUID==NULL) {
$this->resourceUID = $this->newResourceUID();
$sql = "UPDATE psyhvel_resources SET resourceUID='%s' WHERE resourceID=%s";
$sql = sprintf($sql,
$this->getResourceUID(),
$this->getResourceID()
);
$this->kdb->query($sql);
}
$this->setDeleted($commData['deleted']);
$this->setFolderid($commData['folderid']);
$this->setTitle($commData['title']);
$this->setUserID((int)$commData['userID']);
$this->setCreated($commData['created']);
$this->setModified($commData['modified']);
$this->setDescription($commData['description']);
$this->setKeywords($commData['keywords']);
$this->setState($commData['state']);
$this->setSecurity($commData['security']);
}
function newObject() {
global $user;
$this->setResourceID("new");
$this->setDeleted(0);
if ( isset($_GET['folderid']) && is_numeric($_GET['folderid']) ) {
$this->setFolderid($_GET['folderid']);
} else if ( isset($_SESSION['folderid']) && is_numeric($_SESSION['folderid']) ) {
$this->setFolderid($_SESSION['folderid']);
}
$this->setTitle("");
$this->setUserID((int)$user->getId());
$this->setCreated('0000-00-00 00:00:00');
$this->setModified('0000-00-00 00:00:00');
$this->setDescription("");
$this->setKeywords("");
$this->resourceUID = $this->newResourceUID();
}
function updateData($commData) {
$this->setTitle($commData['title']);
$this->setDescription($commData['description']);
$this->setKeywords($commData['keywords']);
}
public function getMenuItems(){
return render_menu_rid($this->getResourceID());
}
protected function newResourceUID() {
$ts = date('YmdHis');
$rn = rand(1000,9999);
return "resob".$ts.$rn;
}
public function getResourceUID() {
return $this->resourceUID;
}
public function getImages() {
//return $this->kdb->getResourcesByType("Image", array("userID"=>$this->user->getId()));
return $this->kdb->getResourcesByType("Image", array("security"=>"'public'"));
}
protected function save() {
// save
if ( $this->getResourceID() == 'new' ) {
$sql = "INSERT INTO psyhvel_resources (resourceUID, type, folderid, title, description, keywords, userID) values ('%s', '%s', %s, '%s', '%s', '%s', %s)";
$sql = sprintf($sql,
$this->getResourceUID(),
$this->getType(),
$this->getFolderid() ? $this->getFolderid() : 'NULL',
$this->getTitle(),
$this->getDescription(),
$this->keywords,
$this->getUserID());
$this->kdb->query($sql);
$dbuid = mysql_insert_id();
$this->setResourceID($dbuid);
} else {
$sql = "UPDATE psyhvel_resources SET title='%s', description='%s', keywords='%s', modified=NOW() WHERE resourceID=%s";
$sql = sprintf($sql,
$this->getTitle(),
$this->getDescription(),
$this->keywords,
$this->getResourceID()
);
$this->kdb->query($sql);
}
}
protected function copy() {
global $user;
$sql = "INSERT INTO psyhvel_resources (resourceUID, type, folderid, title, description, keywords, userID) values ('%s', '%s', %s, '%s', '%s', '%s', %s)";
$fid = 'NULL';
if ( isset($_POST['folderid']) && is_numeric($_POST['folderid']) && $_POST['folderid']>0) {
$fid = $_POST['folderid'];
}
$sql = sprintf($sql,
$this->newResourceUID(),
$this->getType(),
$fid,
$this->getTitle(),
$this->getDescription(),
$this->keywords,
$user->getID());
$this->kdb->query($sql);
$dbuid = mysql_insert_id();
$this->setResourceID($dbuid);
$this->updateFulltext();
}
protected function makeResourceFromXML($xml) {
global $user;
$sql = "INSERT INTO psyhvel_resources (type, folderid, title, description, keywords, userID) values ('%s', %s, '%s', '%s', '%s', %s)";
$fid = 'NULL';
if ( isset($_POST['folderid']) && is_numeric($_POST['folderid']) && $_POST['folderid']>0) {
$fid = $_POST['folderid'];
}
$sql = sprintf($sql,
$xml['type'],
$fid,
$xml['title'],
$xml['description'],
$xml['keywords'],
$user->getID());
$this->kdb->query($sql);
$dbuid = mysql_insert_id();
$this->setResourceID($dbuid);
}
public function getCSSS() {
return $this->css;
}
public function getJSS() {
if ($this->js_compile && $this->mode=='compile') {
return $this->js_compile;
}
return $this->js;
}
public function getFullname($uid=false) {
if ($uid) {
$userid = $uid;
} else {
if ($this->mode=='answer') {
$aob = $this->getAnswerObject();
$userid = $aob->getUserID();
} else {
$userid = $this->getUserID();
}
}
$user = new User((int)$userid);
return $user->getDetails()->getFullnameDisplay();
}
protected function setResourceID($rid) {
$this->resourceID = $rid;
}
public function getResourceID() {
return $this->resourceID;
}
protected function setDeleted($d) {
$this->deleted = $d;
}
public function getDeleted() {
return $this->deleted;
}
public function getType() {
return $this->dtype;
}
protected function setFolderid($f) {
$this->folderid = $f;
}
public function getFolderid() {
return $this->folderid;
}
protected function setTitle($t) {
$this->title = $t;
}
public function getTitle() {
return $this->title;
}
protected function setUserID($u) {
$this->userID = $u;
}
public function getUserID() {
return $this->userID;
}
public function getState() {
return $this->state;
}
public function setState($val) {
if ( $val != 'draft' && $val != 'final' ) {
throw new Exception('hack hack');
}
$this->state = $val;
}
public function validIsDraft() {
if ($this->getState() == 'draft' ) {
return 0;
}
return 1;
}
public function validIsFinal() {
if ($this->getState() == 'final' ) {
return 0;
}
return 1;
}
public function validIsMyDraft() {
if (!$this->isOwner()) {
return 0;
}
if (!$this->validIsDraft()){
return 1;
}
return 0;
}
public function validIsMyPrivate() {
if (!$this->isOwner()) {
return 0;
}
if (!$this->validIsPrivate()){
return 1;
}
return 0;
}
public function validIsSecured() {
if (!$this->validIsMyDraft() && !$this->validIsMyPrivate()){
return 0;
}
return 1;
}
public function validIsPrivate() {
if ($this->getSecurity() == 'private' ) {
return 0;
}
return 1;
}
public function validIsPublic() {
global $user, $psyhvel;
$roles = $user->getRoles();
if ( $roles["authenticated"] > 0 ) {
return 0;
}
return 1;
}
public function getSecurity() {
return $this->security;
}
public function setSecurity($val) {
if ( $val != 'public' && $val != 'private' ) {
throw new Exception('hack hack');
}
$this->security = $val;
}
protected function setCreated($c) {
$this->created = $c;
}
public function getCreated() {
return $this->created;
}
public function getFormattedCreated() {
date_default_timezone_set('UTC');
if ($this->mode=='answer') {
$aob = $this->getAnswerObject();
$d = strtotime($aob->getCreated());
}if ($this->getResourceID()=="new") {
$d = strtotime("NOW");
}else {
$d = strtotime($this->created);
}
return strftime("%e %B %G", $d);
}
protected function setModified($m) {
$this->modified = $m;
}
public function getModified() {
return $this->modified;
}
protected function setDescription($d) {
$this->description = $d;
}
public function getDescription() {
return $this->description;
}
protected function setKeywords($kws) {
$kws_raw = split(",", $kws);
$kws = ",";
foreach ($kws_raw as $kw_raw){
$kw = trim($kw_raw);
$kws .= mb_strtolower($kw, "UTF-8").",";
}
$this->keywords = $kws;
}
public function getKeywords() {
$k = trim($this->keywords, ",");
$kws_raw = split(",", $k);
$kws = implode(", ", $kws_raw);
return $kws;
}
public function getFolders() {
global $kdb, $user;
$ret = $kdb->query("SELECT * FROM folders WHERE userID=".$user->getId());
$res = array();
while ( $row = mysql_fetch_array($ret) ) {
$res[] = $row;
}
return $res;
}
function getCurrentFolderID() {
return $this->getFolderid();
}
function getIsExponated($exhib) {
if ( isset($_GET['aid']) ) {
$ret = $this->kdb->query("SELECT * FROM exponates WHERE answerID=".$_GET['aid']." AND exhibitionID=".$exhib);
while ( $row = mysql_fetch_array($ret) ) {
return true;
}
}
return false;
}
public function isOwner() {
if ( isset($_GET['debug'])) {
$this->psyhvel->out("debug", "Object owner: ".$this->getUserID()." user id:". $this->user->getId());
}
if ( $this->getUserID() == $this->user->getId() ) {
return 0;
}
return 1;
}
public function useImage($iid) {
if ($iid) {
$q = "INSERT INTO psyhvel_usedimages (resourceID, imageUID) SELECT ".$this->getResourceID().", '".$iid."' FROM dual WHERE not exists (SELECT * FROM usedimages WHERE usedimages.resourceID = ".$this->getResourceID()." AND usedimages.imageUID = '".$iid."')";
$this->kdb->query($q);
}
}
public function removeUsedImages() {
$q = "DELETE FROM psyhvel_usedimages WHERE resourceID=".$this->getResourceID();
$this->kdb->query($q);
}
function getUsedImages() {
$q = "SELECT imageUID FROM psyhvel_usedimages WHERE resourceID=".$this->getResourceID();
$ret = $this->kdb->query($q);
$uids = array();
while ( $row = mysql_fetch_array($ret) ) {
$uids []= $row['imageUID'];
}
return $uids;
}
/**
* @Secured('roles' = {'authenticated'}, 'valid' = {'isOwner'})
* */
public function actionSaveSettings() {
if (isset($_POST['status'])) {
$this->setState($_POST['status']);
}
$this->setSecurity($_POST['security']);
$this->kdb->query("UPDATE psyhvel_resources SET state='".$this->getState()."', security='".$this->getSecurity()."' WHERE resourceID=".$this->getResourceID());
$_SESSION['psyhvel_notice'] = gettext("Saved");
return array('rid' => $this->getResourceID());
}
/**
* @Secured('roles' = {'authenticated'}, 'valid' = {'isOwner'})
* */
public function actionExportToWaramu() {
require_once("Waramu.php");
$ws = new Waramu();
$ws->connect();
$r = $ws->newResource($this->toWaramu());
if ( is_int($r) ) { // error
} else {
$this->setWaramuUID($r);
}
$zipcontent = "zipzipzip";
$ares = $ws->addAttachment($this->getWaramuUID(), $zipcontent, $this->getTitle().'.zip');
$ws->disconnect();
$_SESSION['psyhvel_notice'] = gettext("Resource exported to Waramu");
return array('rid' => $this->getResourceID());
}
public function toWaramu() {
$xml = '';
$xml .= '';
$xml .= '