.
*
* You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco,
* California 94120-7775, or email info@knowledgetree.com.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "Powered by
* KnowledgeTree" logo and retain the original copyright notice. If the display of the
* logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices
* must display the words "Powered by KnowledgeTree" and retain the original
* copyright notice.
* Contributor( s): ______________________________________
*/
require_once(KT_LIB_DIR . '/security/Permission.inc');
require_once(KT_LIB_DIR . '/users/User.inc');
require_once(KT_LIB_DIR . '/documentmanagement/Document.inc');
require_once(KT_LIB_DIR . '/documentmanagement/DocumentField.inc');
require_once(KT_LIB_DIR . '/documentmanagement/DocumentType.inc');
require_once(KT_LIB_DIR . '/documentmanagement/MetaData.inc');
require_once(KT_LIB_DIR . '/foldermanagement/Folder.inc');
require_once(KT_LIB_DIR . '/workflow/workflowstate.inc.php');
require_once(KT_LIB_DIR . '/workflow/workflow.inc.php');
require_once(KT_LIB_DIR . '/browse/criteriaregistry.php');
require_once(KT_LIB_DIR . "/util/sanitize.inc");
class BrowseCriterion {
var $sDisplay;
var $aLookup = null;
var $bFolderCriterion = false;
var $aOptions = array();
var $iID;
var $bString = false;
var $sSearchTable = "D";
var $bVisible = true;
var $bContains = false;
var $bHandleNot = true;
var $sDocumentField = null;
var $sSortField = null;
var $sNamespace = null;
function BrowseCriterion() {
$this->sDisplay = '';
}
function headerDisplay () {
return $this->sDisplay;
}
// for final display
function documentDisplay ($oDocument) {
return $this->sDisplay;
}
function folderDisplay ($oDocument) {
return " ";
}
// for parameter display
function baseParameterDisplay() {
$sDisp = sprintf("%s: ", $this->sDisplay);
$bNot = KTUtil::arrayGet($aData, $this->getWidgetBase().'_not', null);
if($bNot !== null) {
if((bool)$bNot) { $sDisp .= _kt('NOT'); }
}
return $sDisp;
}
function parameterDisplay($aData) {
return sprintf("%s %s", $this->baseParameterDisplay(), htmlentities($aData[$this->getWidgetBase()],ENT_QUOTES, 'UTF-8'));
}
function folderQuery ($iParentID, $sSortDirection) {
global $default;
$sFolderQuery = "SELECT f.id FROM $default->folders_table AS f ";/*ok*/
if (!$this->bFolderCriterion) {
$sFolderQuery .= "WHERE parent_id = ? ORDER BY f.name asc";
$aParams = array($iParentID);
return array($sFolderQuery, $aParams);
}
if (!is_null($this->aLookup)) {
$sFolderQuery .= "INNER JOIN " . $this->aLookup["table"] . " lt ON f.$this->sDocumentField = lt.id WHERE parent_id = ?";
$sFolderQuery .= " ORDER BY lt." . $this->aLookup["field"] . " " . $sSortDirection;
$aParams = array($iParentID);
return array($sFolderQuery, $aParams);
}
$sFolderQuery .= "WHERE parent_id = ? ORDER BY " . $this->getFolderSortField() . " " . $sSortDirection;
$aParams = array($iParentID);
return array($sFolderQuery, $aParams);
}
function documentQuery ($iFolderID, $sSortDirection) {
global $default;
// create query to retrieve documents in this folder
$documentQuery = "SELECT d.id as id FROM $default->documents_table AS d ";/*wc*/
if (!is_null($this->aLookup)) {
$sDocumentJoinField = $this->getDocumentField();
$documentQuery .= "INNER JOIN " . $this->aLookup["table"] . " lt ON ";
if (array_key_exists('joinColumn', $this->aLookup)) {
$documentQuery .= "d.$sDocumentJoinField" . " = lt." . $this->aLookup["joinColumn"];
} else {
$documentQuery .= "d.$sDocumentJoinField" . " = lt.id";
}
}
$documentQuery .= " WHERE d.folder_id = ? ";
$aParams = array($iFolderID);
if (!is_null($this->aLookup)) {
if (array_key_exists("whereClause", $this->aLookup)) {
$documentQuery .= "AND lt." . $this->aLookup["whereClause"] . " ";
}
$documentQuery .= "ORDER BY lt." . $this->aLookup["field"] . " " . $sSortDirection;
} else {
$sDocumentJoinField = $this->getDocumentField();
// $sSortField = $this->getSortField();
$documentQuery .= "ORDER BY " . $this->getSortField() . " " . $sSortDirection;
}
return array($documentQuery, $aParams);
}
function getDocumentField () {
return $this->sDocumentField;
}
function getSortField () {
return $this->sSortField;
}
function getFolderSortField () {
return $this->sSortField;
}
function getSearchField () {
return $this->sDocumentField;
}
function getLookup () {
return $this->aLookup;
}
function getName() {
return sanitizeForSQLtoHTML($this->sDocumentField);
}
function getID() {
return $this->iID;
}
function getNameSpace() {
return $this->sNamespace;
}
function setOptions($aOptions) {
$this->aOptions = $aOptions;
}
function searchDisplay($aRequest) {
return "
" . $this->headerDisplay() . ":
" . $this->searchWidget($aRequest) . "
\n";
}
function searchWidget ($aRequest, $aPreValue = null) {
if ($aPreValue != null) {
// !#@&)*( (*&!@# *(&@NOT (*&!@#
$k = array_keys($aPreValue);
$k = $this->getWidgetBase();
if(array_key_exists($k, $aPreValue)) {
$preval = $aPreValue[$k];
}
return $this->getNotWidget($aPreValue) . "getWidgetBase() . "\" value=\"" . $preval . "\"/>";
} else {
return $this->getNotWidget($aPreValue) . "getWidgetBase() . "\" />";
}
}
function getNotWidget($aPreValue=null) {
if (!$this->bHandleNot) { return ''; }
// not perfect, but acceptable.
$form_name = $this->getWidgetBase() . '_not';
$pos_select = '';
$neg_select = '';
if (is_null($aPreValue)) {
$is_positive = true;
} else {
if(array_key_exists($form_name, $aPreValue)) {
$preval = KTUtil::arrayGet($aPreValue, $form_name, "0"); // by default, use "is" not "is not"
}
$is_positive = ($preval == "0"); // 0 or empty or similar.
}
if ($is_positive) {
$pos_select = ' selected="true"';
} else {
$neg_select = ' selected="true"';
}
if (!$this->bContains) {
$not_string = _kt('is not');
$is_string = _kt('is');
} else {
$not_string = _kt('does not contain');
$is_string = _kt('contains');
}
$widget = sprintf(' ', $form_name, $pos_select, $is_string, $neg_select, $not_string);
return $widget;
}
function getWidgetBase () {
//return strtr($this->getNamespace(), '-', '_');
return $this->getNamespace();
}
function getSearchTable() {
return $this->sSearchTable;
}
function searchSQL ($aRequest, $handle_not = true) {
$val = null;
if ($this->bString) {
$val = array($this->getSearchTable() . "." . $this->getSearchField() . " LIKE '%!%'", array(DBUtil::escapeSimple($aRequest[$this->getWidgetBase()])));
} else {
$val = array($this->getSearchTable() . "." . $this->getSearchField() . " = ?", array($aRequest[$this->getWidgetBase()]));
}
// handle the boolean "not" stuff UNLESS our caller is doing so already.
if ($handle_not) {
$want_invert = KTUtil::arrayGet($aRequest, $this->getWidgetBase() . '_not');
if (is_null($want_invert) || ($want_invert == "0")) { // use explicit "0" check
return $val;
} else {
$val[0] = '(NOT (' . $val[0] . '))';
}
}
return $val;
}
function searchJoinSQL () {
return null;
}
}
class NameCriterion extends BrowseCriterion {
var $bFolderCriterion = true;
var $bString = true;
var $sSearchTable = "DC";
var $bContains = true;
var $sDocumentField = 'filename';
var $sSortField = 'filename';
var $sNamespace = 'ktcore.criteria.name';
function NameCriterion() {
$this->sDisplay = _kt('Document Filename');
}
function documentDisplay ($oDocument) {
$aOptions = $this->aOptions;
if (array_key_exists('displayFullPath', $aOptions)) {
$bDisplayFullPath = $aOptions['displayFullPath'];
} else {
$bDisplayFullPath = false;
}
if (array_key_exists('templateBrowsing', $aOptions)) {
$bTemplateBrowsing = $aOptions['templateBrowsing'];
} else {
$bTemplateBrowsing = false;
}
if ($bTemplateBrowsing) {
return displayDocumentLinkForTemplateBrowsing($oDocument, $bDisplayFullPath);
} else {
return displayDocumentLink($oDocument, $bDisplayFullPath);
}
}
function folderDisplay($oFolder) {
return displayFolderLink($oFolder);
}
function getFolderSortField() {
return 'name';
}
}
class IDCriterion extends BrowseCriterion {
var $bFolderCriterion = true;
var $sDocumentField = 'id';
var $sSortField = 'id';
var $sNamespace = 'ktcore.criteria.id';
function IDCriterion() {
$this->sDisplay = _kt('Document ID');
}
function documentDisplay ($oDocument) {
return $oDocument->getID();
}
function folderDisplay($oFolder) {
return $oFolder->getID();
}
}
class TitleCriterion extends BrowseCriterion {
var $bFolderCriterion = true;
var $bString = true;
var $sSearchTable = "DM";
var $bContains = true;
var $sDocumentField = 'name';
var $sSortField = 'name';
var $sNamespace = 'ktcore.criteria.title';
function TitleCriterion() {
$this->sDisplay = _kt('Document Title');
}
function documentDisplay ($oDocument) {
return $oDocument->getName();
}
function folderDisplay($oFolder) {
return $oFolder->getDescription();
}
function getFolderSortField() {
return 'description';
}
}
class CreatorCriterion extends BrowseCriterion {
var $sSearchTable = "D";
var $bFolderCriterion = true;
var $aLookup = array(
"table" => "users",
"field" => "name",
);
var $sDocumentField = 'creator_id';
var $sSortField = 'creator_id';
var $sNamespace = 'ktcore.criteria.creator';
function CreatorCriterion() {
$this->sDisplay = _kt('Creator');
}
function documentDisplay ($oDocument) {
$oCreator = User::get($oDocument->getCreatorID());
if ($oCreator) {
return $oCreator->getName();
}
return " ";
}
function folderDisplay($oFolder) {
return $this->documentDisplay($oFolder);
}
function parameterDisplay($aData) {
$sBase = $this->baseParameterDisplay();
$oUser =& User::get($aData[$this->getWidgetBase()]);
if(PEAR::isError($oUser)) {
return $sBase . 'unknown user';
}
return $sBase . $oUser->getName();
}
function searchWidget ($aRequest, $aPreValue = null) {
$preval = null;
if ($aPreValue != null) {
// !#@&)*( (*&!@# *(&@NOT (*&!@#
$k = array_keys($aPreValue);
$k = $this->getWidgetBase();
$preval = $aPreValue[$k];
}
$sRet = $this->getNotWidget($aPreValue) . "\n";
return $sRet;
}
}
class DateCreatedCriterion extends BrowseCriterion {
var $sSearchTable = "D";
var $sDocumentField = 'created';
var $sSortField = 'created';
var $sNamespace = 'ktcore.criteria.datecreated';
function DateCreatedCriterion() {
$this->sDisplay = _kt('Date Created');
}
function documentDisplay ($oDocument) {
return $oDocument->getCreatedDateTime();
}
function parameterDisplay($aData) {
$sDisp = $this->baseParameterDisplay();
$sStart = KTUtil::arrayGet($aData, $this->getWidgetBase() . '_start', false);
$sEnd = KTUtil::arrayGet($aData, $this->getWidgetBase() . '_end', false);
if($sStart) {
$sDisp .= _kt('after ') .$sStart;
}
if($sStart && $sEnd) {
$sDisp .= _kt(' and ');
}
if($sEnd) {
$sDisp .= _kt('before ') .$sEnd;
}
return $sDisp;
}
function getName() {
return "created";
}
function searchWidget ($aRequest, $aPreValue = null) {
global $default;
// IMPORTANT: this requires the presence of kt3-calendar.js
$sStartWidget = $this->getWidgetBase() . "_start";
$sEndWidget = $this->getWidgetBase() . "_end";
/* // legacy code.
$sToRender = "After date: ";
$sToRender .= " graphicsUrl/calendar/calendar.gif\" name=\"imgCalendar\" width=\"34\" height=\"21\" border=\"0\" alt=\"\">";
$sToRender .= " Before date: ";
$sToRender .= " graphicsUrl/calendar/calendar.gif\" name=\"imgCalendar\" width=\"34\" height=\"21\" border=\"0\" alt=\"\">";
*/
$sToRender = $this->getNotWidget($aPreValue);
$sToRender .= _kt('after') . ': ' . $aPreValue[$sStartWidget] . ' and/or ';
$sToRender .= _kt('before') . ': ' . $aPreValue[$sEndWidget] . ' ';
return $sToRender;
}
function searchSQL ($aRequest) {
$sStartWidget = $this->getWidgetBase() . "_start";
$sEndWidget = $this->getWidgetBase() . "_end";
// XXX: DateTimeFixup: Should be more intelligent with handling
// end date - should be end of day on that day.
if (!array_key_exists($this->getWidgetBase() . "_start", $aRequest)) {
$sStart = null;
} else {
$sStart = $aRequest[$this->getWidgetBase() . "_start"];
}
if (!array_key_exists($this->getWidgetBase() . "_end", $aRequest)) {
$sEnd = null;
} else {
$sEnd = $aRequest[$this->getWidgetBase() . "_end"];
}
$val = null;
if ($sStart && $sEnd) {
$val = array($this->getSearchTable() . "." . $this->getSearchField() . " BETWEEN ? AND ?", array($sStart, $sEnd));
} else if ($sStart) {
$val = array($this->getSearchTable() . "." . $this->getSearchField() . " > ?", array($sStart));
} else if ($sEnd) {
$val = array($this->getSearchTable() . "." . $this->getSearchField() . " < ?", array($sEnd));
} else {
return null;
}
// handle the boolean "not" stuff.
$want_invert = KTUtil::arrayGet($aRequest, $this->getWidgetBase() . '_not');
if (is_null($want_invert) || ($want_invert == "0")) {
return $val;
} else {
$val[0] = '(NOT (' . $val[0] . '))';
}
// finally
return $val;
}
}
class DocumentTypeCriterion extends BrowseCriterion {
var $sSearchTable = "DM";
var $aLookup = array(
"table" => "document_types_lookup",
"field" => "name"
);
var $sDocumentField = 'document_type_id';
var $sSortField = 'document_type_id';
var $sNamespace = 'ktcore.criteria.documenttype';
function DocumentTypeCriterion() {
$this->sDisplay = _kt('Document Type');
}
function documentDisplay ($oDocument) {
$oDocumentType = DocumentType::get($oDocument->getDocumentTypeID());
if ($oDocumentType) {
return $oDocumentType->getName();
}
return " ";
}
function searchWidget ($aRequest, $aPreValue = null) {
$preval = null;
if ($aPreValue != null) {
// !#@&)*( (*&!@# *(&@NOT (*&!@#
$k = array_keys($aPreValue);
$k = $this->getWidgetBase();
$preval = $aPreValue[$k];
}
$sRet = $this->getNotWidget($aPreValue);
$sRet .= "\n";
return $sRet;
}
}
class GenericMetadataCriterion extends BrowseCriterion {
var $aLookup = array(
"table" => "document_fields_link",
"field" => "value",
"joinColumn" => "document_id",
);
var $bString = true;
var $sDocumentField = null;
var $sSortField = null;
var $sNamespace = 'ktcore.criteria.generic';
function initialize($sDisplay, $sDocumentField, $sSortField, $iID, $sNamespace) {
$this->sDisplay = $sDisplay;
$this->sDocumentField = $sDocumentField;
$this->sSortField = $sSortField;
$this->iID = $iID;
$this->aLookup['whereClause'] = 'document_field_id = ' . $iID;
$this->oField = DocumentField::get($iID);
$this->sNamespace = $sNamespace;
$this->sSearchTable = "DFL" . $iID;
}
function getID() {
return $this->iID;
}
function documentDisplay ($oDocument) {
global $default;
$sQuery = "SELECT DFL.value as value " .
"FROM $default->document_fields_link_table AS DFL " .
"WHERE DFL.metadata_version_id = ? " .
"AND DFL.document_field_id = ?";
$aParams = array($oDocument->getMetadataVersionId(), $this->getID());
$res = DBUtil::getOneResultKey(array($sQuery, $aParams), 'value');
if (PEAR::isError($res)) {
// WARN: Add log warning
return " ";
}
return $res;
}
function getName() {
global $default;
$aQuery = array("SELECT name FROM $default->document_fields_table WHERE id = ?", array($this->getID())); /*ok*/
return "gmd_" . DBUtil::getOneResultKey($aQuery, 'name');
}
function getSearchField () {
return $this->aLookup['field'];
}
function searchWidget ($aRequest, $aPreValue = null) {
$preval = null;
if ($aPreValue != null) {
// !#@&)*( (*&!@# *(&@NOT (*&!@#
$k = array_keys($aPreValue);
$k = $this->getWidgetBase();
$preval = $aPreValue[$k];
}
// If there's no lookup, just use the standard text input
if ($this->oField->getHasLookup() == false && $this->oField->getHasInetLookup() == false) {
$this->bContains = true; // contains
return parent::searchWidget($aRequest, $aPreValue);
}
if($this->oField->getHasInetLookup() == true)
{
//$this->bContains = true;
$this->bString = false;
$sRet = $this->getNotWidget($aPreValue);
$innerJoin = $aPreValue[$this->getWidgetBase()."_join"];
$andSelected = "";
$orSelected = "";
if($innerJoin == "AND")
{
$andSelected = "selected=\"selected\"";
}
elseif($innerJoin == "OR")
{
$orSelected = "selected=\"selected\"";
}
$sRet .= "\n";
if($this->oField->getInetLookupType() == "multiwithlist")
{
$sRet .= "\n";
}
elseif($this->oField->getInetLookupType() == "multiwithcheckboxes")
{
$sRet .= "