diff --git a/lib/documentmanagement/DocumentLink.inc b/lib/documentmanagement/DocumentLink.inc index a761d43..8ddf999 100644 --- a/lib/documentmanagement/DocumentLink.inc +++ b/lib/documentmanagement/DocumentLink.inc @@ -31,94 +31,79 @@ class DocumentLink extends KTEntity { /** primary key value */ var $iId; /** primary key of user responsible for creating document */ - var $iParentDocumentID; + var $iParentDocumentId; /** document title name */ - var $iChildDocumentID; + var $iChildDocumentId; /** type of link */ - var $iLinkTypeID; + var $iLinkTypeId; + + + var $_aFieldToSelect = array( + 'iId' => 'id', + 'iParentDocumentId' => 'parent_document_id', + 'iChildDocumentId' => 'child_document_id', + 'iLinkTypeId' => 'link_type_id', + ); + /** * Default constructor * * @param Parent document primary key * @param Child document primary key + * @param Link type primary key * */ - function DocumentLink($iNewParentDocumentID, $iNewChildDocumentID, $iLinkTypeID) { + function DocumentLink($iNewParentDocumentID = null, $iNewChildDocumentID = null, $iLinkTypeId = null) { //object not created yet global $default; $this->iId = -1; - $this->iParentDocumentID = $iNewParentDocumentID; - $this->iChildDocumentID = $iNewChildDocumentID; - $this->iLinkTypeID = $iLinkTypeID; + $this->iParentDocumentId = $iNewParentDocumentID; + $this->iChildDocumentId = $iNewChildDocumentID; + $this->iLinkTypeId = $iLinkTypeId; } - /** - * Get the document field's primary key value - * - * @return int document field's primary key value - * - */ - function getID() { - return $this->iId; - } + function getID() { return $this->iId; } + function getParentDocumentID() { return $this->iParentDocumentId; } + function setParentDocumentID($iNewValue) { $this->iParentDocumentId = $iNewValue; } + function getChildDocumentID() { return $this->iChildDocumentId; } + function setChildDocumentID($iNewValue) { $this->iChildDocumentId = $iNewValue; } + function getLinkTypeID() { return $this->iLinkTypeId; } + function setLinkTypeID($iNewValue) { $this->iLinkTypeId = $iNewValue; } /** - * Get the primary key of the parent document + * Helper getters */ - function getParentDocumentID() { - return $this->iParentDocumentID; - } - + /** - * Set the primary key of the parent document - * - * @param Primary key of parent document - * + * get parent document */ - function setParentDocumentID($iNewValue) { - $this->iParentDocumentID = $iNewValue; - } - + function & getParentDocument() { + return Document::get($this->getParentDocumentId()); + } + /** - * Get the child document's primary key - * - * @return int primary key of child document - * + * get child document */ - function getChildDocumentID() { - return $this->iChildDocumentID; - } - + function & getChildDocument() { + $oDocument = Document::get($this->getChildDocumentId()); + return $oDocument; + } + /** - * Set the child document's primary key - * - * @param Primary key of child document - * + * get link type */ - function setChildDocumentID($iNewValue) { - $this->iChildDocumentID = $iNewValue; - } + function & getLinkType() { + return LinkType::get($this->getLinkTypeId()); + } - /** - * Get the primary key of the link type - */ - function getLinkTypeID() { - return $this->iLinkTypeID; - } - /** - * Set the primary key of the link type - */ - function setLinkTypeID($iNewValue) { - $this->iLinkTypeID = $iNewValue; - } function _fieldValues () { return array( - 'parent_document_id' => $this->iParentDocumentID, - 'child_document_id' => $this->iChildDocumentID, - 'link_type_id' => $this->iLinkTypeID, + 'parent_document_id' => $this->iParentDocumentId, + 'child_document_id' => $this->iChildDocumentId, + 'link_type_id' => $this->iLinkTypeId, ); } @@ -127,16 +112,11 @@ class DocumentLink extends KTEntity { return $default->document_link_table; } - /** - * Static function. - * Given a document_fields primary key it will create a - * DocumentFields object and populate it with the - * corresponding database values - * - * @return DocumentField populated DocumentField object on successful query, false otherwise and set $_SESSION["errorMessage"] - */ + + // static boilerplate function & get($iDocumentLinkID) { global $default; + $sql = $default->db; $result = $sql->query(array("SELECT * FROM $default->document_link_table WHERE id = ?", $iDocumentLinkID));/*ok*/ if ($result) { @@ -151,5 +131,34 @@ class DocumentLink extends KTEntity { $_SESSION["errorMessage"] = $lang_err_database; return false; } + function getList($sWhereClause = null) { + return KTEntityUtil::getList2('DocumentLink', $sWhereClause); + } + function &createFromArray($aArray) { return KTEntityUtil::createFromArray('DocumentLink', $aArray); } + + /** + * Static function + * Get a list of DocumentLinks where iDocumentId is the parent + * + * @param Integer Document ID of parent + * + * @return Array array of DocumentLink objects, false otherwise. + */ + function getLinksFromDocument($iDocumentId) { + return KTEntityUtil::getList2('DocumentLink', sprintf("parent_document_id = %d", $iDocumentId)); + } + + /** + * Static function + * Get a list of DocumentLinks where iDocumentId is the child + * + * @param Integer Document ID of child + * + * @return Array array of DocumentLink objects, false otherwise. + */ + function getLinksToDocument($iDocumentId) { + return KTEntityUtil::getList2('DocumentLink', sprintf("child_document_id = %d", $iDocumentId)); + } } + ?> diff --git a/plugins/ktcore/admin/documentLinks.php b/plugins/ktcore/admin/documentLinks.php index aae3cf3..3c5c5e7 100644 --- a/plugins/ktcore/admin/documentLinks.php +++ b/plugins/ktcore/admin/documentLinks.php @@ -139,6 +139,11 @@ class KTDocLinkAdminDispatcher extends KTAdminDispatcher { $count = 0; foreach ($types_to_delete as $link_id) { $oLinkType = LinkType::get($link_id); + + foreach(DocumentLink::getList(sprintf("link_type_id = %d", $link_id)) as $oLink) { + $oLink->delete(); + } + $oLinkType->delete(); // technically, this is a bad thing $count += 1; } diff --git a/plugins/ktstandard/KTDocumentLinks.php b/plugins/ktstandard/KTDocumentLinks.php new file mode 100644 index 0000000..d612df9 --- /dev/null +++ b/plugins/ktstandard/KTDocumentLinks.php @@ -0,0 +1,304 @@ +aOptions, "folderurl", ""); + $parentDocumentId = KTUtil::arrayGet($_REQUEST, 'fDocumentId'); + return sprintf('%s?action=type_select&fDocumentId=%d&fTargetDocumentId=%d', $baseurl, $parentDocumentId, $aDataRow["document"]->getId()); + } + + function buildFolderLink($aDataRow) { + $baseurl = KTUtil::arrayGet($this->aOptions, "folderurl", ""); + $kt_path_info = KTUtil::arrayGet($_REQUEST, 'kt_path_info'); + $parentDocumentId = KTUtil::arrayGet($_REQUEST, 'fDocumentId'); + + if (empty($kt_path_info)) { + return sprintf('%s?action=new&fDocumentId=%d&fFolderId=%d', $baseurl, $parentDocumentId, $aDataRow["folder"]->getId()); + } else { + return sprintf('%s?kt_path_info=%s&action=new&ftDocumentId=%d&fFolderId=%d', $baseurl, $kt_path_info, $parentDocumentId, $aDataRow["folder"]->getId()); + } + } +} + + + + +class KTDocumentLinks extends KTPlugin { + var $sNamespace = "ktstandard.documentlinks.plugin"; + + function setup() { + $this->registerAction('documentaction', 'KTDocumentLinkAction', 'ktcore.actions.document.link'); + } +} + +class KTDocumentLinkAction extends KTDocumentAction { + var $sDisplayName = 'Links'; + var $sName = 'ktcore.actions.document.link'; + + // display existing links + function do_main() { + $oTemplate =& $this->oValidator->validateTemplate('ktstandard/action/document_links'); + $this->oPage->setBreadcrumbDetails(_("Links")); + $this->oPage->setTitle(_("Links")); + + $oDocument = Document::get( + KTUtil::arrayGet($_REQUEST, 'fDocumentId', 0) + ); + + $oReadPermission =& KTPermission::getByName('ktcore.permissions.read'); + $oWritePermission =& KTPermission::getByName('ktcore.permissions.write'); + + + $aTemplateData = array( + 'context' => $this, + 'links_from' => DocumentLink::getLinksFromDocument($oDocument->getId()), + 'links_to' => DocumentLink::getLinksToDocument($oDocument->getId()), + 'read_permission' => KTPermissionUtil::userHasPermissionOnItem($this->oUser, $oReadPermission, $this->oDocument), + 'write_permission' => KTPermissionUtil::userHasPermissionOnItem($this->oUser, $oWritePermission, $this->oDocument), + ); + + + return $oTemplate->render($aTemplateData); + } + + + + + // select a target for the link + function do_new() { + $this->oPage->setBreadcrumbDetails(_("New Link")); + $this->oPage->setTitle(_("New Link")); + + $oPermission =& KTPermission::getByName('ktcore.permissions.write'); + if (PEAR::isError($oPermission) || + !KTPermissionUtil::userHasPermissionOnItem($this->oUser, $oPermission, $this->oDocument)) { + $this->errorRedirectToMain(_('You do not have sufficient permissions to add a document link'), sprintf("fDocumentId=%d", $this->oDocument->getId())); + exit(0); + } + + $oParentDocument =& $this->oDocument; + + if (PEAR::isError($oParentDocument)) { + $this->errorRedirectToMain(_('Invalid parent document selected.')); + exit(0); + } + + $oFolder = Folder::get(KTUtil::arrayGet($_REQUEST, 'fFolderId', $oParentDocument->getFolderID())); + if (PEAR::isError($oFolder) || ($oFolder == false)) { + $this->errorRedirectToMain(_('Invalid folder selected.')); + exit(0); + } + $iFolderId = $oFolder->getId(); + + // Setup the collection for move display. + + $collection = new DocumentCollection(); + $collection->addColumn(new KTDocumentLinkTitle("Target Documents","title")); + + $qObj = new BrowseQuery($iFolderId); + $collection->setQueryObject($qObj); + + $batchPage = (int) KTUtil::arrayGet($_REQUEST, "page", 0); + $batchSize = 20; + + $resultURL = sprintf("?action=new&fDocumentId=%d", $oParentDocument->getId()); + $collection->setBatching($resultURL, $batchPage, $batchSize); + + // ordering. (direction and column) + $displayOrder = KTUtil::arrayGet($_REQUEST, 'sort_order', "asc"); + if ($displayOrder !== "asc") { $displayOrder = "desc"; } + $displayControl = KTUtil::arrayGet($_REQUEST, 'sort_on', "title"); + + $collection->setSorting($displayControl, $displayOrder); + + $collection->getResults(); + + $aBreadcrumbs = array(); + $folder_path_names = $oFolder->getPathArray(); + $folder_path_ids = explode(',', $oFolder->getParentFolderIds()); + + if ($folder_path_ids[0] == 0) { + $folder_path_ids = array(); + } + $folder_path_ids[] = $oFolder->getId(); + + foreach (range(0, count($folder_path_ids) - 1) as $index) { + $id = $folder_path_ids[$index]; + $url = sprintf("?action=new&fDocumentId=%d&fFolderId=%d", $oParentDocument->getId(), $id); + $aBreadcrumbs[] = array("url" => $url, "name" => $folder_path_names[$index]); + } + + $aTemplateData = array( + 'context' => $this, + 'folder' => $oFolder, + 'breadcrumbs' => $aBreadcrumbs, + 'collection' => $collection, + 'collection_breadcrumbs' => $aBreadcrumbs, + 'link_types' => LinkType::getList("id > 0"), + ); + + $oTemplate =& $this->oValidator->validateTemplate('ktstandard/action/link'); + return $oTemplate->render($aTemplateData); + } + + // select a type for the link + function do_type_select() { + $this->oPage->setBreadcrumbDetails(_("link")); + + $oParentDocument = Document::get(KTUtil::arrayGet($_REQUEST, 'fDocumentId')); + if (PEAR::isError($oParentDocument)) { + $this->errorRedirectToMain(_('Invalid parent document selected.')); + exit(0); + } + + $oTargetDocument = Document::get(KTUtil::arrayGet($_REQUEST, 'fTargetDocumentId')); + if (PEAR::isError($oTargetDocument)) { + $this->errorRedirectToMain(_('Invalid target document selected.')); + exit(0); + } + + + // form fields + $aFields = array(); + + $aVocab = array(); + foreach(LinkType::getList("id > 0") as $oLinkType) { + $aVocab[$oLinkType->getID()] = $oLinkType->getName(); + } + + $aOptions = array('vocab' => $aVocab); + $aFields[] = new KTLookupWidget( + _('Link Type'), + _('The type of link you wish to use'), + 'fLinkTypeId', + null, + $this->oPage, + true, + null, + null, + $aOptions); + + $aTemplateData = array( + 'context' => $this, + 'parent_id' => $oParentDocument->getId(), + 'target_id' => $oTargetDocument->getId(), + 'fields' => $aFields, + ); + + $oTemplate =& $this->oValidator->validateTemplate('ktstandard/action/link_type_select'); + return $oTemplate->render($aTemplateData); + + + } + + + + // make the link + function do_make_link() { + $this->oPage->setBreadcrumbDetails(_("link")); + + // check validity of things + $oParentDocument = Document::get(KTUtil::arrayGet($_REQUEST, 'fDocumentId')); + if (PEAR::isError($oParentDocument)) { + $this->errorRedirectToMain(_('Invalid parent document selected.')); + exit(0); + } + + $oTargetDocument = Document::get(KTUtil::arrayGet($_REQUEST, 'fTargetDocumentId')); + if (PEAR::isError($oTargetDocument)) { + $this->errorRedirectToMain(_('Invalid target document selected.')); + exit(0); + } + + $oLinkType = LinkType::get(KTUtil::arrayGet($_REQUEST, 'fLinkTypeId')); + if (PEAR::isError($oLinkType)) { + $this->errorRedirectToMain(_('Invalid link type selected.')); + exit(0); + } + + + // create document link + $this->startTransaction(); + + $oDocumentLink =& DocumentLink::createFromArray(array( +/* 'parent_document_id' => $oParentDocument->getId(), + 'child_document_id' => $oTargetDocument->getId(), + 'link_type_id' => $oLinkType->getId(),*/ + 'iParentDocumentId' => $oParentDocument->getId(), + 'iChildDocumentId' => $oTargetDocument->getId(), + 'iLinkTypeId' => $oLinkType->getId(), + )); + + if (PEAR::isError($oDocumentLink)) { + $this->errorRedirectToMain(_('Could not create document link'), sprintf('fDocumentId=%d', $oParentDocument->getId())); + exit(0); + } + + $this->commitTransaction(); + + $this->successRedirectToMain(_('Document link created'), sprintf('fDocumentId=%d', $oParentDocument->getId())); + exit(0); + } + + + // delete a link + function do_delete() { + $this->oPage->setBreadcrumbDetails(_("link")); + + // check security + $oPermission =& KTPermission::getByName('ktcore.permissions.write'); + if (PEAR::isError($oPermission) || + !KTPermissionUtil::userHasPermissionOnItem($this->oUser, $oPermission, $this->oDocument)) { + $this->errorRedirectToMain(_('You do not have sufficient permissions to delete a link'), sprintf("fDocumentId=%d", $this->oDocument->getId())); + exit(0); + } + + + // check validity of things + $oDocumentLink = DocumentLink::get(KTUtil::arrayGet($_REQUEST, 'fDocumentLinkId')); + if (PEAR::isError($oDocumentLink)) { + $this->errorRedirectToMain(_('Invalid document link selected.')); + exit(0); + } + $oParentDocument = Document::get(KTUtil::arrayGet($_REQUEST, 'fDocumentId')); + if (PEAR::isError($oParentDocument)) { + $this->errorRedirectToMain(_('Invalid document selected.')); + exit(0); + } + + // do deletion + $this->startTransaction(); + + $res = $oDocumentLink->delete(); + + if (PEAR::isError($res)) { + $this->errorRedirectToMain(_('Could not delete document link'), sprintf('fDocumentId=%d', $oParentDocument->getId())); + exit(0); + } + + $this->commitTransaction(); + + $this->successRedirectToMain(_('Document link deleted'), sprintf('fDocumentId=%d', $oParentDocument->getId())); + exit(0); + } + + +} + +$oRegistry =& KTPluginRegistry::getSingleton(); +$oRegistry->registerPlugin('KTDocumentLinks', 'ktstandard.documentlinks.plugin', __FILE__); + + + +?> \ No newline at end of file diff --git a/plugins/ktstandard/KTStandardPlugin.php b/plugins/ktstandard/KTStandardPlugin.php index dbb79ff..fc1ff0e 100644 --- a/plugins/ktstandard/KTStandardPlugin.php +++ b/plugins/ktstandard/KTStandardPlugin.php @@ -4,6 +4,8 @@ require_once(KT_LIB_DIR . '/plugins/plugin.inc.php'); require_once('KTSubscriptions.php'); require_once('KTDiscussion.php'); require_once('KTEmail.php'); - require_once('KTIndexer.php'); -require_once('KTWorkflowAssociation.php'); \ No newline at end of file +require_once('KTDocumentLinks.php'); +require_once('KTWorkflowAssociation.php'); + +?> diff --git a/sql/mysql/install/data.sql b/sql/mysql/install/data.sql index 1d3d929..309de05 100644 --- a/sql/mysql/install/data.sql +++ b/sql/mysql/install/data.sql @@ -167,7 +167,7 @@ INSERT INTO `document_transaction_types_lookup` VALUES (16, 'Workflow state tran -- Dumping data for table `document_types_lookup` -- -INSERT INTO `document_types_lookup` VALUES (1, 'Default'); +INSERT INTO `document_types_lookup` VALUES (1, 'Default',0); -- -- Dumping data for table `documents` diff --git a/templates/ktstandard/action/document_links.smarty b/templates/ktstandard/action/document_links.smarty new file mode 100644 index 0000000..42b4524 --- /dev/null +++ b/templates/ktstandard/action/document_links.smarty @@ -0,0 +1,74 @@ +
{i18n}The current links to and from this document are displayed below.{/i18n}
+ + +| + | {i18n}Target{/i18n} | +{i18n}Type{/i18n} | +{i18n}Relationship{/i18n} | +
|---|---|---|---|
| Delete | +getId()}&qs[action]=main">{$target->getName()} | +{$type->getName()} | +Linked from this document | +
| +{if $write_permission} + Delete | +{else} + +{/if} +{$target->getName()} | +{$type->getName()} | +Links to this document | +
| There are no links to or from this document. | |||
{i18n}Select a target document to link to.{/i18n} +
+ + + +{else} ++{i18n}No link types are defined. Please ask the administrator to add them.{/i18n}