<?php
/*
* This file is part of the Booby project.
* The booby project is located at the following location:
* http://www.nauta.be/booby/
*
* Booby - Copyright (c) 2003 - 2004 Barry Nauta
*
* The Booby project is released under the General Public License
* More detailes in the file 'gpl.html' or on the following
* website: http://www.gnu.org and look for licenses
*
* Enjoy :-)
*/
require_once ('base/util/databaseConnection.php');
require_once ('base/util/StringUtils.php');
/**
* Services abstract base class
*
* @author Barry Nauta July 2003
* @package be.nauta.booby.base.model
* @copyright
*
* Copyright (c) 2003 - 2004 Barry Nauta <br />
*
* The Booby project is released under the General Public License
* More detailes on the following
* website: <code>http://www.gnu.org</code>
* and look for licenses
*/
class Services
{
/**
* Common string utilities
* @var object
*/
var $stringUtils;
/**
* The specific queries for this sevice
* @var array
*/
var $queries;
/**
* The factory used to construct dedicated items
* @var object
*/
var $itemFactory;
/**
* Default constructor
*/
function Services ()
{
$this->stringUtils = new StringUtils ();
}
/**
* Retrieves all items for a user
* @param integer userId the identifier for the user
* @return array an array of the items for this user
*/
function getItems ($userId)
{
global $db;
$query = sprintf ($this->queries['getItems'], $userId);
$result = $db->Execute($query) or
die("GetItems: " . $db->ErrorMsg());
return $this->itemFactory->resultsetToItems ($result);
}
/**
* Retrieves all items for a user, sorted by the indicated value
* @param integer userId the identifier for the user
* @param integer parentId the identifier of the parent folder in which we would like to sort
* @param string field the field on which we would like to sort
* @return array an array of the sorted items
*/
function getSortedItems ($userId, $parentId, $field, $sortOrder)
{
global $db;
$query = sprintf ($this->queries['getSortedItems'], $userId, $parentId, $field, $sortOrder);
$result = $db->Execute($query) or
die("GetSortedItems: " . $db->ErrorMsg());
$result = $this->itemFactory->resultsetToItems ($result);
return $result;
}
/**
* Retrieves all items for a user, sorted by the indicated value
* @param integer userId the identifier for the user
* @param string field the field on which we would like to sort
* @return array an array of the sorted items
*/
function getAllSortedItems ($userId, $field, $sortOrder)
{
global $db;
$query = sprintf ($this->queries['getAllSortedItems'], $userId, $field, $sortOrder);
$result = $db->Execute($query) or
die("GetAllSortedItems: " . $db->ErrorMsg());
$result = $this->itemFactory->resultsetToItems ($result);
return $result;
}
/**
* Retrieves all items for a user, sorted by the indicated value
* @param integer userId the identifier for the user
* @param string field the field on which we would like to sort
* @return array an array of the sorted items
*/
function getPublicSortedItems ($userId, $parentId, $field, $sortOrder)
{
global $db;
$query = sprintf ($this->queries['getPublicSortedItems'], $parentId, $userId, $field, $sortOrder);
$result = $db->Execute($query) or
die("GetSortedItems: " . $db->ErrorMsg());
$result = $this->itemFactory->resultsetToItems ($result);
return $result;
}
/**
* Retrieves all public items for a user, sorted by the indicated value
* @param integer userId the identifier for the user
* @param string field the field on which we would like to sort
* @return array an array of the sorted items
* @author Michael Haussmann
*/
function getAllPublicSortedItems ($userId, $field, $sortOrder)
{
global $db;
$query = sprintf ($this->queries['getAllPublicSortedItems'], $userId, $field, $sortOrder);
$result = $db->Execute($query) or
die("GetAllSortedItems: " . $db->ErrorMsg());
$result = $this->itemFactory->resultsetToItems ($result);
return $result;
}
/**
* Returns the owner of the item
*
* @param integer itemId the identifier of the item for which
* we would like to know the owner
* @return string the owner of the item
*
* @todo make sure that not everyone can simply execute this
* function.
*/
function getItemOwner ($itemId)
{
global $db;
$query = sprintf ($this->queries['getItemOwner'], $itemId);
$result = $db->Execute($query) or
die("GetItemOwner: " .
$db->ErrorMsg() . "query: -" .
$query . "-");
return $result->fields[0];
}
/**
* Delete a item (with a specified ID) for a specific
* user. This function will recurively delete all children
* of this item if this item is a parent/folder!
*
* @param string userId the identifier for the user
* @param string itemId the identifier for the item that will
* be deleted
*/
function deleteItem ($userId, $itemId)
{
global $db;
$item = $this->getItem ($userId, $itemId);
// if we are a parent, delete the children as well....
if ($item->isParent==1)
{
$children = $this->getChildren ($userId, $itemId);
for ($i=0; $i<count($children); $i++)
{
$this->deleteItem ($userId, $children[$i]->itemId);
}
}
// now delete the specific item (children are already
// deleted if this is a parent item)
$query = sprintf ($this->queries['deleteItem'], $itemId);
$result = $db->Execute($query) or
die("DeleteItem: " .
$db->ErrorMsg() . " " . $query);
}
/**
* Adds an item for a user.
* @abstract
* @param integer userId the identifier for the user
* @param object item the item to be added
*/
function addItem ($userId, $item)
{
}
/**
* Gets a specific item for a user
*
* @param string userId the identifier for the user
* @param string itemId the identifier for the item
* @return object the item for specified user with specified itemId
*/
function getItem ($userId, $itemId)
{
global $db;
$query = sprintf ($this->queries['getItem'], $itemId);
$result = $db->Execute($query) or
die("GetItem: " .
$db->ErrorMsg() . " query: -" .
$query . "-");
$items = $this->itemFactory->resultsetToItems ($result);
if (count ($items) == 0)
{
return null;
}
else
{
return $items[0];
}
}
/**
* Gets a specific item for a user
*
* @param string userId the identifier for the user
* @param string itemId the identifier for the item
* @return object the item for specified user with specified itemId
*/
function getPublicItem ($userId, $itemId)
{
global $db;
$query = sprintf ($this->queries['getPublicItem'], $userId, $itemId);
$result = $db->Execute($query) or
die("GetItem: " .
$db->ErrorMsg() . " query: -" .
$query . "-");
$items = $this->itemFactory->resultsetToItems ($result);
if (count ($items) == 0)
{
return null;
}
else
{
// Return one item only
return $items[0];
}
}
/**
* Checks the owner of the Item
*
* @param string userId the identifier of the user
* @param string itemId the identifier of the item
*/
function checkOwner ($userId, $itemId)
{
if ($userId == null || $userId = 0)
{
return;
}
if ($this->getItemOwner ($itemId) != $userId)
{
die ("Not owner -" . $itemId . "- -" . $userId . "-");
}
}
/**
* Retrieves the children of a specified item
*
* @param string userId the identifier of the user that issues the
* request
* @param integer itemId the identifier of the item for which we would
* like to have its children
* @return array all children for specified user and itemId
*/
function getChildren ($userId, $itemId)
{
global $db;
$query = sprintf ($this->queries ['getItemChildren'], $itemId, $userId);
$result = $db->Execute ($query) or die
("Operations. GetChildren " . $db->ErrorMsg () .
" Error getting children: " . $query);
$items = $this->itemFactory->resultsetToItems($result);
if ($items == null)
{
return null;
}
else if ($items[0]->owner == $userId)
{
return $items;
}
else
{
die ("Not owner?");
}
}
/**
* Retrieves the children of a specified item
*
* @param string userId the identifier of the user that issues the
* request
* @param integer itemId the identifier of the item for which we would
* like to have its children
* @return array all children for specified user and itemId
*/
function getPublicChildren ($userId, $itemId)
{
global $db;
$query = sprintf ($this->queries ['getPublicItemChildren'], $itemId, $userId);
$result = $db->Execute ($query) or die
("Operations. GetPublicChildren " . $db->ErrorMsg () .
" Error getting children: " . $query);
$items = $this->itemFactory->resultsetToItems($result);
return $items;
}
/**
* Retrieves the children of a specified item, but only the children that are parents themselves
*
* @param string userId the identifier of the user that issues the
* request
* @param integer itemId the identifier of the item for which we would
* like to have its children
* @return array all children for specified user and itemId
*/
function getChildrenThatAreParent ($userId, $itemId)
{
global $db;
$query = sprintf ($this->queries ['getItemChildrenThatAreParent'], $itemId, $userId);
$result = $db->Execute ($query) or die
("Operations. GetChildrenThatAreParent " . $db->ErrorMsg () .
" Error getting children: " . $query);
$items = $this->itemFactory->resultsetToItems($result);
if ($items == null)
{
return null;
}
else if ($items[0]->owner == $userId)
{
return $items;
}
else
{
die ("Not owner?");
}
}
/**
* Modifies an item.
* @abstract
* @param string userId the identifier for the user
* @param object item the (modified) item (which will still be identified
* by its original itemId)
*/
function modifyItem ($userId, $item)
{
}
/**
* Moves an item for a user to a new parent
*
* @param string userId the identifier for the user who issues
* the request
* @param integer itemId the identifier for the item that is going to
* be moved
* @param integer parentId the new parentId for the item
*/
function moveItem ($userId, $itemId, $parentId)
{
$item = $this->getItem ($userId, $itemId);
$item->parentId = $parentId;
$this->modifyItem ($userId, $item);
}
/**
* Search for items for a user
*
* @param string userId the identifier for the user for which we
* would like to search for items
* @param string field the field on which we would like to search
* @param string value the value for which we would like to search
*
* @return array all items that match the given search criteria
*/
function searchItems ($userId, $field, $value)
{
global $db;
$query = sprintf ($this->queries ['searchItems'], $field, $value, $userId);
$result = $db->Execute ($query) or die
("Operations. Search " . $db->ErrorMsg () .
" " . $query);
$items = $this->itemFactory->resultsetToItems($result);
return $items;
}
/**
* Search for items for a user
*
* @param string userId the identifier for the user for which we
* would like to search for items
* @param string field the field on which we would like to search
* @param string value the value for which we would like to search
*
* @return array all items that match the given search criteria
*/
function searchPublicItems ($userId, $field, $value)
{
global $db;
$query = sprintf ($this->queries ['searchPublicItems'], $field, $value, $userId);
$result = $db->Execute ($query) or die
("Operations. Search " . $db->ErrorMsg () .
" " . $query);
$items = $this->itemFactory->resultsetToItems($result);
return $items;
}
/**
* Updates the count of the number of times this item
* has been visited
*
* @param string userId the user that requests the update
* @param integer itemId the item for which the count needs to be
* increased
*/
function updateVisiteCount ($userId, $itemId)
{
global $db;
$query = sprintf ($this->queries['updateItemVisitCount'], $itemId);
$result = $db->Execute($query)
or die ("Update visit count failed ".
$db->ErrorMsg () . " " . $query);
}
/**
* Returns the global username (session variable)
* @return string the username (aka loginname)
*/
function getUserName ()
{
return $_SESSION['boobyUsername'];
}
/**
* Retrieves the public children of a specified item,
* but only the children that are parents themselves
*
* @param string userId the identifier of the user that issues the
* request
* @param integer itemId the identifier of the item for which we would
* like to have its children
* @return array all children for specified user and itemId
* @author Michael Haussmann
*/
function getPublicChildrenThatAreParent ($userId, $itemId)
{
global $db;
$query = sprintf ($this->queries ['getPublicItemChildrenThatAreParent'], $itemId, $userId);
$result = $db->Execute ($query) or die
("Operations. GetPublicChildrenThatAreParent " . $db->ErrorMsg () .
" Error getting children: " . $query);
$items = $this->itemFactory->resultsetToItems($result);
if ($items == null)
{
return null;
}
else if ($items[0]->owner == $userId)
{
return $items;
}
else
{
die ("Not owner?");
}
}
/**
* Returns the parent item of an item.
* To get all the parents, see getAncestors
*
* @param object item the item for which we lookup the parent
* @return object item the item which is the parent, or null if it has no other parent than root.
*
* @see getAncestors
* @author Michael Haussmann
*/
function getParent ($item)
{
global $db;
$query = sprintf ($this->queries['getParent'], $item->itemId);
$result = $db->Execute($query)
or die ("getParent failed ".
$db->ErrorMsg () . " " . $query);
$items = $this->itemFactory->resultsetToItems ($result);
if (count ($items) == 0)
{
return null;
}
else
{
return $items[0];
}
}
/**
* Returns an array containing all the ancestors of the given item,
* starting with the item itself and upwards.
* (the items are returned by value).
*
* Example result :
* Array
* (
* [0] => me
* [1] => father
* [2] => grandfather
* )
*
* @author Michael, inspired by tim at correctclick dot com (PHP Manual) 05-Apr-2003 07:48
*
* Modified by barry: ancestors of me should not contain me myself and I, I guess...? :-)
* removed the 'me' from the array
* @todo check double function
*/
function getAncestors ($item)
{
for ($items[] = $item; $item = $this->getParent ($item); $items[] = $item);
$me = array_shift ($items);
return $items;
}
/**
* Retrieves the number of non public children of a specified item.
* Only the children that are not parents are counted.
* The count is not recursive.
*
* @param string userId the identifier of the user that issues the
* request
* @param integer itemId the identifier of the item for which we would
* like to have its children
* @return integer the result
* @author Michael Haussmann
* @static
*/
function getChildrenCount ($item)
{
global $db;
static $queries = null; // avoid unecessary multiple inclusions
if($queries == null)
{
$type = strtolower($item->type);
include ("plugins/".$type."s/sql/".$type."Queries.php");
}
$query = sprintf ($queries ['getChildrenCount'], $item->itemId, $item->owner);
$result = $db->GetOne($query);
if(!$result) return 0;
else return $result;
}
/**
* Retrieves the number of public children of a specified item.
* Only the children that are not parents are counted.
* The count is not recursive.
*
* @param string userId the identifier of the user that issues the
* request
* @param integer itemId the identifier of the item for which we would
* like to have its children
* @return integer the result
* @author Michael Haussmann
* @static
*/
function getPublicChildrenCount ($item)
{
global $db;
static $queries = null; // avoid unecessary multiple inclusions
if($queries == null)
{
$type = strtolower($item->type);
include ("plugins/".$type."s/sql/".$type."Queries.php");
}
$query = sprintf ($queries ['getPublicChildrenCount'], $item->itemId, $item->owner);
$result = $db->getone($query);
if(!$result) return 0;
else return $result;
}
}
?>